Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -511,11 +511,26 @@ def run_discovery(state: EnhancedAgentState, progress_callback=None) -> Enhanced
|
|
| 511 |
topic = state.get("search_topic", "general science")
|
| 512 |
|
| 513 |
system_msg = SystemMessage(content=f"""You are the Discovery Agent for Tuesday Trivia.
|
| 514 |
-
|
| 515 |
-
|
| 516 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 517 |
|
| 518 |
-
user_msg = HumanMessage(content=f"Search for recent breakthroughs in {topic}.")
|
| 519 |
|
| 520 |
discovery_llm = llm_small.bind_tools([search_science_breakthroughs, get_all_previous_posts, check_topic_similarity])
|
| 521 |
|
|
@@ -525,8 +540,10 @@ def run_discovery(state: EnhancedAgentState, progress_callback=None) -> Enhanced
|
|
| 525 |
response = discovery_llm.invoke([system_msg, user_msg])
|
| 526 |
conversation = [system_msg, user_msg, response]
|
| 527 |
|
| 528 |
-
max_steps =
|
| 529 |
steps = 0
|
|
|
|
|
|
|
| 530 |
while hasattr(response, 'tool_calls') and response.tool_calls and steps < max_steps:
|
| 531 |
if progress_callback:
|
| 532 |
progress_callback(f"🔧 Executing tools (step {steps+1}/{max_steps})...")
|
|
@@ -535,6 +552,10 @@ def run_discovery(state: EnhancedAgentState, progress_callback=None) -> Enhanced
|
|
| 535 |
for tool_call in response.tool_calls:
|
| 536 |
name = tool_call['name']
|
| 537 |
args = tool_call['args']
|
|
|
|
|
|
|
|
|
|
|
|
|
| 538 |
|
| 539 |
if name == 'search_science_breakthroughs':
|
| 540 |
res = search_science_breakthroughs.invoke(args)
|
|
@@ -552,6 +573,20 @@ def run_discovery(state: EnhancedAgentState, progress_callback=None) -> Enhanced
|
|
| 552 |
conversation.append(response)
|
| 553 |
steps += 1
|
| 554 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 555 |
# Parse candidates from response
|
| 556 |
candidates_text = response.content
|
| 557 |
|
|
@@ -1032,7 +1067,24 @@ css = """
|
|
| 1032 |
overflow-y: auto;
|
| 1033 |
padding: 20px;
|
| 1034 |
border-radius: 8px;
|
| 1035 |
-
background: #
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1036 |
}
|
| 1037 |
.button-row {
|
| 1038 |
display: flex;
|
|
@@ -1046,6 +1098,9 @@ css = """
|
|
| 1046 |
font-weight: bold;
|
| 1047 |
margin-bottom: 10px;
|
| 1048 |
}
|
|
|
|
|
|
|
|
|
|
| 1049 |
"""
|
| 1050 |
|
| 1051 |
with gr.Blocks(css=css, title="Tuesday Trivia Agent", theme=gr.themes.Soft()) as demo:
|
|
|
|
| 511 |
topic = state.get("search_topic", "general science")
|
| 512 |
|
| 513 |
system_msg = SystemMessage(content=f"""You are the Discovery Agent for Tuesday Trivia.
|
| 514 |
+
|
| 515 |
+
CRITICAL INSTRUCTIONS:
|
| 516 |
+
1. You MUST use the search_science_breakthroughs tool to find recent articles
|
| 517 |
+
2. You MUST use get_all_previous_posts to check for duplicates
|
| 518 |
+
3. You MUST use check_topic_similarity to avoid similar topics
|
| 519 |
+
4. Find 10-15 RECENT breakthroughs from the last 2-4 weeks
|
| 520 |
+
5. Each breakthrough needs: Title, Brief Description, Source URL, Why it's interesting
|
| 521 |
+
|
| 522 |
+
DO NOT provide generic information. ALWAYS use the tools to search for current data.
|
| 523 |
+
|
| 524 |
+
Output Format:
|
| 525 |
+
For each breakthrough provide:
|
| 526 |
+
**Title:** [Exact title from source]
|
| 527 |
+
**Description:** [2-3 sentences]
|
| 528 |
+
**URL:** [Source link]
|
| 529 |
+
**Why Interesting:** [1 sentence hook]
|
| 530 |
+
---
|
| 531 |
+
""")
|
| 532 |
|
| 533 |
+
user_msg = HumanMessage(content=f"Search for recent breakthroughs in {topic}. Use the search tool to find current articles from the past 2-4 weeks.")
|
| 534 |
|
| 535 |
discovery_llm = llm_small.bind_tools([search_science_breakthroughs, get_all_previous_posts, check_topic_similarity])
|
| 536 |
|
|
|
|
| 540 |
response = discovery_llm.invoke([system_msg, user_msg])
|
| 541 |
conversation = [system_msg, user_msg, response]
|
| 542 |
|
| 543 |
+
max_steps = 8 # Increased to ensure tool usage
|
| 544 |
steps = 0
|
| 545 |
+
tool_was_called = False
|
| 546 |
+
|
| 547 |
while hasattr(response, 'tool_calls') and response.tool_calls and steps < max_steps:
|
| 548 |
if progress_callback:
|
| 549 |
progress_callback(f"🔧 Executing tools (step {steps+1}/{max_steps})...")
|
|
|
|
| 552 |
for tool_call in response.tool_calls:
|
| 553 |
name = tool_call['name']
|
| 554 |
args = tool_call['args']
|
| 555 |
+
tool_was_called = True
|
| 556 |
+
|
| 557 |
+
if progress_callback:
|
| 558 |
+
progress_callback(f"🔧 Using {name}...")
|
| 559 |
|
| 560 |
if name == 'search_science_breakthroughs':
|
| 561 |
res = search_science_breakthroughs.invoke(args)
|
|
|
|
| 573 |
conversation.append(response)
|
| 574 |
steps += 1
|
| 575 |
|
| 576 |
+
# If no tools were called, force a search
|
| 577 |
+
if not tool_was_called:
|
| 578 |
+
if progress_callback:
|
| 579 |
+
progress_callback("⚠️ Agent didn't search, forcing search...")
|
| 580 |
+
|
| 581 |
+
# Manually call the search tool
|
| 582 |
+
search_query = f"recent {topic} breakthroughs scientific discoveries"
|
| 583 |
+
search_result = search_science_breakthroughs.invoke({"query": search_query})
|
| 584 |
+
|
| 585 |
+
# Create a follow-up prompt
|
| 586 |
+
force_msg = HumanMessage(content=f"Here are the search results. Now format them into 10-15 breakthroughs:\n\n{search_result}")
|
| 587 |
+
conversation.append(force_msg)
|
| 588 |
+
response = discovery_llm.invoke(conversation)
|
| 589 |
+
|
| 590 |
# Parse candidates from response
|
| 591 |
candidates_text = response.content
|
| 592 |
|
|
|
|
| 1067 |
overflow-y: auto;
|
| 1068 |
padding: 20px;
|
| 1069 |
border-radius: 8px;
|
| 1070 |
+
background: #ffffff;
|
| 1071 |
+
border: 1px solid #e0e0e0;
|
| 1072 |
+
color: #000000 !important;
|
| 1073 |
+
}
|
| 1074 |
+
.output-box * {
|
| 1075 |
+
color: #000000 !important;
|
| 1076 |
+
}
|
| 1077 |
+
.output-box h1, .output-box h2, .output-box h3, .output-box h4 {
|
| 1078 |
+
color: #1a1a1a !important;
|
| 1079 |
+
font-weight: bold;
|
| 1080 |
+
}
|
| 1081 |
+
.output-box strong {
|
| 1082 |
+
color: #000000 !important;
|
| 1083 |
+
font-weight: 600;
|
| 1084 |
+
}
|
| 1085 |
+
.output-box a {
|
| 1086 |
+
color: #0066cc !important;
|
| 1087 |
+
text-decoration: underline;
|
| 1088 |
}
|
| 1089 |
.button-row {
|
| 1090 |
display: flex;
|
|
|
|
| 1098 |
font-weight: bold;
|
| 1099 |
margin-bottom: 10px;
|
| 1100 |
}
|
| 1101 |
+
.gradio-container {
|
| 1102 |
+
font-family: 'Inter', system-ui, sans-serif !important;
|
| 1103 |
+
}
|
| 1104 |
"""
|
| 1105 |
|
| 1106 |
with gr.Blocks(css=css, title="Tuesday Trivia Agent", theme=gr.themes.Soft()) as demo:
|