-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapp.py
More file actions
95 lines (84 loc) · 2.91 KB
/
app.py
File metadata and controls
95 lines (84 loc) · 2.91 KB
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
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
import gradio as gr
from groq import Groq
from src.utils.prompts import SYSTEM_PROMPT, EXAMPLE_QUESTIONS
from src.config.settings import GROQ_API_KEY, CHATBOT_NAME
def get_groq_client():
"""Initialize and return Groq client"""
api_key = GROQ_API_KEY
if not api_key:
raise ValueError("GROQ_API_KEY not found in .env file")
return Groq(api_key=api_key)
client = get_groq_client()
def respond(message, history, model, temperature, max_tokens):
"""
Generate streaming response from Instructor for Agentic AI
Args:
message: User's current question
history: List of previous chat messages (Gradio format)
model: Selected AI model
temperature: Temperature parameter for response randomness
max_tokens: Maximum tokens for response length
Returns:
Generator yielding response chunks
"""
messages = [{"role": "system", "content": SYSTEM_PROMPT}]
if history:
for chat in history:
if isinstance(chat, (list, tuple)) and len(chat) >= 2:
user_msg, assistant_msg = chat[0], chat[1]
if user_msg:
messages.append({"role": "user", "content": str(user_msg)})
if assistant_msg:
messages.append({"role": "assistant", "content": str(assistant_msg)})
messages.append({"role": "user", "content": message})
try:
stream = client.chat.completions.create(
model=model,
messages=messages,
temperature=temperature,
max_tokens=int(max_tokens),
stream=True
)
response = ""
for chunk in stream:
if chunk.choices[0].delta.content:
response += chunk.choices[0].delta.content
yield response
except Exception as e:
yield f"Error: {str(e)}"
app = gr.ChatInterface(
fn=respond,
title=f"🧭 {CHATBOT_NAME}",
description=f"Ask questions about designing, building, and refining agentic AI systems with {CHATBOT_NAME}.",
additional_inputs=[
gr.Dropdown(
choices=[
"qwen/qwen3-32b",
"moonshotai/kimi-k2-instruct",
"moonshotai/kimi-k2-instruct-0905",
],
value="qwen/qwen3-32b",
label="Model",
info="Select the AI model to use"
),
gr.Slider(
minimum=0,
maximum=2,
value=0.9,
step=0.1,
label="Temperature",
info="Controls randomness. Lower = more focused, Higher = more creative"
),
gr.Slider(
minimum=256,
maximum=8192,
value=2048,
step=256,
label="Max Tokens",
info="Maximum length of the response"
),
],
examples=EXAMPLE_QUESTIONS,
)
if __name__ == "__main__":
app.launch(share=True)