Menu
CoddyTech

Rangkuman: Asisten Toko

Termasuk bagian Tools dan Agent dalam perjalanan AI Engineering di Coddy. Pelajaran 10 dari 22.

challenge icon

Tantangan

Sulit

Buat agent loop untuk asisten toko. Kode awal mendefinisikan dua tool, fungsi di baliknya di dalam FUNCTIONS, MAX_STEPS, dan pesan pertama. Kedua fungsi memicu ValueError untuk produk yang tidak dikenal, dan check_stock memicu TimeoutError ketika sistem gudang bermasalah.

  1. Tulis run_tool(block): jalankan FUNCTIONS[block.name](**block.input) dan kembalikan tool_result-nya. Tangkap ValueError dan TimeoutError lalu kembalikan hasil error dengan pesan tersebut dan "is_error": True. Cetak Tool: <name> <input> -> <output> untuk sebuah hasil, atau Tool: <name> <input> -> error: <message> untuk sebuah error.
  2. Panggil model (model="claude-opus-5-5", max_tokens=500, tools=TOOLS) dan lakukan perulangan selama model meminta tool: tambahkan giliran assistant, kirim semua hasil dalam satu pesan user, panggil lagi.
  3. Hentikan perulangan jika telah memanggil model sebanyak MAX_STEPS kali, lalu cetak Stopped: no answer after 4 steps.
  4. Cetak Answer: dan teks akhir jika ada, lalu Steps: dan jumlah pemanggilan.

Sebagai contoh, untuk input Check stock for the desk lamp and the price of the desk lamp program akan mencetak:

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

Coba sendiri

import coddy_ai as anthropic  # dalam produksi: 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}]

# Tulis kodemu di bawah ini

Semua pelajaran di Tools dan Agent

Latihan sendiri: Compiler Python online