Spaces:
Runtime error
Runtime error
Update agent.py
Browse files
agent.py
CHANGED
|
@@ -1,19 +1,14 @@
|
|
| 1 |
"""
|
| 2 |
HF Agents Course — Unit 4
|
| 3 |
-
GAIA Level 1 Agent (
|
| 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.
|
| 27 |
-
max_tokens=
|
| 28 |
)
|
| 29 |
|
| 30 |
-
# =========================
|
| 31 |
-
# System Prompt(GAIA 专用)
|
| 32 |
-
# =========================
|
| 33 |
SYSTEM_PROMPT = (
|
| 34 |
"You are a GAIA benchmark agent.\n"
|
| 35 |
-
"
|
| 36 |
-
"Do NOT explain
|
| 37 |
-
"
|
| 38 |
-
"
|
| 39 |
-
"
|
|
|
|
| 40 |
)
|
| 41 |
|
| 42 |
-
#
|
| 43 |
-
|
| 44 |
-
|
| 45 |
-
|
| 46 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 47 |
|
| 48 |
-
def
|
| 49 |
-
|
| 50 |
-
|
| 51 |
-
|
| 52 |
|
| 53 |
-
|
| 54 |
-
|
| 55 |
-
graph.set_entry_point("agent")
|
| 56 |
-
graph.add_edge("agent", END)
|
| 57 |
-
return graph.compile()
|
| 58 |
|
| 59 |
-
|
|
|
|
|
|
|
| 60 |
|
| 61 |
-
|
| 62 |
-
|
| 63 |
-
#
|
| 64 |
def agent(question: str, files: list[str] | None = None) -> str:
|
| 65 |
-
|
| 66 |
-
|
| 67 |
-
|
| 68 |
-
|
| 69 |
-
|
| 70 |
-
|
| 71 |
-
|
|
|
|
|
|
|
|
|
|
| 72 |
|
| 73 |
-
|
| 74 |
-
|
| 75 |
|
| 76 |
-
|
| 77 |
-
|
|
|
|
| 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"
|