Spaces:
Sleeping
Sleeping
wiki issue
Browse files
app.py
CHANGED
|
@@ -48,47 +48,30 @@ class KnowledgeBaseTool(Tool):
|
|
| 48 |
return f"Knowledge base: {info}. Use this context to answer questions about {topic}."
|
| 49 |
return f"No specific knowledge base entry for '{topic}'. Use general reasoning."
|
| 50 |
|
| 51 |
-
class
|
| 52 |
-
name = "
|
| 53 |
-
description = "Search
|
| 54 |
-
inputs = {"query": {"type": "string", "description": "The search query
|
| 55 |
output_type = "string"
|
| 56 |
|
| 57 |
def __init__(self):
|
| 58 |
super().__init__()
|
| 59 |
-
self.ddg_tool = DuckDuckGoSearchTool()
|
| 60 |
-
self.max_retries = 2
|
| 61 |
-
self.timeout = 8
|
| 62 |
self.is_initialized = True
|
| 63 |
|
| 64 |
def forward(self, query: str) -> str:
|
| 65 |
-
"""Search
|
| 66 |
-
# Try Wikipedia first (reduced timeout)
|
| 67 |
try:
|
| 68 |
import requests
|
| 69 |
wiki_url = "https://en.wikipedia.org/api/rest_v1/page/summary/" + query.replace(" ", "_")
|
| 70 |
-
response = requests.get(wiki_url, timeout=
|
| 71 |
if response.status_code == 200:
|
| 72 |
data = response.json()
|
| 73 |
if 'extract' in data and data['extract']:
|
| 74 |
-
return f"Wikipedia: {data['extract']}"
|
| 75 |
except Exception as e:
|
| 76 |
print(f"Wikipedia search failed: {e}")
|
| 77 |
|
| 78 |
-
|
| 79 |
-
try:
|
| 80 |
-
wikidata_url = f"https://www.wikidata.org/w/api.php?action=wbsearchentities&search={query.replace(' ', '%20')}&language=en&format=json"
|
| 81 |
-
response = requests.get(wikidata_url, timeout=3)
|
| 82 |
-
if response.status_code == 200:
|
| 83 |
-
data = response.json()
|
| 84 |
-
if 'search' in data and data['search']:
|
| 85 |
-
return f"Wikidata: {data['search'][0].get('description', 'No description available')}"
|
| 86 |
-
except Exception as e:
|
| 87 |
-
print(f"Wikidata search failed: {e}")
|
| 88 |
-
|
| 89 |
-
# Skip DuckDuckGo entirely due to consistent timeouts
|
| 90 |
-
# Return knowledge-based response
|
| 91 |
-
return f"External search unavailable. Based on available knowledge, I'll attempt to answer '{query}' using my training data. Note: This information may not be current."
|
| 92 |
|
| 93 |
# --- Basic Agent Definition ---
|
| 94 |
# ----- THIS IS WERE YOU CAN BUILD WHAT YOU WANT ------
|
|
@@ -114,9 +97,9 @@ class SlpMultiAgent:
|
|
| 114 |
|
| 115 |
# Create only essential agents with reduced complexity
|
| 116 |
research_agent = CodeAgent(
|
| 117 |
-
tools=[KnowledgeBaseTool()],
|
| 118 |
model=model,
|
| 119 |
-
additional_authorized_imports=["re", "datetime"],
|
| 120 |
max_steps=2, # Reduced steps for cost
|
| 121 |
name="ResearchAgent",
|
| 122 |
verbosity_level=0,
|
|
@@ -139,11 +122,11 @@ class SlpMultiAgent:
|
|
| 139 |
temperature=0.0,
|
| 140 |
max_tokens=500
|
| 141 |
),
|
| 142 |
-
tools=[KnowledgeBaseTool()],
|
| 143 |
managed_agents=[research_agent, solver_agent], # Only 2 agents
|
| 144 |
name="ManagerAgent",
|
| 145 |
description="Efficient manager for quick problem solving.",
|
| 146 |
-
additional_authorized_imports=["re", "math"],
|
| 147 |
planning_interval=1, # Faster planning
|
| 148 |
verbosity_level=0, # Reduce verbosity
|
| 149 |
max_steps=4, # Drastically reduced steps
|
|
@@ -162,15 +145,16 @@ class SlpMultiAgent:
|
|
| 162 |
lambda: manager_agent.run(f"""
|
| 163 |
Question: {short_question}
|
| 164 |
|
| 165 |
-
|
| 166 |
-
|
| 167 |
-
|
| 168 |
-
|
| 169 |
-
|
| 170 |
|
| 171 |
-
|
|
|
|
| 172 |
|
| 173 |
-
|
| 174 |
""")
|
| 175 |
)
|
| 176 |
break # Success, exit retry loop
|
|
|
|
| 48 |
return f"Knowledge base: {info}. Use this context to answer questions about {topic}."
|
| 49 |
return f"No specific knowledge base entry for '{topic}'. Use general reasoning."
|
| 50 |
|
| 51 |
+
class WikipediaSearchTool(Tool):
|
| 52 |
+
name = "wikipedia_search"
|
| 53 |
+
description = "Search Wikipedia for information"
|
| 54 |
+
inputs = {"query": {"type": "string", "description": "The search query for Wikipedia"}}
|
| 55 |
output_type = "string"
|
| 56 |
|
| 57 |
def __init__(self):
|
| 58 |
super().__init__()
|
|
|
|
|
|
|
|
|
|
| 59 |
self.is_initialized = True
|
| 60 |
|
| 61 |
def forward(self, query: str) -> str:
|
| 62 |
+
"""Search Wikipedia with simple fallback."""
|
|
|
|
| 63 |
try:
|
| 64 |
import requests
|
| 65 |
wiki_url = "https://en.wikipedia.org/api/rest_v1/page/summary/" + query.replace(" ", "_")
|
| 66 |
+
response = requests.get(wiki_url, timeout=2)
|
| 67 |
if response.status_code == 200:
|
| 68 |
data = response.json()
|
| 69 |
if 'extract' in data and data['extract']:
|
| 70 |
+
return f"Wikipedia: {data['extract'][:500]}" # Limit length
|
| 71 |
except Exception as e:
|
| 72 |
print(f"Wikipedia search failed: {e}")
|
| 73 |
|
| 74 |
+
return f"Wikipedia search unavailable for '{query}'. Use your knowledge to answer."
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 75 |
|
| 76 |
# --- Basic Agent Definition ---
|
| 77 |
# ----- THIS IS WERE YOU CAN BUILD WHAT YOU WANT ------
|
|
|
|
| 97 |
|
| 98 |
# Create only essential agents with reduced complexity
|
| 99 |
research_agent = CodeAgent(
|
| 100 |
+
tools=[KnowledgeBaseTool(), WikipediaSearchTool()],
|
| 101 |
model=model,
|
| 102 |
+
additional_authorized_imports=["re", "datetime", "requests"],
|
| 103 |
max_steps=2, # Reduced steps for cost
|
| 104 |
name="ResearchAgent",
|
| 105 |
verbosity_level=0,
|
|
|
|
| 122 |
temperature=0.0,
|
| 123 |
max_tokens=500
|
| 124 |
),
|
| 125 |
+
tools=[KnowledgeBaseTool(), WikipediaSearchTool()],
|
| 126 |
managed_agents=[research_agent, solver_agent], # Only 2 agents
|
| 127 |
name="ManagerAgent",
|
| 128 |
description="Efficient manager for quick problem solving.",
|
| 129 |
+
additional_authorized_imports=["re", "math", "requests"],
|
| 130 |
planning_interval=1, # Faster planning
|
| 131 |
verbosity_level=0, # Reduce verbosity
|
| 132 |
max_steps=4, # Drastically reduced steps
|
|
|
|
| 145 |
lambda: manager_agent.run(f"""
|
| 146 |
Question: {short_question}
|
| 147 |
|
| 148 |
+
Available tools:
|
| 149 |
+
- knowledge_base(topic): Get structured knowledge
|
| 150 |
+
- wikipedia_search(query): Search Wikipedia
|
| 151 |
+
- ResearchAgent: For factual research
|
| 152 |
+
- SolverAgent: For calculations and logic
|
| 153 |
|
| 154 |
+
Be efficient and direct. Always end with:
|
| 155 |
+
<code>final_answer("your answer")</code>
|
| 156 |
|
| 157 |
+
No explanations - just the answer.
|
| 158 |
""")
|
| 159 |
)
|
| 160 |
break # Success, exit retry loop
|