Update agent.py
Browse files
agent.py
CHANGED
|
@@ -21,6 +21,7 @@ def build_graph():
|
|
| 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,40 +30,48 @@ def build_graph():
|
|
| 29 |
print("🔍 No match found. Falling back to LLM.")
|
| 30 |
return {"messages": state["messages"]}
|
| 31 |
|
| 32 |
-
|
| 33 |
-
|
| 34 |
-
|
| 35 |
-
# Instrukcja — jak ma wyglądać odpowiedź
|
| 36 |
-
system_prompt = (
|
| 37 |
-
"You are a helpful AI assistant taking part in the GAIA evaluation benchmark.\n"
|
| 38 |
-
"You must return only the final answer to the user's question.\n"
|
| 39 |
-
"- No explanations.\n"
|
| 40 |
-
"- No formatting like 'Final answer:' or similar.\n"
|
| 41 |
-
"- If the answer is a list, return comma-separated values.\n"
|
| 42 |
-
"- If the answer is unknown, return 'Unknown'."
|
| 43 |
-
)
|
| 44 |
|
| 45 |
-
|
| 46 |
-
|
| 47 |
-
|
| 48 |
-
|
| 49 |
-
|
| 50 |
-
|
| 51 |
-
|
| 52 |
-
print("✅ Used chat-style prompt.")
|
| 53 |
-
except Exception as e:
|
| 54 |
-
print(f"⚠️ Chat-style failed: {e}")
|
| 55 |
-
# Fallback: classic prompt-based
|
| 56 |
-
fallback_prompt = (
|
| 57 |
-
f"{system_prompt}\n\n"
|
| 58 |
-
f"Question: {query}\n"
|
| 59 |
-
f"Answer:"
|
| 60 |
)
|
| 61 |
-
response = llm.invoke(fallback_prompt)
|
| 62 |
-
print("🔁 Used fallback prompt format.")
|
| 63 |
|
| 64 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 65 |
|
|
|
|
| 66 |
|
| 67 |
|
| 68 |
# --- Agent class wrapper for app.py ---
|
|
|
|
| 21 |
huggingfacehub_api_token=os.environ["HF_TOKEN"]
|
| 22 |
)
|
| 23 |
|
| 24 |
+
# Node: retriever
|
| 25 |
def retriever_node(state: MessagesState):
|
| 26 |
query = state["messages"][-1].content.strip()
|
| 27 |
if query in qa_dict:
|
|
|
|
| 30 |
print("🔍 No match found. Falling back to LLM.")
|
| 31 |
return {"messages": state["messages"]}
|
| 32 |
|
| 33 |
+
# Node: assistant (LLM with fallback prompt logic)
|
| 34 |
+
def assistant_node(state: MessagesState):
|
| 35 |
+
query = state["messages"][-1].content.strip()
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 36 |
|
| 37 |
+
system_prompt = (
|
| 38 |
+
"You are a helpful AI assistant taking part in the GAIA evaluation benchmark.\n"
|
| 39 |
+
"You must return only the final answer to the user's question.\n"
|
| 40 |
+
"- No explanations.\n"
|
| 41 |
+
"- No formatting like 'Final answer:' or similar.\n"
|
| 42 |
+
"- If the answer is a list, return comma-separated values.\n"
|
| 43 |
+
"- If the answer is unknown, return 'Unknown'."
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 44 |
)
|
|
|
|
|
|
|
| 45 |
|
| 46 |
+
try:
|
| 47 |
+
chat_input = [
|
| 48 |
+
{"role": "system", "content": system_prompt},
|
| 49 |
+
{"role": "user", "content": query}
|
| 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 the LangGraph
|
| 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()
|
| 75 |
|
| 76 |
|
| 77 |
# --- Agent class wrapper for app.py ---
|