ChatBot / app.py
Eddyhzd
First commit
2cbcc71
raw
history blame
1.21 kB
import gradio as gr
from openai import OpenAI
import os
cle_api = os.environ.get("CLE_API_MISTRAL")
# Initialisation du client Mistral (API compatible OpenAI)
client = OpenAI(api_key=cle_api, base_url="https://api.mistral.ai/v1")
# Chatbot : simple écho Fonction chatbot reliée à Mistral
def chatbot(message, history):
# Préparer l’historique dans le format de Mistral
messages = []
for user_msg, bot_msg in history:
messages.append({"role": "user", "content": user_msg})
messages.append({"role": "assistant", "content": bot_msg})
messages.append({"role": "user", "content": message})
# Appel API Mistral
response = client.chat.completions.create(
model="mistral-small-latest", # tu peux changer le modèle (mistral-medium, mistral-large, etc.)
messages=messages
)
bot_reply = response.choices[0].message.content.strip()
history.append(("Vous: " + message, "Bot: " + bot_reply))
return history, history
with gr.Blocks() as demo:
chatbot_ui = gr.Chatbot(label="ChatBot")
msg = gr.Textbox(placeholder="Écrivez un message...")
msg.submit(chatbot, [msg, chatbot_ui], [chatbot_ui, chatbot_ui])
demo.launch()