nestjs-open-ai-api
v0.0.2
Published
This is a OpenAI API NestJS wrapper built with ChatGPT.
Downloads
2
Maintainers
Readme
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.