Publicado el
Mistral AI

Conversación con un Agente de Mistral AI

Post image En esta publicación, te guiaré a través de un ejemplo de conversación utilizando Agentes de Mistral AI.

Descripción general de los Agentes de Mistral AI

Inspirado por los ejemplos documentados en los recursos oficiales de Mistral, he desarrollado una conversación con un agente de IA. El objetivo es demostrar cómo mantener un historial que conserve el contexto de la conversación con el agente.

Esta demostración utiliza un agente de Mistral AI:

  • Agente de Conversación: Participa en un chat con el usuario mientras conserva el historial de la conversación como contexto.

Código en Python para el Agente de Conversación de IA

import os
from mistralai import Mistral

# Recuperar la clave API de Mistral de las variables de entorno
api_key = os.environ["MISTRAL_API_KEY"]

# Inicializar el cliente de Mistral
client = Mistral(api_key=api_key)

# Inicializar el historial de mensajes
message_history = []

# Establecer la longitud máxima del historial de mensajes
MAX_HISTORY = 30

# Bucle principal del chat
while True:
    # Obtener la entrada del usuario
    user_query = input("User:")

    # Agregar la consulta del usuario al historial de mensajes
    message_history.append(
        {
            "role": "user",
            "content": user_query,
        }
    )

    # Obtener la respuesta del agente de Mistral AI
    chat_response = client.agents.complete(
        agent_id="your-agent-id",
        messages=message_history
    )

    # Imprimir la respuesta del asistente
    assistant_reply = chat_response.choices[0].message.content
    print(f"Assistant: {assistant_reply}")

    # Agregar la respuesta del asistente al historial de mensajes
    message_history.append(
        {
            "role": "assistant",
            "content": assistant_reply,
        }
    )

    # Mantener solo los últimos 30 mensajes en el historial de mensajes
    if len(message_history) > MAX_HISTORY:
        message_history = message_history[-MAX_HISTORY:]

Ejemplo de Ejecución

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?

Enlaces útiles


¿Disfrutaste esta publicación? ¿Te resultó útil? No dudes en dejar un comentario a continuación para compartir tus pensamientos o hacer preguntas. Se requiere una cuenta de GitHub para unirse a la discusión.

Sigue leyendo

Posts relacionados