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_function
s):
- User State: A structured profile that LangMem maintains for each user.
- Semantic Memory: An unstructured memory that generates knowledge triplets from conversations. It can be queried based on relevance, importance, and recency.
- 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
andAsyncClient
objects in theclient.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.
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
File details
Details for the file langmem-0.0.4.tar.gz
.
File metadata
- Download URL: langmem-0.0.4.tar.gz
- Upload date:
- Size: 14.6 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: poetry/1.8.3 CPython/3.11.2 Darwin/23.4.0
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | cec27d49ff3416979be47cf7c93f439cfd835c326b53214d19b6225195fe8a55 |
|
MD5 | c0cbfb32c8fc5f4ebb95929403545005 |
|
BLAKE2b-256 | 2e97c69c1433c30e3a2809128cff8f6cf4f1e08c8f0a301424065e369ee0adb9 |
File details
Details for the file langmem-0.0.4-py3-none-any.whl
.
File metadata
- Download URL: langmem-0.0.4-py3-none-any.whl
- Upload date:
- Size: 15.4 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: poetry/1.8.3 CPython/3.11.2 Darwin/23.4.0
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 3f1dff027591c14ac0f0d585c7b22e665d5c0fcf2143bc8a982c903f6e2c688d |
|
MD5 | 66304140104caf31c31f72bd4ad5b1d8 |
|
BLAKE2b-256 | ea873c0de81ffb1f6756af289265aa2f000688bbf4649330e179844cb1615a2a |