sqfoo commited on
Commit
1fb2c6c
·
verified ·
1 Parent(s): 8c241fa

Update agent.py

Browse files
Files changed (1) hide show
  1. agent.py +29 -6
agent.py CHANGED
@@ -18,12 +18,14 @@ from langchain_community.utilities import WikipediaAPIWrapper
18
  from langchain_community.document_loaders import YoutubeLoader
19
  from langchain_community.document_loaders import UnstructuredExcelLoader
20
  from langchain_community.document_loaders import AssemblyAIAudioTranscriptLoader
 
 
21
 
22
  load_dotenv()
23
  DEFAULT_API_URL = "https://agents-course-unit4-scoring.hf.space"
24
 
25
  @tool
26
- def web_search(query: str) -> str:
27
  """Allows search through DuckDuckGo.
28
  Args:
29
  query: what you want to search
@@ -32,6 +34,16 @@ def web_search(query: str) -> str:
32
  results = search.invoke(query)
33
  return "\n".join(results)
34
 
 
 
 
 
 
 
 
 
 
 
35
  @tool
36
  def visit_webpage(url: str) -> str:
37
  """Fetches raw HTML content of a web page.
@@ -54,6 +66,14 @@ def wiki_search(query: str) -> str:
54
  wikipediatool = WikipediaQueryRun(api_wrapper=api_wrapper)
55
  return wikipediatool.run({"query": query})
56
 
 
 
 
 
 
 
 
 
57
 
58
  @tool
59
  def youtube_transcript(video_url: str) -> str:
@@ -211,7 +231,7 @@ def divide(a: float, b: float) -> float:
211
  class BasicAgent:
212
  def __init__(self):
