File size: 1,214 Bytes
2cbcc71
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
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()