-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
bec5878
commit fe6fb16
Showing
4 changed files
with
90 additions
and
12 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
import { IsNumber, IsString } from 'class-validator'; | ||
|
||
export class TextGenerationDto { | ||
@IsString() | ||
prompt: string; | ||
|
||
@IsString() | ||
model: string = 'gpt-4o'; | ||
|
||
@IsNumber() | ||
temperature: number = 0.7; | ||
|
||
@IsNumber() | ||
max_tokens: number = 300; | ||
|
||
constructor(partial: Partial<TextGenerationDto>) { | ||
Object.assign(this, partial); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
import { Injectable, Logger, OnModuleInit } from '@nestjs/common'; | ||
import { OpenAI } from 'openai'; | ||
import { TextGenerationDto } from './dto/text-generation.dto'; | ||
import { OpenaiSecretsService } from './openai-secrets.service'; | ||
import { ChatCompletion } from 'openai/src/resources/chat/completions'; | ||
|
||
@Injectable() | ||
export class OpenaiTextGenerationService implements OnModuleInit { | ||
private openai: OpenAI; | ||
|
||
constructor( | ||
private readonly _openAiSecrets: OpenaiSecretsService, | ||
private readonly _logger: Logger, | ||
) {} | ||
|
||
async onModuleInit() { | ||
try { | ||
const apiKey = await this._openAiSecrets.getOpenaiApiKey(); | ||
this.openai = new OpenAI({ apiKey: apiKey }); | ||
} catch (error) { | ||
this._logger.error('Failed to initialize OpenAI service.', error); | ||
throw error; | ||
} | ||
} | ||
|
||
public async generateText(dto: TextGenerationDto): Promise<string> { | ||
try { | ||
const { prompt, temperature, max_tokens, model } = dto; | ||
this._logger.log(`Generating text...`); | ||
const chatCompletionResponse: ChatCompletion = | ||
await this.openai.chat.completions.create({ | ||
model, | ||
temperature, | ||
max_tokens, | ||
messages: [{ role: 'system', content: prompt }], | ||
}); | ||
|
||
this._logger.log(`Text generation complete.`); | ||
return chatCompletionResponse.choices[0].message.content; | ||
} catch (error) { | ||
this._logger.error('Failed to generate image.', error); | ||
throw error; | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters