Generating content

رابط برنامه‌نویسی کاربردی (API) جمینی (Gemini) از تولید محتوا با تصاویر، صدا، کد، ابزارها و موارد دیگر پشتیبانی می‌کند. برای جزئیات بیشتر در مورد هر یک از این ویژگی‌ها، ادامه مطلب را بخوانید و نمونه کد متمرکز بر وظیفه را بررسی کنید، یا راهنماهای جامع را بخوانید.

روش: models.generateContent

با دریافت ورودی GenerateContentRequest یک پاسخ مدل تولید می‌کند. برای اطلاعات دقیق در مورد نحوه‌ی استفاده، به راهنمای تولید متن مراجعه کنید. قابلیت‌های ورودی بین مدل‌ها، از جمله مدل‌های تنظیم‌شده، متفاوت است. برای جزئیات بیشتر به راهنمای مدل و راهنمای تنظیم مراجعه کنید.

نقطه پایانی

پست https: / /generativelanguage.googleapis.com /v1beta /{model=models /*}:generateContent

پارامترهای مسیر

string model

الزامی. نام Model که برای تولید تکمیل استفاده می‌شود.

قالب: models/{model} . این قالب به صورت models/{model} است.

درخواست بدنه

بدنه درخواست شامل داده‌هایی با ساختار زیر است:

فیلدها
contents[] object ( Content )

الزامی. محتوای مکالمه فعلی با مدل.

برای پرس‌وجوهای تک نوبتی، این یک نمونه واحد است. برای پرس‌وجوهای چند نوبتی مانند چت ، این یک فیلد تکراری است که شامل سابقه مکالمه و آخرین درخواست است.

tools[] object ( Tool )

اختیاری. فهرستی از Tools Model ممکن است برای تولید پاسخ بعدی استفاده کند.

یک Tool ، قطعه کدی است که سیستم را قادر می‌سازد تا با سیستم‌های خارجی تعامل داشته باشد تا یک یا مجموعه‌ای از اقدامات را خارج از دانش و محدوده Model انجام دهد. Tool پشتیبانی شده عبارتند از Function و codeExecution . برای کسب اطلاعات بیشتر به راهنماهای فراخوانی تابع ( Function calling) و اجرای کد (Code execution) مراجعه کنید.

شیء toolConfig object ( ToolConfig )

اختیاری. پیکربندی ابزار برای هر Tool که در درخواست مشخص شده است. برای مثال استفاده به راهنمای فراخوانی تابع مراجعه کنید.

شیء safetySettings[] object ( SafetySetting )

اختیاری. فهرستی از نمونه‌های منحصر به فرد SafetySetting برای مسدود کردن محتوای ناامن.

این مورد روی GenerateContentRequest.contents و GenerateContentResponse.candidates اعمال خواهد شد. برای هر نوع SafetyCategory نباید بیش از یک تنظیم وجود داشته باشد. API هر محتوا و پاسخی را که آستانه‌های تعیین‌شده توسط این تنظیمات را برآورده نکند، مسدود می‌کند. این لیست، تنظیمات پیش‌فرض برای هر SafetyCategory مشخص‌شده در safetySettings را لغو می‌کند. اگر هیچ SafetySetting برای یک SafetyCategory مشخص‌شده در لیست وجود نداشته باشد، API از تنظیم ایمنی پیش‌فرض برای آن دسته استفاده خواهد کرد. دسته‌های آسیب HARM_CATEGORY_HATE_SPEECH، HARM_CATEGORY_SEXUALLY_EXPLICIT، HARM_CATEGORY_DANGEROUS_CONTENT، HARM_CATEGORY_HARASSMENT، HARM_CATEGORY_CIVIC_INTEGRITY پشتیبانی می‌شوند. برای اطلاعات دقیق در مورد تنظیمات ایمنی موجود، به راهنما مراجعه کنید. همچنین برای یادگیری نحوه لحاظ کردن ملاحظات ایمنی در برنامه‌های هوش مصنوعی خود، به راهنمای ایمنی مراجعه کنید.

شیء systemInstruction object ( Content )

اختیاری. دستورالعمل(های) سیستم توسط توسعه‌دهنده تنظیم می‌شود. در حال حاضر، فقط متن.

شیء generationConfig object ( GenerationConfig )

اختیاری. گزینه‌های پیکربندی برای تولید مدل و خروجی‌ها.

string cachedContent

اختیاری. نام محتوای ذخیره شده برای استفاده به عنوان زمینه برای ارائه پیش‌بینی. قالب: cachedContents/{cachedContent}

درخواست نمونه

متن

پایتون

from google import genai

client = genai.Client()
response = client.models.generate_content(
    model="gemini-2.0-flash", contents="Write a story about a magic backpack."
)
print(response.text)

نود جی اس

// Make sure to include the following import:
// import {GoogleGenAI} from '@google/genai';
const ai = new GoogleGenAI({ apiKey: process.env.GEMINI_API_KEY });

const response = await ai.models.generateContent({
  model: "gemini-2.0-flash",
  contents: "Write a story about a magic backpack.",
});
console.log(response.text);

برو

ctx := context.Background()
client, err := genai.NewClient(ctx, &genai.ClientConfig{
	APIKey:  os.Getenv("GEMINI_API_KEY"),
	Backend: genai.BackendGeminiAPI,
})
if err != nil {
	log.Fatal(err)
}
contents := []*genai.Content{
	genai.NewContentFromText("Write a story about a magic backpack.", genai.RoleUser),
}
response, err := client.Models.GenerateContent(ctx, "gemini-2.0-flash", contents, nil)
if err != nil {
	log.Fatal(err)
}
printResponse(response)

پوسته

curl "https://generativelanguage.googleapis.com/v1beta/models/gemini-2.0-flash:generateContent?key=$GEMINI_API_KEY" \
    -H 'Content-Type: application/json' \
    -X POST \
    -d '{
      "contents": [{
        "parts":[{"text": "Write a story about a magic backpack."}]
        }]
       }' 2> /dev/null

جاوا

Client client = new Client();

GenerateContentResponse response =
        client.models.generateContent(
                "gemini-2.0-flash",
                "Write a story about a magic backpack.",
                null);

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

تصویر

پایتون

from google import genai
import PIL.Image

client = genai.Client()
organ = PIL.Image.open(media / "organ.jpg")
response = client.models.generate_content(
    model="gemini-2.0-flash", contents=["Tell me about this instrument", organ]
)
print(response.text)

نود جی اس

// Make sure to include the following import:
// import {GoogleGenAI} from '@google/genai';
const ai = new GoogleGenAI({ apiKey: process.env.GEMINI_API_KEY });

const organ = await ai.files.upload({
  file: path.join(media, "organ.jpg"),
});

const response = await ai.models.generateContent({
  model: "gemini-2.0-flash",
  contents: [
    createUserContent([
      "Tell me about this instrument", 
      createPartFromUri(organ.uri, organ.mimeType)
    ]),
  ],
});
console.log(response.text);

برو

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

file, err := client.Files.UploadFromPath(
	ctx, 
	filepath.Join(getMedia(), "organ.jpg"), 
	&genai.UploadFileConfig{
		MIMEType : "image/jpeg",
	},
)
if err != nil {
	log.Fatal(err)
}
parts := []*genai.Part{
	genai.NewPartFromText("Tell me about this instrument"),
	genai.NewPartFromURI(file.URI, file.MIMEType),
}
contents := []*genai.Content{
	genai.NewContentFromParts(parts, genai.RoleUser),
}

response, err := client.Models.GenerateContent(ctx, "gemini-2.0-flash", contents, nil)
if err != nil {
	log.Fatal(err)
}
printResponse(response)

پوسته

# Use a temporary file to hold the base64 encoded image data
TEMP_B64=$(mktemp)
trap 'rm -f "$TEMP_B64"' EXIT
base64 $B64FLAGS $IMG_PATH > "$TEMP_B64"

# Use a temporary file to hold the JSON payload
TEMP_JSON=$(mktemp)
trap 'rm -f "$TEMP_JSON"' EXIT

cat > "$TEMP_JSON" << EOF
{
  "contents": [{
    "parts":[
      {"text": "Tell me about this instrument"},
      {
        "inline_data": {
          "mime_type":"image/jpeg",
          "data": "$(cat "$TEMP_B64")"
        }
      }
    ]
  }]
}
EOF

curl "https://generativelanguage.googleapis.com/v1beta/models/gemini-2.0-flash:generateContent?key=$GEMINI_API_KEY" \
    -H 'Content-Type: application/json' \
    -X POST \
    -d "@$TEMP_JSON" 2> /dev/null

جاوا

Client client = new Client();

String path = media_path + "organ.jpg";
byte[] imageData = Files.readAllBytes(Paths.get(path));

Content content =
        Content.fromParts(
                Part.fromText("Tell me about this instrument."),
                Part.fromBytes(imageData, "image/jpeg"));

GenerateContentResponse response = client.models.generateContent("gemini-2.0-flash", content, null);

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

صوتی

پایتون

from google import genai

client = genai.Client()
sample_audio = client.files.upload(file=media / "sample.mp3")
response = client.models.generate_content(
    model="gemini-2.0-flash",
    contents=["Give me a summary of this audio file.", sample_audio],
)
print(response.text)

نود جی اس

// Make sure to include the following import:
// import {GoogleGenAI} from '@google/genai';
const ai = new GoogleGenAI({ apiKey: process.env.GEMINI_API_KEY });

const audio = await ai.files.upload({
  file: path.join(media, "sample.mp3"),
});

const response = await ai.models.generateContent({
  model: "gemini-2.0-flash",
  contents: [
    createUserContent([
      "Give me a summary of this audio file.",
      createPartFromUri(audio.uri, audio.mimeType),
    ]),
  ],
});
console.log(response.text);

برو

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

file, err := client.Files.UploadFromPath(
	ctx, 
	filepath.Join(getMedia(), "sample.mp3"), 
	&genai.UploadFileConfig{
		MIMEType : "audio/mpeg",
	},
)
if err != nil {
	log.Fatal(err)
}

parts := []*genai.Part{
	genai.NewPartFromText("Give me a summary of this audio file."),
	genai.NewPartFromURI(file.URI, file.MIMEType),
}

contents := []*genai.Content{
	genai.NewContentFromParts(parts, genai.RoleUser),
}

response, err := client.Models.GenerateContent(ctx, "gemini-2.0-flash", contents, nil)
if err != nil {
	log.Fatal(err)
}
printResponse(response)

پوسته

# Use File API to upload audio data to API request.
MIME_TYPE=$(file -b --mime-type "${AUDIO_PATH}")
NUM_BYTES=$(wc -c < "${AUDIO_PATH}")
DISPLAY_NAME=AUDIO

tmp_header_file=upload-header.tmp

# Initial resumable request defining metadata.
# The upload url is in the response headers dump them to a file.
curl "${BASE_URL}/upload/v1beta/files?key=${GEMINI_API_KEY}" \
  -D upload-header.tmp \
  -H "X-Goog-Upload-Protocol: resumable" \
  -H "X-Goog-Upload-Command: start" \
  -H "X-Goog-Upload-Header-Content-Length: ${NUM_BYTES}" \
  -H "X-Goog-Upload-Header-Content-Type: ${MIME_TYPE}" \
  -H "Content-Type: application/json" \
  -d "{'file': {'display_name': '${DISPLAY_NAME}'}}" 2> /dev/null

upload_url=$(grep -i "x-goog-upload-url: " "${tmp_header_file}" | cut -d" " -f2 | tr -d "\r")
rm "${tmp_header_file}"

# Upload the actual bytes.
curl "${upload_url}" \
  -H "Content-Length: ${NUM_BYTES}" \
  -H "X-Goog-Upload-Offset: 0" \
  -H "X-Goog-Upload-Command: upload, finalize" \
  --data-binary "@${AUDIO_PATH}" 2> /dev/null > file_info.json

file_uri=$(jq ".file.uri" file_info.json)
echo file_uri=$file_uri

curl "https://generativelanguage.googleapis.com/v1beta/models/gemini-2.0-flash:generateContent?key=$GEMINI_API_KEY" \
    -H 'Content-Type: application/json' \
    -X POST \
    -d '{
      "contents": [{
        "parts":[
          {"text": "Please describe this file."},
          {"file_data":{"mime_type": "audio/mpeg", "file_uri": '$file_uri'}}]
        }]
       }' 2> /dev/null > response.json

cat response.json
echo

jq ".candidates[].content.parts[].text" response.json

ویدئو

پایتون

from google import genai
import time

client = genai.Client()
# Video clip (CC BY 3.0) from https://peach.blender.org/download/
myfile = client.files.upload(file=media / "Big_Buck_Bunny.mp4")
print(f"{myfile=}")

# Poll until the video file is completely processed (state becomes ACTIVE).
while not myfile.state or myfile.state.name != "ACTIVE":
    print("Processing video...")
    print("File state:", myfile.state)
    time.sleep(5)
    myfile = client.files.get(name=myfile.name)

response = client.models.generate_content(
    model="gemini-2.0-flash", contents=[myfile, "Describe this video clip"]
)
print(f"{response.text=}")

نود جی اس

// Make sure to include the following import:
// import {GoogleGenAI} from '@google/genai';
const ai = new GoogleGenAI({ apiKey: process.env.GEMINI_API_KEY });

let video = await ai.files.upload({
  file: path.join(media, 'Big_Buck_Bunny.mp4'),
});

// Poll until the video file is completely processed (state becomes ACTIVE).
while (!video.state || video.state.toString() !== 'ACTIVE') {
  console.log('Processing video...');
  console.log('File state: ', video.state);
  await sleep(5000);
  video = await ai.files.get({name: video.name});
}

const response = await ai.models.generateContent({
  model: "gemini-2.0-flash",
  contents: [
    createUserContent([
      "Describe this video clip",
      createPartFromUri(video.uri, video.mimeType),
    ]),
  ],
});
console.log(response.text);

برو

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

file, err := client.Files.UploadFromPath(
	ctx, 
	filepath.Join(getMedia(), "Big_Buck_Bunny.mp4"), 
	&genai.UploadFileConfig{
		MIMEType : "video/mp4",
	},
)
if err != nil {
	log.Fatal(err)
}

// Poll until the video file is completely processed (state becomes ACTIVE).
for file.State == genai.FileStateUnspecified || file.State != genai.FileStateActive {
	fmt.Println("Processing video...")
	fmt.Println("File state:", file.State)
	time.Sleep(5 * time.Second)

	file, err = client.Files.Get(ctx, file.Name, nil)
	if err != nil {
		log.Fatal(err)
	}
}

parts := []*genai.Part{
	genai.NewPartFromText("Describe this video clip"),
	genai.NewPartFromURI(file.URI, file.MIMEType),
}

contents := []*genai.Content{
	genai.NewContentFromParts(parts, genai.RoleUser),
}

response, err := client.Models.GenerateContent(ctx, "gemini-2.0-flash", contents, nil)
if err != nil {
	log.Fatal(err)
}
printResponse(response)

پوسته

# Use File API to upload audio data to API request.
MIME_TYPE=$(file -b --mime-type "${VIDEO_PATH}")
NUM_BYTES=$(wc -c < "${VIDEO_PATH}")
DISPLAY_NAME=VIDEO

# Initial resumable request defining metadata.
# The upload url is in the response headers dump them to a file.
curl "${BASE_URL}/upload/v1beta/files?key=${GEMINI_API_KEY}" \
  -D "${tmp_header_file}" \
  -H "X-Goog-Upload-Protocol: resumable" \
  -H "X-Goog-Upload-Command: start" \
  -H "X-Goog-Upload-Header-Content-Length: ${NUM_BYTES}" \
  -H "X-Goog-Upload-Header-Content-Type: ${MIME_TYPE}" \
  -H "Content-Type: application/json" \
  -d "{'file': {'display_name': '${DISPLAY_NAME}'}}" 2> /dev/null

upload_url=$(grep -i "x-goog-upload-url: " "${tmp_header_file}" | cut -d" " -f2 | tr -d "\r")
rm "${tmp_header_file}"

# Upload the actual bytes.
curl "${upload_url}" \
  -H "Content-Length: ${NUM_BYTES}" \
  -H "X-Goog-Upload-Offset: 0" \
  -H "X-Goog-Upload-Command: upload, finalize" \
  --data-binary "@${VIDEO_PATH}" 2> /dev/null > file_info.json

file_uri=$(jq ".file.uri" file_info.json)
echo file_uri=$file_uri

state=$(jq ".file.state" file_info.json)
echo state=$state

name=$(jq ".file.name" file_info.json)
echo name=$name

while [[ "($state)" = *"PROCESSING"* ]];
do
  echo "Processing video..."
  sleep 5
  # Get the file of interest to check state
  curl https://generativelanguage.googleapis.com/v1beta/files/$name > file_info.json
  state=$(jq ".file.state" file_info.json)
done

curl "https://generativelanguage.googleapis.com/v1beta/models/gemini-2.0-flash:generateContent?key=$GEMINI_API_KEY" \
    -H 'Content-Type: application/json' \
    -X POST \
    -d '{
      "contents": [{
        "parts":[
          {"text": "Transcribe the audio from this video, giving timestamps for salient events in the video. Also provide visual descriptions."},
          {"file_data":{"mime_type": "video/mp4", "file_uri": '$file_uri'}}]
        }]
       }' 2> /dev/null > response.json

cat response.json
echo

jq ".candidates[].content.parts[].text" response.json

پی دی اف

پایتون

from google import genai

client = genai.Client()
sample_pdf = client.files.upload(file=media / "test.pdf")
response = client.models.generate_content(
    model="gemini-2.0-flash",
    contents=["Give me a summary of this document:", sample_pdf],
)
print(f"{response.text=}")

برو

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

file, err := client.Files.UploadFromPath(
	ctx, 
	filepath.Join(getMedia(), "test.pdf"), 
	&genai.UploadFileConfig{
		MIMEType : "application/pdf",
	},
)
if err != nil {
	log.Fatal(err)
}

parts := []*genai.Part{
	genai.NewPartFromText("Give me a summary of this document:"),
	genai.NewPartFromURI(file.URI, file.MIMEType),
}

contents := []*genai.Content{
	genai.NewContentFromParts(parts, genai.RoleUser),
}

response, err := client.Models.GenerateContent(ctx, "gemini-2.0-flash", contents, nil)
if err != nil {
	log.Fatal(err)
}
printResponse(response)

پوسته

MIME_TYPE=$(file -b --mime-type "${PDF_PATH}")
NUM_BYTES=$(wc -c < "${PDF_PATH}")
DISPLAY_NAME=TEXT


echo $MIME_TYPE
tmp_header_file=upload-header.tmp

# Initial resumable request defining metadata.
# The upload url is in the response headers dump them to a file.
curl "${BASE_URL}/upload/v1beta/files?key=${GEMINI_API_KEY}" \
  -D upload-header.tmp \
  -H "X-Goog-Upload-Protocol: resumable" \
  -H "X-Goog-Upload-Command: start" \
  -H "X-Goog-Upload-Header-Content-Length: ${NUM_BYTES}" \
  -H "X-Goog-Upload-Header-Content-Type: ${MIME_TYPE}" \
  -H "Content-Type: application/json" \
  -d "{'file': {'display_name': '${DISPLAY_NAME}'}}" 2> /dev/null

upload_url=$(grep -i "x-goog-upload-url: " "${tmp_header_file}" | cut -d" " -f2 | tr -d "\r")
rm "${tmp_header_file}"

# Upload the actual bytes.
curl "${upload_url}" \
  -H "Content-Length: ${NUM_BYTES}" \
  -H "X-Goog-Upload-Offset: 0" \
  -H "X-Goog-Upload-Command: upload, finalize" \
  --data-binary "@${PDF_PATH}" 2> /dev/null > file_info.json

file_uri=$(jq ".file.uri" file_info.json)
echo file_uri=$file_uri

# Now generate content using that file
curl "https://generativelanguage.googleapis.com/v1beta/models/gemini-2.0-flash:generateContent?key=$GEMINI_API_KEY" \
    -H 'Content-Type: application/json' \
    -X POST \
    -d '{
      "contents": [{
        "parts":[
          {"text": "Can you add a few more lines to this poem?"},
          {"file_data":{"mime_type": "application/pdf", "file_uri": '$file_uri'}}]
        }]
       }' 2> /dev/null > response.json

cat response.json
echo

jq ".candidates[].content.parts[].text" response.json

چت

پایتون

from google import genai
from google.genai import types

client = genai.Client()
# Pass initial history using the "history" argument
chat = client.chats.create(
    model="gemini-2.0-flash",
    history=[
        types.Content(role="user", parts=[types.Part(text="Hello")]),
        types.Content(
            role="model",
            parts=[
                types.Part(
                    text="Great to meet you. What would you like to know?"
                )
            ],
        ),
    ],
)
response = chat.send_message(message="I have 2 dogs in my house.")
print(response.text)
response = chat.send_message(message="How many paws are in my house?")
print(response.text)

نود جی اس

// Make sure to include the following import:
// import {GoogleGenAI} from '@google/genai';
const ai = new GoogleGenAI({ apiKey: process.env.GEMINI_API_KEY });
const chat = ai.chats.create({
  model: "gemini-2.0-flash",
  history: [
    {
      role: "user",
      parts: [{ text: "Hello" }],
    },
    {
      role: "model",
      parts: [{ text: "Great to meet you. What would you like to know?" }],
    },
  ],
});

const response1 = await chat.sendMessage({
  message: "I have 2 dogs in my house.",
});
console.log("Chat response 1:", response1.text);

const response2 = await chat.sendMessage({
  message: "How many paws are in my house?",
});
console.log("Chat response 2:", response2.text);

برو

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

// Pass initial history using the History field.
history := []*genai.Content{
	genai.NewContentFromText("Hello", genai.RoleUser),
	genai.NewContentFromText("Great to meet you. What would you like to know?", genai.RoleModel),
}

chat, err := client.Chats.Create(ctx, "gemini-2.0-flash", nil, history)
if err != nil {
	log.Fatal(err)
}

firstResp, err := chat.SendMessage(ctx, genai.Part{Text: "I have 2 dogs in my house."})
if err != nil {
	log.Fatal(err)
}
fmt.Println(firstResp.Text())

secondResp, err := chat.SendMessage(ctx, genai.Part{Text: "How many paws are in my house?"})
if err != nil {
	log.Fatal(err)
}
fmt.Println(secondResp.Text())

پوسته

curl https://generativelanguage.googleapis.com/v1beta/models/gemini-2.0-flash:generateContent?key=$GEMINI_API_KEY \
    -H 'Content-Type: application/json' \
    -X POST \
    -d '{
      "contents": [
        {"role":"user",
         "parts":[{
           "text": "Hello"}]},
        {"role": "model",
         "parts":[{
           "text": "Great to meet you. What would you like to know?"}]},
        {"role":"user",
         "parts":[{
           "text": "I have two dogs in my house. How many paws are in my house?"}]},
      ]
    }' 2> /dev/null | grep "text"

جاوا

Client client = new Client();

Content userContent = Content.fromParts(Part.fromText("Hello"));
Content modelContent =
        Content.builder()
                .role("model")
                .parts(
                        Collections.singletonList(
                                Part.fromText("Great to meet you. What would you like to know?")
                        )
                ).build();

Chat chat = client.chats.create(
        "gemini-2.0-flash",
        GenerateContentConfig.builder()
                .systemInstruction(userContent)
                .systemInstruction(modelContent)
                .build()
);

GenerateContentResponse response1 = chat.sendMessage("I have 2 dogs in my house.");
System.out.println(response1.text());

GenerateContentResponse response2 = chat.sendMessage("How many paws are in my house?");
System.out.println(response2.text());

حافظه پنهان

پایتون

from google import genai
from google.genai import types

client = genai.Client()
document = client.files.upload(file=media / "a11.txt")
model_name = "gemini-1.5-flash-001"

cache = client.caches.create(
    model=model_name,
    config=types.CreateCachedContentConfig(
        contents=[document],
        system_instruction="You are an expert analyzing transcripts.",
    ),
)
print(cache)

response = client.models.generate_content(
    model=model_name,
    contents="Please summarize this transcript",
    config=types.GenerateContentConfig(cached_content=cache.name),
)
print(response.text)

نود جی اس

// Make sure to include the following import:
// import {GoogleGenAI} from '@google/genai';
const ai = new GoogleGenAI({ apiKey: process.env.GEMINI_API_KEY });
const filePath = path.join(media, "a11.txt");
const document = await ai.files.upload({
  file: filePath,
  config: { mimeType: "text/plain" },
});
console.log("Uploaded file name:", document.name);
const modelName = "gemini-1.5-flash-001";

const contents = [
  createUserContent(createPartFromUri(document.uri, document.mimeType)),
];

const cache = await ai.caches.create({
  model: modelName,
  config: {
    contents: contents,
    systemInstruction: "You are an expert analyzing transcripts.",
  },
});
console.log("Cache created:", cache);

const response = await ai.models.generateContent({
  model: modelName,
  contents: "Please summarize this transcript",
  config: { cachedContent: cache.name },
});
console.log("Response text:", response.text);

برو

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

modelName := "gemini-1.5-flash-001"
document, err := client.Files.UploadFromPath(
	ctx, 
	filepath.Join(getMedia(), "a11.txt"), 
	&genai.UploadFileConfig{
		MIMEType : "text/plain",
	},
)
if err != nil {
	log.Fatal(err)
}
parts := []*genai.Part{
	genai.NewPartFromURI(document.URI, document.MIMEType),
}
contents := []*genai.Content{
	genai.NewContentFromParts(parts, genai.RoleUser),
}
cache, err := client.Caches.Create(ctx, modelName, &genai.CreateCachedContentConfig{
	Contents: contents,
	SystemInstruction: genai.NewContentFromText(
		"You are an expert analyzing transcripts.", genai.RoleUser,
	),
})
if err != nil {
	log.Fatal(err)
}
fmt.Println("Cache created:")
fmt.Println(cache)

// Use the cache for generating content.
response, err := client.Models.GenerateContent(
	ctx,
	modelName,
	genai.Text("Please summarize this transcript"),
	&genai.GenerateContentConfig{
		CachedContent: cache.Name,
	},
)
if err != nil {
	log.Fatal(err)
}
printResponse(response)

مدل تنظیم‌شده

پایتون

# With Gemini 2 we're launching a new SDK. See the following doc for details.
# https://ai.google.dev/gemini-api/docs/migrate

حالت JSON

پایتون

from google import genai
from google.genai import types
from typing_extensions import TypedDict

class Recipe(TypedDict):
    recipe_name: str
    ingredients: list[str]

client = genai.Client()
result = client.models.generate_content(
    model="gemini-2.0-flash",
    contents="List a few popular cookie recipes.",
    config=types.GenerateContentConfig(
        response_mime_type="application/json", response_schema=list[Recipe]
    ),
)
print(result)

نود جی اس

// Make sure to include the following import:
// import {GoogleGenAI} from '@google/genai';
const ai = new GoogleGenAI({ apiKey: process.env.GEMINI_API_KEY });
const response = await ai.models.generateContent({
  model: "gemini-2.0-flash",
  contents: "List a few popular cookie recipes.",
  config: {
    responseMimeType: "application/json",
    responseSchema: {
      type: "array",
      items: {
        type: "object",
        properties: {
          recipeName: { type: "string" },
          ingredients: { type: "array", items: { type: "string" } },
        },
        required: ["recipeName", "ingredients"],
      },
    },
  },
});
console.log(response.text);

برو

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

schema := &genai.Schema{
	Type: genai.TypeArray,
	Items: &genai.Schema{
		Type: genai.TypeObject,
		Properties: map[string]*genai.Schema{
			"recipe_name": {Type: genai.TypeString},
			"ingredients": {
				Type:  genai.TypeArray,
				Items: &genai.Schema{Type: genai.TypeString},
			},
		},
		Required: []string{"recipe_name"},
	},
}

config := &genai.GenerateContentConfig{
	ResponseMIMEType: "application/json",
	ResponseSchema:   schema,
}

response, err := client.Models.GenerateContent(
	ctx,
	"gemini-2.0-flash",
	genai.Text("List a few popular cookie recipes."),
	config,
)
if err != nil {
	log.Fatal(err)
}
printResponse(response)

پوسته

curl "https://generativelanguage.googleapis.com/v1beta/models/gemini-2.0-flash:generateContent?key=$GEMINI_API_KEY" \
-H 'Content-Type: application/json' \
-d '{
    "contents": [{
      "parts":[
        {"text": "List 5 popular cookie recipes"}
        ]
    }],
    "generationConfig": {
        "response_mime_type": "application/json",
        "response_schema": {
          "type": "ARRAY",
          "items": {
            "type": "OBJECT",
            "properties": {
              "recipe_name": {"type":"STRING"},
            }
          }
        }
    }
}' 2> /dev/null | head

جاوا

Client client = new Client();

Schema recipeSchema = Schema.builder()
        .type(Array.class.getSimpleName())
        .items(Schema.builder()
                .type(Object.class.getSimpleName())
                .properties(
                        Map.of("recipe_name", Schema.builder()
                                        .type(String.class.getSimpleName())
                                        .build(),
                                "ingredients", Schema.builder()
                                        .type(Array.class.getSimpleName())
                                        .items(Schema.builder()
                                                .type(String.class.getSimpleName())
                                                .build())
                                        .build())
                )
                .required(List.of("recipe_name", "ingredients"))
                .build())
        .build();

GenerateContentConfig config =
        GenerateContentConfig.builder()
                .responseMimeType("application/json")
                .responseSchema(recipeSchema)
                .build();

GenerateContentResponse response =
        client.models.generateContent(
                "gemini-2.0-flash",
                "List a few popular cookie recipes.",
                config);

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

اجرای کد

پایتون

from google import genai
from google.genai import types

client = genai.Client()
response = client.models.generate_content(
    model="gemini-2.0-pro-exp-02-05",
    contents=(
        "Write and execute code that calculates the sum of the first 50 prime numbers. "
        "Ensure that only the executable code and its resulting output are generated."
    ),
)
# Each part may contain text, executable code, or an execution result.
for part in response.candidates[0].content.parts:
    print(part, "\n")

print("-" * 80)
# The .text accessor concatenates the parts into a markdown-formatted text.
print("\n", response.text)

برو

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

response, err := client.Models.GenerateContent(
	ctx,
	"gemini-2.0-pro-exp-02-05",
	genai.Text(
		`Write and execute code that calculates the sum of the first 50 prime numbers.
		 Ensure that only the executable code and its resulting output are generated.`,
	),
	&genai.GenerateContentConfig{},
)
if err != nil {
	log.Fatal(err)
}

// Print the response.
printResponse(response)

fmt.Println("--------------------------------------------------------------------------------")
fmt.Println(response.Text())

جاوا

Client client = new Client();

String prompt = """
        Write and execute code that calculates the sum of the first 50 prime numbers.
        Ensure that only the executable code and its resulting output are generated.
        """;

GenerateContentResponse response =
        client.models.generateContent(
                "gemini-2.0-pro-exp-02-05",
                prompt,
                null);

for (Part part : response.candidates().get().getFirst().content().get().parts().get()) {
    System.out.println(part + "\n");
}

System.out.println("-".repeat(80));
System.out.println(response.text());

فراخوانی تابع

پایتون

from google import genai
from google.genai import types

client = genai.Client()

def add(a: float, b: float) -> float:
    """returns a + b."""
    return a + b

def subtract(a: float, b: float) -> float:
    """returns a - b."""
    return a - b

def multiply(a: float, b: float) -> float:
    """returns a * b."""
    return a * b

def divide(a: float, b: float) -> float:
    """returns a / b."""
    return a / b

# Create a chat session; function calling (via tools) is enabled in the config.
chat = client.chats.create(
    model="gemini-2.0-flash",
    config=types.GenerateContentConfig(tools=[add, subtract, multiply, divide]),
)
response = chat.send_message(
    message="I have 57 cats, each owns 44 mittens, how many mittens is that in total?"
)
print(response.text)

برو

ctx := context.Background()
client, err := genai.NewClient(ctx, &genai.ClientConfig{
	APIKey:  os.Getenv("GEMINI_API_KEY"),
	Backend: genai.BackendGeminiAPI,
})
if err != nil {
	log.Fatal(err)
}
modelName := "gemini-2.0-flash"

// Create the function declarations for arithmetic operations.
addDeclaration := createArithmeticToolDeclaration("addNumbers", "Return the result of adding two numbers.")
subtractDeclaration := createArithmeticToolDeclaration("subtractNumbers", "Return the result of subtracting the second number from the first.")
multiplyDeclaration := createArithmeticToolDeclaration("multiplyNumbers", "Return the product of two numbers.")
divideDeclaration := createArithmeticToolDeclaration("divideNumbers", "Return the quotient of dividing the first number by the second.")

// Group the function declarations as a tool.
tools := []*genai.Tool{
	{
		FunctionDeclarations: []*genai.FunctionDeclaration{
			addDeclaration,
			subtractDeclaration,
			multiplyDeclaration,
			divideDeclaration,
		},
	},
}

// Create the content prompt.
contents := []*genai.Content{
	genai.NewContentFromText(
		"I have 57 cats, each owns 44 mittens, how many mittens is that in total?", genai.RoleUser,
	),
}

// Set up the generate content configuration with function calling enabled.
config := &genai.GenerateContentConfig{
	Tools: tools,
	ToolConfig: &genai.ToolConfig{
		FunctionCallingConfig: &genai.FunctionCallingConfig{
			// The mode equivalent to FunctionCallingConfigMode.ANY in JS.
			Mode: genai.FunctionCallingConfigModeAny,
		},
	},
}

genContentResp, err := client.Models.GenerateContent(ctx, modelName, contents, config)
if err != nil {
	log.Fatal(err)
}

// Assume the response includes a list of function calls.
if len(genContentResp.FunctionCalls()) == 0 {
	log.Println("No function call returned from the AI.")
	return nil
}
functionCall := genContentResp.FunctionCalls()[0]
log.Printf("Function call: %+v\n", functionCall)

// Marshal the Args map into JSON bytes.
argsMap, err := json.Marshal(functionCall.Args)
if err != nil {
	log.Fatal(err)
}

// Unmarshal the JSON bytes into the ArithmeticArgs struct.
var args ArithmeticArgs
if err := json.Unmarshal(argsMap, &args); err != nil {
	log.Fatal(err)
}

// Map the function name to the actual arithmetic function.
var result float64
switch functionCall.Name {
	case "addNumbers":
		result = add(args.FirstParam, args.SecondParam)
	case "subtractNumbers":
		result = subtract(args.FirstParam, args.SecondParam)
	case "multiplyNumbers":
		result = multiply(args.FirstParam, args.SecondParam)
	case "divideNumbers":
		result = divide(args.FirstParam, args.SecondParam)
	default:
		return fmt.Errorf("unimplemented function: %s", functionCall.Name)
}
log.Printf("Function result: %v\n", result)

// Prepare the final result message as content.
resultContents := []*genai.Content{
	genai.NewContentFromText("The final result is " + fmt.Sprintf("%v", result), genai.RoleUser),
}

// Use GenerateContent to send the final result.
finalResponse, err := client.Models.GenerateContent(ctx, modelName, resultContents, &genai.GenerateContentConfig{})
if err != nil {
	log.Fatal(err)
}

printResponse(finalResponse)

نود جی اس

  // Make sure to include the following import:
  // import {GoogleGenAI} from '@google/genai';
  const ai = new GoogleGenAI({ apiKey: process.env.GEMINI_API_KEY });

  /**
   * The add function returns the sum of two numbers.
   * @param {number} a
   * @param {number} b
   * @returns {number}
   */
  function add(a, b) {
    return a + b;
  }

  /**
   * The subtract function returns the difference (a - b).
   * @param {number} a
   * @param {number} b
   * @returns {number}
   */
  function subtract(a, b) {
    return a - b;
  }

  /**
   * The multiply function returns the product of two numbers.
   * @param {number} a
   * @param {number} b
   * @returns {number}
   */
  function multiply(a, b) {
    return a * b;
  }

  /**
   * The divide function returns the quotient of a divided by b.
   * @param {number} a
   * @param {number} b
   * @returns {number}
   */
  function divide(a, b) {
    return a / b;
  }

  const addDeclaration = {
    name: "addNumbers",
    parameters: {
      type: "object",
      description: "Return the result of adding two numbers.",
      properties: {
        firstParam: {
          type: "number",
          description:
            "The first parameter which can be an integer or a floating point number.",
        },
        secondParam: {
          type: "number",
          description:
            "The second parameter which can be an integer or a floating point number.",
        },
      },
      required: ["firstParam", "secondParam"],
    },
  };

  const subtractDeclaration = {
    name: "subtractNumbers",
    parameters: {
      type: "object",
      description:
        "Return the result of subtracting the second number from the first.",
      properties: {
        firstParam: {
          type: "number",
          description: "The first parameter.",
        },
        secondParam: {
          type: "number",
          description: "The second parameter.",
        },
      },
      required: ["firstParam", "secondParam"],
    },
  };

  const multiplyDeclaration = {
    name: "multiplyNumbers",
    parameters: {
      type: "object",
      description: "Return the product of two numbers.",
      properties: {
        firstParam: {
          type: "number",
          description: "The first parameter.",
        },
        secondParam: {
          type: "number",
          description: "The second parameter.",
        },
      },
      required: ["firstParam", "secondParam"],
    },
  };

  const divideDeclaration = {
    name: "divideNumbers",
    parameters: {
      type: "object",
      description:
        "Return the quotient of dividing the first number by the second.",
      properties: {
        firstParam: {
          type: "number",
          description: "The first parameter.",
        },
        secondParam: {
          type: "number",
          description: "The second parameter.",
        },
      },
      required: ["firstParam", "secondParam"],
    },
  };

  // Step 1: Call generateContent with function calling enabled.
  const generateContentResponse = await ai.models.generateContent({
    model: "gemini-2.0-flash",
    contents:
      "I have 57 cats, each owns 44 mittens, how many mittens is that in total?",
    config: {
      toolConfig: {
        functionCallingConfig: {
          mode: FunctionCallingConfigMode.ANY,
        },
      },
      tools: [
        {
          functionDeclarations: [
            addDeclaration,
            subtractDeclaration,
            multiplyDeclaration,
            divideDeclaration,
          ],
        },
      ],
    },
  });

  // Step 2: Extract the function call.(
  // Assuming the response contains a 'functionCalls' array.
  const functionCall =
    generateContentResponse.functionCalls &&
    generateContentResponse.functionCalls[0];
  console.log(functionCall);

  // Parse the arguments.
  const args = functionCall.args;
  // Expected args format: { firstParam: number, secondParam: number }

  // Step 3: Invoke the actual function based on the function name.
  const functionMapping = {
    addNumbers: add,
    subtractNumbers: subtract,
    multiplyNumbers: multiply,
    divideNumbers: divide,
  };
  const func = functionMapping[functionCall.name];
  if (!func) {
    console.error("Unimplemented error:", functionCall.name);
    return generateContentResponse;
  }
  const resultValue = func(args.firstParam, args.secondParam);
  console.log("Function result:", resultValue);

  // Step 4: Use the chat API to send the result as the final answer.
  const chat = ai.chats.create({ model: "gemini-2.0-flash" });
  const chatResponse = await chat.sendMessage({
    message: "The final result is " + resultValue,
  });
  console.log(chatResponse.text);
  return chatResponse;
}

