use sqlite

This commit is contained in:
朱潮 2025-12-18 11:12:05 +08:00
parent a92bea5b58
commit 881845f284
4 changed files with 64 additions and 7 deletions

View File

@ -7,7 +7,6 @@ from langchain.chat_models import init_chat_model
from langchain.agents import create_agent from langchain.agents import create_agent
from langchain.agents.middleware import SummarizationMiddleware from langchain.agents.middleware import SummarizationMiddleware
from langchain_mcp_adapters.client import MultiServerMCPClient from langchain_mcp_adapters.client import MultiServerMCPClient
from langgraph.checkpoint.memory import MemorySaver
from utils.fastapi_utils import detect_provider from utils.fastapi_utils import detect_provider
from .guideline_middleware import GuidelineMiddleware from .guideline_middleware import GuidelineMiddleware
from .tool_output_length_middleware import ToolOutputLengthMiddleware from .tool_output_length_middleware import ToolOutputLengthMiddleware
@ -16,13 +15,14 @@ from agent.agent_config import AgentConfig
from agent.prompt_loader import load_system_prompt_async, load_mcp_settings_async from agent.prompt_loader import load_system_prompt_async, load_mcp_settings_async
from agent.agent_memory_cache import get_memory_cache_manager from agent.agent_memory_cache import get_memory_cache_manager
from .checkpoint_utils import prepare_checkpoint_message from .checkpoint_utils import prepare_checkpoint_message
import aiosqlite
logger = logging.getLogger('app') from langgraph.checkpoint.sqlite.aio import AsyncSqliteSaver
# 全局 MemorySaver 实例 # 全局 MemorySaver 实例
_global_checkpointer = MemorySaver() # from langgraph.checkpoint.memory import MemorySaver
# 使用内存缓存管理器 # _global_checkpointer = MemorySaver()
logger = logging.getLogger('app')
# Utility functions # Utility functions
def read_system_prompt(): def read_system_prompt():
"""读取通用的无状态系统prompt""" """读取通用的无状态系统prompt"""
@ -75,7 +75,8 @@ async def init_agent(config: AgentConfig):
# 初始化 checkpointer 和中间件 # 初始化 checkpointer 和中间件
checkpointer = None checkpointer = None
if config.session_id: if config.session_id:
checkpointer = _global_checkpointer conn = aiosqlite.connect("projects/memory/checkpoints.db")
checkpointer = AsyncSqliteSaver(conn=conn)
await prepare_checkpoint_message(config, checkpointer) await prepare_checkpoint_message(config, checkpointer)
# 获取缓存管理器 # 获取缓存管理器
cache_manager = get_memory_cache_manager() cache_manager = get_memory_cache_manager()

53
poetry.lock generated
View File

