Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[BUG] Problem with the user and assistant roles when using the Mistarl api. #757

Open
Neuro-Storm opened this issue Feb 23, 2025 · 3 comments
Labels
bug Something isn't working

Comments

@Neuro-Storm
Copy link

Describe the bug
When using the Mistarl API via LiteLLM or OpenAIServerModel, an error occurs due to the fact that the Mistral API expects a message with the User or Tool role, and receives it with the Assistant role.

Code to reproduce the error
I used this code:
model_mistral = LiteLLMModel(
model_id="mistral/mistral-large-latest",
api_key=mistral_api_key,
timeout=29,
)

and such

model_mistral_openAI = OpenAIServerModel(
model_id="mistral-large-latest",
api_base="https://api.mistral.ai/v1",
api_key=mistral_api_key,
)

Error logs (if any)
Error:

Error in generating model output:
litellm.BadRequestError: MistralException - Error code: 400 - {'object': 'error', 'message': 'Expected last role User or Tool (or
Assistant with prefix True) for serving but got assistant', 'type': 'invalid_request_error', 'param': None, 'code': None}

Expected behavior
Perhaps you should immediately pass the Assistant role with prefix True?

Packages version:
smolagents==1.9.2

Additional context

@Neuro-Storm Neuro-Storm added the bug Something isn't working label Feb 23, 2025
@keetrap
Copy link
Contributor

keetrap commented Feb 23, 2025

@Neuro-Storm
Working fine for me. If possible please share complete code.

@ctomoiaga
Copy link

Using this on a multi agent setup myself and I get the same error. I'll see about the code but the agents are simple agents with some tools... The error is intermittent, not on every call,

@Neuro-Storm
Copy link
Author

@Neuro-Storm Working fine for me. If possible please share complete code.

@keetrap Here is the code I used:

from dotenv import load_dotenv
from smolagents import (
    CodeAgent,
    DuckDuckGoSearchTool,
    ToolCallingAgent,
    LiteLLMModel,
    VisitWebpageTool,
    OpenAIServerModel,
)
import datetime
from pathlib import Path


load_dotenv()
mistral_api_key = os.getenv("MISTRAL_API_KEY")  #  Ключ Mistral AI
if not mistral_api_key:
    raise ValueError("MISTRAL_API_KEY is not set in your environment variables.")

    
#--- LLM Setup (Corrected) ---
model_mistral = LiteLLMModel(
    model_id="mistral/mistral-large-latest",
    api_key=mistral_api_key, #  Key for Mistral
    timeout=61,  # Increase timeout
)
model_mistral_openAI = OpenAIServerModel(
    model_id="mistral-large-latest", #the model name in the Mistral documentation
    api_base="https://api.mistral.ai/v1",  # URL endpoints Mistral
    api_key=mistral_api_key,
)  

model=model_mistral
#--- Tools ---

#Agent for Internet search (using ToolCallingAgent)
web_search_agent = ToolCallingAgent(
    tools=[DuckDuckGoSearchTool(max_results=5), VisitWebpageTool(), file_operations],
    model=model,
    max_steps=6,
    name="Web_searcher",
    description="Performs an Internet search. Pass your request to it as an argument. Also tell them that they need to use the tools DuckDuckGoSearchTool for web search and VisitWebpageTool for visiting web pages, but they sometimes forget. Keep in mind that this agent can only search for one query at a time, as it can only take 6 steps. They understand natural language, so don't forget to send them suggestions for the search result.",
    #planning_interval=5,
)

#Main Agent (Manager)
manager_agent = CodeAgent(
    tools=[], 
    model=model,
    managed_agents=[web_search_agent],  # Passing the search agent as a managed one
    max_steps=15,
    additional_authorized_imports=["time", "numpy", "pandas"],
    planning_interval=5,
)

#Simple test
task = "sort these letters 'h t c m n b s' in alphabetical order, also write the remaining letters in the alphabet."
result = manager_agent.run(task)
print(f"Result: {result}")

Here's what I get in the output:

╭─────────────────────────── New run ──────────────────────────────╮
│                                                                                                                                        │
│ sort these letters 'h t c m n b s' in alphabetical order, and also write the remaining letters in the alphabet.                                  │
│                                                                                                                                        │
╰─ LiteLLMModel - mistral/mistral-large-latest ─────────────────────╯
──────────────────────────── Initial plan ────────────────────────────
Here is the plan of action that I will follow to solve the task:

                1. **Sort the given letters**: Arrange the letters 'h t c m n b s' in alphabetical order.
