Update agent.py
Browse files
agent.py
CHANGED
|
@@ -32,130 +32,130 @@ def build_gemini_llm():
|
|
| 32 |
raise ValueError("GOOGLE_API_KEY environment variable is not set.")
|
| 33 |
return ChatGoogleGenerativeAI(model = "gemini-3.7-flash", temperature = 0, max_output_tokens = 1025, include_thoughts=True)
|
| 34 |
|
| 35 |
-
@tool
|
| 36 |
-
def extract_text_from_image(img_path: str) -> str:
|
| 37 |
-
|
| 38 |
-
|
| 39 |
-
|
| 40 |
-
|
| 41 |
-
|
| 42 |
-
|
| 43 |
-
|
| 44 |
-
|
| 45 |
-
|
| 46 |
-
|
| 47 |
-
|
| 48 |
-
|
| 49 |
-
|
| 50 |
-
|
| 51 |
-
|
| 52 |
-
|
| 53 |
-
|
| 54 |
-
|
| 55 |
-
|
| 56 |
-
|
| 57 |
-
|
| 58 |
-
|
| 59 |
-
|
| 60 |
-
|
| 61 |
-
|
| 62 |
-
|
| 63 |
-
|
| 64 |
-
|
| 65 |
-
|
| 66 |
-
|
| 67 |
-
|
| 68 |
-
|
| 69 |
-
|
| 70 |
-
|
| 71 |
-
|
| 72 |
-
|
| 73 |
-
|
| 74 |
-
|
| 75 |
-
|
| 76 |
-
|
| 77 |
-
|
| 78 |
-
|
| 79 |
-
@tool
|
| 80 |
-
def download_and_read_file(task_id: str) -> str:
|
| 81 |
-
|
| 82 |
-
|
| 83 |
-
|
| 84 |
-
|
| 85 |
-
|
| 86 |
-
|
| 87 |
-
|
| 88 |
-
|
| 89 |
-
|
| 90 |
-
|
| 91 |
-
|
| 92 |
-
|
| 93 |
-
|
| 94 |
-
|
| 95 |
-
|
| 96 |
-
|
| 97 |
-
|
| 98 |
-
|
| 99 |
-
|
| 100 |
-
|
| 101 |
-
|
| 102 |
-
|
| 103 |
-
|
| 104 |
-
|
| 105 |
-
|
| 106 |
-
|
| 107 |
-
|
| 108 |
-
|
| 109 |
-
|
| 110 |
-
|
| 111 |
-
|
| 112 |
-
|
| 113 |
-
|
| 114 |
-
|
| 115 |
-
|
| 116 |
-
|
| 117 |
-
|
| 118 |
-
|
| 119 |
-
|
| 120 |
-
|
| 121 |
-
|
| 122 |
-
|
| 123 |
-
|
| 124 |
-
|
| 125 |
-
|
| 126 |
-
|
| 127 |
-
|
| 128 |
-
|
| 129 |
-
|
| 130 |
-
|
| 131 |
-
|
| 132 |
-
|
| 133 |
-
|
| 134 |
-
|
| 135 |
-
|
| 136 |
-
|
| 137 |
-
|
| 138 |
|
| 139 |
-
|
| 140 |
-
|
| 141 |
-
|
| 142 |
-
|
| 143 |
-
|
| 144 |
-
|
| 145 |
-
|
| 146 |
-
|
| 147 |
-
|
| 148 |
-
|
| 149 |
-
|
| 150 |
-
|
| 151 |
|
| 152 |
@tool
|
| 153 |
-
def wikipedia_search(query: str):
|
| 154 |
"""
|
| 155 |
-
Search wikipedia for a query and return
|
| 156 |
-
Takes a string query as the
|
| 157 |
Args:
|
| 158 |
-
query (str):
|
| 159 |
"""
|
| 160 |
try:
|
| 161 |
search_results = WikipediaLoader(query=query, load_max_docs=3).load()
|
|
@@ -164,7 +164,7 @@ def wikipedia_search(query: str):
|
|
| 164 |
return f"No Wikipedia results found for {query}. Consider another query or try a web search."
|
| 165 |
print("Result from wikipedia:")
|
| 166 |
print(search_results)
|
| 167 |
-
return "
|
| 168 |
f"Title: {doc.metadata.get('title', 'Unknown')}\n"
|
| 169 |
f"Content: {doc.page_content}"
|
| 170 |
for doc in search_results
|
|
|
|
| 32 |
raise ValueError("GOOGLE_API_KEY environment variable is not set.")
|
| 33 |
return ChatGoogleGenerativeAI(model = "gemini-3.7-flash", temperature = 0, max_output_tokens = 1025, include_thoughts=True)
|
| 34 |
|
| 35 |
+
# @tool
|
| 36 |
+
# def extract_text_from_image(img_path: str) -> str:
|
| 37 |
+
# """
|
| 38 |
+
# Describe the image and extract any text in it.
|
| 39 |
+
# Args:
|
| 40 |
+
# img_path (str): the path to the image file.
|
| 41 |
+
# """
|
| 42 |
+
# all_text = ""
|
| 43 |
+
# try:
|
| 44 |
+
# # Read image and encode as base64
|
| 45 |
+
# with open(img_path, "rb") as image_file:
|
| 46 |
+
# image_bytes = image_file.read()
|
| 47 |
+
|
| 48 |
+
# image_base64 = base64.b64encode(image_bytes).decode("utf-8")
|
| 49 |
+
|
| 50 |
+
# # Prepare the prompt including the base64 image data
|
| 51 |
+
# message = [
|
| 52 |
+
# HumanMessage(
|
| 53 |
+
# content=[
|
| 54 |
+
# {
|
| 55 |
+
# "type": "text",
|
| 56 |
+
# "text": (
|
| 57 |
+
# "Describe the image and extract any text in it."
|
| 58 |
+
# ),
|
| 59 |
+
# },
|
| 60 |
+
# {
|
| 61 |
+
# "type": "image_url",
|
| 62 |
+
# "image_url": {
|
| 63 |
+
# "url": f"data:image/png;base64,{image_base64}"
|
| 64 |
+
# },
|
| 65 |
+
# },
|
| 66 |
+
# ]
|
| 67 |
+
# )
|
| 68 |
+
# ]
|
| 69 |
+
# response = model.invoke(message)
|
| 70 |
+
# # Append extracted text
|
| 71 |
+
# all_text += response.text + "\n\n"
|
| 72 |
+
# return all_text.strip()
|
| 73 |
+
# except Exception as e:
|
| 74 |
+
# # A butler should handle errors gracefully
|
| 75 |
+
# error_msg = f"Error extracting text: {str(e)}"
|
| 76 |
+
# print(error_msg)
|
| 77 |
+
# return ""
|
| 78 |
+
|
| 79 |
+
# @tool
|
| 80 |
+
# def download_and_read_file(task_id: str) -> str:
|
| 81 |
+
# """
|
| 82 |
+
# Download and read the file attached to the GAIA task its contents.
|
| 83 |
+
# Always call this first if there is a file attached to a GAIA Task.
|
| 84 |
+
# Args:
|
| 85 |
+
# task_id (str): The ID of the GAIA task.
|
| 86 |
+
# Returns:
|
| 87 |
+
# str: The contents of the file as a string.
|
| 88 |
+
# """
|
| 89 |
+
|
| 90 |
+
# try:
|
| 91 |
+
# # Download the file from the GAIA API
|
| 92 |
+
# response = requests.get(f"{FILES_URL}/{task_id}", timeout = 10)
|
| 93 |
+
# response.raise_for_status()
|
| 94 |
+
|
| 95 |
+
# # Determine the file type and read its contents
|
| 96 |
+
# content_disposition = response.headers.get("content-disposition", "")
|
| 97 |
+
# content_type = response.headers.get("content-type", "")
|
| 98 |
+
# filename = None
|
| 99 |
+
# if "filename=" in content_disposition:
|
| 100 |
+
# filename = content_disposition.split("filename=")[1].strip('"')
|
| 101 |
+
|
| 102 |
+
# if not filename:
|
| 103 |
+
# filename = f"{task_id}.bin"
|
| 104 |
+
|
| 105 |
+
# ext = Path(filename).suffix.lower()
|
| 106 |
+
|
| 107 |
+
# if ext in(".txt", ".py", ".json", ".md", ".ymal", ".html", ".xml", ""):
|
| 108 |
+
# return response.text
|
| 109 |
+
|
| 110 |
+
# if ext == ".xlsx" or "xlsx" in content_type:
|
| 111 |
+
# import pandas as pd
|
| 112 |
+
# with tempfile.NamedTemporaryFile(suffix=".xlsx", delete=False) as file:
|
| 113 |
+
# file.write(response.content)
|
| 114 |
+
# temp_path = file.name
|
| 115 |
+
|
| 116 |
+
# read_file = pd.read_excel(temp_path)
|
| 117 |
+
# return read_file.to_string()
|
| 118 |
+
# if ext == ".csv" or "csv" in content_type:
|
| 119 |
+
# import pandas as pd
|
| 120 |
+
# with tempfile.NamedTemporaryFile(suffix=".csv", delete=False) as file:
|
| 121 |
+
# file.write(response.content)
|
| 122 |
+
# temp_path = file.name
|
| 123 |
+
# read_file = pd.read_csv(temp_path)
|
| 124 |
+
# return read_file.to_string()
|
| 125 |
+
# if ext == ".csv" or "csv" in content_type:
|
| 126 |
+
# import pandas as pd
|
| 127 |
+
# with tempfile.NamedTemporaryFile(suffix=".csv", delete=False) as file:
|
| 128 |
+
# file.write(response.content)
|
| 129 |
+
# temp_path = file.name
|
| 130 |
+
# read_file = pd.read_csv(temp_path)
|
| 131 |
+
# return read_file.to_string()
|
| 132 |
+
# if ext == ".jpg" or ext == ".jpeg" or ext == ".png" or "image" in content_type:
|
| 133 |
+
# from PIL import Image
|
| 134 |
+
# with tempfile.NamedTemporaryFile(suffix=ext, delete=False) as file:
|
| 135 |
+
# file.write(response.content)
|
| 136 |
+
# temp_path = file.name
|
| 137 |
+
# return extract_text_from_image(temp_path)
|
| 138 |
|
| 139 |
+
# # Unsupported file type
|
| 140 |
+
# return (
|
| 141 |
+
# f"Unsupported file type: {content_type}. "
|
| 142 |
+
# "I downloaded the file successfully, but I don't know "
|
| 143 |
+
# "how to extract its contents."
|
| 144 |
+
# )
|
| 145 |
+
# except requests.RequestException as e:
|
| 146 |
+
# return f"Failed to download file: {e}"
|
| 147 |
+
# except Exception as e:
|
| 148 |
+
# return f"Failed to read file: {e}"
|
| 149 |
+
# except Exception as e:
|
| 150 |
+
# return f"error downloading or reading file: {str(e)}"
|
| 151 |
|
| 152 |
@tool
|
| 153 |
+
def wikipedia_search(query: str) -> str:
|
| 154 |
"""
|
| 155 |
+
Search wikipedia for a query and return results.
|
| 156 |
+
Takes a string query as the keywords to search
|
| 157 |
Args:
|
| 158 |
+
query (str): Keywords you want to search.
|
| 159 |
"""
|
| 160 |
try:
|
| 161 |
search_results = WikipediaLoader(query=query, load_max_docs=3).load()
|
|
|
|
| 164 |
return f"No Wikipedia results found for {query}. Consider another query or try a web search."
|
| 165 |
print("Result from wikipedia:")
|
| 166 |
print(search_results)
|
| 167 |
+
return "---\n".join(
|
| 168 |
f"Title: {doc.metadata.get('title', 'Unknown')}\n"
|
| 169 |
f"Content: {doc.page_content}"
|
| 170 |
for doc in search_results
|