Eddyhzd commited on
Commit
b25820d
·
1 Parent(s): e6c446b

Fix: Simple chatBot mistral

Browse files
Files changed (1) hide show
  1. app.py +34 -12
app.py CHANGED
@@ -1,16 +1,38 @@
1
- from mcp.client.session import MCPClient
 
 
2
 
3
- # Connexion
4
- client = MCPClient("csv_analyzer")
5
- client.connect()
6
 
7
- # Lister les colonnes
8
- print("Colonnes dispo:", client.call("list_columns"))
9
 
10
- # Filtrer des lignes
11
- rows = client.call("filter_rows", column="pays", value="France", limit=3)
12
- print("Lignes filtrées:", rows)
 
 
 
 
 
 
13
 
14
- # Analyse avec Mistral
15
- analysis = client.call("analyze_data", question="Quels sont les 3 produits les plus fréquents ?")
16
- print("Analyse:", analysis)
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import gradio as gr
2
+ from openai import OpenAI
3
+ import os
4
 
5
+ cle_api = os.environ.get("CLE_API_MISTRAL")
 
 
6
 
7
+ # Initialisation du client Mistral (API compatible OpenAI)
8
+ client = OpenAI(api_key=cle_api, base_url="https://api.mistral.ai/v1")
9
 
10
+ # Chatbot : simple écho Fonction chatbot reliée à Mistral
11
+ def chatbot(message, history):
12
+ # Préparer l’historique dans le format de Mistral
13
+ messages = []
14
+ for user_msg, bot_msg in history:
15
+ messages.append({"role": "user", "content": user_msg})
16
+ messages.append({"role": "assistant", "content": bot_msg})
17
+
18
+ messages.append({"role": "user", "content": message})
19
 
20
+ # Appel API Mistral
21
+ response = client.chat.completions.create(
22
+ model="mistral-small-latest",
23
+ messages=messages
24
+ )
25
+
26
+ bot_reply = response.choices[0].message.content.strip()
27
+ history.append(("Vous: " + message, "Bot: " + bot_reply))
28
+ return history, history
29
+
30
+ with gr.Blocks() as demo:
31
+
32
+
33
+ chatbot_ui = gr.Chatbot(label="ChatBot")
34
+ msg = gr.Textbox(placeholder="Écrivez un message...")
35
+
36
+ msg.submit(chatbot, [msg, chatbot_ui], [chatbot_ui, chatbot_ui])
37
+
38
+ demo.launch()