LangChainIntegration (langchain.py)

Mnemon · ai, langchain-integration, runnable-patching, framework-adapter, step-iteration, integration

Mnemon moth — LangChain integration. System 2 EME via per-step patching: RunnableSequence.invoke is patched to iterate each step individually. Each step is a segment. If a step's output is cached (same input hash), it is returned directly and the LLM is not called for that step.

"""
Mnemon moth — LangChain integration.

System 2 EME via per-step patching:
  RunnableSequence.invoke is patched to iterate each step individually.
  Each step is a segment. If a step's output is cached (same input hash),
  it is returned directly and the LLM is not called for that step.
  Only changed steps call the LLM.

BaseChatModel.invoke / ainvoke:
  LLM-step caching for all providers (Groq, Anthropic, OpenAI, etc.)
  Cache keyed on original input — never patched versions.

Legacy Chain.__call__ (LangChain v0.1) gets chain-level System 1 cache.
"""

from __future__ import annotations

import importlib.util
import logging
from typing import Any, Dict, List, Optional

import time as _time

from mnemon.moth import MnemonIntegration
from ._utils import extract_query, prompt_hash, track

... (truncated -- full source via MCP)

See the full source, get the GitHub permalink, and search 40K more like it.

Get a free API key