_call_agent_v3
This commit is contained in:
parent
b4cf5face0
commit
80559bdd7d
@ -138,7 +138,7 @@ class ScheduleExecutor:
|
|||||||
logger.info(f"Executing scheduled task: {task_id} ({task.get('name', '')}) for bot={bot_id} user={user_id}")
|
logger.info(f"Executing scheduled task: {task_id} ({task.get('name', '')}) for bot={bot_id} user={user_id}")
|
||||||
|
|
||||||
# 调用 agent
|
# 调用 agent
|
||||||
response_text = await self._call_agent_v2(bot_id, user_id, task)
|
response_text = await self._call_agent_v3(bot_id, user_id, task)
|
||||||
|
|
||||||
# 写入日志
|
# 写入日志
|
||||||
duration_ms = int((time.time() - start_time) * 1000)
|
duration_ms = int((time.time() - start_time) * 1000)
|
||||||
@ -188,6 +188,31 @@ class ScheduleExecutor:
|
|||||||
|
|
||||||
return data["choices"][0]["message"]["content"]
|
return data["choices"][0]["message"]["content"]
|
||||||
|
|
||||||
|
async def _call_agent_v3(self, bot_id: str, user_id: str, task: dict) -> str:
|
||||||
|
"""通过 HTTP 调用 /api/v3/chat/completions 接口(从数据库读取配置)"""
|
||||||
|
url = "http://127.0.0.1:8001/api/v3/chat/completions"
|
||||||
|
|
||||||
|
payload = {
|
||||||
|
"messages": [{"role": "user", "content": task["message"]}],
|
||||||
|
"stream": False,
|
||||||
|
"bot_id": bot_id,
|
||||||
|
"session_id": f"schedule_{task['id']}",
|
||||||
|
"user_identifier": user_id,
|
||||||
|
}
|
||||||
|
|
||||||
|
headers = {
|
||||||
|
"Content-Type": "application/json",
|
||||||
|
}
|
||||||
|
|
||||||
|
async with aiohttp.ClientSession() as session:
|
||||||
|
async with session.post(url, json=payload, headers=headers, timeout=aiohttp.ClientTimeout(total=300)) as resp:
|
||||||
|
if resp.status != 200:
|
||||||
|
body = await resp.text()
|
||||||
|
raise RuntimeError(f"API returned {resp.status}: {body}")
|
||||||
|
data = await resp.json()
|
||||||
|
|
||||||
|
return data["choices"][0]["message"]["content"]
|
||||||
|
|
||||||
def _update_task_after_execution(self, task_id: str, tasks_file: Path):
|
def _update_task_after_execution(self, task_id: str, tasks_file: Path):
|
||||||
"""执行后更新 tasks.yaml"""
|
"""执行后更新 tasks.yaml"""
|
||||||
try:
|
try:
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user