anthropics agent
-
https://zhuanlan.zhihu.com/p/32454721762
-
code:https://github.com/anthropics/anthropic-quickstarts/blob/main/computer-use-demo/computer_use_demo/loop.py
sampling_loop函数
每次进行循环,输出extract tool_use,如果存在继续循环,不存在则输出final result
model本轮response_params为assistant, tool的execute response为 user,放入history开始下一轮。
smolagents
主要看下 code Agent
采用了memory来存储ReAct的信息
- plan
- action
- observation
code:src/smolagents/agents.py
主要深挖:_run 函数
def _run(
self, task: str, max_steps: int, images: List["PIL.Image.Image"] | None = None
) -> Generator[ActionStep | AgentType, None, None]:
final_answer = None
self.step_number = 1
while final_answer is None and self.step_number <= max_steps:
step_start_time = time.time()
memory_step = self._create_memory_step(step_start_time, images)
try:
final_answer = self._execute_step(task, memory_step)
except AgentGenerationError as e:
# Agent generation errors are not caused by a Model error but an implementation error: so we should raise them and exit.
raise e
except AgentError as e:
# Other AgentError types are caused by the Model, so we should log them and iterate.
memory_step.error = e
finally:
self._finalize_step(memory_step, step_start_time)
yield memory_step
self.step_number += 1
if final_answer is None and self.step_number == max_steps + 1:
final_answer = self._handle_max_steps_reached(task, images, step_start_time)
yield memory_step
yield handle_agent_output_types(final_answer)
大模型每次都会根据目前memory的信息进行plan,得到response,从中提取code action,执行code execute 核心逻辑,如果返回final result则结束,否则继续下一步的 plan + action
openai-agents-python
https://github.com/openai/openai-agents-python
这个loop部分就纯粹用function call,具体的loop写法可以参考openai官方的:
- https://platform.openai.com/docs/guides/function-calling?api-mode=chat
主要分为 5步
- 定义tool,构建agent,这部分现在基本上格式都是遵循openai的格式,没啥可说的
- 解析completion.choices[0].message.tool_calls这里面的tool信息
- 执行tool
- 这步需要注意,通过tool call里面的tool id和tool执行的结果进行关联
messages.append(completion.choices[0].message) # append model's function call message
messages.append({ # append result message
"role": "tool",
"tool_call_id": tool_call.id,
"content": str(result)
})
- 直接把messages都送给大模型,得到最终结果
总结
主要有以下几种范式
- tool里面存在stop 相关的 tool,如果输出stop tool则stop,输出final ouput
- 和1相似, 在loop中存在一个固定的类似于意图识别的模块,决定agent是否结束
- 直接采用function call的方式,输入也直接将tool的description的合集输入到tools中,如果输出completion.choices[0].message.tool_calls为空,则结束loop(这个模式是现在的主流模式,主要是要模型强,强的情况下就不需要其他操作了, 最普通的也最实用)
- 将function call的信息直接写在final prompt里面,输出来parser output中特定的格式,如果存在tool则继续loop,如果不存在tool则结束loop