feat: improve agent collaboration observability
This commit is contained in:
@@ -29,6 +29,38 @@ class LLMService:
|
||||
def __init__(self, db: Optional[AsyncSession] = None):
|
||||
self._provider_cache: Dict[str, BaseLLMProvider] = {}
|
||||
self._db = db
|
||||
|
||||
@staticmethod
|
||||
def _is_missing_model_id(model_id: Optional[str]) -> bool:
|
||||
if model_id is None:
|
||||
return True
|
||||
return str(model_id).strip().lower() in {"", "none", "null", "undefined"}
|
||||
|
||||
async def resolve_chat_model_id(self, model_id: Optional[str]) -> str:
|
||||
"""解析 chat 模型。未指定时使用当前启用的默认 chat 模型。"""
|
||||
if not self._is_missing_model_id(model_id):
|
||||
return str(model_id)
|
||||
|
||||
if not self._db:
|
||||
raise ValueError("未找到可用的 chat 模型,请先在模型配置中启用一个模型")
|
||||
|
||||
from ai_platform.models import LLMModel
|
||||
|
||||
result = await self._db.execute(
|
||||
select(LLMModel)
|
||||
.where(
|
||||
LLMModel.is_deleted == False,
|
||||
LLMModel.is_active == True,
|
||||
LLMModel.model_type == "chat",
|
||||
)
|
||||
.order_by(LLMModel.sort.desc(), LLMModel.sys_create_datetime.desc())
|
||||
)
|
||||
model = result.scalars().first()
|
||||
if not model:
|
||||
raise ValueError("未找到可用的 chat 模型,请先在模型配置中启用一个模型")
|
||||
|
||||
logger.info("No model_id supplied, fallback to default chat model %s", model.id)
|
||||
return str(model.id)
|
||||
|
||||
async def _get_provider_async(self, model_id: str) -> tuple:
|
||||
"""
|
||||
@@ -45,6 +77,8 @@ class LLMService:
|
||||
if not self._db:
|
||||
raise ValueError("数据库会话未初始化")
|
||||
|
||||
model_id = await self.resolve_chat_model_id(model_id)
|
||||
|
||||
# 查询模型
|
||||
result = await self._db.execute(
|
||||
select(LLMModel).where(
|
||||
@@ -319,14 +353,18 @@ class LLMService:
|
||||
async def get_provider():
|
||||
return await self._get_provider_async(model_id)
|
||||
|
||||
loop = asyncio.get_event_loop()
|
||||
if loop.is_running():
|
||||
# 如果已有事件循环在运行,使用线程池
|
||||
with concurrent.futures.ThreadPoolExecutor() as executor:
|
||||
future = executor.submit(asyncio.run, get_provider())
|
||||
provider, model_name = future.result()
|
||||
try:
|
||||
loop = asyncio.get_event_loop()
|
||||
except RuntimeError:
|
||||
provider, model_name = asyncio.run(get_provider())
|
||||
else:
|
||||
provider, model_name = loop.run_until_complete(get_provider())
|
||||
if loop.is_running():
|
||||
# 如果已有事件循环在运行,使用线程池
|
||||
with concurrent.futures.ThreadPoolExecutor() as executor:
|
||||
future = executor.submit(asyncio.run, get_provider())
|
||||
provider, model_name = future.result()
|
||||
else:
|
||||
provider, model_name = loop.run_until_complete(get_provider())
|
||||
|
||||
# 使用获取到的 provider 进行流式调用
|
||||
yield from self.chat_stream_sync_with_provider(
|
||||
|
||||
Reference in New Issue
Block a user