2. **Identify the complete list of letters in the alphabet**: Ask the Web_searcher to find the complete list of letters in the alphabet.
3. **Determine the remaining letters**: Subtract the sorted letters from the complete list of alphabet letters to find the remaining
letters.
4. **Compile the final answer**: Combine the sorted letters and the remaining letters into the final answer.
5. **Provide the final answer**: Use the final_answer tool to present the sorted letters and the remaining letters in the alphabet.
 
━━━━━━━━━━━━━━━━━━━━━━━━━━ Step 1 ━━━━━━━━━━━━━━━━━━━━━━━━━━━

Give Feedback / Get Help: https://github.com/BerriAI/litellm/issues/new
LiteLLM.Info: If you need to debug this error, use `litellm._turn_on_debug()'.


Provider List: https://docs.litellm.ai/docs/providers

Error in generating model output:
litellm.BadRequestError: MistralException - Error code: 400 - {'object': 'error', 'message': 'Expected last role User or Tool (or
Assistant with prefix True) for serving but got assistant', 'type': 'invalid_request_error', 'param': None, 'code': None}
[Step 0: Duration 10.10 seconds| Input tokens: 1,186 | Output tokens: 147]
━━━━━━━━━━━━━━━━━━━━━━━━━━━ Step 2 ━━━━━━━━━━━━━━━━━━━━━━━━━━
 ─ Executing parsed code: ───────────────────────────────────────────────
  #Step 1: Sort the given letters
  given_letters = 'h t c m n b s'
  sorted_letters = ' '.join(sorted(given_letters.split()))
  print(f"Sorted letters: {sorted_letters}")

  #Step 2: Identify the complete list of letters in the alphabet
  import string
  alphabet = string.ascii_lowercase

  #Step 3: Determine the remaining letters
  remaining_letters = ' '.join([letter for letter in alphabet if letter not in given_letters.replace(' ', '')])
  print(f"Remaining letters: {remaining_letters}")

  #Step 4: Compile the final answer
  final_answer(f"Sorted letters: {sorted_letters}\nRemaining letters: {remaining_letters}")
 ────────────────────────────────────────────────────────────────────────
Execution logs:
Sorted letters: b c h m n s t

Warning to user: Code execution failed due to an unauthorized import - Consider passing said import under `additional_authorized_imports`
when initializing your CodeAgent.
Code execution failed at line 'import string' due to: InterpreterError: Import of string is not allowed. Authorized imports are:
['itertools', 'datetime', 'pandas', 'stat', 're', 'random', 'statistics', 'unicodedata', 'numpy', 'collections', 'queue', 'time', 'math']
[Step 1: Duration 9.52 seconds| Input tokens: 4,842 | Output tokens: 491]
━━━━━━━━━━━━━━━━━━━━━━━━━ Step 3 ━━━━━━━━━━━━━━━━━━━━━━━━━━━━
 ─ Executing parsed code: ──────────────────────────────────────────────────
  #Step 1: Sort the given letters
  given_letters = 'h t c m n b s'
  sorted_letters = ' '.join(sorted(given_letters.split()))
  print(f"Sorted letters: {sorted_letters}")

  #Step 2: Identify the complete list of letters in the alphabet
  alphabet = ''.join([chr(i) for i in range(ord('a'), ord('z')+1)])

  #Step 3: Determine the remaining letters
  remaining_letters = ' '.join([letter for letter in alphabet if letter not in given_letters.replace(' ', '')])
  print(f"Remaining letters: {remaining_letters}")

  #Step 4: Compile the final answer
  final_answer(f"Sorted letters: {sorted_letters}\nRemaining letters: {remaining_letters}")
 ──────────────────────────────────────────────────────────────────────
Execution logs:
Sorted letters: b c h m n s t
Remaining letters: a d e f g i j k l o p q r u v w x y z

Out - Final answer: Sorted letters: b c h m n s t
Remaining letters: a d e f g i j k l o p q r u v w x y z
[Step 2: Duration 12.15 seconds| Input tokens: 9,240 | Output tokens: 1,104]
Result: Sorted letters: b c h m n s t
Remaining letters: a d e f g i j k l o p q r u v w x y z

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

No branches or pull requests

3 participants