sqfoo commited on
Commit
c41d5cf
·
verified ·
1 Parent(s): d1fc653

Update agent.py

Browse files
Files changed (1) hide show
  1. agent.py +85 -33
agent.py CHANGED
@@ -1,4 +1,5 @@
1
  import os
 
2
  from typing import TypedDict, List, Dict, Any, Optional
3
  from langchain.agents import create_tool_calling_agent, AgentExecutor, initialize_agent
4
  from langchain_google_genai import ChatGoogleGenerativeAI
@@ -11,13 +12,16 @@ from langchain_community.tools import DuckDuckGoSearchResults
11
  from langchain_community.document_loaders import ImageCaptionLoader
12
  import requests, time
13
  import pandas as pd
14
- from pypdf import PdfReader
15
  from langchain_community.tools import WikipediaQueryRun
16
  from langchain_community.utilities import WikipediaAPIWrapper
17
  from langchain_community.document_loaders import YoutubeLoader
18
  from langchain_community.document_loaders import UnstructuredExcelLoader
19
  from langchain_community.document_loaders import AssemblyAIAudioTranscriptLoader
20
 
 
 
 
21
  @tool
22
  def web_search(query: str) -> str:
23
  """Allows search through DuckDuckGo.
@@ -67,38 +71,79 @@ def youtube_transcript(video_url: str) -> str:
67
 
68
  # 4. File Reading
69
  @tool
70
- def read_file(dir: str) -> str:
71
- """Read the content of the file
72
  Args:
73
- dir: the filepath
74
  """
75
- extension = dir.split['.'][-1]
76
- if extension == 'pdf':
77
- reader = PdfReader(dir)
78
- contents = [p.extract_text() for p in reader.pages]
79
- return "\n".join(contents)
80
- else:
81
- with open(dir) as f:
82
- return f.read()
83
 
84
  @tool
85
- def excel_read(dir: str) -> str:
86
- """Read the content of the excel file
87
  Args:
88
- dir: the filepath
89
  """
90
- loader = UnstructuredExcelLoader(dir, mode="elements")
91
- docs = loader.load()
92
- contents = [doc.page_content for doc in docs]
93
- return "\n".join(contents)
94
-
 
 
 
 
 
 
 
 
 
 
 
 
 
 
95
  @tool
96
- def mp3_listen(dir: str) -> str:
97
- """Listen to the provided mp3 file
98
  Args:
99
- dir: the filepath
100
  """
101
- loader = AssemblyAIAudioTranscriptLoader(file_path=dir)
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
102
  docs = loader.load()
103
  contents = [doc.page_content for doc in docs]
104
  return "\n".join(contents)
@@ -171,7 +216,7 @@ class BasicAgent:
171
  max_tokens=128,
172
  timeout=None,
173
  max_retries=2,
174
- google_api_key="AIzaSyAxVUPaGJIgdxB46ZR0RWPKSjB9a63Z80o",
175
  # other params...
176
  )
177
  # System Prompt for few shot prompting
@@ -188,13 +233,16 @@ class BasicAgent:
188
  - visit_webpage: visit the given webpage url by passing the url as input
189
  - wiki_search: wiki search the content of the query by passing the query as input if the question asks for wiki search it
190
  - 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
191
- - read_file: read the content of the attached file by passing the file directory as input
192
- - excel_read: read the content of the attached excel file by passing the file directory as input
193
- - mp3_listen: listen to the content of the attached mp3 file by passing the file directory as input
194
- - image_caption: understand the visual content of the attached image by passing the image directory as input
195
- - python_tool: run the python code
 
 
 
196
  """
