Update agent.py
Browse files
agent.py
CHANGED
|
@@ -1,4 +1,3 @@
|
|
| 1 |
-
"""LangGraph agent using retriever fallback to Qwen2.5-Coder-32B-Instruct (no tools)."""
|
| 2 |
import os
|
| 3 |
import pandas as pd
|
| 4 |
from langchain_core.messages import HumanMessage, AIMessage
|
|
@@ -13,17 +12,15 @@ qa_dict = {
|
|
| 13 |
for _, row in qa_pairs.iterrows()
|
| 14 |
}
|
| 15 |
|
| 16 |
-
# --- Define LangGraph with fallback to LLM ---
|
| 17 |
def build_graph():
|
| 18 |
"""Construct a LangGraph agent with retriever and fallback LLM."""
|
| 19 |
-
|
| 20 |
-
# Initialize HuggingFace Qwen model as fallback
|
| 21 |
llm = HuggingFaceEndpoint(
|
| 22 |
repo_id="Qwen/Qwen2.5-Coder-32B-Instruct",
|
|
|
|
| 23 |
huggingfacehub_api_token=os.environ["HF_TOKEN"]
|
| 24 |
)
|
| 25 |
|
| 26 |
-
# Node: Retriever
|
| 27 |
def retriever_node(state: MessagesState):
|
| 28 |
query = state["messages"][-1].content.strip()
|
| 29 |
if query in qa_dict:
|
|
@@ -32,13 +29,11 @@ def build_graph():
|
|
| 32 |
print("🔍 No match found. Falling back to LLM.")
|
| 33 |
return {"messages": state["messages"]}
|
| 34 |
|
| 35 |
-
# Node: Fallback LLM (Qwen)
|
| 36 |
def assistant_node(state: MessagesState):
|
| 37 |
-
|
| 38 |
-
|
| 39 |
-
return {"messages": [
|
| 40 |
|
| 41 |
-
# Build graph
|
| 42 |
builder = StateGraph(MessagesState)
|
| 43 |
builder.add_node("retriever", retriever_node)
|
| 44 |
builder.add_node("assistant", assistant_node)
|
|
|
|
|
|
|
| 1 |
import os
|
| 2 |
import pandas as pd
|
| 3 |
from langchain_core.messages import HumanMessage, AIMessage
|
|
|
|
| 12 |
for _, row in qa_pairs.iterrows()
|
| 13 |
}
|
| 14 |
|
|
|
|
| 15 |
def build_graph():
|
| 16 |
"""Construct a LangGraph agent with retriever and fallback LLM."""
|
| 17 |
+
|
|
|
|
| 18 |
llm = HuggingFaceEndpoint(
|
| 19 |
repo_id="Qwen/Qwen2.5-Coder-32B-Instruct",
|
| 20 |
+
task="chat-completion",
|
| 21 |
huggingfacehub_api_token=os.environ["HF_TOKEN"]
|
| 22 |
)
|
| 23 |
|
|
|
|
| 24 |
def retriever_node(state: MessagesState):
|
| 25 |
query = state["messages"][-1].content.strip()
|
| 26 |
if query in qa_dict:
|
|
|
|
| 29 |
print("🔍 No match found. Falling back to LLM.")
|
| 30 |
return {"messages": state["messages"]}
|
| 31 |
|
|
|
|
| 32 |
def assistant_node(state: MessagesState):
|
| 33 |
+
# <--- Use invoke with list of messages to get correct "chat" format
|
| 34 |
+
result = llm.invoke(state["messages"])
|
| 35 |
+
return {"messages": [result]}
|
| 36 |
|
|
|
|
| 37 |
builder = StateGraph(MessagesState)
|
| 38 |
builder.add_node("retriever", retriever_node)
|
| 39 |
builder.add_node("assistant", assistant_node)
|