Buckets:
๐ ์์ด์ ํธ ๋ฉ๋ชจ๋ฆฌ ๊ด๋ฆฌ[[-manage-your-agents-memory]]
๊ฒฐ๊ตญ ์์ด์ ํธ๋ ๋๊ตฌ์ ํ๋กฌํํธ๋ก ์ด๋ฃจ์ด์ง ๋จ์ํ ๊ตฌ์ฑ์์๋ก ์ ์๋ฉ๋๋ค. ๊ทธ๋ฆฌ๊ณ ๋ฌด์๋ณด๋ค ์ค์ํ ๊ฒ์ ์์ด์ ํธ๊ฐ ๊ณผ๊ฑฐ ๋จ๊ณ์ ๋ฉ๋ชจ๋ฆฌ๋ฅผ ๊ฐ์ง๊ณ ์์ด ๊ณํ, ์คํ, ์ค๋ฅ์ ์ด๋ ฅ์ ์ถ์ ํ๋ค๋ ์ ์ ๋๋ค.
์์ด์ ํธ ๋ฉ๋ชจ๋ฆฌ ์ฌ์[[replay-your-agents-memory]]
๊ณผ๊ฑฐ ์คํ๋ ์์ด์ ํธ๋ฅผ ํ์ธํ๊ธฐ ์ํ ๋ช ๊ฐ์ง ๊ธฐ๋ฅ์ ์ ๊ณตํฉ๋๋ค.
๊ณ์ธก ๊ฐ์ด๋์์ ์ธ๊ธํ ๋ฐ์ ๊ฐ์ด, ์์ด์ ํธ ์คํ์ ๊ณ์ธกํ์ฌ ํน์ ๋จ๊ณ๋ฅผ ํ๋ํ๊ฑฐ๋ ์ถ์ํ ์ ์๋ ์ฐ์ํ UI๋ก ์๊ฐํํ ์ ์์ต๋๋ค.
๋ํ ๋ค์๊ณผ ๊ฐ์ด 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()
์์ด์ ํธ ๋ฉ๋ชจ๋ฆฌ ๋์ ๋ณ๊ฒฝ[[dynamically-change-the-agents-memory]]
๋ง์ ๊ณ ๊ธ ์ฌ์ฉ ์ฌ๋ก์์๋ ์์ด์ ํธ์ ๋ฉ๋ชจ๋ฆฌ๋ฅผ ๋์ ์ผ๋ก ์์ ํด์ผ ํฉ๋๋ค.
์์ด์ ํธ์ ๋ฉ๋ชจ๋ฆฌ๋ ๋ค์๊ณผ ๊ฐ์ด ์ ๊ทผํ ์ ์์ต๋๋ค.
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) # JavaScript ์ ๋๋ฉ์ด์
์ด ์๋ฃ๋ ํ์ ์คํฌ๋ฆฐ์ท์ ์ฐ๋๋ก ํฉ๋๋ค.
driver = helium.get_driver()
latest_step = memory_step.step_number
for previous_memory_step in agent.memory.steps: # ์ด์ ์คํฌ๋ฆฐ์ท์ ๋ก๊ทธ์์ ์ ๊ฑฐํ์ฌ ์ฒ๋ฆฌ ๊ณผ์ ์ ๊ฐ์ํํฉ๋๋ค.
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,
)
์ ์ฒด ์๋ ์์๋ ๋น์ ์น ๋ธ๋ผ์ฐ์ ์ฝ๋์์ ํ์ธํ ์ ์์ต๋๋ค.
์์ด์ ํธ๋ฅผ ๋จ๊ณ๋ณ๋ก ์คํ[[run-agents-one-step-at-a-time]]
์ด ๊ธฐ๋ฅ์ ๋๊ตฌ ํธ์ถ์ ์ค๋ ์๊ฐ์ด ๊ฑธ๋ฆฌ๋ ๊ฒฝ์ฐ์ ์ ์ฉํฉ๋๋ค. ์์ด์ ํธ๋ฅผ ํ ๋จ๊ณ์ฉ ์คํํ๋ฉด์ ๊ฐ ๋จ๊ณ์์ ๋ฉ๋ชจ๋ฆฌ๋ฅผ ์ ๋ฐ์ดํธํ ์ ์์ต๋๋ค.
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?"
# ํ์์ ๋ฐ๋ผ ๋ค๋ฅธ ์์ด์ ํธ์ ๋ฉ๋ชจ๋ฆฌ๋ฅผ ๋ถ๋ฌ์ ๋ฉ๋ชจ๋ฆฌ๋ฅผ ์์ ํ ์ ์์ต๋๋ค.
# agent.memory.steps = previous_agent.memory.steps
# ์๋ก์ด ์์
์ ์์ํฉ๋๋ค!
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=[],
)
# ํ ๋จ๊ณ๋ฅผ ์คํํฉ๋๋ค.
final_answer = agent.step(memory_step)
agent.memory.steps.append(memory_step)
step_number += 1
# ํ์ํ ๊ฒฝ์ฐ ๋ฉ๋ชจ๋ฆฌ๋ฅผ ์์ ํ ์๋ ์์ต๋๋ค
# ์๋ฅผ ๋ค์ด ์ต์ ๋จ๊ณ๋ฅผ ์
๋ฐ์ดํธ ํ๋ ค๋ฉด ๋ค์๊ณผ ๊ฐ์ด ์ฒ๋ฆฌํฉ๋๋ค:
# agent.memory.steps[-1] = ...
print("The final answer is:", final_answer)
Xet Storage Details
- Size:
- 5.8 kB
- Xet hash:
- 739fe01cd9bb441bde27bfe0a8b46c9079724273230f921827bffd9aec43d6e3
Xet efficiently stores files, intelligently splitting them into unique chunks and accelerating uploads and downloads. More info.