← Lesson
Functions
Joeven
Run
Reset
Python loads on first run
def search(q, k=3): """A note inside the function: return k fake documents.""" hits = [] for i in range(k): hits.append("doc " + str(i) + " about " + q) return hits def add(a, b): return a + b def call_tool(name, **kwargs): registry = {"search": search, "add": add} fn = registry.get(name) if fn is None: return {"ok": False, "error": "unknown tool " + name} return {"ok": True, "result": fn(**kwargs)} print(call_tool("search", q="agents", k=2)) print(call_tool("add", a=3, b=4)) print(call_tool("shell", cmd="rm")) args = {"q": "nyc", "k": 1} print("unpacked dict", search(**args)) print("keyword call", search(q="nyc", k=1))
Run to execute this in your browser. Nothing is sent to a server.