@platohq/nestjs-openai
TypeScript icon, indicating that this package has built-in type declarations

0.0.3 • Public • Published

@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,
  }),
});

Readme

Keywords

none

Package Sidebar

Install

npm i @platohq/nestjs-openai

Weekly Downloads

3

Version

0.0.3

License

MIT

Unpacked Size

7.85 kB

Total Files

18

Last publish

Collaborators

  • raphaplato
  • renansmoreira_
  • jkernech