Ky fillim i shpejtë ju tregon se si të instaloni bibliotekat tona dhe të bëni kërkesën tuaj të parë të Gemini API.
Para se të filloni
Ju duhet një çelës API Gemini. Nëse nuk e keni tashmë një të tillë, mund ta merrni falas në Google AI Studio .
Instaloni Google GenAI SDK
Python
Duke përdorur Python 3.9+ , instaloni paketën google-genai
duke përdorur komandën e mëposhtme pip :
pip install -q -U google-genai
JavaScript
Duke përdorur Node.js v18+ , instaloni Google Gen AI SDK për TypeScript dhe JavaScript duke përdorur komandën e mëposhtme npm :
npm install @google/genai
Shkoni
Instaloni google.golang.org/genai në drejtorinë tuaj të modulit duke përdorur komandën go get :
go get google.golang.org/genai
Java
Nëse jeni duke përdorur Maven, mund të instaloni google-genai duke shtuar sa vijon në varësitë tuaja:
<dependencies>
<dependency>
<groupId>com.google.genai</groupId>
<artifactId>google-genai</artifactId>
<version>1.0.0</version>
</dependency>
</dependencies>
Skripti i aplikacioneve
- Për të krijuar një projekt të ri Apps Script, shkoni te script.new .
- Klikoni Projekti pa titull .
- Riemërtoni projektin e Skriptit të Aplikacioneve AI Studio dhe klikoni Riemërtoni .
- Vendosni çelësin tuaj API
- Në të majtë, klikoni Cilësimet e projektit
.
- Nën Vetitë e skriptit, klikoni "Shto vetinë e skriptit" .
- Për "Property" , vendosni emrin e çelësit:
GEMINI_API_KEY
. - Për Vlera , vendosni vlerën për çelësin API.
- Klikoni Ruaj vetitë e skriptit .
- Në të majtë, klikoni Cilësimet e projektit
- Zëvendësoni përmbajtjen e skedarit
Code.gs
me kodin e mëposhtëm:
Bëni kërkesën tuaj të parë
Këtu është një shembull që përdor metodën generateContent
për të dërguar një kërkesë te Gemini API duke përdorur modelin Gemini 2.5 Flash.
Nëse e vendosni çelësin tuaj API si variablin e mjedisit GEMINI_API_KEY
, ai do të merret automatikisht nga klienti kur përdorni bibliotekat e API-së Gemini . Përndryshe, do t'ju duhet të kaloni çelësin tuaj API si argument kur inicializoni klientin.
Vini re se të gjitha mostrat e kodit në dokumentet e Gemini API supozojnë se ju keni vendosur variablin e mjedisit GEMINI_API_KEY
.
Python
from google import genai
# The client gets the API key from the environment variable `GEMINI_API_KEY`.
client = genai.Client()
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";
// The client gets the API key from the environment variable `GEMINI_API_KEY`.
const ai = new GoogleGenAI({});
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();
Shkoni
package main
import (
"context"
"fmt"
"log"
"google.golang.org/genai"
)
func main() {
ctx := context.Background()
// The client gets the API key from the environment variable `GEMINI_API_KEY`.
client, err := genai.NewClient(ctx, nil)
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 `GEMINI_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());
}
}
Skripti i aplikacioneve
// 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';
const options = {
method: 'POST',
contentType: 'application/json',
headers: {
'x-goog-api-key': apiKey,
},
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);
}
PUSHIMI
curl "https://generativelanguage.googleapis.com/v1beta/models/gemini-2.5-flash:generateContent" \
-H "x-goog-api-key: $GEMINI_API_KEY" \
-H 'Content-Type: application/json' \
-X POST \
-d '{
"contents": [
{
"parts": [
{
"text": "Explain how AI works in a few words"
}
]
}
]
}'
"Thinking" është aktivizuar si parazgjedhje në shumë nga mostrat tona të kodit
Shumë mostra kodi në këtë faqe përdorin modelin Gemini 2.5 Flash , i cili ka funksionin "të menduarit" të aktivizuar si parazgjedhje për të përmirësuar cilësinë e përgjigjes. Duhet të jeni të vetëdijshëm se kjo mund të rrisë kohën e përgjigjes dhe përdorimin e tokenit. Nëse i jepni përparësi shpejtësisë ose dëshironi të minimizoni kostot, mund ta çaktivizoni këtë veçori duke vendosur buxhetin e të menduarit në zero, siç tregohet në shembujt më poshtë. Për më shumë detaje, shihni udhëzuesin e të menduarit .
Python
from google import genai
from google.genai import types
client = genai.Client()
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({});
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();
Shkoni
package main
import (
"context"
"fmt"
"os"
"google.golang.org/genai"
)
func main() {
ctx := context.Background()
client, err := genai.NewClient(ctx, nil)
if err != nil {
log.Fatal(err)
}
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())
}
PUSHIMI
curl "https://generativelanguage.googleapis.com/v1beta/models/gemini-2.5-flash:generateContent" \
-H "x-goog-api-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
}
}
}'
Skripti i aplikacioneve
// 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';
const options = {
method: 'POST',
contentType: 'application/json',
headers: {
'x-goog-api-key': apiKey,
},
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);
}
Çfarë është më pas
Tani që bëtë kërkesën tuaj të parë API, mund të dëshironi të eksploroni udhëzuesit e mëposhtëm që tregojnë Binjakët në veprim: