临时修改:用内存 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,19 +27,15 @@ 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 初始化子图表
await init_subgraph_tables(checkpointer.conn)
# 2. 构建 AI Agent 服务 # 2. 构建 AI Agent 服务
agent_service = AIAgentService(checkpointer) agent_service = AIAgentService(checkpointer)
@@ -47,16 +44,6 @@ async def lifespan(app: FastAPI):
# 3. 创建历史查询服务(保持原有的 checkpointer 参数) # 3. 创建历史查询服务(保持原有的 checkpointer 参数)
history_service = ThreadHistoryService(checkpointer) history_service = ThreadHistoryService(checkpointer)
# 3.5 创建子图 Repositories
contact_repo = ContactRepository(checkpointer.conn)
dictionary_repo = DictionaryRepository(checkpointer.conn)
news_repo = NewsRepository(checkpointer.conn)
# 3.6 创建子图 API 客户端(真实数据库模式)
contact_api = ContactAPIClient(checkpointer.conn)
dictionary_api = DictionaryAPIClient(word_repository=dictionary_repo)
news_api = NewsAPIClient(news_repository=news_repo)
# 4. 创建审核管理器 # 4. 创建审核管理器
review_manager = ReviewManager(InMemoryReviewStore()) review_manager = ReviewManager(InMemoryReviewStore())
@@ -64,18 +51,18 @@ async def lifespan(app: FastAPI):
app.state.agent_service = agent_service app.state.agent_service = agent_service
app.state.history_service = history_service app.state.history_service = history_service
app.state.review_manager = review_manager app.state.review_manager = review_manager
app.state.contact_api = contact_api app.state.contact_api = ContactAPIClient()
app.state.dictionary_api = dictionary_api app.state.dictionary_api = DictionaryAPIClient()
app.state.news_api = news_api app.state.news_api = NewsAPIClient()
app.state.contact_repo = contact_repo app.state.contact_repo = None
app.state.dictionary_repo = dictionary_repo app.state.dictionary_repo = None
app.state.news_repo = news_repo app.state.news_repo = None
# 应用运行中... # 应用运行中...
yield yield
# 6. 关闭时自动清理数据库连接async with 负责) # 6. 关闭时清理
info("🛑 应用关闭,数据库连接池已释放") 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} # ⭐ 敏感密钥配置