Mohammad Haghir commited on
Commit ·
528bce2
1
Parent(s): 4bf1a0c
file handling
Browse files- agent_utils.py +33 -0
- app.py +3 -3
- requirements.txt +2 -1
agent_utils.py
CHANGED
|
@@ -3,6 +3,11 @@ import arxiv
|
|
| 3 |
import os
|
| 4 |
from tavily import TavilyClient
|
| 5 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 6 |
def wiki_ret(question: str) -> str:
|
| 7 |
""" Retrieve docs from wikipedia """
|
| 8 |
|
|
@@ -59,3 +64,31 @@ def tavily_ret(query: str, max_results: int = 3) -> str:
|
|
| 59 |
summaries.append(f"Title: {item['title']}\nURL: {item['url']}\nSnippet: {item['content']}\n")
|
| 60 |
|
| 61 |
return "\n\n".join(summaries)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 3 |
import os
|
| 4 |
from tavily import TavilyClient
|
| 5 |
|
| 6 |
+
import requests
|
| 7 |
+
from PIL import Image
|
| 8 |
+
from io import BytesIO
|
| 9 |
+
|
| 10 |
+
|
| 11 |
def wiki_ret(question: str) -> str:
|
| 12 |
""" Retrieve docs from wikipedia """
|
| 13 |
|
|
|
|
| 64 |
summaries.append(f"Title: {item['title']}\nURL: {item['url']}\nSnippet: {item['content']}\n")
|
| 65 |
|
| 66 |
return "\n\n".join(summaries)
|
| 67 |
+
|
| 68 |
+
|
| 69 |
+
|
| 70 |
+
def handle_file_tool(input: dict) -> str:
|
| 71 |
+
"""Processes file attached to a question"""
|
| 72 |
+
question = input.get("question", "")
|
| 73 |
+
file_url = input.get("file", "")
|
| 74 |
+
|
| 75 |
+
if not file_url:
|
| 76 |
+
return "No file provided."
|
| 77 |
+
|
| 78 |
+
response = requests.get(file_url)
|
| 79 |
+
if not response.ok:
|
| 80 |
+
return f"Failed to download file: {file_url}"
|
| 81 |
+
|
| 82 |
+
# Handle image files
|
| 83 |
+
if file_url.endswith((".png", ".jpg", ".jpeg")):
|
| 84 |
+
image = Image.open(BytesIO(response.content))
|
| 85 |
+
return f"Received image for question: '{question}'. (Add vision model here.)"
|
| 86 |
+
|
| 87 |
+
# Handle PDFs
|
| 88 |
+
elif file_url.endswith(".pdf"):
|
| 89 |
+
with open("temp.pdf", "wb") as f:
|
| 90 |
+
f.write(response.content)
|
| 91 |
+
return f"Received PDF for question: '{question}'. (Add PDF parser here.)"
|
| 92 |
+
|
| 93 |
+
else:
|
| 94 |
+
return f"Unsupported file type: {file_url}"
|
app.py
CHANGED
|
@@ -17,7 +17,7 @@ from langchain_core.messages import HumanMessage
|
|
| 17 |
from langgraph.graph import START, END, StateGraph
|
| 18 |
from langgraph.prebuilt import ToolNode, tools_condition
|
| 19 |
|
| 20 |
-
from agent_utils import wiki_ret, arxiv_ret, tavily_ret
|
| 21 |
|
| 22 |
# (Keep Constants as is)
|
| 23 |
# --- Constants ---
|
|
@@ -25,7 +25,7 @@ DEFAULT_API_URL = "https://agents-course-unit4-scoring.hf.space"
|
|
| 25 |
|
| 26 |
groq_api_key = os.getenv("GROQ_API_KEY")
|
| 27 |
|
| 28 |
-
llm = ChatGroq(api_key=groq_api_key, model="
|
| 29 |
|
| 30 |
class GraphState(TypedDict):
|
| 31 |
messages: str #Annotated[list, operator.add]
|
|
@@ -82,7 +82,7 @@ class BasicAgent:
|
|
| 82 |
def create_graph(self):
|
| 83 |
builder = StateGraph(GraphState)
|
| 84 |
builder.add_node("agent", self.agent)
|
| 85 |
-
builder.add_node("tools", ToolNode(tools = [wiki_ret, arxiv_ret, tavily_ret]))
|
| 86 |
|
| 87 |
builder.add_edge(START, "agent")
|
| 88 |
builder.add_conditional_edges("agent", tools_condition)
|
|
|
|
| 17 |
from langgraph.graph import START, END, StateGraph
|
| 18 |
from langgraph.prebuilt import ToolNode, tools_condition
|
| 19 |
|
| 20 |
+
from agent_utils import wiki_ret, arxiv_ret, tavily_ret, handle_file_tool
|
| 21 |
|
| 22 |
# (Keep Constants as is)
|
| 23 |
# --- Constants ---
|
|
|
|
| 25 |
|
| 26 |
groq_api_key = os.getenv("GROQ_API_KEY")
|
| 27 |
|
| 28 |
+
llm = ChatGroq(api_key=groq_api_key, model="llama-3.3-70b-versatile")
|
| 29 |
|
| 30 |
class GraphState(TypedDict):
|
| 31 |
messages: str #Annotated[list, operator.add]
|
|
|
|
| 82 |
def create_graph(self):
|
| 83 |
builder = StateGraph(GraphState)
|
| 84 |
builder.add_node("agent", self.agent)
|
| 85 |
+
builder.add_node("tools", ToolNode(tools = [wiki_ret, arxiv_ret, tavily_ret, handle_file_tool]))
|
| 86 |
|
| 87 |
builder.add_edge(START, "agent")
|
| 88 |
builder.add_conditional_edges("agent", tools_condition)
|
requirements.txt
CHANGED
|
@@ -6,4 +6,5 @@ langchain-core
|
|
| 6 |
wikipedia
|
| 7 |
langgraph
|
| 8 |
arxiv
|
| 9 |
-
tavily-python
|
|
|
|
|
|
| 6 |
wikipedia
|
| 7 |
langgraph
|
| 8 |
arxiv
|
| 9 |
+
tavily-python
|
| 10 |
+
PIL
|