BT

Facilitating the Spread of Knowledge and Innovation in Professional Software Development

Write for InfoQ

Topics

Choose your language

InfoQ Homepage News Google Debuts OpenAI-compatible API for Gemini

Google Debuts OpenAI-compatible API for Gemini

In an effort to make it easier for developers who adopted OpenAI for their LLM-based solutions to switch to Gemini, Google has launched a new endpoint for its Gemini API that allows them to easily switch from one service to the other. The new endpoint is still in beta and provides only partial coverage of OpenAI capabilities.

According to Google, their new openai endpoint can replace OpenAI's own endpoint when using direct REST calls or with any of OpenAI's official SDK. For example, if you have a program using the OpenAI SDK, say, in Python, you can replace its initialization code as shown in the following snippet to use Google's models instead of OpenAI's:

from openai import OpenAI
client = OpenAI(
    api_key="gemini_api_key",
    base_url="https://generativelanguage.googleapis.com/v1beta/openai/"
)

Notice how you will need to provide a Gemini API key, either in the code or through the OPENAI_API_KEY environment variable. To generate text, you can use the Chat Completions API as shown below, where you specify the name of the Gemini model you would like to use:

response = client.chat.completions.create(
    model="gemini-1.5-flash",
    n=1,
    messages=[
        {"role": "system", "content": "You are a helpful assistant."},
        {
            "role": "user",
            "content": "Explain to me how AI works"
        }
    ]
)

print(response.choices[0].message)

The new Gemini endpoint also support OpenAI's Embeddings API, used to measure the relatedness of text strings. In short, the Embeddings API map text into a vector of floating point numbers you can use to search for a specific value, group text into clusters, detect anomalies, make recommendations, and so on. The following snippet shows how you can use it on Gemini with the OpenAI SDK:

response = client.embeddings.create(
    input="Your text string goes here",
    model="text-embedding-004"
)

print(response.data[0].embedding)

At the moment, the Chat Completions API and Embeddings API are the only two OpenAI capabilities that you can use on Gemini models through the new openai endpoint. In addition, image upload and structured output have only limited support. Google says they have plans to also add more OpenAI capabilities to make it easier to adopt Gemini as a replacement for OpenAI on existing solutions, but it is not clear in which timeframe.

Reddit commentators praised Google's move as a workaround to lock-in for OpenAI's API users, although this is still far away from having a standard API to make it possible to easily switch from one model provider to another.

As a more generic approach, the vLLM project aims to support a variety of generative and embedding models and provides an OpenAI compatible server. With vLLM you can use Mistral, Llama, Llava, and many other major models currently available.

About the Author

Rate this Article

Adoption
Style

BT