开始使用免费开始使用

Say More, Agent: Enforcing Answers Length

You're building a real estate assistant that helps users find homes based on their preferences. Sometimes, the agent gives overly brief or vague recommendations, like "You should check out this house in Brooklyn."

To make responses more helpful, you want to validate that each answer contains at least 200 characters. If it's too short, the agent should try again using your error message as guidance.

In this exercise, you'll write a validation function that checks the length of a response.

本练习是课程的一部分

AI Agents with Hugging Face smolagents

查看课程

练习说明

  • Check if the final_answer is shorter than 200 characters. If it is, raise an exception with a helpful error message.
  • If it passes the check, return True to mark the answer as valid.

交互式实操练习

通过完成这段示例代码来试试这个练习。

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 ____
编辑并运行代码