custom_role_map.py
Run the Example
1
Set up your virtual environment
2
Install dependencies
3
Export your API keys
4
Run the example
Save the code above as
custom_role_map.py, then run:Documentation Index
Fetch the complete documentation index at: /llms.txt
Use this file to discover all available pages before exploring further.
Point OpenAIChat at Mistral’s base_url and remap the model role to assistant via role_map.
"""This example shows how to use a custom role map with the OpenAIChat class.
This is useful when using a custom model that doesn't support the default role map.
To run this example:
- Set the MISTRAL_API_KEY environment variable.
- Run `uv pip install openai agno` to install dependencies.
"""
from os import getenv
from agno.agent import Agent
from agno.models.openai import OpenAIChat
# ---------------------------------------------------------------------------
# Create Agent
# ---------------------------------------------------------------------------
# Using these Mistral model and url as an example.
model_id = "mistral-medium-2505"
base_url = "https://api.mistral.ai/v1"
api_key = getenv("MISTRAL_API_KEY")
mistral_role_map = {
"system": "system",
"user": "user",
"assistant": "assistant",
"tool": "tool",
"model": "assistant",
}
# When initializing the model, we pass our custom role map.
model = OpenAIChat(
id=model_id,
base_url=base_url,
api_key=api_key,
role_map=mistral_role_map,
)
agent = Agent(model=model, markdown=True)
# Running the agent with a custom role map.
res = agent.print_response("Hey, how are you doing?")
# ---------------------------------------------------------------------------
# Run Agent
# ---------------------------------------------------------------------------
if __name__ == "__main__":
pass
Set up your virtual environment
uv venv --python 3.12
source .venv/bin/activate
uv venv --python 3.12
.venv\Scripts\activate
Install dependencies
uv pip install -U agno openai
Export your API keys
export MISTRAL_API_KEY="your_mistral_api_key_here"
$Env:MISTRAL_API_KEY="your_mistral_api_key_here"
Run the example
custom_role_map.py, then run:python custom_role_map.py
Was this page helpful?