def __init__(self,
llm,
searcher_cfg,
protocol=MindSearchProtocol(),
max_turn=10):
self.local_dict = {}
self.ptr = 0
self.llm = llm
self.max_turn = max_turn
WebSearchGraph.searcher_cfg = searcher_cfg
super().__init__(llm=llm, action_executor=None, protocol=protocol)
def stream_chat(self, message, **kwargs):
if isinstance(message, str):
message = [{'role': 'user', 'content': message}]
elif isinstance(message, dict):
message = [message]
as_dict = kwargs.pop('as_dict', False)
return_early = kwargs.pop('return_early', False)
self.local_dict.clear()
self.ptr = 0
inner_history = message[:]
agent_return = AgentReturn()
agent_return.type = 'planner'
agent_return.nodes = {}
agent_return.adjacency_list = {}
agent_return.inner_steps = deepcopy(inner_history)
for _ in range(self.max_turn):
prompt = self._protocol.format(inner_step=inner_history)
code = None
for model_state, response, _ in self.llm.stream_chat(
prompt, session_id=random.randint(0, 999999), **kwargs):
if model_state.value < 0:
agent_return.state = getattr(AgentStatusCode,
model_state.name)
yield deepcopy(agent_return)
return
response = response.replace('<|plugin|>', '<|interpreter|>')
_, language, action = self._protocol.parse(response)
if not language and not action:
continue
code = action['parameters']['command'] if action else ''
agent_return.state = self._determine_agent_state(
model_state, code, agent_return)
agent_return.response = language if not code else code
# if agent_return.state == AgentStatusCode.STREAM_ING:
yield deepcopy(agent_return)
inner_history.append({'role': 'language', 'content': language})
print(colored(response, 'blue'))
if code:
yield from self._process_code(agent_return, inner_history,
code, as_dict, return_early)
else:
agent_return.state = AgentStatusCode.END
yield deepcopy(agent_return)
return
agent_return.state = AgentStatusCode.END
yield deepcopy(agent_return)
def _determine_agent_state(self, model_state, code, agent_return):
if code:
return (AgentStatusCode.PLUGIN_START if model_state
== ModelStatusCode.END else AgentStatusCode.PLUGIN_START)
return (AgentStatusCode.ANSWER_ING
if agent_return.nodes and 'response' in agent_return.nodes else
AgentStatusCode.STREAM_ING)
def _process_code(self,
agent_return,
inner_history,
code,
as_dict=False,
return_early=False):
for node_name, node, adj in self.execute_code(
code, return_early=return_early):
if as_dict and 'detail' in node:
node['detail'] = asdict(node['detail'])
if not adj:
agent_return.nodes[node_name] = node
else:
agent_return.adjacency_list[node_name] = adj
# state 1进行中,2未开始,3已结束
for start_node, neighbors in agent_return.adjacency_list.items():
for neighbor in neighbors:
if neighbor['name'] not in agent_return.nodes:
state = 2
elif 'detail' not in agent_return.nodes[neighbor['name']]:
state = 2
elif agent_return.nodes[neighbor['name']][
'detail'].state == AgentStatusCode.END:
state = 3
else:
state = 1
neighbor['state'] = state
if not adj:
yield deepcopy((agent_return, node_name))
reference, references_url = self._generate_reference(
agent_return, code, as_dict)
inner_history.append({
'role': 'tool',
'content': code,
'name': 'plugin'
})
inner_history.append({
'role': 'environment',
'content': reference,
'name': 'plugin'
})
agent_return.inner_steps = deepcopy(inner_history)
agent_return.state = AgentStatusCode.PLUGIN_RETURN
agent_return.references.update(references_url)
yield deepcopy(agent_return)
def _generate_reference(self, agent_return, code, as_dict):
node_list = [
node.strip().strip('\"') for node in re.findall(
r'graph\.node\("((?:[^"\\]|\\.)*?)"\)', code)
]
if 'add_response_node' in code:
return self._protocol.response_prompt, dict()
references = []
references_url = dict()
for node_name in node_list:
ref_results = None
ref2url = None
if as_dict:
actions = agent_return.nodes[node_name]['detail']['actions']
else:
#actions = agent_return.nodes[node_name]['detail'].actions
node_details = agent_return.nodes.get(node_name, {}).get('detail', {})
actions = node_details.get('actions', None)
if actions is None:
# 如果 actions 为空,进行适当的处理,比如日志记录或其他错误处理
print(f"警告: 节点 '{node_name}' 的 actions 未找到!")
if actions:
ref_results = actions[0]['result'][0][
'content'] if as_dict else actions[0].result[0]['content']
if ref_results:
ref_results = json.loads(ref_results)
ref2url = {
idx: item['url']
for idx, item in ref_results.items()
}
node_details = agent_return.nodes.get('根', {})
response = node_details.get('response') or node_details.get('actions', None)
# 如果没有找到 'response' 或 '响应',则记录日志并使用默认响应
if response is None:
logging.error("节点 '根' 的 'response' 或 '响应' 未找到!")
response = "默认响应"
ref = f"## {node_name}\n\n{response}\n"
updated_ref = re.sub(
r'\[\[(\d+)\]\]',
lambda match: f'[[{int(match.group(1)) + self.ptr}]]', ref)
numbers = [int(n) for n in re.findall(r'\[\[(\d+)\]\]', ref)]
if numbers:
try:
assert all(str(elem) in ref2url for elem in numbers)
except Exception as exc:
logger.info(f'Illegal reference id: {str(exc)}')
if ref2url:
references_url.update({
str(idx + self.ptr): ref2url[str(idx)]
for idx in set(numbers) if str(idx) in ref2url
})
self.ptr += max(numbers) + 1
references.append(updated_ref)
return '\n'.join(references), references_url
def execute_code(self, command: str, return_early=False):
def extract_code(text: str) -> str:
text = re.sub(r'from ([\w.]+) import WebSearchGraph', '', text)
triple_match = re.search(r'```[^\n]*\n(.+?)```', text, re.DOTALL)
single_match = re.search(r'`([^`]*)`', text, re.DOTALL)
if triple_match:
return triple_match.group(1)
elif single_match:
return single_match.group(1)
return text
def run_command(cmd):
try:
exec(cmd, globals(), self.local_dict)
plan_graph = self.local_dict.get('graph')
assert plan_graph is not None
for future in as_completed(plan_graph.future_to_query):
future.result()
plan_graph.future_to_query.clear()
plan_graph.searcher_resp_queue.put(plan_graph.end_signal)
except Exception as e:
logger.exception(f'Error executing code: {e}')
raise
command = extract_code(command)
producer_thread = threading.Thread(target=run_command,
args=(command, ))
producer_thread.start()
responses = defaultdict(list)
ordered_nodes = []
active_node = None
while True:
try:
item = self.local_dict.get('graph').searcher_resp_queue.get(
timeout=60)
if item is WebSearchGraph.end_signal:
for node_name in ordered_nodes:
# resp = None
for resp in responses[node_name]:
yield deepcopy(resp)
# if resp:
# assert resp[1][
# 'detail'].state == AgentStatusCode.END
break
node_name, node, adj = item
if node_name in ['root', 'response']:
yield deepcopy((node_name, node, adj))
else:
if node_name not in ordered_nodes:
ordered_nodes.append(node_name)
responses[node_name].append((node_name, node, adj))
if not active_node and ordered_nodes:
active_node = ordered_nodes[0]
while active_node and responses[active_node]:
if return_early:
if 'detail' in responses[active_node][-1][
1] and responses[active_node][-1][1][
'detail'].state == AgentStatusCode.END:
item = responses[active_node][-1]
else:
item = responses[active_node].pop(0)
else:
item = responses[active_node].pop(0)
if 'detail' in item[1] and item[1][
'detail'].state == AgentStatusCode.END:
ordered_nodes.pop(0)
responses[active_node].clear()
active_node = None
yield deepcopy(item)
except queue.Empty:
if not producer_thread.is_alive():
break
producer_thread.join()
return 修改