پوسته


cat > tools.json << EOF
{
  "function_declarations": [
    {
      "name": "enable_lights",
      "description": "Turn on the lighting system."
    },
    {
      "name": "set_light_color",
      "description": "Set the light color. Lights must be enabled for this to work.",
      "parameters": {
        "type": "object",
        "properties": {
          "rgb_hex": {
            "type": "string",
            "description": "The light color as a 6-digit hex string, e.g. ff0000 for red."
          }
        },
        "required": [
          "rgb_hex"
        ]
      }
    },
    {
      "name": "stop_lights",
      "description": "Turn off the lighting system."
    }
  ]
} 
EOF

curl "https://generativelanguage.googleapis.com/v1beta/models/gemini-2.0-flash:generateContent?key=$GEMINI_API_KEY" \
  -H 'Content-Type: application/json' \
  -d @<(echo '
  {
    "system_instruction": {
      "parts": {
        "text": "You are a helpful lighting system bot. You can turn lights on and off, and you can set the color. Do not perform any other tasks."
      }
    },
    "tools": ['$(cat tools.json)'],

    "tool_config": {
      "function_calling_config": {"mode": "auto"}
    },

    "contents": {
      "role": "user",
      "parts": {
        "text": "Turn on the lights please."
      }
    }
  }
') 2>/dev/null |sed -n '/"content"/,/"finishReason"/p'

جاوا

Client client = new Client();

FunctionDeclaration addFunction =
        FunctionDeclaration.builder()
                .name("addNumbers")
                .parameters(
                        Schema.builder()
                                .type("object")
                                .properties(Map.of(
                                        "firstParam", Schema.builder().type("number").description("First number").build(),
                                        "secondParam", Schema.builder().type("number").description("Second number").build()))
                                .required(Arrays.asList("firstParam", "secondParam"))
                                .build())
                .build();

FunctionDeclaration subtractFunction =
        FunctionDeclaration.builder()
                .name("subtractNumbers")
                .parameters(
                        Schema.builder()
                                .type("object")
                                .properties(Map.of(
                                        "firstParam", Schema.builder().type("number").description("First number").build(),
                                        "secondParam", Schema.builder().type("number").description("Second number").build()))
                                .required(Arrays.asList("firstParam", "secondParam"))
                                .build())
                .build();

FunctionDeclaration multiplyFunction =
        FunctionDeclaration.builder()
                .name("multiplyNumbers")
                .parameters(
                        Schema.builder()
                                .type("object")
                                .properties(Map.of(
                                        "firstParam", Schema.builder().type("number").description("First number").build(),
                                        "secondParam", Schema.builder().type("number").description("Second number").build()))
                                .required(Arrays.asList("firstParam", "secondParam"))
                                .build())
                .build();

FunctionDeclaration divideFunction =
        FunctionDeclaration.builder()
                .name("divideNumbers")
                .parameters(
                        Schema.builder()
                                .type("object")
                                .properties(Map.of(
                                        "firstParam", Schema.builder().type("number").description("First number").build(),
                                        "secondParam", Schema.builder().type("number").description("Second number").build()))
                                .required(Arrays.asList("firstParam", "secondParam"))
                                .build())
                .build();

GenerateContentConfig config = GenerateContentConfig.builder()
        .toolConfig(ToolConfig.builder().functionCallingConfig(
                FunctionCallingConfig.builder().mode("ANY").build()
        ).build())
        .tools(
                Collections.singletonList(
                        Tool.builder().functionDeclarations(
                                Arrays.asList(
                                        addFunction,
                                        subtractFunction,
                                        divideFunction,
                                        multiplyFunction
                                )
                        ).build()

                )
        )
        .build();

GenerateContentResponse response =
        client.models.generateContent(
                "gemini-2.0-flash",
                "I have 57 cats, each owns 44 mittens, how many mittens is that in total?",
                config);


if (response.functionCalls() == null || response.functionCalls().isEmpty()) {
    System.err.println("No function call received");
    return null;
}

var functionCall = response.functionCalls().getFirst();
String functionName = functionCall.name().get();
var arguments = functionCall.args();

Map<String, BiFunction<Double, Double, Double>> functionMapping = new HashMap<>();
functionMapping.put("addNumbers", (a, b) -> a + b);
functionMapping.put("subtractNumbers", (a, b) -> a - b);
functionMapping.put("multiplyNumbers", (a, b) -> a * b);
functionMapping.put("divideNumbers", (a, b) -> b != 0 ? a / b : Double.NaN);

BiFunction<Double, Double, Double> function = functionMapping.get(functionName);

Number firstParam = (Number) arguments.get().get("firstParam");
Number secondParam = (Number) arguments.get().get("secondParam");
Double result = function.apply(firstParam.doubleValue(), secondParam.doubleValue());

System.out.println(result);

پیکربندی نسل

پایتون

from google import genai
from google.genai import types

client = genai.Client()
response = client.models.generate_content(
    model="gemini-2.0-flash",
    contents="Tell me a story about a magic backpack.",
    config=types.GenerateContentConfig(
        candidate_count=1,
        stop_sequences=["x"],
        max_output_tokens=20,
        temperature=1.0,
    ),
)
print(response.text)

نود جی اس

// Make sure to include the following import:
// import {GoogleGenAI} from '@google/genai';
const ai = new GoogleGenAI({ apiKey: process.env.GEMINI_API_KEY });

const response = await ai.models.generateContent({
  model: "gemini-2.0-flash",
  contents: "Tell me a story about a magic backpack.",
  config: {
    candidateCount: 1,
    stopSequences: ["x"],
    maxOutputTokens: 20,
    temperature: 1.0,
  },
});

console.log(response.text);

برو

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

// Create local variables for parameters.
candidateCount := int32(1)
maxOutputTokens := int32(20)
temperature := float32(1.0)

response, err := client.Models.GenerateContent(
	ctx,
	"gemini-2.0-flash",
	genai.Text("Tell me a story about a magic backpack."),
	&genai.GenerateContentConfig{
		CandidateCount:  candidateCount,
		StopSequences:   []string{"x"},
		MaxOutputTokens: maxOutputTokens,
		Temperature:     &temperature,
	},
)
if err != nil {
	log.Fatal(err)
}

printResponse(response)

پوسته

curl https://generativelanguage.googleapis.com/v1beta/models/gemini-2.0-flash:generateContent?key=$GEMINI_API_KEY \
    -H 'Content-Type: application/json' \
    -X POST \
    -d '{
        "contents": [{
            "parts":[
                {"text": "Explain how AI works"}
            ]
        }],
        "generationConfig": {
            "stopSequences": [
                "Title"
            ],
            "temperature": 1.0,
            "maxOutputTokens": 800,
            "topP": 0.8,
            "topK": 10
        }
    }'  2> /dev/null | grep "text"

