BiGuan commited on
Commit
05c0199
·
verified ·
1 Parent(s): 19c8f73

Update agent.py

Browse files
Files changed (1) hide show
  1. agent.py +46 -47
agent.py CHANGED
@@ -1,19 +1,14 @@
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")
@@ -23,55 +18,59 @@ 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]
 
1
  """
2
  HF Agents Course — Unit 4
3
+ GAIA Level 1 Agent (Qwen3.5-35B-A3B, safe mode)
4
  """
5
 
6
  import os
7
+ import re
8
+ import requests
9
  from dotenv import load_dotenv
10
  from langchain_openai import ChatOpenAI
 
 
 
 
11
 
 
 
 
12
  load_dotenv()
13
 
14
  AGICTO_API_KEY = os.getenv("AGICTO_API_KEY")
 
18
  model="qwen3.5-35b-a3b",
19
  openai_api_key=AGICTO_API_KEY,
20
  openai_api_base=AGICTO_BASE_URL,
21
+ temperature=0.0,
22
+ max_tokens=512,
23
  )
24
 
 
 
 
25
  SYSTEM_PROMPT = (
26
  "You are a GAIA benchmark agent.\n"
27
+ "Answer directly.\n"
28
+ "Do NOT explain.\n"
29
+ "Do NOT output reasoning.\n"
30
+ "Do NOT output tags.\n"
31
+ "Output ONLY the final answer.\n"
32
+ "No punctuation. No units. No extra words."
33
  )
34
 
35
+ # ---------- 工具 ----------
36
+ def download_file(url: str) -> str:
37
+ try:
38
+ r = requests.get(url, timeout=10)
39
+ if r.ok:
40
+ return r.text[:8000]
41
+ except Exception:
42
+ pass
43
+ return ""
44
 
45
+ def clean_output(text: str) -> str:
46
+ # 去除 Qwen3 思考链
47
+ text = re.sub(r".*?", "", text, flags=re.S)
48
+ text = text.strip()
49
 
50
+ # 只保留第一行
51
+ text = text.splitlines()[0]
 
 
 
52
 
53
+ # 去除所有非必要字符
54
+ for ch in ".,;:!?()[]{}<>\"'":
55
+ text = text.replace(ch, "")
56
 
57
+ return text.strip() or "0"
58
+
59
+ # ---------- ✅ HF 官方接口 ----------
60
  def agent(question: str, files: list[str] | None = None) -> str:
61
+ try:
62
+ context = ""
63
+ if files:
64
+ for f in files:
65
+ context += download_file(f) + "\n"
66
+
67
+ messages = [
68
+ {"role": "system", "content": SYSTEM_PROMPT},
69
+ {"role": "user", "content": f"{context}{question}".strip()},
70
+ ]
71
 
72
+ res = llm.invoke(messages)
73
+ return clean_output(res.content)
74
 
75
+ except Exception:
76
+ return "0"