nestjs-open-ai-api
TypeScript icon, indicating that this package has built-in type declarations

0.0.2 • Public • Published

NestJS Open AI API

This is a OpenAI API NestJS wrapper built with ChatGPT.

Installation

npm install nestjs-open-ai-api

Usage

Sync way

@Module({
  imports: [
    OpenAiModule.register({
      apiKey: '...'
    })
  ]
})
export class AppModule {}

Async way

@Module({
  imports: [
    OpenAiModule.registerAsync({
      imports: [ConfigModule.forFeature(openAiLoader)],
      inject: [ConfigService],
      useFactory(configService: ConfigService) {
        const apiKey = this.configService.get<string>('openAi.apiKey')
        return {
          apiKey: configService.apiKey
        }
      }
    })
  ]
})
export class AppModule {}

OpenAiService

@Injectable()
export class AppService {
  constructor(
    private readonly openAiService: OpenAiService
  ) {
    const { data } = await this.openAiService.client.createCompletion({
      model: 'text-davinci-003',
      prompt: "Decide whether a Tweet's sentiment is positive, neutral, or negative.\n\nTweet: \"I loved the new Batma movie!\"\nSentiment:",
      temperature: 0,
      max_tokens: 60,
      top_p: 1,
      frequency_penalty: 0.5,
      presence_penalty: 0,
    });
    return data;
  }
}

Support

Feel free to create pull request.

License

This Nest Module is MIT licensed.

Package Sidebar

Install

npm i nestjs-open-ai-api

Weekly Downloads

0

Version

0.0.2

License

MIT

Unpacked Size

10.6 kB

Total Files

16

Last publish

Collaborators

  • fdorantesm