The code below is working. I can curl questions to ChatGPT and it replies on a one-off basis. However, if I try to engage in a conversation that require the state of the previous submissions to be referenced, the chat can not follow.
I would like to know what I need to do (and the code needed) to retain the context of the conversation
const express = require("express");
const cors = require("cors");
const bodyParser = require("body-parser");
const { Configuration, OpenAIApi } = require("openai");
const configuration = new Configuration({
apiKey: "sk-my-key",
});
const openai = new OpenAIApi(configuration);
// Set up the server
const app = express();
app.use(bodyParser.json());
app.use(cors())
// Set up the ChatGPT endpoint
app.post("/chat", async (req, res) => {
// Get the prompt from the request
const { prompt } = req.body;
// Generate a response with ChatGPT
const completion = await openai.createCompletion({
model: "text-davinci-002",
prompt: prompt,
});
res.send(completion.data.choices[0].text);
});
// Start the server
const port = 8080;
app.listen(port, () => {
console.log(`Server listening on port ${port}`);
});
CURL being run in new terminal:
curl -X POST -H "Content-Type: application/json" -d '{"prompt":"Hello, how are you doing today?"}' http://localhost:8080/chat