Commit ·
5dce464
1
Parent(s): 0c8d19f
Added filereader, removed wikipedia. 30%
Browse files- .gitignore +3 -1
- agent.py +36 -6
- app.py +31 -0
.gitignore
CHANGED
|
@@ -173,4 +173,6 @@ cython_debug/
|
|
| 173 |
# PyPI configuration file
|
| 174 |
.pypirc
|
| 175 |
|
| 176 |
-
.langgraph_api/
|
|
|
|
|
|
|
|
|
| 173 |
# PyPI configuration file
|
| 174 |
.pypirc
|
| 175 |
|
| 176 |
+
.langgraph_api/
|
| 177 |
+
|
| 178 |
+
files/
|
agent.py
CHANGED
|
@@ -8,14 +8,13 @@ from langgraph.prebuilt import tools_condition, ToolNode
|
|
| 8 |
from langchain_core.runnables import RunnableConfig
|
| 9 |
|
| 10 |
# Wikepedia tool
|
| 11 |
-
from langchain_community.tools import WikipediaQueryRun
|
| 12 |
-
from langchain_community.utilities import WikipediaAPIWrapper
|
| 13 |
from langchain_community.document_loaders import WikipediaLoader
|
| 14 |
-
from langchain_community.retrievers import WikipediaRetriever
|
| 15 |
|
| 16 |
# Tavily web search
|
| 17 |
from langchain_community.tools.tavily_search import TavilySearchResults
|
| 18 |
|
|
|
|
|
|
|
| 19 |
|
| 20 |
load_dotenv()
|
| 21 |
|
|
@@ -51,7 +50,7 @@ def wikipedia_search(query: str) -> str:
|
|
| 51 |
|
| 52 |
for next_doc in documents:
|
| 53 |
formatted_doc = f'<Document source="{next_doc.metadata["source"]}" title="{next_doc.metadata.get("title", "")}"/>\n{next_doc.page_content}\n</Document>'
|
| 54 |
-
formatted_search_docs
|
| 55 |
|
| 56 |
result = f"{{wiki_results: {formatted_search_docs}}}"
|
| 57 |
|
|
@@ -89,7 +88,30 @@ def web_search(query: str) -> str:
|
|
| 89 |
return result
|
| 90 |
|
| 91 |
|
| 92 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 93 |
|
| 94 |
|
| 95 |
# --- GRAPH ---
|
|
@@ -180,7 +202,7 @@ if __name__ == "__main__":
|
|
| 180 |
for m in messages["messages"]:
|
| 181 |
m.pretty_print()
|
| 182 |
|
| 183 |
-
|
| 184 |
|
| 185 |
print("******** TESTING WEB SEARCH TOOL ********")
|
| 186 |
# expected answer is "Samuel"
|
|
@@ -189,3 +211,11 @@ if __name__ == "__main__":
|
|
| 189 |
messages = graph.invoke({"messages": messages})
|
| 190 |
for m in messages["messages"]:
|
| 191 |
m.pretty_print()
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 8 |
from langchain_core.runnables import RunnableConfig
|
| 9 |
|
| 10 |
# Wikepedia tool
|
|
|
|
|
|
|
| 11 |
from langchain_community.document_loaders import WikipediaLoader
|
|
|
|
| 12 |
|
| 13 |
# Tavily web search
|
| 14 |
from langchain_community.tools.tavily_search import TavilySearchResults
|
| 15 |
|
| 16 |
+
# Python loader
|
| 17 |
+
from langchain_community.document_loaders import PythonLoader
|
| 18 |
|
| 19 |
load_dotenv()
|
| 20 |
|
|
|
|
| 50 |
|
| 51 |
for next_doc in documents:
|
| 52 |
formatted_doc = f'<Document source="{next_doc.metadata["source"]}" title="{next_doc.metadata.get("title", "")}"/>\n{next_doc.page_content}\n</Document>'
|
| 53 |
+
formatted_search_docs = formatted_search_docs + formatted_doc
|
| 54 |
|
| 55 |
result = f"{{wiki_results: {formatted_search_docs}}}"
|
| 56 |
|
|
|
|
| 88 |
return result
|
| 89 |
|
| 90 |
|
| 91 |
+
# https://python.langchain.com/api_reference/community/document_loaders/langchain_community.document_loaders.python.PythonLoader.html
|
| 92 |
+
def python_file_reader(file_name: str) -> str:
|
| 93 |
+
"""Reads a python file and returns the content
|
| 94 |
+
|
| 95 |
+
Args:
|
| 96 |
+
file_name: the filename to read
|
| 97 |
+
"""
|
| 98 |
+
file_path = os.path.join(os.path.dirname(__file__), "files", file_name)
|
| 99 |
+
loader = PythonLoader(file_path=file_path)
|
| 100 |
+
documents = loader.load()
|
| 101 |
+
formatted_search_docs = "\n\n---\n\n"
|
| 102 |
+
|
| 103 |
+
for next_doc in documents:
|
| 104 |
+
formatted_doc = (
|
| 105 |
+
f'<Document source="{file_name}"/>\n{next_doc.page_content}\n</Document>'
|
| 106 |
+
)
|
| 107 |
+
formatted_search_docs = formatted_search_docs + formatted_doc
|
| 108 |
+
|
| 109 |
+
result = f"{{python_code: {formatted_search_docs}}}"
|
| 110 |
+
|
| 111 |
+
return result
|
| 112 |
+
|
| 113 |
+
|
| 114 |
+
tools = [meaning_of_life, web_search, python_file_reader]
|
| 115 |
|
| 116 |
|
| 117 |
# --- GRAPH ---
|
|
|
|
| 202 |
for m in messages["messages"]:
|
| 203 |
m.pretty_print()
|
| 204 |
|
| 205 |
+
|
| 206 |
|
| 207 |
print("******** TESTING WEB SEARCH TOOL ********")
|
| 208 |
# expected answer is "Samuel"
|
|
|
|
| 211 |
messages = graph.invoke({"messages": messages})
|
| 212 |
for m in messages["messages"]:
|
| 213 |
m.pretty_print()
|
| 214 |
+
"""
|
| 215 |
+
|
| 216 |
+
print("******** PYTHON LOAD TOOL ********")
|
| 217 |
+
question = "what does this python code do? filename is f918266a-b3e0-4914-865d-4faa564f1aef.py"
|
| 218 |
+
messages = [HumanMessage(content=question)]
|
| 219 |
+
messages = graph.invoke({"messages": messages})
|
| 220 |
+
for m in messages["messages"]:
|
| 221 |
+
m.pretty_print()
|
app.py
CHANGED
|
@@ -80,6 +80,31 @@ def run_and_submit_all(profile: gr.OAuthProfile | None):
|
|
| 80 |
print(f"An unexpected error occurred fetching questions: {e}")
|
| 81 |
return f"An unexpected error occurred fetching questions: {e}", None
|
| 82 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 83 |
# 3. Run your Agent
|
| 84 |
results_log = []
|
| 85 |
answers_payload = []
|
|
@@ -87,6 +112,12 @@ def run_and_submit_all(profile: gr.OAuthProfile | None):
|
|
| 87 |
for item in questions_data:
|
| 88 |
task_id = item.get("task_id")
|
| 89 |
question_text = item.get("question")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 90 |
if not task_id or question_text is None:
|
| 91 |
print(f"Skipping item with missing task_id or question: {item}")
|
| 92 |
continue
|
|
|
|
| 80 |
print(f"An unexpected error occurred fetching questions: {e}")
|
| 81 |
return f"An unexpected error occurred fetching questions: {e}", None
|
| 82 |
|
| 83 |
+
# 2.5 Download Files
|
| 84 |
+
for item in questions_data:
|
| 85 |
+
task_id = item.get("task_id")
|
| 86 |
+
file_name = item.get("file_name")
|
| 87 |
+
file_url = f"{api_url}/files/{task_id}"
|
| 88 |
+
try:
|
| 89 |
+
if file_name != "":
|
| 90 |
+
file_path = os.path.join("files", file_name)
|
| 91 |
+
if not os.path.exists(file_path):
|
| 92 |
+
print(f"Downloading file from {file_url} for task {task_id}...")
|
| 93 |
+
response = requests.get(file_url, timeout=15)
|
| 94 |
+
response.raise_for_status()
|
| 95 |
+
file_path = os.path.join("files", file_name)
|
| 96 |
+
with open(file_path, "wb") as f:
|
| 97 |
+
f.write(response.content)
|
| 98 |
+
print(f"Downloaded {file_name}.")
|
| 99 |
+
else:
|
| 100 |
+
print(f"File {file_name} already exists. Skipping download.")
|
| 101 |
+
except requests.exceptions.RequestException as e:
|
| 102 |
+
print(f"Error downloading file for task {task_id}: {e}")
|
| 103 |
+
except Exception as e:
|
| 104 |
+
print(
|
| 105 |
+
f"An unexpected error occurred downloading file for task {task_id}: {e}"
|
| 106 |
+
)
|
| 107 |
+
|
| 108 |
# 3. Run your Agent
|
| 109 |
results_log = []
|
| 110 |
answers_payload = []
|
|
|
|
| 112 |
for item in questions_data:
|
| 113 |
task_id = item.get("task_id")
|
| 114 |
question_text = item.get("question")
|
| 115 |
+
|
| 116 |
+
# if there is a file add it to the question
|
| 117 |
+
file_name = item.get("file_name")
|
| 118 |
+
if file_name != "":
|
| 119 |
+
question_text += " File to use is " + file_name
|
| 120 |
+
|
| 121 |
if not task_id or question_text is None:
|
| 122 |
print(f"Skipping item with missing task_id or question: {item}")
|
| 123 |
continue
|