Skip to main content
GACS will never ask for your seed phrase, private keys, or payment. free and ad-free.
Back to lessonLab · Module 6 / 12
Lab

Tool Use & API Integration

Hands-on lab with starter code you can copy + run, and guided steps to produce the artifact for this module.

Setup

  1. Open a fresh terminal in a new folder, e.g. mkdir lab-6 && cd lab-6.
  2. Create a Python 3.11 venv: python -m venv .venv && source .venv/bin/activate.
  3. Install the dependencies noted on each starter snippet (the small pip install … line above the code).
  4. Copy each starter into a file with the suggested name, then run it and capture the output.

Starter code

01 · OpenAI tool-calling with a real tool
save as: 06_01_lab.py
python
import json, requests
from openai import OpenAI
client = OpenAI()

def get_weather(city: str):
    r = requests.get(f"https://wttr.in/{city}?format=j1", timeout=10).json()
    return {"temp_c": r["current_condition"][0]["temp_C"]}

tools = [{
    "type":"function",
    "function":{
        "name":"get_weather",
        "description":"Current weather for a city",
        "parameters":{"type":"object","properties":{"city":{"type":"string"}},"required":["city"]},
    }}]

msgs = [{"role":"user","content":"What's the weather in Tokyo?"}]
r = client.chat.completions.create(model="gpt-5-mini", messages=msgs, tools=tools)
call = r.choices[0].message.tool_calls[0]
result = get_weather(**json.loads(call.function.arguments))
msgs += [r.choices[0].message, {"role":"tool","tool_call_id":call.id,"content":json.dumps(result)}]
final = client.chat.completions.create(model="gpt-5-mini", messages=msgs)
print(final.choices[0].message.content)
Run with: python 06_01_lab.py

Guided steps

Work through each step in order. Stop when your output matches the expected artifact — then push your work to your lab folder.

  1. 1
    Real tool integration

    Do — Wire a weather, web-search, and calculator tool into an OpenAI tool-calling agent.

    Expected artifact — Three end-to-end transcripts showing the right tool selected per question.

    Run free:
  2. 2
    Schema fuzzing

    Do — Feed 20 ambiguous prompts and log which tool the agent picks. Identify mis-routings.

    Expected artifact — routing.csv with prompt, chosen_tool, correct_tool, match?.

    Run free:
  3. 3
    Tool failure resilience

    Do — Make the weather tool randomly raise 30% of the time. Add retry+fallback logic to the agent.

    Expected artifact — Over 50 runs, success rate ≥95%; retries logged.

    Run free:

Authoritative sources

Independent primary sources used to check and corroborate the guidance on this page.