Validace finální odpovědi agenta

AI Agents with Hugging Face smolagents

Adel Nehme

VP of AI Curriculum, DataCamp

Proč je validace důležitá

  • Odpověď agenta nebyla užitečná
  • Zákaznická zkušenost byla narušena

Proto smolagents umožňuje validaci finálních odpovědí!

AI Agents with Hugging Face smolagents

Validace odpovědí agenta

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
  • Pokud final_answer nesplní pravidlo, vyvolá se výjimka. Jinak se vrátí True.
AI Agents with Hugging Face smolagents

Použití výstupní validace v agentovi

car_advisor = CodeAgent(
    tools=[WebSearchTool()],
    model=InferenceClientModel(),
    final_answer_checks=[check_answer_length],
    verbosity_level=0
)
  • Před odpovědí spustí validaci check_answer_length.
  • Automaticky opakuje pokus na základě zprávy výjimky definované ve funkci.
AI Agents with Hugging Face smolagents

Meta-evaluace: AI validuje 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 with Hugging Face smolagents

Validace uvažování pomocí meta-hodnotitele

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 with Hugging Face smolagents

Kombinace více validací

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

Vyšší pravděpodobnost zachycení a opravy chyb dříve, než je uživatel uvidí!

AI Agents with Hugging Face smolagents

Návrh inteligentních systémů

AI Agents with Hugging Face smolagents

Pojďme si procvičit!

AI Agents with Hugging Face smolagents

Preparing Video For Download...