Skip to main content

MemHQ adapter for LangChain — chat message history and retriever backed by MemHQ.

Project description

memhq-langchain

MemHQ adapter for LangChain (Python). Drop-in chat message history and retriever backed by the MemHQ hosted API. Sync and async both supported via httpx.

Install

pip install memhq-langchain

Quickstart — Chat history

import os
from langchain_openai import ChatOpenAI
from langchain_core.prompts import ChatPromptTemplate, MessagesPlaceholder
from langchain_core.runnables.history import RunnableWithMessageHistory

from memhq_langchain import MemHQChatMessageHistory

model = ChatOpenAI(model="gpt-4o-mini")
prompt = ChatPromptTemplate.from_messages([
    ("system", "You have access to the user's memory."),
    MessagesPlaceholder("history"),
    ("human", "{input}"),
])

chain = RunnableWithMessageHistory(
    prompt | model,
    lambda session_id: MemHQChatMessageHistory(
        api_key=os.environ["MEMHQ_API_KEY"],
        session_id=session_id,
        user_id="user_42",
    ),
    input_messages_key="input",
    history_messages_key="history",
)

reply = chain.invoke(
    {"input": "What's my favorite coffee order?"},
    config={"configurable": {"session_id": "conv_42"}},
)

Quickstart — Retriever

from memhq_langchain import MemHQRetriever

retriever = MemHQRetriever(
    api_key=os.environ["MEMHQ_API_KEY"],
    user_id="user_42",
    limit=8,
)

docs = retriever.invoke("pricing discussions")

Configuration

Argument Description
api_key MemHQ project API key. Falls back to MEMHQ_API_KEY.
user_id External user id. Required.
session_id Thread anchor for the history adapter.
recall_limit Memories surfaced per history read. Default 10.
limit Memories returned per retriever query. Default 10.
mode "hybrid" (default), "vector", or "lexical".
base_url Override the API base.

Reference

Full reference: https://docs.memhq.ai/sdks/langchain-py

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

memhq_langchain-0.1.0.tar.gz (9.3 kB view details)

Uploaded Source

Built Distribution

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

memhq_langchain-0.1.0-py3-none-any.whl (10.8 kB view details)

Uploaded Python 3

File details

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

File metadata

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

File hashes

Hashes for memhq_langchain-0.1.0.tar.gz
Algorithm Hash digest
SHA256 c1dabae72c5e318e497d0172ef06c18915aba7b7a4762e7acb66504bdcfb7e64
MD5 f830e53d0146edfee11bb4babce5a398
BLAKE2b-256 cd978930a90139bf96d65fd4e9d44cd80c3dd0ed94dec1099c1840ebe497a24a

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for memhq_langchain-0.1.0-py3-none-any.whl
Algorithm Hash digest
SHA256 2067175ab00b67d275f73cc2c4c192985c2ffb3535e37f192fba465d8392fb5a
MD5 3c03a2fd128b3afabbc62bdd78b028f5
BLAKE2b-256 d7aa37be87e173c4b9e111f831329ac69710aaa4536548d03e17db40798c705a

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