mirror of
https://github.com/vitalityAI/therapist.git
synced 2025-04-16 17:40:16 -04:00
42 lines
873 B
JavaScript
42 lines
873 B
JavaScript
import { Configuration, OpenAIApi } from "openai";
|
|
import * as dotenv from 'dotenv'
|
|
dotenv.config()
|
|
|
|
const configuration = new Configuration({
|
|
apiKey: process.env.OPENAI_API_KEY,
|
|
});
|
|
|
|
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
|
|
}
|
|
]
|
|
}
|
|
|
|
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
|
|
}
|
|
}
|
|
|
|
export {
|
|
Conversation
|
|
} |