Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Only initialize openAi instance if AI endpoint is called #1119

Merged
merged 4 commits into from
Nov 14, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,12 @@

All notable changes to this project will be documented in this file.

## [v0.58.1](https://github.com/serlo/api.serlo.org/compare/v0.58.0..v0.58.1) - November 14, 2023

### Fixed

- Only initialize openAi instance if AI endpoint is called

## [v0.58.0](https://github.com/serlo/api.serlo.org/compare/v0.57.11..v0.58.0) - November 13, 2023

### Changed
Expand Down
2 changes: 2 additions & 0 deletions __config__/jest.setup.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@ import {
setup,
} from './setup'

process.env.OPENAI_API_KEY = 'fake-test-key-we-are-mocking-responses'

jest.mock('@google-cloud/storage', () => {
return {
Storage: jest.fn().mockImplementation(() => ({
Expand Down
2 changes: 1 addition & 1 deletion lerna.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,5 +3,5 @@
"packages/*"
],
"npmClient": "yarn",
"version": "0.58.0"
"version": "0.58.1"
}
2 changes: 1 addition & 1 deletion packages/server/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@serlo/api.serlo.org",
"version": "0.58.0",
"version": "0.58.1",
"private": true,
"repository": "serlo/api.serlo.org",
"license": "Apache-2.0",
Expand Down
23 changes: 20 additions & 3 deletions packages/server/src/model/ai.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,19 @@ import { APIError, OpenAI } from 'openai'

import { UserInputError } from '~/errors'

const openai = new OpenAI({
apiKey: process.env.OPENAI_API_KEY,
})
// singleton instance so that it doesn't have to be reinitialized on every
// request
let openai: OpenAI | undefined

function getOpenAIInstance() {
if (process.env.OPENAI_API_KEY !== undefined && openai === undefined) {
openai = new OpenAI({
apiKey: process.env.OPENAI_API_KEY,
})
}

return openai
}

interface ExecutePromptParams {
prompt: string
Expand All @@ -24,6 +34,13 @@ async function executePrompt({
}

try {
const openai = getOpenAIInstance()
if (!openai) {
throw new Error(
'OpenAI instance could not be created due to missing API key.',
)
}

const response = await openai.chat.completions.create({
model: 'gpt-4-1106-preview',
messages: [
Expand Down
5 changes: 5 additions & 0 deletions scripts/changelog.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1629,6 +1629,11 @@ async function exec(): Promise<void> {
changed: ['remove license queries, simplify `License` type'],
internal: ['Move content generation service logic to api'],
},
{
tagName: 'v0.58.1',
date: '2023-11-14',
fixed: ['Only initialize openAi instance if AI endpoint is called'],
},
],
})

Expand Down