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
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)
The following response was returned.
AI mimics human intelligence by learning patterns from data, using algorithms to solve problems and make decisions.
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)
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)
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)
The following error response was returned.
openai.BadRequestError: Error code: 400 - [{'error': {'code': 400, 'message': 'Request contains an invalid argument.', 'status': 'INVALID_ARGUMENT'}}]
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
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);
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
The following response was returned.
AI systems learn from data, identify patterns, and make predictions or decisions based on those patterns.
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.
The above is the detailed content of Using Gemini with the OpenAI Library. For more information, please follow other related articles on the PHP Chinese website!

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics











Python is suitable for data science, web development and automation tasks, while C is suitable for system programming, game development and embedded systems. Python is known for its simplicity and powerful ecosystem, while C is known for its high performance and underlying control capabilities.

Python excels in gaming and GUI development. 1) Game development uses Pygame, providing drawing, audio and other functions, which are suitable for creating 2D games. 2) GUI development can choose Tkinter or PyQt. Tkinter is simple and easy to use, PyQt has rich functions and is suitable for professional development.

You can learn the basics of Python within two hours. 1. Learn variables and data types, 2. Master control structures such as if statements and loops, 3. Understand the definition and use of functions. These will help you start writing simple Python programs.

You can learn basic programming concepts and skills of Python within 2 hours. 1. Learn variables and data types, 2. Master control flow (conditional statements and loops), 3. Understand the definition and use of functions, 4. Quickly get started with Python programming through simple examples and code snippets.

Python is easier to learn and use, while C is more powerful but complex. 1. Python syntax is concise and suitable for beginners. Dynamic typing and automatic memory management make it easy to use, but may cause runtime errors. 2.C provides low-level control and advanced features, suitable for high-performance applications, but has a high learning threshold and requires manual memory and type safety management.

To maximize the efficiency of learning Python in a limited time, you can use Python's datetime, time, and schedule modules. 1. The datetime module is used to record and plan learning time. 2. The time module helps to set study and rest time. 3. The schedule module automatically arranges weekly learning tasks.

Python is widely used in the fields of web development, data science, machine learning, automation and scripting. 1) In web development, Django and Flask frameworks simplify the development process. 2) In the fields of data science and machine learning, NumPy, Pandas, Scikit-learn and TensorFlow libraries provide strong support. 3) In terms of automation and scripting, Python is suitable for tasks such as automated testing and system management.

Python excels in automation, scripting, and task management. 1) Automation: File backup is realized through standard libraries such as os and shutil. 2) Script writing: Use the psutil library to monitor system resources. 3) Task management: Use the schedule library to schedule tasks. Python's ease of use and rich library support makes it the preferred tool in these areas.
