에이전트 최종 답변 검증

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 에이전트 만들기

Vamos praticar!

Hugging Face smolagents로 AI 에이전트 만들기

Preparing Video For Download...