greenfield-hippos / backend

0 stars 1 forks source link

How do we manage context and previous messages inside the same conversation when calling OpenAI api #29

Open vicentbe10 opened 2 hours ago

vicentbe10 commented 2 hours ago

For an idea context we should input an array of all questions and answers in each request. As this is not possible for long conversations (there is a limit of tokens to be used as input) the usual practice is to limit the number of previous interactions. We could use the last 5 for example.

So before sending the request we should prepare a conversation history that after will pass to the "message" property.

// Prepare the conversation history
  const conversation = [
    {
      role: 'system',
      content:
        'You are an expert programming tutor. Help students understand coding concepts without giving direct code examples. Use clear explanations.',
    },
    ...recentMessages.map((message) => ({
      role: message.sender === 'user' ? 'user' : 'assistant',
      content: message.content,
    })),
    {
      role: 'user',
      content: content,
    },
  ];
vicentbe10 commented 2 hours ago

@laurencecnerual Added in assignees so you can check and ask questions here if necessary. Thanks!