临时修改:用内存 checkpoint 测试后端
All checks were successful
构建并部署 AI Agent 服务 / deploy (push) Successful in 6m22s

This commit is contained in:
2026-04-29 19:20:34 +08:00
parent 81f48c8467
commit 8bc5f3cbb5
2 changed files with 36 additions and 49 deletions

View File

@@ -4,7 +4,8 @@ FastAPI 后端 - 支持动态模型切换,使用 PostgreSQL 持久化记忆
""" """
import os import os
from app.config import DB_URI, BACKEND_PORT # from app.config import DB_URI, BACKEND_PORT
from app.config import BACKEND_PORT
import uuid import uuid
import json import json
from contextlib import asynccontextmanager from contextlib import asynccontextmanager
@@ -14,7 +15,7 @@ from fastapi import FastAPI, HTTPException, WebSocket, WebSocketDisconnect, Depe
from fastapi.middleware.cors import CORSMiddleware from fastapi.middleware.cors import CORSMiddleware
from fastapi.responses import StreamingResponse from fastapi.responses import StreamingResponse
from pydantic import BaseModel from pydantic import BaseModel
from langgraph.checkpoint.postgres.aio import AsyncPostgresSaver from langgraph.checkpoint.memory import MemorySaver
from .agent.service import AIAgentService from .agent.service import AIAgentService
from .agent.history import ThreadHistoryService from .agent.history import ThreadHistoryService
from app.core.human_review import ( from app.core.human_review import (
@@ -26,56 +27,42 @@ from app.core.human_review import (
from app.subgraphs.contact.api_client import ContactAPIClient from app.subgraphs.contact.api_client import ContactAPIClient
from app.subgraphs.dictionary.api_client import DictionaryAPIClient from app.subgraphs.dictionary.api_client import DictionaryAPIClient
from app.subgraphs.news_analysis.api_client import NewsAPIClient from app.subgraphs.news_analysis.api_client import NewsAPIClient
from .db.init_db import init_subgraph_tables # from .db.init_db import init_subgraph_tables
from .db.models import ContactRepository, DictionaryRepository, NewsRepository # from .db.models import ContactRepository, DictionaryRepository, NewsRepository
from app.logger import info, error from app.logger import info, error
@asynccontextmanager @asynccontextmanager
async def lifespan(app: FastAPI): async def lifespan(app: FastAPI):
"""应用生命周期管理:创建并注入全局服务""" """应用生命周期管理:创建并注入全局服务(临时用内存 checkpoint"""
# 1. 创建数据库连接池并初始化表(仅 checkpointer # 1. 创建内存 checkpointer(临时测试
async with AsyncPostgresSaver.from_conn_string(DB_URI) as checkpointer: checkpointer = MemorySaver()
await checkpointer.setup()
# 1.5 初始化子图表 # 2. 构建 AI Agent 服务
await init_subgraph_tables(checkpointer.conn) agent_service = AIAgentService(checkpointer)
await agent_service.initialize()
# 2. 构建 AI Agent 服务 # 3. 创建历史查询服务(保持原有的 checkpointer 参数)
agent_service = AIAgentService(checkpointer) history_service = ThreadHistoryService(checkpointer)
await agent_service.initialize()
# 3. 创建历史查询服务(保持原有的 checkpointer 参数) # 4. 创建审核管理器
history_service = ThreadHistoryService(checkpointer) review_manager = ReviewManager(InMemoryReviewStore())
# 3.5 创建子图 Repositories # 5. 将服务实例存入 app.state
contact_repo = ContactRepository(checkpointer.conn) app.state.agent_service = agent_service
dictionary_repo = DictionaryRepository(checkpointer.conn) app.state.history_service = history_service
news_repo = NewsRepository(checkpointer.conn) app.state.review_manager = review_manager
app.state.contact_api = ContactAPIClient()
app.state.dictionary_api = DictionaryAPIClient()
app.state.news_api = NewsAPIClient()
app.state.contact_repo = None
app.state.dictionary_repo = None
app.state.news_repo = None
# 3.6 创建子图 API 客户端(真实数据库模式) # 应用运行中...
contact_api = ContactAPIClient(checkpointer.conn) yield
dictionary_api = DictionaryAPIClient(word_repository=dictionary_repo)
news_api = NewsAPIClient(news_repository=news_repo)
# 4. 创建审核管理器 # 6. 关闭时清理
review_manager = ReviewManager(InMemoryReviewStore()) info("🛑 应用关闭")
# 5. 将服务实例存入 app.state
app.state.agent_service = agent_service
app.state.history_service = history_service
app.state.review_manager = review_manager
app.state.contact_api = contact_api
app.state.dictionary_api = dictionary_api
app.state.news_api = news_api
app.state.contact_repo = contact_repo
app.state.dictionary_repo = dictionary_repo
app.state.news_repo = news_repo
# 应用运行中...
yield
# 6. 关闭时自动清理数据库连接async with 负责)
info("🛑 应用关闭,数据库连接池已释放")
app = FastAPI(lifespan=lifespan) app = FastAPI(lifespan=lifespan)

View File

@@ -15,7 +15,7 @@ services:
# ========================================================================= # =========================================================================
# PostgreSQL 数据库配置 # PostgreSQL 数据库配置
# ========================================================================= # =========================================================================
- DB_HOST=115.190.121.151 - DB_HOST=ai-postgres
- DB_PORT=5432 - DB_PORT=5432
- DB_USER=postgres - DB_USER=postgres
- DB_PASSWORD=${DB_PASSWORD:?请配置 DB_PASSWORD本地.env 文件 | CI/CDSecrets} # ⭐ 敏感密钥配置 - DB_PASSWORD=${DB_PASSWORD:?请配置 DB_PASSWORD本地.env 文件 | CI/CDSecrets} # ⭐ 敏感密钥配置