Skip to main content

LangChain memory and retriever backed by OriginTrail DKG v10 Working Memory

Project description

langchain-dkg

CI PyPI Python License

LangChain memory and retriever backed by OriginTrail DKG v10 Working Memory.

Give any LangChain agent persistent, verifiable, queryable memory — every conversation turn stored as a cryptographically-linked Knowledge Asset on the Decentralized Knowledge Graph.

Install

pip install langchain-dkg

Requires a running DKG v10 node. Install with:

npm install -g @origintrail-official/dkg
dkg init && dkg start
export DKG_TOKEN=$(dkg auth show)

Quick start

from langchain_dkg import DKGChatMessageHistory, DKGMemory, DKGRetriever
from langchain_core.messages import HumanMessage, AIMessage

# Store and retrieve conversation turns
history = DKGChatMessageHistory(context_graph_id="my-project")
history.add_message(HumanMessage(content="What is a Knowledge Asset?"))
history.add_message(AIMessage(content="An ownable container of structured knowledge on the DKG."))

messages = history.messages  # tri-modal semantic search

With a LangChain chain (modern LCEL style)

from langchain_openai import ChatOpenAI
from langchain_core.prompts import ChatPromptTemplate, MessagesPlaceholder
from langchain_dkg import DKGMemory

llm = ChatOpenAI(model="gpt-4o-mini")
prompt = ChatPromptTemplate.from_messages([
    ("system", "You are a helpful assistant."),
    MessagesPlaceholder(variable_name="history"),
    ("human", "{input}"),
])

chain_with_memory = DKGMemory.wrap_chain(
    prompt | llm,
    context_graph_id="my-project",
)

response = chain_with_memory.invoke(
    {"input": "What is DKG?"},
    config={"configurable": {"session_id": "user-42"}},
)

RAG retrieval via SPARQL

from langchain_dkg import DKGRetriever
from langchain.chains import RetrievalQA

retriever = DKGRetriever(limit=10)
chain = RetrievalQA.from_chain_type(llm=llm, retriever=retriever)

Components

Class LangChain base Purpose
DKGChatMessageHistory BaseChatMessageHistory Stores turns in DKG WM; retrieves via tri-modal search
DKGMemory Factory for RunnableWithMessageHistory with DKG backend
DKGRetriever BaseRetriever SPARQL retriever — returns triples as Document objects
DKGClient Low-level async HTTP client for the DKG v10 API

Memory layers

DKG v10 has three memory layers:

Layer Scope Cost Use
Working Memory (wm) Private to your node Free Default for conversation history
Shared Working Memory (swm) Gossip-replicated Free Team-visible context
Verified Memory On-chain, permanent TRAC Auditable, publishable knowledge

By default, turns are written to Shared Working Memory (swm). Use layer="wm" for private-only storage.

Explicit promotion to Shared Memory:

turn_uri = history.get_turn_uri("**Human:** Summarize this meeting")
await history.promote_to_shared(turn_uri)

Configuration

Env var Default Description
DKG_TOKEN Bearer token from dkg auth show
DKG_BASE_URL http://localhost:9200 DKG node API URL

Or pass token= / base_url= directly to DKGClient.

Session isolation

Each session_id passed to chain_with_memory.invoke(config={"configurable": {"session_id": "..."}}) becomes a sessionUri in DKG, linking turns together within the shared Context Graph.

Development

pip install -e ".[dev]"
pytest tests/unit/                                    # unit tests (no node required)
DKG_TOKEN=$(dkg auth show) pytest tests/integration/  # integration tests
python examples/research_agent.py                     # demo script

License

MIT

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

langchain_dkg-0.1.3.tar.gz (26.4 kB view details)

Uploaded Source

Built Distribution

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

langchain_dkg-0.1.3-py3-none-any.whl (12.1 kB view details)

Uploaded Python 3

File details

Details for the file langchain_dkg-0.1.3.tar.gz.

File metadata

  • Download URL: langchain_dkg-0.1.3.tar.gz
  • Upload date:
  • Size: 26.4 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.1.0 CPython/3.13.12

File hashes

Hashes for langchain_dkg-0.1.3.tar.gz
Algorithm Hash digest
SHA256 4355e06cfb3df25fdedbb4a71dcbc11df8d33edc3d0095a130a6e3bdd0519569
MD5 dcc95ef245265a58e0f3f177042ad10c
BLAKE2b-256 7b394f06923d31491e5f17ad2eeac043381653308ca510dca76d3f9905b5422b

See more details on using hashes here.

Provenance

The following attestation bundles were made for langchain_dkg-0.1.3.tar.gz:

Publisher: publish.yml on haroldboom/dkg-langchain

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file langchain_dkg-0.1.3-py3-none-any.whl.

File metadata

  • Download URL: langchain_dkg-0.1.3-py3-none-any.whl
  • Upload date:
  • Size: 12.1 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.1.0 CPython/3.13.12

File hashes

Hashes for langchain_dkg-0.1.3-py3-none-any.whl
Algorithm Hash digest
SHA256 89d5e6ba03be9fcabd1dd06bd5fefedd147ccb20954003fa57d5966edf67dee0
MD5 ba6e04d69fb217c559d100f703814521
BLAKE2b-256 63ef582c86dafad60abf24cff7e2093e84831ceb3f4f788d21bc05ce3e598e85

See more details on using hashes here.

Provenance

The following attestation bundles were made for langchain_dkg-0.1.3-py3-none-any.whl:

Publisher: publish.yml on haroldboom/dkg-langchain

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

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