Update agent.py
Browse files
agent.py
CHANGED
|
@@ -2,7 +2,7 @@ import os
|
|
| 2 |
import pandas as pd
|
| 3 |
from langchain_core.messages import HumanMessage, AIMessage
|
| 4 |
from langgraph.graph import StateGraph, MessagesState
|
| 5 |
-
from
|
| 6 |
from tools import TOOLS
|
| 7 |
|
| 8 |
# --- Read local QA data for retriever ---
|
|
@@ -14,14 +14,18 @@ qa_dict = {
|
|
| 14 |
}
|
| 15 |
|
| 16 |
def build_graph():
|
| 17 |
-
# Initialize Mistral model
|
| 18 |
llm = HuggingFaceEndpoint(
|
| 19 |
repo_id="mistralai/Mistral-7B-Instruct-v0.3",
|
| 20 |
-
task="
|
| 21 |
-
huggingfacehub_api_token=os.environ["HF_TOKEN"]
|
|
|
|
|
|
|
|
|
|
|
|
|
| 22 |
)
|
| 23 |
|
| 24 |
-
# Retriever node
|
| 25 |
def retriever_node(state: MessagesState):
|
| 26 |
query = state["messages"][-1].content.strip()
|
| 27 |
if query in qa_dict:
|
|
@@ -30,36 +34,32 @@ def build_graph():
|
|
| 30 |
print("🔍 No match. Sending to LLM.")
|
| 31 |
return {"messages": state["messages"]}
|
| 32 |
|
| 33 |
-
# Assistant node (LLM)
|
| 34 |
def assistant_node(state: MessagesState):
|
| 35 |
query = state["messages"][-1].content.strip()
|
| 36 |
|
|
|
|
| 37 |
system_prompt = (
|
| 38 |
-
"You are a helpful assistant evaluated by the GAIA benchmark.
|
| 39 |
-
"Only return the final answer, with no explanations.
|
| 40 |
-
"- No prefixes like 'Final answer:'
|
| 41 |
-
"- If it's a list, output comma-separated
|
| 42 |
-
"- If unknown, say 'Unknown'
|
| 43 |
"- Never justify or explain"
|
| 44 |
)
|
| 45 |
-
|
| 46 |
-
prompt = [
|
| 47 |
-
{"role": "system", "content": system_prompt},
|
| 48 |
-
{"role": "user", "content": query},
|
| 49 |
-
]
|
| 50 |
|
| 51 |
-
# prompt
|
|
|
|
| 52 |
response = llm.invoke(prompt).strip()
|
| 53 |
|
| 54 |
-
#
|
| 55 |
-
|
| 56 |
for tag in ("Final answer:", "Answer:", "assistant:"):
|
| 57 |
if response.lower().startswith(tag.lower()):
|
| 58 |
response = response[len(tag):].strip()
|
| 59 |
|
| 60 |
return {"messages": [AIMessage(content=response.strip())]}
|
| 61 |
|
| 62 |
-
# Tool node
|
| 63 |
def tool_node(state: MessagesState):
|
| 64 |
try:
|
| 65 |
tool_signal = state.get("tool_call", "")
|
|
@@ -80,7 +80,7 @@ def build_graph():
|
|
| 80 |
print(f"⚠️ Tool error: {e}")
|
| 81 |
return {"messages": [AIMessage(content="Unknown")]} # fail-safe
|
| 82 |
|
| 83 |
-
# Build LangGraph
|
| 84 |
builder = StateGraph(MessagesState)
|
| 85 |
builder.add_node("retriever", retriever_node)
|
| 86 |
builder.add_node("assistant", assistant_node)
|
|
@@ -94,7 +94,7 @@ def build_graph():
|
|
| 94 |
|
| 95 |
return builder.compile()
|
| 96 |
|
| 97 |
-
# Agent class
|
| 98 |
class BasicAgent:
|
| 99 |
def __init__(self):
|
| 100 |
print("✅ BasicAgent initialized with retriever + LLM + tools")
|
|
@@ -105,4 +105,4 @@ class BasicAgent:
|
|
| 105 |
result = self.graph.invoke({"messages": [HumanMessage(content=question)]})
|
| 106 |
answer = result["messages"][-1].content.strip()
|
| 107 |
print(f"📤 Answer: {answer}")
|
| 108 |
-
return answer
|
|
|
|
| 2 |
import pandas as pd
|
| 3 |
from langchain_core.messages import HumanMessage, AIMessage
|
| 4 |
from langgraph.graph import StateGraph, MessagesState
|
| 5 |
+
from langchain_community.llms import HuggingFaceEndpoint
|
| 6 |
from tools import TOOLS
|
| 7 |
|
| 8 |
# --- Read local QA data for retriever ---
|
|
|
|
| 14 |
}
|
| 15 |
|
| 16 |
def build_graph():
|
| 17 |
+
# Initialize Mistral model - CORRECTED CONFIGURATION
|
| 18 |
llm = HuggingFaceEndpoint(
|
| 19 |
repo_id="mistralai/Mistral-7B-Instruct-v0.3",
|
| 20 |
+
task="text-generation",
|
| 21 |
+
huggingfacehub_api_token=os.environ["HF_TOKEN"],
|
| 22 |
+
model_kwargs={
|
| 23 |
+
"max_new_tokens": 512,
|
| 24 |
+
"temperature": 0.1
|
| 25 |
+
}
|
| 26 |
)
|
| 27 |
|
| 28 |
+
# Retriever node (unchanged)
|
| 29 |
def retriever_node(state: MessagesState):
|
| 30 |
query = state["messages"][-1].content.strip()
|
| 31 |
if query in qa_dict:
|
|
|
|
| 34 |
print("🔍 No match. Sending to LLM.")
|
| 35 |
return {"messages": state["messages"]}
|
| 36 |
|
| 37 |
+
# Assistant node (LLM) - UPDATED FOR TEXT-GENERATION
|
| 38 |
def assistant_node(state: MessagesState):
|
| 39 |
query = state["messages"][-1].content.strip()
|
| 40 |
|
| 41 |
+
# Format system prompt for text-generation
|
| 42 |
system_prompt = (
|
| 43 |
+
"You are a helpful assistant evaluated by the GAIA benchmark. "
|
| 44 |
+
"Only return the final answer, with no explanations. "
|
| 45 |
+
"- No prefixes like 'Final answer:' "
|
| 46 |
+
"- If it's a list, output comma-separated "
|
| 47 |
+
"- If unknown, say 'Unknown' "
|
| 48 |
"- Never justify or explain"
|
| 49 |
)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 50 |
|
| 51 |
+
# Format prompt for text-generation model
|
| 52 |
+
prompt = f"<s>[INST] {system_prompt}\n\n{query} [/INST]"
|
| 53 |
response = llm.invoke(prompt).strip()
|
| 54 |
|
| 55 |
+
# Clean up response
|
|
|
|
| 56 |
for tag in ("Final answer:", "Answer:", "assistant:"):
|
| 57 |
if response.lower().startswith(tag.lower()):
|
| 58 |
response = response[len(tag):].strip()
|
| 59 |
|
| 60 |
return {"messages": [AIMessage(content=response.strip())]}
|
| 61 |
|
| 62 |
+
# Tool node (unchanged)
|
| 63 |
def tool_node(state: MessagesState):
|
| 64 |
try:
|
| 65 |
tool_signal = state.get("tool_call", "")
|
|
|
|
| 80 |
print(f"⚠️ Tool error: {e}")
|
| 81 |
return {"messages": [AIMessage(content="Unknown")]} # fail-safe
|
| 82 |
|
| 83 |
+
# Build LangGraph (unchanged)
|
| 84 |
builder = StateGraph(MessagesState)
|
| 85 |
builder.add_node("retriever", retriever_node)
|
| 86 |
builder.add_node("assistant", assistant_node)
|
|
|
|
| 94 |
|
| 95 |
return builder.compile()
|
| 96 |
|
| 97 |
+
# Agent class (unchanged)
|
| 98 |
class BasicAgent:
|
| 99 |
def __init__(self):
|
| 100 |
print("✅ BasicAgent initialized with retriever + LLM + tools")
|
|
|
|
| 105 |
result = self.graph.invoke({"messages": [HumanMessage(content=question)]})
|
| 106 |
answer = result["messages"][-1].content.strip()
|
| 107 |
print(f"📤 Answer: {answer}")
|
| 108 |
+
return answer
|