Update app.py
Browse files
app.py
CHANGED
@@ -47,151 +47,92 @@ def get_model_and_tokenizer() -> Tuple[AutoModelForCausalLM, AutoTokenizer]:
|
|
47 |
raise e
|
48 |
return models["7B"], tokenizers["7B"]
|
49 |
|
50 |
-
# --- Default Prompt Templates
|
51 |
default_prompts = {
|
52 |
"coding": {
|
53 |
-
"brainstorm":
|
54 |
-
|
55 |
-
|
56 |
-
|
57 |
-
{user_prompt}
|
58 |
-
|
59 |
-
|
60 |
-
|
61 |
-
|
62 |
-
|
63 |
-
""
|
64 |
-
|
65 |
-
|
66 |
-
|
67 |
-
**
|
68 |
-
|
69 |
-
|
70 |
-
**
|
71 |
-
|
72 |
-
|
73 |
-
**
|
74 |
-
|
75 |
-
|
76 |
-
|
77 |
-
|
78 |
-
"synthesis": """**Synthesis & Final Refinement (Round 3)**
|
79 |
-
Review the detailed code and reasoning below, and synthesize a final, refined response that:
|
80 |
-
1. Combines the brainstorming insights and advanced code generation.
|
81 |
-
2. Summarizes the solution succinctly.
|
82 |
-
3. Provides any additional improvements.
|
83 |
-
|
84 |
-
**Detailed Code & Reasoning:**
|
85 |
-
{round2_response}
|
86 |
-
""",
|
87 |
-
"rationale": """**Pun Generation and Rationale (Round 4)**
|
88 |
-
Based on the final refined response below, generate a clear, stand-alone pun-filled birthday message with a coding twist, then explain in detail why that pun was chosen.
|
89 |
-
|
90 |
-
Final Refined Response:
|
91 |
-
{final_response}
|
92 |
-
|
93 |
-
Your answer should:
|
94 |
-
1. Clearly output the pun as a separate line.
|
95 |
-
2. Explain the pun’s connection to birthdays and coding concepts (e.g., binary, syntax).
|
96 |
-
3. Describe any creative insights behind the choice.
|
97 |
-
"""
|
98 |
},
|
99 |
"math": {
|
100 |
-
"brainstorm":
|
101 |
-
|
102 |
-
|
103 |
-
|
104 |
-
{user_prompt}
|
105 |
-
|
106 |
-
|
107 |
-
|
108 |
-
|
109 |
-
|
110 |
-
""
|
111 |
-
|
112 |
-
|
113 |
-
|
114 |
-
**
|
115 |
-
|
116 |
-
|
117 |
-
**
|
118 |
-
|
119 |
-
|
120 |
-
**
|
121 |
-
|
122 |
-
|
123 |
-
""
|
124 |
-
|
125 |
-
Review the strategy and previous analysis below, and produce a refined, step-by-step solution that:
|
126 |
-
1. Clearly explains the solution path.
|
127 |
-
2. Highlights key steps and justifications.
|
128 |
-
3. Summarizes the final answer.
|
129 |
-
|
130 |
-
**Detailed Strategy:**
|
131 |
-
{round2_response}
|
132 |
-
""",
|
133 |
-
"rationale": """**Solution Rationale (Round 4)**
|
134 |
-
Based on the final refined solution below, provide a detailed explanation of the key steps and mathematical insights.
|
135 |
-
|
136 |
-
Final Refined Solution:
|
137 |
-
{final_response}
|
138 |
-
|
139 |
-
Your response should:
|
140 |
-
1. Clearly explain why each step was taken.
|
141 |
-
2. Detail any assumptions and mathematical principles used.
|
142 |
-
3. Summarize the creative reasoning behind the solution.
|
143 |
-
"""
|
144 |
},
|
145 |
"writing": {
|
146 |
-
"brainstorm":
|
147 |
-
|
148 |
-
|
149 |
-
|
150 |
-
{user_prompt}
|
151 |
-
|
152 |
-
|
153 |
-
|
154 |
-
|
155 |
-
|
156 |
-
""
|
157 |
-
|
158 |
-
|
159 |
-
|
160 |
-
**
|
161 |
-
|
162 |
-
|
163 |
-
**
|
164 |
-
|
165 |
-
|
166 |
-
**
|
167 |
-
|
168 |
-
|
169 |
-
|
170 |
-
|
171 |
-
"synthesis": """**Draft Writing (Round 3)**
|
172 |
-
Review the outline below and produce a refined draft of the creative piece that:
|
173 |
-
1. Synthesizes the brainstorming insights and the outline.
|
174 |
-
2. Provides a coherent and engaging narrative.
|
175 |
-
3. Includes stylistic and thematic elements.
|
176 |
-
|
177 |
-
**Outline:**
|
178 |
-
{round2_response}
|
179 |
-
""",
|
180 |
-
"rationale": """**Final Editing and Rationale (Round 4)**
|
181 |
-
Based on the final draft below, refine the piece further and provide a detailed explanation of your creative choices.
|
182 |
-
|
183 |
-
Final Draft:
|
184 |
-
{final_response}
|
185 |
-
|
186 |
-
Your answer should:
|
187 |
-
1. Present the final refined text.
|
188 |
-
2. Explain the narrative choices, stylistic decisions, and thematic connections.
|
189 |
-
3. Detail any creative insights that influenced the final version.
|
190 |
-
"""
|
191 |
}
|
192 |
}
|
193 |
|
194 |
-
# --- Domain Detection ---
|
195 |
def detect_domain(user_prompt: str) -> str:
|
196 |
"""
|
197 |
Detect the domain based on keywords.
|
@@ -204,7 +145,7 @@ def detect_domain(user_prompt: str) -> str:
|
|
204 |
math_keywords = ["solve", "integral", "derivative", "equation", "proof", "calculate", "sum", "product"]
|
205 |
writing_keywords = ["write", "story", "essay", "novel", "poem", "article", "narrative", "creative"]
|
206 |
coding_keywords = ["code", "program", "debug", "compile", "algorithm", "function"]
|
207 |
-
|
208 |
if any(kw in prompt_lower for kw in math_keywords):
|
209 |
logging.info("Domain detected as: math")
|
210 |
return "math"
|
@@ -286,7 +227,7 @@ class MultiRoundAgent:
|
|
286 |
r1 = generate_response(self.model, self.tokenizer, prompt_r1, params.get("max_new_tokens"), params.get("temp"), params.get("top_p"))
|
287 |
self.memory_manager.store(f"Round 1 Response: {r1}")
|
288 |
|
289 |
-
# Round 2: Secondary Generation (strategy
|
290 |
logging.info("--- Round 2 ---")
|
291 |
prompt_r2 = self.prompt_templates["round2"].format(brainstorm_response=r1, user_prompt=user_prompt)
|
292 |
r2 = generate_response(self.model, self.tokenizer, prompt_r2, params.get("max_new_tokens") + 100, params.get("temp"), params.get("top_p"))
|
@@ -343,7 +284,7 @@ class MultiRoundAgent:
|
|
343 |
def swarm_agent_iterative(user_prompt: str, temp: float, top_p: float, max_new_tokens: int, memory_top_k: int,
|
344 |
prompt_templates: Dict[str, str], domain: str, show_raw: bool) -> Generator[str, None, None]:
|
345 |
"""
|
346 |
-
Wraps the multi-round agent functionality. Depending on the detected domain,
|
347 |
it runs the 4-round pipeline.
|
348 |
"""
|
349 |
model, tokenizer = get_model_and_tokenizer()
|
@@ -364,12 +305,12 @@ def handle_explanation_request(user_prompt: str, history: List) -> str:
|
|
364 |
explanation_prompt += f"- {item}\n"
|
365 |
else:
|
366 |
explanation_prompt += "No stored final output found.\n"
|
367 |
-
|
368 |
explanation_prompt += "\nRecent related exchanges:\n"
|
369 |
for chat in history:
|
370 |
if ("explain" in chat[0].lower()) or (chat[1] and "explain" in chat[1].lower()):
|
371 |
explanation_prompt += f"User: {chat[0]}\nAssistant: {chat[1]}\n"
|
372 |
-
|
373 |
explanation_prompt += "\nBased on the above context, please provide a detailed explanation of the creative choices."
|
374 |
model, tokenizer = get_model_and_tokenizer()
|
375 |
explanation = generate_response(model, tokenizer, explanation_prompt, 300, 0.7, 0.9)
|
@@ -392,10 +333,11 @@ def format_history(history: List) -> List[Dict[str, str]]:
|
|
392 |
return messages
|
393 |
|
394 |
# --- Gradio Chat Interface Function ---
|
395 |
-
def gradio_interface(message: str, history: List, param_state: Dict, prompt_state: Dict) -> Generator[List[Dict[str, str]], None, None]:
|
396 |
"""
|
397 |
Called by Gradio's ChatInterface. Uses current generation parameters and preset prompt templates.
|
398 |
If the user asks for an explanation, routes the request accordingly.
|
|
|
399 |
"""
|
400 |
if "explain" in message.lower():
|
401 |
explanation = handle_explanation_request(message, history)
|
@@ -413,8 +355,9 @@ def gradio_interface(message: str, history: List, param_state: Dict, prompt_stat
|
|
413 |
logging.error(f"Parameter conversion error: {e}")
|
414 |
temp, top_p, max_new_tokens, memory_top_k, show_raw = 0.5, 0.9, 300, 2, False
|
415 |
|
416 |
-
domain
|
417 |
-
|
|
|
418 |
prompt_templates = prompt_state.get(domain, default_prompts.get(domain, default_prompts["coding"]))
|
419 |
|
420 |
history = history + [[message, ""]]
|
@@ -436,7 +379,7 @@ ui_description = '''
|
|
436 |
<div>
|
437 |
<h1 style="text-align: center;">DeepSeek Agent Swarm Chat</h1>
|
438 |
<p style="text-align: center;">
|
439 |
-
Multi-round agent with 4-round prompt chaining
|
440 |
<br>- Coding
|
441 |
<br>- Math
|
442 |
<br>- Writing
|
@@ -488,16 +431,18 @@ with gr.Blocks(css=css, title="DeepSeek Agent Swarm Chat") as demo:
|
|
488 |
|
489 |
with gr.Tabs():
|
490 |
with gr.Tab("Chat"):
|
|
|
|
|
491 |
chatbot = gr.Chatbot(height=450, placeholder=ui_placeholder, label="Agent Swarm Output", type="messages")
|
492 |
gr.ChatInterface(
|
493 |
fn=gradio_interface,
|
494 |
chatbot=chatbot,
|
495 |
-
additional_inputs=[param_state, prompt_state],
|
496 |
examples=[
|
497 |
['How can we build a robust web service that scales efficiently under load?'],
|
498 |
['Solve the integral of x^2 from 0 to 1.'],
|
499 |
['Write a short story about a mysterious writer in a busy city.'],
|
500 |
-
['Create a
|
501 |
],
|
502 |
cache_examples=False,
|
503 |
type="messages",
|
@@ -619,4 +564,4 @@ with gr.Blocks(css=css, title="DeepSeek Agent Swarm Chat") as demo:
|
|
619 |
gr.Markdown(ui_license)
|
620 |
|
621 |
if __name__ == "__main__":
|
622 |
-
demo.launch()
|
|
|
47 |
raise e
|
48 |
return models["7B"], tokenizers["7B"]
|
49 |
|
50 |
+
# --- Refactored Default Prompt Templates ---
|
51 |
default_prompts = {
|
52 |
"coding": {
|
53 |
+
"brainstorm": (
|
54 |
+
"**Round 1: Brainstorm & Analysis**\n"
|
55 |
+
"Please analyze the following coding challenge or question. Consider the overall problem, "
|
56 |
+
"potential edge cases, and any assumptions you might need to make. Explain your reasoning as you think aloud.\n\n"
|
57 |
+
"**User Request:**\n{user_prompt}\n"
|
58 |
+
),
|
59 |
+
"round2": (
|
60 |
+
"**Round 2: Detailed Reasoning & Strategy**\n"
|
61 |
+
"Based on your initial analysis, please break down the problem into logical steps. "
|
62 |
+
"Outline a plan or strategy that could be used to solve the challenge, highlighting key algorithms, structures, or design considerations.\n\n"
|
63 |
+
"**Initial Analysis:**\n{brainstorm_response}\n\n"
|
64 |
+
"**User Request:**\n{user_prompt}\n"
|
65 |
+
),
|
66 |
+
"synthesis": (
|
67 |
+
"**Round 3: Synthesis & Implementation**\n"
|
68 |
+
"Taking into account the steps outlined previously, synthesize a coherent solution. "
|
69 |
+
"Provide a detailed explanation of how the code addresses the problem while encouraging best practices and clear logic.\n\n"
|
70 |
+
"**Detailed Strategy:**\n{round2_response}\n"
|
71 |
+
),
|
72 |
+
"rationale": (
|
73 |
+
"**Round 4: Reflection & Final Output**\n"
|
74 |
+
"Review your solution and provide a final, well-rounded response that summarizes your reasoning and the implementation strategy. "
|
75 |
+
"Explain any key decisions made during the process and how they contribute to an effective solution.\n\n"
|
76 |
+
"**Final Draft:**\n{final_response}\n"
|
77 |
+
)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
78 |
},
|
79 |
"math": {
|
80 |
+
"brainstorm": (
|
81 |
+
"**Round 1: Problem Analysis & Exploration**\n"
|
82 |
+
"Carefully analyze the mathematical problem provided. Describe the underlying concepts and any assumptions you are making. "
|
83 |
+
"Detail your initial reasoning and potential methods to tackle the problem.\n\n"
|
84 |
+
"**Problem:**\n{user_prompt}\n"
|
85 |
+
),
|
86 |
+
"round2": (
|
87 |
+
"**Round 2: Detailed Reasoning & Methodology**\n"
|
88 |
+
"Based on your initial exploration, break down the problem into sequential steps or methodologies. "
|
89 |
+
"Explain the reasoning behind each step and how they connect to solve the problem.\n\n"
|
90 |
+
"**Initial Analysis:**\n{brainstorm_response}\n\n"
|
91 |
+
"**Problem:**\n{user_prompt}\n"
|
92 |
+
),
|
93 |
+
"synthesis": (
|
94 |
+
"**Round 3: Synthesis & Step-by-Step Solution**\n"
|
95 |
+
"Integrate your previous reasoning into a structured solution. Clearly explain each step of your calculation or proof, "
|
96 |
+
"ensuring that your logical progression is easy to follow.\n\n"
|
97 |
+
"**Detailed Methodology:**\n{round2_response}\n"
|
98 |
+
),
|
99 |
+
"rationale": (
|
100 |
+
"**Round 4: Reflection & Final Explanation**\n"
|
101 |
+
"Present your final solution along with a detailed explanation of the reasoning behind each step. "
|
102 |
+
"Discuss any assumptions and insights that helped you arrive at the final answer.\n\n"
|
103 |
+
"**Final Solution:**\n{final_response}\n"
|
104 |
+
)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
105 |
},
|
106 |
"writing": {
|
107 |
+
"brainstorm": (
|
108 |
+
"**Round 1: Creative Exploration & Conceptualization**\n"
|
109 |
+
"Read the following writing prompt and explore its themes, tone, and potential narrative directions. "
|
110 |
+
"Outline your initial thoughts and reasoning behind various creative choices.\n\n"
|
111 |
+
"**Writing Prompt:**\n{user_prompt}\n"
|
112 |
+
),
|
113 |
+
"round2": (
|
114 |
+
"**Round 2: Detailed Outline & Narrative Structure**\n"
|
115 |
+
"Based on your brainstorming, create a detailed outline that organizes the narrative or essay. "
|
116 |
+
"Explain the reasoning behind your structure, the flow of ideas, and how you plan to incorporate creative elements.\n\n"
|
117 |
+
"**Initial Brainstorming:**\n{brainstorm_response}\n\n"
|
118 |
+
"**Writing Prompt:**\n{user_prompt}\n"
|
119 |
+
),
|
120 |
+
"synthesis": (
|
121 |
+
"**Round 3: Draft Synthesis & Refinement**\n"
|
122 |
+
"Integrate your outline and creative ideas into a coherent draft. Provide a well-rounded narrative that is both engaging and logically structured. "
|
123 |
+
"Explain your thought process as you refine the narrative.\n\n"
|
124 |
+
"**Outline & Strategy:**\n{round2_response}\n"
|
125 |
+
),
|
126 |
+
"rationale": (
|
127 |
+
"**Round 4: Reflection & Final Editing**\n"
|
128 |
+
"Review your draft and provide a final version that reflects thoughtful editing and creative reasoning. "
|
129 |
+
"Explain the choices made in refining the text, from structure to stylistic decisions.\n\n"
|
130 |
+
"**Final Draft:**\n{final_response}\n"
|
131 |
+
)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
132 |
}
|
133 |
}
|
134 |
|
135 |
+
# --- Domain Detection (Retained for fallback) ---
|
136 |
def detect_domain(user_prompt: str) -> str:
|
137 |
"""
|
138 |
Detect the domain based on keywords.
|
|
|
145 |
math_keywords = ["solve", "integral", "derivative", "equation", "proof", "calculate", "sum", "product"]
|
146 |
writing_keywords = ["write", "story", "essay", "novel", "poem", "article", "narrative", "creative"]
|
147 |
coding_keywords = ["code", "program", "debug", "compile", "algorithm", "function"]
|
148 |
+
|
149 |
if any(kw in prompt_lower for kw in math_keywords):
|
150 |
logging.info("Domain detected as: math")
|
151 |
return "math"
|
|
|
227 |
r1 = generate_response(self.model, self.tokenizer, prompt_r1, params.get("max_new_tokens"), params.get("temp"), params.get("top_p"))
|
228 |
self.memory_manager.store(f"Round 1 Response: {r1}")
|
229 |
|
230 |
+
# Round 2: Secondary Generation (detailed reasoning/strategy)
|
231 |
logging.info("--- Round 2 ---")
|
232 |
prompt_r2 = self.prompt_templates["round2"].format(brainstorm_response=r1, user_prompt=user_prompt)
|
233 |
r2 = generate_response(self.model, self.tokenizer, prompt_r2, params.get("max_new_tokens") + 100, params.get("temp"), params.get("top_p"))
|
|
|
284 |
def swarm_agent_iterative(user_prompt: str, temp: float, top_p: float, max_new_tokens: int, memory_top_k: int,
|
285 |
prompt_templates: Dict[str, str], domain: str, show_raw: bool) -> Generator[str, None, None]:
|
286 |
"""
|
287 |
+
Wraps the multi-round agent functionality. Depending on the detected or selected domain,
|
288 |
it runs the 4-round pipeline.
|
289 |
"""
|
290 |
model, tokenizer = get_model_and_tokenizer()
|
|
|
305 |
explanation_prompt += f"- {item}\n"
|
306 |
else:
|
307 |
explanation_prompt += "No stored final output found.\n"
|
308 |
+
|
309 |
explanation_prompt += "\nRecent related exchanges:\n"
|
310 |
for chat in history:
|
311 |
if ("explain" in chat[0].lower()) or (chat[1] and "explain" in chat[1].lower()):
|
312 |
explanation_prompt += f"User: {chat[0]}\nAssistant: {chat[1]}\n"
|
313 |
+
|
314 |
explanation_prompt += "\nBased on the above context, please provide a detailed explanation of the creative choices."
|
315 |
model, tokenizer = get_model_and_tokenizer()
|
316 |
explanation = generate_response(model, tokenizer, explanation_prompt, 300, 0.7, 0.9)
|
|
|
333 |
return messages
|
334 |
|
335 |
# --- Gradio Chat Interface Function ---
|
336 |
+
def gradio_interface(message: str, history: List, param_state: Dict, prompt_state: Dict, mode: str) -> Generator[List[Dict[str, str]], None, None]:
|
337 |
"""
|
338 |
Called by Gradio's ChatInterface. Uses current generation parameters and preset prompt templates.
|
339 |
If the user asks for an explanation, routes the request accordingly.
|
340 |
+
The selected mode (coding, math, or writing) overrides automatic domain detection.
|
341 |
"""
|
342 |
if "explain" in message.lower():
|
343 |
explanation = handle_explanation_request(message, history)
|
|
|
355 |
logging.error(f"Parameter conversion error: {e}")
|
356 |
temp, top_p, max_new_tokens, memory_top_k, show_raw = 0.5, 0.9, 300, 2, False
|
357 |
|
358 |
+
# Use selected mode if provided; otherwise, fallback to domain detection.
|
359 |
+
domain = mode if mode in default_prompts else detect_domain(message)
|
360 |
+
# Get the prompt templates for the chosen domain.
|
361 |
prompt_templates = prompt_state.get(domain, default_prompts.get(domain, default_prompts["coding"]))
|
362 |
|
363 |
history = history + [[message, ""]]
|
|
|
379 |
<div>
|
380 |
<h1 style="text-align: center;">DeepSeek Agent Swarm Chat</h1>
|
381 |
<p style="text-align: center;">
|
382 |
+
Multi-round agent with 4-round prompt chaining, supporting three modes:
|
383 |
<br>- Coding
|
384 |
<br>- Math
|
385 |
<br>- Writing
|
|
|
431 |
|
432 |
with gr.Tabs():
|
433 |
with gr.Tab("Chat"):
|
434 |
+
# Add a mode selector for explicit domain selection.
|
435 |
+
mode_selector = gr.Radio(choices=["coding", "math", "writing"], value="coding", label="Select Mode")
|
436 |
chatbot = gr.Chatbot(height=450, placeholder=ui_placeholder, label="Agent Swarm Output", type="messages")
|
437 |
gr.ChatInterface(
|
438 |
fn=gradio_interface,
|
439 |
chatbot=chatbot,
|
440 |
+
additional_inputs=[param_state, prompt_state, mode_selector],
|
441 |
examples=[
|
442 |
['How can we build a robust web service that scales efficiently under load?'],
|
443 |
['Solve the integral of x^2 from 0 to 1.'],
|
444 |
['Write a short story about a mysterious writer in a busy city.'],
|
445 |
+
['Create a creative and reflective solution for a coding challenge.']
|
446 |
],
|
447 |
cache_examples=False,
|
448 |
type="messages",
|
|
|
564 |
gr.Markdown(ui_license)
|
565 |
|
566 |
if __name__ == "__main__":
|
567 |
+
demo.launch(share=True)
|