- src/backends/langgraph.py: LangGraphBackend implements ChatBackend; complete() runs one graph turn per session (thread_id=sessionId) and returns state-prefixed content; stream() emits the full state prefix before body fragments (full-answer slicing, not yet real incremental output - assignment 8) - src/core/config.py: AGENT_BACKEND=fastgpt|langgraph, langgraph mode no longer requires FastGPT credentials at startup - src/core/fastgpt_client.py: lifespan builds the backend by switch - test/backends/test_langgraph_backend.py: adapter acceptance tests Verified: - pytest: 48 passed - AGENT_BACKEND=langgraph uvicorn: POST /chat -> 1002; browser_addon -> browser_addon.1002; SSE = stage_code, text_delta, single done Known limit: stream slices a complete answer; no formUpdate deltas yet (assignment 6).
41 lines
1.6 KiB
Python
41 lines
1.6 KiB
Python
import os
|
||
from dotenv import load_dotenv
|
||
|
||
load_dotenv()
|
||
|
||
class Config:
|
||
PROJECT_NAME = "Flexible Employment Analysis API"
|
||
API_V1_STR = "/api/v1"
|
||
SECRET_KEY = os.getenv("SECRET_KEY", "your-secret-key")
|
||
DATABASE_URL = os.getenv("DATABASE_URL", "sqlite:///./test.db")
|
||
DEBUG = os.getenv("DEBUG", "false").lower() in ("true", "1", "t")
|
||
|
||
# FastGPT Configuration
|
||
FASTGPT_API_KEY = os.getenv("ANALYSIS_AUTH_TOKEN")
|
||
FASTGPT_BASE_URL = os.getenv("ANALYSIS_SERVICE_URL")
|
||
FASTGPT_APP_ID = os.getenv("APP_ID")
|
||
|
||
# Chat backend switch(作业 2)
|
||
# "fastgpt" 保持现有行为;"langgraph" 使用内存图 fake 回复。
|
||
AGENT_BACKEND = os.getenv("AGENT_BACKEND", "fastgpt")
|
||
|
||
@classmethod
|
||
def validate(cls):
|
||
"""Validate required configuration."""
|
||
# TODO(作业 2): 语音管线仍依赖 FastGPT。当前仅按文本后端开关校验;
|
||
# 接入语音开关后,需要单独校验语音路径所需配置。
|
||
if cls.AGENT_BACKEND not in ("fastgpt", "langgraph"):
|
||
raise ValueError(
|
||
f"AGENT_BACKEND must be 'fastgpt' or 'langgraph', got {cls.AGENT_BACKEND!r}"
|
||
)
|
||
if cls.AGENT_BACKEND != "fastgpt":
|
||
return
|
||
required = [
|
||
("FASTGPT_BASE_URL (ANALYSIS_SERVICE_URL)", cls.FASTGPT_BASE_URL),
|
||
("FASTGPT_API_KEY (ANALYSIS_AUTH_TOKEN)", cls.FASTGPT_API_KEY),
|
||
("FASTGPT_APP_ID (APP_ID)", cls.FASTGPT_APP_ID),
|
||
]
|
||
missing = [name for name, value in required if not value]
|
||
if missing:
|
||
raise ValueError(f"Missing required environment variables: {', '.join(missing)}")
|