Unable to maintain user session in open ai Chat completion API

I ran into this issue last night and solved it: as others are saying you need to store some user identifier on the server side where you send your requests. A solution to this is to store a dictionary of users (sessionId) and their message history. Here is how you would do it in React and NodeJs:

React

const sessionId = uuidv4();
  const data = {
    userSession: sessionId,
    prompt: prompt,
  };

  const handleChatCompletion = async () => {
    try {
      setIsLoading(true);
      const apiResponse = await fetch(
        "api-endpoint/chat/completions",
        {
          method: "POST",
          headers: {
            "Content-Type": "application/json",
          },
          body: JSON.stringify(data),
        }
      );

      const result = await apiResponse.text();
      setResponse(result);
    } catch (error) {
      console.error("Error:", error);
    } finally {
      setIsLoading(false);
    }
  };

NodeJS

let userMemories = {};
app.post("/api/chat-completions", async (req, res) => {
  try {
    const { userSession, prompt } = req.body;
    console.log("User prompt: ", prompt, userSession);

    let currentUserMessages = userMemories[userSession] || [];

    const messages = [
      { role: "system", content: "You are a helpful assistant." },
      ...currentUserMessages.map((msg) => ({ role: "user", content: msg })),
      { role: "user", content: prompt }, // Add the current prompt to the end
    ];

    currentUserMessages.push(prompt); // Push the current prompt to memory
    userMemories[userSession] = currentUserMessages; // Update the memory for the current userSession

    const response = await axios.post(
      "https://api.openai.com/v1/chat/completions",
      {
        model: "gpt-3.5-turbo",
        messages: messages,
      },
      {
        headers: {
          "Content-Type": "application/json",
          Authorization: `Bearer ${openaiApiKey}`,
        },
      }
    );

    console.log("Response:\n", response.data.choices[0].message.content);
    const formattedResponse = formatResponse(
      response.data.choices[0].message.content
    );

    res.send(formattedResponse);
  } catch (error) {
    console.error(
      "Error:",
      error.response ? error.response.data : error.message
    );
    res.status(500).json({ error: "Internal Server Error" });
  }
});

// Route to clear the history
app.post("/api/clear-history", (req, res) => {
  const userSession = req.body.sessionId;
  userMemories[userSession] = []; // Reset the memory array to an empty array
  console.log("Conversation cleared for user: ", userSession);
  res.send("Conversation history cleared.");
});