← Lesson
Function Calling
Joeven
Run
Reset
Python loads on first run
import json def get_job(job_id): jobs = {17: {"status": "failed"}, 42: {"status": "ok"}} if job_id not in jobs: return {"error": "not_found", "job_id": job_id} out = {"job_id": job_id} out.update(jobs[job_id]) return out REGISTRY = {"get_job": get_job} def dispatch(call): name = call.get("name") if name not in REGISTRY: return {"error": "unknown_tool", "name": name} args = dict(call.get("args") or {}) if name == "get_job" and "job_id" in args: try: args["job_id"] = int(args["job_id"]) except (TypeError, ValueError): return {"error": "bad_args", "detail": "job_id must be an integer"} try: return {"ok": True, "result": REGISTRY[name](**args)} except TypeError as e: return {"error": "bad_args", "detail": str(e)} turns = [ {"name": "get_job", "args": {"job_id": "17"}}, {"name": "get_job", "args": {"job_id": 99}}, {"name": "launch_nukes", "args": {}}, ] for t in turns: print(json.dumps({"call": t, "obs": dispatch(t)}))
Run to execute this in your browser. Nothing is sent to a server.