Gemini API hızlı başlangıç kılavuzu

Bu hızlı başlangıç kılavuzunda, kitaplıklarımızı nasıl yükleyeceğiniz ve ilk Gemini API isteğinizi nasıl göndereceğiniz gösterilmektedir.

Başlamadan önce

Gemini API anahtarınız olmalıdır. Hesabınız yoksa Google AI Studio'dan ücretsiz olarak alabilirsiniz.

Google GenAI SDK'sını yükleme

Python

Python 3.9 veya sonraki bir sürümü kullanarak aşağıdaki pip komutunu kullanarak google-genai paketini yükleyin:

pip install -q -U google-genai

JavaScript

Node.js 18 veya sonraki bir sürümü kullanarak aşağıdaki npm komutunu kullanarak TypeScript ve JavaScript için Google Gen AI SDK'sını yükleyin:

npm install @google/genai

Go

go get komutunu kullanarak google.golang.org/genai'yi modül dizininize yükleyin:

go get google.golang.org/genai

Java

Maven kullanıyorsanız bağımlılıklarınıza aşağıdakileri ekleyerek google-genai'yı yükleyebilirsiniz:

<dependencies>
  <dependency>
    <groupId>com.google.genai</groupId>
    <artifactId>google-genai</artifactId>
    <version>1.0.0</version>
  </dependency>
</dependencies>

Apps Komut Dosyası

  1. Yeni bir Apps Komut Dosyası projesi oluşturmak için script.new adresine gidin.
  2. Başlıksız proje'yi tıklayın.
  3. Apps Komut Dosyası projesini AI Studio olarak yeniden adlandırın ve Yeniden adlandır'ı tıklayın.
  4. API anahtarınızı ayarlayın
    1. Sol tarafta Proje Ayarları'nı Proje ayarlarının simgesi tıklayın.
    2. Komut Dosyası Özellikleri bölümünde Komut dosyası özelliği ekle'yi tıklayın.
    3. Mülk alanına anahtar adını girin: GEMINI_API_KEY.
    4. Değer alanına API anahtarının değerini girin.
    5. Komut dosyası özelliklerini kaydet'i tıklayın.
  5. Code.gs dosyası içeriğini aşağıdaki kodla değiştirin:

İlk isteğinizi gönderin

Gemini API'ye istek göndermek için generateContent yöntemini kullanın.

Python

from google import genai

client = genai.Client(api_key="YOUR_API_KEY")

response = client.models.generate_content(
    model="gemini-2.0-flash", contents="Explain how AI works in a few words"
)
print(response.text)

JavaScript

import { GoogleGenAI } from "@google/genai";

const ai = new GoogleGenAI({ apiKey: "YOUR_API_KEY" });

async function main() {
  const response = await ai.models.generateContent({
    model: "gemini-2.0-flash",
    contents: "Explain how AI works in a few words",
  });
  console.log(response.text);
}

main();

Go

package main

import (
    "context"
    "fmt"
    "log"

    "google.golang.org/genai"
)

func main() {
    ctx := context.Background()
    client, err := genai.NewClient(ctx, &genai.ClientConfig{
        APIKey:  "YOUR_API_KEY",
        Backend: genai.BackendGeminiAPI,
    })
    if err != nil {
        log.Fatal(err)
    }

    result, err := client.Models.GenerateContent(
        ctx,
        "gemini-2.0-flash",
        genai.Text("Explain how AI works in a few words"),
        nil,
    )
    if err != nil {
        log.Fatal(err)
    }
    fmt.Println(result.Text())
}

Java

package com.example;

import com.google.genai.Client;
import com.google.genai.types.GenerateContentResponse;

public class GenerateTextFromTextInput {
  public static void main(String[] args) {
    // The client gets the API key from the environment variable `GOOGLE_API_KEY`.
    Client client = new Client();

    GenerateContentResponse response =
        client.models.generateContent(
            "gemini-2.0-flash",
            "Explain how AI works in a few words",
            null);

    System.out.println(response.text());
  }
}

Apps Komut Dosyası

// See https://developers.google.com/apps-script/guides/properties
// for instructions on how to set the API key.
const apiKey = PropertiesService.getScriptProperties().getProperty('GEMINI_API_KEY');
function main() {
  const payload = {
    contents: [
      {
        parts: [
          { text: 'Explain how AI works in a few words' },
        ],
      },
    ],
  };

  const url = `https://generativelanguage.googleapis.com/v1beta/models/gemini-2.0-flash:generateContent?key=${apiKey}`;
  const options = {
    method: 'POST',
    contentType: 'application/json',
    payload: JSON.stringify(payload)
  };

  const response = UrlFetchApp.fetch(url, options);
  const data = JSON.parse(response);
  const content = data['candidates'][0]['content']['parts'][0]['text'];
  console.log(content);
}

REST

curl "https://generativelanguage.googleapis.com/v1beta/models/gemini-2.0-flash:generateContent?key=$YOUR_API_KEY" \
  -H 'Content-Type: application/json' \
  -X POST \
  -d '{
    "contents": [
      {
        "parts": [
          {
            "text": "Explain how AI works in a few words"
          }
        ]
      }
    ]
  }'

Sırada ne var?

İlk API isteğinizi gönderdiğinize göre, Gemini'nin işleyişini gösteren aşağıdaki kılavuzları inceleyebilirsiniz: