Menu

One base_url, many models: switching to an OpenAI-compatible endpoint
πŸ“°

One base_url, many models: switching to an OpenAI-compatible endpoint

DEV Community: apiΒ·batonyzeng-sketchΒ·13 days ago
#kEaLtiMy
#devtoapi#devto#dev#model#openai
Long article (349 words) β€” no AI summary yet.
Reading 0:00
15s threshold

Disclosure up front: I work on the gateway described below, so treat the code as practical and the conclusions as my own experience, not neutral benchmarks.

The problem

Most teams start with one model provider. Then a second need appears β€” cheaper bulk tasks, a model that handles a different language better, or a local option for sensitive data. Suddenly you are managing two SDKs, two key formats, and two billing dashboards.

The idea

An OpenAI-compatible endpoint keeps the request and response shape you already use. You change the base URL and the key. The rest of the client code stays put.

from openai import OpenAI

client = OpenAI ( base_url = "https://your-gateway.example/v1" , api_key = "YOUR_KEY" , )

resp = client . chat . completions . create ( model = "chosen-model" , messages = [ { "role" : "user" , "content" : "Hello" } ] , )

Enter fullscreen mode

Exit fullscreen mode

That is the whole migration.…

Continue reading β€” create a free account

Join HashtagPLUS to read full articles, follow hashtags, vote, and join the conversation.