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

1.0.2 • Public • Published

Nestjs OpenAI Actions Status Node Version NPM version

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

Readme

Keywords

Package Sidebar

Install

npm i @redredgroup/nestjs-openai

Weekly Downloads

37

Version

1.0.2

License

Apache-2.0

Unpacked Size

27 kB

Total Files

6

Last publish

Collaborators

  • jieey1140