Gemini models are accessible using the OpenAI libraries (Python and TypeScript / Javascript) along with the REST API, by updating three lines of code and using your Gemini API key . If you aren't already using the OpenAI libraries, we recommend that you call the Gemini API directly .
Python
from openai import OpenAI
client = OpenAI(
api_key="GEMINI_API_KEY",
base_url="https://generativelanguage.googleapis.com/v1beta/openai/"
)
response = client.chat.completions.create(
model="gemini-3-flash-preview",
messages=[
{ "role": "system",
"content": "You are a helpful assistant."
},
{
"role": "user",
"content": "Explain to me how AI works"
}
]
)
print(response.choices[0].message)
JavaScript
import OpenAI from "openai";
const openai = new OpenAI({
apiKey: "GEMINI_API_KEY",
baseURL: "https://generativelanguage.googleapis.com/v1beta/openai/"
});
const response = await openai.chat.completions.create({
model: "gemini-3-flash-preview",
messages: [
{ role: "system",
content: "You are a helpful assistant."
},
{
role: "user",
content: "Explain to me how AI works",
},
],
});
console.log(response.choices[0].message);
PUSHTIM
curl "https://generativelanguage.googleapis.com/v1beta/openai/chat/completions" \
-H "Content-Type: application/json" \
-H "Authorization: Bearer $GEMINI_API_KEY" \
-d '{
"model": "gemini-3-flash-preview",
"messages": [
{
"role": "user",
"content": "Explain to me how AI works"
}
]
}'
Çfarë ndryshoi? Vetëm tre rreshta!
api_key="GEMINI_API_KEY": Zëvendësoni "GEMINI_API_KEY" me çelësin tuaj aktual Gemini API, të cilin mund ta merrni në Google AI Studio .base_url="https://generativelanguage.googleapis.com/v1beta/openai/": Kjo i tregon bibliotekës OpenAI të dërgojë kërkesa te pika fundore e API-t Gemini në vend të URL-së së parazgjedhur.model="gemini-3-flash-preview": Zgjidhni një model të pajtueshëm Gemini
Të menduarit
Gemini models are trained to think through complex problems, leading to significantly improved reasoning. The Gemini API comes with thinking parameters which give fine grain control over how much the model will think.
Modele të ndryshme Gemini kanë konfigurime të ndryshme arsyetimi, mund të shihni se si ato përputhen me përpjekjet e arsyetimit të OpenAI si më poshtë:
reasoning_effort (OpenAI) | thinking_level (Gemini 3.1 Pro) | thinking_level (Gemini 3.1 Flash-Lite) | thinking_level (Binjakët 3 Flash) | thinking_budget (Binjakët 2.5) |
|---|---|---|---|---|
minimal | low | minimal | minimal | 1,024 |
low | low | low | low | 1,024 |
medium | medium | medium | medium | 8,192 |
high | high | high | high | 24,576 |
Nëse nuk specifikohet reasoning_effort , Gemini përdor nivelin ose buxhetin e parazgjedhur të modelit.
Nëse doni të çaktivizoni të menduarit, mund ta vendosni reasoning_effort në "none" për modelet 2.5. Arsyetimi nuk mund të çaktivizohet për modelet Gemini 2.5 Pro ose 3.
Python
from openai import OpenAI
client = OpenAI(
api_key="GEMINI_API_KEY",
base_url="https://generativelanguage.googleapis.com/v1beta/openai/"
)
response = client.chat.completions.create(
model="gemini-3-flash-preview",
reasoning_effort="low",
messages=[
{ "role": "system",
"content": "You are a helpful assistant."
},
{
"role": "user",
"content": "Explain to me how AI works"
}
]
)
print(response.choices[0].message)
JavaScript
import OpenAI from "openai";
const openai = new OpenAI({
apiKey: "GEMINI_API_KEY",
baseURL: "https://generativelanguage.googleapis.com/v1beta/openai/"
});
const response = await openai.chat.completions.create({
model: "gemini-3-flash-preview",
reasoning_effort: "low",
messages: [
{ role: "system",
content: "You are a helpful assistant."
},
{
role: "user",
content: "Explain to me how AI works",
},
],
});
console.log(response.choices[0].message);
PUSHTIM
curl "https://generativelanguage.googleapis.com/v1beta/openai/chat/completions" \
-H "Content-Type: application/json" \
-H "Authorization: Bearer $GEMINI_API_KEY" \
-d '{
"model": "gemini-3-flash-preview",
"reasoning_effort": "low",
"messages": [
{
"role": "user",
"content": "Explain to me how AI works"
}
]
}'
Modelet e të menduarit Gemini prodhojnë gjithashtu përmbledhje mendimesh . Mund të përdorni fushën extra_body për të përfshirë fushat Gemini në kërkesën tuaj.
Vini re se reasoning_effort dhe thinking_level / thinking_budget mbivendosen me njëra-tjetrën, kështu që ato nuk mund të përdoren në të njëjtën kohë.
Python
from openai import OpenAI
client = OpenAI(
api_key="GEMINI_API_KEY",
base_url="https://generativelanguage.googleapis.com/v1beta/openai/"
)
response = client.chat.completions.create(
model="gemini-3-flash-preview",
messages=[{"role": "user", "content": "Explain to me how AI works"}],
extra_body={
'extra_body': {
"google": {
"thinking_config": {
"thinking_level": "low",
"include_thoughts": True
}
}
}
}
)
print(response.choices[0].message)
JavaScript
import OpenAI from "openai";
const openai = new OpenAI({
apiKey: "GEMINI_API_KEY",
baseURL: "https://generativelanguage.googleapis.com/v1beta/openai/"
});
const response = await openai.chat.completions.create({
model: "gemini-3-flash-preview",
messages: [{role: "user", content: "Explain to me how AI works",}],
extra_body: {
"google": {
"thinking_config": {
"thinking_level": "low",
"include_thoughts": true
}
}
}
});
console.log(response.choices[0].message);
PUSHTIM
curl "https://generativelanguage.googleapis.com/v1beta/openai/chat/completions" \
-H "Content-Type: application/json" \
-H "Authorization: Bearer GEMINI_API_KEY" \
-d '{
"model": "gemini-3-flash-preview",
"messages": [{"role": "user", "content": "Explain to me how AI works"}],
"extra_body": {
"google": {
"thinking_config": {
"thinking_level": "low",
"include_thoughts": true
}
}
}
}'
Gemini 3 mbështet përputhshmërinë me OpenAI për nënshkrimet e mendimeve në API-të e përfundimit të bisedave. Mund ta gjeni shembullin e plotë në faqen e nënshkrimeve të mendimeve .
Transmetim
API-ja Gemini mbështet përgjigjet e transmetimit .
Python
from openai import OpenAI
client = OpenAI(
api_key="GEMINI_API_KEY",
base_url="https://generativelanguage.googleapis.com/v1beta/openai/"
)
response = client.chat.completions.create(
model="gemini-3-flash-preview",
messages=[
{
"role": "system",
"content": "You are a helpful assistant."
},
{ "role": "user",
"content": "Hello!"
}
],
stream=True
)
for chunk in response:
print(chunk.choices[0].delta)
JavaScript
import OpenAI from "openai";
const openai = new OpenAI({
apiKey: "GEMINI_API_KEY",
baseURL: "https://generativelanguage.googleapis.com/v1beta/openai/"
});
async function main() {
const completion = await openai.chat.completions.create({
model: "gemini-3-flash-preview",
messages: [
{
"role": "system",
"content": "You are a helpful assistant."
},
{
"role": "user",
"content": "Hello!"
}
],
stream: true,
});
for await (const chunk of completion) {
console.log(chunk.choices[0].delta.content);
}
}
main();
PUSHTIM
curl "https://generativelanguage.googleapis.com/v1beta/openai/chat/completions" \
-H "Content-Type: application/json" \
-H "Authorization: Bearer GEMINI_API_KEY" \
-d '{
"model": "gemini-3-flash-preview",
"messages": [
{"role": "user", "content": "Explain to me how AI works"}
],
"stream": true
}'
Thirrja e funksionit
Thirrja e funksioneve e bën më të lehtë për ju marrjen e rezultateve të të dhënave të strukturuara nga modelet gjeneruese dhe mbështetet në Gemini API .
Python
from openai import OpenAI
client = OpenAI(
api_key="GEMINI_API_KEY",
base_url="https://generativelanguage.googleapis.com/v1beta/openai/"
)
tools = [
{
"type": "function",
"function": {
"name": "get_weather",
"description": "Get the weather in a given location",
"parameters": {
"type": "object",
"properties": {
"location": {
"type": "string",
"description": "The city and state, e.g. Chicago, IL",
},
"unit": {"type": "string", "enum": ["celsius", "fahrenheit"]},
},
"required": ["location"],
},
}
}
]
messages = [{"role": "user", "content": "What's the weather like in Chicago today?"}]
response = client.chat.completions.create(
model="gemini-3-flash-preview",
messages=messages,
tools=tools,
tool_choice="auto"
)
print(response)
JavaScript
import OpenAI from "openai";
const openai = new OpenAI({
apiKey: "GEMINI_API_KEY",
baseURL: "https://generativelanguage.googleapis.com/v1beta/openai/"
});
async function main() {
const messages = [{"role": "user", "content": "What's the weather like in Chicago today?"}];
const tools = [
{
"type": "function",
"function": {
"name": "get_weather",
"description": "Get the weather in a given location",
"parameters": {
"type": "object",
"properties": {
"location": {
"type": "string",
"description": "The city and state, e.g. Chicago, IL",
},
"unit": {"type": "string", "enum": ["celsius", "fahrenheit"]},
},
"required": ["location"],
},
}
}
];
const response = await openai.chat.completions.create({
model: "gemini-3-flash-preview",
messages: messages,
tools: tools,
tool_choice: "auto",
});
console.log(response);
}
main();
PUSHTIM
curl "https://generativelanguage.googleapis.com/v1beta/openai/chat/completions" \
-H "Content-Type: application/json" \
-H "Authorization: Bearer GEMINI_API_KEY" \
-d '{
"model": "gemini-3-flash-preview",
"messages": [
{
"role": "user",
"content": "What'\''s the weather like in Chicago today?"
}
],
"tools": [
{
"type": "function",
"function": {
"name": "get_weather",
"description": "Get the current weather in a given location",
"parameters": {
"type": "object",
"properties": {
"location": {
"type": "string",
"description": "The city and state, e.g. Chicago, IL"
},
"unit": {
"type": "string",
"enum": ["celsius", "fahrenheit"]
}
},
"required": ["location"]
}
}
}
],
"tool_choice": "auto"
}'
Kuptimi i imazhit
Modelet Gemini janë multimodale në thelb dhe ofrojnë performancën më të mirë në klasën e tyre në shumë detyra të zakonshme të shikimit .
Python
import base64
from openai import OpenAI
client = OpenAI(
api_key="GEMINI_API_KEY",
base_url="https://generativelanguage.googleapis.com/v1beta/openai/"
)
# Function to encode the image
def encode_image(image_path):
with open(image_path, "rb") as image_file:
return base64.b64encode(image_file.read()).decode('utf-8')
# Getting the base64 string
base64_image = encode_image("Path/to/agi/image.jpeg")
response = client.chat.completions.create(
model="gemini-3-flash-preview",
messages=[
{
"role": "user",
"content": [
{
"type": "text",
"text": "What is in this image?",
},
{
"type": "image_url",
"image_url": {
"url": f"data:image/jpeg;base64,{base64_image}"
},
},
],
}
],
)
print(response.choices[0])
JavaScript
import OpenAI from "openai";
import fs from 'fs/promises';
const openai = new OpenAI({
apiKey: "GEMINI_API_KEY",
baseURL: "https://generativelanguage.googleapis.com/v1beta/openai/"
});
async function encodeImage(imagePath) {
try {
const imageBuffer = await fs.readFile(imagePath);
return imageBuffer.toString('base64');
} catch (error) {
console.error("Error encoding image:", error);
return null;
}
}
async function main() {
const imagePath = "Path/to/agi/image.jpeg";
const base64Image = await encodeImage(imagePath);
const messages = [
{
"role": "user",
"content": [
{
"type": "text",
"text": "What is in this image?",
},
{
"type": "image_url",
"image_url": {
"url": `data:image/jpeg;base64,${base64Image}`
},
},
],
}
];
try {
const response = await openai.chat.completions.create({
model: "gemini-3-flash-preview",
messages: messages,
});
console.log(response.choices[0]);
} catch (error) {
console.error("Error calling Gemini API:", error);
}
}
main();
PUSHTIM
bash -c '
base64_image=$(base64 -i "Path/to/agi/image.jpeg");
curl "https://generativelanguage.googleapis.com/v1beta/openai/chat/completions" \
-H "Content-Type: application/json" \
-H "Authorization: Bearer GEMINI_API_KEY" \
-d "{
\"model\": \"gemini-3-flash-preview\",
\"messages\": [
{
\"role\": \"user\",
\"content\": [
{ \"type\": \"text\", \"text\": \"What is in this image?\" },
{
\"type\": \"image_url\",
\"image_url\": { \"url\": \"data:image/jpeg;base64,${base64_image}\" }
}
]
}
]
}"
'
Gjeneroni një imazh
Generate an image using gemini-2.5-flash-image or gemini-3-pro-image-preview . Supported parameters include prompt , model , n , size , and response_format . Any other parameters not listed here or in the extra_body section will be silently ignored by the compatibility layer.
Python
import base64
from openai import OpenAI
from PIL import Image
from io import BytesIO
client = OpenAI(
api_key="GEMINI_API_KEY",
base_url="https://generativelanguage.googleapis.com/v1beta/openai/",
)
response = client.images.generate(
model="gemini-2.5-flash-image",
prompt="a portrait of a sheepadoodle wearing a cape",
response_format='b64_json',
n=1,
)
for image_data in response.data:
image = Image.open(BytesIO(base64.b64decode(image_data.b64_json)))
image.show()
JavaScript
import OpenAI from "openai";
const openai = new OpenAI({
apiKey: "GEMINI_API_KEY",
baseURL: "https://generativelanguage.googleapis.com/v1beta/openai/",
});
async function main() {
const image = await openai.images.generate(
{
model: "gemini-2.5-flash-image",
prompt: "a portrait of a sheepadoodle wearing a cape",
response_format: "b64_json",
n: 1,
}
);
console.log(image.data);
}
main();
PUSHTIM
curl "https://generativelanguage.googleapis.com/v1beta/openai/images/generations" \
-H "Content-Type: application/json" \
-H "Authorization: Bearer GEMINI_API_KEY" \
-d '{
"model": "gemini-2.5-flash-image",
"prompt": "a portrait of a sheepadoodle wearing a cape",
"response_format": "b64_json",
"n": 1,
}'
Gjenero një video
Generate a video using veo-3.1-generate-preview via the Sora-compatible /v1/videos endpoint. Supported top-level parameters are prompt and model . Additional parameters like duration_seconds , image , and aspect_ratio must be passed with extra_body . See the extra_body section for all available parameters.
Gjenerimi i videos është një operacion afatgjatë që kthen një ID operacioni që mund ta anketoni për përfundim.
Python
from openai import OpenAI
client = OpenAI(
api_key="GEMINI_API_KEY",
base_url="https://generativelanguage.googleapis.com/v1beta/openai/"
)
# Returns a Long Running Operation (status: processing)
response = client.videos.create(
model="veo-3.1-generate-preview",
prompt="A cinematic drone shot of a waterfall",
)
print(f"Operation ID: {response.id}")
print(f"Status: {response.status}")
JavaScript
import OpenAI from "openai";
const openai = new OpenAI({
apiKey: "GEMINI_API_KEY",
baseURL: "https://generativelanguage.googleapis.com/v1beta/openai/"
});
async function main() {
// Returns a Long Running Operation (status: processing)
const response = await openai.videos.create({
model: "veo-3.1-generate-preview",
prompt: "A cinematic drone shot of a waterfall",
});
console.log(`Operation ID: ${response.id}`);
console.log(`Status: ${response.status}`);
}
main();
PUSHTIM
curl "https://generativelanguage.googleapis.com/v1beta/openai/videos" \
-H "Authorization: Bearer $GEMINI_API_KEY" \
-F "model=veo-3.1-generate-preview" \
-F "prompt=A cinematic drone shot of a waterfall"
Kontrolloni statusin e videos
Gjenerimi i videos është asinkron. Përdorni GET /v1/videos/{id} për të anketuar statusin dhe për të marrë URL-në përfundimtare të videos kur të përfundojë:
Python
import time
from openai import OpenAI
client = OpenAI(
api_key="GEMINI_API_KEY",
base_url="https://generativelanguage.googleapis.com/v1beta/openai/"
)
# Poll until video is ready
video_id = response.id # From the create call
while True:
video = client.videos.retrieve(video_id)
if video.status == "completed":
print(f"Video URL: {video.url}")
break
elif video.status == "failed":
print(f"Generation failed: {video.error}")
break
print(f"Status: {video.status}. Waiting...")
time.sleep(10)
JavaScript
import OpenAI from "openai";
const openai = new OpenAI({
apiKey: "GEMINI_API_KEY",
baseURL: "https://generativelanguage.googleapis.com/v1beta/openai/"
});
async function main() {
// Poll until video is ready
const videoId = response.id; // From the create call
while (true) {
const video = await openai.videos.retrieve(videoId);
if (video.status === "completed") {
console.log(`Video URL: ${video.url}`);
break;
} else if (video.status === "failed") {
console.log(`Generation failed: ${video.error}`);
break;
}
console.log(`Status: ${video.status}. Waiting...`);
await new Promise(resolve => setTimeout(resolve, 10000));
}
}
main();
PUSHTIM
curl "https://generativelanguage.googleapis.com/v1beta/openai/videos/VIDEO_ID" \
-H "Authorization: Bearer $GEMINI_API_KEY"
Kuptimi i audios
Analizoni hyrjen audio:
Python
import base64
from openai import OpenAI
client = OpenAI(
api_key="GEMINI_API_KEY",
base_url="https://generativelanguage.googleapis.com/v1beta/openai/"
)
with open("/path/to/your/audio/file.wav", "rb") as audio_file:
base64_audio = base64.b64encode(audio_file.read()).decode('utf-8')
response = client.chat.completions.create(
model="gemini-3-flash-preview",
messages=[
{
"role": "user",
"content": [
{
"type": "text",
"text": "Transcribe this audio",
},
{
"type": "input_audio",
"input_audio": {
"data": base64_audio,
"format": "wav"
}
}
],
}
],
)
print(response.choices[0].message.content)
JavaScript
import fs from "fs";
import OpenAI from "openai";
const client = new OpenAI({
apiKey: "GEMINI_API_KEY",
baseURL: "https://generativelanguage.googleapis.com/v1beta/openai/",
});
const audioFile = fs.readFileSync("/path/to/your/audio/file.wav");
const base64Audio = Buffer.from(audioFile).toString("base64");
async function main() {
const response = await client.chat.completions.create({
model: "gemini-3-flash-preview",
messages: [
{
role: "user",
content: [
{
type: "text",
text: "Transcribe this audio",
},
{
type: "input_audio",
input_audio: {
data: base64Audio,
format: "wav",
},
},
],
},
],
});
console.log(response.choices[0].message.content);
}
main();
PUSHTIM
bash -c '
base64_audio=$(base64 -i "/path/to/your/audio/file.wav");
curl "https://generativelanguage.googleapis.com/v1beta/openai/chat/completions" \
-H "Content-Type: application/json" \
-H "Authorization: Bearer GEMINI_API_KEY" \
-d "{
\"model\": \"gemini-3-flash-preview\",
\"messages\": [
{
\"role\": \"user\",
\"content\": [
{ \"type\": \"text\", \"text\": \"Transcribe this audio file.\" },
{
\"type\": \"input_audio\",
\"input_audio\": {
\"data\": \"${base64_audio}\",
\"format\": \"wav\"
}
}
]
}
]
}"
'
Prodhim i strukturuar
Modelet Gemini mund të nxjerrin objekte JSON në çdo strukturë që përcaktoni .
Python
from pydantic import BaseModel
from openai import OpenAI
client = OpenAI(
api_key="GEMINI_API_KEY",
base_url="https://generativelanguage.googleapis.com/v1beta/openai/"
)
class CalendarEvent(BaseModel):
name: str
date: str
participants: list[str]
completion = client.beta.chat.completions.parse(
model="gemini-3-flash-preview",
messages=[
{"role": "system", "content": "Extract the event information."},
{"role": "user", "content": "John and Susan are going to an AI conference on Friday."},
],
response_format=CalendarEvent,
)
print(completion.choices[0].message.parsed)
JavaScript
import OpenAI from "openai";
import { zodResponseFormat } from "openai/helpers/zod";
import { z } from "zod";
const openai = new OpenAI({
apiKey: "GEMINI_API_KEY",
baseURL: "https://generativelanguage.googleapis.com/v1beta/openai"
});
const CalendarEvent = z.object({
name: z.string(),
date: z.string(),
participants: z.array(z.string()),
});
const completion = await openai.chat.completions.parse({
model: "gemini-3-flash-preview",
messages: [
{ role: "system", content: "Extract the event information." },
{ role: "user", content: "John and Susan are going to an AI conference on Friday" },
],
response_format: zodResponseFormat(CalendarEvent, "event"),
});
const event = completion.choices[0].message.parsed;
console.log(event);
Vendosje
Text embeddings measure the relatedness of text strings and can be generated using the Gemini API . You can use gemini-embedding-2-preview for multimodal embeddings or gemini-embedding-001 for text-only embeddings.
Python
from openai import OpenAI
client = OpenAI(
api_key="GEMINI_API_KEY",
base_url="https://generativelanguage.googleapis.com/v1beta/openai/"
)
response = client.embeddings.create(
input="Your text string goes here",
model="gemini-embedding-2-preview"
)
print(response.data[0].embedding)
JavaScript
import OpenAI from "openai";
const openai = new OpenAI({
apiKey: "GEMINI_API_KEY",
baseURL: "https://generativelanguage.googleapis.com/v1beta/openai/"
});
async function main() {
const embedding = await openai.embeddings.create({
model: "gemini-embedding-2-preview",
input: "Your text string goes here",
});
console.log(embedding);
}
main();
PUSHTIM
curl "https://generativelanguage.googleapis.com/v1beta/openai/embeddings" \
-H "Content-Type: application/json" \
-H "Authorization: Bearer GEMINI_API_KEY" \
-d '{
"input": "Your text string goes here",
"model": "gemini-embedding-2-preview"
}'
API-ja e grupeve
Mund të krijoni punë në grupe , t'i dorëzoni ato dhe të kontrolloni statusin e tyre duke përdorur bibliotekën OpenAI.
Do të duhet ta përgatitni skedarin JSONL në formatin e hyrjes OpenAI. Për shembull:
{"custom_id": "request-1", "method": "POST", "url": "/v1/chat/completions", "body": {"model": "gemini-3-flash-preview", "messages": [{"role": "user", "content": "Tell me a one-sentence joke."}]}}
{"custom_id": "request-2", "method": "POST", "url": "/v1/chat/completions", "body": {"model": "gemini-3-flash-preview", "messages": [{"role": "user", "content": "Why is the sky blue?"}]}}
Pajtueshmëria me OpenAI për Batch mbështet krijimin e një grupi, monitorimin e statusit të punës dhe shikimin e rezultateve të grupit.
Compatibility for upload and download is currently not supported. Instead, the following example uses the genai client for uploading and downloading files , the same as when using the Gemini Batch API .
Python
from openai import OpenAI
# Regular genai client for uploads & downloads
from google import genai
client = genai.Client()
openai_client = OpenAI(
api_key="GEMINI_API_KEY",
base_url="https://generativelanguage.googleapis.com/v1beta/openai/"
)
# Upload the JSONL file in OpenAI input format, using regular genai SDK
uploaded_file = client.files.upload(
file='my-batch-requests.jsonl',
config=types.UploadFileConfig(display_name='my-batch-requests', mime_type='jsonl')
)
# Create batch
batch = openai_client.batches.create(
input_file_id=batch_input_file_id,
endpoint="/v1/chat/completions",
completion_window="24h"
)
# Wait for batch to finish (up to 24h)
while True:
batch = client.batches.retrieve(batch.id)
if batch.status in ('completed', 'failed', 'cancelled', 'expired'):
break
print(f"Batch not finished. Current state: {batch.status}. Waiting 30 seconds...")
time.sleep(30)
print(f"Batch finished: {batch}")
# Download results in OpenAI output format, using regular genai SDK
file_content = genai_client.files.download(file=batch.output_file_id).decode('utf-8')
# See batch_output JSONL in OpenAI output format
for line in file_content.splitlines():
print(line)
The OpenAI SDK also supports generating embeddings with the Batch API . To do so, switch out the create method's endpoint field for an embeddings endpoint, as well as the url and model keys in the JSONL file:
# JSONL file using embeddings model and endpoint
# {"custom_id": "request-1", "method": "POST", "url": "/v1/embeddings", "body": {"model": "ggemini-embedding-001", "messages": [{"role": "user", "content": "Tell me a one-sentence joke."}]}}
# {"custom_id": "request-2", "method": "POST", "url": "/v1/embeddings", "body": {"model": "gemini-embedding-001", "messages": [{"role": "user", "content": "Why is the sky blue?"}]}}
# ...
# Create batch step with embeddings endpoint
batch = openai_client.batches.create(
input_file_id=batch_input_file_id,
endpoint="/v1/embeddings",
completion_window="24h"
)
Shihni seksionin e gjenerimit të ngulitjes në seri të librit të gatimit të përputhshmërisë OpenAI për një shembull të plotë.
Përfundimi Fleks dhe Prioritet
API-ja Gemini përputhet me parametrin service_tier të OpenAI në emër dhe logjikë, duke zbatuar kufizime dhe duke drejtuar me elegancë trafikun për nivelet e përfundimit Flex dhe Priority.
Python
from openai import OpenAI
client = OpenAI(
api_key="GEMINI_API_KEY",
base_url="https://generativelanguage.googleapis.com/v1beta/openai/"
)
completion = client.chat.completions.create(
model="gemini-3-flash-preview",
messages=[
{"role": "user", "content": "Write a short poem about clouds."}
],
service_tier="priority" # Or service_tier="flex"
)
print(completion)
Kur nuk është caktuar në mënyrë të qartë, service_tier vendoset si standard si parazgjedhje, ekuivalente me default për OpenAI. Mësoni më shumë rreth niveleve të përfundimit në dokumentacionin e Optimizimit .
Aktivizo veçoritë Gemini me extra_body
Ekzistojnë disa veçori të mbështetura nga Gemini që nuk janë të disponueshme në modelet OpenAI, por mund të aktivizohen duke përdorur fushën extra_body .
| Parametri | Lloji | Pika e Fundit | Përshkrimi |
|---|---|---|---|
cached_content | Tekst | Bisedë | Korespondon me memorjen e përgjithshme të përmbajtjes së Gemini. |
thinking_config | Objekti | Bisedë | Korespondon me ThinkingConfig të Gemini-t. |
aspect_ratio | Tekst | Imazhe | Raporti i aspektit të daljes (p.sh., "16:9" , "1:1" , "9:16" ). |
generation_config | Objekti | Imazhe | Objekti i konfigurimit të gjenerimit Gemini (p.sh., {"responseModalities": ["IMAGE"], "candidateCount": 2} ). |
safety_settings | Listë | Imazhe | Filtra të personalizuar të pragut të sigurisë (p.sh., [{"category": "HARM_CATEGORY_HATE_SPEECH", "threshold": "BLOCK_NONE"}] ). |
tools | Listë | Imazhe | Aktivizon tokëzimin (p.sh., [{"google_search": {}}] ). Vetëm për gemini-3-pro-image-preview . |
aspect_ratio | Tekst | Video | Dimensionet e videos dalëse ( 16:9 për horizont, 9:16 për portret). Hartat nga size nëse nuk specifikohen. |
resolution | Tekst | Video | Rezolucioni i daljes ( 720p , 1080p , 4K ). Shënim: 1080p dhe 4K aktivizojnë tubacionin e marrjes së mostrave shtesë. |
duration_seconds | Numër i plotë | Video | Gjatësia e gjenerimit (vlerat: 4 , 6 , 8 ). Duhet të jetë 8 kur përdoret reference_images , interpolim ose zgjerim. |
frame_rate | Tekst | Video | Shpejtësia e kuadrove për daljen e videos (p.sh., "24" ). |
input_reference | Tekst | Video | Të dhëna referuese për gjenerimin e videos. |
extend_video_id | Tekst | Video | ID-ja e një videoje ekzistuese që do të zgjerohet. |
negative_prompt | Tekst | Video | Artikuj që duhen përjashtuar (p.sh., "shaky camera" ). |
seed | Numër i plotë | Video | Numër i plotë për gjenerim determinist. |
style | Tekst | Video | Stil vizual (parazgjedhur cinematic , creative optimizuar për mediat sociale). |
person_generation | Tekst | Video | Kontrollon gjenerimin e njerëzve ( allow_adult , allow_all , dont_allow ). |
reference_images | Listë | Video | Deri në 3 imazhe për referencë stili/personazhi (asete base64). |
image | Tekst | Video | Imazh hyrës fillestar i koduar në Base64 për të kushtëzuar gjenerimin e videos. |
last_frame | Objekti | Video | Imazhi përfundimtar për interpolim (kërkon image si kornizë të parë). |
Shembull duke përdorur extra_body
Ja një shembull i përdorimit të extra_body për të vendosur cached_content :
Python
from openai import OpenAI
client = OpenAI(
api_key=MY_API_KEY,
base_url="https://generativelanguage.googleapis.com/v1beta/"
)
stream = client.chat.completions.create(
model="gemini-3-flash-preview",
n=1,
messages=[
{
"role": "user",
"content": "Summarize the video"
}
],
stream=True,
stream_options={'include_usage': True},
extra_body={
'extra_body':
{
'google': {
'cached_content': "cachedContents/0000aaaa1111bbbb2222cccc3333dddd4444eeee"
}
}
}
)
for chunk in stream:
print(chunk)
print(chunk.usage.to_dict())
Listoni modelet
Merrni një listë të modeleve Gemini në dispozicion:
Python
from openai import OpenAI
client = OpenAI(
api_key="GEMINI_API_KEY",
base_url="https://generativelanguage.googleapis.com/v1beta/openai/"
)
models = client.models.list()
for model in models:
print(model.id)
JavaScript
import OpenAI from "openai";
const openai = new OpenAI({
apiKey: "GEMINI_API_KEY",
baseURL: "https://generativelanguage.googleapis.com/v1beta/openai/",
});
async function main() {
const list = await openai.models.list();
for await (const model of list) {
console.log(model);
}
}
main();
PUSHTIM
curl https://generativelanguage.googleapis.com/v1beta/openai/models \
-H "Authorization: Bearer GEMINI_API_KEY"
Merrni një model
Merrni një model Gemini:
Python
from openai import OpenAI
client = OpenAI(
api_key="GEMINI_API_KEY",
base_url="https://generativelanguage.googleapis.com/v1beta/openai/"
)
model = client.models.retrieve("gemini-3-flash-preview")
print(model.id)
JavaScript
import OpenAI from "openai";
const openai = new OpenAI({
apiKey: "GEMINI_API_KEY",
baseURL: "https://generativelanguage.googleapis.com/v1beta/openai/",
});
async function main() {
const model = await openai.models.retrieve("gemini-3-flash-preview");
console.log(model.id);
}
main();
PUSHTIM
curl https://generativelanguage.googleapis.com/v1beta/openai/models/gemini-3-flash-preview \
-H "Authorization: Bearer GEMINI_API_KEY"
Kufizimet aktuale
Mbështetja për bibliotekat OpenAI është ende në versionin beta, ndërsa ne zgjerojmë mbështetjen për veçoritë.
Nëse keni pyetje në lidhje me parametrat e mbështetur, veçoritë e ardhshme ose hasni ndonjë problem gjatë fillimit me Gemini, bashkohuni me Forumin tonë të Zhvilluesve .
Çfarë vjen më pas
Provoni OpenAI Compatibility Colab tonë për të punuar me shembuj më të detajuar.