common-chatgpt
is a Node.js module that provides an easy way to integrate OpenAI's powerful language model, GPT-3, into your any chatbot or conversational AI project.
- Setup your ChatGPT in just
2
lines 😎
It also includes types for models
provided by ChatGPT, so no need to remember all the Model names
- Examples
- Installation
- Usage
- Discord
- Default Configs
npm i generic-chatgpt
whatsapp-web.js - Javascript
const { Client } = require('whatsapp-web.js'); // -------------|
const { Chat } = require('common-chatgpt'); // |
const qrcode = require('qrcode-terminal'); // |-- Client Setup
// |
const client = new Client(); // -------------------------------|
// setup `chat` 1st `LINE`
const chat = new Chat({
apiKey: process.env.CHAT_GPT_KEY,
// ... see configurations and default values below
});
client.on('qr', (qr) => { // ----------------------------------|
qrcode.generate(qr, { small: true }); // |
console.log('QR RECEIVED', qr); // |-- For getting QR, check `whatsapp-web.js`
// |
}); // --------------------------------------------------------|
client.on('message_create', async (msg) => {
if (msg.id.remote === process.env.WA_CHAT_ID && !msg.fromMe) {
// get response in single line 2nd `LINE`
const chatgpt_response = await chat.getMessage(msg.body, msg.author);
// send it anywhere
client.sendMessage(process.env.WA_CHAT_ID, chatgpt_response);
}
});
client.initialize();
whatsapp-web.js - Typescript
import * as WAWebJS from 'whatsapp-web.js'; // ----------------|
import { Client } from 'whatsapp-web.js'; // |
import qrcode from 'qrcode-terminal'; // |-- Client Setup
import { Chat } from 'common-chatgpt'; // |
const client = new Client({}); // -----------------------------|
// setup `chat` 1st `LINE`
const chat = new Chat({
apiKey: process.env.CHAT_GPT_KEY as string,
// ... see configurations and default values below
});
client.on('qr', (qr: string) => { // --------------------------|
qrcode.generate(qr, { small: true }); // |
console.log('QR RECEIVED', qr); // |-- For getting QR, check `whatsapp-web.js`
// |
}); // --------------------------------------------------------|
client.on('message_create', async (msg: WAWebJS.Message) => {
if (msg.id.remote === (process.env.WA_CHAT_ID as string) && !msg.fromMe) {
// get response in single line 2nd `LINE`
const chatgpt_response = await chat.getMessage(msg.body, msg.author ?? '');
// send it anywhere
client.sendMessage(process.env.WA_CHAT_ID as string, chatgpt_response);
}
});
client.initialize();
const { Client, Events, GatewayIntentBits } = require('discord.js');
const { Chat } = require('common-chatgpt'); // |
require('dotenv/config'); // |
// |
const client = new Client({ // |
intents: [ // |
GatewayIntentBits.Guilds, // |
GatewayIntentBits.GuildMessages, // |
GatewayIntentBits.MessageContent, // |
], // |-- Client Setup
}); // |
// |
client.once(Events.ClientReady, (clientUser) => { // |
console.log(`Logged in as ${clientUser.user.tag}`); // |
}); // |
// |
const BOT_CHANNEL = '1075363634479374376'; // |
// |
client.login(process.env.BOT_TOKEN); // -----------------------|
// setup `chat` 1st `LINE`
const chat = new Chat({
apiKey: process.env.CHAT_GPT_KEY as string,
// ... see configurations and default values below
});
client.on(Events.MessageCreate, async (message) => {
if (message.author.bot) return;
if (message.channel.id !== BOT_CHANNEL) return;
// get response in single line 2nd `LINE`
const chatgpt_response = await chat.getMessage(
message.content,
message.member.displayName
);
// send it anywhere
await message.channel.send(chatgpt_response);
});
type Model = 'text-davinci-003' | 'text-curie-001' | 'text-babbage-001' | 'text-ada-001';
{
model: "text-davinci-003",
max_token: 300,
temprature: 0.8,
}
We welcome contributions from anyone who is interested in improving common-chatgpt
. To get started, please follow these steps:
- Fork this repository to your own account.
- Create a new branch with a descriptive name (
git checkout -b my-new-feature
). - Implement your new feature or bug fix.
- Commit your changes with commit messages (
git commit -am 'Add some feature'
). - Push your branch to your fork (
git push origin my-new-feature
). - Submit a pull request
Email: duggalpiyush0@gmail.com Checkout my other projects.
The MIT License (MIT)
Copyright (c) 2023 Piyush Duggal