Examples · agents · 31 of 339
02-typed
A typed call: the type you expect decides the shape of the reply.
tin run examples/agents/02-typed.tin// A typed call: the type you expect decides the shape of the reply.
//
// `let order: Order = Clerk(...)` makes the compiler generate a JSON Schema for
// Order and the runtime require the model to answer in that shape. What comes
// back is a real Tin value — fields, sum types and all — not a string to parse.
type Size = | Small | Medium | Large
type Order = {
/// What the customer wants to drink
drink: string
size: Size
/// How many cups
quantity: int
/// Extra requests, if any
notes: Option<string>
}
/// The menu price of one cup, in cents.
tool fn price(drink: string, size: Size): int {
let base = if drink.lower().contains("latte") { 450 } else { 300 }
match size {
Small => base
Medium => base + 50
Large => base + 100
}
}
agent Clerk {
model: "mock"
system: "You take coffee orders."
tools: [price]
}
fn total(o: Order): int => price(o.drink, o.size) * o.quantity
fn main() {
let order: Order = Clerk("Two cups please: 2 of the 'oat latte', Large")
print(order)
print("{order.quantity} x {order.drink}")
// a tool fn is still a function: call it yourself any time
print("total: {total(order)} cents")
// the reply type can be as small as an int or a bool
let cents: int = Clerk("what is the price of a Medium 'espresso'?")
print("an espresso costs {cents} cents")
} Order { drink: "oat latte", size: Large, quantity: 2, notes: None }
2 x oat latte
total: 1100 cents
an espresso costs 350 cents