mirror of
https://github.com/vitalityAI/therapist.git
synced 2025-04-13 08:10:17 -04:00
chatai + state
This commit is contained in:
parent
c9ad7424e2
commit
fbfc0d014c
48
src/chat.js
48
src/chat.js
|
@ -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
|
|
||||||
}
|
|
Loading…
Reference in New Issue
Block a user