使用 Hugging Face smolagents 的 AI 代理
Adel Nehme
VP of AI Curriculum, DataCamp

為避免這種情況,smolagents 允許你驗證最終答案!
def check_answer_length(final_answer, agent_memory):
# Check if the answer is substantial enough
if len(final_answer) < 200:
raise Exception("Car recommendation is too brief")
return True
final_answer 不符合規則就擲回例外;否則回傳 True。car_advisor = CodeAgent(
tools=[WebSearchTool()],
model=InferenceClientModel(),
final_answer_checks=[check_answer_length],
verbosity_level=0
)
check_answer_length 驗證。
validation_prompt = """
Reasoning process: {}
Agent's final answer: {}
Does the final answer logically follow
from the reasoning and solve the user's
question?
Respond only TRUE or FALSE.
No other text.
"""
def check_reasoning_accuracy(final_answer, agent_memory):
evaluator_model = InferenceClientModel()
reasoning_steps = agent_memory.get_succinct_steps()
final_prompt = validation_prompt.format(reasoning_steps, final_answer)
message = ChatMessage(role='user', content=final_prompt)
evaluation = evaluator_model([message])
if evaluation.content == "FALSE":
raise Exception("The agent's reasoning process contains logical errors")
else:
return True
car_advisor = CodeAgent(
tools=[WebSearchTool()],
model=InferenceClientModel(),
final_answer_checks=[check_answer_length, check_reasoning_accuracy],
verbosity_level=0
)
在使用者看到前,更可能攔截並修正錯誤!

使用 Hugging Face smolagents 的 AI 代理