Gemini モデルには、REST API とともに OpenAI ライブラリ(Python と TypeScript / Javascript)を使用してアクセスできます。3 行のコードを更新し、Gemini API キーを使用します。OpenAI ライブラリを使用していない場合は、Gemini API を直接呼び出すことをおすすめします。
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-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)
Node.js
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-1.5-flash",
messages: [
{ role: "system", content: "You are a helpful assistant." },
{
role: "user",
content: "Explain to me how AI works",
},
],
});
console.log(response.choices[0].message);
REST
curl "https://generativelanguage.googleapis.com/v1beta/openai/chat/completions" \
-H "Content-Type: application/json" \
-H "Authorization: Bearer gemini_api_key" \
-d '{
"model": "gemini-1.5-flash",
"messages": [
{"role": "user", "content": "Explain to me how AI works"}
]
}'
ストリーミング
Gemini API はストリーミング レスポンスをサポートしています。
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-1.5-flash",
messages=[
{"role": "system", "content": "You are a helpful assistant."},
{"role": "user", "content": "Hello!"}
],
stream=True
)
for chunk in response:
print(chunk.choices[0].delta)
Node.js
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-1.5-flash",
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();
REST
curl "https://generativelanguage.googleapis.com/v1beta/openai/chat/completions" \
-H "Content-Type: application/json" \
-H "Authorization: Bearer gemini_api_key" \
-d '{
"model": "gemini-1.5-flash",
"messages": [
{"role": "user", "content": "Explain to me how AI works"}
],
"stream": true
}'
関数呼び出し
関数呼び出しを使用すると、生成モデルから構造化データ出力を簡単に取得できます。これは 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-1.5-flash",
messages=messages,
tools=tools,
tool_choice="auto"
)
print(response)
Node.js
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-1.5-flash",
messages: messages,
tools: tools,
tool_choice: "auto",
});
console.log(response);
}
main();
REST
curl "https://generativelanguage.googleapis.com/v1beta/openai/chat/completions" \
-H "Content-Type: application/json" \
-H "Authorization: Bearer gemini_api_key" \
-d '{
"model": "gemini-1.5-flash",
"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"
}'
エンベディング
テキスト エンベディングはテキスト文字列の関連性を測定し、Gemini API を使用して生成できます。
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="text-embedding-004"
)
print(response.data[0].embedding)
Node.js
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: "text-embedding-004",
input: "Your text string goes here",
});
console.log(embedding);
}
main();
REST
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": "text-embedding-004"
}'
現在の制限事項
機能のサポートを拡大する間、OpenAI ライブラリのサポートはまだベータ版です。次の機能は制限されます。
サポートされているパラメータ、今後の機能についてご不明な点がある場合や、Gemini の利用を開始する際に問題が発生した場合は、デベロッパー フォーラムにご参加ください。