This is a OpenAI API NestJS wrapper built with ChatGPT.
npm install nestjs-open-ai-api
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;
}
}
Feel free to create pull request.
This Nest Module is MIT licensed.