Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support index embed specification via string #3317

Merged
merged 4 commits into from
Feb 9, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion libs/checkpoint/langgraph/store/base/__init__.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
"""Base classes and types for persistent key-value stores.

Check notice on line 1 in libs/checkpoint/langgraph/store/base/__init__.py

View workflow job for this annotation

GitHub Actions / benchmark

Benchmark results

......................................... fanout_to_subgraph_10x: Mean +- std dev: 64.0 ms +- 1.2 ms ......................................... fanout_to_subgraph_10x_sync: Mean +- std dev: 55.4 ms +- 0.6 ms ......................................... fanout_to_subgraph_10x_checkpoint: Mean +- std dev: 80.7 ms +- 1.2 ms ......................................... fanout_to_subgraph_10x_checkpoint_sync: Mean +- std dev: 99.4 ms +- 2.4 ms ......................................... fanout_to_subgraph_100x: Mean +- std dev: 641 ms +- 9 ms ......................................... fanout_to_subgraph_100x_sync: Mean +- std dev: 551 ms +- 17 ms ......................................... fanout_to_subgraph_100x_checkpoint: Mean +- std dev: 828 ms +- 29 ms ......................................... fanout_to_subgraph_100x_checkpoint_sync: Mean +- std dev: 1.01 sec +- 0.02 sec ......................................... react_agent_10x: Mean +- std dev: 31.0 ms +- 0.7 ms ......................................... react_agent_10x_sync: Mean +- std dev: 23.4 ms +- 0.2 ms ......................................... react_agent_10x_checkpoint: Mean +- std dev: 39.5 ms +- 0.9 ms ......................................... react_agent_10x_checkpoint_sync: Mean +- std dev: 36.8 ms +- 0.6 ms ......................................... react_agent_100x: Mean +- std dev: 346 ms +- 7 ms ......................................... react_agent_100x_sync: Mean +- std dev: 275 ms +- 2 ms ......................................... react_agent_100x_checkpoint: Mean +- std dev: 649 ms +- 7 ms ......................................... react_agent_100x_checkpoint_sync: Mean +- std dev: 620 ms +- 7 ms ......................................... wide_state_25x300: Mean +- std dev: 23.9 ms +- 0.4 ms ......................................... wide_state_25x300_sync: Mean +- std dev: 16.0 ms +- 0.2 ms ......................................... wide_state_25x300_checkpoint: Mean +- std dev: 242 ms +- 2 ms ......................................... wide_state_25x300_checkpoint_sync: Mean +- std dev: 237 ms +- 2 ms ......................................... wide_state_15x600: Mean +- std dev: 28.1 ms +- 0.6 ms ......................................... wide_state_15x600_sync: Mean +- std dev: 18.6 ms +- 0.2 ms ......................................... wide_state_15x600_checkpoint: Mean +- std dev: 424 ms +- 7 ms ......................................... wide_state_15x600_checkpoint_sync: Mean +- std dev: 416 ms +- 4 ms ......................................... wide_state_9x1200: Mean +- std dev: 28.1 ms +- 0.5 ms ......................................... wide_state_9x1200_sync: Mean +- std dev: 18.5 ms +- 0.2 ms ......................................... wide_state_9x1200_checkpoint: Mean +- std dev: 275 ms +- 6 ms ......................................... wide_state_9x1200_checkpoint_sync: Mean +- std dev: 267 ms +- 2 ms

Check notice on line 1 in libs/checkpoint/langgraph/store/base/__init__.py

View workflow job for this annotation

GitHub Actions / benchmark

Comparison against main

+-----------------------------------------+---------+------------------------+ | Benchmark | main | changes | +=========================================+=========+========================+ | fanout_to_subgraph_100x_checkpoint | 848 ms | 828 ms: 1.02x faster | +-----------------------------------------+---------+------------------------+ | wide_state_15x600_checkpoint_sync | 424 ms | 416 ms: 1.02x faster | +-----------------------------------------+---------+------------------------+ | wide_state_25x300_checkpoint_sync | 241 ms | 237 ms: 1.02x faster | +-----------------------------------------+---------+------------------------+ | wide_state_9x1200_checkpoint_sync | 271 ms | 267 ms: 1.02x faster | +-----------------------------------------+---------+------------------------+ | fanout_to_subgraph_10x_sync | 56.3 ms | 55.4 ms: 1.02x faster | +-----------------------------------------+---------+------------------------+ | wide_state_25x300_checkpoint | 246 ms | 242 ms: 1.02x faster | +-----------------------------------------+---------+------------------------+ | wide_state_15x600_checkpoint | 430 ms | 424 ms: 1.01x faster | +-----------------------------------------+---------+------------------------+ | fanout_to_subgraph_100x | 648 ms | 641 ms: 1.01x faster | +-----------------------------------------+---------+------------------------+ | wide_state_9x1200_checkpoint | 277 ms | 275 ms: 1.01x faster | +-----------------------------------------+---------+------------------------+ | fanout_to_subgraph_10x | 64.4 ms | 64.0 ms: 1.01x faster | +-----------------------------------------+---------+------------------------+ | fanout_to_subgraph_10x_checkpoint | 81.0 ms | 80.7 ms: 1.00x faster | +-----------------------------------------+---------+------------------------+ | wide_state_25x300_sync | 16.0 ms | 16.0 ms: 1.00x slower | +-----------------------------------------+---------+------------------------+ | react_agent_10x_checkpoint_sync | 36.7 ms | 36.8 ms: 1.00x slower | +-----------------------------------------+---------+------------------------+ | react_agent_10x_sync | 23.3 ms | 23.4 ms: 1.01x slower | +-----------------------------------------+---------+------------------------+ | wide_state_9x1200_sync | 18.4 ms | 18.5 ms: 1.01x slower | +-----------------------------------------+---------+------------------------+ | wide_state_9x1200 | 27.9 ms | 28.1 ms: 1.01x slower | +-----------------------------------------+---------+------------------------+ | wide_state_15x600 | 27.9 ms | 28.1 ms: 1.01x slower | +-----------------------------------------+---------+------------------------+ | react_agent_100x_checkpoint_sync | 616 ms | 620 ms: 1.01x slower | +-----------------------------------------+---------+------------------------+ | wide_state_15x600_sync | 18.4 ms | 18.6 ms: 1.01x slower | +-----------------------------------------+---------+------------------------+ | fanout_to_subgraph_100x_checkpoint_sync | 998 ms | 1.01 sec: 1.01x slower | +-----------------------------------------+---------+------------------------+ | Geometric mean | (ref) | 1.00x faster | +-----------------------------------------+---------+------------------------+ Benchmark hidden because not significant (8): react_agent_10x, wide_state_25x300, react_agent_100x_sync, fanout_to_subgraph_10x_checkpoint_sync, react_agent_100x_checkpoint, react_agent_10x_checkpoint, fanout_to_subgraph_100x_sync, react_agent_100x

Stores provide long-term memory that persists across threads and conversations.
Supports hierarchical namespaces, key-value storage, and optional vector search.
Expand Down Expand Up @@ -493,13 +493,14 @@
- cohere:embed-multilingual-light-v3.0: 384
"""

