WFGY/adapters/langchain
Zaious 0d8a6437a0 fix: minimize runtime intervention by scoping query capture to retriever
Replace over-broad on_chain_start with on_retriever_start to capture
the query embedding as late and as safely as possible in the RetrievalQA
flow, minimizing framework footprint.

Addresses maintainer review feedback for minimal & safe implementation.
2026-03-11 11:45:49 +08:00
..
firewall.py fix: minimize runtime intervention by scoping query capture to retriever 2026-03-11 11:45:49 +08:00
README.md feat: Introduce LangChain and LlamaIndex Semantic Firewall adapters 2026-03-10 04:30:30 +08:00

WFGY LangChain Adapter (Semantic Firewall)

This adapter provides a WFGYSemanticFirewall callback handler for LangChain, enabling real-time monitoring of semantic tension (ΔS) to prevent hallucinations and perception drift.

Quick Start

from wfgy.adapters.langchain.firewall import WFGYSemanticFirewall
from langchain_openai import OpenAIEmbeddings
from langchain_openai import ChatOpenAI
from langchain.chains import RetrievalQA

# Initialize embeddings (must be consistent)
embeddings = OpenAIEmbeddings()

# Initialize WFGY Firewall
firewall = WFGYSemanticFirewall(embedding_model=embeddings)

# Plug into any LangChain object or chain
llm = ChatOpenAI(callbacks=[firewall])

# or use it in a chain
qa_chain = RetrievalQA.from_chain_type(
    llm=llm,
    chain_type="stuff",
    retriever=your_retriever,
    callbacks=[firewall]
)

qa_chain.run("How does WFGY work?")

Metrics Tracked

  • ΔS (Query <-> Context): Measures how well the retrieved documents match the user's intent. High ΔS indicates "Perception Drift".
  • ΔS (Final Answer): Measures how well the LLM's response stays within the semantic bounds of the query.

Zones

  • Safe: ΔS < 0.4
  • Risk: 0.6 < ΔS < 0.85
  • Danger: ΔS ≥ 0.85 (Triggers alerts)