47 lines
1.3 KiB
Python
47 lines
1.3 KiB
Python
|
|
"""
|
|||
|
|
Agent Service 配置模块 - 配置构建和解析
|
|||
|
|
"""
|
|||
|
|
|
|||
|
|
from typing import Dict, Any, Tuple, Optional
|
|||
|
|
from langchain_core.messages import HumanMessage
|
|||
|
|
from backend.app.logger import warning
|
|||
|
|
|
|||
|
|
|
|||
|
|
class ServiceConfig:
|
|||
|
|
"""配置构建器"""
|
|||
|
|
|
|||
|
|
def __init__(self, chat_services: dict):
|
|||
|
|
self.chat_services = chat_services
|
|||
|
|
|
|||
|
|
def resolve_model(self, model: Optional[str]) -> str:
|
|||
|
|
"""
|
|||
|
|
解析并验证模型名称,不可用时回退到第一个可用模型
|
|||
|
|
"""
|
|||
|
|
if not model or model not in self.chat_services:
|
|||
|
|
fallback = next(iter(self.chat_services.keys()))
|
|||
|
|
warning(f"模型 '{model}' 不可用,回退到 '{fallback}'")
|
|||
|
|
return fallback
|
|||
|
|
return model
|
|||
|
|
|
|||
|
|
def build_invocation(
|
|||
|
|
self, message: str, thread_id: str, model: str, user_id: str
|
|||
|
|
) -> Tuple[Dict[str, Any], Dict[str, Any]]:
|
|||
|
|
"""
|
|||
|
|
构建图调用所需的 config 和 input_state
|
|||
|
|
|
|||
|
|
Returns:
|
|||
|
|
(config, input_state) 元组
|
|||
|
|
"""
|
|||
|
|
config = {
|
|||
|
|
"configurable": {
|
|||
|
|
"thread_id": thread_id,
|
|||
|
|
},
|
|||
|
|
"metadata": {"user_id": user_id}
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
input_state = {
|
|||
|
|
"messages": [HumanMessage(content=message)],
|
|||
|
|
"user_id": user_id,
|
|||
|
|
}
|
|||
|
|
return config, input_state
|