جاوا

Client client = new Client();

GenerateContentConfig config =
        GenerateContentConfig.builder()
                .candidateCount(1)
                .stopSequences(List.of("x"))
                .maxOutputTokens(20)
                .temperature(1.0F)
                .build();

GenerateContentResponse response =
        client.models.generateContent(
                "gemini-2.0-flash",
                "Tell me a story about a magic backpack.",
                config);

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

تنظیمات ایمنی

پایتون

from google import genai
from google.genai import types

client = genai.Client()
unsafe_prompt = (
    "I support Martians Soccer Club and I think Jupiterians Football Club sucks! "
    "Write a ironic phrase about them including expletives."
)
response = client.models.generate_content(
    model="gemini-2.0-flash",
    contents=unsafe_prompt,
    config=types.GenerateContentConfig(
        safety_settings=[
            types.SafetySetting(
                category="HARM_CATEGORY_HATE_SPEECH",
                threshold="BLOCK_MEDIUM_AND_ABOVE",
            ),
            types.SafetySetting(
                category="HARM_CATEGORY_HARASSMENT", threshold="BLOCK_ONLY_HIGH"
            ),
        ]
    ),
)
try:
    print(response.text)
except Exception:
    print("No information generated by the model.")

print(response.candidates[0].safety_ratings)

