npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

nestjs-chatgpt

v1.0.6

Published

NestJS module for interacting with the OpenAI's ChatGPT API

Downloads

414

Readme

ChatGptService

This service allows you to generate text using the OpenAI API. It uses the axios library to make a post request to the OpenAI API with the provided prompt and API key.

The service exports a single method, generateText, which takes an object with a prompt and apiKey property as an argument. The prompt property should be a string containing the text you would like to generate a response for, and the apiKey property should be a string containing your OpenAI API key.

The generateText method returns a promise that resolves to the generated text.

Usage

To use the ChatGptService, you will first need to import it and the CreateChatgptDto:

import { ChatGptService } from 'nestjs-chatgpt';

You will also need to import the Injectable decorator from @nestjs/common:

import { Injectable } from '@nestjs/common';

in your module you have to include the apiKey in forRoot method:

@Module({
  imports: [ChatGptModule.forRoot('Your apiKey')],
})
export class AppModule {}

You can then use the ChatGptService in a controller or another service by injecting it with the @Injectable() decorator:

@Injectable()
export class MyController {
  constructor(private readonly chatGptService: ChatGptService) {}

  async generateText(prompt: string): Promise<string> {
    const createChatgptDto: CreateChatgptDto = { prompt };
    return this.chatGptService.generateTextGPT3(createChatgptDto);
  }
}

@Injectable()
export class MyController {
  constructor(private readonly chatGptService: ChatGptService) {}

  async generateText(prompt: string, model: string, apiKey: string): Promise<string> {
    const createChatgptDto: CreateChatgptDto = { prompt, model };
    return this.chatGptService.generateText(createChatgptDto);
  }
}

You can then use the generateText method in your controller or service to generate text for the provided prompt:

@Injectable()
const generatedTextGPT3 = await this.generateTextGPT3('What is the meaning of life?', 'my-api-key');
console.log(generatedText);
@Injectable()
const generatedTextGPT3 = await this.generateText('What is the meaning of life?', 'text-davinci-003','my-api-key');
console.log(generatedText);

Error Handling

The generateText method throws a HttpException if there is an error with generating the text.

It is recommended to catch the exception and handle it appropriately in your controller or service.

try {
  const generatedText = await this.generateText('What is the meaning of life?', 'my-api-key');
  console.log(generatedText);
} catch (error) {
  if (error instanceof HttpException) {
    console.log(`Error generating text: ${error.message}`);
  }
}

```