- 添加多 Agent 图协作框架 (graph, supervisor, workers) - 添加迭代器和集成模块 - 添加多 Agent 规划文档 Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
71 lines
2.0 KiB
Python
71 lines
2.0 KiB
Python
"""
|
|
Research Worker - 信息搜索和调研
|
|
"""
|
|
import json
|
|
from langchain_core.language_models import BaseChatModel
|
|
|
|
from .base import BaseWorker
|
|
from ..types import TaskItem
|
|
from ..prompts import RESEARCH_SYSTEM_PROMPT
|
|
|
|
|
|
class ResearchWorker(BaseWorker):
|
|
"""Research Worker - 信息搜索和调研"""
|
|
|
|
def __init__(
|
|
self,
|
|
llm: BaseChatModel,
|
|
tool_registry=None,
|
|
tools: list = None
|
|
):
|
|
super().__init__(
|
|
llm=llm,
|
|
name="research",
|
|
system_prompt=RESEARCH_SYSTEM_PROMPT,
|
|
tools=tools or [],
|
|
tool_registry=tool_registry
|
|
)
|
|
|
|
async def execute(self, task: TaskItem, context: dict) -> dict:
|
|
"""执行调研任务"""
|
|
|
|
# 构建消息
|
|
messages = self._build_messages(task.description, context)
|
|
|
|
try:
|
|
# 调用 LLM
|
|
response = await self.llm.ainvoke(messages)
|
|
|
|
content = response.content if hasattr(response, 'content') else str(response)
|
|
|
|
# 尝试提取搜索结果
|
|
search_results = self._extract_search_results(content)
|
|
|
|
return {
|
|
"success": True,
|
|
"content": content,
|
|
"context": {
|
|
"research_results": search_results,
|
|
"last_research_by": self.name
|
|
}
|
|
}
|
|
|
|
except Exception as e:
|
|
return {
|
|
"success": False,
|
|
"content": "",
|
|
"error": str(e),
|
|
"context": {}
|
|
}
|
|
|
|
def _extract_search_results(self, content: str) -> list:
|
|
"""从内容中提取搜索结果"""
|
|
# 简单实现:查找以 - 或 * 开头的行
|
|
results = []
|
|
for line in content.split('\n'):
|
|
line = line.strip()
|
|
if line.startswith(('- ', '* ', '1. ', '2. ', '3. ')):
|
|
results.append(line.lstrip('-*123. '))
|
|
|
|
return results[:10] # 限制数量
|