File size: 3,187 Bytes
f6ac7a6
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
e13c0b3
f6ac7a6
 
e13c0b3
 
f6ac7a6
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
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
80
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
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()