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.

fix(integrations): include langgraph dependency in langchain and lang… (#257)

SOURAV SUMANcc901846ba3e
diff --git a/docs/langchain.md b/docs/langchain.md
index 0c0b6768..f8b883df 100644
--- a/docs/langchain.md
+++ b/docs/langchain.md
@@ -1,6 +1,6 @@
# LangChain & LangGraph Integration
-Laya provides fast (~33 ms), non-autoregressive decision components for **LangChain** and **LangGraph**:
+Laya provides fast, non-autoregressive decision components for **LangChain** and **LangGraph** (single-question latency measured at **32.8 ms** with `laya-multilingual` and **39.5 ms** with `laya` on a Tesla T4 GPU; 193–464 ms on CPU):
* **`LayaRouter`**: Conditional edge and branch router with confidence fallback gating.
* **`LayaGuardrail`**: Sub-40ms inline screening for prompt injections, jailbreaks, and sensitive data.
@@ -14,14 +14,16 @@ Supports both **local in-process inference** (`Agent` or `Router`) and **remote
## Installation
```bash
-pip install "laya[langchain]"
+pip install "laya[langchain]" # Installs both langchain-core and langgraph
+# or
+pip install "laya[langgraph]"
```
---
## 1. LangGraph Conditional Edge Routing
-In LangGraph, conditional edges determine which node executes next. Autoregressive LLMs take 500–2,000 ms to make this decision. `LayaRouter` runs in **~33 ms**:
+In LangGraph, conditional edges determine which node executes next. Autoregressive LLMs take 500–2,000 ms to make this decision. `LayaRouter` runs in **~33 ms** (measured at 32.8 ms on `laya-multilingual` / 39.5 ms on `laya` English on a Tesla T4 GPU):
```python
from typing import TypedDict
diff --git a/examples/langchain_quickstart.py b/examples/langchain_quickstart.py
index 34e5c36c..bc46ccdc 100644
--- a/examples/langchain_quickstart.py
+++ b/examples/langchain_quickstart.py
@@ -91,3 +91,41 @@ print(f"Churn Risk: {enriched_state['triage']['churn_risk']}")
# api_key="optional-secret-key",
# criteria={...}
# )
+
+
+# =====================================================================
+# 5. Compiled LangGraph StateGraph Workflow
+# =====================================================================
+try:
+ from langgraph.graph import StateGraph, END
+
+ class AgentState(TypedDict):
+ input: str
+ response: str
+
+ workflow = StateGraph(AgentState)
+ workflow.add_node("billing_agent", lambda state: {"response": "Routing to Billing Specialist."})
+ workflow.add_node("technical_agent", lambda state: {"response": "Routing to Technical Support Specialist."})
+ workflow.add_node("sales_agent", lambda state: {"response": "Routing to Enterprise Sales Representative."})
+ workflow.add_node("human_agent", lambda state: {"response": "Routing to Human Escalation Tier."})
+
+ workflow.set_conditional_entry_point(
+ router_node,
+ {
+ "billing_agent": "billing_agent",
+ "technical_agent": "technical_agent",
+ "sales_agent": "sales_agent",
+ "human_agent": "human_agent",
+ }
+ )
+ workflow.add_edge("billing_agent", END)
+ workflow.add_edge("technical_agent", END)
+ workflow.add_edge("sales_agent", END)
+ workflow.add_edge("human_agent", END)
+
+ app = workflow.compile()
+ graph_res = app.invoke({"input": "I was charged twice on invoice #9821."})
+ print("\nLangGraph StateGraph Result:", graph_res["response"])
+except (ImportError, Exception) as e:
+ print(f"\nNote: To run the compiled LangGraph StateGraph workflow, install 'laya[langchain]': {e}")
+
diff --git a/pyproject.toml b/pyproject.toml
index a18490ca..82669ef5 100644
--- a/pyproject.toml
+++ b/pyproject.toml
@@ -53,6 +53,11 @@ onnx = ["onnx", "onnxruntime"]
# `pip install laya[langchain]` adds the LangChain / LangGraph integrations (laya.integrations).
langchain = [
"langchain-core>=0.2.0",
+ "langgraph>=0.1.0",
+]
+langgraph = [
+ "langchain-core>=0.2.0",
+ "langgraph>=0.1.0",
]
[project.scripts]
diff --git a/tests/test_langchain.py b/tests/test_langchain.py
index c309accf..ad12bf45 100644
--- a/tests/test_langchain.py
+++ b/tests/test_langchain.py
@@ -114,6 +114,11 @@ check("router/fallback_on_low_confidence", router.invoke("lowconf question"), "h
# LangGraph callable protocol
check("router/callable_protocol", router({"messages": [DummyMessage("human", "refund please")]}), "billing")
+# LangGraph conditional edge mapping simulation
+mapping = {"billing": "BillingNode", "technical": "TechNode", "human_agent": "HumanNode"}
+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")
+
# --------------------------------------------------------------- 3. LayaGuardrail
def mock_guard_response(state, questions):