開始使用免費開始

更聰明的代理:驗證並重試

你已經撰寫了一個驗證函式,用來檢查房地產代理的回覆是否包含足夠的細節。現在要在代理中使用這個函式。

如果回覆太短,你的 check_answer_length() 函式會擲出例外,代理會根據你的回饋自動重試,並產生更完整的答案。

注意此處我們使用 verbosity_level=2,以便觀察代理的思考過程與動作。

本練習屬於課程

使用 Hugging Face smolagents 的 AI 代理

檢視課程

練習說明

  • 在建立代理時,將 check_answer_length 加入 final_answer_checks 清單。
  • 使用簡單的提示執行代理,以觸發一個回覆。

動手互動練習

試著完成這個範例程式碼,體驗一下這個練習。

real_estate_agent = CodeAgent(
    tools=[],
    model=model,
    # Create the agent with answer length validation
    final_answer_checks=[____],
    verbosity_level=2
)

# Run the agent with a short prompt
response = real_estate_agent.run("Suggest a neigborhood for a couple moving to Austin.")
print(response)
編輯並執行程式碼