๐ The Rise of OpenAI-Compatible Endpoints
OpenAI's Chat Completions API became so widely adopted that other AI providers started building their own endpoints with the exact same structure. These are called "OpenAI Compatible Endpoints". ๐ค
For example, Google created one at:
<https://generativelanguage.googleapis.com/v1beta/openai/>๐ The Smart Decision by OpenAI
OpenAI took it a step further โ they allowed developers to reuse the same Python SDK for other providers, simply by swapping out the base URL and API key.
So instead of learning a brand new library for every provider, you can do this:
gemini = OpenAI(
base_url="<https://generativelanguage.googleapis.com/v1beta/openai/>",
api_key="AIz...."
)
gemini.chat.completions.create(...)2โ ๏ธ Important: Even though
OpenAIappears in the code, no OpenAI model is being used here. You're simply borrowing their lightweight SDK as a communication tool to reach Google's servers. ๐
๐งช Try Google Gemini
๐ Step 1: Setup API Key
Visit Google AI Studio :
https://aistudio.google.com/Generate your API key :
https://aistudio.google.com/api-keysAdd it to your
.envfile and save ๐พ
GOOGLE_API_KEY=AIz...
๐คซ Step 2: Loading Environment Variables
โถ๏ธ Create a new code cell and run it:
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!")๐โโ๏ธ Step 3 : Call Gemini Endpoint
โถ๏ธ Create a new code cell and run it:
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
Here's the exciting part โ Ollama runs right on your local machine, and it too exposes an OpenAI-compatible endpoint! ๐
๐๏ธ Step 1: Setup up Ollama
First, make sure Ollama is running.
โถ๏ธ Create a new code cell and run it:
requests.get("<http://localhost:11434>").contentIf the following check doesn't return "Ollama is running", then:
โถ๏ธ Create a new code cell and run it:
!ollama serveโฌ๏ธ Step 2 โ Download Llama 3.2 from Meta
โถ๏ธ Create a new code cell and run it:
!ollama pull llama3.2๐ก Note: If your machine has limited RAM, use
llama3.2:1binstead.
โ Avoidllama3.3orllama4โ they're too large for most local machines.
๐ Step 3 โ Connect Using the OpenAI SDK
โถ๏ธ Create a new code cell and run it:
OLLAMA_BASE_URL = "<http://localhost:11434/v1>"
ollama = OpenAI(base_url=OLLAMA_BASE_URL, api_key='ollama')๐งช Step 4 โ Try It Out
โถ๏ธ Create a new code cell and run it:
response = ollama.chat.completions.create(
model="llama3.2",
messages=[{"role": "user", "content": "Tell me a fun fact"}]
)
print(response.choices[0].message.content)๐ค Bonus โ Try DeepSeek R1
โถ๏ธ Create a new code cell and run it:
!ollama pull deepseek-r1:1.5bโถ๏ธ Create a new code cell and run it:
response = ollama.chat.completions.create(
model="deepseek-r1:1.5b",
messages=[{"role": "user", "content": "Tell me a fun fact"}]
)
print(response.choices[0].message.content)๐ All of this runs 100% locally โ no internet, no cloud, no API costs. Just your machine doing the work! ๐
โ Special Note
The ! tells Jupyter to run the command in the terminal/shell instead of treating it as Python code.
!ollama pull deepseek-r1:1.5b # runs in terminal
ollama pull deepseek-r1:1.5b # would crash โ not valid Python โ๐ฏ Think of ! as saying: "Hey, run this in the command line, not Python."
๐ How to Stop Ollama from Running in the Background Always
If Ollama starts automatically without you running ollama serve, here's how to stop it and prevent it from auto-starting:
๐ช Windows
Stop it:
Right-click the Ollama icon in the bottom-right system tray โ click Quit Ollama
Prevent auto-start:
Open Task Manager (Ctrl + Shift + Esc) โ go to Startup Apps tab โ find Ollama โ right-click โ Disable
๐ macOS
Stop it:
Click the Ollama icon in the top menu bar โ click the โ๏ธ gear icon โ select Quit Ollama
Prevent auto-start:
Open System Settings โ search for Login Items โ under Allow in the Background, find Ollama โ toggle it Off
๐ง Linux
Stop it:
sudo systemctl stop ollamaPrevent auto-start:
sudo systemctl disable ollama