本指南解释如何向部署的跨线程存储添加语义搜索,以便智能体可以通过语义相似性搜索内存和其他文档。

前提条件

步骤

  1. 更新您的 langgraph.json 配置文件以包含存储配置:
{
    ...
    "store": {
        "index": {
            "embed": "openai:text-embedding-3-small",
            "dims": 1536,
            "fields": ["$"]
        }
    }
}
此配置:
  • 使用 OpenAI 的 text-embedding-3-small 模型生成嵌入
  • 将嵌入维度设置为 1536(与模型的输出匹配)
  • 索引存储数据中的所有字段(["$"] 表示索引所有内容,或指定特定字段,如 ["text", "metadata.title"]
  1. 要使用上面的字符串嵌入格式,请确保您的依赖项包含 langchain >= 0.3.8
# 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 nodes. The store requires a namespace tuple to organize memories:
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",
        limit=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:
{
    ...
    "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:
# 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:
from langgraph_sdk import get_client

async def search_store():
    client = get_client()
    results = await client.store.search_items(
        ("memory", "facts"),
        query="your search query",
        limit=3  # number of results to return
    )
    return results

# Use in an async context
results = await search_store()

Connect these docs programmatically to Claude, VSCode, and more via MCP for real-time answers.