Spaces:
Sleeping
Sleeping
commit
Browse files- agent.py +112 -131
- requirements.txt +11 -5
agent.py
CHANGED
|
@@ -1,109 +1,99 @@
|
|
| 1 |
# agent.py
|
| 2 |
# =========================================================
|
| 3 |
-
#
|
| 4 |
-
#
|
| 5 |
-
# -
|
| 6 |
-
# -
|
| 7 |
-
# -
|
|
|
|
| 8 |
# =========================================================
|
| 9 |
|
| 10 |
import os
|
| 11 |
import pickle
|
| 12 |
-
from typing import TypedDict
|
| 13 |
|
| 14 |
# ---------------------------------------------------------
|
| 15 |
-
# LangChain / LangGraph
|
| 16 |
# ---------------------------------------------------------
|
| 17 |
-
from langchain_core.tools import create_retriever_tool
|
| 18 |
-
from langchain_community.document_loaders import WikipediaLoader, ArxivLoader
|
| 19 |
-
from langchain_community.vectorstores import FAISS
|
| 20 |
-
from langchain_huggingface import HuggingFaceEmbeddings
|
| 21 |
-
|
| 22 |
from langchain_openai import ChatOpenAI
|
| 23 |
-
from langchain_core.documents import Document
|
| 24 |
from langchain_core.messages import SystemMessage, HumanMessage
|
| 25 |
-
from langchain_core.
|
|
|
|
|
|
|
| 26 |
from langchain_community.tools import DuckDuckGoSearchRun
|
|
|
|
|
|
|
| 27 |
|
| 28 |
from langgraph.graph import START, END, StateGraph
|
| 29 |
-
from langgraph.prebuilt import ToolNode, tools_condition
|
| 30 |
|
| 31 |
|
| 32 |
# =========================================================
|
| 33 |
-
# State ์ ์
|
| 34 |
-
# - tool์ ์ด๋ฏธ ์ฌ์ฉํ๋์ง ์ฌ๋ถ๋ฅผ ๊ธฐ๋ก
|
| 35 |
# =========================================================
|
| 36 |
class AgentState(TypedDict):
|
| 37 |
-
messages:
|
| 38 |
-
used_tools: bool
|
| 39 |
|
| 40 |
|
| 41 |
-
#
|
| 42 |
-
#
|
| 43 |
-
#
|
| 44 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 45 |
|
| 46 |
|
| 47 |
# =========================================================
|
| 48 |
-
#
|
| 49 |
# =========================================================
|
|
|
|
|
|
|
| 50 |
|
| 51 |
-
|
| 52 |
-
def wiki_search(query: str) -> dict:
|
| 53 |
"""
|
| 54 |
-
|
| 55 |
-
|
| 56 |
"""
|
| 57 |
-
|
| 58 |
-
|
| 59 |
-
|
|
|
|
|
|
|
| 60 |
|
| 61 |
|
| 62 |
-
|
| 63 |
-
def arxiv_search(query: str) -> dict:
|
| 64 |
"""
|
| 65 |
-
|
| 66 |
-
|
| 67 |
"""
|
| 68 |
-
|
| 69 |
-
|
| 70 |
-
|
|
|
|
|
|
|
| 71 |
|
| 72 |
|
| 73 |
-
|
| 74 |
-
def web_search(query: str) -> dict:
|
| 75 |
"""
|
| 76 |
-
|
| 77 |
-
|
| 78 |
"""
|
| 79 |
try:
|
| 80 |
-
return
|
| 81 |
except Exception:
|
| 82 |
-
return
|
| 83 |
-
|
| 84 |
-
|
| 85 |
-
TOOLS = [wiki_search, arxiv_search, web_search]
|
| 86 |
-
|
| 87 |
-
|
| 88 |
-
# =========================================================
|
| 89 |
-
# SYSTEM PROMPT (GAIA ๊ท์น ๊ณ ์ )
|
| 90 |
-
# =========================================================
|
| 91 |
-
SYSTEM_PROMPT = """
|
| 92 |
-
You are solving GAIA benchmark questions.
|
| 93 |
-
|
| 94 |
-
You MUST:
|
| 95 |
-
- Use tools if factual information is required.
|
| 96 |
-
- Reason internally but DO NOT reveal reasoning.
|
| 97 |
-
- Output ONLY the final answer.
|
| 98 |
-
- No explanation.
|
| 99 |
-
- No extra text.
|
| 100 |
-
""".strip()
|
| 101 |
-
|
| 102 |
-
SYS_MSG = SystemMessage(content=SYSTEM_PROMPT)
|
| 103 |
|
| 104 |
|
| 105 |
# =========================================================
|
| 106 |
-
# FAISS Retriever (์ ์ง)
|
| 107 |
# =========================================================
|
| 108 |
embeddings = HuggingFaceEmbeddings(
|
| 109 |
model_name="sentence-transformers/all-mpnet-base-v2"
|
|
@@ -114,26 +104,21 @@ if os.path.exists("faiss.pkl"):
|
|
| 114 |
vector_store = pickle.load(f)
|
| 115 |
else:
|
| 116 |
seed_docs = [
|
| 117 |
-
Document(page_content="GAIA questions require
|
| 118 |
]
|
| 119 |
vector_store = FAISS.from_documents(seed_docs, embeddings)
|
| 120 |
with open("faiss.pkl", "wb") as f:
|
| 121 |
pickle.dump(vector_store, f)
|
| 122 |
|
| 123 |
-
retriever_tool = create_retriever_tool(
|
| 124 |
-
retriever=vector_store.as_retriever(),
|
| 125 |
-
name="question_retriever",
|
| 126 |
-
description="Retrieve similar factual questions",
|
| 127 |
-
)
|
| 128 |
-
|
| 129 |
|
| 130 |
# =========================================================
|
| 131 |
-
# LangGraph ๋น๋
|
| 132 |
# =========================================================
|
| 133 |
def build_agent():
|
| 134 |
"""
|
| 135 |
-
|
| 136 |
-
|
|
|
|
| 137 |
"""
|
| 138 |
|
| 139 |
llm = ChatOpenAI(
|
|
@@ -142,81 +127,77 @@ def build_agent():
|
|
| 142 |
max_tokens=128,
|
| 143 |
)
|
| 144 |
|
| 145 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 146 |
|
| 147 |
-
|
| 148 |
-
|
| 149 |
-
|
| 150 |
-
|
| 151 |
-
|
| 152 |
-
|
| 153 |
-
|
| 154 |
-
|
|
|
|
| 155 |
|
| 156 |
-
|
| 157 |
-
|
| 158 |
-
return {
|
| 159 |
-
"messages": [msg],
|
| 160 |
-
"used_tools": state.get("used_tools", False),
|
| 161 |
-
}
|
| 162 |
-
|
| 163 |
-
def tools_node(state: AgentState):
|
| 164 |
-
# Tool์ ์ฌ์ฉํ ์๊ฐ used_tools = True
|
| 165 |
-
return {
|
| 166 |
-
"messages": state["messages"],
|
| 167 |
-
"used_tools": True,
|
| 168 |
-
}
|
| 169 |
-
|
| 170 |
-
# ---------------------
|
| 171 |
-
# Conditional (ํต์ฌ)
|
| 172 |
-
# ---------------------
|
| 173 |
-
def tool_condition_once(state: AgentState):
|
| 174 |
-
"""
|
| 175 |
-
Tool์ ๋ฑ ํ ๋ฒ๋ง ํ์ฉ.
|
| 176 |
-
์ด๋ฏธ ์ฌ์ฉํ๋ค๋ฉด ๋ฌด์กฐ๊ฑด END.
|
| 177 |
-
"""
|
| 178 |
-
if state.get("used_tools"):
|
| 179 |
-
return END
|
| 180 |
-
return tools_condition(state)
|
| 181 |
-
|
| 182 |
-
# ---------------------
|
| 183 |
-
# Graph
|
| 184 |
-
# ---------------------
|
| 185 |
-
builder = StateGraph(AgentState)
|
| 186 |
|
| 187 |
-
|
| 188 |
-
|
| 189 |
-
|
|
|
|
| 190 |
|
| 191 |
-
|
| 192 |
-
builder.add_edge("inject_system", "assistant")
|
| 193 |
|
| 194 |
-
|
| 195 |
-
|
| 196 |
-
|
| 197 |
-
|
|
|
|
|
|
|
|
|
|
| 198 |
|
| 199 |
-
builder.add_edge(
|
|
|
|
| 200 |
|
| 201 |
-
# recursion_limit์ ์์ ์ฅ์น
|
| 202 |
return builder.compile()
|
| 203 |
|
| 204 |
|
| 205 |
# =========================================================
|
| 206 |
-
# Public API (app.py์์
|
| 207 |
# =========================================================
|
| 208 |
class BasicAgent:
|
| 209 |
def __init__(self):
|
| 210 |
self.graph = build_agent()
|
| 211 |
-
print("
|
| 212 |
|
| 213 |
def __call__(self, question: str) -> str:
|
| 214 |
result = self.graph.invoke(
|
| 215 |
{
|
| 216 |
"messages": [HumanMessage(content=question)],
|
| 217 |
-
|
| 218 |
-
},
|
| 219 |
-
# ์ฌ๊ธฐ์ recursion_limit์ ๋ฃ๋๋ค (LangGraph ๋ฒ์ ํธํ)
|
| 220 |
-
config={"recursion_limit": 10},
|
| 221 |
)
|
| 222 |
return result["messages"][-1].content.strip()
|
|
|
|
| 1 |
# agent.py
|
| 2 |
# =========================================================
|
| 3 |
+
# GAIA Benchmark Agent (FINAL, STABLE)
|
| 4 |
+
#
|
| 5 |
+
# - OpenAI GPT-4o-mini
|
| 6 |
+
# - Wikipedia / Arxiv / DuckDuckGo ๊ฒ์ (API Key ๋ถํ์)
|
| 7 |
+
# - HuggingFace Embeddings + FAISS Retriever ์ ์ง
|
| 8 |
+
# - LangGraph ์ฌ์ฉ (๋จ์ผ ๋
ธ๋, ์ฌ๊ท/ํด์ฝ๋ง ์์ ์ ๊ฑฐ)
|
| 9 |
# =========================================================
|
| 10 |
|
| 11 |
import os
|
| 12 |
import pickle
|
| 13 |
+
from typing import TypedDict, List
|
| 14 |
|
| 15 |
# ---------------------------------------------------------
|
| 16 |
+
# LangChain / LangGraph ๊ธฐ๋ณธ ๊ตฌ์ฑ
|
| 17 |
# ---------------------------------------------------------
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 18 |
from langchain_openai import ChatOpenAI
|
|
|
|
| 19 |
from langchain_core.messages import SystemMessage, HumanMessage
|
| 20 |
+
from langchain_core.documents import Document
|
| 21 |
+
|
| 22 |
+
from langchain_community.document_loaders import WikipediaLoader, ArxivLoader
|
| 23 |
from langchain_community.tools import DuckDuckGoSearchRun
|
| 24 |
+
from langchain_community.vectorstores import FAISS
|
| 25 |
+
from langchain_huggingface import HuggingFaceEmbeddings
|
| 26 |
|
| 27 |
from langgraph.graph import START, END, StateGraph
|
|
|
|
| 28 |
|
| 29 |
|
| 30 |
# =========================================================
|
| 31 |
+
# State ์ ์ (LangGraph์ฉ)
|
|
|
|
| 32 |
# =========================================================
|
| 33 |
class AgentState(TypedDict):
|
| 34 |
+
messages: List
|
|
|
|
| 35 |
|
| 36 |
|
| 37 |
+
# =========================================================
|
| 38 |
+
# System Prompt (GAIA ๊ท์น ๊ณ ์ )
|
| 39 |
+
# =========================================================
|
| 40 |
+
SYSTEM_PROMPT = """
|
| 41 |
+
You are solving GAIA benchmark questions.
|
| 42 |
+
|
| 43 |
+
You MUST:
|
| 44 |
+
- Use the provided context as the only source of truth.
|
| 45 |
+
- Reason internally but DO NOT reveal reasoning.
|
| 46 |
+
- Output ONLY the final answer.
|
| 47 |
+
- No explanation.
|
| 48 |
+
- No extra text.
|
| 49 |
+
""".strip()
|
| 50 |
+
|
| 51 |
+
SYS_MSG = SystemMessage(content=SYSTEM_PROMPT)
|
| 52 |
|
| 53 |
|
| 54 |
# =========================================================
|
| 55 |
+
# ๊ฒ์ ๋๊ตฌ (Python ํจ์๋ก๋ง ์ฌ์ฉ)
|
| 56 |
# =========================================================
|
| 57 |
+
ddg = DuckDuckGoSearchRun()
|
| 58 |
+
|
| 59 |
|
| 60 |
+
def wiki_search_text(query: str) -> str:
|
|
|
|
| 61 |
"""
|
| 62 |
+
Wikipedia ๊ฒ์ (์ต๋ 2๋ฌธ์).
|
| 63 |
+
wikipedia ํจํค์ง๊ฐ ์์ผ๋ฉด ์๋์ผ๋ก ๋น ๋ฌธ์์ด ๋ฐํ.
|
| 64 |
"""
|
| 65 |
+
try:
|
| 66 |
+
docs = WikipediaLoader(query=query, load_max_docs=2).load()
|
| 67 |
+
return "\n\n".join(d.page_content for d in docs)
|
| 68 |
+
except Exception:
|
| 69 |
+
return ""
|
| 70 |
|
| 71 |
|
| 72 |
+
def arxiv_search_text(query: str) -> str:
|
|
|
|
| 73 |
"""
|
| 74 |
+
Arxiv ๊ฒ์ (์ต๋ 2๋
ผ๋ฌธ, ์๋ถ๋ถ๋ง ์ฌ์ฉ).
|
| 75 |
+
arxiv ํจํค์ง๊ฐ ์์ผ๋ฉด ์๋์ผ๋ก ๋น ๋ฌธ์์ด ๋ฐํ.
|
| 76 |
"""
|
| 77 |
+
try:
|
| 78 |
+
docs = ArxivLoader(query=query, load_max_docs=2).load()
|
| 79 |
+
return "\n\n".join(d.page_content[:1000] for d in docs)
|
| 80 |
+
except Exception:
|
| 81 |
+
return ""
|
| 82 |
|
| 83 |
|
| 84 |
+
def web_search_text(query: str) -> str:
|
|
|
|
| 85 |
"""
|
| 86 |
+
DuckDuckGo ์น ๊ฒ์.
|
| 87 |
+
API Key ๋ถํ์, Space ํ๊ฒฝ์์ ์์ .
|
| 88 |
"""
|
| 89 |
try:
|
| 90 |
+
return ddg.run(query)
|
| 91 |
except Exception:
|
| 92 |
+
return ""
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 93 |
|
| 94 |
|
| 95 |
# =========================================================
|
| 96 |
+
# FAISS Retriever (์๊ตฌ์ฌํญ ๋๋ฌธ์ ์ ์ง)
|
| 97 |
# =========================================================
|
| 98 |
embeddings = HuggingFaceEmbeddings(
|
| 99 |
model_name="sentence-transformers/all-mpnet-base-v2"
|
|
|
|
| 104 |
vector_store = pickle.load(f)
|
| 105 |
else:
|
| 106 |
seed_docs = [
|
| 107 |
+
Document(page_content="GAIA questions require exact factual answers."),
|
| 108 |
]
|
| 109 |
vector_store = FAISS.from_documents(seed_docs, embeddings)
|
| 110 |
with open("faiss.pkl", "wb") as f:
|
| 111 |
pickle.dump(vector_store, f)
|
| 112 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 113 |
|
| 114 |
# =========================================================
|
| 115 |
+
# LangGraph ๋น๋ (๋จ์ผ assistant ๋
ธ๋)
|
| 116 |
# =========================================================
|
| 117 |
def build_agent():
|
| 118 |
"""
|
| 119 |
+
LangGraph๋ฅผ 'ํ์๋ง' ์ฌ์ฉ.
|
| 120 |
+
- START โ assistant โ END
|
| 121 |
+
- tool node, conditional edge, recursion ์ ๋ถ ์์
|
| 122 |
"""
|
| 123 |
|
| 124 |
llm = ChatOpenAI(
|
|
|
|
| 127 |
max_tokens=128,
|
| 128 |
)
|
| 129 |
|
| 130 |
+
def assistant(state: AgentState):
|
| 131 |
+
# ๋ง์ง๋ง ๋ฉ์์ง๋ ํญ์ ์ง๋ฌธ
|
| 132 |
+
question = state["messages"][-1].content
|
| 133 |
+
|
| 134 |
+
# -----------------------------
|
| 135 |
+
# ๊ฒ์ (ํ ๋ฒ๋ง ์คํ)
|
| 136 |
+
# -----------------------------
|
| 137 |
+
wiki = wiki_search_text(question)
|
| 138 |
+
arxiv = arxiv_search_text(question)
|
| 139 |
+
web = web_search_text(question)
|
| 140 |
+
|
| 141 |
+
# -----------------------------
|
| 142 |
+
# ์ปจํ
์คํธ ๊ตฌ์ฑ
|
| 143 |
+
# -----------------------------
|
| 144 |
+
context = f"""
|
| 145 |
+
[WIKIPEDIA]
|
| 146 |
+
{wiki}
|
| 147 |
+
|
| 148 |
+
[ARXIV]
|
| 149 |
+
{arxiv}
|
| 150 |
+
|
| 151 |
+
[WEB]
|
| 152 |
+
{web}
|
| 153 |
+
""".strip()
|
| 154 |
|
| 155 |
+
# -----------------------------
|
| 156 |
+
# LLM ํ๋กฌํํธ
|
| 157 |
+
# -----------------------------
|
| 158 |
+
prompt = [
|
| 159 |
+
SYS_MSG,
|
| 160 |
+
HumanMessage(
|
| 161 |
+
content=f"""
|
| 162 |
+
Question:
|
| 163 |
+
{question}
|
| 164 |
|
| 165 |
+
Context:
|
| 166 |
+
{context}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 167 |
|
| 168 |
+
Answer with ONLY the final answer.
|
| 169 |
+
""".strip()
|
| 170 |
+
),
|
| 171 |
+
]
|
| 172 |
|
| 173 |
+
response = llm.invoke(prompt)
|
|
|
|
| 174 |
|
| 175 |
+
return {"messages": [response]}
|
| 176 |
+
|
| 177 |
+
# -----------------------------
|
| 178 |
+
# Graph ์ ์
|
| 179 |
+
# -----------------------------
|
| 180 |
+
builder = StateGraph(AgentState)
|
| 181 |
+
builder.add_node("assistant", assistant)
|
| 182 |
|
| 183 |
+
builder.add_edge(START, "assistant")
|
| 184 |
+
builder.add_edge("assistant", END)
|
| 185 |
|
|
|
|
| 186 |
return builder.compile()
|
| 187 |
|
| 188 |
|
| 189 |
# =========================================================
|
| 190 |
+
# Public API (app.py์์ import)
|
| 191 |
# =========================================================
|
| 192 |
class BasicAgent:
|
| 193 |
def __init__(self):
|
| 194 |
self.graph = build_agent()
|
| 195 |
+
print("GAIA GPT-4o-mini Agent initialized (STABLE)")
|
| 196 |
|
| 197 |
def __call__(self, question: str) -> str:
|
| 198 |
result = self.graph.invoke(
|
| 199 |
{
|
| 200 |
"messages": [HumanMessage(content=question)],
|
| 201 |
+
}
|
|
|
|
|
|
|
|
|
|
| 202 |
)
|
| 203 |
return result["messages"][-1].content.strip()
|
requirements.txt
CHANGED
|
@@ -1,15 +1,21 @@
|
|
| 1 |
gradio
|
| 2 |
requests
|
|
|
|
|
|
|
| 3 |
langgraph
|
| 4 |
langchain
|
| 5 |
-
|
| 6 |
langchain-community
|
| 7 |
-
|
| 8 |
-
|
|
|
|
| 9 |
sentence-transformers
|
| 10 |
faiss-cpu
|
|
|
|
|
|
|
| 11 |
ddgs
|
| 12 |
duckduckgo-search
|
|
|
|
|
|
|
| 13 |
wikipedia
|
| 14 |
-
arxiv
|
| 15 |
-
pymupdf
|
|
|
|
| 1 |
gradio
|
| 2 |
requests
|
| 3 |
+
|
| 4 |
+
# LangGraph / LangChain core
|
| 5 |
langgraph
|
| 6 |
langchain
|
| 7 |
+
langchain-core
|
| 8 |
langchain-community
|
| 9 |
+
langchain-openai
|
| 10 |
+
|
| 11 |
+
# Embedding / VectorStore
|
| 12 |
sentence-transformers
|
| 13 |
faiss-cpu
|
| 14 |
+
|
| 15 |
+
# Search (API key ๋ถํ์)
|
| 16 |
ddgs
|
| 17 |
duckduckgo-search
|
| 18 |
+
|
| 19 |
+
# Document loaders
|
| 20 |
wikipedia
|
| 21 |
+
arxiv
|
|
|