Created
December 12, 2023 20:43
-
-
Save leifermendez/f95556f928b98b9b1c508030abd55f22 to your computer and use it in GitHub Desktop.
chatgpt.class.js (usando el state)
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; |
Author
leifermendez
commented
Dec 12, 2023
disculpa de donde sale la funcion sendFlowSimple?
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