#API repeating messages

1 messages · Page 1 of 1 (latest)

feral sigil
#

I am creating a chatbot which takes in a pdf and return the answer to the queries asked by the user related to the content, and it is wroking fine. But sometimes the chatbot is repeating the previous answers, which I absolutely want to avoid, what can i do?

Here is my code

const { messages, chatId } = await req.json();
    const previousMessage: string = messages[messages.length - 1].content;

    const content = await fetchAction(api.chatbook.similarContent, {
      chatId,
      query: previousMessage,
    });

    const prompt = {
      role: "system",
      content: `You are a helpful assistant. Your task is to answer questions related to the context provided to you. Please STRICTLY ADHERE to answering questions related to the content only and if a question is unrelated then refrain from giving an answer even if you know the answer and say 'I'm sorry but I do not know the answer to this query as it is unrelated to the content'. You will not apologize for previous responses. And process the response intelligently into markdown formatting for structure, without altering the contents.
  
      START CONTENT BLOCK
      ${content}
      END CONTENT BLOCK
    `,
    };

    const response = await openai.chat.completions.create({
      model: "gpt-3.5-turbo",
      stream: true,
      messages: [
        prompt,
        ...messages.filter((message: Message) => message.role === "user"),
      ],
    });
grand burrowBOT
#

Hi @feral sigil. I'm kapa.ai, your friendly support bot. I will try to answer your question by searching through the knowledge base. :wave:

#

Based on the information provided in the extracts, it seems like the issue might be related to how the context is being managed in the conversation with the chatbot. In the OpenAI API, the context of the conversation is crucial for the model to generate relevant responses. If the model doesn't have the full context, it might generate responses that are repetitive or irrelevant.

In the code you provided, it seems like you're only passing the system message and the user messages to the OpenAI API. If the assistant's previous responses are not included in the messages array, the model won't have the context of its previous responses, which might be why it's repeating itself.

One solution could be to include the assistant's previous responses in the messages array. This way, the model will have the full context of the conversation, including its own previous responses, and it should be less likely to repeat itself.

Here's an example of how you might modify your code, based on a similar issue discussed in the OpenAI Community Forum (https://community.openai.com/t/316958):

const response = await openai.chat.completions.create({
  model: "gpt-3.5-turbo",
  stream: true,
  messages: [
    prompt,
    ...messages,  // Include both user and assistant messages
  ],
});

In this modified code, messages should be an array that includes both the user's messages and the assistant's responses, in the order they occurred in the conversation. Each message should be an object with a role (either "system", "user", or "assistant") and content (the content of the message).

#

Please note that this is just a suggestion based on the information available in the extracts, and you might need to adjust it to fit your specific use case.

Note: you can ask me a follow up question by @ mentioning me again :speech_balloon:

feral sigil
#

@grand burrow
Can you refine this prompt for me?

You are a helpful assistant. Your task is to answer questions related to the context provided to you. Please STRICTLY ADHERE to answering questions related to the content only and if a question is unrelated then refrain from giving an answer even if you know the answer and say 'I'm sorry but I do not know the answer to this query as it is unrelated to the content'. You will not apologize for previous responses. And process the response intelligently into markdown formatting for structure, without altering the contents.

  START CONTENT BLOCK
  ${content}
  END CONTENT BLOCK
grand burrowBOT
#

Let me check the knowledge base for you. One moment! :books:

#

Based on the extracts provided, here's a refined version of your prompt:

{
  role: "system",
  content: `You are a helpful assistant. Your task is to provide answers strictly related to the following content. If a question is unrelated to the content, respond with 'I'm sorry, but I can only provide answers related to the provided content.' Do not apologize for previous responses. Format your responses in markdown for structure, without altering the content.

  ### START CONTENT BLOCK ###
  ${content}
  ### END CONTENT BLOCK ###
  
  Remember to be specific in your responses, provide context where necessary, and answer as if you were an expert on the content. If the reasoning behind an answer is important, include a step-by-step explanation.`
}

This refined prompt includes some of the advice from the OpenAI Cookbook (https://cookbook.openai.com/articles/how_to_work_with_large_language_models#more-prompt-advice) on how to improve model outputs. It encourages the model to be more specific, provide context, and answer as if it was an expert. It also prompts the model to include its reasoning if necessary.

Remember, the more specific and clear your instructions, the better the model can respond.

Note: you can ask me a follow up question by @ mentioning me again :speech_balloon:

#

@feral sigil kapa.ai is still learning and improving, please let us know how it did by reacting below