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

OV-39: integrate Open AI #53

Merged
merged 14 commits into from
Aug 28, 2024
1 change: 1 addition & 0 deletions backend/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@
"fastify": "4.28.1",
"knex": "3.1.0",
"objection": "3.1.4",
"openai": "4.56.0",
"pg": "8.12.0",
"pino": "9.3.2",
"pino-pretty": "10.3.1",
Expand Down
6 changes: 6 additions & 0 deletions backend/src/common/config/base-config.package.ts
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,12 @@ class BaseConfig implements Config {
env: 'PORT',
default: null,
},
OPEN_AI_KEY: {
doc: 'Key for Open Ai',
format: String,
env: 'OPEN_AI_KEY',
default: null,
},
},
DB: {
CONNECTION_STRING: {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ type EnvironmentSchema = {
APP: {
PORT: number;
ENVIRONMENT: ValueOf<typeof AppEnvironment>;
OPEN_AI_KEY: string;
};
DB: {
CONNECTION_STRING: string;
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
const CHAT_MODEL = 'gpt-4o-mini';

export { CHAT_MODEL };
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
export { CHAT_MODEL } from './chat-model.constant.js';
export { MAX_TOKEN } from './max-token.constant.js';
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
const MAX_TOKEN = 16_000;

export { MAX_TOKEN };
1 change: 1 addition & 0 deletions backend/src/common/services/open-ai/libs/enums/enums.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export { OpenAIRole } from './open-ai-role.enum.js';
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
const OpenAIRole = {
USER: 'user',
SYSTEM: 'system',
ASSISTANT: 'assistant',
} as const;

export { OpenAIRole };
10 changes: 10 additions & 0 deletions backend/src/common/services/open-ai/libs/types/message.type.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
import { type ValueOf } from 'shared';

import { type OpenAIRole } from '../enums/enums.js';

type Message = {
role: ValueOf<typeof OpenAIRole>;
content: string;
};

export { type Message };
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
import { type Message } from './message.type.js';

type OpenAIService = {
generateText(messages: Message[]): Promise<string>;
};

export { type OpenAIService };
2 changes: 2 additions & 0 deletions backend/src/common/services/open-ai/libs/types/types.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
export { type Message } from './message.type.js';
export { type OpenAIService } from './open-ai-service.type.js';
41 changes: 41 additions & 0 deletions backend/src/common/services/open-ai/open-ai.service.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
import OpenAI from 'openai';

import { type BaseConfig } from '~/common/config/base-config.package.js';

import { CHAT_MODEL, MAX_TOKEN } from './libs/constants/constants.js';
import {
type Message,
type OpenAIService as OpenAIServiceModule,
} from './libs/types/types.js';

type Constructor = {
config: BaseConfig;
};

class OpenAIService implements OpenAIServiceModule {
private config: BaseConfig;
private openAi: OpenAI;

public constructor({ config }: Constructor) {
this.config = config;
this.openAi = this.initOpenAi();
}

private initOpenAi = (): OpenAI => {
return new OpenAI({
apiKey: this.config.ENV.APP.OPEN_AI_KEY,
});
};

public async generateText(messages: Message[]): Promise<string> {
const completion = await this.openAi.chat.completions.create({
messages,
model: CHAT_MODEL,
max_tokens: MAX_TOKEN,
});

return completion.choices[0]?.message.content || '';
}
}

export { OpenAIService };
170 changes: 167 additions & 3 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading