Menu
CoddyTech

Özet: Mağaza Asistanı

Coddy'nin AI Engineering Journey'sinin Araçlar ve Ajanlar bölümünün bir parçası. Ders 10 / 22.

challenge icon

Görev

Zor

Mağaza asistanının ajan döngüsünü oluşturun. Başlangıç kodu iki aracı, bunların arkasındaki fonksiyonları FUNCTIONS içinde, MAX_STEPS değerini ve ilk mesajı tanımlar. Her iki fonksiyon da bilinmeyen bir ürün için ValueError fırlatır ve check_stock depo sistemi kapalı olduğunda TimeoutError fırlatır.

  1. run_tool(block) fonksiyonunu yazın: FUNCTIONS[block.name](**block.input) çalıştırın ve onun tool_result değerini döndürün. ValueError ve TimeoutError hatalarını yakalayın ve mesaj ile birlikte "is_error": True içeren bir hata sonucu döndürün. Bir sonuç için Tool: <name> <input> -> <output>, bir hata için Tool: <name> <input> -> error: <message> yazdırın.
  2. Modeli çağırın (model="claude-opus-5-5", max_tokens=500, tools=TOOLS) ve araç talep ettiği sürece döngüyü sürdürün: asistan sırasını ekleyin, tüm sonuçları tek bir kullanıcı mesajında gönderin, tekrar çağırın.
  3. Modeli MAX_STEPS kez çağırdığında döngüyü durdurun ve ardından Stopped: no answer after 4 steps yazdırın.
  4. Varsa Answer: ve ardından nihai metni, sonra Steps: ve çağrı sayısını yazdırın.

Örneğin, Check stock for the desk lamp and the price of the desk lamp girdisi için program şunu yazdırır:

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

Kendin dene

import coddy_ai as anthropic  # prodüksiyonda: 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}]

# Kodunuzu aşağıya yazın

Araçlar ve Ajanlar bölümündeki tüm dersler

Kendi başına pratik yap: Online Python derleyicisi