@ -182,6 +182,25 @@ files = [
frozenlist = ">=1.1.0" frozenlist = ">=1.1.0"
typing-extensions = {version = ">=4.2", markers = "python_version < \"3.13\""} typing-extensions = {version = ">=4.2", markers = "python_version < \"3.13\""}
[[package]]
name = "aiosqlite"
version = "0.21.0"
description = "asyncio bridge to the standard sqlite3 module"
optional = false
python-versions = ">=3.9"
groups = ["main"]
files = [
{file = "aiosqlite-0.21.0-py3-none-any.whl", hash = "sha256:2549cf4057f95f53dcba16f2b64e8e2791d7e1adedb13197dd8ed77bb226d7d0"},
{file = "aiosqlite-0.21.0.tar.gz", hash = "sha256:131bb8056daa3bc875608c631c678cda73922a2d4ba8aec373b19f18c17e7aa3"},
]
[package.dependencies]
typing_extensions = ">=4.0"
[package.extras]
dev = ["attribution (==1.7.1)", "black (==24.3.0)", "build (>=1.2)", "coverage[toml] (==7.6.10)", "flake8 (==7.0.0)", "flake8-bugbear (==24.12.12)", "flit (==3.10.1)", "mypy (==1.14.1)", "ufmt (==2.5.1)", "usort (==1.0.8.post1)"]
docs = ["sphinx (==8.1.3)", "sphinx-mdinclude (==0.6.1)"]
[[package]] [[package]]
name = "annotated-types" name = "annotated-types"
version = "0.7.0" version = "0.7.0"
@ -1237,6 +1256,23 @@ files = [
langchain-core = ">=0.2.38" langchain-core = ">=0.2.38"
ormsgpack = ">=1.12.0" ormsgpack = ">=1.12.0"
[[package]]
name = "langgraph-checkpoint-sqlite"
version = "3.0.1"
description = "Library with a SQLite implementation of LangGraph checkpoint saver."
optional = false
python-versions = ">=3.10"
groups = ["main"]
files = [
{file = "langgraph_checkpoint_sqlite-3.0.1-py3-none-any.whl", hash = "sha256:616124676e5827294966997ed853f5d41490cc61f73b3c79359f4ff307728508"},
{file = "langgraph_checkpoint_sqlite-3.0.1.tar.gz", hash = "sha256:c6580138e6abfd2ade7ea49186c664d47ef28dc44538674fa47e50a8a5f8af83"},
]
[package.dependencies]
aiosqlite = ">=0.20"
langgraph-checkpoint = ">=3,<4.0.0"
sqlite-vec = ">=0.1.6"
[[package]] [[package]]
name = "langgraph-prebuilt" name = "langgraph-prebuilt"
version = "1.0.5" version = "1.0.5"
@ -3312,6 +3348,21 @@ files = [
{file = "sniffio-1.3.1.tar.gz", hash = "sha256:f4324edc670a0f49750a81b895f35c3adb843cca46f0530f79fc1babb23789dc"}, {file = "sniffio-1.3.1.tar.gz", hash = "sha256:f4324edc670a0f49750a81b895f35c3adb843cca46f0530f79fc1babb23789dc"},
] ]
[[package]]
name = "sqlite-vec"
version = "0.1.6"
description = ""
optional = false
python-versions = "*"
groups = ["main"]
files = [
{file = "sqlite_vec-0.1.6-py3-none-macosx_10_6_x86_64.whl", hash = "sha256:77491bcaa6d496f2acb5cc0d0ff0b8964434f141523c121e313f9a7d8088dee3"},
{file = "sqlite_vec-0.1.6-py3-none-macosx_11_0_arm64.whl", hash = "sha256:fdca35f7ee3243668a055255d4dee4dea7eed5a06da8cad409f89facf4595361"},
{file = "sqlite_vec-0.1.6-py3-none-manylinux_2_17_aarch64.manylinux2014_aarch64.whl", hash = "sha256:7b0519d9cd96164cd2e08e8eed225197f9cd2f0be82cb04567692a0a4be02da3"},
{file = "sqlite_vec-0.1.6-py3-none-manylinux_2_17_x86_64.manylinux2014_x86_64.manylinux1_x86_64.whl", hash = "sha256:823b0493add80d7fe82ab0fe25df7c0703f4752941aee1c7b2b02cec9656cb24"},
{file = "sqlite_vec-0.1.6-py3-none-win_amd64.whl", hash = "sha256:c65bcfd90fa2f41f9000052bcb8bb75d38240b2dae49225389eca6c3136d3f0c"},
]
[[package]] [[package]]
name = "sse-starlette" name = "sse-starlette"
version = "3.0.2" version = "3.0.2"
@ -4300,4 +4351,4 @@ cffi = ["cffi (>=1.17,<2.0) ; platform_python_implementation != \"PyPy\" and pyt
[metadata] [metadata]
lock-version = "2.1" lock-version = "2.1"
python-versions = ">=3.12,<4.0" python-versions = ">=3.12,<4.0"
content-hash = "a68bc624d1e70c475a496d81739797e72492ac7b39c133132a6297287d2eaf52" content-hash = "82684f606cbcf38de0c8391dd01a9c1273e140d42a5928c2987a7e972c85d36e"

View File

@ -19,6 +19,7 @@ dependencies = [
"numpy<2", "numpy<2",
"aiohttp", "aiohttp",
"aiofiles", "aiofiles",
"aiosqlite==0.21.0",
"huey (>=2.5.3,<3.0.0)", "huey (>=2.5.3,<3.0.0)",
"pandas>=1.5.0", "pandas>=1.5.0",
"openpyxl>=3.0.0", "openpyxl>=3.0.0",
@ -30,6 +31,7 @@ dependencies = [
"langchain-mcp-adapters (>=0.2.1,<0.3.0)", "langchain-mcp-adapters (>=0.2.1,<0.3.0)",
"langchain-openai (>=1.1.1,<2.0.0)", "langchain-openai (>=1.1.1,<2.0.0)",
"cachetools (>=6.2.4,<7.0.0)", "cachetools (>=6.2.4,<7.0.0)",
"langgraph-checkpoint-sqlite (>=3.0.1,<4.0.0)",
] ]
[tool.poetry.requires-plugins] [tool.poetry.requires-plugins]

View File

@ -2,6 +2,7 @@ aiofiles==25.1.0 ; python_version >= "3.12" and python_version < "4.0"
aiohappyeyeballs==2.6.1 ; python_version >= "3.12" and python_version < "4.0" aiohappyeyeballs==2.6.1 ; python_version >= "3.12" and python_version < "4.0"
aiohttp==3.13.1 ; python_version >= "3.12" and python_version < "4.0" aiohttp==3.13.1 ; python_version >= "3.12" and python_version < "4.0"
aiosignal==1.4.0 ; python_version >= "3.12" and python_version < "4.0" aiosignal==1.4.0 ; python_version >= "3.12" and python_version < "4.0"
aiosqlite==0.21.0 ; python_version >= "3.12" and python_version < "4.0"
annotated-types==0.7.0 ; python_version >= "3.12" and python_version < "4.0" annotated-types==0.7.0 ; python_version >= "3.12" and python_version < "4.0"
anthropic==0.75.0 ; python_version >= "3.12" and python_version < "4.0" anthropic==0.75.0 ; python_version >= "3.12" and python_version < "4.0"
anyio==4.11.0 ; python_version >= "3.12" and python_version < "4.0" anyio==4.11.0 ; python_version >= "3.12" and python_version < "4.0"
@ -41,6 +42,7 @@ langchain-core==1.1.3 ; python_version >= "3.12" and python_version < "4.0"
langchain-mcp-adapters==0.2.1 ; python_version >= "3.12" and python_version < "4.0" langchain-mcp-adapters==0.2.1 ; python_version >= "3.12" and python_version < "4.0"
langchain-openai==1.1.1 ; python_version >= "3.12" and python_version < "4.0" langchain-openai==1.1.1 ; python_version >= "3.12" and python_version < "4.0"
langchain==1.1.3 ; python_version >= "3.12" and python_version < "4.0" langchain==1.1.3 ; python_version >= "3.12" and python_version < "4.0"
langgraph-checkpoint-sqlite==3.0.1 ; python_version >= "3.12" and python_version < "4.0"
langgraph-checkpoint==3.0.1 ; python_version >= "3.12" and python_version < "4.0" langgraph-checkpoint==3.0.1 ; python_version >= "3.12" and python_version < "4.0"
langgraph-prebuilt==1.0.5 ; python_version >= "3.12" and python_version < "4.0" langgraph-prebuilt==1.0.5 ; python_version >= "3.12" and python_version < "4.0"
langgraph-sdk==0.2.15 ; python_version >= "3.12" and python_version < "4.0" langgraph-sdk==0.2.15 ; python_version >= "3.12" and python_version < "4.0"
@ -93,6 +95,7 @@ scipy==1.16.2 ; python_version >= "3.12" and python_version < "4.0"
sentence-transformers==5.1.1 ; python_version >= "3.12" and python_version < "4.0" sentence-transformers==5.1.1 ; python_version >= "3.12" and python_version < "4.0"
six==1.17.0 ; python_version >= "3.12" and python_version < "4.0" six==1.17.0 ; python_version >= "3.12" and python_version < "4.0"
sniffio==1.3.1 ; python_version >= "3.12" and python_version < "4.0" sniffio==1.3.1 ; python_version >= "3.12" and python_version < "4.0"
sqlite-vec==0.1.6 ; python_version >= "3.12" and python_version < "4.0"
sse-starlette==3.0.2 ; python_version >= "3.12" and python_version < "4.0" sse-starlette==3.0.2 ; python_version >= "3.12" and python_version < "4.0"
starlette==0.47.3 ; python_version >= "3.12" and python_version < "4.0" starlette==0.47.3 ; python_version >= "3.12" and python_version < "4.0"
sympy==1.14.0 ; python_version >= "3.12" and python_version < "4.0" sympy==1.14.0 ; python_version >= "3.12" and python_version < "4.0"