16. Working with APIs
HTTP Basics
AI services expose REST APIs. You send a request; you get a response. The requests library handles this in Python:
import requests
response = requests.get("https://api.example.com/models")
print(response.status_code) # 200 = success
print(response.text) # Raw response text
POST Requests
AI APIs typically use POST for completions:
import requests
url = "https://api.openai.com/v1/chat/completions"
headers = {
"Authorization": "Bearer YOUR_API_KEY",
"Content-Type": "application/json"
}
payload = {
"model": "gpt-4",
"messages": [
{"role": "user", "content": "Hello"}
],
"temperature": 0.7
}
response = requests.post(url, headers=headers, json=payload)
print(response.status_code)
print(response.json())
Response Handling
response = requests.post(url, headers=headers, json=payload)
if response.status_code == 200:
data = response.json()
print(data["choices"][0]["message"]["content"])
else:
print(f"Error {response.status_code}: {response.text}")
Query Parameters
# GET with query parameters
params = {
"model": "gpt-4",
"limit": 10
}
response = requests.get(
"https://api.openai.com/v1/models",
headers=headers,
params=params
)
Local verification checkpoint
Run the smallest example from this chapter in a local workspace and record the package version, runtime, data path, and observed output. If the result depends on model size, vector count, CPU/GPU backend, or available memory, note that constraint beside the exercise so the lesson remains reproducible.
Local verification checkpoint
Run the smallest example from this chapter in a local workspace and record the package version, runtime, data path, and observed output. If the result depends on model size, vector count, CPU/GPU backend, or available memory, note that constraint beside the exercise so the lesson remains reproducible.
Use a public API (like JSONPlaceholder) to make a GET request, check the status code, and print the response JSON.