Update agent.py
Browse files
agent.py
CHANGED
|
@@ -2,7 +2,9 @@ import os
|
|
| 2 |
import pandas as pd
|
| 3 |
from langchain_core.messages import HumanMessage, AIMessage
|
| 4 |
from langgraph.graph import StateGraph, MessagesState
|
|
|
|
| 5 |
from langchain_huggingface import HuggingFaceEndpoint
|
|
|
|
| 6 |
|
| 7 |
# --- Load local QA metadata for retriever ---
|
| 8 |
QA_PATH = "metadata.jsonl"
|
|
@@ -16,10 +18,14 @@ from langchain_huggingface import HuggingFaceEndpoint
|
|
| 16 |
|
| 17 |
def build_graph():
|
| 18 |
llm = HuggingFaceEndpoint(
|
| 19 |
-
repo_id="Qwen/Qwen2.5-32B-Instruct",
|
|
|
|
| 20 |
task="text-generation",
|
| 21 |
huggingfacehub_api_token=os.environ["HF_TOKEN"]
|
| 22 |
)
|
|
|
|
|
|
|
|
|
|
| 23 |
|
| 24 |
# Node: retriever
|
| 25 |
def retriever_node(state: MessagesState):
|
|
@@ -30,45 +36,39 @@ def build_graph():
|
|
| 30 |
print("🔍 No match found. Falling back to LLM.")
|
| 31 |
return {"messages": state["messages"]}
|
| 32 |
|
| 33 |
-
# Node: assistant
|
| 34 |
def assistant_node(state: MessagesState):
|
| 35 |
query = state["messages"][-1].content.strip()
|
| 36 |
|
| 37 |
system_prompt = (
|
| 38 |
-
"You are a helpful
|
| 39 |
-
"
|
| 40 |
-
"- No
|
| 41 |
-
"-
|
| 42 |
-
"- If
|
| 43 |
-
"-
|
| 44 |
)
|
| 45 |
|
| 46 |
-
|
| 47 |
-
|
| 48 |
-
|
| 49 |
-
|
| 50 |
-
|
| 51 |
-
response = llm.invoke(chat_input)
|
| 52 |
-
print("✅ Used chat-style prompt.")
|
| 53 |
-
except Exception as e:
|
| 54 |
-
print(f"⚠️ Chat-style failed: {e}")
|
| 55 |
-
fallback_prompt = (
|
| 56 |
-
f"{system_prompt}\n\n"
|
| 57 |
-
f"Question: {query}\n"
|
| 58 |
-
f"Answer:"
|
| 59 |
-
)
|
| 60 |
-
response = llm.invoke(fallback_prompt)
|
| 61 |
-
print("🔁 Used fallback prompt format.")
|
| 62 |
|
|
|
|
| 63 |
return {"messages": [AIMessage(content=response.strip())]}
|
| 64 |
|
| 65 |
-
# Build
|
| 66 |
builder = StateGraph(MessagesState)
|
| 67 |
builder.add_node("retriever", retriever_node)
|
| 68 |
builder.add_node("assistant", assistant_node)
|
|
|
|
| 69 |
|
|
|
|
| 70 |
builder.set_entry_point("retriever")
|
| 71 |
builder.add_edge("retriever", "assistant")
|
|
|
|
|
|
|
| 72 |
builder.set_finish_point("assistant")
|
| 73 |
|
| 74 |
return builder.compile()
|
|
|
|
| 2 |
import pandas as pd
|
| 3 |
from langchain_core.messages import HumanMessage, AIMessage
|
| 4 |
from langgraph.graph import StateGraph, MessagesState
|
| 5 |
+
from langgraph.prebuilt import ToolNode, tools_condition
|
| 6 |
from langchain_huggingface import HuggingFaceEndpoint
|
| 7 |
+
from tools import TOOLS
|
| 8 |
|
| 9 |
# --- Load local QA metadata for retriever ---
|
| 10 |
QA_PATH = "metadata.jsonl"
|
|
|
|
| 18 |
|
| 19 |
def build_graph():
|
| 20 |
llm = HuggingFaceEndpoint(
|
| 21 |
+
# repo_id="Qwen/Qwen2.5-32B-Instruct",
|
| 22 |
+
repo_id="mistralai/Mistral-7B-Instruct-v0.3",
|
| 23 |
task="text-generation",
|
| 24 |
huggingfacehub_api_token=os.environ["HF_TOKEN"]
|
| 25 |
)
|
| 26 |
+
|
| 27 |
+
llm_with_tools = llm.bind_tools(TOOLS)
|
| 28 |
+
|
| 29 |
|
| 30 |
# Node: retriever
|
| 31 |
def retriever_node(state: MessagesState):
|
|
|
|
| 36 |
print("🔍 No match found. Falling back to LLM.")
|
| 37 |
return {"messages": state["messages"]}
|
| 38 |
|
| 39 |
+
# Node: assistant
|
| 40 |
def assistant_node(state: MessagesState):
|
| 41 |
query = state["messages"][-1].content.strip()
|
| 42 |
|
| 43 |
system_prompt = (
|
| 44 |
+
"You are a helpful assistant evaluated by the GAIA benchmark.\n"
|
| 45 |
+
"Only return the final answer, with no explanations.\n"
|
| 46 |
+
"- No prefixes like 'Final answer:'\n"
|
| 47 |
+
"- If it's a list, output comma-separated\n"
|
| 48 |
+
"- If unknown, say 'Unknown'\n"
|
| 49 |
+
"- Never justify or explain"
|
| 50 |
)
|
| 51 |
|
| 52 |
+
# LangChain expects list of messages for tool-call-capable models
|
| 53 |
+
messages = [
|
| 54 |
+
{"role": "system", "content": system_prompt},
|
| 55 |
+
{"role": "user", "content": query},
|
| 56 |
+
]
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 57 |
|
| 58 |
+
response = llm_with_tools.invoke(messages)
|
| 59 |
return {"messages": [AIMessage(content=response.strip())]}
|
| 60 |
|
| 61 |
+
# === Build LangGraph ===
|
| 62 |
builder = StateGraph(MessagesState)
|
| 63 |
builder.add_node("retriever", retriever_node)
|
| 64 |
builder.add_node("assistant", assistant_node)
|
| 65 |
+
builder.add_node("tools", ToolNode(TOOLS)) # LangGraph's tool executor
|
| 66 |
|
| 67 |
+
# --- Edges ---
|
| 68 |
builder.set_entry_point("retriever")
|
| 69 |
builder.add_edge("retriever", "assistant")
|
| 70 |
+
builder.add_conditional_edges("assistant", tools_condition)
|
| 71 |
+
builder.add_edge("tools", "assistant")
|
| 72 |
builder.set_finish_point("assistant")
|
| 73 |
|
| 74 |
return builder.compile()
|