Skip to main content

The langmem alpha SDK

Project description

LangMem Python SDK

LangMem gives your chat bot long-term memory so it can personalize its interactions to each user and environment.

1. Install

To install the LangMem Python SDK, run the following command:

pip install -U langmem

Before using LangMem, make sure to set your API URL and API key as environment variables:

export LANGMEM_API_URL=https://your-instance-url
export LANGMEM_API_KEY=your-api-key

2. Save Conversations

LangMem automatically extracts memories from conversations in the background. To save a conversation, use the add_messages method:

import uuid
from langmem import AsyncClient

client = AsyncClient()
user_id = str(uuid.uuid4())
thread_id = str(uuid.uuid4())

messages = [
    {"role": "user", "content": "Hi, I love playing basketball!", "metadata": {"user_id": user_id}},
    {"role": "assistant", "content": "That's great! Basketball is a fun sport. Do you have a favorite player?"},
    {"role": "user", "content": "Yeah, Steph Curry is amazing!", "metadata": {"user_id": user_id}}
]

await client.add_messages(thread_id=thread_id, messages=messages)
await client.trigger_all_for_thread(thread_id=thread_id)

3. Remember

To retrieve relevant memories for a user, use the query_user_memory method:

async def completion_with_memory(messages, user_id):
    memories = await client.query_user_memory(
        user_id=user_id,
        text=messages[-1]["content"],
    )
    facts = "\n".join([mem["text"] for mem in memories["memories"]])

    system_prompt = {
        "role": "system",
        "content": "Here are some things you know"
         f" about the user:\n\n{facts}"
        }

    return await completion([system_prompt] + messages)

new_messages = [
    {"role": "user", "content": "Do you remember who my favorite basketball player is?", "metadata": {"user_id": user_id}}
]

response = await completion_with_memory(new_messages, user_id=user_id)
print(response.choices[0].message.content)

Concepts

LangMem organizes conversations into threads, where each thread contains a list of messages with content, role, and optional user information. As the app developer, you can configure different memory types based on your needs.

Memory Types

LangMem supports three types of user memories (exposed as memory_functions):

  1. User State: A structured profile that LangMem maintains for each user.
  2. Semantic Memory: An unstructured memory that generates knowledge triplets from conversations. It can be queried based on relevance, importance, and recency.
  3. Append-only State: A hybrid of the above two memory types that allows you to customize the memory schema while still retrieving based on relevance and recency.

You can also track thread-level memories using the thread_summary memory type, which is useful for including summaries of recent conversations in your system prompt.

For more details on memory types and when to use each one, refer to the Memory Types documentation.

Reference

  • Client: Documentation for the Client and AsyncClient objects in the client.py file.

Note

LangMem is currently in early alpha, so expect improvements and potential breaking changes. Your feedback is important to us!

For a more detailed walkthrough of the core functionality, check out the LangMem Walkthrough Notebook.

Thanks!

Thanks for your feedback! Email your questions and requests to mailto:will@langchain.dev.

memy

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

langmem-0.0.2.tar.gz (14.3 kB view details)

Uploaded Source

Built Distribution

langmem-0.0.2-py3-none-any.whl (14.9 kB view details)

Uploaded Python 3

File details

Details for the file langmem-0.0.2.tar.gz.

File metadata

  • Download URL: langmem-0.0.2.tar.gz
  • Upload date:
  • Size: 14.3 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: poetry/1.7.1 CPython/3.11.2 Darwin/23.4.0

File hashes

Hashes for langmem-0.0.2.tar.gz
Algorithm Hash digest
SHA256 2874e8c74b5ad4e98ec968edf0157efd9e62d73120c2ab7ace3657664dbf6420
MD5 552d9ed14df34a66c2660a1eb6e26643
BLAKE2b-256 12767977adb14b46b6cc7e3ec29898ad6896cd61555332af5302a6e8e2f9f3f5

See more details on using hashes here.

File details

Details for the file langmem-0.0.2-py3-none-any.whl.

File metadata

  • Download URL: langmem-0.0.2-py3-none-any.whl
  • Upload date:
  • Size: 14.9 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: poetry/1.7.1 CPython/3.11.2 Darwin/23.4.0

File hashes

Hashes for langmem-0.0.2-py3-none-any.whl
Algorithm Hash digest
SHA256 7c11a76a700640f715581de99384c771763e3fb842f71eb8775b31447e52c332
MD5 b9f0395af9b3e0932b3533a04af78799
BLAKE2b-256 fb53ab93e56dbad02eb8c3088816954e92d30199b18b170ac29eede228fd31ea

See more details on using hashes here.

Supported by

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