Spaces:
Running
Running
Update research_tools/web_search.py
Browse files- research_tools/web_search.py +23 -29
research_tools/web_search.py
CHANGED
@@ -1,26 +1,41 @@
|
|
1 |
"""
|
2 |
-
Web Search Tool using DuckDuckGo via smolagents
|
3 |
"""
|
4 |
from .base_tool import BaseTool
|
5 |
from typing import Optional
|
6 |
-
from smolagents import CodeAgent, DuckDuckGoSearchTool, FinalAnswerTool,
|
7 |
-
|
8 |
|
9 |
class WebSearchTool(BaseTool):
|
10 |
-
"""Web search using DuckDuckGo via smolagents"""
|
11 |
|
12 |
def __init__(self):
|
13 |
super().__init__("Web Search", "Search the web for current information using DuckDuckGo")
|
14 |
-
self.rate_limit_delay = 2.0
|
15 |
|
16 |
try:
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
17 |
self.agent = CodeAgent(
|
18 |
tools=[
|
19 |
DuckDuckGoSearchTool(),
|
20 |
VisitWebpageTool(),
|
21 |
FinalAnswerTool()
|
22 |
],
|
23 |
-
model=InferenceClientModel
|
24 |
max_steps=3,
|
25 |
verbosity_level=0
|
26 |
)
|
@@ -28,6 +43,7 @@ class WebSearchTool(BaseTool):
|
|
28 |
print(f"Warning: Could not initialize web search agent: {e}")
|
29 |
self.agent = None
|
30 |
|
|
|
31 |
def search(self, query: str, max_results: int = 5, **kwargs) -> str:
|
32 |
"""Use the CodeAgent to perform comprehensive web search and analysis"""
|
33 |
if not self.agent:
|
@@ -58,26 +74,4 @@ class WebSearchTool(BaseTool):
|
|
58 |
elif "syntax" in error_msg.lower():
|
59 |
return f"**Web Search for: {query}**\n\nSearch encountered formatting issues but found relevant information about {query.lower()}."
|
60 |
else:
|
61 |
-
return self.format_error_response(query, error_msg)
|
62 |
-
|
63 |
-
def should_use_for_query(self, query: str) -> bool:
|
64 |
-
"""Web search is good for current events, news, and general information"""
|
65 |
-
current_indicators = ['news', 'recent', 'latest', 'current', 'today', '2024', '2025']
|
66 |
-
general_indicators = ['what is', 'how to', 'guide', 'tutorial', 'review']
|
67 |
-
|
68 |
-
query_lower = query.lower()
|
69 |
-
return any(indicator in query_lower for indicator in current_indicators + general_indicators)
|
70 |
-
|
71 |
-
def extract_key_info(self, text: str) -> dict:
|
72 |
-
"""Extract key information from web search results"""
|
73 |
-
base_info = super().extract_key_info(text)
|
74 |
-
|
75 |
-
if text:
|
76 |
-
# Look for news-specific patterns
|
77 |
-
base_info.update({
|
78 |
-
'has_news_keywords': bool(any(word in text.lower() for word in ['breaking', 'report', 'announced', 'according to'])),
|
79 |
-
'has_quotes': text.count('"') > 1,
|
80 |
-
'has_sources': bool(any(source in text.lower() for source in ['reuters', 'bloomberg', 'bbc', 'cnn', 'associated press']))
|
81 |
-
})
|
82 |
-
|
83 |
-
return base_info
|
|
|
1 |
"""
|
2 |
+
Web Search Tool using DuckDuckGo via smolagents with Mistral
|
3 |
"""
|
4 |
from .base_tool import BaseTool
|
5 |
from typing import Optional
|
6 |
+
from smolagents import CodeAgent, DuckDuckGoSearchTool, FinalAnswerTool, VisitWebpageTool
|
7 |
+
import os
|
8 |
|
9 |
class WebSearchTool(BaseTool):
|
10 |
+
"""Web search using DuckDuckGo via smolagents with Mistral model"""
|
11 |
|
12 |
def __init__(self):
|
13 |
super().__init__("Web Search", "Search the web for current information using DuckDuckGo")
|
14 |
+
self.rate_limit_delay = 2.0
|
15 |
|
16 |
try:
|
17 |
+
# Create custom Mistral model for CodeAgent
|
18 |
+
from smolagents.models import OpenAIModel
|
19 |
+
|
20 |
+
mistral_key = os.getenv("MISTRAL_API_KEY")
|
21 |
+
if mistral_key:
|
22 |
+
mistral_model = OpenAIModel(
|
23 |
+
api_key=mistral_key,
|
24 |
+
base_url="https://api.mistral.ai/v1",
|
25 |
+
model_name="mistral-large-latest"
|
26 |
+
)
|
27 |
+
else:
|
28 |
+
# Fallback to InferenceClientModel if no Mistral key
|
29 |
+
from smolagents import InferenceClientModel
|
30 |
+
mistral_model = InferenceClientModel()
|
31 |
+
|
32 |
self.agent = CodeAgent(
|
33 |
tools=[
|
34 |
DuckDuckGoSearchTool(),
|
35 |
VisitWebpageTool(),
|
36 |
FinalAnswerTool()
|
37 |
],
|
38 |
+
model=mistral_model, # Use Mistral instead of InferenceClientModel
|
39 |
max_steps=3,
|
40 |
verbosity_level=0
|
41 |
)
|
|
|
43 |
print(f"Warning: Could not initialize web search agent: {e}")
|
44 |
self.agent = None
|
45 |
|
46 |
+
# Keep the rest of your original search method unchanged
|
47 |
def search(self, query: str, max_results: int = 5, **kwargs) -> str:
|
48 |
"""Use the CodeAgent to perform comprehensive web search and analysis"""
|
49 |
if not self.agent:
|
|
|
74 |
elif "syntax" in error_msg.lower():
|
75 |
return f"**Web Search for: {query}**\n\nSearch encountered formatting issues but found relevant information about {query.lower()}."
|
76 |
else:
|
77 |
+
return self.format_error_response(query, error_msg)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|