555e282cc4
pi-agent-plugin checks / lint (push) Has been cancelled
pi-agent-plugin checks / test (20) (push) Has been cancelled
pi-agent-plugin checks / test (22) (push) Has been cancelled
pi-agent-plugin checks / build (push) Has been cancelled
TypeScript SDK CI / check_changes (push) Has been cancelled
TypeScript SDK CI / changelog_check (push) Has been cancelled
ci / changelog_check (push) Has been cancelled
ci / check_changes (push) Has been cancelled
ci / build_mem0 (3.10) (push) Has been cancelled
ci / build_mem0 (3.11) (push) Has been cancelled
ci / build_mem0 (3.12) (push) Has been cancelled
CLI Node CI / lint (push) Has been cancelled
CLI Node CI / test (20) (push) Has been cancelled
CLI Node CI / test (22) (push) Has been cancelled
CLI Node CI / build (push) Has been cancelled
CLI Python CI / lint (push) Has been cancelled
CLI Python CI / test (3.10) (push) Has been cancelled
CLI Python CI / test (3.11) (push) Has been cancelled
CLI Python CI / test (3.12) (push) Has been cancelled
CLI Python CI / build (push) Has been cancelled
openclaw checks / lint (push) Has been cancelled
openclaw checks / test (20) (push) Has been cancelled
openclaw checks / test (22) (push) Has been cancelled
openclaw checks / build (push) Has been cancelled
opencode-plugin checks / build (push) Has been cancelled
TypeScript SDK CI / build_ts_sdk (20) (push) Has been cancelled
TypeScript SDK CI / build_ts_sdk (22) (push) Has been cancelled
TypeScript SDK CI / integration_ts_sdk (20) (push) Has been cancelled
TypeScript SDK CI / integration_ts_sdk (22) (push) Has been cancelled
185 lines
3.9 KiB
Plaintext
185 lines
3.9 KiB
Plaintext
---
|
|
title: Async Client
|
|
description: "Use the AsyncMemoryClient for non-blocking memory operations in high-concurrency Python applications."
|
|
---
|
|
|
|
The `AsyncMemoryClient` is an asynchronous client for interacting with the Mem0 API. It provides similar functionality to the synchronous `MemoryClient` but allows for non-blocking operations, which can be beneficial in applications that require high concurrency.
|
|
|
|
## Initialization
|
|
|
|
To use the async client, you first need to initialize it:
|
|
|
|
<CodeGroup>
|
|
|
|
```python Python
|
|
import os
|
|
from mem0 import AsyncMemoryClient
|
|
|
|
os.environ["MEM0_API_KEY"] = "your-api-key"
|
|
|
|
client = AsyncMemoryClient()
|
|
```
|
|
|
|
```javascript JavaScript
|
|
const { MemoryClient } = require('mem0ai');
|
|
const client = new MemoryClient({ apiKey: 'your-api-key'});
|
|
```
|
|
|
|
</CodeGroup>
|
|
|
|
## Methods
|
|
|
|
The `AsyncMemoryClient` provides the following methods:
|
|
|
|
### Add
|
|
|
|
Add a new memory asynchronously.
|
|
|
|
<CodeGroup>
|
|
|
|
```python Python
|
|
messages = [
|
|
{"role": "user", "content": "Alice loves playing badminton"},
|
|
{"role": "assistant", "content": "That's great! Alice is a fitness freak"},
|
|
]
|
|
await client.add(messages, user_id="alice")
|
|
```
|
|
|
|
```javascript JavaScript
|
|
const messages = [
|
|
{"role": "user", "content": "Alice loves playing badminton"},
|
|
{"role": "assistant", "content": "That's great! Alice is a fitness freak"},
|
|
];
|
|
await client.add(messages, { userId: "alice" });
|
|
```
|
|
|
|
</CodeGroup>
|
|
|
|
### Search
|
|
|
|
Search for memories based on a query asynchronously.
|
|
|
|
<CodeGroup>
|
|
|
|
```python Python
|
|
await client.search("What is Alice's favorite sport?", filters={"user_id": "alice"})
|
|
```
|
|
|
|
```javascript JavaScript
|
|
await client.search("What is Alice's favorite sport?", { filters: { userId: "alice" } });
|
|
```
|
|
|
|
</CodeGroup>
|
|
|
|
### Get All
|
|
|
|
Retrieve all memories for a user asynchronously.
|
|
|
|
<Callout type="warning" title="Filters Required">
|
|
`get_all()` now requires filters to be specified.
|
|
</Callout>
|
|
|
|
<CodeGroup>
|
|
|
|
```python Python
|
|
await client.get_all(filters={"AND": [{"user_id": "alice"}]})
|
|
```
|
|
|
|
```javascript JavaScript
|
|
await client.getAll({ filters: {"AND": [{"user_id": "alice"}]} });
|
|
```
|
|
|
|
</CodeGroup>
|
|
|
|
### Delete
|
|
|
|
Delete a specific memory asynchronously.
|
|
|
|
<CodeGroup>
|
|
|
|
```python Python
|
|
await client.delete(memory_id="memory-id-here")
|
|
```
|
|
|
|
```javascript JavaScript
|
|
await client.delete("memory-id-here");
|
|
```
|
|
|
|
</CodeGroup>
|
|
|
|
### Delete All
|
|
|
|
Delete all memories for a user asynchronously.
|
|
|
|
<CodeGroup>
|
|
|
|
```python Python
|
|
await client.delete_all(user_id="alice")
|
|
```
|
|
|
|
```javascript JavaScript
|
|
await client.deleteAll({ userId: "alice" });
|
|
```
|
|
|
|
</CodeGroup>
|
|
|
|
<Note>
|
|
At least one filter (`user_id`, `agent_id`, `app_id`, or `run_id`) is required: calling `delete_all` with no filters raises an error to prevent accidental data loss. You can pass `"*"` as a value to delete all memories for a given entity type (e.g., `user_id="*"` removes memories for every user). A full project wipe requires all four filters set to `"*"`.
|
|
</Note>
|
|
|
|
### History
|
|
|
|
Get the history of a specific memory asynchronously.
|
|
|
|
<CodeGroup>
|
|
|
|
```python Python
|
|
await client.history(memory_id="memory-id-here")
|
|
```
|
|
|
|
```javascript JavaScript
|
|
await client.history("memory-id-here");
|
|
```
|
|
|
|
</CodeGroup>
|
|
|
|
### Users
|
|
|
|
Get all users, agents, and runs which have memories associated with them asynchronously.
|
|
|
|
<CodeGroup>
|
|
|
|
```python Python
|
|
await client.users()
|
|
```
|
|
|
|
```javascript JavaScript
|
|
await client.users();
|
|
```
|
|
|
|
</CodeGroup>
|
|
|
|
### Reset
|
|
|
|
Reset the client, deleting all users and memories asynchronously.
|
|
|
|
<CodeGroup>
|
|
|
|
```python Python
|
|
await client.reset()
|
|
```
|
|
|
|
```javascript JavaScript
|
|
await client.reset();
|
|
```
|
|
|
|
</CodeGroup>
|
|
|
|
## Conclusion
|
|
|
|
The `AsyncMemoryClient` provides a powerful way to interact with the Mem0 API asynchronously, allowing for more efficient and responsive applications. By using this client, you can perform memory operations without blocking your application's execution.
|
|
|
|
If you have any questions or need further assistance, please don't hesitate to reach out:
|
|
|
|
<Snippet file="get-help.mdx" />
|