197
- self.tools = [web_search, visit_webpage, wiki_search, youtube_transcript, read_file, excel_read, mp3_listen, image_caption, python_tool]
198
  self.prompt = ChatPromptTemplate.from_messages([
199
  ("system", self.sys_prompt),
200
  ("human", "{input}")
@@ -209,12 +257,16 @@ class BasicAgent:
209
  )
210
  print("BasicAgent initialized.")
211
 
212
- def __call__(self, question: str) -> str:
 
213
  print(f"Agent received question (first 50 chars): {question[:50]}...")
214
  # response = self.agent_exe.invoke({"input": f"Question: {question}"})
215
  # fixed_answer = response['message'][-1].content
216
  time.sleep(15)
217
- fixed_answer = self.agent.run(question)
 
 
 
218
  # fixed_answer = "This is a default answer."
219
  print(f"Agent returning fixed answer: {fixed_answer}")
220
  return fixed_answer
 
1
  import os
2
+ from dotenv import load_dotenv
3
  from typing import TypedDict, List, Dict, Any, Optional
4
  from langchain.agents import create_tool_calling_agent, AgentExecutor, initialize_agent
5
  from langchain_google_genai import ChatGoogleGenerativeAI
 
12
  from langchain_community.document_loaders import ImageCaptionLoader
13
  import requests, time
14
  import pandas as pd
15
+ from pathlib import Path
16
  from langchain_community.tools import WikipediaQueryRun
17
  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.
 
71
 
72
  # 4. File Reading
73
  @tool
74
+ def read_file(task_id: str) -> str:
75
+ """First download the file, then read its content
76
  Args:
77
+ dir: the task_id
78
  """
79
+ file_url = f'{DEFAULT_API_URL}/files/{task_id}'
80
+ r = requests.get(file_url, timeout=15, allow_redirects=True)
81
+ with open('temp', "wb") as fp:
82
+ fp.write(r.content)
83
+ with open('temp') as f:
84
+ return f.read()
 
 
85
 
86
  @tool
87
+ def excel_read(task_id: str) -> str:
88
+ """First download the excel file, then read its content
89
  Args:
90
+ dir: the task_id
91
  """
92
+ try:
93
+ file_url = f'{DEFAULT_API_URL}/files/{task_id}'
94
+ r = requests.get(file_url, timeout=15, allow_redirects=True)
95
+ with open('temp.xlsx', "wb") as fp:
96
+ fp.write(r.content)
97
+ # Read the Excel file
98
+ df = pd.read_excel('temp.xlsx')
99
+ # Run various analyses based on the query
100
+ result = (
101
+ f"Excel file loaded with {len(df)} rows and {len(df.columns)} columns.\n"
102
+ )
103
+ result += f"Columns: {', '.join(df.columns)}\n\n"
104
+ # Add summary statistics
105
+ result += "Summary statistics:\n"
106
+ result += str(df.describe())
107
+ return result
108
+ except Exception as e:
109
+ return f"Error analyzing Excel file: {str(e)}"
110
+
111
  @tool
112
+ def csv_read(task_id: str) -> str:
113
+ """First download the csv file, then read its content
114
  Args:
115
+ dir: the task_id
116
  """
117
+ try:
118
+ file_url = f'{DEFAULT_API_URL}/files/{task_id}'
119
+ r = requests.get(file_url, timeout=15, allow_redirects=True)
120
+ with open('temp.csv', "wb") as fp:
121
+ fp.write(r.content)
122
+ # Read the CSV file
123
+ df = pd.read_csv(temp.csv)
124
+ # Run various analyses based on the query
125
+ result = (
126
+ f"Excel file loaded with {len(df)} rows and {len(df.columns)} columns.\n"
127
+ )
128
+ result += f"Columns: {', '.join(df.columns)}\n\n"
129
+ # Add summary statistics
130
+ result += "Summary statistics:\n"
131
+ result += str(df.describe())
132
+ return result
133
+ except Exception as e:
134
+ return f"Error analyzing CSV file: {str(e)}"
135
+
136
+ @tool
137
+ def mp3_listen(task_id: str) -> str:
138
+ """First download the mp3 file, then listen to it
139
+ Args:
140
+ dir: the task_id
141
+ """
142
+ file_url = f'{DEFAULT_API_URL}/files/{task_id}'
143
+ r = requests.get(file_url, timeout=15, allow_redirects=True)
144
+ with open('temp.mp3', "wb") as fp:
145
+ fp.write(r.content)
146
+ loader = AssemblyAIAudioTranscriptLoader(file_path="temp.mp3", api_key=os.getenv("AssemblyAI_API_KEY"))
147
  docs = loader.load()
148
  contents = [doc.page_content for doc in docs]
149
  return "\n".join(contents)
 
216
  max_tokens=128,
217
  timeout=None,
218
  max_retries=2,
219
+ google_api_key=os.getenv("GEMINI_API_KEY"),
220
  # other params...
221
  )
222
  # System Prompt for few shot prompting
 
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
238
+ - csv_read: read the content of the attached csv file by passing the TASK-ID as input
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}")
 
257
  )
258
  print("BasicAgent initialized.")
259
 
260
+ def __call__(self, task: dict) -> str:
261
+ task_id, question, Level, file_name = task["task_id"], task["question"], Level, task["file_name"]
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