π€« Loading Environment Variables
import os
from dotenv import load_dotenv
load_dotenv(override=True)
api_key = os.getenv('OPENAI_API_KEY')
if not api_key:
print("No API key was found - please head over to the troubleshooting notebook in this folder to identify & fix!")
elif not api_key.startswith("sk-proj-"):
print("An API key was found, but it doesn't start sk-proj-; please check you're using the right key - see troubleshooting notebook")
else:
print("API key found and looks good so far!")βοΈ Endpoint Payload Setup and Calling API
Payload and Header
import requests
headers = {"Authorization": f"Bearer {api_key}", "Content-Type": "application/json"}
payload = {
"model": "gpt-5-nano",
"messages": [
{"role": "user", "content": "Tell me a fun fact"}]
}
payload ## prints the payloadCalling the Endpoint
response = requests.post(
"<https://api.openai.com/v1/chat/completions>",
headers=headers,
json=payload
)
response.json() ## prints the responsePrint Relevant Message
response.json()["choices"][0]["message"]["content"]π§ Client Library vs API
API Approach
import requests
headers = {
"Content-Type": "application/json",
"Authorization": f"Bearer {os.getenv('OPENAI_API_KEY')}"
}
payload = {
"model": "gpt-5-nano",
"messages": [{"role": "user", "content": "Tell me a fun fact"}]
}
response = requests.post(
"<https://api.openai.com/v1/chat/completions>",
headers=headers,
json=payload
)
print(response.json()["choices"][0]["message"]["content"])
Client Library Approach
from openai import OpenAI
client = OpenAI()
response = client.chat.completions.create(
model="gpt-5-nano",
messages=[{"role": "user", "content": "Tell me a fun fact"}]
)
print(response.choices[0].message.content)
π§ͺ Try Google Gemini
GEMINI_BASE_URL = "<https://generativelanguage.googleapis.com/v1beta/openai/>"
load_dotenv(override=True)
google_api_key = os.getenv("GOOGLE_API_KEY")
if not google_api_key:
print("No API key was found - please be sure to add your key to the .env file, and save the file! Or you can skip the next 2 cells if you don't want to use Gemini")
elif not google_api_key.startswith("AIz"):
print("An API key was found, but it doesn't start AIz")
else:
print("API key found and looks good so far!")gemini = OpenAI(base_url=GEMINI_BASE_URL, api_key=google_api_key)
response = gemini.chat.completions.create(model="gemini-2.5-flash-lite", messages=[{"role": "user", "content": "Tell me a fun fact"}])
response.choices[0].message.contentπ§ͺ Try Ollama
requests.get("<http://localhost:11434>").content
## check ollama is running or not!ollama serve ## in case ollama not running!ollama pull llama3.2OLLAMA_BASE_URL = "<http://localhost:11434/v1>"
ollama = OpenAI(base_url=OLLAMA_BASE_URL, api_key='ollama')response = ollama.chat.completions.create(
model="llama3.2",
messages=[{"role": "user", "content": "Tell me a fun fact"}]
)
print(response.choices[0].message.content)π§ͺ Try DeepSeek R1
!ollama pull deepseek-r1:1.5b ## pull deepseek modelresponse = ollama.chat.completions.create(
model="deepseek-r1:1.5b",
messages=[{"role": "user", "content": "Tell me a fun fact"}]
)
print(response.choices[0].message.content)