開始使用免費開始

多說一點,Agent:強制答案長度

你正在建立一個不動產助理,根據使用者的偏好幫忙找房。有時候,agent 會給出過於簡短或含糊的建議,例如「You should check out this house in Brooklyn.」。

為了讓回覆更有幫助,你想驗證每個答案至少包含 200 個字元。如果太短,agent 應該根據你的錯誤訊息再試一次。

在這個練習中,你會撰寫一個驗證函式,用來檢查回覆的長度。

本練習屬於課程

使用 Hugging Face smolagents 的 AI 代理

檢視課程

練習說明

  • 檢查 final_answer 是否少於 200 個字元。若是,請拋出例外並提供有幫助的錯誤訊息。
  • 如果通過檢查,回傳 True 以標記該答案為有效。

動手互動練習

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

def check_answer_length(final_answer, agent_memory):
    # Check if answer contains less than 200 characters
    if len(str(final_answer)) < ____:
        raise Exception("The answer is too short. Please include more details.")
    # Return True if check passes
    return ____
編輯並執行程式碼