-
Notifications
You must be signed in to change notification settings - Fork 1.3k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Update docstrings for store classes (#2616)
- Loading branch information
Showing
13 changed files
with
533 additions
and
68 deletions.
There are no files selected for viewing
2 changes: 1 addition & 1 deletion
2
docs/cassettes/cross-thread-persistence_c871a073-a466-46ad-aafe-2b870831057e.msgpack.zlib
Large diffs are not rendered by default.
Oops, something went wrong.
2 changes: 1 addition & 1 deletion
2
docs/cassettes/cross-thread-persistence_d362350b-d730-48bd-9652-983812fd7811.msgpack.zlib
Large diffs are not rendered by default.
Oops, something went wrong.
2 changes: 1 addition & 1 deletion
2
docs/cassettes/cross-thread-persistence_d862be40-1f8a-4057-81c4-b7bf073dc4c1.msgpack.zlib
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,123 @@ | ||
# How to add semantic search to your LangGraph deployment | ||
|
||
This guide explains how to add semantic search to your LangGraph deployment's cross-thread [store](../../concepts/persistence.md#memory-store), so that your agent can search for memories and other documents by semantic similarity. | ||
|
||
## Prerequisites | ||
|
||
- A LangGraph deployment (see [how to deploy](setup_pyproject.md)) | ||
- API keys for your embedding provider (in this case, OpenAI) | ||
- `langchain >= 0.3.8` (if you specify using the string format below) | ||
|
||
## Steps | ||
|
||
1. Update your `langgraph.json` configuration file to include the store configuration: | ||
|
||
```json | ||
{ | ||
... | ||
"store": { | ||
"index": { | ||
"embed": "openai:text-embeddings-3-small", | ||
"dims": 1536, | ||
"fields": ["$"] | ||
} | ||
} | ||
} | ||
``` | ||
|
||
This configuration: | ||
|
||
- Uses OpenAI's text-embeddings-3-small model for generating embeddings | ||
- Sets the embedding dimension to 1536 (matching the model's output) | ||
- Indexes all fields in your stored data (`["$"]` means index everything, or specify specific fields like `["text", "metadata.title"]`) | ||
|
||
2. To use the string embedding format above, make sure your dependencies include `langchain >= 0.3.8`: | ||
|
||
```toml | ||
# In pyproject.toml | ||
[project] | ||
dependencies = [ | ||
"langchain>=0.3.8" | ||
] | ||
``` | ||
|
||
Or if using requirements.txt: | ||
|
||
``` | ||
langchain>=0.3.8 | ||
``` | ||
|
||
## Usage | ||
|
||
Once configured, you can use semantic search in your LangGraph nodes. The store requires a namespace tuple to organize memories: | ||
|
||
```python | ||
def search_memory(state: State, *, store: BaseStore): | ||
# Search the store using semantic similarity | ||
# The namespace tuple helps organize different types of memories | ||
# e.g., ("user_facts", "preferences") or ("conversation", "summaries") | ||
results = store.search( | ||
namespace=("memory", "facts"), # Organize memories by type | ||
query="your search query", | ||
k=3 # number of results to return | ||
) | ||
return results | ||
``` | ||
|
||
## Custom Embeddings | ||
|
||
If you want to use custom embeddings, you can pass a path to a custom embedding function: | ||
|
||
```json | ||
{ | ||
... | ||
"store": { | ||
"index": { | ||
"embed": "path/to/embedding_function.py:embed", | ||
"dims": 1536, | ||
"fields": ["$"] | ||
} | ||
} | ||
} | ||
``` | ||
|
||
The deployment will look for the function in the specified path. The function must be async and accept a list of strings: | ||
|
||
```python | ||
# path/to/embedding_function.py | ||
from openai import AsyncOpenAI | ||
|
||
client = AsyncOpenAI() | ||
|
||
async def aembed_texts(texts: list[str]) -> list[list[float]]: | ||
"""Custom embedding function that must: | ||
1. Be async | ||
2. Accept a list of strings | ||
3. Return a list of float arrays (embeddings) | ||
""" | ||
response = await client.embeddings.create( | ||
model="text-embedding-3-small", | ||
input=texts | ||
) | ||
return [e.embedding for e in response.data] | ||
``` | ||
|
||
## Querying via the API | ||
|
||
You can also query the store using the LangGraph SDK. Since the SDK uses async operations: | ||
|
||
```python | ||
from langgraph_sdk import get_client | ||
|
||
async def search_store(): | ||
client = get_client() | ||
results = await client.store.search( | ||
namespace=("memory", "facts"), | ||
query="your search query", | ||
limit=3 # number of results to return | ||
) | ||
return results | ||
|
||
# Use in an async context | ||
results = await search_store() | ||
``` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.