OpenAI की सुविधा के साथ काम करता है

Gemini के मॉडल को REST API के साथ-साथ OpenAI लाइब्रेरी (Python और TypeScript / JavaScript) का इस्तेमाल करके ऐक्सेस किया जा सकता है. इसके लिए, कोड की तीन लाइनों को अपडेट करना होगा और 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 के साथ काम करने वाले पैरामीटर और आने वाली सुविधाओं के बारे में आपका कोई सवाल है या Gemini का इस्तेमाल शुरू करने में कोई समस्या आ रही है, तो हमारे डेवलपर फ़ोरम में शामिल हों.