@redredgroup/nestjs-openai
v1.0.2
Published
OpenAI integration to a NestJS application
Downloads
63
Readme
Nestjs OpenAI
Introduction
This package is a module that converts the asynchronous OpenAI to Nestjs.
Installation
using npm
npm install @redredgroup/nestjs-openai
using yarn
yarn install @redredgroup/nestjs-openai
using pnpm
pnpm add @redredgroup/nestjs-openai
Import module
import { Module } from '@nestjs/common';
import { OpenAIModule } from '@redredgroup/nestjs-openai';
@Module({
imports: [
OpenAIModule.forRoot({
options: {
apiKey: 'OPENAI_API_KEY',
//other openai Options
},
}),
],
})
export class AppModule {}
//Or the forRootAsync module using @nestjs/Config
import { Module } from '@nestjs/common';
import { OpenAIModule } from '@redredgroup/nestjs-openai';
import { ConfigModule, ConfigService } from '@nestjs/config';
@Module({
imports: [
OpenAIModule.forRootAsync({
imports: [ConfigModule],
inject: [ConfigService],
useFactory: (configService: ConfigService) => ({
options: {
apiKey: configService.get('OPENAI_API_KEY'),
//other openai Options
},
}),
}),
],
})
export class AppModule {}
Output 4 nicknames similar to the injected nickname
import { Injectable } from '@nestjs/common';
import { OpenAIService } from '@redredgroup/nestjs-openai';
import { ChatCompletion } from 'openai/resources';
@Injectable()
export class AppService {
constructor(private readonly openAiService: OpenAIService) {}
async generateRandomNickname(nickname: string): Promise<ChatCompletion> {
const chatCompletion = await this.openAiService.chat.completions.create({
messages: [
{
role: 'system',
content: 'You are a helpful assistant that generates nicknames.',
},
{
role: 'user',
content: `Generate 4 nicknames similar to "${nickname}"`,
},
],
model: 'gpt-3.5-turbo',
});
return chatCompletion;
}
}
Copyright
© 2023 REDREDGROUP Software. All Right Reserved.
License
Apache-2.0