Ai Engineering Core / Chat Completion API & SDK

Trying Other Model Providers Chat Completion API

๐ŸŒŸ 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 OpenAI appears 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-keys

  • Add it to your .env file 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>").content

If 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:1b instead.
โŒ Avoid llama3.3 or llama4 โ€” 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 ollama

Prevent auto-start:

sudo systemctl disable ollama