نود جی اس

  // Make sure to include the following import:
  // import {GoogleGenAI} from '@google/genai';
  const ai = new GoogleGenAI({ apiKey: process.env.GEMINI_API_KEY });
  const unsafePrompt =
    "I support Martians Soccer Club and I think Jupiterians Football Club sucks! Write a ironic phrase about them including expletives.";

  const response = await ai.models.generateContent({
    model: "gemini-2.0-flash",
    contents: unsafePrompt,
    config: {
      safetySettings: [
        {
          category: "HARM_CATEGORY_HATE_SPEECH",
          threshold: "BLOCK_MEDIUM_AND_ABOVE",
        },
        {
          category: "HARM_CATEGORY_HARASSMENT",
          threshold: "BLOCK_ONLY_HIGH",
        },
      ],
    },
  });

  try {
    console.log("Generated text:", response.text);
  } catch (error) {
    console.log("No information generated by the model.");
  }
  console.log("Safety ratings:", response.candidates[0].safetyRatings);
  return response;
}

برو

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

unsafePrompt := "I support Martians Soccer Club and I think Jupiterians Football Club sucks! " +
	"Write a ironic phrase about them including expletives."

config := &genai.GenerateContentConfig{
	SafetySettings: []*genai.SafetySetting{
		{
			Category:  "HARM_CATEGORY_HATE_SPEECH",
			Threshold: "BLOCK_MEDIUM_AND_ABOVE",
		},
		{
			Category:  "HARM_CATEGORY_HARASSMENT",
			Threshold: "BLOCK_ONLY_HIGH",
		},
	},
}
contents := []*genai.Content{
	genai.NewContentFromText(unsafePrompt, genai.RoleUser),
}
response, err := client.Models.GenerateContent(ctx, "gemini-2.0-flash", contents, config)
if err != nil {
	log.Fatal(err)
}

// Print the generated text.
text := response.Text()
fmt.Println("Generated text:", text)

// Print the and safety ratings from the first candidate.
if len(response.Candidates) > 0 {
	fmt.Println("Finish reason:", response.Candidates[0].FinishReason)
	safetyRatings, err := json.MarshalIndent(response.Candidates[0].SafetyRatings, "", "  ")
	if err != nil {
		return err
	}
	fmt.Println("Safety ratings:", string(safetyRatings))
} else {
	fmt.Println("No candidate returned.")
}

پوسته

echo '{
    "safetySettings": [
        {"category": "HARM_CATEGORY_HARASSMENT", "threshold": "BLOCK_ONLY_HIGH"},
        {"category": "HARM_CATEGORY_HATE_SPEECH", "threshold": "BLOCK_MEDIUM_AND_ABOVE"}
    ],
    "contents": [{
        "parts":[{
            "text": "'I support Martians Soccer Club and I think Jupiterians Football Club sucks! Write a ironic phrase about them.'"}]}]}' > request.json

curl "https://generativelanguage.googleapis.com/v1beta/models/gemini-2.0-flash:generateContent?key=$GEMINI_API_KEY" \
    -H 'Content-Type: application/json' \
    -X POST \
    -d @request.json 2> /dev/null

جاوا

Client client = new Client();

String unsafePrompt = """
         I support Martians Soccer Club and I think Jupiterians Football Club sucks!
         Write a ironic phrase about them including expletives.
        """;

GenerateContentConfig config =
        GenerateContentConfig.builder()
                .safetySettings(Arrays.asList(
                        SafetySetting.builder()
                                .category("HARM_CATEGORY_HATE_SPEECH")
                                .threshold("BLOCK_MEDIUM_AND_ABOVE")
                                .build(),
                        SafetySetting.builder()
                                .category("HARM_CATEGORY_HARASSMENT")
                                .threshold("BLOCK_ONLY_HIGH")
                                .build()
                )).build();

GenerateContentResponse response =
        client.models.generateContent(
                "gemini-2.0-flash",
                unsafePrompt,
                config);

try {
    System.out.println(response.text());
} catch (Exception e) {
    System.out.println("No information generated by the model");
}

System.out.println(response.candidates().get().getFirst().safetyRatings());

دستورالعمل سیستم

پایتون

from google import genai
from google.genai import types

client = genai.Client()
response = client.models.generate_content(
    model="gemini-2.0-flash",
    contents="Good morning! How are you?",
    config=types.GenerateContentConfig(
        system_instruction="You are a cat. Your name is Neko."
    ),
)
print(response.text)

نود جی اس

// Make sure to include the following import:
// import {GoogleGenAI} from '@google/genai';
const ai = new GoogleGenAI({ apiKey: process.env.GEMINI_API_KEY });
const response = await ai.models.generateContent({
  model: "gemini-2.0-flash",
  contents: "Good morning! How are you?",
  config: {
    systemInstruction: "You are a cat. Your name is Neko.",
  },
});
console.log(response.text);

برو

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

// Construct the user message contents.
contents := []*genai.Content{
	genai.NewContentFromText("Good morning! How are you?", genai.RoleUser),
}

// Set the system instruction as a *genai.Content.
config := &genai.GenerateContentConfig{
	SystemInstruction: genai.NewContentFromText("You are a cat. Your name is Neko.", genai.RoleUser),
}

response, err := client.Models.GenerateContent(ctx, "gemini-2.0-flash", contents, config)
if err != nil {
	log.Fatal(err)
}
printResponse(response)

پوسته

curl "https://generativelanguage.googleapis.com/v1beta/models/gemini-2.0-flash:generateContent?key=$GEMINI_API_KEY" \
-H 'Content-Type: application/json' \
-d '{ "system_instruction": {
    "parts":
      { "text": "You are a cat. Your name is Neko."}},
    "contents": {
      "parts": {
        "text": "Hello there"}}}'

جاوا

Client client = new Client();

Part textPart = Part.builder().text("You are a cat. Your name is Neko.").build();

Content content = Content.builder().role("system").parts(ImmutableList.of(textPart)).build();

GenerateContentConfig config = GenerateContentConfig.builder()
        .systemInstruction(content)
        .build();

GenerateContentResponse response =
        client.models.generateContent(
                "gemini-2.0-flash",
                "Good morning! How are you?",
                config);

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

بدنه پاسخ

در صورت موفقیت، بدنه پاسخ شامل نمونه‌ای از GenerateContentResponse است.

روش: models.streamGenerateContent

با دریافت ورودی GenerateContentRequest ، یک پاسخ استریم‌شده از مدل تولید می‌کند.

نقطه پایانی

