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.3.tar.gz (14.3 kB view details)

Uploaded Source

Built Distribution

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

Uploaded Python 3

File details

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

File metadata

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

File hashes

Hashes for langmem-0.0.3.tar.gz
Algorithm Hash digest
SHA256 130c66b4694b50fc3b0becc00278b4178cecafa585907a497eafb6485576034b
MD5 4fbd8912142f2516fafaf816fc715ef5
BLAKE2b-256 5f5b0000f466e51b23d9c35316c84d99d26854f97f8db78e196fe731d4fe3e34

See more details on using hashes here.

File details

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

File metadata

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

File hashes

Hashes for langmem-0.0.3-py3-none-any.whl
Algorithm Hash digest
SHA256 fee6ff5ebd0fe6ce481ee4fef9a398a4280259259891faee4c07208f26943ac2
MD5 74e97f7f416ebebf82d3ba0b5607ea17
BLAKE2b-256 8fdefe172b470ba5a0a97e747ffec72f2785ff3070e9a918d1697ac9b8042328

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