embed: Union[Embeddings, EmbeddingsFunc, AEmbeddingsFunc]
embed: Union[Embeddings, EmbeddingsFunc, AEmbeddingsFunc, str]
"""Optional function to generate embeddings from text.

Can be specified in three ways:
1. A LangChain Embeddings instance
2. A synchronous embedding function (EmbeddingsFunc)
3. An asynchronous embedding function (AEmbeddingsFunc)
4. A provider string (e.g., "openai:text-embedding-3-small")

???+ example "Examples"
Using LangChain's initialization with InMemoryStore:
Expand Down
41 changes: 40 additions & 1 deletion libs/checkpoint/langgraph/store/base/embed.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
"""

import asyncio
import functools
import json
from typing import Any, Awaitable, Callable, Optional, Sequence, Union

Expand All @@ -28,7 +29,7 @@


def ensure_embeddings(
embed: Union[Embeddings, EmbeddingsFunc, AEmbeddingsFunc, None],
embed: Union[Embeddings, EmbeddingsFunc, AEmbeddingsFunc, str, None],
) -> Embeddings:
"""Ensure that an embedding function conforms to LangChain's Embeddings interface.

Expand Down Expand Up @@ -62,9 +63,37 @@ async def my_async_fn(texts):
embeddings = ensure_embeddings(my_async_fn)
result = await embeddings.aembed_query("hello") # Returns [0.1, 0.2]
```

Initialize embeddings using a provider string:
```python
# Requires langchain>=0.3.9 and langgraph-checkpoint>=2.0.11
embeddings = ensure_embeddings("openai:text-embedding-3-small")
result = embeddings.embed_query("hello")
```
"""
if embed is None:
raise ValueError("embed must be provided")
if isinstance(embed, str):
init_embeddings = _get_init_embeddings()
if init_embeddings is None:
from importlib.metadata import PackageNotFoundError, version

try:
lc_version = version("langchain")
version_info = f"Found langchain version {lc_version}, but"
except PackageNotFoundError:
version_info = "langchain is not installed;"

raise ValueError(
f"Could not load embeddings from string '{embed}'. {version_info} "
"loading embeddings by provider:identifier string requires langchain>=0.3.9 "
"as well as the provider-specific package. "
"Install LangChain with: pip install 'langchain>=0.3.9' "
"and the provider-specific package (e.g., 'langchain-openai>=0.3.0'). "
"Alternatively, specify 'embed' as a compatible Embeddings object or python function."
)
return init_embeddings(embed)

if isinstance(embed, Embeddings):
return embed
return EmbeddingsLambda(embed)
Expand Down Expand Up @@ -373,6 +402,16 @@ def _is_async_callable(
)


@functools.lru_cache
def _get_init_embeddings() -> Optional[Callable[[str], Embeddings]]:
try:
from langchain.embeddings import init_embeddings # type: ignore

return init_embeddings
except ImportError:
return None


__all__ = [
"ensure_embeddings",
"EmbeddingsFunc",
Expand Down
2 changes: 1 addition & 1 deletion libs/checkpoint/langgraph/store/memory/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -493,7 +493,7 @@ def _cosine_similarity(X: list[float], Y: list[list[float]]) -> list[float]:
if not Y:
return []
if _check_numpy():
import numpy as np # type: ignore
import numpy as np # type: ignore[import-not-found]

X_arr = np.array(X) if not isinstance(X, np.ndarray) else X
Y_arr = np.array(Y) if not isinstance(Y, np.ndarray) else Y
Expand Down
2 changes: 1 addition & 1 deletion libs/checkpoint/pyproject.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[tool.poetry]
name = "langgraph-checkpoint"
version = "2.0.11"
version = "2.0.12"
description = "Library with base interfaces for LangGraph checkpoint savers."
authors = []
license = "MIT"
Expand Down
Loading