smolagents 文档
📚 管理你的代理的记忆
加入 Hugging Face 社区
并获得增强的文档体验
开始使用
📚 管理你的代理的记忆
最终,一个代理可以由简单的组件定义:它有工具、提示。最重要的是,它有过去步骤的记忆,记录了规划、执行和错误的历程。
回放你的代理的记忆
我们提出了几个功能来检查过去的代理运行。
你可以对代理的运行进行检测,以在一个出色的用户界面中显示它,该界面允许你放大/缩小特定步骤,如检测指南中所强调的那样。
你也可以使用 agent.replay()
,如下所示
在代理运行之后
from smolagents import HfApiModel, CodeAgent
agent = CodeAgent(tools=[], model=HfApiModel(), verbosity_level=0)
result = agent.run("What's the 20th Fibonacci number?")
如果你想回放上次运行,只需使用
agent.replay()
动态更改代理的记忆
许多高级用例需要动态修改代理的记忆。
你可以使用以下方式访问代理的记忆
from smolagents import ActionStep
system_prompt_step = agent.memory.system_prompt
print("The system prompt given to the agent was:")
print(system_prompt_step.system_prompt)
task_step = agent.memory.steps[0]
print("\n\nThe first task step was:")
print(task_step.task)
for step in agent.memory.steps:
if isinstance(step, ActionStep):
if step.error is not None:
print(f"\nStep {step.step_number} got this error:\n{step.error}\n")
else:
print(f"\nStep {step.step_number} got these observations:\n{step.observations}\n")
使用 agent.memory.get_full_steps()
获取作为字典的完整步骤。
你也可以使用步骤回调来动态更改代理的记忆。
步骤回调可以在它们的参数中访问代理本身,因此它们可以访问如上所述的任何记忆步骤,并在需要时更改它。例如,假设你正在观察网页浏览器代理执行的每个步骤的屏幕截图。你想要记录最新的屏幕截图,并删除旧步骤的图像以节省令牌成本。
你可以运行如下代码。注意:此代码不完整,为了简洁起见,删除了一些导入和对象定义,请访问原始脚本以获取完整的可用代码。
import helium
from PIL import Image
from io import BytesIO
from time import sleep
def update_screenshot(memory_step: ActionStep, agent: CodeAgent) -> None:
sleep(1.0) # Let JavaScript animations happen before taking the screenshot
driver = helium.get_driver()
latest_step = memory_step.step_number
for previous_memory_step in agent.memory.steps: # Remove previous screenshots from logs for lean processing
if isinstance(previous_memory_step, ActionStep) and previous_memory_step.step_number <= latest_step - 2:
previous_memory_step.observations_images = None
png_bytes = driver.get_screenshot_as_png()
image = Image.open(BytesIO(png_bytes))
memory_step.observations_images = [image.copy()]
然后你应在初始化代理时在 step_callbacks
参数中传递此函数
CodeAgent(
tools=[DuckDuckGoSearchTool(), go_back, close_popups, search_item_ctrl_f],
model=model,
additional_authorized_imports=["helium"],
step_callbacks=[update_screenshot],
max_steps=20,
verbosity_level=2,
)
前往我们的视觉网页浏览器代码以查看完整的可用示例。
一次运行一个步骤的代理
如果你的工具调用需要数天时间,这将非常有用:你可以逐步运行你的代理。这也将允许你在每个步骤更新记忆。
from smolagents import HfApiModel, CodeAgent, ActionStep, TaskStep
agent = CodeAgent(tools=[], model=HfApiModel(), verbosity_level=1)
print(agent.memory.system_prompt)
task = "What is the 20th Fibonacci number?"
# You could modify the memory as needed here by inputting the memory of another agent.
# agent.memory.steps = previous_agent.memory.steps
# Let's start a new task!
agent.memory.steps.append(TaskStep(task=task, task_images=[]))
final_answer = None
step_number = 1
while final_answer is None and step_number <= 10:
memory_step = ActionStep(
step_number=step_number,
observations_images=[],
)
# Run one step.
final_answer = agent.step(memory_step)
agent.memory.steps.append(memory_step)
step_number += 1
# Change the memory as you please!
# For instance to update the latest step:
# agent.memory.steps[-1] = ...
print("The final answer is:", final_answer)