olaf-bu commited on
Commit
f4d8679
·
1 Parent(s): cf6e09d

improved web search

Browse files
Files changed (2) hide show
  1. app.py +1 -0
  2. tools.py +33 -2
app.py CHANGED
@@ -120,6 +120,7 @@ def run_and_submit_all(profile: Optional[gr.OAuthProfile]):
120
  file_path = None
121
 
122
  submitted_answer = agent(question_text, file_path)
 
123
  answers_payload.append({'task_id': task_id,
124
  'submitted_answer': submitted_answer}
125
  )
 
120
  file_path = None
121
 
122
  submitted_answer = agent(question_text, file_path)
123
+ looger.info('task: %s: answer "%s"', task_id, submitted_answer)
124
  answers_payload.append({'task_id': task_id,
125
  'submitted_answer': submitted_answer}
126
  )
tools.py CHANGED
@@ -2,7 +2,8 @@ import logging
2
  from typing import List
3
  import os
4
 
5
- from smolagents import Tool, DuckDuckGoSearchTool, PythonInterpreterTool, VisitWebpageTool, WikipediaSearchTool
 
6
 
7
  import whisper
8
  from youtube_transcript_api import YouTubeTranscriptApi
@@ -93,6 +94,36 @@ class YouTubeTranscriptionTool(Tool):
93
  return ' '.join([entry['text'] for entry in transcript])
94
 
95
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
96
  def get_tools() -> List[Tool]:
97
  """
98
  Returns a list of available tools for the agent.
@@ -101,7 +132,7 @@ def get_tools() -> List[Tool]:
101
  List[Tool]: List of initialized tool instances.
102
  """
103
  try:
104
- tools = [DuckDuckGoSearchTool(),
105
  PythonInterpreterTool(),
106
  WikipediaSearchTool(),
107
  VisitWebpageTool(),
 
2
  from typing import List
3
  import os
4
 
5
+ from smolagents import Tool, PythonInterpreterTool, VisitWebpageTool, WikipediaSearchTool
6
+ from duckduckgo_search import DDGS
7
 
8
  import whisper
9
  from youtube_transcript_api import YouTubeTranscriptApi
 
94
  return ' '.join([entry['text'] for entry in transcript])
95
 
96
 
97
+ class ReliableDuckDuckGoSearchTool(Tool):
98
+ """Reliable search for web content.
99
+
100
+ especially try to avoid the ratelimit problems of the lite duckduckgosearch.
101
+
102
+ Args:
103
+ query (str): query to search with
104
+
105
+ Returns:
106
+ str: the query results
107
+ """
108
+
109
+ name = 'duckduckgo_search'
110
+ description = 'Performs a web search using DuckDuckGo and returns the top result.'
111
+
112
+ def __init__(self, num_results=5):
113
+ self.num_results = num_results
114
+
115
+ def forward(self, query: str) -> str:
116
+ results_text = []
117
+ with DDGS() as ddgs:
118
+ results = ddgs.text(query, max_results=self.num_results)
119
+ for r in results:
120
+ results_text.append(f"{r['title']}: {r['href']}")
121
+
122
+ if not results_text:
123
+ return 'No results found.'
124
+ return '\n'.join(results_text)
125
+
126
+
127
  def get_tools() -> List[Tool]:
128
  """
129
  Returns a list of available tools for the agent.
 
132
  List[Tool]: List of initialized tool instances.
133
  """
134
  try:
135
+ tools = [ReliableDuckDuckGoSearchTool(),
136
  PythonInterpreterTool(),
137
  WikipediaSearchTool(),
138
  VisitWebpageTool(),