Skip to main content

LangChain memory backend for Sulcus thermodynamic memory

Project description

sulcus-langchain

LangChain memory backend for Sulcus — thermodynamic memory for AI agents.

Sulcus stores memories as nodes with heat (thermodynamic relevance) that decays over time. This integration exposes Sulcus as three LangChain primitives:

Class LangChain base Purpose
SulcusMemory BaseMemory Plug-in memory for chains (load + save)
SulcusChatMessageHistory BaseChatMessageHistory Per-session chat history
SulcusRetriever BaseRetriever Semantic search over stored memories

Installation

pip install sulcus-langchain

# For async support (AsyncSulcus):
pip install sulcus-langchain[async]

Note: This package imports only from langchain-core — not the full langchain package.


Quick Start

SulcusMemory — drop-in chain memory

from sulcus import Sulcus
from sulcus_langchain import SulcusMemory
from langchain_core.prompts import ChatPromptTemplate, MessagesPlaceholder
from langchain_openai import ChatOpenAI

client = Sulcus(api_key="sk-...", namespace="my-chatbot")
memory = SulcusMemory(client=client, memory_type="conversation")

# Load memories relevant to the current input
variables = memory.load_memory_variables({"input": "Tell me about photosynthesis"})
print(variables["history"])  # formatted string of relevant memories

# After a chain turn, persist the exchange
memory.save_context(
    {"input": "Tell me about photosynthesis"},
    {"output": "Photosynthesis is the process by which plants convert light..."},
)

Memory type mapping:

memory_type Sulcus type
"conversation" "episodic"
"facts" "semantic"
"preferences" "preference"
"procedures" "procedural"

SulcusChatMessageHistory — per-session chat history

from sulcus import Sulcus
from sulcus_langchain import SulcusChatMessageHistory

client = Sulcus(api_key="sk-...", namespace="chatbot")
history = SulcusChatMessageHistory(client=client, session_id="user-123")

history.add_user_message("What's the capital of France?")
history.add_ai_message("The capital of France is Paris.")

for msg in history.messages:
    print(f"{msg.type}: {msg.content}")

# Clear a session's history
history.clear()

SulcusRetriever — RAG over stored memories

from sulcus import Sulcus
from sulcus_langchain import SulcusRetriever

client = Sulcus(api_key="sk-...", namespace="knowledge-base")
retriever = SulcusRetriever(
    client=client,
    search_limit=5,
    memory_type="semantic",  # only fetch facts
    min_heat=0.3,             # ignore cold (low-relevance) memories
)

docs = retriever.invoke("What do we know about the user's preferences?")
for doc in docs:
    print(doc.page_content)
    print(doc.metadata)  # {"memory_id": ..., "heat": 0.87, "memory_type": "semantic", ...}

Full chain example

See examples/basic_chain.py for a complete ChatOpenAI + SulcusMemory chain.

from sulcus import Sulcus
from sulcus_langchain import SulcusMemory
from langchain_core.prompts import PromptTemplate
from langchain_openai import ChatOpenAI

client = Sulcus(api_key="sk-sulcus-...", namespace="assistant")
memory = SulcusMemory(
    client=client,
    memory_type="conversation",
    memory_key="history",
    search_limit=8,
)

llm = ChatOpenAI(model="gpt-4o-mini")
prompt = PromptTemplate.from_template(
    "Relevant context:\n{history}\n\nHuman: {input}\nAI:"
)

# Manual chain loop
user_input = "What was my last question?"
mem_vars = memory.load_memory_variables({"input": user_input})
response = llm.invoke(prompt.format(input=user_input, **mem_vars))
memory.save_context({"input": user_input}, {"output": response.content})

Configuration

All classes accept these common parameters:

Parameter Type Default Description
client Sulcus required Initialised Sulcus client
memory_type str "conversation" Memory type label (SulcusMemory only)
heat float 0.8 Initial heat for new memory nodes
search_limit int 10 Max memories returned per search
namespace str client default Sulcus namespace

Architecture

LangChain chain
     │
     ├── load_memory_variables(inputs)
     │        └── Sulcus.search(query, memory_type=...)
     │                 └── GET /api/v1/agent/search
     │
     └── save_context(inputs, outputs)
              └── Sulcus.remember(content, memory_type=...)
                       └── POST /api/v1/agent/nodes

Memory nodes are stored in the Sulcus golden index with thermodynamic heat. Over time, unused memories cool down and become less retrievable — natural forgetting. Pin important memories with client.pin(memory_id) to prevent decay.


License

MIT © dforge

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

sulcus_langchain-0.1.0.tar.gz (9.9 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

sulcus_langchain-0.1.0-py3-none-any.whl (10.6 kB view details)

Uploaded Python 3

File details

Details for the file sulcus_langchain-0.1.0.tar.gz.

File metadata

  • Download URL: sulcus_langchain-0.1.0.tar.gz
  • Upload date:
  • Size: 9.9 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.14.3

File hashes

Hashes for sulcus_langchain-0.1.0.tar.gz
Algorithm Hash digest
SHA256 dcad709b757d6aaa6088a204ead8e821136d8505273f6fbab5787af14c7bae08
MD5 4d561d50ac42256af24c6a4b17d9c8de
BLAKE2b-256 a6f44b27e9eea922586890eccbb1ca89eb5b2818258c9020b7038d40f4bcbfb6

See more details on using hashes here.

File details

Details for the file sulcus_langchain-0.1.0-py3-none-any.whl.

File metadata

File hashes

Hashes for sulcus_langchain-0.1.0-py3-none-any.whl
Algorithm Hash digest
SHA256 43354cac5c73125cb1c856a846c23bc8e7446eeafd83b76ced590996ee27f2c4
MD5 b6b5273a5c57fcfd2b32d73dcb2a0df2
BLAKE2b-256 c26662b83a2bac741e7266157f161542425df61aded95b4cdd3f32b4dcdb3c66

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page