213
  self.model = ChatGoogleGenerativeAI(
214
- model="gemini-2.0-flash",
215
  temperature=0,
216
  max_tokens=128,
217
  timeout=None,
@@ -229,9 +249,11 @@ class BasicAgent:
229
  If you are asked for a comma separated list, apply the above rules depending of whether the element to put in the list is a number or a string.
230
 
231
  You have access to the following tools:
232
- - web_search: web search the content of the query by passing the query as input
 
233
  - visit_webpage: visit the given webpage url by passing the url as input
234
  - wiki_search: wiki search the content of the query by passing the query as input if the question asks for wiki search it
 
235
  - youtube_transcript: fetch the transcript of the Youtube video by passing the video url as input if the question asks for watching a Youtube video
236
  - read_file: read the content of the attached file by passing the TASK-ID as input
237
  - excel_read: read the content of the attached excel file by passing the TASK-ID as input
@@ -239,10 +261,10 @@ class BasicAgent:
239
  - mp3_listen: listen to the content of the attached mp3 file by passing the TASK-ID as input
240
  - image_caption: understand the visual content of the attached image by passing the TASK-ID as input
241
  - python_tool: run the python code
242
-
243
  If Task ID is included in the question, remember to call the relevant read tools [ie. read_file, excel_read, csv_read, mp3_listen, image_caption]
244
  """
245
- self.tools = [web_search, visit_webpage, wiki_search, youtube_transcript, read_file, excel_read, csv_read, mp3_listen, image_caption, python_tool]
246
  self.prompt = ChatPromptTemplate.from_messages([
247
  ("system", self.sys_prompt),
248
  ("human", "{input}")
@@ -262,11 +284,12 @@ class BasicAgent:
262
  print(f"Agent received question (first 50 chars): {question[:50]}...")
263
  # response = self.agent_exe.invoke({"input": f"Question: {question}"})
264
  # fixed_answer = response['message'][-1].content
265
- time.sleep(15)
266
  if file_name == "" or file_name is None:
267
  fixed_answer = self.agent.run(question)
268
  else:
269
  fixed_answer = self.agent.run(f'{question} with TASK-ID: {task_id}')
270
  # fixed_answer = "This is a default answer."
271
  print(f"Agent returning fixed answer: {fixed_answer}")
 
272
  return fixed_answer
 
18
  from langchain_community.document_loaders import YoutubeLoader
19
  from langchain_community.document_loaders import UnstructuredExcelLoader
20
  from langchain_community.document_loaders import AssemblyAIAudioTranscriptLoader
21
+ from langchain.text_splitter import CharacterTextSplitter
22
+ from langchain_community.utilities import GoogleSerperAPIWrapper
23
 
24
  load_dotenv()
25
  DEFAULT_API_URL = "https://agents-course-unit4-scoring.hf.space"
26
 
27
  @tool
28
+ def duckduck_websearch(query: str) -> str:
29
  """Allows search through DuckDuckGo.
30
  Args:
31
  query: what you want to search
 
34
  results = search.invoke(query)
35
  return "\n".join(results)
36
 
37
+ @tool
38
+ def serper_websearch(query: str) -> str:
39
+ """Allows search through Serper.
40
+ Args:
41
+ query: what you want to search
42
+ """
43
+ search = GoogleSerperAPIWrapper(serper_api_key=os.getenv("SERPER_API_KEY"))
44
+ results = search.run(query)
45
+ return results
46
+
47
  @tool
48
  def visit_webpage(url: str) -> str:
49
  """Fetches raw HTML content of a web page.
 
66
  wikipediatool = WikipediaQueryRun(api_wrapper=api_wrapper)
67
  return wikipediatool.run({"query": query})
68
 
69
+ @tool
70
+ def text_splitter(text: str) -> List[str]:
71
+ """Splits text into chunks using LangChain's CharacterTextSplitter.
72
+ Args:
73
+ text: A string of text to split.
74
+ """
75
+ splitter = CharacterTextSplitter(chunk_size=450, chunk_overlap=10)
76
+ return splitter.split_text(text)
77
 
78
  @tool
79
  def youtube_transcript(video_url: str) -> str:
 
231
  class BasicAgent:
232
  def __init__(self):
233
  self.model = ChatGoogleGenerativeAI(
234
+ model="gemini-2.0-flash-lite",
235
  temperature=0,
236
  max_tokens=128,
237
  timeout=None,
 
249
  If you are asked for a comma separated list, apply the above rules depending of whether the element to put in the list is a number or a string.
250
 
251
  You have access to the following tools:
252
+ - serper_websearch: web search the content of the query by passing the query as input with Serper Search Engine
253
+ - duckduck_websearch: web search the content of the query by passing the query as input with DuckDuckGo Search Engine
254
  - visit_webpage: visit the given webpage url by passing the url as input
255
  - wiki_search: wiki search the content of the query by passing the query as input if the question asks for wiki search it
256
+ - text_splitter: split text into chunks
257
  - youtube_transcript: fetch the transcript of the Youtube video by passing the video url as input if the question asks for watching a Youtube video
258
  - read_file: read the content of the attached file by passing the TASK-ID as input
259
  - excel_read: read the content of the attached excel file by passing the TASK-ID as input
 
261
  - mp3_listen: listen to the content of the attached mp3 file by passing the TASK-ID as input
262
  - image_caption: understand the visual content of the attached image by passing the TASK-ID as input
263
  - python_tool: run the python code
264
+
265
  If Task ID is included in the question, remember to call the relevant read tools [ie. read_file, excel_read, csv_read, mp3_listen, image_caption]
266
  """
267
+ self.tools = [serper_websearch, duckduck_websearch, visit_webpage, wiki_search, text_splitter, youtube_transcript, read_file, excel_read, csv_read, mp3_listen, image_caption, python_tool]
268
  self.prompt = ChatPromptTemplate.from_messages([
269
  ("system", self.sys_prompt),
270
  ("human", "{input}")
 
284
  print(f"Agent received question (first 50 chars): {question[:50]}...")
285
  # response = self.agent_exe.invoke({"input": f"Question: {question}"})
286
  # fixed_answer = response['message'][-1].content
287
+
288
  if file_name == "" or file_name is None:
289
  fixed_answer = self.agent.run(question)
290
  else:
291
  fixed_answer = self.agent.run(f'{question} with TASK-ID: {task_id}')
292
  # fixed_answer = "This is a default answer."
293
  print(f"Agent returning fixed answer: {fixed_answer}")
294
+ time.sleep(60)
295
  return fixed_answer