Enhance LLM integration by adding OpenAI client support and enabling dynamic routing between DeepSeek and OpenAI services. Update environment configuration to include LLM_SERVICE_CODE for service selection, and modify API server to accommodate new request parameters for language and model. Implement streaming response improvements for chat interactions, allowing for segmented SSE output. Update documentation and impact analysis to reflect these changes.
This commit is contained in:
+42
-21
@@ -108,16 +108,27 @@ def setup_environment():
|
||||
logger.info("请将G3SB Schema文件放置在 ./data/schemas/ 目录")
|
||||
return False
|
||||
|
||||
# 检查API Key
|
||||
api_key = os.getenv("DEEPSEEK_API_KEY", "").strip()
|
||||
if not api_key:
|
||||
logger.warning("环境变量 DEEPSEEK_API_KEY 未设置")
|
||||
logger.info("请在 .env 文件中配置,或 export DEEPSEEK_API_KEY=your_key")
|
||||
# 检查 LLM Key(DeepSeek / OpenAI 可切换)
|
||||
llm_sc = (os.getenv("LLM_SERVICE_CODE") or "").strip().lower()
|
||||
if llm_sc and llm_sc not in ("deepseek", "openai"):
|
||||
logger.warning("未知 LLM_SERVICE_CODE=%r(仅支持 deepseek/openai)", llm_sc)
|
||||
return False
|
||||
if llm_sc == "openai":
|
||||
if not (os.getenv("OPENAI_API_KEY") or "").strip():
|
||||
logger.warning("LLM_SERVICE_CODE=openai 但 OPENAI_API_KEY 未设置")
|
||||
return False
|
||||
else:
|
||||
if not (os.getenv("DEEPSEEK_API_KEY") or "").strip():
|
||||
logger.warning("环境变量 DEEPSEEK_API_KEY 未设置(默认 LLM_SERVICE_CODE=deepseek)")
|
||||
logger.info("请在 .env 文件中配置,或 export DEEPSEEK_API_KEY=your_key")
|
||||
return False
|
||||
|
||||
logger.info(f"[OK] 环境检查通过")
|
||||
logger.info(f" - Schema: {schema_path}")
|
||||
logger.info(f" - API Key: {'已配置' if api_key else '未配置'}")
|
||||
logger.info(
|
||||
" - LLM: %s",
|
||||
(llm_sc or "deepseek(auto)"),
|
||||
)
|
||||
|
||||
return True
|
||||
|
||||
@@ -160,6 +171,7 @@ def load_schema(schema_path: str, schema_meta_path: Optional[str] = None):
|
||||
def create_orchestrator(schema_mgr, args):
|
||||
"""创建编排器"""
|
||||
from agents.orchestrator import Text2SQLOrchestrator
|
||||
from llm.router import create_llm_client, resolve_llm_service_code
|
||||
from llm.deepseek_client import DeepSeekConfig
|
||||
|
||||
translate_en = os.getenv("TRANSLATE_EN_TO_ZH", "true").strip().lower() not in (
|
||||
@@ -171,25 +183,34 @@ def create_orchestrator(schema_mgr, args):
|
||||
if getattr(args, "no_translate_en", False):
|
||||
translate_en = False
|
||||
|
||||
api_key = (args.api_key or os.getenv("DEEPSEEK_API_KEY") or "").strip()
|
||||
if not api_key:
|
||||
raise ValueError(
|
||||
"未配置 DeepSeek API Key:请在 .env 中设置 DEEPSEEK_API_KEY,"
|
||||
"或使用命令行参数 --api-key"
|
||||
sc = resolve_llm_service_code()
|
||||
if sc == "deepseek":
|
||||
api_key = (args.api_key or os.getenv("DEEPSEEK_API_KEY") or "").strip()
|
||||
if not api_key:
|
||||
raise ValueError(
|
||||
"未配置 DeepSeek API Key:请在 .env 中设置 DEEPSEEK_API_KEY,"
|
||||
"或使用命令行参数 --api-key"
|
||||
)
|
||||
base_url = (os.getenv("DEEPSEEK_BASE_URL") or "https://api.deepseek.com").strip()
|
||||
cfg = DeepSeekConfig(
|
||||
api_key=api_key,
|
||||
base_url=base_url,
|
||||
model_name=args.model or "deepseek-chat",
|
||||
temperature=args.temperature,
|
||||
max_tokens=args.max_tokens,
|
||||
)
|
||||
llm_client = create_llm_client("deepseek", **cfg.__dict__)
|
||||
else:
|
||||
# openai:完全由 OPENAI_* 决定;同时沿用 temperature/max_tokens 作为默认值覆盖
|
||||
llm_client = create_llm_client(
|
||||
"openai",
|
||||
temperature=args.temperature,
|
||||
max_tokens=args.max_tokens,
|
||||
)
|
||||
base_url = (os.getenv("DEEPSEEK_BASE_URL") or "https://api.deepseek.com").strip()
|
||||
|
||||
config = DeepSeekConfig(
|
||||
api_key=api_key,
|
||||
base_url=base_url,
|
||||
model_name=args.model or "deepseek-chat",
|
||||
temperature=args.temperature,
|
||||
max_tokens=args.max_tokens,
|
||||
)
|
||||
|
||||
orchestrator = Text2SQLOrchestrator(
|
||||
schema_manager=schema_mgr,
|
||||
deepseek_config=config,
|
||||
llm_client=llm_client,
|
||||
vector_db_path=args.vector_db,
|
||||
max_retry=args.max_retry,
|
||||
use_vector_search=not args.no_vector_search,
|
||||
|
||||
Reference in New Issue
Block a user