mirror of
https://github.com/vitalityAI/therapist.git
synced 2025-04-16 17:40:16 -04:00
chatgpt conversations
This commit is contained in:
parent
96f2d2ea69
commit
4e6c94fbfa
72
chat.js
72
chat.js
|
@ -5,60 +5,38 @@ dotenv.config()
|
||||||
const configuration = new Configuration({
|
const configuration = new Configuration({
|
||||||
apiKey: process.env.OPENAI_API_KEY,
|
apiKey: process.env.OPENAI_API_KEY,
|
||||||
});
|
});
|
||||||
console.log(process.env.OPENAI_API_KEY)
|
|
||||||
const openai = new OpenAIApi(configuration);
|
const openai = new OpenAIApi(configuration);
|
||||||
|
|
||||||
export default async function (req, res) {
|
class Conversation {
|
||||||
if (!configuration.apiKey) {
|
messages;
|
||||||
res.status(500).json({
|
|
||||||
error: {
|
constructor(context = "ChatGPT, for the following conversation, please pretend to be a therapist working at a suicide. Respond as if I've called you.") {
|
||||||
message: "OpenAI API key not configured, please follow instructions in README.md",
|
this.messages = [
|
||||||
|
{
|
||||||
|
"role": "system",
|
||||||
|
"content": context
|
||||||
}
|
}
|
||||||
});
|
]
|
||||||
return;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
const animal = req.body.animal || '';
|
async message(msg) {
|
||||||
if (animal.trim().length === 0) {
|
this.messages.push({
|
||||||
res.status(400).json({
|
"role": "user",
|
||||||
error: {
|
"content": msg
|
||||||
message: "Please enter a valid animal",
|
})
|
||||||
}
|
|
||||||
});
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
try {
|
const res = await openai.createChatCompletion({
|
||||||
const completion = await openai.createCompletion({
|
model: "gpt-3.5-turbo",
|
||||||
model: "text-davinci-003",
|
messages: this.messages
|
||||||
prompt: generatePrompt(animal),
|
})
|
||||||
temperature: 0.6,
|
const next = res.data.choices[0].message
|
||||||
});
|
this.messages.push(next)
|
||||||
res.status(200).json({ result: completion.data.choices[0].text });
|
|
||||||
} catch (error) {
|
return next.content
|
||||||
// Consider adjusting the error handling logic for your use case
|
|
||||||
if (error.response) {
|
|
||||||
console.error(error.response.status, error.response.data);
|
|
||||||
res.status(error.response.status).json(error.response.data);
|
|
||||||
} else {
|
|
||||||
console.error(`Error with OpenAI API request: ${error.message}`);
|
|
||||||
res.status(500).json({
|
|
||||||
error: {
|
|
||||||
message: 'An error occurred during your request.',
|
|
||||||
}
|
|
||||||
});
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
function generatePrompt(animal) {
|
export {
|
||||||
const capitalizedAnimal =
|
Conversation
|
||||||
animal[0].toUpperCase() + animal.slice(1).toLowerCase();
|
|
||||||
return `Suggest three names for an animal that is a superhero.
|
|
||||||
Animal: Cat
|
|
||||||
Names: Captain Sharpclaw, Agent Fluffball, The Incredible Feline
|
|
||||||
Animal: Dog
|
|
||||||
Names: Ruff the Protector, Wonder Canine, Sir Barks-a-Lot
|
|
||||||
Animal: ${capitalizedAnimal}
|
|
||||||
Names:`;
|
|
||||||
}
|
}
|
|
@ -5,7 +5,8 @@
|
||||||
"private": true,
|
"private": true,
|
||||||
"scripts": {
|
"scripts": {
|
||||||
"chat": "node chat.js",
|
"chat": "node chat.js",
|
||||||
"call": "node call.js"
|
"call": "node call.js",
|
||||||
|
"test": "node test.js"
|
||||||
},
|
},
|
||||||
"dependencies": {
|
"dependencies": {
|
||||||
"express": "^4.18.2",
|
"express": "^4.18.2",
|
||||||
|
|
11
test.js
Normal file
11
test.js
Normal file
|
@ -0,0 +1,11 @@
|
||||||
|
import { Conversation } from "./chat.js"
|
||||||
|
|
||||||
|
const main = async () => {
|
||||||
|
let convo = new Conversation()
|
||||||
|
let res = await convo.message("I'm just feeling sad.")
|
||||||
|
console.log("RESPONSE", res)
|
||||||
|
res = await convo.message("Yeah. My parents got divorced last week, and I think it's my fault.")
|
||||||
|
console.log("RESPONSE", res)
|
||||||
|
}
|
||||||
|
|
||||||
|
main()
|
Loading…
Reference in New Issue
Block a user