File size: 2,565 Bytes
849aaf7
 
 
 
6116057
 
849aaf7
81576ad
df55a30
 
 
81576ad
 
df55a30
 
 
81576ad
849aaf7
df55a30
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
c6ba0c0
df55a30
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
# 2) The actual app
import os
from getpass import getpass
from openai import OpenAI
import gradio as gr

# ——— Configure your OpenRouter key ———
OPENROUTER_API_KEY = os.getenv("OPENROUTER_API_KEY")

# Check if the API key was retrieved
if not OPENROUTER_API_KEY:
    print("Error: OPENROUTER_API_KEY not found in environment.")
    print("Please set your API key in the environment as 'OPENROUTER_API_KEY'.")
else:
    client = OpenAI(
        base_url="https://openrouter.ai/api/v1",
        api_key=OPENROUTER_API_KEY,
    )

    def openrouter_chat(user_message, history):
        """Send user_message and history to mistralai/devstral-small:free and append to history."""
        history = history or []

        # Build the messages list from the history and the current user message
        # Gradio history is a list of tuples [(user_msg, bot_msg), ...]
        # We need to convert it to the OpenAI API format: list of dicts [{"role": "user", "content": ...}, {"role": "assistant", "content": ...}, ...]
        messages_for_api = []
        for human_message, ai_message in history:
            messages_for_api.append({"role": "user", "content": human_message})
            if ai_message is not None: # Only add assistant message if it exists
                messages_for_api.append({"role": "assistant", "content": ai_message})

        # Add the current user message
        messages_for_api.append({"role": "user", "content": user_message})

        try:
            # Call the model with the mistralai/Devstral-Small-2505 for full conversation history
            resp = client.chat.completions.create(
                model="mistralai/devstral-small:free",
                messages=messages_for_api,
                # you can tweak max_tokens, temperature, etc. here
            )
            bot_reply = resp.choices[0].message.content
            # Append the user message and bot reply to the history for Gradio display
            history.append((user_message, bot_reply))
        except Exception as e:
            # Handle potential errors and append an error message to the history
            history.append((user_message, f"Error: {e}"))


        return history, ""

    with gr.Blocks() as demo:
        gr.Markdown("## 🦜🔗 Gradio + OpenRouter Chat with Devstral-Small (with History)")
        chatbot = gr.Chatbot(label="Chat")
        msg_in = gr.Textbox(placeholder="Type your question here…", label="You")
        msg_in.submit(openrouter_chat, inputs=[msg_in, chatbot], outputs=[chatbot, msg_in])

    demo.launch()