پست https: / /generativelanguage.googleapis.com /v1beta /{model=models /*}:streamGenerateContent

پارامترهای مسیر

string model

الزامی. نام Model که برای تولید تکمیل استفاده می‌شود.

قالب: models/{model} . این قالب به صورت models/{model} است.

درخواست بدنه

بدنه درخواست شامل داده‌هایی با ساختار زیر است:

فیلدها
contents[] object ( Content )

الزامی. محتوای مکالمه فعلی با مدل.

برای پرس‌وجوهای تک نوبتی، این یک نمونه واحد است. برای پرس‌وجوهای چند نوبتی مانند چت ، این یک فیلد تکراری است که شامل سابقه مکالمه و آخرین درخواست است.

tools[] object ( Tool )

اختیاری. فهرستی از Tools Model ممکن است برای تولید پاسخ بعدی استفاده کند.

یک Tool ، قطعه کدی است که سیستم را قادر می‌سازد تا با سیستم‌های خارجی تعامل داشته باشد تا یک یا مجموعه‌ای از اقدامات را خارج از دانش و محدوده Model انجام دهد. Tool پشتیبانی شده عبارتند از Function و codeExecution . برای کسب اطلاعات بیشتر به راهنماهای فراخوانی تابع ( Function calling) و اجرای کد (Code execution) مراجعه کنید.

شیء toolConfig object ( ToolConfig )

اختیاری. پیکربندی ابزار برای هر Tool که در درخواست مشخص شده است. برای مثال استفاده به راهنمای فراخوانی تابع مراجعه کنید.

شیء safetySettings[] object ( SafetySetting )

اختیاری. فهرستی از نمونه‌های منحصر به فرد SafetySetting برای مسدود کردن محتوای ناامن.

این مورد روی GenerateContentRequest.contents و GenerateContentResponse.candidates اعمال خواهد شد. برای هر نوع SafetyCategory نباید بیش از یک تنظیم وجود داشته باشد. API هر محتوا و پاسخی را که آستانه‌های تعیین‌شده توسط این تنظیمات را برآورده نکند، مسدود می‌کند. این لیست، تنظیمات پیش‌فرض برای هر SafetyCategory مشخص‌شده در safetySettings را لغو می‌کند. اگر هیچ SafetySetting برای یک SafetyCategory مشخص‌شده در لیست وجود نداشته باشد، API از تنظیم ایمنی پیش‌فرض برای آن دسته استفاده خواهد کرد. دسته‌های آسیب HARM_CATEGORY_HATE_SPEECH، HARM_CATEGORY_SEXUALLY_EXPLICIT، HARM_CATEGORY_DANGEROUS_CONTENT، HARM_CATEGORY_HARASSMENT، HARM_CATEGORY_CIVIC_INTEGRITY پشتیبانی می‌شوند. برای اطلاعات دقیق در مورد تنظیمات ایمنی موجود، به راهنما مراجعه کنید. همچنین برای یادگیری نحوه لحاظ کردن ملاحظات ایمنی در برنامه‌های هوش مصنوعی خود، به راهنمای ایمنی مراجعه کنید.

شیء systemInstruction object ( Content )

اختیاری. دستورالعمل(های) سیستم توسط توسعه‌دهنده تنظیم می‌شود. در حال حاضر، فقط متن.

شیء generationConfig object ( GenerationConfig )

اختیاری. گزینه‌های پیکربندی برای تولید مدل و خروجی‌ها.

string cachedContent

اختیاری. نام محتوای ذخیره شده برای استفاده به عنوان زمینه برای ارائه پیش‌بینی. قالب: cachedContents/{cachedContent}

درخواست نمونه

متن

پایتون

from google import genai

client = genai.Client()
response = client.models.generate_content_stream(
    model="gemini-2.0-flash", contents="Write a story about a magic backpack."
)
for chunk in response:
    print(chunk.text)
    print("_" * 80)

نود جی اس

// Make sure to include the following import:
// import {GoogleGenAI} from '@google/genai';
const ai = new GoogleGenAI({ apiKey: process.env.GEMINI_API_KEY });

const response = await ai.models.generateContentStream({
  model: "gemini-2.0-flash",
  contents: "Write a story about a magic backpack.",
});
let text = "";
for await (const chunk of response) {
  console.log(chunk.text);
  text += chunk.text;
}

برو

ctx := context.Background()
client, err := genai.NewClient(ctx, &genai.ClientConfig{
	APIKey:  os.Getenv("GEMINI_API_KEY"),
	Backend: genai.BackendGeminiAPI,
})
if err != nil {
	log.Fatal(err)
}
contents := []*genai.Content{
	genai.NewContentFromText("Write a story about a magic backpack.", genai.RoleUser),
}
for response, err := range client.Models.GenerateContentStream(
	ctx,
	"gemini-2.0-flash",
	contents,
	nil,
) {
	if err != nil {
		log.Fatal(err)
	}
	fmt.Print(response.Candidates[0].Content.Parts[0].Text)
}

پوسته

curl "https://generativelanguage.googleapis.com/v1beta/models/gemini-2.0-flash:streamGenerateContent?alt=sse&key=${GEMINI_API_KEY}" \
        -H 'Content-Type: application/json' \
        --no-buffer \
        -d '{ "contents":[{"parts":[{"text": "Write a story about a magic backpack."}]}]}'

جاوا

Client client = new Client();

ResponseStream<GenerateContentResponse> responseStream =
        client.models.generateContentStream(
                "gemini-2.0-flash",
                "Write a story about a magic backpack.",
                null);

StringBuilder response = new StringBuilder();
for (GenerateContentResponse res : responseStream) {
    System.out.print(res.text());
    response.append(res.text());
}

responseStream.close();

تصویر

پایتون

from google import genai
import PIL.Image

client = genai.Client()
organ = PIL.Image.open(media / "organ.jpg")
response = client.models.generate_content_stream(
    model="gemini-2.0-flash", contents=["Tell me about this instrument", organ]
)
for chunk in response:
    print(chunk.text)
    print("_" * 80)

نود جی اس

// Make sure to include the following import:
// import {GoogleGenAI} from '@google/genai';
const ai = new GoogleGenAI({ apiKey: process.env.GEMINI_API_KEY });

const organ = await ai.files.upload({
  file: path.join(media, "organ.jpg"),
});

const response = await ai.models.generateContentStream({
  model: "gemini-2.0-flash",
  contents: [
    createUserContent([
      "Tell me about this instrument", 
      createPartFromUri(organ.uri, organ.mimeType)
    ]),
  ],
});
let text = "";
for await (const chunk of response) {
  console.log(chunk.text);
  text += chunk.text;
}

برو

ctx := context.Background()
client, err := genai.NewClient(ctx, &genai.ClientConfig{
	APIKey:  os.Getenv("GEMINI_API_KEY"),
	Backend: genai.BackendGeminiAPI,
})
if err != nil {
	log.Fatal(err)
}
file, err := client.Files.UploadFromPath(
	ctx, 
	filepath.Join(getMedia(), "organ.jpg"), 
	&genai.UploadFileConfig{
		MIMEType : "image/jpeg",
	},
)
if err != nil {
	log.Fatal(err)
}
parts := []*genai.Part{
	genai.NewPartFromText("Tell me about this instrument"),
	genai.NewPartFromURI(file.URI, file.MIMEType),
}
contents := []*genai.Content{
	genai.NewContentFromParts(parts, genai.RoleUser),
}
for response, err := range client.Models.GenerateContentStream(
	ctx,
	"gemini-2.0-flash",
	contents,
	nil,
) {
	if err != nil {
		log.Fatal(err)
	}
	fmt.Print(response.Candidates[0].Content.Parts[0].Text)
}

پوسته

cat > "$TEMP_JSON" << EOF
{
  "contents": [{
    "parts":[
      {"text": "Tell me about this instrument"},
      {
        "inline_data": {
          "mime_type":"image/jpeg",
          "data": "$(cat "$TEMP_B64")"
        }
      }
    ]
  }]
}
EOF

curl "https://generativelanguage.googleapis.com/v1beta/models/gemini-2.0-flash:streamGenerateContent?alt=sse&key=$GEMINI_API_KEY" \
    -H 'Content-Type: application/json' \
    -X POST \
    -d "@$TEMP_JSON" 2> /dev/null

جاوا

Client client = new Client();

String path = media_path + "organ.jpg";
byte[] imageData = Files.readAllBytes(Paths.get(path));

Content content =
        Content.fromParts(
                Part.fromText("Tell me about this instrument."),
                Part.fromBytes(imageData, "image/jpeg"));


ResponseStream<GenerateContentResponse> responseStream =
        client.models.generateContentStream(
                "gemini-2.0-flash",
                content,
                null);

StringBuilder response = new StringBuilder();
for (GenerateContentResponse res : responseStream) {
    System.out.print(res.text());
    response.append(res.text());
}

responseStream.close();

صوتی

پایتون

from google import genai

client = genai.Client()
sample_audio = client.files.upload(file=media / "sample.mp3")
response = client.models.generate_content_stream(
    model="gemini-2.0-flash",
    contents=["Give me a summary of this audio file.", sample_audio],
)
for chunk in response:
    print(chunk.text)
    print("_" * 80)

برو

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

file, err := client.Files.UploadFromPath(
	ctx, 
	filepath.Join(getMedia(), "sample.mp3"), 
	&genai.UploadFileConfig{
		MIMEType : "audio/mpeg",
	},
)
if err != nil {
	log.Fatal(err)
}

parts := []*genai.Part{
	genai.NewPartFromText("Give me a summary of this audio file."),
	genai.NewPartFromURI(file.URI, file.MIMEType),
}

contents := []*genai.Content{
	genai.NewContentFromParts(parts, genai.RoleUser),
}

for result, err := range client.Models.GenerateContentStream(
	ctx,
	"gemini-2.0-flash",
	contents,
	nil,
) {
	if err != nil {
		log.Fatal(err)
	}
	fmt.Print(result.Candidates[0].Content.Parts[0].Text)
}

پوسته

# Use File API to upload audio data to API request.
MIME_TYPE=$(file -b --mime-type "${AUDIO_PATH}")
NUM_BYTES=$(wc -c < "${AUDIO_PATH}")
DISPLAY_NAME=AUDIO

tmp_header_file=upload-header.tmp

# Initial resumable request defining metadata.
# The upload url is in the response headers dump them to a file.
curl "${BASE_URL}/upload/v1beta/files?key=${GEMINI_API_KEY}" \
  -D upload-header.tmp \
  -H "X-Goog-Upload-Protocol: resumable" \
  -H "X-Goog-Upload-Command: start" \
  -H "X-Goog-Upload-Header-Content-Length: ${NUM_BYTES}" \
  -H "X-Goog-Upload-Header-Content-Type: ${MIME_TYPE}" \
  -H "Content-Type: application/json" \
  -d "{'file': {'display_name': '${DISPLAY_NAME}'}}" 2> /dev/null

upload_url=$(grep -i "x-goog-upload-url: " "${tmp_header_file}" | cut -d" " -f2 | tr -d "\r")
rm "${tmp_header_file}"

# Upload the actual bytes.
curl "${upload_url}" \
  -H "Content-Length: ${NUM_BYTES}" \
  -H "X-Goog-Upload-Offset: 0" \
  -H "X-Goog-Upload-Command: upload, finalize" \
  --data-binary "@${AUDIO_PATH}" 2> /dev/null > file_info.json

file_uri=$(jq ".file.uri" file_info.json)
echo file_uri=$file_uri

curl "https://generativelanguage.googleapis.com/v1beta/models/gemini-2.0-flash:streamGenerateContent?alt=sse&key=$GEMINI_API_KEY" \
    -H 'Content-Type: application/json' \
    -X POST \
    -d '{
      "contents": [{
        "parts":[
          {"text": "Please describe this file."},
          {"file_data":{"mime_type": "audio/mpeg", "file_uri": '$file_uri'}}]
        }]
       }' 2> /dev/null > response.json

cat response.json
echo

ویدئو

پایتون

from google import genai
import time

client = genai.Client()
# Video clip (CC BY 3.0) from https://peach.blender.org/download/
myfile = client.files.upload(file=media / "Big_Buck_Bunny.mp4")
print(f"{myfile=}")

# Poll until the video file is completely processed (state becomes ACTIVE).
while not myfile.state or myfile.state.name != "ACTIVE":
    print("Processing video...")
    print("File state:", myfile.state)
    time.sleep(5)
    myfile = client.files.get(name=myfile.name)

response = client.models.generate_content_stream(
    model="gemini-2.0-flash", contents=[myfile, "Describe this video clip"]
)
for chunk in response:
    print(chunk.text)
    print("_" * 80)

نود جی اس

// Make sure to include the following import:
// import {GoogleGenAI} from '@google/genai';
const ai = new GoogleGenAI({ apiKey: process.env.GEMINI_API_KEY });

let video = await ai.files.upload({
  file: path.join(media, 'Big_Buck_Bunny.mp4'),
});

// Poll until the video file is completely processed (state becomes ACTIVE).
while (!video.state || video.state.toString() !== 'ACTIVE') {
  console.log('Processing video...');
  console.log('File state: ', video.state);
  await sleep(5000);
  video = await ai.files.get({name: video.name});
}

const response = await ai.models.generateContentStream({
  model: "gemini-2.0-flash",
  contents: [
    createUserContent([
      "Describe this video clip",
      createPartFromUri(video.uri, video.mimeType),
    ]),
  ],
});
let text = "";
for await (const chunk of response) {
  console.log(chunk.text);
  text += chunk.text;
}

برو

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

file, err := client.Files.UploadFromPath(
	ctx, 
	filepath.Join(getMedia(), "Big_Buck_Bunny.mp4"), 
	&genai.UploadFileConfig{
		MIMEType : "video/mp4",
	},
)
if err != nil {
	log.Fatal(err)
}

// Poll until the video file is completely processed (state becomes ACTIVE).
for file.State == genai.FileStateUnspecified || file.State != genai.FileStateActive {
	fmt.Println("Processing video...")
	fmt.Println("File state:", file.State)
	time.Sleep(5 * time.Second)

	file, err = client.Files.Get(ctx, file.Name, nil)
	if err != nil {
		log.Fatal(err)
	}
}

parts := []*genai.Part{
	genai.NewPartFromText("Describe this video clip"),
	genai.NewPartFromURI(file.URI, file.MIMEType),
}

contents := []*genai.Content{
	genai.NewContentFromParts(parts, genai.RoleUser),
}

for result, err := range client.Models.GenerateContentStream(
	ctx,
	"gemini-2.0-flash",
	contents,
	nil,
) {
	if err != nil {
		log.Fatal(err)
	}
	fmt.Print(result.Candidates[0].Content.Parts[0].Text)
}

پوسته

# Use File API to upload audio data to API request.
MIME_TYPE=$(file -b --mime-type "${VIDEO_PATH}")
NUM_BYTES=$(wc -c < "${VIDEO_PATH}")
DISPLAY_NAME=VIDEO_PATH

# Initial resumable request defining metadata.
# The upload url is in the response headers dump them to a file.
curl "${BASE_URL}/upload/v1beta/files?key=${GEMINI_API_KEY}" \
  -D upload-header.tmp \
  -H "X-Goog-Upload-Protocol: resumable" \
  -H "X-Goog-Upload-Command: start" \
  -H "X-Goog-Upload-Header-Content-Length: ${NUM_BYTES}" \
  -H "X-Goog-Upload-Header-Content-Type: ${MIME_TYPE}" \
  -H "Content-Type: application/json" \
  -d "{'file': {'display_name': '${DISPLAY_NAME}'}}" 2> /dev/null

upload_url=$(grep -i "x-goog-upload-url: " "${tmp_header_file}" | cut -d" " -f2 | tr -d "\r")
rm "${tmp_header_file}"

# Upload the actual bytes.
curl "${upload_url}" \
  -H "Content-Length: ${NUM_BYTES}" \
  -H "X-Goog-Upload-Offset: 0" \
  -H "X-Goog-Upload-Command: upload, finalize" \
  --data-binary "@${VIDEO_PATH}" 2> /dev/null > file_info.json

file_uri=$(jq ".file.uri" file_info.json)
echo file_uri=$file_uri

state=$(jq ".file.state" file_info.json)
echo state=$state

while [[ "($state)" = *"PROCESSING"* ]];
do
  echo "Processing video..."
  sleep 5
  # Get the file of interest to check state
  curl https://generativelanguage.googleapis.com/v1beta/files/$name > file_info.json
  state=$(jq ".file.state" file_info.json)
done

curl "https://generativelanguage.googleapis.com/v1beta/models/gemini-2.0-flash:streamGenerateContent?alt=sse&key=$GEMINI_API_KEY" \
    -H 'Content-Type: application/json' \
    -X POST \
    -d '{
      "contents": [{
        "parts":[
          {"text": "Please describe this file."},
          {"file_data":{"mime_type": "video/mp4", "file_uri": '$file_uri'}}]
        }]
       }' 2> /dev/null > response.json

cat response.json
echo

پی دی اف

پایتون

from google import genai

client = genai.Client()
sample_pdf = client.files.upload(file=media / "test.pdf")
response = client.models.generate_content_stream(
    model="gemini-2.0-flash",
    contents=["Give me a summary of this document:", sample_pdf],
)

for chunk in response:
    print(chunk.text)
    print("_" * 80)

برو

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

file, err := client.Files.UploadFromPath(
	ctx, 
	filepath.Join(getMedia(), "test.pdf"), 
	&genai.UploadFileConfig{
		MIMEType : "application/pdf",
	},
)
if err != nil {
	log.Fatal(err)
}

parts := []*genai.Part{
	genai.NewPartFromText("Give me a summary of this document:"),
	genai.NewPartFromURI(file.URI, file.MIMEType),
}

contents := []*genai.Content{
	genai.NewContentFromParts(parts, genai.RoleUser),
}

for result, err := range client.Models.GenerateContentStream(
	ctx,
	"gemini-2.0-flash",
	contents,
	nil,
) {
	if err != nil {
		log.Fatal(err)
	}
	fmt.Print(result.Candidates[0].Content.Parts[0].Text)
}

پوسته

MIME_TYPE=$(file -b --mime-type "${PDF_PATH}")
NUM_BYTES=$(wc -c < "${PDF_PATH}")
DISPLAY_NAME=TEXT


echo $MIME_TYPE
tmp_header_file=upload-header.tmp

# Initial resumable request defining metadata.
# The upload url is in the response headers dump them to a file.
curl "${BASE_URL}/upload/v1beta/files?key=${GEMINI_API_KEY}" \
  -D upload-header.tmp \
  -H "X-Goog-Upload-Protocol: resumable" \
  -H "X-Goog-Upload-Command: start" \
  -H "X-Goog-Upload-Header-Content-Length: ${NUM_BYTES}" \
  -H "X-Goog-Upload-Header-Content-Type: ${MIME_TYPE}" \
  -H "Content-Type: application/json" \
  -d "{'file': {'display_name': '${DISPLAY_NAME}'}}" 2> /dev/null

upload_url=$(grep -i "x-goog-upload-url: " "${tmp_header_file}" | cut -d" " -f2 | tr -d "\r")
rm "${tmp_header_file}"

# Upload the actual bytes.
curl "${upload_url}" \
  -H "Content-Length: ${NUM_BYTES}" \
  -H "X-Goog-Upload-Offset: 0" \
  -H "X-Goog-Upload-Command: upload, finalize" \
  --data-binary "@${PDF_PATH}" 2> /dev/null > file_info.json

file_uri=$(jq ".file.uri" file_info.json)
echo file_uri=$file_uri

# Now generate content using that file
curl "https://generativelanguage.googleapis.com/v1beta/models/gemini-2.0-flash:streamGenerateContent?alt=sse&key=$GEMINI_API_KEY" \
    -H 'Content-Type: application/json' \
    -X POST \
    -d '{
      "contents": [{
        "parts":[
          {"text": "Can you add a few more lines to this poem?"},
          {"file_data":{"mime_type": "application/pdf", "file_uri": '$file_uri'}}]
        }]
       }' 2> /dev/null > response.json

cat response.json
echo

چت

پایتون

from google import genai
from google.genai import types

client = genai.Client()
chat = client.chats.create(
    model="gemini-2.0-flash",
    history=[
        types.Content(role="user", parts=[types.Part(text="Hello")]),
        types.Content(
            role="model",
            parts=[
                types.Part(
                    text="Great to meet you. What would you like to know?"
                )
            ],
        ),
    ],
)
response = chat.send_message_stream(message="I have 2 dogs in my house.")
for chunk in response:
    print(chunk.text)
    print("_" * 80)
response = chat.send_message_stream(message="How many paws are in my house?")
for chunk in response:
    print(chunk.text)
    print("_" * 80)

print(chat.get_history())

نود جی اس

// Make sure to include the following import:
// import {GoogleGenAI} from '@google/genai';
const ai = new GoogleGenAI({ apiKey: process.env.GEMINI_API_KEY });
const chat = ai.chats.create({
  model: "gemini-2.0-flash",
  history: [
    {
      role: "user",
      parts: [{ text: "Hello" }],
    },
    {
      role: "model",
      parts: [{ text: "Great to meet you. What would you like to know?" }],
    },
  ],
});

console.log("Streaming response for first message:");
const stream1 = await chat.sendMessageStream({
  message: "I have 2 dogs in my house.",
});
for await (const chunk of stream1) {
  console.log(chunk.text);
  console.log("_".repeat(80));
}

console.log("Streaming response for second message:");
const stream2 = await chat.sendMessageStream({
  message: "How many paws are in my house?",
});
for await (const chunk of stream2) {
  console.log(chunk.text);
  console.log("_".repeat(80));
}

console.log(chat.getHistory());

برو

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

history := []*genai.Content{
	genai.NewContentFromText("Hello", genai.RoleUser),
	genai.NewContentFromText("Great to meet you. What would you like to know?", genai.RoleModel),
}
chat, err := client.Chats.Create(ctx, "gemini-2.0-flash", nil, history)
if err != nil {
	log.Fatal(err)
}

for chunk, err := range chat.SendMessageStream(ctx, genai.Part{Text: "I have 2 dogs in my house."}) {
	if err != nil {
		log.Fatal(err)
	}
	fmt.Println(chunk.Text())
	fmt.Println(strings.Repeat("_", 64))
}

for chunk, err := range chat.SendMessageStream(ctx, genai.Part{Text: "How many paws are in my house?"}) {
	if err != nil {
		log.Fatal(err)
	}
	fmt.Println(chunk.Text())
	fmt.Println(strings.Repeat("_", 64))
}

fmt.Println(chat.History(false))

پوسته

curl https://generativelanguage.googleapis.com/v1beta/models/gemini-2.0-flash:streamGenerateContent?alt=sse&key=$GEMINI_API_KEY \
    -H 'Content-Type: application/json' \
    -X POST \
    -d '{
      "contents": [
        {"role":"user",
         "parts":[{
           "text": "Hello"}]},
        {"role": "model",
         "parts":[{
           "text": "Great to meet you. What would you like to know?"}]},
        {"role":"user",
         "parts":[{
           "text": "I have two dogs in my house. How many paws are in my house?"}]},
      ]
    }' 2> /dev/null | grep "text"

بدنه پاسخ

در صورت موفقیت، بدنه پاسخ شامل جریانی از نمونه‌های GenerateContentResponse است.

تولید پاسخ محتوا

پاسخ از مدلی که از پاسخ‌های کاندید چندگانه پشتیبانی می‌کند.

رتبه‌بندی‌های ایمنی و فیلترینگ محتوا برای هر دو مورد در GenerateContentResponse.prompt_feedback و برای هر کاندید در finishReason و safetyRatings گزارش می‌شوند. API: - یا همه کاندیدهای درخواستی یا هیچ‌کدام از آنها را برمی‌گرداند. - فقط در صورتی که مشکلی در اعلان وجود داشته باشد، هیچ کاندیدی را برنمی‌گرداند ( promptFeedback بررسی کنید). - بازخورد مربوط به هر کاندید را در finishReason و safetyRatings گزارش می‌دهد.

فیلدها
object ( Candidate ) candidates[] (نامزد)

پاسخ‌های کاندیداها از مدل.

object ( PromptFeedback ) promptFeedback (بازخورد سریع)

بازخورد مربوط به فیلترهای محتوا را برمی‌گرداند.

شیء usageMetadata object ( UsageMetadata )

فقط خروجی. فراداده در مورد استفاده از توکن در درخواست‌های تولید.

string modelVersion

فقط خروجی. نسخه مدل مورد استفاده برای تولید پاسخ.

string responseId

فقط خروجی. responseId برای شناسایی هر پاسخ استفاده می‌شود.

نمایش JSON
{
  "candidates": [
    {
      object (Candidate)
    }
  ],
  "promptFeedback": {
    object (PromptFeedback)
  },
  "usageMetadata": {
    object (UsageMetadata)
  },
  "modelVersion": string,
  "responseId": string
}

بازخورد سریع

مجموعه‌ای از فراداده‌های بازخورد که در اعلان GenerateContentRequest.content مشخص شده‌اند.

فیلدها
شمارشی blockReason enum ( BlockReason )

اختیاری. در صورت تنظیم، اعلان مسدود شده و هیچ نامزدی بازگردانده نمی‌شود. اعلان را به صورت دیگری بنویسید.

شیء safetyRatings[] object ( SafetyRating )

رتبه‌بندی‌ها برای ایمنی سوال. حداکثر یک رتبه‌بندی برای هر دسته وجود دارد.

نمایش JSON
{
  "blockReason": enum (BlockReason),
  "safetyRatings": [
    {
      object (SafetyRating)
    }
  ]
}

بلاک‌ریجن

دلیل مسدود شدن اعلان را مشخص می‌کند.

انوم‌ها
BLOCK_REASON_UNSPECIFIED مقدار پیش‌فرض. این مقدار استفاده نشده است.
SAFETY به دلایل ایمنی، درخواست مسدود شد. برای فهمیدن اینکه کدام دسته از دسته‌بندی‌های ایمنی آن را مسدود کرده است، safetyRatings بررسی کنید.
OTHER به دلایل نامعلومی، پیام رسان مسدود شد.
BLOCKLIST به دلیل وجود اصطلاحاتی که در فهرست اصطلاحات مسدود شده وجود دارند، درخواست مسدود شد.
PROHIBITED_CONTENT به دلیل محتوای ممنوعه، اعلان مسدود شد.
IMAGE_SAFETY کاندیداها به دلیل محتوای تولید تصویر ناامن مسدود شدند.

کاربردفراداده

فراداده در مورد استفاده از توکن درخواست تولید.

فیلدها
integer promptTokenCount

تعداد توکن‌های موجود در اعلان. وقتی cachedContent تنظیم شده باشد، این مقدار همچنان اندازه کل مؤثر اعلان است، به این معنی که شامل تعداد توکن‌های موجود در محتوای ذخیره شده نیز می‌شود.

integer cachedContentTokenCount

تعداد توکن‌ها در بخش ذخیره‌شده‌ی اعلان (محتوای ذخیره‌شده)

integer candidatesTokenCount

تعداد کل توکن‌ها در بین تمام کاندیدهای پاسخ تولید شده.

toolUsePromptTokenCount integer

فقط خروجی. تعداد توکن‌های موجود در اعلان(های) استفاده از ابزار.

integer thoughtsTokenCount

فقط خروجی. تعداد توکن‌های افکار برای مدل‌های تفکر.

integer totalTokenCount

تعداد کل توکن‌ها برای درخواست تولید (نامزدهای اعلان + پاسخ).

object ( ModalityTokenCount ) promptTokensDetails[] (تعداد توکن‌های مدالیتی)

فقط خروجی. فهرست روش‌هایی که در ورودی درخواست پردازش شده‌اند.

object ( ModalityTokenCount ) cacheTokensDetails[] (تعداد توکن‌های مدالیتی)

فقط خروجی. فهرستی از روش‌های محتوای ذخیره‌شده در ورودی درخواست.

شیء candidatesTokensDetails[] object ( ModalityTokenCount )

فقط خروجی. فهرست روش‌هایی که در پاسخ برگردانده شده‌اند.

شیء toolUsePromptTokensDetails[] object ( ModalityTokenCount )

فقط خروجی. فهرست روش‌هایی که برای ورودی‌های درخواست استفاده از ابزار پردازش شده‌اند.

نمایش JSON
{
  "promptTokenCount": integer,
  "cachedContentTokenCount": integer,
  "candidatesTokenCount": integer,
  "toolUsePromptTokenCount": integer,
  "thoughtsTokenCount": integer,
  "totalTokenCount": integer,
  "promptTokensDetails": [
    {
      object (ModalityTokenCount)
    }
  ],
  "cacheTokensDetails": [
    {
      object (ModalityTokenCount)
    }
  ],
  "candidatesTokensDetails": [
    {
      object (ModalityTokenCount)
    }
  ],
  "toolUsePromptTokensDetails": [
    {
      object (ModalityTokenCount)
    }
  ]
}

نامزد

یک کاندید پاسخ که از مدل تولید شده است.

فیلدها
شیء content object ( Content )

فقط خروجی. محتوای تولید شده از مدل برگردانده می‌شود.

شمارشی finishReason enum ( FinishReason )

اختیاری. فقط خروجی. دلیل اینکه مدل تولید توکن‌ها را متوقف کرد.

اگر خالی باشد، مدل تولید توکن‌ها را متوقف نکرده است.

شیء safetyRatings[] object ( SafetyRating )

فهرست رتبه‌بندی‌ها برای ایمنی یک کاندیدای پاسخ.

حداکثر یک رتبه‌بندی برای هر دسته وجود دارد.

شیء فراداده citationMetadata object ( CitationMetadata )

فقط خروجی. اطلاعات استناد برای کاندیدای تولید شده توسط مدل.

این فیلد می‌تواند با اطلاعات تلاوت برای هر متنی که در content وجود دارد، پر شود. اینها قطعاتی هستند که از مطالب دارای حق چاپ در داده‌های آموزشی LLM پایه "تلاوت" می‌شوند.

integer tokenCount

فقط خروجی. تعداد توکن‌ها برای این نامزد.

شیء groundingAttributions[] object ( GroundingAttribution )

فقط خروجی. اطلاعات انتساب منابعی که در ارائه پاسخی مستدل نقش داشته‌اند.

این فیلد برای فراخوانی‌های GenerateAnswer پر می‌شود.

شیء groundingMetadata object ( GroundingMetadata )

فقط خروجی. ابرداده پایه برای کاندیدا.

این فیلد برای فراخوانی‌های GenerateContent پر می‌شود.

number avgLogprobs

فقط خروجی. میانگین لگاریتم نمره احتمال داوطلب.

شیء logprobsResult object ( LogprobsResult )

فقط خروجی. نمرات لگاریتم درستنمایی برای توکن‌های پاسخ و توکن‌های برتر

شیء urlContextMetadata object ( UrlContextMetadata )

فقط خروجی. فراداده مربوط به ابزار بازیابی متن url.

integer index

فقط خروجی. فهرست کاندیدا در فهرست کاندیداهای پاسخ.

string finishMessage

اختیاری. فقط خروجی. دلیل توقف تولید توکن‌ها توسط مدل را شرح می‌دهد. این مقدار فقط زمانی پر می‌شود که finishReason تنظیم شده باشد.

نمایش JSON
{
  "content": {
    object (Content)
  },
  "finishReason": enum (FinishReason),
  "safetyRatings": [
    {
      object (SafetyRating)
    }
  ],
  "citationMetadata": {
    object (CitationMetadata)
  },
  "tokenCount": integer,
  "groundingAttributions": [
    {
      object (GroundingAttribution)
    }
  ],
  "groundingMetadata": {
    object (GroundingMetadata)
  },
  "avgLogprobs": number,
  "logprobsResult": {
    object (LogprobsResult)
  },
  "urlContextMetadata": {
    object (UrlContextMetadata)
  },
  "index": integer,
  "finishMessage": string
}

دلیل پایان

دلیل توقف تولید توکن‌ها توسط مدل را تعریف می‌کند.

انوم‌ها
FINISH_REASON_UNSPECIFIED مقدار پیش‌فرض. این مقدار استفاده نشده است.
STOP نقطه توقف طبیعی مدل یا توالی توقف ارائه شده.
MAX_TOKENS حداکثر تعداد توکن‌ها همانطور که در درخواست مشخص شده بود، حاصل شد.
SAFETY محتوای کاندید پاسخ به دلایل ایمنی علامت‌گذاری شد.
RECITATION محتوای کاندید پاسخ به دلایل تکرار علامت‌گذاری شد.
LANGUAGE محتوای کاندید پاسخ به دلیل استفاده از زبانی که پشتیبانی نمی‌شود، علامت‌گذاری شد.
OTHER دلیل نامعلوم.
BLOCKLIST تولید توکن متوقف شد زیرا محتوا حاوی عبارات ممنوعه است.
PROHIBITED_CONTENT تولید توکن به دلیل احتمال وجود محتوای ممنوعه متوقف شد.
SPII تولید توکن متوقف شد زیرا محتوا احتمالاً حاوی اطلاعات حساس قابل شناسایی شخصی (SPII) است.
MALFORMED_FUNCTION_CALL فراخوانی تابع تولید شده توسط مدل نامعتبر است.
IMAGE_SAFETY تولید توکن متوقف شد زیرا تصاویر تولید شده حاوی موارد نقض ایمنی بودند.
IMAGE_PROHIBITED_CONTENT تولید تصویر متوقف شد زیرا تصاویر تولید شده حاوی محتوای ممنوعه دیگری بودند.
IMAGE_OTHER تولید تصویر به دلیل مشکلات متفرقه دیگر متوقف شد.
NO_IMAGE انتظار می‌رفت که این مدل یک تصویر ایجاد کند، اما هیچ تصویری ایجاد نشد.
IMAGE_RECITATION تولید تصویر به دلیل تلاوت متوقف شد.
UNEXPECTED_TOOL_CALL مدل یک فراخوانی ابزار ایجاد کرد اما هیچ ابزاری در درخواست فعال نشد.
TOO_MANY_TOOL_CALLS مدل، ابزارهای زیادی را پشت سر هم فراخوانی کرد، بنابراین سیستم از اجرا خارج شد.

انتساب زمینی

انتساب منبعی که در پاسخ به یک سوال نقش داشته است.

فیلدها
شیء sourceId object ( AttributionSourceId )

فقط خروجی. شناسه منبعی که در این انتساب مشارکت داشته است.

شیء content object ( Content )

محتوای منبع پایه که این انتساب را تشکیل می‌دهد.

نمایش JSON
{
  "sourceId": {
    object (AttributionSourceId)
  },
  "content": {
    object (Content)
  }
}

شناسه منبع انتساب

شناسه منبعی که در این انتساب مشارکت داشته است.

فیلدها
Union type source اتحادیه
source می‌تواند فقط یکی از موارد زیر باشد:
شیء groundingPassage object ( GroundingPassageId )

شناسه برای یک متن درون‌خطی.

شیء semanticRetrieverChunk object ( SemanticRetrieverChunk )

شناسه‌ای برای یک Chunk که از طریق بازیابی معنایی واکشی شده است.

نمایش JSON
{

  // source
  "groundingPassage": {
    object (GroundingPassageId)
  },
  "semanticRetrieverChunk": {
    object (SemanticRetrieverChunk)
  }
  // Union type
}

شناسه گذرگاه زمین

شناسه‌ای برای یک قطعه درون یک GroundingPassage .

فیلدها
string passageId

فقط خروجی. شناسه‌ی متنی که با GroundingPassage.id مربوط به GenerateAnswerRequest مطابقت دارد.

integer partIndex

فقط خروجی. اندیس قطعه درون GroundingPassage.content مربوط به GenerateAnswerRequest .

نمایش JSON
{
  "passageId": string,
  "partIndex": integer
}

SemanticRetrieverChunk

شناسه‌ای برای یک Chunk بازیابی شده از طریق Semantic Retriever که در GenerateAnswerRequest با استفاده از SemanticRetrieverConfig مشخص شده است.

فیلدها
string source

فقط خروجی. نام منبعی که با SemanticRetrieverConfig.source درخواست مطابقت دارد. مثال: corpora/123 یا corpora/123/documents/abc

string chunk

فقط خروجی. نام Chunk که شامل متن نسبت داده شده است. مثال: corpora/123/documents/abc/chunks/xyz

نمایش JSON
{
  "source": string,
  "chunk": string
}

فراداده زمینی

هنگام فعال شدن اتصال به زمین، فراداده به کلاینت بازگردانده می‌شود.

فیلدها
شیء groundingChunks[] object ( GroundingChunk )

فهرست مراجع پشتیبان بازیابی شده از منبع اتصال زمین مشخص شده.

شیء groundingSupports[] object ( GroundingSupport )

فهرست پشتیبانی زمینی.

string webSearchQueries[]

عبارت‌های جستجوی وب برای جستجوی وب بعدی.

شیء searchEntryPoint object ( SearchEntryPoint )

اختیاری. ورودی جستجوی گوگل برای جستجوهای وب بعدی.

شیء retrievalMetadata object ( RetrievalMetadata )

فراداده مربوط به بازیابی در جریان اتصال به زمین.

string googleMapsWidgetContextToken

اختیاری. نام منبع توکن زمینه ویجت نقشه‌های گوگل که می‌تواند با ویجت PlacesContextElement برای رندر کردن داده‌های زمینه‌ای استفاده شود. فقط در صورتی که اتصال به زمین با نقشه‌های گوگل فعال باشد، مقداردهی می‌شود.

نمایش JSON
{
  "groundingChunks": [
    {
      object (GroundingChunk)
    }
  ],
  "groundingSupports": [
    {
      object (GroundingSupport)
    }
  ],
  "webSearchQueries": [
    string
  ],
  "searchEntryPoint": {
    object (SearchEntryPoint)
  },
  "retrievalMetadata": {
    object (RetrievalMetadata)
  },
  "googleMapsWidgetContextToken": string
}

جستجوی ورودی

نقطه ورود جستجوی گوگل.

فیلدها
string renderedContent

اختیاری. قطعه محتوای وب که می‌تواند در یک صفحه وب یا نمای وب یک برنامه جاسازی شود.

رشته sdkBlob string ( bytes format)

اختیاری. JSON کدگذاری شده با Base64 که آرایه‌ای از تاپل‌های <search term, search url> را نشان می‌دهد.

یک رشته کدگذاری شده با base64.

نمایش JSON
{
  "renderedContent": string,
  "sdkBlob": string
}

گراندینگ چانک

قطعه اتصال به زمین.

فیلدها
chunk_type Union type
نوع تکه. chunk_type فقط می‌تواند یکی از موارد زیر باشد:
شیء web object ( Web )

تکه‌ای از تار عنکبوت که به زمین وصل می‌شود.

شیء retrievedContext object ( RetrievedContext )

اختیاری. تکه زمین از متن بازیابی شده توسط ابزار جستجوی فایل.

شیء maps object ( Maps )

اختیاری. قطعه اتصال به زمین از نقشه‌های گوگل.

نمایش JSON
{

  // chunk_type
  "web": {
    object (Web)
  },
  "retrievedContext": {
    object (RetrievedContext)
  },
  "maps": {
    object (Maps)
  }
  // Union type
}

وب

تکه‌ای از وب.

فیلدها
string uri

مرجع URI مربوط به آن قطعه داده.

string title

عنوان قطعه.

نمایش JSON
{
  "uri": string,
  "title": string
}

بازیابی‌شدهزمینه

تکه‌ای از متن که توسط ابزار جستجوی فایل بازیابی شده است.

فیلدها
string uri

اختیاری. مرجع URI سند بازیابی معنایی.

string title

اختیاری. عنوان سند.

string text

اختیاری. متن قطعه کد.

نمایش JSON
{
  "uri": string,
  "title": string,
  "text": string
}

نقشه‌ها

یک قطعه زمین از نقشه‌های گوگل. یک قطعه نقشه مربوط به یک مکان واحد است.

فیلدها
string uri

مرجع URI آن مکان.

string title

عنوان مکان.

string text

پاسخ توضیحات متنی مکان.

string placeId

این شناسه مکان، در قالب places/{placeId} . کاربر می‌تواند از این شناسه برای جستجوی آن مکان استفاده کند.

شیء placeAnswerSources object ( PlaceAnswerSources )

منابعی که پاسخ‌هایی در مورد ویژگی‌های یک مکان مشخص در نقشه‌های گوگل ارائه می‌دهند.

نمایش JSON
{
  "uri": string,
  "title": string,
  "text": string,
  "placeId": string,
  "placeAnswerSources": {
    object (PlaceAnswerSources)
  }
}

منابع PlaceAnswer

مجموعه‌ای از منابع که پاسخ‌هایی در مورد ویژگی‌های یک مکان مشخص در نقشه‌های گوگل ارائه می‌دهند. هر پیام PlaceAnswerSources مربوط به یک مکان خاص در نقشه‌های گوگل است. ابزار نقشه‌های گوگل از این منابع برای پاسخ به سوالاتی در مورد ویژگی‌های مکان استفاده کرده است (مثلاً: "آیا بار فو وای‌فای دارد" یا "آیا بار فو برای ویلچر قابل دسترسی است؟"). در حال حاضر ما فقط از گزیده‌های نقد و بررسی به عنوان منبع پشتیبانی می‌کنیم.

فیلدها
reviewSnippets[] object ( ReviewSnippet )

گزیده‌هایی از نظرات که برای تولید پاسخ در مورد ویژگی‌های یک مکان مشخص در نقشه‌های گوگل استفاده می‌شوند.

نمایش JSON
{
  "reviewSnippets": [
    {
      object (ReviewSnippet)
    }
  ]
}

نقد و بررسی قطعه کد

بخشی از نقد کاربر را که به سوالی در مورد ویژگی‌های یک مکان خاص در نقشه‌های گوگل پاسخ می‌دهد، در بر می‌گیرد.

فیلدها
string reviewId

شناسه‌ی قطعه نقد و بررسی.

string googleMapsUri

لینکی که مربوط به نظر کاربر در نقشه گوگل باشد.

string title

عنوان نقد.

نمایش JSON
{
  "reviewId": string,
  "googleMapsUri": string,
  "title": string
}

پشتیبانی اتصال به زمین

پشتیبانی زمینی.

فیلدها
groundingChunkIndices[] integer

فهرستی از شاخص‌ها (در 'grounding_chunk') که استنادهای مرتبط با ادعا را مشخص می‌کند. برای مثال [1،3،4] به این معنی است که grounding_chunk[1]، grounding_chunk[3]، grounding_chunk[4] محتوای بازیابی شده منتسب به ادعا هستند.

number confidenceScores[]

امتیاز اطمینان مراجع پشتیبانی. از ۰ تا ۱ متغیر است. ۱ مطمئن‌ترین است. این لیست باید اندازه‌ای برابر با groundingChunkIndices داشته باشد.

شیء segment object ( Segment )

بخشی از محتوایی که این پشتیبانی به آن تعلق دارد.

نمایش JSON
{
  "groundingChunkIndices": [
    integer
  ],
  "confidenceScores": [
    number
  ],
  "segment": {
    object (Segment)
  }
}

بخش

بخش بندی محتوا.

فیلدها
integer partIndex

فقط خروجی. اندیس یک شیء Part درون شیء Content والد آن.

integer startIndex

فقط خروجی. اندیس شروع در قطعه داده شده، که بر حسب بایت اندازه‌گیری می‌شود. فاصله از ابتدای قطعه، شامل همه اجزا، از صفر شروع می‌شود.

integer endIndex

فقط خروجی. اندیس پایان در قطعه داده شده، که بر حسب بایت اندازه‌گیری می‌شود. فاصله از ابتدای قطعه، منحصراً، از صفر شروع می‌شود.

string text

فقط خروجی. متن مربوط به بخش پاسخ.

نمایش JSON
{
  "partIndex": integer,
  "startIndex": integer,
  "endIndex": integer,
  "text": string
}

بازیابیفراداده

فراداده مربوط به بازیابی در جریان اتصال به زمین.

فیلدها
number googleSearchDynamicRetrievalScore

اختیاری. امتیازی که نشان می‌دهد اطلاعات حاصل از جستجوی گوگل چقدر می‌تواند به پاسخ سوال کمک کند. امتیاز در محدوده [0، 1] است، که در آن 0 کمترین احتمال و 1 بیشترین احتمال را دارد. این امتیاز فقط زمانی پر می‌شود که جستجوی گوگل مبتنی بر جستجو و بازیابی پویا فعال باشد. این امتیاز با آستانه مقایسه می‌شود تا مشخص شود که آیا جستجوی گوگل فعال شود یا خیر.

نمایش JSON
{
  "googleSearchDynamicRetrievalScore": number
}

نتیجه‌ی لاگ‌پروبز

نتیجه لاگ‌پروبز

فیلدها
شیء topCandidates[] object ( TopCandidates )

طول = تعداد کل مراحل رمزگشایی.

شیء chosenCandidates[] object ( Candidate )

طول = تعداد کل مراحل رمزگشایی. کاندیداهای انتخاب شده ممکن است در topCandidates باشند یا نباشند.

number logProbabilitySum

مجموع احتمالات لگاریتمی برای همه توکن‌ها.

نمایش JSON
{
  "topCandidates": [
    {
      object (TopCandidates)
    }
  ],
  "chosenCandidates": [
    {
      object (Candidate)
    }
  ],
  "logProbabilitySum": number
}

کاندیداهای برتر

کاندیداهایی با احتمال لگاریتمی بالا در هر مرحله رمزگشایی.

فیلدها
object ( Candidate ) candidates[] (نامزد)

بر اساس احتمال لگاریتمی به ترتیب نزولی مرتب شده‌اند.

نمایش JSON
{
  "candidates": [
    {
      object (Candidate)
    }
  ]
}

نامزد

کاندید برای توکن logprobs و امتیاز.

فیلدها
string token

مقدار رشته توکن کاندیدا.

integer tokenId

مقدار شناسه توکن کاندیدا.

number logProbability

لگاریتم احتمال کاندیدا.

نمایش JSON
{
  "token": string,
  "tokenId": integer,
  "logProbability": number
}

فراداده‌ی UrlContext

فراداده مربوط به ابزار بازیابی متن url.

فیلدها
شیء urlMetadata[] object ( UrlMetadata )

فهرست زمینه آدرس اینترنتی.

نمایش JSON
{
  "urlMetadata": [
    {
      object (UrlMetadata)
    }
  ]
}

آدرس فراداده

زمینه بازیابی یک آدرس اینترنتی واحد.

فیلدها
string retrievedUrl

آدرس اینترنتی (url) توسط ابزار بازیابی شد.

متغیر شمارشی urlRetrievalStatus enum ( UrlRetrievalStatus )

وضعیت بازیابی آدرس اینترنتی (URL).

نمایش JSON
{
  "retrievedUrl": string,
  "urlRetrievalStatus": enum (UrlRetrievalStatus)
}

وضعیت بازیابی آدرس

وضعیت بازیابی آدرس اینترنتی (URL).

انوم‌ها
URL_RETRIEVAL_STATUS_UNSPECIFIED مقدار پیش‌فرض. این مقدار استفاده نشده است.
URL_RETRIEVAL_STATUS_SUCCESS بازیابی آدرس اینترنتی (URL) با موفقیت انجام شد.
URL_RETRIEVAL_STATUS_ERROR بازیابی آدرس اینترنتی (URL) به دلیل خطا با شکست مواجه شد.
URL_RETRIEVAL_STATUS_PAYWALL بازیابی آدرس اینترنتی (URL) ناموفق است زیرا محتوا پشت دیوار پرداخت (paywall) قرار دارد.
URL_RETRIEVAL_STATUS_UNSAFE بازیابی آدرس اینترنتی (URL) به دلیل ناامن بودن محتوا با شکست مواجه شد.

فراداده استناد

مجموعه‌ای از منابع ارجاع‌دهنده به یک محتوا.

فیلدها
شیء citationSources[] object ( CitationSource )

استناد به منابع برای یک پاسخ خاص.

نمایش JSON
{
  "citationSources": [
    {
      object (CitationSource)
    }
  ]
}

منبع استناد

استناد به یک منبع برای بخشی از یک پاسخ خاص.

فیلدها
integer startIndex

اختیاری. شروع بخشی از پاسخ که به این منبع نسبت داده می‌شود.

اندیس، شروع سگمنت را نشان می‌دهد که بر حسب بایت اندازه‌گیری می‌شود.

integer endIndex

اختیاری. پایان بخش نسبت داده شده، منحصر به فرد.

string uri

اختیاری. آدرس اینترنتی (URI) که به عنوان منبع بخشی از متن نسبت داده شده است.

string license

اختیاری. مجوز پروژه گیت‌هاب که به عنوان منبعی برای بخش اختصاص داده شده است.

اطلاعات مجوز برای استناد به کد مورد نیاز است.

نمایش JSON
{
  "startIndex": integer,
  "endIndex": integer,
  "uri": string,
  "license": string
}

پیکربندی نسل

گزینه‌های پیکربندی برای تولید مدل و خروجی‌ها. همه پارامترها برای هر مدلی قابل تنظیم نیستند.

فیلدها
string stopSequences[]

اختیاری. مجموعه‌ای از توالی‌های کاراکتری (تا ۵) که تولید خروجی را متوقف می‌کنند. در صورت مشخص شدن، API با اولین ظهور stop_sequence متوقف می‌شود. توالی توقف به عنوان بخشی از پاسخ لحاظ نخواهد شد.

string responseMimeType

اختیاری. نوع MIME متن کاندید تولید شده. انواع MIME پشتیبانی شده عبارتند از: text/plain : (پیش فرض) خروجی متن. application/json : پاسخ JSON در کاندیدهای پاسخ. text/x.enum : پاسخ ENUM به عنوان رشته در کاندیدهای پاسخ. برای لیستی از تمام انواع MIME متنی پشتیبانی شده به اسناد مراجعه کنید.

شی responseSchema object ( Schema )

اختیاری. طرحواره خروجی متن کاندید تولید شده. طرحواره‌ها باید زیرمجموعه‌ای از طرحواره OpenAPI باشند و می‌توانند اشیاء، مقادیر اولیه یا آرایه‌ها باشند.

در صورت تنظیم، یک responseMimeType سازگار نیز باید تنظیم شود. انواع MIME سازگار: application/json : طرحواره برای پاسخ JSON. برای جزئیات بیشتر به راهنمای تولید متن JSON مراجعه کنید.

مقدار _responseJsonSchema value ( Value format)

اختیاری. طرحواره خروجی پاسخ تولید شده. این جایگزینی برای responseSchema است که JSON Schema را می‌پذیرد.

در صورت تنظیم، responseSchema باید حذف شود، اما responseMimeType الزامی است.

اگرچه می‌توان کل JSON Schema را ارسال کرد، اما همه ویژگی‌ها پشتیبانی نمی‌شوند. به طور خاص، فقط ویژگی‌های زیر پشتیبانی می‌شوند:

  • $id
  • $defs
  • $ref
  • $anchor
  • type
  • format
  • title
  • description
  • enum (برای رشته‌ها و اعداد)
  • items
  • prefixItems
  • minItems
  • maxItems
  • minimum
  • maximum
  • anyOf
  • oneOf (به همان صورت anyOf تفسیر می‌شود)
  • properties
  • additionalProperties
  • required

همچنین می‌توان ویژگی غیر استاندارد propertyOrdering تنظیم کرد.

ارجاعات چرخه‌ای تا حد محدودی باز می‌شوند و به همین دلیل، فقط می‌توانند در ویژگی‌های غیرضروری استفاده شوند. (ویژگی‌های Nullable کافی نیستند.) اگر $ref روی یک زیر-طرح تنظیم شود، هیچ ویژگی دیگری، به جز آن‌هایی که با $ شروع می‌شوند، نمی‌تواند تنظیم شود.

مقدار responseJsonSchema value ( Value format)

اختیاری. یک جزئیات داخلی. به جای این فیلد responseJsonSchema استفاده کنید.

responseModalities[] enum ( Modality )

اختیاری. روش‌های درخواستی پاسخ. نشان‌دهنده‌ی مجموعه‌ای از روش‌هایی است که مدل می‌تواند برگرداند و باید در پاسخ انتظار داشته باشد. این دقیقاً مطابق با روش‌های پاسخ است.

یک مدل ممکن است چندین ترکیب از روش‌های پشتیبانی‌شده داشته باشد. اگر روش‌های درخواستی با هیچ یک از ترکیبات پشتیبانی‌شده مطابقت نداشته باشند، یک خطا برگردانده می‌شود.

یک لیست خالی معادل درخواست فقط متن است.

integer candidateCount

اختیاری. تعداد پاسخ‌های تولید شده برای بازگشت. در صورت عدم تنظیم، این مقدار به طور پیش‌فرض ۱ خواهد بود. لطفاً توجه داشته باشید که این مقدار برای مدل‌های نسل قبلی (خانواده Gemini 1.0) کار نمی‌کند.

integer maxOutputTokens

اختیاری. حداکثر تعداد توکن‌هایی که باید در یک کاندید پاسخ گنجانده شود.

نکته: مقدار پیش‌فرض بر اساس مدل متفاوت است، به ویژگی Model.output_token_limit Model که از تابع getModel برگردانده می‌شود، مراجعه کنید.

number temperature

اختیاری. تصادفی بودن خروجی را کنترل می‌کند.

نکته: مقدار پیش‌فرض بر اساس مدل متفاوت است، به ویژگی Model.temperature Model برگردانده شده از تابع getModel مراجعه کنید.

مقادیر می‌توانند از [0.0، 2.0] متغیر باشند.

number topP

اختیاری. حداکثر احتمال تجمعی توکن‌ها که هنگام نمونه‌برداری در نظر گرفته می‌شود.

این مدل از نمونه‌برداری ترکیبی Top-k و Top-p (هسته) استفاده می‌کند.

توکن‌ها بر اساس احتمالات اختصاص داده شده به آنها مرتب می‌شوند، به طوری که فقط محتمل‌ترین توکن‌ها در نظر گرفته می‌شوند. نمونه‌گیری Top-k مستقیماً حداکثر تعداد توکن‌های مورد بررسی را محدود می‌کند، در حالی که نمونه‌گیری Nucleus تعداد توکن‌ها را بر اساس احتمال تجمعی محدود می‌کند.

Note: The default value varies by Model and is specified by the Model.top_p attribute returned from the getModel function. An empty topK attribute indicates that the model doesn't apply top-k sampling and doesn't allow setting topK on requests.

topK integer

Optional. The maximum number of tokens to consider when sampling.

Gemini models use Top-p (nucleus) sampling or a combination of Top-k and nucleus sampling. Top-k sampling considers the set of topK most probable tokens. Models running with nucleus sampling don't allow topK setting.

Note: The default value varies by Model and is specified by the Model.top_p attribute returned from the getModel function. An empty topK attribute indicates that the model doesn't apply top-k sampling and doesn't allow setting topK on requests.

seed integer

Optional. Seed used in decoding. If not set, the request uses a randomly generated seed.

presencePenalty number

Optional. Presence penalty applied to the next token's logprobs if the token has already been seen in the response.

This penalty is binary on/off and not dependant on the number of times the token is used (after the first). Use frequencyPenalty for a penalty that increases with each use.

A positive penalty will discourage the use of tokens that have already been used in the response, increasing the vocabulary.

A negative penalty will encourage the use of tokens that have already been used in the response, decreasing the vocabulary.

frequencyPenalty number

Optional. Frequency penalty applied to the next token's logprobs, multiplied by the number of times each token has been seen in the respponse so far.

A positive penalty will discourage the use of tokens that have already been used, proportional to the number of times the token has been used: The more a token is used, the more difficult it is for the model to use that token again increasing the vocabulary of responses.

Caution: A negative penalty will encourage the model to reuse tokens proportional to the number of times the token has been used. Small negative values will reduce the vocabulary of a response. Larger negative values will cause the model to start repeating a common token until it hits the maxOutputTokens limit.

responseLogprobs boolean

Optional. If true, export the logprobs results in response.

logprobs integer

Optional. Only valid if responseLogprobs=True . This sets the number of top logprobs to return at each decoding step in the Candidate.logprobs_result . The number must be in the range of [0, 20].

enableEnhancedCivicAnswers boolean

Optional. Enables enhanced civic answers. It may not be available for all models.

speechConfig object ( SpeechConfig )

Optional. The speech generation config.

thinkingConfig object ( ThinkingConfig )

Optional. Config for thinking features. An error will be returned if this field is set for models that don't support thinking.

imageConfig object ( ImageConfig )

Optional. Config for image generation. An error will be returned if this field is set for models that don't support these config options.

mediaResolution enum ( MediaResolution )

Optional. If specified, the media resolution specified will be used.

نمایش JSON
{
  "stopSequences": [
    string
  ],
  "responseMimeType": string,
  "responseSchema": {
    object (Schema)
  },
  "_responseJsonSchema": value,
  "responseJsonSchema": value,
  "responseModalities": [
    enum (Modality)
  ],
  "candidateCount": integer,
  "maxOutputTokens": integer,
  "temperature": number,
  "topP": number,
  "topK": integer,
  "seed": integer,
  "presencePenalty": number,
  "frequencyPenalty": number,
  "responseLogprobs": boolean,
  "logprobs": integer,
  "enableEnhancedCivicAnswers": boolean,
  "speechConfig": {
    object (SpeechConfig)
  },
  "thinkingConfig": {
    object (ThinkingConfig)
  },
  "imageConfig": {
    object (ImageConfig)
  },
  "mediaResolution": enum (MediaResolution)
}

روش

Supported modalities of the response.

انوم‌ها
MODALITY_UNSPECIFIED مقدار پیش‌فرض.
TEXT Indicates the model should return text.
IMAGE Indicates the model should return images.
AUDIO Indicates the model should return audio.

SpeechConfig

The speech generation config.

فیلدها
voiceConfig object ( VoiceConfig )

The configuration in case of single-voice output.

multiSpeakerVoiceConfig object ( MultiSpeakerVoiceConfig )

Optional. The configuration for the multi-speaker setup. It is mutually exclusive with the voiceConfig field.

languageCode string

Optional. Language code (in BCP 47 format, eg "en-US") for speech synthesis.

Valid values are: de-DE, en-AU, en-GB, en-IN, en-US, es-US, fr-FR, hi-IN, pt-BR, ar-XA, es-ES, fr-CA, id-ID, it-IT, ja-JP, tr-TR, vi-VN, bn-IN, gu-IN, kn-IN, ml-IN, mr-IN, ta-IN, te-IN, nl-NL, ko-KR, cmn-CN, pl-PL, ru-RU, and th-TH.

نمایش JSON
{
  "voiceConfig": {
    object (VoiceConfig)
  },
  "multiSpeakerVoiceConfig": {
    object (MultiSpeakerVoiceConfig)
  },
  "languageCode": string
}

VoiceConfig

The configuration for the voice to use.

فیلدها
voice_config Union type
The configuration for the speaker to use. voice_config can be only one of the following:
prebuiltVoiceConfig object ( PrebuiltVoiceConfig )

The configuration for the prebuilt voice to use.

نمایش JSON
{

  // voice_config
  "prebuiltVoiceConfig": {
    object (PrebuiltVoiceConfig)
  }
  // Union type
}

PrebuiltVoiceConfig

The configuration for the prebuilt speaker to use.

فیلدها
voiceName string

The name of the preset voice to use.

نمایش JSON
{
  "voiceName": string
}

MultiSpeakerVoiceConfig

The configuration for the multi-speaker setup.

فیلدها
speakerVoiceConfigs[] object ( SpeakerVoiceConfig )

Required. All the enabled speaker voices.

نمایش JSON
{
  "speakerVoiceConfigs": [
    {
      object (SpeakerVoiceConfig)
    }
  ]
}

SpeakerVoiceConfig

The configuration for a single speaker in a multi speaker setup.

فیلدها
speaker string

Required. The name of the speaker to use. Should be the same as in the prompt.

voiceConfig object ( VoiceConfig )

Required. The configuration for the voice to use.

نمایش JSON
{
  "speaker": string,
  "voiceConfig": {
    object (VoiceConfig)
  }
}

ThinkingConfig

Config for thinking features.

فیلدها
includeThoughts boolean

Indicates whether to include thoughts in the response. If true, thoughts are returned only when available.

thinkingBudget integer

The number of thoughts tokens that the model should generate.

نمایش JSON
{
  "includeThoughts": boolean,
  "thinkingBudget": integer
}

ImageConfig

Config for image generation features.

فیلدها
aspectRatio string

Optional. The aspect ratio of the image to generate. Supported aspect ratios: 1:1, 2:3, 3:2, 3:4, 4:3, 9:16, 16:9, 21:9.

If not specified, the model will choose a default aspect ratio based on any reference images provided.

نمایش JSON
{
  "aspectRatio": string
}

MediaResolution

Media resolution for the input media.

انوم‌ها
MEDIA_RESOLUTION_UNSPECIFIED Media resolution has not been set.
MEDIA_RESOLUTION_LOW Media resolution set to low (64 tokens).
MEDIA_RESOLUTION_MEDIUM Media resolution set to medium (256 tokens).
MEDIA_RESOLUTION_HIGH Media resolution set to high (zoomed reframing with 256 tokens).

HarmCategory

The category of a rating.

These categories cover various kinds of harms that developers may wish to adjust.

انوم‌ها
HARM_CATEGORY_UNSPECIFIED Category is unspecified.
HARM_CATEGORY_DEROGATORY PaLM - Negative or harmful comments targeting identity and/or protected attribute.
HARM_CATEGORY_TOXICITY PaLM - Content that is rude, disrespectful, or profane.
HARM_CATEGORY_VIOLENCE PaLM - Describes scenarios depicting violence against an individual or group, or general descriptions of gore.
HARM_CATEGORY_SEXUAL PaLM - Contains references to sexual acts or other lewd content.
HARM_CATEGORY_MEDICAL PaLM - Promotes unchecked medical advice.
HARM_CATEGORY_DANGEROUS PaLM - Dangerous content that promotes, facilitates, or encourages harmful acts.
HARM_CATEGORY_HARASSMENT Gemini - Harassment content.
HARM_CATEGORY_HATE_SPEECH Gemini - Hate speech and content.
HARM_CATEGORY_SEXUALLY_EXPLICIT Gemini - Sexually explicit content.
HARM_CATEGORY_DANGEROUS_CONTENT Gemini - Dangerous content.
HARM_CATEGORY_CIVIC_INTEGRITY

Gemini - Content that may be used to harm civic integrity. DEPRECATED: use enableEnhancedCivicAnswers instead.

ModalityTokenCount

Represents token counting info for a single modality.

فیلدها
modality enum ( Modality )

The modality associated with this token count.

tokenCount integer

Number of tokens.

نمایش JSON
{
  "modality": enum (Modality),
  "tokenCount": integer
}

روش

Content Part modality

انوم‌ها
MODALITY_UNSPECIFIED Unspecified modality.
TEXT Plain text.
IMAGE تصویر.
VIDEO ویدئو.
AUDIO Audio.
DOCUMENT Document, eg PDF.

SafetyRating

Safety rating for a piece of content.

The safety rating contains the category of harm and the harm probability level in that category for a piece of content. Content is classified for safety across a number of harm categories and the probability of the harm classification is included here.

فیلدها
category enum ( HarmCategory )

Required. The category for this rating.

probability enum ( HarmProbability )

Required. The probability of harm for this content.

blocked boolean

Was this content blocked because of this rating?

نمایش JSON
{
  "category": enum (HarmCategory),
  "probability": enum (HarmProbability),
  "blocked": boolean
}

HarmProbability

The probability that a piece of content is harmful.

The classification system gives the probability of the content being unsafe. This does not indicate the severity of harm for a piece of content.

انوم‌ها
HARM_PROBABILITY_UNSPECIFIED Probability is unspecified.
NEGLIGIBLE Content has a negligible chance of being unsafe.
LOW Content has a low chance of being unsafe.
MEDIUM Content has a medium chance of being unsafe.
HIGH Content has a high chance of being unsafe.

SafetySetting

Safety setting, affecting the safety-blocking behavior.

Passing a safety setting for a category changes the allowed probability that content is blocked.

فیلدها
category enum ( HarmCategory )

Required. The category for this setting.

threshold enum ( HarmBlockThreshold )

Required. Controls the probability threshold at which harm is blocked.

نمایش JSON
{
  "category": enum (HarmCategory),
  "threshold": enum (HarmBlockThreshold)
}

HarmBlockThreshold

Block at and beyond a specified harm probability.

انوم‌ها
HARM_BLOCK_THRESHOLD_UNSPECIFIED Threshold is unspecified.
BLOCK_LOW_AND_ABOVE Content with NEGLIGIBLE will be allowed.
BLOCK_MEDIUM_AND_ABOVE Content with NEGLIGIBLE and LOW will be allowed.
BLOCK_ONLY_HIGH Content with NEGLIGIBLE, LOW, and MEDIUM will be allowed.
BLOCK_NONE All content will be allowed.
OFF Turn off the safety filter.