Spaces:
Sleeping
Sleeping
Update agent.py
Browse files
agent.py
CHANGED
|
@@ -34,7 +34,7 @@ def visit_webpage(url: str) -> str:
|
|
| 34 |
"""
|
| 35 |
try:
|
| 36 |
response = requests.get(url, timeout=5)
|
| 37 |
-
return response.text
|
| 38 |
except Exception as e:
|
| 39 |
return f"[ERROR fetching {url}]: {str(e)}"
|
| 40 |
|
|
@@ -66,15 +66,8 @@ def youtube_transcript(video_url: str) -> str:
|
|
| 66 |
# 4. File Reading
|
| 67 |
@tool
|
| 68 |
def read_file(dir: str) -> str:
|
| 69 |
-
"""Read the content of the provided file
|
| 70 |
-
Args:
|
| 71 |
-
dir: the filepath
|
| 72 |
-
"""
|
| 73 |
extension = dir.split['.'][-1]
|
| 74 |
-
if extension == '
|
| 75 |
-
dataframe = pd.read_excel(dir)
|
| 76 |
-
return dataframe.to_string()
|
| 77 |
-
elif extension == 'pdf':
|
| 78 |
reader = PdfReader(dir)
|
| 79 |
contents = [p.extract_text() for p in reader.pages]
|
| 80 |
return "\n".join(contents)
|
|
@@ -82,6 +75,31 @@ def read_file(dir: str) -> str:
|
|
| 82 |
with open(dir) as f:
|
| 83 |
return f.read()
|
| 84 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 85 |
# 5. Image Open
|
| 86 |
@tool
|
| 87 |
def image_caption(dir: str) -> str:
|
|
@@ -95,6 +113,46 @@ def image_caption(dir: str) -> str:
|
|
| 95 |
|
| 96 |
# 2. Coding
|
| 97 |
from langchain_experimental.tools import PythonREPLTool
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 98 |
# 3. Multi-Modality
|
| 99 |
|
| 100 |
# ("human", f"Question: {question}\nReport to validate: {final_answer}")
|
|
@@ -123,24 +181,16 @@ class BasicAgent:
|
|
| 123 |
- wiki_search: wiki search the content of the query by passing the query as input if the question asks for wiki search it
|
| 124 |
- 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
|
| 125 |
- read_file: read the content of the attached file by passing the file directory as input
|
|
|
|
|
|
|
| 126 |
- image_caption: understand the visual content of the attached image by passing the image directory as input
|
| 127 |
-
-
|
| 128 |
-
|
| 129 |
-
|
| 130 |
-
|
| 131 |
-
|
| 132 |
-
ACTION: Find the Youtube url in the provided TASK info, that is 'https://www.youtube.com/watch?v=L1vXCYZAYYM'. Call youtube_transcript tool to extract video transcript with ht extracted Youtube url. Use LLM to understand the retrived transcript.
|
| 133 |
-
|
| 134 |
-
TASK: How many Grammy Awards that Taylor Swift has won.
|
| 135 |
-
ACTION: Call the web_search tools with the query: 'how many Grammy Awards that Taylor Swift has won.' to extract the answer.
|
| 136 |
-
|
| 137 |
-
TASK: How much the total expense in this spreadsheet?
|
| 138 |
-
ACTION: Call the read_file tool to extract the content of the provided spreadfile. Then, use LLM to extract the amount of every expense and sum them up.
|
| 139 |
-
|
| 140 |
-
TASK: How many All England Title that Lee Chong Wei won?
|
| 141 |
-
ACTION: Call wiki_search with the query: "Lee Chong Wei". Extract the relevant row of All England Title and count how many rows is there.
|
| 142 |
"""
|
| 143 |
-
self.tools = [web_search, visit_webpage, wiki_search, youtube_transcript, read_file, image_caption,
|
| 144 |
self.prompt = ChatPromptTemplate.from_messages([
|
| 145 |
("system", self.sys_prompt),
|
| 146 |
("human", "{input}")
|
|
@@ -152,14 +202,14 @@ class BasicAgent:
|
|
| 152 |
verbose=True,
|
| 153 |
system_prompt=self.prompt
|
| 154 |
)
|
| 155 |
-
|
| 156 |
|
| 157 |
def __call__(self, question: str) -> str:
|
| 158 |
-
|
| 159 |
# response = self.agent_exe.invoke({"input": f"Question: {question}"})
|
| 160 |
# fixed_answer = response['message'][-1].content
|
| 161 |
time.sleep(15)
|
| 162 |
fixed_answer = self.agent.run(question)
|
| 163 |
# fixed_answer = "This is a default answer."
|
| 164 |
-
|
| 165 |
return fixed_answer
|
|
|
|
| 34 |
"""
|
| 35 |
try:
|
| 36 |
response = requests.get(url, timeout=5)
|
| 37 |
+
return response.text[:5000]
|
| 38 |
except Exception as e:
|
| 39 |
return f"[ERROR fetching {url}]: {str(e)}"
|
| 40 |
|
|
|
|
| 66 |
# 4. File Reading
|
| 67 |
@tool
|
| 68 |
def read_file(dir: str) -> str:
|
|
|
|
|
|
|
|
|
|
|
|
|
| 69 |
extension = dir.split['.'][-1]
|
| 70 |
+
if extension == 'pdf':
|
|
|
|
|
|
|
|
|
|
| 71 |
reader = PdfReader(dir)
|
| 72 |
contents = [p.extract_text() for p in reader.pages]
|
| 73 |
return "\n".join(contents)
|
|
|
|
| 75 |
with open(dir) as f:
|
| 76 |
return f.read()
|
| 77 |
|
| 78 |
+
@tool
|
| 79 |
+
from langchain_community.document_loaders import UnstructuredExcelLoader
|
| 80 |
+
# from langchain_core.documents import Document
|
| 81 |
+
def excel_read(dir: str) -> str:
|
| 82 |
+
"""Read the content of the excel file
|
| 83 |
+
Args:
|
| 84 |
+
dir: the filepath
|
| 85 |
+
"""
|
| 86 |
+
loader = UnstructuredExcelLoader(dir, mode="elements")
|
| 87 |
+
docs = loader.load()
|
| 88 |
+
contents = [doc.page_content for doc in docs]
|
| 89 |
+
return "\n".join(contents)
|
| 90 |
+
|
| 91 |
+
@tool
|
| 92 |
+
from langchain_community.document_loaders import AssemblyAIAudioTranscriptLoader
|
| 93 |
+
def mp3_listen(dir: str) -> str:
|
| 94 |
+
"""Listen to the provided mp3 file
|
| 95 |
+
Args:
|
| 96 |
+
dir: the filepath
|
| 97 |
+
"""
|
| 98 |
+
loader = AssemblyAIAudioTranscriptLoader(file_path=dir)
|
| 99 |
+
docs = loader.load()
|
| 100 |
+
contents = [doc.page_content for doc in docs]
|
| 101 |
+
return "\n".join(contents)
|
| 102 |
+
|
| 103 |
# 5. Image Open
|
| 104 |
@tool
|
| 105 |
def image_caption(dir: str) -> str:
|
|
|
|
| 113 |
|
| 114 |
# 2. Coding
|
| 115 |
from langchain_experimental.tools import PythonREPLTool
|
| 116 |
+
python_tool = PythonREPLTool()
|
| 117 |
+
|
| 118 |
+
@tool
|
| 119 |
+
def multiply(a: float, b: float) -> float:
|
| 120 |
+
"""Multiply two numbers.
|
| 121 |
+
Args:
|
| 122 |
+
a: first float
|
| 123 |
+
b: second float
|
| 124 |
+
"""
|
| 125 |
+
return a * b
|
| 126 |
+
|
| 127 |
+
@tool
|
| 128 |
+
def add(a: float, b: float) -> float:
|
| 129 |
+
"""Add two numbers.
|
| 130 |
+
Args:
|
| 131 |
+
a: first float
|
| 132 |
+
b: second float
|
| 133 |
+
"""
|
| 134 |
+
return a + b
|
| 135 |
+
|
| 136 |
+
@tool
|
| 137 |
+
def subtract(a: float, b: float) -> float:
|
| 138 |
+
"""Subtract two numbers.
|
| 139 |
+
Args:
|
| 140 |
+
a: first float
|
| 141 |
+
b: second float
|
| 142 |
+
"""
|
| 143 |
+
return a - b
|
| 144 |
+
|
| 145 |
+
@tool
|
| 146 |
+
def divide(a: float, b: float) -> float:
|
| 147 |
+
"""Divide two numbers.
|
| 148 |
+
Args:
|
| 149 |
+
a: first float
|
| 150 |
+
b: second float
|
| 151 |
+
"""
|
| 152 |
+
if b == 0:
|
| 153 |
+
raise ValueError("Cannot divide by zero.")
|
| 154 |
+
return a / b
|
| 155 |
+
|
| 156 |
# 3. Multi-Modality
|
| 157 |
|
| 158 |
# ("human", f"Question: {question}\nReport to validate: {final_answer}")
|
|
|
|
| 181 |
- wiki_search: wiki search the content of the query by passing the query as input if the question asks for wiki search it
|
| 182 |
- 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
|
| 183 |
- read_file: read the content of the attached file by passing the file directory as input
|
| 184 |
+
- excel_read: read the content of the attached excel file by passing the file directory as input
|
| 185 |
+
- mp3_listen: listen to the content of the attached mp3 file by passing the file directory as input
|
| 186 |
- image_caption: understand the visual content of the attached image by passing the image directory as input
|
| 187 |
+
- python_tool: run the python code
|
| 188 |
+
- multiply: multiply two numbers, A and B
|
| 189 |
+
- add: add two numbers, A and B
|
| 190 |
+
- subtract: Subtract A by B with passing A as the first argument
|
| 191 |
+
- divide: Divide A by B with passing A as the first argument
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 192 |
"""
|
| 193 |
+
self.tools = [web_search, visit_webpage, wiki_search, youtube_transcript, read_file, excel_read, mp3_listen, image_caption, python_tool, multiply, add, subtract, divide]
|
| 194 |
self.prompt = ChatPromptTemplate.from_messages([
|
| 195 |
("system", self.sys_prompt),
|
| 196 |
("human", "{input}")
|
|
|
|
| 202 |
verbose=True,
|
| 203 |
system_prompt=self.prompt
|
| 204 |
)
|
| 205 |
+
prfloat("BasicAgent initialized.")
|
| 206 |
|
| 207 |
def __call__(self, question: str) -> str:
|
| 208 |
+
prfloat(f"Agent received question (first 50 chars): {question[:50]}...")
|
| 209 |
# response = self.agent_exe.invoke({"input": f"Question: {question}"})
|
| 210 |
# fixed_answer = response['message'][-1].content
|
| 211 |
time.sleep(15)
|
| 212 |
fixed_answer = self.agent.run(question)
|
| 213 |
# fixed_answer = "This is a default answer."
|
| 214 |
+
prfloat(f"Agent returning fixed answer: {fixed_answer}")
|
| 215 |
return fixed_answer
|