chatai + state

This commit is contained in:
Claeb101 2023-03-04 20:47:47 -05:00
parent c9ad7424e2
commit fbfc0d014c

View File

@ -1,5 +1,6 @@
import { Configuration, OpenAIApi } from "openai"; import { Configuration, OpenAIApi } from "openai";
import * as dotenv from 'dotenv' import * as dotenv from 'dotenv'
import { getMessages } from "./session";
dotenv.config() dotenv.config()
const configuration = new Configuration({ const configuration = new Configuration({
@ -8,35 +9,24 @@ const configuration = new Configuration({
const openai = new OpenAIApi(configuration); const openai = new OpenAIApi(configuration);
class Conversation { export const chat = async (callId) => {
messages; const msgs = await getMessages(callId)
const 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": "ChatGPT, for the following conversation, please pretend to be a therapist working at a suicide. Respond as if I've called you."
"role": "system", }
"content": context ]
}
] for(let msg of msgs){
messages.push({
"role": msg.role,
"content": msg.content
})
} }
async message(msg) { await openai.createChatCompletion({
this.messages.push({ model: "gpt-3.5-turbo",
"role": "user", messages: messages
"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
}
} }
export {
Conversation
}