Veröffentlicht am
Mistral AI

Konversation mit einem Mistral AI Agenten

Post image In diesem Beitrag werde ich Ihnen eine Beispielkonversation mit einem Mistral AI Agenten vorstellen.

Überblick über Mistral AI Agenten

Inspiriert von den Beispielen in den offiziellen Ressourcen von Mistral habe ich eine Konversation mit einem AI-Agenten entwickelt. Das Ziel ist es zu zeigen, wie man den Kontext der Konversation mit dem Agenten durch die Aufrechterhaltung eines Verlaufs bewahren kann.

Diese Demonstration verwendet einen Mistral AI Agenten:

  • Konversationsagent: Führt einen Chat mit dem Benutzer und bewahrt dabei den Konversationsverlauf als Kontext.

Python-Code für den Konversations-AI-Agenten

import os
from mistralai import Mistral

# Mistral API-Schlüssel aus den Umgebungsvariablen abrufen
api_key = os.environ["MISTRAL_API_KEY"]

# Mistral-Client initialisieren
client = Mistral(api_key=api_key)

# Nachrichtenverlauf initialisieren
message_history = []

# Maximale Länge des Nachrichtenverlaufs festlegen
MAX_HISTORY = 30

# Haupt-Chat-Schleife
while True:
    # Benutzereingabe abfragen
    user_query = input("User:")

    # Benutzereingabe zum Nachrichtenverlauf hinzufügen
    message_history.append(
        {
            "role": "user",
            "content": user_query,
        }
    )

    # Antwort vom Mistral AI Agenten abrufen
    chat_response = client.agents.complete(
        agent_id="your-agent-id",
        messages=message_history
    )

    # Antwort des Assistenten ausgeben
    assistant_reply = chat_response.choices[0].message.content
    print(f"Assistant: {assistant_reply}")

    # Antwort des Assistenten zum Nachrichtenverlauf hinzufügen
    message_history.append(
        {
            "role": "assistant",
            "content": assistant_reply,
        }
    )

    # Nur die letzten 30 Nachrichten im Nachrichtenverlauf behalten
    if len(message_history) > MAX_HISTORY:
        message_history = message_history[-MAX_HISTORY:]

Beispielausführung

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?

Hat Ihnen dieser Beitrag gefallen? Fanden Sie ihn hilfreich? Hinterlassen Sie gerne einen Kommentar unten, um Ihre Gedanken mitzuteilen oder Fragen zu stellen. Ein GitHub-Konto ist erforderlich, um an der Diskussion teilzunehmen.

Weiterlesen

Ähnliche Beiträge