Messages not working when having tool messages between them

Hi my problem is that gpt 3.5 turbo doesn’t remember the last messages. I am using an open source model for function calling which has openai compatibility and then I am appending all of the tool/tool calls messages to the messages list. Then I send that messages list to gpt 3.5 turbo and he doesn’t remember for example what my name was. Can anyone help?

Do you have code we can look at to make sure old messages are appended correctly?

Thanks for the response

messages = [
                {"role": "system", "content": "You have a set of functions tailored for specific types of queries "
                                              "related to Curlypets. Use these functions to ensure accurate and "
                                              "detailed responses. When faced with ambiguous queries (like for "
                                              "example where a state and a questions in connection with a product are "
                                              "metioned/explained) where there is context needed from either the "
                                              "Curlypets product database or the general knowledge base, "
                                              "please retrieve that context by calling the"
                                              "suited function."},
                {"role": "user", "content": "Hi Ich heiße Pascal"},
                {"role": "assistant", "content": "Hi Pascal wie geht es dir!"},
                {"role": "user", "content": user_input}
            ]
    tools = [
        {
            "type": "function",
            "function": {
                "name": "general_knowledge_retrieval",
                "description": """This function accesses a knowledge base, encompassing an extensive array of topics
                    regarding Curlypets. This function is adept at fielding inquiries across a broad spectrum,
                    including company policies, product care tips (for other product related queries rely on the product_data_retrieval function), and general/order/shipping FAQs. Its coverage extends
                    to logistical aspects such as shipping, delivery protocols, exchanges, and returns, alongside
                    customer service concerns. It is not suited for query’s regarding product recommendations or usage
                    questions. It does not have data of users like their last message.""",
                "parameters": {
                    "type": "object",
                    "properties": {
                        "query": {
                            "type": "string",
                            "description": "The user's query, to be used with semantic search. For example if the "
                                           "user input is 'Hallo warte auf meine Bestellung', the query argument "
                                           "should be 'Lieferzeiten'. So the query shouldn't always be the exact same "
                                           "as the user input."
                        }
                    },
                    "required": ["query"],
                },
            },
        },
        {
            "type": "function",
            "function": {
                "name": "product_data_retrieval",
                "description": """Get detailed specifics on Curlypets products, including names, prices, sizes and 
                    basic usage. Ideal for product information queries or questions about a question like 'Wie hält das 
                    Halsband zecken ab', excluding logistics and customer service issues.""",
                "parameters": {
                    "type": "object",
                    "properties": {
                        "query": {
                            "type": "string",
                            "description": "The user's query, to be used with semantic search."
                        }
                    },
                    "required": ["query"],
                },
            },
        },
        {
            "type": "function",
            "function": {
                "name": "general_conversation",
                "description": """This function is specifically designed as a last-resort option for queries that do 
                    not align with the capabilities of other specialized functions within the system. It should be 
                    employed only when no other function can effectively address the user's inquiry. This function is 
                    suited for general conversations, greetings, feedback or questions that fall outside the specific operational domains of 
                    predefined functions. Utilize this function to ensure a responsive and coherent interaction when 
                    other functions are not applicable.""",
                "parameters": {},
            },
        }
    ]
    print(messages)
    chat_completion = client.chat.completions.create(
        model="accounts/fireworks/models/firefunction-v1",
        messages=messages,
        tools=tools,
        temperature=0.0,
        tool_choice="any"
    )

    global reco

    def general_knowledge_retrieval(query: str):
        # dummy function
        return "Dummy response"

    def product_data_retrieval(query: str):
        # dummy function
        return "Dummy response"

    messages[0] = {"role": "system", "content": """You are an assistant named 'Snoopy' have been programmed to 
    respond to customer inquiries for Curlypets, a company dedicated to cat and dog well-being products. Your 
    responses should be short, in a friendly, non-formal German style (that means not using ‘sie’ but using ‘du’), 
    enhanced with 1-2 emojis per response to make the interaction more engaging. When addressing customer questions, 
    rely exclusively on the data provided by the function and by your memory. This is crucial for ensuring accuracy in your responses. 
    If the provided functions do not yield information pertinent to the customer's query, transparently inform the 
    customer that you do not have the necessary information.Keep your responses concise and directly related to the 
    customer's inquiry. Refrain from greeting the user, since it has already been done."""}
    print(messages)
    tool_calls = chat_completion.choices[0].message.tool_calls
    messages.append(
        {
            "content": "None",
            "role": "assistant",
            "tool_calls": tool_calls
        }
    )
    print(messages)
    for tool_call in tool_calls:
        function_name = tool_call.function.name
        function_args = json.loads(tool_call.function.arguments)

        if function_name == "general_knowledge_retrieval":
            function_response = general_knowledge_retrieval(
                query=function_args.get("query")
            )
            messages.append(
                {
                    "tool_call_id": tool_call.id,
                    "role": "tool",
                    "name": function_name,
                    "content": function_response,
                }
            )
        elif function_name == "product_data_retrieval":
            function_response = product_data_retrieval(
                query=function_args.get("query")
            )
            messages.append(
                {
                    "tool_call_id": tool_call.id,
                    "role": "tool",
                    "name": function_name,
                    "content": function_response,
                }
            )
        elif function_name == "general_conversation":
            messages.pop()
    print(messages)
    function_enriched_response = client_openai.chat.completions.create(
        model="gpt-3.5-turbo",
        messages=messages,
        temperature=0.1,
        tools=tools
    )

    # Extract the response content
    res = function_enriched_response.choices[0].message.content

    # Append the assistant's response to the messages
    messages.append({"role": "assistant", "content": res})

    # Prepare the response object
    if "reco" in globals() and reco == "True":
        res_pro['reco'] = "True"
    else:
        res_pro['reco'] = "False"
    res_pro['response'] = res
    res_pro['History'] = str(messages)  # Convert the messages list to a string for the response

    # Return the response
    return jsonify(res_pro)
1 Like