Spaces:
Sleeping
Sleeping
commit
Browse files
agent.py
CHANGED
|
@@ -1,7 +1,17 @@
|
|
| 1 |
# agent.py
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 2 |
import os
|
| 3 |
import pickle
|
| 4 |
|
|
|
|
|
|
|
|
|
|
| 5 |
from langchain_core.tools import create_retriever_tool
|
| 6 |
from langchain_community.document_loaders import WikipediaLoader, ArxivLoader
|
| 7 |
from langchain_community.vectorstores import FAISS
|
|
@@ -16,43 +26,74 @@ from langchain_community.tools import DuckDuckGoSearchRun
|
|
| 16 |
from langgraph.graph import START, StateGraph, MessagesState
|
| 17 |
from langgraph.prebuilt import ToolNode, tools_condition
|
| 18 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 19 |
ddg = DuckDuckGoSearchRun()
|
| 20 |
|
| 21 |
-
|
| 22 |
-
#
|
| 23 |
-
#
|
|
|
|
|
|
|
|
|
|
|
|
|
| 24 |
@tool
|
| 25 |
def wiki_search(query: str) -> dict:
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 26 |
docs = WikipediaLoader(query=query, load_max_docs=2).load()
|
|
|
|
|
|
|
| 27 |
text = "\n\n".join(d.page_content for d in docs)
|
|
|
|
| 28 |
return {"wiki": text}
|
| 29 |
|
| 30 |
|
| 31 |
@tool
|
| 32 |
def arxiv_search(query: str) -> dict:
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 33 |
docs = ArxivLoader(query=query, load_max_docs=2).load()
|
|
|
|
|
|
|
| 34 |
text = "\n\n".join(d.page_content[:1000] for d in docs)
|
| 35 |
-
return {"arxiv": text}
|
| 36 |
|
|
|
|
| 37 |
|
| 38 |
-
ddg = DuckDuckGoSearchRun()
|
| 39 |
|
| 40 |
@tool
|
| 41 |
def web_search(query: str) -> dict:
|
| 42 |
-
"""
|
|
|
|
|
|
|
|
|
|
| 43 |
try:
|
|
|
|
| 44 |
result = ddg.run(query)
|
| 45 |
return {"web": result}
|
| 46 |
-
except Exception
|
|
|
|
| 47 |
return {"web": ""}
|
| 48 |
|
| 49 |
|
|
|
|
| 50 |
TOOLS = [wiki_search, arxiv_search, web_search]
|
| 51 |
|
| 52 |
|
| 53 |
-
#
|
| 54 |
-
#
|
| 55 |
-
# -
|
|
|
|
| 56 |
SYSTEM_PROMPT = """
|
| 57 |
You are solving GAIA benchmark questions.
|
| 58 |
|
|
@@ -64,27 +105,36 @@ You MUST:
|
|
| 64 |
- No extra text.
|
| 65 |
""".strip()
|
| 66 |
|
|
|
|
| 67 |
SYS_MSG = SystemMessage(content=SYSTEM_PROMPT)
|
| 68 |
|
| 69 |
|
| 70 |
-
#
|
| 71 |
-
#
|
| 72 |
-
# -
|
|
|
|
|
|
|
|
|
|
| 73 |
embeddings = HuggingFaceEmbeddings(
|
| 74 |
model_name="sentence-transformers/all-mpnet-base-v2"
|
| 75 |
)
|
| 76 |
|
|
|
|
| 77 |
if os.path.exists("faiss.pkl"):
|
| 78 |
with open("faiss.pkl", "rb") as f:
|
| 79 |
vector_store = pickle.load(f)
|
| 80 |
else:
|
|
|
|
| 81 |
seed_docs = [
|
| 82 |
Document(page_content="GAIA questions require factual exact answers."),
|
| 83 |
]
|
| 84 |
vector_store = FAISS.from_documents(seed_docs, embeddings)
|
|
|
|
|
|
|
| 85 |
with open("faiss.pkl", "wb") as f:
|
| 86 |
pickle.dump(vector_store, f)
|
| 87 |
|
|
|
|
| 88 |
retriever_tool = create_retriever_tool(
|
| 89 |
retriever=vector_store.as_retriever(),
|
| 90 |
name="question_retriever",
|
|
@@ -92,48 +142,74 @@ retriever_tool = create_retriever_tool(
|
|
| 92 |
)
|
| 93 |
|
| 94 |
|
| 95 |
-
#
|
| 96 |
-
#
|
| 97 |
-
#
|
| 98 |
def build_agent():
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 99 |
llm = ChatOpenAI(
|
| 100 |
model="gpt-4o-mini",
|
| 101 |
-
temperature=0,
|
| 102 |
max_tokens=128,
|
| 103 |
)
|
| 104 |
|
|
|
|
| 105 |
llm_with_tools = llm.bind_tools(TOOLS)
|
| 106 |
|
|
|
|
|
|
|
|
|
|
| 107 |
def retriever(state: MessagesState):
|
|
|
|
| 108 |
return {"messages": [SYS_MSG] + state["messages"]}
|
| 109 |
|
| 110 |
def assistant(state: MessagesState):
|
|
|
|
| 111 |
return {"messages": [llm_with_tools.invoke(state["messages"])]}
|
| 112 |
|
|
|
|
|
|
|
|
|
|
| 113 |
builder = StateGraph(MessagesState)
|
| 114 |
|
| 115 |
builder.add_node("retriever", retriever)
|
| 116 |
builder.add_node("assistant", assistant)
|
| 117 |
builder.add_node("tools", ToolNode(TOOLS))
|
| 118 |
|
|
|
|
| 119 |
builder.add_edge(START, "retriever")
|
| 120 |
builder.add_edge("retriever", "assistant")
|
|
|
|
|
|
|
| 121 |
builder.add_conditional_edges("assistant", tools_condition)
|
|
|
|
|
|
|
| 122 |
builder.add_edge("tools", "assistant")
|
| 123 |
|
| 124 |
return builder.compile()
|
| 125 |
|
| 126 |
|
| 127 |
-
#
|
| 128 |
-
#
|
| 129 |
-
# -
|
|
|
|
| 130 |
class BasicAgent:
|
| 131 |
def __init__(self):
|
|
|
|
| 132 |
self.graph = build_agent()
|
| 133 |
print("โ
LangGraph GPT-4o-mini Agent initialized")
|
| 134 |
|
| 135 |
def __call__(self, question: str) -> str:
|
|
|
|
| 136 |
result = self.graph.invoke(
|
| 137 |
{"messages": [HumanMessage(content=question)]}
|
| 138 |
)
|
|
|
|
|
|
|
| 139 |
return result["messages"][-1].content.strip()
|
|
|
|
| 1 |
# agent.py
|
| 2 |
+
# =========================================================
|
| 3 |
+
# LangGraph ๊ธฐ๋ฐ GAIA ๋ฌธ์ ํด๊ฒฐ Agent
|
| 4 |
+
# - OpenAI GPT-4o-mini ์ฌ์ฉ
|
| 5 |
+
# - Wikipedia / Arxiv / DuckDuckGo ๊ฒ์ ๋๊ตฌ ํฌํจ
|
| 6 |
+
# - HuggingFace Embedding + FAISS Retriever ์ ์ง
|
| 7 |
+
# =========================================================
|
| 8 |
+
|
| 9 |
import os
|
| 10 |
import pickle
|
| 11 |
|
| 12 |
+
# ---------------------------------------------------------
|
| 13 |
+
# LangChain / LangGraph ํต์ฌ ๊ตฌ์ฑ์์
|
| 14 |
+
# ---------------------------------------------------------
|
| 15 |
from langchain_core.tools import create_retriever_tool
|
| 16 |
from langchain_community.document_loaders import WikipediaLoader, ArxivLoader
|
| 17 |
from langchain_community.vectorstores import FAISS
|
|
|
|
| 26 |
from langgraph.graph import START, StateGraph, MessagesState
|
| 27 |
from langgraph.prebuilt import ToolNode, tools_condition
|
| 28 |
|
| 29 |
+
|
| 30 |
+
# ---------------------------------------------------------
|
| 31 |
+
# DuckDuckGo ๊ฒ์ ์ธ์คํด์ค
|
| 32 |
+
# - API Key ๋ถํ์
|
| 33 |
+
# - Space ํ๊ฒฝ์์ ์ฌ์ฉ ๊ฐ๋ฅ
|
| 34 |
+
# ---------------------------------------------------------
|
| 35 |
ddg = DuckDuckGoSearchRun()
|
| 36 |
|
| 37 |
+
|
| 38 |
+
# =========================================================
|
| 39 |
+
# TOOLS ์ ์
|
| 40 |
+
# โป LangChain ์ต์ ๋ฒ์ ์์๋
|
| 41 |
+
# @tool ํจ์์ "docstring"์ด ๋ฐ๋์ ํ์ํจ
|
| 42 |
+
# =========================================================
|
| 43 |
+
|
| 44 |
@tool
|
| 45 |
def wiki_search(query: str) -> dict:
|
| 46 |
+
"""
|
| 47 |
+
Search Wikipedia for factual information related to the query.
|
| 48 |
+
Returns raw page content as text.
|
| 49 |
+
"""
|
| 50 |
+
# Wikipedia์์ ์ต๋ 2๊ฐ ๋ฌธ์ ๋ก๋
|
| 51 |
docs = WikipediaLoader(query=query, load_max_docs=2).load()
|
| 52 |
+
|
| 53 |
+
# ๋ฌธ์ ๋ด์ฉ์ ํ๋์ ๋ฌธ์์ด๋ก ๋ณํฉ
|
| 54 |
text = "\n\n".join(d.page_content for d in docs)
|
| 55 |
+
|
| 56 |
return {"wiki": text}
|
| 57 |
|
| 58 |
|
| 59 |
@tool
|
| 60 |
def arxiv_search(query: str) -> dict:
|
| 61 |
+
"""
|
| 62 |
+
Search Arxiv for academic papers related to the query.
|
| 63 |
+
Returns truncated paper content.
|
| 64 |
+
"""
|
| 65 |
+
# Arxiv์์ ์ต๋ 2๊ฐ ๋
ผ๋ฌธ ๋ก๋
|
| 66 |
docs = ArxivLoader(query=query, load_max_docs=2).load()
|
| 67 |
+
|
| 68 |
+
# ๋
ผ๋ฌธ์ ๊ธธ๊ธฐ ๋๋ฌธ์ ์๋ถ๋ถ๋ง ์ฌ์ฉ
|
| 69 |
text = "\n\n".join(d.page_content[:1000] for d in docs)
|
|
|
|
| 70 |
|
| 71 |
+
return {"arxiv": text}
|
| 72 |
|
|
|
|
| 73 |
|
| 74 |
@tool
|
| 75 |
def web_search(query: str) -> dict:
|
| 76 |
+
"""
|
| 77 |
+
Search the web using DuckDuckGo.
|
| 78 |
+
No API key required. Suitable for Hugging Face Spaces.
|
| 79 |
+
"""
|
| 80 |
try:
|
| 81 |
+
# DuckDuckGo ๊ฒ์ ์คํ
|
| 82 |
result = ddg.run(query)
|
| 83 |
return {"web": result}
|
| 84 |
+
except Exception:
|
| 85 |
+
# ๊ฒ์ ์คํจ ์ ๋น ๊ฒฐ๊ณผ ๋ฐํ (๊ทธ๋ํ ์ค๋จ ๋ฐฉ์ง)
|
| 86 |
return {"web": ""}
|
| 87 |
|
| 88 |
|
| 89 |
+
# LangGraph์์ ์ฌ์ฉํ Tool ๋ชฉ๋ก
|
| 90 |
TOOLS = [wiki_search, arxiv_search, web_search]
|
| 91 |
|
| 92 |
|
| 93 |
+
# =========================================================
|
| 94 |
+
# SYSTEM PROMPT
|
| 95 |
+
# - GAIA ํ๊ฐ ๊ท์น์ ๋ง๊ฒ ์ถ๋ ฅ ํ์ ๊ฐ์
|
| 96 |
+
# =========================================================
|
| 97 |
SYSTEM_PROMPT = """
|
| 98 |
You are solving GAIA benchmark questions.
|
| 99 |
|
|
|
|
| 105 |
- No extra text.
|
| 106 |
""".strip()
|
| 107 |
|
| 108 |
+
# SystemMessage ๊ฐ์ฒด๋ก ๋ณํ
|
| 109 |
SYS_MSG = SystemMessage(content=SYSTEM_PROMPT)
|
| 110 |
|
| 111 |
|
| 112 |
+
# =========================================================
|
| 113 |
+
# RETRIEVER (FAISS + HuggingFace Embeddings)
|
| 114 |
+
# - ์ด์ ์ง๋ฌธ๊ณผ ์ ์ฌํ ํจํด์ ์ ๊ณตํ๊ธฐ ์ํ ์ฉ๋
|
| 115 |
+
# =========================================================
|
| 116 |
+
|
| 117 |
+
# ๋ฌธ์ฅ ์๋ฒ ๋ฉ ๋ชจ๋ธ (768์ฐจ์)
|
| 118 |
embeddings = HuggingFaceEmbeddings(
|
| 119 |
model_name="sentence-transformers/all-mpnet-base-v2"
|
| 120 |
)
|
| 121 |
|
| 122 |
+
# FAISS ์ธ๋ฑ์ค๊ฐ ์ด๋ฏธ ์์ผ๋ฉด ๋ก๋
|
| 123 |
if os.path.exists("faiss.pkl"):
|
| 124 |
with open("faiss.pkl", "rb") as f:
|
| 125 |
vector_store = pickle.load(f)
|
| 126 |
else:
|
| 127 |
+
# ์ต์ด ์คํ ์ ๊ธฐ๋ณธ ์๋ ๋ฌธ์ ์์ฑ
|
| 128 |
seed_docs = [
|
| 129 |
Document(page_content="GAIA questions require factual exact answers."),
|
| 130 |
]
|
| 131 |
vector_store = FAISS.from_documents(seed_docs, embeddings)
|
| 132 |
+
|
| 133 |
+
# ๋ค์ ์คํ์ ์ํด ์ ์ฅ
|
| 134 |
with open("faiss.pkl", "wb") as f:
|
| 135 |
pickle.dump(vector_store, f)
|
| 136 |
|
| 137 |
+
# Retriever Tool (ํ์ฌ ๊ทธ๋ํ์์๋ ๋ณด์กฐ ์ญํ )
|
| 138 |
retriever_tool = create_retriever_tool(
|
| 139 |
retriever=vector_store.as_retriever(),
|
| 140 |
name="question_retriever",
|
|
|
|
| 142 |
)
|
| 143 |
|
| 144 |
|
| 145 |
+
# =========================================================
|
| 146 |
+
# LANGGRAPH ๊ทธ๋ํ ๋น๋
|
| 147 |
+
# =========================================================
|
| 148 |
def build_agent():
|
| 149 |
+
"""
|
| 150 |
+
LangGraph StateGraph๋ฅผ ๊ตฌ์ฑํ์ฌ
|
| 151 |
+
- System Prompt ์ฃผ์
|
| 152 |
+
- LLM โ Tool โ LLM ๋ฐ๋ณต ํ๋ฆ์ ์์ฑ
|
| 153 |
+
"""
|
| 154 |
+
|
| 155 |
+
# OpenAI GPT-4o-mini (Space์์ ์์ ์ ์ผ๋ก ์ฌ์ฉ ๊ฐ๋ฅ)
|
| 156 |
llm = ChatOpenAI(
|
| 157 |
model="gpt-4o-mini",
|
| 158 |
+
temperature=0, # deterministic
|
| 159 |
max_tokens=128,
|
| 160 |
)
|
| 161 |
|
| 162 |
+
# LLM์ Tool ์ฌ์ฉ ๊ถํ ๋ถ์ฌ
|
| 163 |
llm_with_tools = llm.bind_tools(TOOLS)
|
| 164 |
|
| 165 |
+
# ---------------------
|
| 166 |
+
# Graph Node ์ ์
|
| 167 |
+
# ---------------------
|
| 168 |
def retriever(state: MessagesState):
|
| 169 |
+
# ์์คํ
ํ๋กฌํํธ๋ฅผ ํญ์ ๋ฉ์์ง ๋งจ ์์ ์ฝ์
|
| 170 |
return {"messages": [SYS_MSG] + state["messages"]}
|
| 171 |
|
| 172 |
def assistant(state: MessagesState):
|
| 173 |
+
# LLM ์คํ (Tool ํธ์ถ ์ฌ๋ถ๋ tools_condition์ด ํ๋จ)
|
| 174 |
return {"messages": [llm_with_tools.invoke(state["messages"])]}
|
| 175 |
|
| 176 |
+
# ---------------------
|
| 177 |
+
# Graph ๊ตฌ์ฑ
|
| 178 |
+
# ---------------------
|
| 179 |
builder = StateGraph(MessagesState)
|
| 180 |
|
| 181 |
builder.add_node("retriever", retriever)
|
| 182 |
builder.add_node("assistant", assistant)
|
| 183 |
builder.add_node("tools", ToolNode(TOOLS))
|
| 184 |
|
| 185 |
+
# START โ retriever โ assistant
|
| 186 |
builder.add_edge(START, "retriever")
|
| 187 |
builder.add_edge("retriever", "assistant")
|
| 188 |
+
|
| 189 |
+
# assistant๊ฐ tool ํธ์ถ์ ์ํ๋ฉด tools๋ก ๋ถ๊ธฐ
|
| 190 |
builder.add_conditional_edges("assistant", tools_condition)
|
| 191 |
+
|
| 192 |
+
# tool ์คํ ํ ๋ค์ assistant๋ก ๋ณต๊ท
|
| 193 |
builder.add_edge("tools", "assistant")
|
| 194 |
|
| 195 |
return builder.compile()
|
| 196 |
|
| 197 |
|
| 198 |
+
# =========================================================
|
| 199 |
+
# ์ธ๋ถ์์ ์ฌ์ฉํ๋ PUBLIC API
|
| 200 |
+
# - app.py์์ ์ด ํด๋์ค๋ง import
|
| 201 |
+
# =========================================================
|
| 202 |
class BasicAgent:
|
| 203 |
def __init__(self):
|
| 204 |
+
# LangGraph ์ปดํ์ผ
|
| 205 |
self.graph = build_agent()
|
| 206 |
print("โ
LangGraph GPT-4o-mini Agent initialized")
|
| 207 |
|
| 208 |
def __call__(self, question: str) -> str:
|
| 209 |
+
# ์ง๋ฌธ์ HumanMessage๋ก ๊ฐ์ธ Graph ์คํ
|
| 210 |
result = self.graph.invoke(
|
| 211 |
{"messages": [HumanMessage(content=question)]}
|
| 212 |
)
|
| 213 |
+
|
| 214 |
+
# ์ต์ข
assistant ๋ฉ์์ง๋ง ๋ฐํ
|
| 215 |
return result["messages"][-1].content.strip()
|