smolagents 文件

📚 管理智慧體的記憶體

Hugging Face's logo
加入 Hugging Face 社群

並獲得增強的文件體驗

開始使用

📚 管理智慧體的記憶體

歸根結底,智慧體可以由簡單的元件定義:它有工具、提示詞。最重要的是,它有過去步驟的記憶,記錄了規劃、執行和錯誤的歷史。

重放智慧體的記憶

我們提供了幾個功能來檢查過去的智慧體執行情況。

您可以檢測智慧體的執行,並將其顯示在一個出色的使用者介面中,讓您可以放大/縮小特定步驟,如 檢測指南 中所述。

您還可以使用 agent.replay(),如下所示

在智慧體執行之後

from smolagents import InferenceClientModel, CodeAgent

agent = CodeAgent(tools=[], model=InferenceClientModel(), 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() 以字典形式獲取完整步驟。

您還可以使用步驟回撥來動態更改智慧體的記憶。

步驟回撥可以在其引數中訪問 agent 本身,因此它們可以像上面所強調的那樣訪問任何記憶步驟,並根據需要進行更改。例如,假設您正在觀察網路瀏覽器智慧體執行的每個步驟的螢幕截圖。您希望記錄最新的螢幕截圖,並從舊步驟中刪除影像以節省詞元成本。

您可以執行類似下面的程式碼。注意:此程式碼不完整,為簡潔起見,已刪除一些匯入和物件定義,請訪問 原始指令碼 獲取完整的可執行程式碼。

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=[WebSearchTool(), 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 InferenceClientModel, CodeAgent, ActionStep, TaskStep

agent = CodeAgent(tools=[], model=InferenceClientModel(), verbosity_level=1)
agent.python_executor.send_tools({**agent.tools})
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)
< > 在 GitHub 上更新

© . This site is unofficial and not affiliated with Hugging Face, Inc.