Classify into a category tree
Open in playground →Pick a department first, then a category inside it, in one call. Each step chooses from a short list, so the tree can hold far more categories than one list.
Challenges
departmentchooses from 3 departments. Each department has its own field with its categories, and"when": {"department": ...}runs only the one that matches, so the other two are skipped.- A French press goes to
home_kitchen, thencoffee_tea. A backpacking tent, "Two-person backpacking tent, 4 lb, waterproof rainfly.", came backsports_outdoorsandcamping, and the other two departments were skipped.
import os
from typellm import TypeLLMClient
client = TypeLLMClient(api_key=os.environ["TYPELLM_API_KEY"])
response = client.generate(
context="Product: Stainless steel French press, 34 oz, double-wall insulated, makes 8 cups.",
questions={
"department": {
"type": "string",
"enum": [
"electronics",
"home_kitchen",
"sports_outdoors"
],
"instructions": "Which department does this product belong to?"
},
"electronics": {
"type": "string",
"enum": [
"phones",
"laptops",
"audio",
"cameras"
],
"instructions": "Which kind of electronics is it?",
"when": {
"department": "electronics"
}
},
"home_kitchen": {
"type": "string",
"enum": [
"cookware",
"coffee_tea",
"bedding",
"lighting"
],
"instructions": "Which kind of home and kitchen product is it?",
"when": {
"department": "home_kitchen"
}
},
"sports_outdoors": {
"type": "string",
"enum": [
"camping",
"cycling",
"fitness",
"hiking"
],
"instructions": "Which kind of sports and outdoors product is it?",
"when": {
"department": "sports_outdoors"
}
}
},
)
print(response)Generation(
result={
'department': 'home_kitchen',
'home_kitchen': 'coffee_tea',
},
thinking={},
usage=Usage(input_tokens=218, thinking_tokens=0),
skipped=[
'electronics',
'sports_outdoors',
],
)