From fbfc0d014c84634801ebb550a54ec845167c55bd Mon Sep 17 00:00:00 2001 From: Claeb101 Date: Sat, 4 Mar 2023 20:47:47 -0500 Subject: [PATCH] chatai + state --- src/chat.js | 48 +++++++++++++++++++----------------------------- 1 file changed, 19 insertions(+), 29 deletions(-) diff --git a/src/chat.js b/src/chat.js index c7ce208..2b14cba 100644 --- a/src/chat.js +++ b/src/chat.js @@ -1,5 +1,6 @@ import { Configuration, OpenAIApi } from "openai"; import * as dotenv from 'dotenv' +import { getMessages } from "./session"; dotenv.config() const configuration = new Configuration({ @@ -8,35 +9,24 @@ const configuration = new Configuration({ const openai = new OpenAIApi(configuration); -class Conversation { - messages; - - constructor(context = "ChatGPT, for the following conversation, please pretend to be a therapist working at a suicide. Respond as if I've called you.") { - this.messages = [ - { - "role": "system", - "content": context - } - ] +export const chat = async (callId) => { + const msgs = await getMessages(callId) + const messages = [ + { + "role": "system", + "content": "ChatGPT, for the following conversation, please pretend to be a therapist working at a suicide. Respond as if I've called you." + } + ] + + for(let msg of msgs){ + messages.push({ + "role": msg.role, + "content": msg.content + }) } - async message(msg) { - this.messages.push({ - "role": "user", - "content": msg - }) - - const res = await openai.createChatCompletion({ - model: "gpt-3.5-turbo", - messages: this.messages - }) - const next = res.data.choices[0].message - this.messages.push(next) - - return next.content - } + await openai.createChatCompletion({ + model: "gpt-3.5-turbo", + messages: messages + }) } - -export { - Conversation -} \ No newline at end of file