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ı
- Yeni bir Apps Komut Dosyası projesi oluşturmak için script.new adresine gidin.
- Başlıksız proje'yi tıklayın.
- Apps Komut Dosyası projesini AI Studio olarak yeniden adlandırın ve Yeniden adlandır'ı tıklayın.
- API anahtarınızı ayarlayın
- Sol tarafta Proje Ayarları'nı
tıklayın.
- Komut Dosyası Özellikleri bölümünde Komut dosyası özelliği ekle'yi tıklayın.
- Mülk alanına anahtar adını girin:
GEMINI_API_KEY
. - Değer alanına API anahtarının değerini girin.
- Komut dosyası özelliklerini kaydet'i tıklayın.
- Sol tarafta Proje Ayarları'nı
Code.gs
dosyası içeriğini aşağıdaki kodla değiştirin:
İlk isteğinizi gönderin
Gemini 2.5 Flash modelini kullanarak Gemini API'ye istek göndermek için generateContent
yöntemini kullanan bir örnek aşağıda verilmiştir.
Python
from google import genai
client = genai.Client(api_key="YOUR_API_KEY")
response = client.models.generate_content(
model="gemini-2.5-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.5-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.5-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.5-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.5-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.5-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"
}
]
}
]
}'
"Düşünme", kod örneklerimizin çoğunda varsayılan olarak açıktır.
Bu sitedeki birçok kod örneğinde, yanıt kalitesini artırmak için varsayılan olarak "düşünme" özelliğinin etkinleştirildiği Gemini 2.5 Flash modeli kullanılmaktadır. Bu durumun yanıt süresini ve jeton kullanımını artırabileceğini unutmayın. Hıza öncelik veriyorsanız veya maliyetleri en aza indirmek istiyorsanız aşağıdaki örneklerde gösterildiği gibi düşünme bütçesini sıfıra ayarlayarak bu özelliği devre dışı bırakabilirsiniz. Daha fazla bilgi için düşünme kılavuzuna bakın.
Python
from google import genai
from google.genai import types
client = genai.Client(api_key="GEMINI_API_KEY")
response = client.models.generate_content(
model="gemini-2.5-flash",
contents="Explain how AI works in a few words",
config=types.GenerateContentConfig(
thinking_config=types.ThinkingConfig(thinking_budget=0) # Disables thinking
),
)
print(response.text)
JavaScript
import { GoogleGenAI } from "@google/genai";
const ai = new GoogleGenAI({ apiKey: "GEMINI_API_KEY" });
async function main() {
const response = await ai.models.generateContent({
model: "gemini-2.5-flash",
contents: "Explain how AI works in a few words",
config: {
thinkingConfig: {
thinkingBudget: 0, // Disables thinking
},
}
});
console.log(response.text);
}
await main();
Go
package main
import (
"context"
"fmt"
"os"
"google.golang.org/genai"
)
func main() {
ctx := context.Background()
client, _ := genai.NewClient(ctx, &genai.ClientConfig{
APIKey: os.Getenv("GEMINI_API_KEY"),
Backend: genai.BackendGeminiAPI,
})
result, _ := client.Models.GenerateContent(
ctx,
"gemini-2.5-flash",
genai.Text("Explain how AI works in a few words"),
&genai.GenerateContentConfig{
ThinkingConfig: &genai.ThinkingConfig{
ThinkingBudget: int32(0), // Disables thinking
},
}
)
fmt.Println(result.Text())
}
REST
curl "https://generativelanguage.googleapis.com/v1beta/models/gemini-2.5-flash:generateContent?key=$GEMINI_API_KEY" \
-H 'Content-Type: application/json' \
-X POST \
-d '{
"contents": [
{
"parts": [
{
"text": "Explain how AI works in a few words"
}
]
}
]
"generationConfig": {
"thinkingConfig": {
"thinkingBudget": 0
}
}
}'
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.5-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);
}
Sırada ne var?
İlk API isteğinizi gönderdiğinize göre, Gemini'nin kullanımdaki durumunu gösteren aşağıdaki kılavuzları inceleyebilirsiniz: