Surajgjadhav's picture
Upload agent.py
564fee6 verified
raw
history blame contribute delete
3.19 kB
from langgraph.prebuilt import ToolNode
from retriever import (
get_file,
extract_image_info,
file_retriever_tool,
fetch_text_from_url,
excel_data_retriever,
download_file_from_url,
image_decoder,
csv_data_retriever,
)
from tools import (
search_tool,
calc,
wiki_search,
arxiv_search,
run_python_code,
get_image_captioning,
)
from typing import List, TypedDict, Annotated, Optional
from langchain_core.messages import AnyMessage, SystemMessage
from langgraph.graph.message import add_messages
from langgraph.graph import START, StateGraph
from langgraph.prebuilt import tools_condition
from dotenv import load_dotenv
from langchain_huggingface import HuggingFaceEndpoint, ChatHuggingFace
load_dotenv()
MODEL_NAME = "Qwen/Qwen3-Next-80B-A3B-Thinking"
SYSTEM_PROMPT = """
You are a literary data assistant.
## Capabilities
- `fetch_text_from_url`: loads document text from a URL into the conversation.
- `search_tool`: search tool to access information from internet
- `calc`: Calulate expression
- `run_python_code`: execute given python code and return result of execution
- `wiki_search`: search documets on Wikipedia
- `arxiv_search`: Search research papaers on arxiv
- `download_file_from_url`: download and stoere file from url
- `extract_image_info`: extract information from image
- `file_retriever_tool`: extract file from GAIA API for given task_id
- `fetch_text_from_url`: fetch textual info from URL
- `excel_data_retriever`: retreieve data from excel file
- `image_decoder`: convert image from url to base64 decoded image
- `get_image_captioning`: get captioning for gibven image urls
- `csv_data_retriever`: retrieve data from csv file
"""
llm = HuggingFaceEndpoint(
repo_id="Qwen/Qwen2.5-Coder-32B-Instruct",
)
class AgentState(TypedDict):
file_name: Optional[str]
task_id: Optional[str]
messages: Annotated[list[AnyMessage], add_messages]
tools = [
search_tool,
calc,
run_python_code,
wiki_search,
arxiv_search,
download_file_from_url,
extract_image_info,
file_retriever_tool,
fetch_text_from_url,
excel_data_retriever,
image_decoder,
get_image_captioning,
csv_data_retriever,
]
tool_node = ToolNode(tools)
def assistant(state: AgentState):
sys_msg = SystemMessage(content=SYSTEM_PROMPT)
return {
"messages": [chat_with_tools.invoke([sys_msg] + state["messages"])],
"file_name": state["file_name"],
"task_id": state["task_id"],
}
chat = ChatHuggingFace(llm=llm, verbose=True)
chat_with_tools = chat.bind_tools(tools)
def build_agent():
## The graph
builder = StateGraph(AgentState)
# Define nodes: these do the work
builder.add_node("assistant", assistant)
builder.add_node("tools", ToolNode(tools))
# Define edges: these determine how the control flow moves
builder.add_edge(START, "assistant")
builder.add_conditional_edges(
"assistant",
# If the latest message requires a tool, route to tools
# Otherwise, provide a direct response
tools_condition,
)
builder.add_edge("tools", "assistant")
return builder.compile()