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
- Open a fresh terminal in a new folder, e.g.
mkdir lab-6 && cd lab-6. - Create a Python 3.11 venv:
python -m venv .venv && source .venv/bin/activate. - Install the dependencies noted on each starter snippet (the small pip install … line above the code).
- Copy each starter into a file with the suggested name, then run it and capture the output.
Starter code
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)python 06_01_lab.pyGuided steps
Work through each step in order. Stop when your output matches the expected artifact — then push your work to your lab folder.
- 1Real 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: - 2Schema 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: - 3Tool 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.
- NIST National Initiative for Cybersecurity Education (NICE)
US national framework for cybersecurity work roles and competencies.
- Financial Action Task Force (FATF)
Global AML/CFT standards referenced by financial-crime curricula.
- FBI Internet Crime Complaint Center — Annual Reports
Primary loss and typology data used in fraud-investigation training.
- US Federal Trade Commission — Consumer Sentinel Data Book
Official complaint statistics for teaching fraud trend analysis.
