Published on

Conversation with Mistral AI Agent

3 min read
Authors
  • Profile picture of aithemes.net
    Name
    aithemes.net
    Twitter
Post image In this post, I will walk you through an example conversation using Mistral AI Agents.

Overview of Mistral AI Agents

Inspired by the examples documented in Mistral’s official resources, I have developed a conversation with an AI agent. The objective is to demonstrate how to maintain a history that keeps the context of the conversation with the agent.

This demonstration utilizes a Mistral AI agent:

  • Conversation Agent: Engages in a chat with the user while preserving the conversation history as context.

Python Code for the Conversation AI Agent

import os
from mistralai import Mistral

# Retrieve Mistral API key from environment variables
api_key = os.environ["MISTRAL_API_KEY"]

# Initialize the Mistral client
client = Mistral(api_key=api_key)

# Initialize message history
message_history = []

# Set maximum message history length
MAX_HISTORY = 30

# Main chat loop
while True:
    # Get user input
    user_query = input("User:")

    # Append the user's query to message history
    message_history.append(
        {
            "role": "user",
            "content": user_query,
        }
    )

    # Get the response from the Mistral AI agent
    chat_response = client.agents.complete(
        agent_id="your-agent-id",
        messages=message_history
    )

    # Print the assistant's response
    assistant_reply = chat_response.choices[0].message.content
    print(f"Assistant: {assistant_reply}")

    # Append the assistant's response to the message history
    message_history.append(
        {
            "role": "assistant",
            "content": assistant_reply,
        }
    )

    # Keep only the last 30 messages in the message history
    if len(message_history) > MAX_HISTORY:
        message_history = message_history[-MAX_HISTORY:]

Example Execution

User: Tell me the name of a city in South America
Assistant: Sure! One city in South America is Buenos Aires, which is the capital of Argentina. Do you have any specific interests or questions about cities in South America?
User: Tell me the population of the city you gave me
Assistant: Buenos Aires, the capital of Argentina, has a population of approximately 3 million people within the city itself, according to the latest estimates. However, the Greater Buenos Aires metropolitan area has a population of around 15 million people, making it one of the largest urban areas in South America.
User: Tell me the altitude of the city you gave me
Assistant: Buenos Aires is located at a relatively low altitude. The city's elevation is approximately 25 meters (82 feet) above sea level. This low altitude contributes to its generally temperate climate. Do you have any other questions about Buenos Aires or another topic you'd like to explore?
User: Tell me the average temperature of the city you gave me
Assistant: Buenos Aires has a humid subtropical climate with mild winters and hot, humid summers. The average annual temperature is around 18°C (64°F). During the summer months (December to February), the average temperature is about 25°C (77°F), but it can often feel warmer due to humidity. In the winter months (June to August), the average temperature is around 11°C (52°F). Do you have any other questions about the climate or any other aspect of Buenos Aires?

Enjoyed this post? Found it helpful? Feel free to leave a comment below to share your thoughts or ask questions. A GitHub account is required to join the discussion.