Skip to content

Commit

Permalink
OV-39: + open-ai service
Browse files Browse the repository at this point in the history
  • Loading branch information
sergiy4 committed Aug 22, 2024
1 parent c51b15e commit 2b5a507
Show file tree
Hide file tree
Showing 2 changed files with 48 additions and 0 deletions.
43 changes: 43 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,43 @@
import OpenAI, { type OpenAI as OpenAIType } from 'openai';

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

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

type Constructor = {
config: BaseConfig;
};

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

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

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

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

const generatedText = completion.choices[0]?.message.content || '';
return { generatedText };
}
}

export { OpenAIService };
5 changes: 5 additions & 0 deletions backend/src/common/services/services.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,10 @@
import { config } from '~/common/config/config.js';

import { CryptService } from './crypt/crypt.service.js';
import { OpenAIService } from './open-ai/open-ai.service.js';

const openAIService = new OpenAIService({ config });
const cryptService = new CryptService();

export { cryptService };
export { openAIService };

0 comments on commit 2b5a507

Please sign in to comment.