Xác thực câu trả lời cuối của tác nhân

AI Agents với Hugging Face smolagents

Adel Nehme

VP of AI Curriculum, DataCamp

Vì sao xác thực quan trọng

  • Câu trả lời của tác nhân không hữu ích
  • Mất trải nghiệm khách hàng

Để tránh điều này, smolagents cho phép xác thực câu trả lời cuối!

AI Agents với Hugging Face smolagents

Xác thực phản hồi của tác nhân

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
  • Nếu final_answer không đạt, ném ngoại lệ; ngược lại trả về True.
AI Agents với Hugging Face smolagents

Dùng xác thực đầu ra trong tác nhân của bạn

car_advisor = CodeAgent(
    tools=[WebSearchTool()],
    model=InferenceClientModel(),
    final_answer_checks=[check_answer_length],
    verbosity_level=0
)
  • Chạy kiểm tra check_answer_length trước khi phản hồi.
  • Tự động thử lại dựa trên thông báo ngoại lệ trong hàm.
AI Agents với Hugging Face smolagents

Đánh giá meta: Dùng AI để kiểm định AI

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.
"""
AI Agents với Hugging Face smolagents

Xác thực lập luận bằng bộ đánh giá meta

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
AI Agents với Hugging Face smolagents

Kết hợp nhiều bước xác thực

car_advisor = CodeAgent(
    tools=[WebSearchTool()],
    model=InferenceClientModel(),
    final_answer_checks=[check_answer_length, check_reasoning_accuracy],
    verbosity_level=0
)

Dễ phát hiện và sửa lỗi trước khi người dùng nhìn thấy!

AI Agents với Hugging Face smolagents

Thiết kế hệ thống thông minh

AI Agents với Hugging Face smolagents

Ayo berlatih!

AI Agents với Hugging Face smolagents

Preparing Video For Download...