Skip to content

Instantly share code, notes, and snippets.

@leifermendez
Created December 12, 2023 20:43
Show Gist options
  • Save leifermendez/f95556f928b98b9b1c508030abd55f22 to your computer and use it in GitHub Desktop.
Save leifermendez/f95556f928b98b9b1c508030abd55f22 to your computer and use it in GitHub Desktop.
chatgpt.class.js (usando el state)
const { CoreClass } = require('@bot-whatsapp/bot');
class ChatGPTClass extends CoreClass {
queue = [];
optionsGPT = { model: "gpt-3.5-turbo" };
openai = undefined;
constructor(_database, _provider) {
super(null, _database, _provider)
this.init().then();
}
/**
* Esta funciona inicializa
*/
init = async () => {
const { ChatGPTAPI } = await import("chatgpt");
this.openai = new ChatGPTAPI(
{
apiKey: process.env.OPENAI_API_KEY
}
);
};
handleMsg = async (ctx) => {
const state = {
getMyState: this.stateHandler.getMyState(ctx.from),
get: this.stateHandler.get(ctx.from),
getAllState: this.stateHandler.getAllState,
update: this.stateHandler.updateState(ctx),
clear: this.stateHandler.clear(ctx.from),
}
const {from, body} = ctx
const conversationList = state.getMyState()?.conversationList ?? []
const interaccionChatGPT = await this.openai.sendMessage(body,
{
conversationId: !conversationList.length
? undefined
: conversationList[conversationList.length - 1].conversationId,
parentMessageId: !conversationList.length
? undefined
: conversationList[conversationList.length - 1].id,
});
conversationList.push(interaccionChatGPT);
const parseMessage = {
...interaccionChatGPT,
answer: interaccionChatGPT.text
}
await state.update({threadId:null, conversationList})
this.sendFlowSimple([parseMessage], from)
}
}
module.exports = ChatGPTClass;
@leifermendez
Copy link
Author

require('dotenv').config()
const { createProvider } = require('@bot-whatsapp/bot')

const BaileysProvider = require('@bot-whatsapp/provider/baileys')
const MockAdapter = require('@bot-whatsapp/database/mock')
const ChatGPTClass = require('./chatgpt.class')


const createBotAI = ({provider, database}) => {
    return new ChatGPTClass(database, provider)
}

const main = async () => {
    const adapterDB = new MockAdapter()

    const adapterProvider = createProvider(BaileysProvider)

     createBotAI({
        provider: adapterProvider,
        database: adapterDB,
    })
}

main()

@abelacco
Copy link

disculpa de donde sale la funcion sendFlowSimple?

@leifermendez
Copy link
Author

disculpa de donde sale la funcion sendFlowSimple?

esa funcion ya existe en "CoreClass " es extendida

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment