Upload agent.py

#429
by BiGuan - opened
Files changed (1) hide show
  1. agent.py +77 -0
agent.py ADDED
@@ -0,0 +1,77 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ """
2
+ HF Agents Course — Unit 4
3
+ GAIA Level 1 Agent (LangGraph + Qwen3.5-35B-A3B)
4
+ """
5
+
6
+ import os
7
+ from dotenv import load_dotenv
8
+ from langchain_openai import ChatOpenAI
9
+ from langgraph.graph import StateGraph, END
10
+ from langchain_core.messages import HumanMessage
11
+ from typing import TypedDict, Annotated
12
+ import operator
13
+
14
+ # =========================
15
+ # 基础配置
16
+ # =========================
17
+ load_dotenv()
18
+
19
+ AGICTO_API_KEY = os.getenv("AGICTO_API_KEY")
20
+ AGICTO_BASE_URL = os.getenv("AGICTO_BASE_URL", "https://agicto.com/v1")
21
+
22
+ llm = ChatOpenAI(
23
+ model="qwen3.5-35b-a3b",
24
+ openai_api_key=AGICTO_API_KEY,
25
+ openai_api_base=AGICTO_BASE_URL,
26
+ temperature=0.1,
27
+ max_tokens=1024,
28
+ )
29
+
30
+ # =========================
31
+ # System Prompt(GAIA 专用)
32
+ # =========================
33
+ SYSTEM_PROMPT = (
34
+ "You are a GAIA benchmark agent.\n"
35
+ "Think step-by-step internally.\n"
36
+ "Do NOT explain your reasoning.\n"
37
+ "Output ONLY the final exact answer.\n"
38
+ "No units, no punctuation, no extra words.\n"
39
+ "If the question includes a file URL, use its content."
40
+ )
41
+
42
+ # =========================
43
+ # LangGraph State
44
+ # =========================
45
+ class AgentState(TypedDict):
46
+ messages: Annotated[list, operator.add]
47
+
48
+ def build_graph():
49
+ def run_agent(state: AgentState) -> AgentState:
50
+ response = llm.invoke(state["messages"])
51
+ return {"messages": [response]}
52
+
53
+ graph = StateGraph(AgentState)
54
+ graph.add_node("agent", run_agent)
55
+ graph.set_entry_point("agent")
56
+ graph.add_edge("agent", END)
57
+ return graph.compile()
58
+
59
+ graph = build_graph()
60
+
61
+ # =========================
62
+ # ✅ HF 官方接口
63
+ # =========================
64
+ def agent(question: str, files: list[str] | None = None) -> str:
65
+ """
66
+ 必须保留此函数签名,HF Unit 4 评分器会直接调用。
67
+ """
68
+ messages = [
69
+ {"role": "system", "content": SYSTEM_PROMPT},
70
+ {"role": "user", "content": question},
71
+ ]
72
+
73
+ result = graph.invoke({"messages": messages})
74
+ answer = result["messages"][-1].content
75
+
76
+ # ✅ GAIA 硬性规则
77
+ return answer.strip().splitlines()[0]