エージェントの最終回答の検証

Hugging Face smolagents で学ぶ AI エージェント

Adel Nehme

VP of AI Curriculum, DataCamp

検証が重要な理由

  • エージェントの回答が役に立たなかった
  • 顧客体験が損なわれた

これを防ぐため、smolagents では最終回答を検証できます!

Hugging Face smolagents で学ぶ AI エージェント

エージェントの応答の検証

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 を返します。
Hugging Face smolagents で学ぶ AI エージェント

エージェントへの出力検証の組み込み

car_advisor = CodeAgent(
    tools=[WebSearchTool()],
    model=InferenceClientModel(),
    final_answer_checks=[check_answer_length],
    verbosity_level=0
)
  • 応答前に check_answer_length 検証を実行します。
  • 関数内で定義された例外メッセージに基づき自動的に再試行します。
Hugging Face smolagents で学ぶ AI エージェント

メタ評価:AIによる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.
"""
Hugging Face smolagents で学ぶ AI エージェント

メタ評価器による推論の検証

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
Hugging Face smolagents で学ぶ AI エージェント

複数の検証の組み合わせ

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

ユーザーに届く前にエラーを検出・修正できる可能性が高まります!

Hugging Face smolagents で学ぶ AI エージェント

インテリジェントシステムの設計

Hugging Face smolagents で学ぶ AI エージェント

では、練習しましょう!

Hugging Face smolagents で学ぶ AI エージェント

Preparing Video For Download...