use case Β· practical Β· function calling
The move that turns a chat model into an agent: given a set of tools and a user request, decide which tool to call and produce a valid call for it. Phi-4-mini is specifically trained for function calling, so it's good at this β and here it runs entirely on your GPU, emitting strict JSON (WebLLM's JSON mode) that your app could dispatch to a real API. Nothing leaves the device.
Available tools (the model must choose one, or none):
The chosen tool + arguments stream here as JSON, then are parsed and pretty-printed.
The tools are described in the system prompt, and the model is asked to return a single JSON object
{ "tool": "β¦", "arguments": { β¦ } }. Passing
response_format: { type: "json_object" } constrains WebLLM's sampler so the output is
always valid JSON β no stray prose around it. The page then JSON.parses the result and
checks the chosen tool exists and its arguments match the declared parameters. In a real app you'd
take that object and actually call the function; here we just show it, so you can see the model's
routing decision on-device.
β Basics Β· Phi-4-mini Β· Next: Wild β