#Help me with chatgpt code

2 messages · Page 1 of 1 (latest)

limpid violet
#

Hi. I have modified the code for chatbot so the chatbot can remember the last three messages in a chat and respond from this if you for example ask: tell me more. But i have one problem. The chatbot cant differentiate the chat conversations from different users. It mixes together chats from different sessions. Can someone fix it?

import express from 'express'
import * as dotenv from 'dotenv'
import cors from 'cors'
import { Configuration, OpenAIApi } from 'openai'

dotenv.config()

const configuration = new Configuration({
apiKey: process.env.OPENAI_API_KEY,
});

const openai = new OpenAIApi(configuration);

const app = express()
app.use(cors())
app.use(express.json())

app.get('/', async (req, res) => {
res.status(200).send({
message: 'Hei. Hva kan jeg hjelpe med?'
})
})

let previousInformation = {};

app.post('/', async (req, res) => {
try {
const userId = req.body.userId;
const prompt = req.body.prompt;

// Lagre de tre siste meldingene fra brukeren i en array
if (!previousInformation[userId]) {
    previousInformation[userId] = { lastMessages: []};
}
previousInformation[userId].lastMessages.push(prompt);
if (previousInformation[userId].lastMessages.length > 3) {
    previousInformation[userId].lastMessages.shift();
}
// Inkludere de tre siste meldingene i prompt-parameteren
let newPrompt = ${previousInformation[userId].lastMessages.join(" ")} Spørsmål: ${prompt};

const response = await openai.createCompletion({
  model: "text-davinci-003",
  prompt: newPrompt,
  temperature: 0,
  max_tokens: 500,
  top_p: 1,
  frequency_penalty: 0.5,
  presence_penalty: 0,
});

res.status(200).send({
  bot: response.data.choices[0].text
});

} catch (error) {
console.error(error)
res.status(500).send(error || 'Something went wrong');
}
});

app.listen(5000, () => console.log('AI server started on http://localhost:5000/'))

rigid plinth
#

I don‘t know how this app is structured, but from the logic you‘d need to store all conversations and there last 3 messages separately and then need to either have an element to click to choose the conversation you want to continue or have any other user-Input to make your choice