0.0.3 • Published 1 year ago

@platohq/nestjs-openai v0.0.3

Weekly downloads
-
License
MIT
Repository
github
Last release
1 year ago

@platohq/nestjs-openai

The NestJS module for OpenAI based on the official openai package

How to install

npm install @platohq/nestjs-openai

or

yarn add @platohq/nestjs-openai

How to use

Register the module

import { OpenAIModule } from '@platohq/nestjs-openai';

@Module({
  imports: [
    OpenAIModule.register({
      apiKey: process.env.OPENAI_API_KEY,
    }),
  ],
})
export class AppModule {
}

Inject the service

import { OpenAIClient } from '@platohq/nestjs-openai';

@Injectable()
export class AppService {
  constructor(private readonly openAIClient: OpenAIClient) {
  }

  async createCompletion(prompt: string) {
    const { data } = await this.openAIClient.createCompletion({
      model: 'text-davinci-002',
      max_tokens: 1000,
      temperature: 0.8,
      prompt,
    });
  
    return data;
  }
}

Async options

Quite often you might want to asynchronously pass your module options instead of passing them beforehand. In such case, use registerAsync() method, that provides a couple of various ways to deal with async data.

Use factory

OpenAIModule.registerAsync({
  useFactory: () => ({
    apiKey: process.env.OPENAI_API_KEY,
  }),
});