Logo

dev-resources.site

for different kinds of informations.

Using Gemini with the OpenAI Library

Published at
11/10/2024
Categories
openai
python
javascript
gemini
Author
m_sea_bass
Categories
4 categories in total
openai
open
python
open
javascript
open
gemini
open
Author
10 person written this
m_sea_bass
open
Using Gemini with the OpenAI Library

Based on this article, we can now use Gemini with the OpenAI Library. So, I decided to give it a try in this article

Currently, only the Chat Completion API and Embedding API are available.

In this article, I tried using both Python and JavaScript.

Python

First, let’s set up the environment.

pip install openai python-dotenv
Enter fullscreen mode Exit fullscreen mode

Next, let's run the following code.

import os

from dotenv import load_dotenv
from openai import OpenAI


load_dotenv()
GOOGLE_API_KEY = os.getenv("GOOGLE_API_KEY")

client = OpenAI(
    api_key=GOOGLE_API_KEY,
    base_url="https://generativelanguage.googleapis.com/v1beta/"
)


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 briefly(less than 30 words) to me how AI works."
        }
    ]
)

print(response.choices[0].message.content)
Enter fullscreen mode Exit fullscreen mode

The following response was returned.

AI mimics human intelligence by learning patterns from data, using algorithms to solve problems and make decisions. 
Enter fullscreen mode Exit fullscreen mode

In the content field, you can specify either a string or 'type': 'text'.

import os

from dotenv import load_dotenv
from openai import OpenAI


load_dotenv()
GOOGLE_API_KEY = os.getenv("GOOGLE_API_KEY")

client = OpenAI(
    api_key=GOOGLE_API_KEY,
    base_url="https://generativelanguage.googleapis.com/v1beta/"
)

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

print(response.choices[0].message.content)
Enter fullscreen mode Exit fullscreen mode

However, errors occurred with image and audio inputs.

Sample code for image input

import os

from dotenv import load_dotenv
from openai import OpenAI


load_dotenv()
GOOGLE_API_KEY = os.getenv("GOOGLE_API_KEY")

client = OpenAI(
    api_key=GOOGLE_API_KEY,
    base_url="https://generativelanguage.googleapis.com/v1beta/"
)

# png to base64 text
import base64
with open("test.png", "rb") as image:
    b64str = base64.b64encode(image.read()).decode("utf-8")

response = client.chat.completions.create(
    model="gemini-1.5-flash",
    # model="gpt-4o",
    n=1,
    messages=[
        {"role": "system", "content": "You are a helpful assistant."},
        {
            "role": "user",
            "content": [
                {
                    "type": "text",
                    "text": "Describe the image in the image below.",
                },
                {
                    "type": "image_url",
                    "image_url": {
                        "url": f"data:image/png;base64,{b64str}"
                    }
                }
            ]
        }
    ]
)

print(response.choices[0].message.content)
Enter fullscreen mode Exit fullscreen mode

Sample code for audio input

import os

from dotenv import load_dotenv
from openai import OpenAI


load_dotenv()
GOOGLE_API_KEY = os.getenv("GOOGLE_API_KEY")

client = OpenAI(
    api_key=GOOGLE_API_KEY,
    base_url="https://generativelanguage.googleapis.com/v1beta/"
)

# png to base64 text
import base64
with open("test.wav", "rb") as audio:
    b64str = base64.b64encode(audio.read()).decode("utf-8")

response = client.chat.completions.create(
    model="gemini-1.5-flash",
    # model="gpt-4o-audio-preview", 
    n=1,
    modalities=["text"],
    messages=[
        {"role": "system", "content": "You are a helpful assistant."},
        {
            "role": "user",
            "content": [
                {
                    "type": "text",
                    "text": "What does he say?",
                },
                {
                    "type": "input_audio",
                    "input_audio": {
                        "data": b64str,
                        "format": "wav",
                    }
                }
            ]
        }
    ]
)

print(response.choices[0].message.content)
Enter fullscreen mode Exit fullscreen mode

The following error response was returned.

openai.BadRequestError: Error code: 400 - [{'error': {'code': 400, 'message': 'Request contains an invalid argument.', 'status': 'INVALID_ARGUMENT'}}]
Enter fullscreen mode Exit fullscreen mode

Currently, only text input is supported, but it seems that image and audio inputs will be available in the future.

JavaScript

Let's take a look at the JavaScript sample code.

First, let’s set up the environment.

npm init -y
npm install openai
npm pkg set type=module
Enter fullscreen mode Exit fullscreen mode

Next, let’s run the following code.

import OpenAI from "openai";

const GOOGLE_API_KEY = process.env.GOOGLE_API_KEY;
const openai = new OpenAI({
    apiKey: GOOGLE_API_KEY,
    baseURL: "https://generativelanguage.googleapis.com/v1beta/"
});

const response = await openai.chat.completions.create({
    model: "gemini-1.5-flash",
    messages: [
        { role: "system", content: "You are a helpful assistant." },
        {
            role: "user",
            content: "Explain briefly(less than 30 words) to me how AI works",
        },
    ],
});

console.log(response.choices[0].message.content);
Enter fullscreen mode Exit fullscreen mode

When running the code, make sure to include the API key in the .env file. The .env file will be loaded at runtime.

node --env-file=.env run.js
Enter fullscreen mode Exit fullscreen mode

The following response was returned.

AI systems learn from data, identify patterns, and make predictions or decisions based on those patterns.
Enter fullscreen mode Exit fullscreen mode

It's great that we can use other models within the same library.

Personally, I'm happy about this because OpenAI makes it easier to edit conversation history.

gemini Article's
30 articles in total
Favicon
Building an Audio Conversation Bot with Twilio, FastAPI, and Google Gemini
Favicon
Comunicando JAVA com o GeminiAI
Favicon
Function calling with Google Gemini chat AI
Favicon
Daytona-Sample-React: Text AI
Favicon
Google Gemini 2.0 Flash Thinking: Advanced AI Reasoning Redefined
Favicon
Diary App, memories AI integration
Favicon
Utilizando o Gemini AI para ler um cupom fiscal
Favicon
🧠Generative AI - 3
Favicon
🧠Generative AI - 2
Favicon
Criando um Chatbot com JavaScript e Gemini AI: criando o backend
Favicon
Google’s Most Powerful AI Yet: Google Gemini 2.0 Explained
Favicon
Let's Create Google Gemini 2.0 Using JavaScript | Gemini AI Chatbot Clone
Favicon
Function-Calling vs. Model Context Protocol (MCP)
Favicon
Criando um Chatbot com JavaScript e Gemini AI: criando o frontend
Favicon
Best AI project ideas, in google's opinion 🫶
Favicon
Diary App, diary AI integration
Favicon
Horóscopo com IA: Uma Experiência com Next.js e Gemini
Favicon
Building a video insights generator using Gemini Flash
Favicon
How Amazon Q Stands Out: A Comparison with Microsoft Copilot and Google Gemini
Favicon
Using Gemini with the OpenAI Library
Favicon
Generate tags using gemini AI
Favicon
Build Your Own AI API With Google Gemini and Air Pipe
Favicon
Certainly! Absolutely! I apologize!
Favicon
Educome
Favicon
AI-powered development: Chrome extension with Google Gemini
Favicon
Construindo Chatbots com IA: Como Utilizar APIs de Processamento de Linguagem Natural
Favicon
Prove it is feasible with Google AI Studio
Favicon
AI Quiz Creator
Favicon
Gemini API: The Free Tier That Makes Developers Happy
Favicon
AI Project from Scratch, The Idea , Alive Diary

Featured ones: