mistralai / mistral-inference

Official inference library for Mistral models
https://mistral.ai/
Apache License 2.0
9.58k stars 845 forks source link

[MISTRAL AI ERROR] Mistral AI responding with Unexpected role RoleEnum.tool error #135

Closed muhammadfaizan027915 closed 6 months ago

muhammadfaizan027915 commented 6 months ago

I am using Mistral AI client in my javascript project and Mistral Ai is giving me a tool_calls response. When I call the tool and push response in messages array. And Then I call a send a second request to the Mistral AI it gives me this error: MistralAPIError: HTTP error! status: 400 Response: {"object":"error","message":"Unexpected role RoleEnum.tool","type":"invalid_request_error","param":null,"code":null}

My messages array contains: Messages: [ { "role": "user", "name": "User", "content": "give me random cat image\n\n" }, { "role": "tool", "name": "RandomCatImage", "content": "{"data":{"id":"cpq","url":"[https://cdn2.thecatapi.com/images/cpq.png/",/"width/":1024,/"height/":765%7D],/"success/":true,/"message/":/"Successfully accomplished!"}" } ]

My implementation for function calling:

for (const tool of toolCalls) {
      const functionName = tool.function.name;
      const functionArguments = JSON.parse(tool.function.arguments);

      const desiredTool = httpTools?.find((tool) => (tool?.config as HttpToolSchema["config"])?.name?.replaceAll(" ", "") === functionName);
      const httpResponse = await httpToolHandler(desiredTool?.config as HttpToolSchema["config"])(functionArguments);

      messages?.push({ role: "tool", name: functionName, content: httpResponse });
    }

I am unable to get chat response from Mistral AI after calling a tool. Screenshot_1

muhammadfaizan027915 commented 6 months ago

I have found the bug causing this error.

rohitashPrajapati commented 6 months ago

I am also facing the same issue, what were your findings?