Menu
CoddyTech

Riepilogo: Assistente acquisti

Fa parte della sezione Strumenti e Agenti del percorso AI Engineering di Coddy. Lezione 10 di 22.

challenge icon

Sfida

Difficile

Crea il ciclo dell'agente assistente alle vendite. Il codice iniziale definisce due strumenti, le funzioni ad essi collegate in FUNCTIONS, MAX_STEPS e il primo messaggio. Entrambe le funzioni sollevano ValueError per un prodotto sconosciuto e check_stock solleva TimeoutError quando il sistema del magazzino non è disponibile.

  1. Scrivi run_tool(block): esegui FUNCTIONS[block.name](**block.input) e restituisci il suo tool_result. Cattura ValueError e TimeoutError e restituisci un risultato di errore con il messaggio e "is_error": True. Stampa Tool: <name> <input> -> <output> per un risultato, oppure Tool: <name> <input> -> error: <message> per un errore.
  2. Chiama il modello (model="claude-opus-5-5", max_tokens=500, tools=TOOLS) ed esegui un ciclo finché richiede degli strumenti: aggiungi il turno dell'assistente, invia tutti i risultati in un unico messaggio utente, chiama di nuovo.
  3. Interrompi il ciclo quando ha chiamato il modello MAX_STEPS volte, quindi stampa Stopped: no answer after 4 steps.
  4. Stampa Answer: e il testo finale quando ce n'è uno, poi Steps: e il numero di chiamate.

Ad esempio, per l'input Check stock for the desk lamp and the price of the desk lamp il programma stampa:

Tool: check_stock {'product': 'desk lamp'} -> desk lamp: 12 in stock
Tool: get_price {'item': 'desk lamp'} -> desk lamp: 40 dollars
Answer: Here is what I found:
- desk lamp: 12 in stock
- desk lamp: 40 dollars
Steps: 2

Provalo tu

import coddy_ai as anthropic  # in produzione: import anthropic

client = anthropic.Anthropic()

MAX_STEPS = 4
STOCK = {"desk lamp": 12, "bookshelf": 5, "office chair": 0}
PRICES = {"desk lamp": 40, "bookshelf": 85, "office chair": 65, "garden bench": 150}


def check_stock(product):
    if product == "garden bench":
        raise TimeoutError("The warehouse system is temporarily unavailable, try again")
    if product not in STOCK:
        raise ValueError(f"Unknown product: {product}")
    return f"{product}: {STOCK[product]} in stock"


def get_price(item):
    if item not in PRICES:
        raise ValueError(f"No price for {item}")
    return f"{item}: {PRICES[item]} dollars"


TOOLS = [
    {
        "name": "check_stock",
        "description": "Check how many units of a product are in stock",
        "input_schema": {
            "type": "object",
            "properties": {"product": {"type": "string", "description": "The product name"}},
            "required": ["product"],
        },
    },
    {
        "name": "get_price",
        "description": "Look up the price of an item in dollars",
        "input_schema": {
            "type": "object",
            "properties": {"item": {"type": "string", "description": "The item to price"}},
            "required": ["item"],
        },
    },
]
FUNCTIONS = {"check_stock": check_stock, "get_price": get_price}

request = input()
messages = [{"role": "user", "content": request}]

# Scrivi il tuo codice qui sotto

Tutte le lezioni di Strumenti e Agenti

Esercitati da solo: Compilatore Python online