Python ile çözüm: fatura hesaplayıcı

Dil modelleri kesin hesapta hata yapar. Hesabı Python düğümüne, yazmayı modele bırakın.

İleri8 dk

Bu derste

  • Özel Python modeli yazmak
  • JSON girdi ve sayısal çıktılar
  • Kesin kodu dil modeliyle birleştirmek

Kendiniz oluşturun

Bu iş akışı ve Python modeli hesabınızda oluşturulur ve düzenleyicide açılır; değiştirip çalıştırabilirsiniz.

Çalıştırmak için örnek girdi

[{"name":"A4 kâğıt","qty":3,"price":250},{"name":"Kalem","qty":10,"price":60}]

Bu iş akışının düğümleri

Kalemler (JSON)

items

Kalem listesi: ad, adet, fiyat.

Vergi yüzdesi

tax_percent

Örneğin 10.

Fatura hesaplayıcı

main.py

Yalıtılmış ortamda Python kodunuz: ara toplamı, vergiyi ve toplamı kesin hesaplar.

Metin şablonu

{{table}} {{total}}

Kodun kesin sayılarını bir yazma isteğine dönüştürür.

Dil modeli

Claude Haiku

Yalnızca mesajı yazar; hesabı kod yaptı.

Toplam

total

Sonucu bu anahtarla API yanıtında ve sohbette döndürür.

Müşteri mesajı

message

Sonucu bu anahtarla API yanıtında ve sohbette döndürür.

Python kodu

execute işlevi girdileri inputs içinde alır ve çıktıların bir sözlüğünü döndürür. Kod yalıtılmış ortamda çalışır.

main.pypython
"""Invoice calculator: exact totals for a list of items (from the MLGrid lessons)."""


def execute(inputs: dict, params: dict) -> dict:
    items = inputs.get("items") or []
    if isinstance(items, dict):
        items = items.get("items", [])
    tax_percent = float(inputs.get("tax_percent") or 0)

    lines, subtotal = [], 0.0
    for item in items:
        qty = float(item.get("qty", 1))
        price = float(item.get("price", 0))
        amount = qty * price
        subtotal += amount
        lines.append(f"{item.get('name', '?')}: {qty:g} x {price:,.2f} = {amount:,.2f}")

    tax = round(subtotal * tax_percent / 100, 2)
    total = round(subtotal + tax, 2)
    lines.append(f"subtotal: {subtotal:,.2f}")
    lines.append(f"tax ({tax_percent:g}%): {tax:,.2f}")
    return {"table": "\n".join(lines), "tax": tax, "total": total}