Skip to content

fdorantesm/nestjs-open-ai-api

Repository files navigation

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.

About

A Nestjs Open AI Module

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published