Scott Cogan commited on
Commit
7153704
·
1 Parent(s): b78f79e
Files changed (1) hide show
  1. app.py +43 -14
app.py CHANGED
@@ -101,14 +101,32 @@ def open_youtube_video(url: str, query: str) -> str:
101
 
102
  @tool
103
  def google_search(query: str) -> str:
104
- '''Performs a Google search for the given query.'''
105
- llm = ChatGoogleGenerativeAI(
106
- model="gemini-2.5-flash-preview-05-20",
107
- max_tokens=8192,
108
- temperature=0
109
- )
110
- response = llm.invoke(query, tools=[GenAITool(google_search={})])
111
- return response.content
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
112
 
113
  def log_message(message: BaseMessage, prefix: str = ""):
114
  """Helper function to log message details."""
@@ -150,12 +168,23 @@ class BasicAgent:
150
  self.tool_executor = ToolExecutor(self.tools)
151
 
152
  # System message
153
- self.sys_msg = SystemMessage('''You are a general AI assistant. I will ask you a question. Only provide YOUR FINAL ANSWER and nothing else.
154
- YOUR FINAL ANSWER should be a number OR as few words as possible OR a comma separated list of numbers and/or strings.
155
- If you are asked for a number, don't use comma to write your number neither use units such as $ or percent sign unless specified otherwise.
156
- If you are asked for a string, don't use articles, neither abbreviations (e.g. for cities), and write the digits in plain text unless specified otherwise.
157
- If you are asked for a comma separated list, apply the above rules depending of whether the element to be put in the list is a number or a string.
158
- You have access to multiple tools and should use as many as you need to answer the question.''')
 
 
 
 
 
 
 
 
 
 
 
159
 
160
  # Create the graph
161
  self.workflow = StateGraph(AgentState)
 
101
 
102
  @tool
103
  def google_search(query: str) -> str:
104
+ '''Performs a web search for the given query using DuckDuckGo.'''
105
+ try:
106
+ response = requests.get(
107
+ "https://api.duckduckgo.com/",
108
+ params={
109
+ "q": query,
110
+ "format": "json",
111
+ "no_html": 1,
112
+ "no_redirect": 1
113
+ }
114
+ )
115
+ response.raise_for_status()
116
+ data = response.json()
117
+
118
+ # Combine Abstract and Related Topics for a comprehensive response
119
+ result = []
120
+ if data.get("Abstract"):
121
+ result.append(data["Abstract"])
122
+ if data.get("RelatedTopics"):
123
+ for topic in data["RelatedTopics"][:3]: # Limit to first 3 related topics
124
+ if "Text" in topic:
125
+ result.append(topic["Text"])
126
+
127
+ return "\n".join(result) if result else "No results found."
128
+ except Exception as e:
129
+ return f"Error performing search: {str(e)}"
130
 
131
  def log_message(message: BaseMessage, prefix: str = ""):
132
  """Helper function to log message details."""
 
168
  self.tool_executor = ToolExecutor(self.tools)
169
 
170
  # System message
171
+ self.sys_msg = SystemMessage('''You are a general AI assistant. I will ask you a question. Follow these steps:
172
+ 1. First, use the google_search tool to find relevant information about the question.
173
+ 2. Analyze the search results to find the specific information needed.
174
+ 3. If needed, use additional tools to gather more information.
175
+ 4. Only after gathering all necessary information, provide YOUR FINAL ANSWER.
176
+
177
+ YOUR FINAL ANSWER should be:
178
+ - A number (not written as a word) if asked for a quantity
179
+ - As few words as possible if asked for a string
180
+ - A comma separated list of numbers and/or strings if asked for a list
181
+
182
+ Rules for formatting:
183
+ - For numbers: Don't use commas or units ($, %, etc.) unless specified
184
+ - For strings: Don't use articles or abbreviations
185
+ - For lists: Apply the above rules based on whether each element is a number or string
186
+
187
+ You MUST use tools to verify information before providing your final answer.''')
188
 
189
  # Create the graph
190
  self.workflow = StateGraph(AgentState)