跳到主要内容

文档索引

在以下地址获取完整的文档索引:https://docs.langchain.org.cn/llms.txt

在进一步探索之前,请使用此文件发现所有可用页面。

当将智能体部署到 LangSmith 时,服务器提供了一个内置的、由 Postgres 支持的长期记忆存储,并可通过 pgvector 实现可选的向量搜索。你可以使用自己的 BaseStore 实现来替换它,以使用不同的存储后端、自定义索引或专门的搜索功能。 你需要提供一个异步上下文管理器的路径,该管理器会生成一个 BaseStore 实例,服务器会自动管理该存储的生命周期。
自定义存储目前处于 alpha(实验性)阶段。在次要版本更新中,此功能可能会发生重大变更。

定义存储

基于现有的 LangSmith 应用程序,创建一个定义了异步上下文管理器的文件,该管理器用于生成你的自定义存储。如果你正在开始一个新项目,可以使用 CLI 从模板创建一个应用程序。
langgraph new --template=new-langgraph-project-python my_new_project
异步上下文管理器模式允许服务器在应用程序生命周期的合适点打开和关闭存储连接。以下示例使用了带有语义搜索功能的 AsyncSqliteStore
不建议在生产部署中使用 SQLite。
# ./src/agent/store.py
import contextlib

from langchain.embeddings import init_embeddings
from langgraph.store.base import IndexConfig
from langgraph.store.sqlite import AsyncSqliteStore

embeddings = init_embeddings("openai:text-embedding-3-small")


@contextlib.asynccontextmanager
async def generate_store():
    """Yield a BaseStore, open for the duration of the server."""
    async with AsyncSqliteStore.from_conn_string(
        "./custom_store.sql",
        index=IndexConfig(
            dims=1536,
            embed=embeddings,
            fields=["$"],
        ),
    ) as store:
        await store.setup()
        yield store
当配置了自定义存储后,它会完全替换内置的 Postgres 存储。语义搜索和 TTL 清理等功能将取决于你的具体实现。

配置 langgraph.json

store 键添加到你的 langgraph.json 配置文件中。path 指向你之前定义的异步上下文管理器。
{
  "dependencies": ["."],
  "graphs": {
    "agent": "./src/agent/graph.py:graph"
  },
  "env": ".env",
  "store": {
    "path": "./src/agent/store.py:generate_store"
  }
}

启动服务器

在本地测试服务器
langgraph dev --no-browser
服务器日志将确认你的自定义存储已激活。
Using custom store. Skipping store TTL sweeper.

部署中

你可以将此应用原样部署到 LangSmith 或你自托管的平台。

后续步骤


© . This site is unofficial and not affiliated with LangChain, Inc.