Esta página fue traducida por una máquina y aún no ha sido revisada por una persona. El inglés es la referencia; las correcciones son bienvenidas en GitHub. GitHub

Cookwala / SDK y escenarios / 019

Convert quantities read from a recipe document

Recipes carry exact metric quantities and a display string per language. Read a quantity, convert it to the unit on your spoon, and scale the yield of a batch.

Audiencia: home cook · Objetivo: Hacer que las personas estén más sanas · Nivel: beginner

Leer primero: /docs/CORE/ /docs/QUICKSTART/

Pasos

  1. Shakshuka's first ingredient is 30 ml of olive oil, displayed as "2 tbsp olive oil". getRecipe

  2. Convert that quantity, taken from the document, to tablespoons. convert

  3. Koshari yields six servings and 3,200 g in total. getRecipe

  4. The total mass in kilograms. convert

Resultado

You can read and convert any quantity in a recipe without touching its text, which stays a display aid. Next: scenario 020 moves from quantities to temperatures.

Línea de comandos

curl -s http://localhost:7878/v1/tools/recipes/shakshuka | head -40
cookwala convert 30 ml tbsp
cookwala convert 3200 g kg

Salida grabada

Estado: ok

[1] getRecipe: {"$schema": "https://cookwala.ai/v1/schemas/recipe.schema.json", "cookwala": "0.1.0", "id": "example-shakshuka", "revision": 2, "updated": "2026-10-03", "hash": "sha256:2ea6fab93cc673fc6784f9bc9fe401d2387bb7779dc537df97286b73807c8841", "verification": {"level": "V2", "evidence": {"simulatorRuns": 1, "humanReviews": 1, "fieldReports": 0}, "reviewedBy": "fifi.cooking editorial", "notes": "Illustrative example for the spec; values are realistic but not field-verified."}, "dish": {"names": {"en": "Shakshuka", "ar": "شكشوكة", "fr": "Chakchouka"}, "cuisine": ["TN", "MA", "DZ", "LY"], "course": "main…
[2] convert: {"value": 2.0, "unit": "tbsp"}
[3] getRecipe: {"$schema": "https://cookwala.ai/v1/schemas/recipe.schema.json", "cookwala": "0.2.0", "id": "example-koshari", "revision": 1, "updated": "2026-10-04", "verification": {"level": "V1", "evidence": {"simulatorRuns": 0, "humanReviews": 1, "fieldReports": 0}, "reviewedBy": "cookwala.ai editorial (structure only)", "notes": "Example recipe written to exercise the standard: structured steps, end conditions, hazards and critical control points. Not simulated and not field-verified; values are typical home-cooking values, not measurements."}, "dish": {"names": {"en": "Koshari", "ar": "كشري"}, "cuisine"…
[4] convert: {"value": 3.2, "unit": "kg"}

Código

Ejecutar: python scenarios/out/019-convert-quantities-from-a-recipe/python.py

# Scenario 019: Convert quantities read from a recipe document
# Recipes carry exact metric quantities and a display string per language. Read a quantity, convert it to the unit on your spoon, and scale the yield of a batch.
# Run a hub first: python hub/cookwala_hub.py --recipes examples
import json, os
from cookwala.client import CookwalaClient, CookwalaProblem

def load(path):
    with open(path, encoding="utf-8") as f:
        return json.load(f)

def pick(obj, path):
    for part in path.replace("]", "").replace("[", ".").split("."):
        obj = obj[int(part)] if part.isdigit() else (len(obj) if part == "length" else obj.get(part))
    return obj

c = CookwalaClient(os.environ.get("COOKWALA_HUB", "http://localhost:7878"))


# Step 1: Shakshuka's first ingredient is 30 ml of olive oil, displayed as "2 tbsp olive oil".
shakshuka = c.get_recipe("shakshuka")
assert pick(shakshuka, "ingredients[0].qty.value") == 30, pick(shakshuka, "ingredients[0].qty.value")
print("ok", "ingredients[0].qty.value", "=", 30)
assert pick(shakshuka, "ingredients[0].qty.unit") == "ml", pick(shakshuka, "ingredients[0].qty.unit")
print("ok", "ingredients[0].qty.unit", "=", "ml")
assert pick(shakshuka, "ingredients[0].display.en") == "2 tbsp olive oil", pick(shakshuka, "ingredients[0].display.en")
print("ok", "ingredients[0].display.en", "=", "2 tbsp olive oil")

# Step 2: Convert that quantity, taken from the document, to tablespoons.
spoons = c.convert(pick(shakshuka, "ingredients[0].qty.value"), pick(shakshuka, "ingredients[0].qty.unit"), "tbsp")
assert pick(spoons, "value") == 2.0, pick(spoons, "value")
print("ok", "value", "=", 2.0)
assert pick(spoons, "unit") == "tbsp", pick(spoons, "unit")
print("ok", "unit", "=", "tbsp")

# Step 3: Koshari yields six servings and 3,200 g in total.
koshari = c.get_recipe("koshari")
assert pick(koshari, "yield.servings") == 6, pick(koshari, "yield.servings")
print("ok", "yield.servings", "=", 6)
assert pick(koshari, "yield.totalMass.value") == 3200, pick(koshari, "yield.totalMass.value")
print("ok", "yield.totalMass.value", "=", 3200)
assert pick(koshari, "yield.totalMass.unit") == "g", pick(koshari, "yield.totalMass.unit")
print("ok", "yield.totalMass.unit", "=", "g")

# Step 4: The total mass in kilograms.
kg = c.convert(pick(koshari, "yield.totalMass.value"), "g", "kg")
assert pick(kg, "value") == 3.2, pick(kg, "value")
print("ok", "value", "=", 3.2)
assert pick(kg, "unit") == "kg", pick(kg, "unit")
print("ok", "unit", "=", "kg")

print("scenario complete")

scenarios/019-convert-quantities-from-a-recipe.json

Anterior: 018 Siguiente: 020