Skip to content

armgit / laya

Public Imported from GitHub
0

Non-autoregressive System 1 decision engine. Typed choice, score and yes/no decisions over any text in a single forward pass, in 100+ languages, with a router that picks the right checkpoint per request.

docs(langchain): show full-conversation state_key

Bruce-Yii3dbab474507a
diff --git a/docs/langchain.md b/docs/langchain.md
index f8b883df..a5bd8fd3 100644
--- a/docs/langchain.md
+++ b/docs/langchain.md
@@ -71,6 +71,34 @@ result = app.invoke({"input": "I was billed twice for last month's subscription.
print(result["response"]) # -> "Handling billing..."
```
+### Routing with the full conversation
+
+When a graph state contains a `messages` list, Laya uses the newest user
+message by default. To evaluate the full conversation instead, pass a callable
+`state_key` that returns a chronological list of `role`/`content` dictionaries:
+
+```python
+router = LayaRouter(
+ criteria={
+ "billing_agent": "invoices, payment methods, duplicate charges, refunds",
+ "tech_support": "system errors, bugs, API downtime, stack traces",
+ },
+ state_key=lambda state: state["messages"],
+)
+
+route = router.invoke({
+ "messages": [
+ {"role": "user", "content": "My checkout failed yesterday."},
+ {"role": "assistant", "content": "What error did you see?"},
+ {"role": "user", "content": "It says my card was charged twice."},
+ ]
+})
+```
+
+The same callable `state_key` pattern works with `LayaGuardrail`, `LayaTriage`,
+and `LayaEvaluator`. Conversation lists are serialized in the order supplied;
+if they exceed the model context window, Laya preserves the newest turns.
+
---
## 2. Real-Time Prompt Guardrails
diff --git a/tests/test_langchain.py b/tests/test_langchain.py
index ad12bf45..628876c0 100644
--- a/tests/test_langchain.py
+++ b/tests/test_langchain.py
@@ -69,6 +69,19 @@ check("extract/dict_with_messages", _extract_text({"messages": msgs}), "second h
# Custom callable extractor
check("extract/custom_callable", _extract_text({"custom": "special"}, lambda x: x["custom"].upper()), "SPECIAL")
+# A callable state_key can deliberately preserve the full chronological
+# conversation instead of the default newest-user-message extraction.
+conversation = [
+ {"role": "user", "content": "My checkout failed yesterday."},
+ {"role": "assistant", "content": "What error did you see?"},
+ {"role": "user", "content": "It says my card was charged twice."},
+]
+check(
+ "extract/callable_full_conversation",
+ _extract_text({"messages": conversation}, lambda state: state["messages"]),
+ conversation,
+)
+
# --------------------------------------------------------------- 2. LayaRouter
def mock_router_response(state, questions):
@@ -119,6 +132,37 @@ mapping = {"billing": "BillingNode", "technical": "TechNode", "human_agent": "Hu
check("router/langgraph_edge_routing", mapping[router({"input": "I need an invoice refund"})], "BillingNode")
check("router/langgraph_edge_fallback", mapping[router({"input": "lowconf question"})], "HumanNode")
+# The component passes a callable-selected conversation list through unchanged.
+captured_conversation = []
+
+
+def mock_conversation_router_response(state, questions):
+ captured_conversation.append(state)
+ return {
+ "model": "mock",
+ "answers": {
+ "route": {
+ "type": "choice",
+ "choice": "billing",
+ "probabilities": {"billing": 1.0},
+ "confidence": 1.0,
+ }
+ },
+ }
+
+
+conversation_router = LayaRouter(
+ criteria={"billing": "invoices and refunds"},
+ state_key=lambda state: state["messages"],
+ agent=MockLayaAgent(mock_conversation_router_response),
+)
+check(
+ "router/callable_full_conversation_route",
+ conversation_router.invoke({"messages": conversation}),
+ "billing",
+)
+check("router/callable_full_conversation_state", captured_conversation[0], conversation)
+
# --------------------------------------------------------------- 3. LayaGuardrail
def mock_guard_response(state, questions):