Caching

Кэширование контекста позволяет сохранять и повторно использовать предварительно вычисленные входные токены, которые вы хотите использовать повторно, например, при постановке разных вопросов об одном и том же медиафайле. Это может привести к экономии средств и повышению скорости, в зависимости от использования. Подробное введение см. в руководстве по кэшированию контекста .

Метод: cachedContents.create

Создает ресурс CachedContent.

Конечная точка

пост https: / /generativelanguage.googleapis.com /v1beta /cachedContents

Текст запроса

Тело запроса содержит экземпляр CachedContent .

Поля
contents[] object ( Content )

Необязательно. Только ввод. Неизменяемо. Содержимое для кэширования.

tools[] object ( Tool )

Необязательный. Только входные данные. Неизменяемый. Список Tools модель может использовать для генерации следующего ответа.

expiration Union type
Указывает, когда истекает срок действия этого ресурса. expiration может быть только одним из следующих:
строка expireTime string ( Timestamp format)

Временная метка в формате UTC, когда ресурс считается истёкшим. Она всегда указывается на выходе, независимо от того, что было отправлено на входе.

Использует RFC 3339, согласно которому сгенерированный вывод всегда будет нормализован по оси Z и содержать 0, 3, 6 или 9 знаков после запятой. Также допускаются смещения, отличные от «Z». Примеры: "2014-10-02T15:01:23Z" , "2014-10-02T15:01:23.045123456Z" или "2014-10-02T15:01:23+05:30" .

строка ttl string ( Duration format)

Только ввод. Новый TTL для этого ресурса, только ввод.

Длительность в секундах, содержащая до девяти знаков после запятой, заканчивается на « s ». Пример: "3.5s" .

string displayName

Необязательно. Неизменяемо. Созданное пользователем осмысленное отображаемое имя кэшированного контента. Максимум 128 символов Unicode.

string model

Обязательно. Неизменяемо. Имя Model , используемой для кэшированного контента. Формат: models/{model}

object ( Content ) systemInstruction (Контент)

Необязательно. Только ввод. Неизменяемо. Системная инструкция, заданная разработчиком. В настоящее время только текст.

объект toolConfig object ( ToolConfig )

Необязательно. Только ввод. Неизменяемо. Конфигурация инструмента. Эта конфигурация общая для всех инструментов.

Пример запроса

Базовый

Питон

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)

Node.js

// 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)

Оболочка

wget https://storage.googleapis.com/generativeai-downloads/data/a11.txt
echo '{
  "model": "models/gemini-1.5-flash-001",
  "contents":[
    {
      "parts":[
        {
          "inline_data": {
            "mime_type":"text/plain",
            "data": "'$(base64 $B64FLAGS a11.txt)'"
          }
        }
      ],
    "role": "user"
    }
  ],
  "systemInstruction": {
    "parts": [
      {
        "text": "You are an expert at analyzing transcripts."
      }
    ]
  },
  "ttl": "300s"
}' > request.json

curl -X POST "https://generativelanguage.googleapis.com/v1beta/cachedContents?key=$GEMINI_API_KEY" \
 -H 'Content-Type: application/json' \
 -d @request.json \
 > cache.json

CACHE_NAME=$(cat cache.json | grep '"name":' | cut -d '"' -f 4 | head -n 1)

curl -X POST "https://generativelanguage.googleapis.com/v1beta/models/gemini-1.5-flash-001:generateContent?key=$GEMINI_API_KEY" \
-H 'Content-Type: application/json' \
-d '{
      "contents": [
        {
          "parts":[{
            "text": "Please summarize this transcript"
          }],
          "role": "user"
        },
      ],
      "cachedContent": "'$CACHE_NAME'"
    }'

От имени

Питон

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.",
    ),
)
cache_name = cache.name  # Save the name for later

# Later retrieve the cache
cache = client.caches.get(name=cache_name)
response = client.models.generate_content(
    model=model_name,
    contents="Find a lighthearted moment from this transcript",
    config=types.GenerateContentConfig(cached_content=cache.name),
)
print(response.text)

Node.js

// 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.",
  },
});
const cacheName = cache.name; // Save the name for later

// Later retrieve the cache
const retrievedCache = await ai.caches.get({ name: cacheName });
const response = await ai.models.generateContent({
  model: modelName,
  contents: "Find a lighthearted moment from this transcript",
  config: { cachedContent: retrievedCache.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)
}
cacheName := cache.Name

// Later retrieve the cache.
cache, err = client.Caches.Get(ctx, cacheName, &genai.GetCachedContentConfig{})
if err != nil {
	log.Fatal(err)
}

response, err := client.Models.GenerateContent(
	ctx,
	modelName,
	genai.Text("Find a lighthearted moment from this transcript"),
	&genai.GenerateContentConfig{
		CachedContent: cache.Name,
	},
)
if err != nil {
	log.Fatal(err)
}
fmt.Println("Response from cache (create from name):")
printResponse(response)

Из чата

Питон

from google import genai
from google.genai import types

client = genai.Client()
model_name = "gemini-1.5-flash-001"
system_instruction = "You are an expert analyzing transcripts."

# Create a chat session with the given system instruction.
chat = client.chats.create(
    model=model_name,
    config=types.GenerateContentConfig(system_instruction=system_instruction),
)
document = client.files.upload(file=media / "a11.txt")

response = chat.send_message(
    message=["Hi, could you summarize this transcript?", document]
)
print("\n\nmodel:  ", response.text)
response = chat.send_message(
    message=["Okay, could you tell me more about the trans-lunar injection"]
)
print("\n\nmodel:  ", response.text)

# To cache the conversation so far, pass the chat history as the list of contents.
cache = client.caches.create(
    model=model_name,
    config={
        "contents": chat.get_history(),
        "system_instruction": system_instruction,
    },
)
# Continue the conversation using the cached content.
chat = client.chats.create(
    model=model_name,
    config=types.GenerateContentConfig(cached_content=cache.name),
)
response = chat.send_message(
    message="I didn't understand that last part, could you explain it in simpler language?"
)
print("\n\nmodel:  ", response.text)

Node.js

// Make sure to include the following import:
// import {GoogleGenAI} from '@google/genai';
const ai = new GoogleGenAI({ apiKey: process.env.GEMINI_API_KEY });
const modelName = "gemini-1.5-flash-001";
const systemInstruction = "You are an expert analyzing transcripts.";

// Create a chat session with the system instruction.
const chat = ai.chats.create({
  model: modelName,
  config: { systemInstruction: systemInstruction },
});
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);

let response = await chat.sendMessage({
  message: createUserContent([
    "Hi, could you summarize this transcript?",
    createPartFromUri(document.uri, document.mimeType),
  ]),
});
console.log("\n\nmodel:", response.text);

response = await chat.sendMessage({
  message: "Okay, could you tell me more about the trans-lunar injection",
});
console.log("\n\nmodel:", response.text);

// To cache the conversation so far, pass the chat history as the list of contents.
const chatHistory = chat.getHistory();
const cache = await ai.caches.create({
  model: modelName,
  config: {
    contents: chatHistory,
    systemInstruction: systemInstruction,
  },
});

// Continue the conversation using the cached content.
const chatWithCache = ai.chats.create({
  model: modelName,
  config: { cachedContent: cache.name },
});
response = await chatWithCache.sendMessage({
  message:
    "I didn't understand that last part, could you explain it in simpler language?",
});
console.log("\n\nmodel:", 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"
systemInstruction := "You are an expert analyzing transcripts."

// Create initial chat with a system instruction.
chat, err := client.Chats.Create(ctx, modelName, &genai.GenerateContentConfig{
	SystemInstruction: genai.NewContentFromText(systemInstruction, genai.RoleUser),
}, nil)
if err != nil {
	log.Fatal(err)
}

document, err := client.Files.UploadFromPath(
	ctx, 
	filepath.Join(getMedia(), "a11.txt"), 
	&genai.UploadFileConfig{
		MIMEType : "text/plain",
	},
)
if err != nil {
	log.Fatal(err)
}

// Send first message with the transcript.
parts := make([]genai.Part, 2)
parts[0] = genai.Part{Text: "Hi, could you summarize this transcript?"}
parts[1] = genai.Part{
	FileData: &genai.FileData{
		FileURI :      document.URI,
		MIMEType: document.MIMEType,
	},
}

// Send chat message.
resp, err := chat.SendMessage(ctx, parts...)
if err != nil {
	log.Fatal(err)
}
fmt.Println("\n\nmodel: ", resp.Text())

resp, err = chat.SendMessage(
	ctx, 
	genai.Part{
		Text: "Okay, could you tell me more about the trans-lunar injection",
	},
)
if err != nil {
	log.Fatal(err)
}
fmt.Println("\n\nmodel: ", resp.Text())

// To cache the conversation so far, pass the chat history as the list of contents.
cache, err := client.Caches.Create(ctx, modelName, &genai.CreateCachedContentConfig{
	Contents:          chat.History(false),
	SystemInstruction: genai.NewContentFromText(systemInstruction, genai.RoleUser),
})
if err != nil {
	log.Fatal(err)
}

// Continue the conversation using the cached history.
chat, err = client.Chats.Create(ctx, modelName, &genai.GenerateContentConfig{
	CachedContent: cache.Name,
}, nil)
if err != nil {
	log.Fatal(err)
}

resp, err = chat.SendMessage(
	ctx, 
	genai.Part{
		Text: "I didn't understand that last part, could you explain it in simpler language?",
	},
)
if err != nil {
	log.Fatal(err)
}
fmt.Println("\n\nmodel: ", resp.Text())

Тело ответа

В случае успеха тело ответа содержит вновь созданный экземпляр CachedContent .

Метод: cachedContents.list

Списки кэшированного содержимого.

Конечная точка

получить https: / /generativelanguage.googleapis.com /v1beta /cachedContents

Параметры запроса

pageSize integer

Необязательно. Максимальное количество возвращаемых кэшированных элементов. Служба может вернуть меньше этого значения. Если не указано, будет возвращено некоторое количество элементов по умолчанию (меньше максимального). Максимальное значение — 1000; значения выше 1000 будут приведены к 1000.

string pageToken

Необязательно. Токен страницы, полученный из предыдущего вызова cachedContents.list . Укажите его для получения следующей страницы.

При пагинации все остальные параметры, предоставленные cachedContents.list , должны соответствовать вызову, который предоставил токен страницы.

Текст запроса

Тело запроса должно быть пустым.

Тело ответа

Ответ со списком CachedContents.

В случае успеха тело ответа содержит данные со следующей структурой:

Поля
объект cachedContents[] object ( CachedContent )

Список кэшированного содержимого.

string nextPageToken

Токен, который можно отправить как pageToken для получения следующей страницы. Если это поле пропущено, последующих страниц не будет.

JSON-представление
{
  "cachedContents": [
    {
      object (CachedContent)
    }
  ],
  "nextPageToken": string
}

Метод: cachedContents.get

Читает ресурс CachedContent.

Конечная точка

получить https: / /generativelanguage.googleapis.com /v1beta /{name=cachedContents /*}

Параметры пути

string name

Обязательно. Имя ресурса, ссылающегося на запись кэша контента. Формат: cachedContents/{id} Имеет вид cachedContents/{cachedcontent} .

Текст запроса

Тело запроса должно быть пустым.

Пример запроса

Питон

from google import genai

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={
        "contents": [document],
        "system_instruction": "You are an expert analyzing transcripts.",
    },
)
print(client.caches.get(name=cache.name))

Node.js

// 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.",
  },
});
const retrievedCache = await ai.caches.get({ name: cache.name });
console.log("Retrieved Cache:", retrievedCache);

Идти

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)
}

cache, err = client.Caches.Get(ctx, cache.Name, &genai.GetCachedContentConfig{})
if err != nil {
	log.Fatal(err)
}
fmt.Println("Retrieved cache:")
fmt.Println(cache)

Оболочка

curl "https://generativelanguage.googleapis.com/v1beta/$CACHE_NAME?key=$GEMINI_API_KEY"

Тело ответа

В случае успеха тело ответа содержит экземпляр CachedContent .

Метод: cachedContents.patch

Обновляет ресурс CachedContent (обновляется только срок действия).

Конечная точка

патч https: / /generativelanguage.googleapis.com /v1beta /{cachedContent.name=cachedContents /*}
PATCH https://generativelanguage.googleapis.com/v1beta/{cachedContent.name=cachedContents/*}

Параметры пути

string cachedContent.name

Только вывод. Идентификатор. Имя ресурса, относящегося к кэшированному содержимому. Формат: cachedContents/{id} Имеет вид cachedContents/{cachedcontent} .

Параметры запроса

string ( FieldMask format) updateMask (формат FieldMask)

Список полей для обновления.

Это список полных имён полей, разделённых запятыми. Пример: "user.displayName,photo" .

Текст запроса

Тело запроса содержит экземпляр CachedContent .

Поля
expiration Union type
Указывает, когда истекает срок действия этого ресурса. expiration может быть только одним из следующих:
строка expireTime string ( Timestamp format)

Временная метка в формате UTC, когда ресурс считается истёкшим. Она всегда указывается на выходе, независимо от того, что было отправлено на входе.

Использует RFC 3339, согласно которому сгенерированный вывод всегда будет нормализован по оси Z и содержать 0, 3, 6 или 9 знаков после запятой. Также допускаются смещения, отличные от «Z». Примеры: "2014-10-02T15:01:23Z" , "2014-10-02T15:01:23.045123456Z" или "2014-10-02T15:01:23+05:30" .

строка ttl string ( Duration format)

Только ввод. Новый TTL для этого ресурса, только ввод.

Длительность в секундах, содержащая до девяти знаков после запятой, заканчивается на « s ». Пример: "3.5s" .

Пример запроса

Питон

from google import genai
from google.genai import types
import datetime

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={
        "contents": [document],
        "system_instruction": "You are an expert analyzing transcripts.",
    },
)

# Update the cache's time-to-live (ttl)
ttl = f"{int(datetime.timedelta(hours=2).total_seconds())}s"
client.caches.update(
    name=cache.name, config=types.UpdateCachedContentConfig(ttl=ttl)
)
print(f"After update:\n {cache}")

# Alternatively, update the expire_time directly
# Update the expire_time directly in valid RFC 3339 format (UTC with a "Z" suffix)
expire_time = (
    (
        datetime.datetime.now(datetime.timezone.utc)
        + datetime.timedelta(minutes=15)
    )
    .isoformat()
    .replace("+00:00", "Z")
)
client.caches.update(
    name=cache.name,
    config=types.UpdateCachedContentConfig(expire_time=expire_time),
)

Node.js

// 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)),
];

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

// Update the cache's time-to-live (ttl)
const ttl = `${2 * 3600}s`; // 2 hours in seconds
cache = await ai.caches.update({
  name: cache.name,
  config: { ttl },
});
console.log("After update (TTL):", cache);

// Alternatively, update the expire_time directly (in RFC 3339 format with a "Z" suffix)
const expireTime = new Date(Date.now() + 15 * 60000)
  .toISOString()
  .replace(/\.\d{3}Z$/, "Z");
cache = await ai.caches.update({
  name: cache.name,
  config: { expireTime: expireTime },
});
console.log("After update (expire_time):", cache);

Идти

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)
}

_, err = client.Caches.Delete(ctx, cache.Name, &genai.DeleteCachedContentConfig{})
if err != nil {
	log.Fatal(err)
}
fmt.Println("Cache deleted:", cache.Name)

Оболочка

curl -X PATCH "https://generativelanguage.googleapis.com/v1beta/$CACHE_NAME?key=$GEMINI_API_KEY" \
 -H 'Content-Type: application/json' \
 -d '{"ttl": "600s"}'

Тело ответа

В случае успеха тело ответа содержит экземпляр CachedContent .

Метод: cachedContents.delete

Удаляет ресурс CachedContent.

Конечная точка

удалить https: / /generativelanguage.googleapis.com /v1beta /{name=cachedContents /*}

Параметры пути

string name

Обязательно. Имя ресурса, ссылающегося на запись кэша контента. Формат: cachedContents/{id} Имеет вид cachedContents/{cachedcontent} .

Текст запроса

Тело запроса должно быть пустым.

Пример запроса

Питон

from google import genai

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={
        "contents": [document],
        "system_instruction": "You are an expert analyzing transcripts.",
    },
)
client.caches.delete(name=cache.name)

Node.js

// 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.",
  },
});
await ai.caches.delete({ name: cache.name });
console.log("Cache deleted:", cache.name);

Идти

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)
}

_, err = client.Caches.Delete(ctx, cache.Name, &genai.DeleteCachedContentConfig{})
if err != nil {
	log.Fatal(err)
}
fmt.Println("Cache deleted:", cache.Name)

Оболочка

curl -X DELETE "https://generativelanguage.googleapis.com/v1beta/$CACHE_NAME?key=$GEMINI_API_KEY"

Тело ответа

В случае успеха тело ответа представляет собой пустой объект JSON.

Ресурс REST: cachedContents

Ресурс: CachedContent

Контент, который был предварительно обработан и может быть использован в последующем запросе к GenerativeService.

Кэшированный контент можно использовать только с той моделью, для которой он был создан.

Поля
contents[] object ( Content )

Необязательно. Только ввод. Неизменяемо. Содержимое для кэширования.

tools[] object ( Tool )

Необязательный. Только входные данные. Неизменяемый. Список Tools модель может использовать для генерации следующего ответа.

createTime string ( Timestamp format)

Только вывод. Время создания записи в кэше.

Использует RFC 3339, согласно которому сгенерированный вывод всегда будет нормализован по оси Z и содержать 0, 3, 6 или 9 знаков после запятой. Также допускаются смещения, отличные от «Z». Примеры: "2014-10-02T15:01:23Z" , "2014-10-02T15:01:23.045123456Z" или "2014-10-02T15:01:23+05:30" .

строка updateTime string ( Timestamp format)

Только вывод. Время последнего обновления записи в кэше (по UTC).

Использует RFC 3339, согласно которому сгенерированный вывод всегда будет нормализован по оси Z и содержать 0, 3, 6 или 9 знаков после запятой. Также допускаются смещения, отличные от «Z». Примеры: "2014-10-02T15:01:23Z" , "2014-10-02T15:01:23.045123456Z" или "2014-10-02T15:01:23+05:30" .

объект usageMetadata object ( UsageMetadata )

Только вывод. Метаданные об использовании кэшированного контента.

expiration Union type
Указывает, когда истекает срок действия этого ресурса. expiration может быть только одним из следующих:
строка expireTime string ( Timestamp format)

Временная метка в формате UTC, когда ресурс считается истёкшим. Она всегда указывается на выходе, независимо от того, что было отправлено на входе.

Использует RFC 3339, согласно которому сгенерированный вывод всегда будет нормализован по оси Z и содержать 0, 3, 6 или 9 знаков после запятой. Также допускаются смещения, отличные от «Z». Примеры: "2014-10-02T15:01:23Z" , "2014-10-02T15:01:23.045123456Z" или "2014-10-02T15:01:23+05:30" .

строка ttl string ( Duration format)

Только ввод. Новый TTL для этого ресурса, только ввод.

Длительность в секундах, содержащая до девяти знаков после запятой, заканчивается на « s ». Пример: "3.5s" .

string name

Только вывод. Идентификатор. Имя ресурса, относящегося к кэшированному содержимому. Формат: cachedContents/{id}

string displayName

Необязательно. Неизменяемо. Созданное пользователем осмысленное отображаемое имя кэшированного контента. Максимум 128 символов Unicode.

string model

Обязательно. Неизменяемо. Имя Model , используемой для кэшированного контента. Формат: models/{model}

object ( Content ) systemInstruction (Контент)

Необязательно. Только ввод. Неизменяемо. Системная инструкция, заданная разработчиком. В настоящее время только текст.

объект toolConfig object ( ToolConfig )

Необязательно. Только ввод. Неизменяемо. Конфигурация инструмента. Эта конфигурация общая для всех инструментов.

JSON-представление
{
  "contents": [
    {
      object (Content)
    }
  ],
  "tools": [
    {
      object (Tool)
    }
  ],
  "createTime": string,
  "updateTime": string,
  "usageMetadata": {
    object (UsageMetadata)
  },

  // expiration
  "expireTime": string,
  "ttl": string
  // Union type
  "name": string,
  "displayName": string,
  "model": string,
  "systemInstruction": {
    object (Content)
  },
  "toolConfig": {
    object (ToolConfig)
  }
}

Содержание

Базовый структурированный тип данных, содержащий многочастное содержимое сообщения.

Content включает поле role , обозначающее производителя Content , и поле parts , содержащее многокомпонентные данные, включающие содержимое поворота сообщения.

Поля
parts[] object ( Part )

Упорядоченные Parts , составляющие единое сообщение. Части могут иметь разные типы MIME.

string role

Необязательно. Производитель контента. Должен быть либо «пользователь», либо «модель».

Полезно устанавливать для многооборотных разговоров, в противном случае можно оставить пустым или не устанавливать.

JSON-представление
{
  "parts": [
    {
      object (Part)
    }
  ],
  "role": string
}

Часть

Тип данных, содержащий медиа-данные, являющиеся частью многокомпонентного сообщения Content .

Part состоит из данных, имеющих связанный тип данных. Part может содержать только один из допустимых типов в Part.data .

Part должна иметь фиксированный тип IANA MIME, определяющий тип и подтип носителя, если поле inlineData заполнено необработанными байтами.

Поля
thought boolean

Необязательно. Указывает, была ли деталь разработана на основе модели.

string ( bytes format) thoughtSignature (формат байтов)

Необязательно. Непрозрачная подпись для мысли, чтобы её можно было использовать повторно в последующих запросах.

Строка в кодировке base64.

объект partMetadata object ( Struct format)

Пользовательские метаданные, связанные с частью. Агентам, использующим genai.Part в качестве представления контента, может потребоваться отслеживать дополнительную информацию. Например, это может быть имя файла/источника, из которого получена часть, или способ мультиплексирования нескольких потоков частей.

Union type data
data могут быть только одним из следующих:
text string

Встроенный текст.

объект inlineData object ( Blob )

Встроенные медиа-байты.

object ( FunctionCall ) functionCall ( FunctionCall )

Предсказанный FunctionCall возвращаемый из модели, содержащий строку, представляющую FunctionDeclaration.name с аргументами и их значениями.

object ( FunctionResponse ) functionResponse ( FunctionResponse )

Результат вывода FunctionCall , содержащий строку, представляющую FunctionDeclaration.name , и структурированный объект JSON, содержащий любые выходные данные функции, используется в качестве контекста для модели.

объект fileData object ( FileData )

Данные на основе URI.

object ( ExecutableCode ) executableCode (ExecutableCode)

Код, сгенерированный моделью и предназначенный для выполнения.

object ( CodeExecutionResult ) codeExecutionResult ( CodeExecutionResult )

Результат выполнения ExecutableCode .

metadata Union type
Управляет дополнительной предварительной обработкой данных. metadata могут быть только одними из следующих:
object ( VideoMetadata ) videoMetadata ( VideoMetadata )

Необязательно. Метаданные видео. Метаданные следует указывать только в том случае, если видеоданные представлены в inlineData или fileData.

JSON-представление
{
  "thought": boolean,
  "thoughtSignature": string,
  "partMetadata": {
    object
  },

  // data
  "text": string,
  "inlineData": {
    object (Blob)
  },
  "functionCall": {
    object (FunctionCall)
  },
  "functionResponse": {
    object (FunctionResponse)
  },
  "fileData": {
    object (FileData)
  },
  "executableCode": {
    object (ExecutableCode)
  },
  "codeExecutionResult": {
    object (CodeExecutionResult)
  }
  // Union type

  // metadata
  "videoMetadata": {
    object (VideoMetadata)
  }
  // Union type
}

Клякса

Необработанные медиа-байты.

Текст не следует отправлять в виде необработанных байтов, используйте поле «текст».

Поля
string mimeType

Стандартный тип MIME IANA для исходных данных. Примеры: - image/png - image/jpeg. Если указан неподдерживаемый тип MIME, будет возвращена ошибка. Полный список поддерживаемых типов см. в разделе «Поддерживаемые форматы файлов» .

string ( bytes format) data (формат байтов)

Необработанные байты для медиаформатов.

Строка в кодировке base64.

JSON-представление
{
  "mimeType": string,
  "data": string
}

FunctionCall

Предсказанный FunctionCall возвращаемый из модели, содержащий строку, представляющую FunctionDeclaration.name с аргументами и их значениями.

Поля
string id

Необязательно. Уникальный идентификатор вызова функции. Если он заполнен, клиент выполнит functionCall и вернет ответ с соответствующим id .

string name

Обязательно. Имя вызываемой функции. Имя должно содержать символы az, AZ, 0-9 или символы подчеркивания и тире. Максимальная длина — 64 символа.

объект args object ( Struct format)

Необязательно. Параметры и значения функции в формате JSON-объекта.

JSON-представление
{
  "id": string,
  "name": string,
  "args": {
    object
  }
}

ФункцияОтвет

Результат вызова FunctionCall , содержащий строку, представляющую FunctionDeclaration.name , и структурированный JSON-объект, содержащий все выходные данные функции, используется в качестве контекста для модели. Он должен содержать результат вызова FunctionCall , выполненного на основе прогноза модели.

Поля
string id

Необязательно. Идентификатор вызова функции, для которой предназначен этот ответ. Заполняется клиентом в соответствии с id вызова функции.

string name

Обязательно. Имя вызываемой функции. Имя должно содержать символы az, AZ, 0-9 или символы подчеркивания и тире. Максимальная длина — 64 символа.

object ( Struct format) response (формат Struct)

Обязательно. Ответ функции в формате JSON-объекта. Вызывающие могут использовать любые ключи по своему выбору, соответствующие синтаксису функции, для возврата выходных данных функции, например, «output», «result» и т. д. В частности, если вызов функции не удалось выполнить, ответ может содержать ключ «error» для возврата информации об ошибке в модель.

parts[] object ( FunctionResponsePart )

Необязательные. Упорядоченные Parts , составляющие ответ функции. Части могут иметь разные типы MIME IANA.

willContinue boolean

Необязательно. Сигнализирует о продолжении вызова функции и о том, что будут возвращены дополнительные ответы, превращая вызов функции в генератор. Применимо только к вызовам функций NON_BLOCKING, в противном случае игнорируется. Если установлено значение false, последующие ответы не будут учитываться. Разрешается возвращать пустой response с willContinue=False чтобы сигнализировать о завершении вызова функции. Это может привести к запуску генерации модели. Чтобы избежать запуска генерации и завершить вызов функции, дополнительно установите scheduling значение SILENT .

scheduling enum ( Scheduling )

Необязательно. Указывает, как следует планировать ответ в диалоге. Применимо только к вызовам функций NON_BLOCKING, в противном случае игнорируется. По умолчанию WHEN_IDLE.

JSON-представление
{
  "id": string,
  "name": string,
  "response": {
    object
  },
  "parts": [
    {
      object (FunctionResponsePart)
    }
  ],
  "willContinue": boolean,
  "scheduling": enum (Scheduling)
}

ФункцияОтветЧасть

Тип данных, содержащий медиа, являющийся частью сообщения FunctionResponse .

FunctionResponsePart состоит из данных, имеющих связанный тип данных. FunctionResponsePart может содержать только один из допустимых типов в FunctionResponsePart.data .

FunctionResponsePart должен иметь фиксированный тип IANA MIME, определяющий тип и подтип носителя, если поле inlineData заполнено необработанными байтами.

Поля
Union type data
Данные части ответа функции. data могут быть только одними из следующих:
объект inlineData object ( FunctionResponseBlob )

Встроенные медиа-байты.

JSON-представление
{

  // data
  "inlineData": {
    object (FunctionResponseBlob)
  }
  // Union type
}

ФункцияResponseBlob

Необработанные медиа-байты для ответа функции.

Текст не следует отправлять в виде необработанных байтов, используйте поле «FunctionResponse.response».

Поля
string mimeType

Стандартный тип MIME IANA для исходных данных. Примеры: - image/png - image/jpeg. Если указан неподдерживаемый тип MIME, будет возвращена ошибка. Полный список поддерживаемых типов см. в разделе «Поддерживаемые форматы файлов» .

string ( bytes format) data (формат байтов)

Необработанные байты для медиаформатов.

Строка в кодировке base64.

JSON-представление
{
  "mimeType": string,
  "data": string
}

Планирование

Указывает, как следует планировать ответ в разговоре.

Перечисления
SCHEDULING_UNSPECIFIED Это значение не используется.
SILENT Только добавляйте результат в контекст беседы, не прерывайте и не запускайте генерацию.
WHEN_IDLE Добавьте результат в контекст беседы и подсказывайте, как сгенерировать вывод, не прерывая текущую генерацию.
INTERRUPT Добавить результат в контекст разговора, прервать текущую генерацию и предложить сгенерировать вывод.

FileData

Данные на основе URI.

Поля
string mimeType

Необязательно. Стандартный тип MIME IANA для исходных данных.

string fileUri

Обязательно. URI.

JSON-представление
{
  "mimeType": string,
  "fileUri": string
}

ИсполняемыйКод

Код, сгенерированный моделью, предназначенный для выполнения, и результат, возвращаемый модели.

Генерируется только при использовании инструмента CodeExecution , в котором код будет автоматически выполнен, а также будет сгенерирован соответствующий CodeExecutionResult .

Поля
перечисление language enum ( Language )

Обязательно. Язык программирования code .

code string

Обязательно. Код для выполнения.

JSON-представление
{
  "language": enum (Language),
  "code": string
}

Язык

Поддерживаемые языки программирования для сгенерированного кода.

Перечисления
LANGUAGE_UNSPECIFIED Язык не указан. Это значение не следует использовать.
PYTHON Python >= 3.10, с доступными numpy и simpy.

CodeExecutionResult

Результат выполнения ExecutableCode .

Генерируется только при использовании CodeExecution и всегда следует за part содержащей ExecutableCode .

Поля
перечисление outcome enum ( Outcome )

Обязательно. Результат выполнения кода.

output string

Необязательно. Содержит stdout при успешном выполнении кода, stderr или другое описание в противном случае.

JSON-представление
{
  "outcome": enum (Outcome),
  "output": string
}

Исход

Перечисление возможных результатов выполнения кода.

Перечисления
OUTCOME_UNSPECIFIED Статус не указан. Это значение не следует использовать.
OUTCOME_OK Выполнение кода завершено успешно.
OUTCOME_FAILED Выполнение кода завершено, но с ошибкой. stderr должен содержать причину.
OUTCOME_DEADLINE_EXCEEDED Выполнение кода заняло слишком много времени и было отменено. Возможно, присутствует частичный вывод, а возможно, и нет.

ВидеоМетаданные

Метаданные описывают входной видеоконтент.

Поля
startOffset string ( Duration format)

Необязательно. Начальное смещение видео.

Длительность в секундах, содержащая до девяти знаков после запятой, заканчивается на « s ». Пример: "3.5s" .

endOffset string ( Duration format)

Необязательно. Смещение конца видео.

Длительность в секундах, содержащая до девяти знаков после запятой, заканчивается на « s ». Пример: "3.5s" .

number fps

Необязательно. Частота кадров видео, отправляемого модели. Если не указано, по умолчанию будет использоваться значение 1,0. Диапазон кадров в секунду: (0,0, 24,0).

JSON-представление
{
  "startOffset": string,
  "endOffset": string,
  "fps": number
}

Инструмент

Подробная информация об инструментах, которые модель может использовать для генерации ответа.

Tool — это фрагмент кода, который позволяет системе взаимодействовать с внешними системами для выполнения действия или набора действий, выходящих за рамки знаний и области действия модели.

Следующий идентификатор: 12

Поля
functionDeclarations[] object ( FunctionDeclaration )

Необязательно. Список доступных модели FunctionDeclarations , которые можно использовать для вызова функций.

Модель или система не выполняет функцию. Вместо этого определённая функция может быть возвращена как FunctionCall с аргументами на клиентскую сторону для выполнения. Модель может принять решение о вызове подмножества этих функций, заполнив FunctionCall в ответе. Следующий этап диалога может содержать FunctionResponse с контекстом генерации Content.role «function» для следующего этапа модели.

object ( GoogleSearchRetrieval ) googleSearchRetrieval ( GoogleSearchRetrieval )

Необязательно. Инструмент поиска, работающий на основе поиска Google.

object ( CodeExecution ) codeExecution ( CodeExecution )

Необязательно. Позволяет модели выполнять код в процессе генерации.

объект computerUse object ( ComputerUse )

Необязательный. Инструмент для поддержки прямого взаимодействия модели с компьютером. При включении он автоматически заполняет объявления функций, специфичные для конкретного компьютера.

object ( UrlContext ) urlContext ( UrlContext )

Необязательно. Инструмент для поддержки поиска контекста URL.

JSON-представление
{
  "functionDeclarations": [
    {
      object (FunctionDeclaration)
    }
  ],
  "googleSearchRetrieval": {
    object (GoogleSearchRetrieval)
  },
  "codeExecution": {
    object (CodeExecution)
  },
  "googleSearch": {
    object (GoogleSearch)
  },
  "computerUse": {
    object (ComputerUse)
  },
  "urlContext": {
    object (UrlContext)
  },
  "fileSearch": {
    object (FileSearch)
  }
}

Объявление функции

Структурированное представление объявления функции, как определено в спецификации OpenAPI 3.03 . В это объявление включены имя функции и параметры. Это объявление FunctionDeclaration представляет собой блок кода, который может использоваться моделью в качестве Tool и выполняться клиентом.

Поля
string name

Обязательно. Имя функции. Должно содержать символы az, AZ, 0-9 или символы подчеркивания, двоеточия, точки и тире. Максимальная длина — 64 символа.

string description

Обязательно. Краткое описание функции.

перечисление behavior enum ( Behavior )

Необязательно. Задаёт поведение функции. В настоящее время поддерживается только методом BidiGenerateContent.

объект parameters object ( Schema )

Необязательно. Описывает параметры этой функции. Соответствует строке объекта параметра Open API 3.03. Ключ: имя параметра. Имена параметров чувствительны к регистру. Значение схемы: схема, определяющая тип, используемый для параметра.

Значение parametersJsonSchema value ( Value format)

Необязательно. Описывает параметры функции в формате JSON-схемы. Схема должна описывать объект, свойства которого являются параметрами функции. Например:

{
  "type": "object",
  "properties": {
    "name": { "type": "string" },
    "age": { "type": "integer" }
  },
  "additionalProperties": false,
  "required": ["name", "age"],
  "propertyOrdering": ["name", "age"]
}

Это поле является взаимоисключающим с parameters .

объект response object ( Schema )

Необязательно. Описывает выходные данные этой функции в формате схемы JSON. Соответствует объекту ответа Open API 3.03. Схема определяет тип, используемый для значения ответа функции.

Значение responseJsonSchema value ( Value format)

Необязательно. Описывает выходные данные этой функции в формате схемы JSON. Значение, указанное в схеме, является значением ответа функции.

Это поле является взаимоисключающим с response .

JSON-представление
{
  "name": string,
  "description": string,
  "behavior": enum (Behavior),
  "parameters": {
    object (Schema)
  },
  "parametersJsonSchema": value,
  "response": {
    object (Schema)
  },
  "responseJsonSchema": value
}

Схема

Объект Schema позволяет определять типы входных и выходных данных. Эти типы могут быть объектами, а также примитивами и массивами. Представляет собой выбранное подмножество объекта схемы OpenAPI 3.0 .

Поля
type enum ( Type )

Обязательно. Тип данных.

string format

Необязательно. Формат данных. Допускается любое значение, но большинство из них не активируют какую-либо специальную функциональность.

string title

Необязательно. Название схемы.

string description

Необязательно. Краткое описание параметра. Может содержать примеры использования. Описание параметра можно отформатировать в формате Markdown.

boolean nullable

Необязательно. Указывает, может ли значение быть нулевым.

enum[] string

Необязательно. Возможные значения элемента Type.STRING в формате enum. Например, мы можем определить Enum Direction следующим образом: {type:STRING, format:enum, enum:["EAST", NORTH", "SOUTH", "WEST"]}

string ( int64 format) maxItems (формат int64)

Необязательно. Максимальное количество элементов для Type.ARRAY.

string ( int64 format) minItems (формат int64)

Необязательно. Минимальное количество элементов для Type.ARRAY.

properties map (key: string, value: object ( Schema ))

Необязательно. Свойства типа.OBJECT.

Объект, содержащий список пар "key": value . Пример: { "name": "wrench", "mass": "1.3kg", "count": "3" } .

required[] string

Необязательно. Обязательные свойства Type.OBJECT.

string ( int64 format) minProperties (формат int64)

Необязательно. Минимальное количество свойств для Type.OBJECT.

string ( int64 format) maxProperties (формат int64)

Необязательно. Максимальное количество свойств для Type.OBJECT.

minLength string ( int64 format)

Необязательно. ПОЛЯ СХЕМЫ ДЛЯ ТИПА STRING Минимальная длина Type.STRING

maxLength string ( int64 format)

Необязательно. Максимальная длина Type.STRING

string pattern

Необязательно. Шаблон Type.STRING для ограничения строки регулярным выражением.

example value ( Value format)

Необязательно. Пример объекта. Заполняется только в том случае, если объект является корневым.

anyOf[] object ( Schema )

Необязательно. Значение должно быть проверено по любой (одной или нескольким) подсхемам в списке.

propertyOrdering[] string

Необязательно. Порядок свойств. Нестандартное поле в спецификации Open API. Используется для определения порядка свойств в ответе.

значение default value ( Value format)

Необязательное. Значение поля по умолчанию. Согласно схеме JSON, это поле предназначено для генераторов документации и не влияет на валидацию. Поэтому оно включено сюда и игнорируется, чтобы разработчики, отправляющие схемы с полем default не получали ошибок неизвестного поля.

объект items object ( Schema )

Необязательно. Схема элементов Type.ARRAY.

minimum number

Необязательно. ПОЛЯ СХЕМЫ ДЛЯ ТИПА INTEGER и NUMBER Минимальное значение Type.INTEGER и Type.NUMBER

maximum number

Необязательно. Максимальное значение Type.INTEGER и Type.NUMBER.

JSON-представление
{
  "type": enum (Type),
  "format": string,
  "title": string,
  "description": string,
  "nullable": boolean,
  "enum": [
    string
  ],
  "maxItems": string,
  "minItems": string,
  "properties": {
    string: {
      object (Schema)
    },
    ...
  },
  "required": [
    string
  ],
  "minProperties": string,
  "maxProperties": string,
  "minLength": string,
  "maxLength": string,
  "pattern": string,
  "example": value,
  "anyOf": [
    {
      object (Schema)
    }
  ],
  "propertyOrdering": [
    string
  ],
  "default": value,
  "items": {
    object (Schema)
  },
  "minimum": number,
  "maximum": number
}

Тип

Тип содержит список типов данных OpenAPI, как определено в https://spec.openapis.org/oas/v3.0.3#data-types

Перечисления
TYPE_UNSPECIFIED Не указано, не следует использовать.
STRING Тип строки.
NUMBER Тип числа.
INTEGER Целочисленный тип.
BOOLEAN Булевский тип.
ARRAY Тип массива.
OBJECT Тип объекта.
NULL Нулевой тип.

Поведение

Определяет поведение функции. По умолчанию — BLOCKING .

Перечисления
UNSPECIFIED Это значение не используется.
BLOCKING Если установлено, система будет ждать ответа функции, прежде чем продолжить разговор.
NON_BLOCKING Если установлено, система не будет дожидаться ответа функции. Вместо этого она будет пытаться обрабатывать ответы функций по мере их поступления, поддерживая при этом диалог между пользователем и моделью.

GoogleSearchRetrieval

Инструмент для извлечения общедоступных веб-данных для заземления, работающий на базе Google.

Поля
object ( DynamicRetrievalConfig ) dynamicRetrievalConfig ( DynamicRetrievalConfig )

Задает конфигурацию динамического извлечения для заданного источника.

JSON-представление
{
  "dynamicRetrievalConfig": {
    object (DynamicRetrievalConfig)
  }
}

DynamicRetrievalConfig

Описывает возможности настройки динамического поиска.

Поля
mode enum ( Mode )

Режим предиктора, который будет использоваться при динамическом поиске.

dynamicThreshold number

Пороговое значение, используемое при динамическом поиске. Если не задано, используется системное значение по умолчанию.

JSON-представление
{
  "mode": enum (Mode),
  "dynamicThreshold": number
}

Режим

Режим предиктора, который будет использоваться при динамическом поиске.

Перечисления
MODE_UNSPECIFIED Всегда запускайте извлечение.
MODE_DYNAMIC Запускайте извлечение только тогда, когда система решит, что это необходимо.

CodeExecution

Этот тип не имеет полей.

Инструмент, который выполняет код, сгенерированный моделью, и автоматически возвращает результат модели.

См. также ExecutableCode и CodeExecutionResult , которые генерируются только при использовании этого инструмента.

GoogleSearch

Тип инструмента GoogleSearch. Инструмент для поддержки поиска Google в модели. Работает на базе Google.

Поля
object ( Interval ) timeRangeFilter (Интервал)

Необязательно. Фильтрация результатов поиска по определённому временному диапазону. Если клиенты указали время начала, им необходимо указать время окончания (и наоборот).

JSON-представление
{
  "timeRangeFilter": {
    object (Interval)
  }
}

Интервал

Представляет временной интервал, закодированный как начальная метка времени (включительно) и конечная метка времени (исключая).

Начало должно быть меньше или равно концу. Если начало равно концу, интервал пустой (не соответствует ни одному времени). Если ни начало, ни конец не указаны, интервал соответствует любому времени.

Поля
строка startTime string ( Timestamp format)

Необязательно. Начало интервала включено.

Если указано, то соответствующая этому интервалу временная метка должна быть такой же или более поздней.

Использует RFC 3339, согласно которому сгенерированный вывод всегда будет нормализован по оси Z и содержать 0, 3, 6 или 9 знаков после запятой. Также допускаются смещения, отличные от «Z». Примеры: "2014-10-02T15:01:23Z" , "2014-10-02T15:01:23.045123456Z" или "2014-10-02T15:01:23+05:30" .

строка endTime string ( Timestamp format)

Необязательно. Исключительный конец интервала.

Если указано, то соответствующая этому интервалу временная метка должна быть до конца.

Использует RFC 3339, согласно которому сгенерированный вывод всегда будет нормализован по оси Z и содержать 0, 3, 6 или 9 знаков после запятой. Также допускаются смещения, отличные от «Z». Примеры: "2014-10-02T15:01:23Z" , "2014-10-02T15:01:23.045123456Z" или "2014-10-02T15:01:23+05:30" .

JSON-представление
{
  "startTime": string,
  "endTime": string
}

ComputerUse

Тип инструмента «Используйте компьютер».

Поля
перечисление environment enum ( Environment )

Обязательно. Среда, в которой будет осуществляться эксплуатация.

string excludedPredefinedFunctions[]

Необязательно. По умолчанию предопределённые функции включаются в финальный вызов модели. Некоторые из них можно явно исключить из автоматического включения. Это может служить двум целям: 1. Использование более ограниченного/иного пространства действий. 2. Улучшение определений/инструкций предопределённых функций.

JSON-представление
{
  "environment": enum (Environment),
  "excludedPredefinedFunctions": [
    string
  ]
}

Среда

Представляет собой рабочую среду, например веб-браузер.

Перечисления
ENVIRONMENT_UNSPECIFIED По умолчанию используется браузер.
ENVIRONMENT_BROWSER Работает в веб-браузере.

UrlContext

Этот тип не имеет полей.

Инструмент для поддержки поиска контекста URL.

FileSearch

Инструмент FileSearch для извлечения информации из корпусов семантического поиска. Файлы импортируются в корпусы семантического поиска с помощью API ImportFile.

Поля
object ( RetrievalResource ) retrievalResources[] ( RetrievalResource )

Обязательно. Ресурсы семантического поиска. В настоящее время поддерживается только один корпус. В будущем мы можем добавить поддержку нескольких корпусов.

object ( RetrievalConfig ) retrievalConfig ( RetrievalConfig )

Необязательно. Конфигурация для извлечения.

JSON-представление
{
  "retrievalResources": [
    {
      object (RetrievalResource)
    }
  ],
  "retrievalConfig": {
    object (RetrievalConfig)
  }
}

RetrievalResource

Ресурс семантического поиска, из которого следует извлечь информацию.

Поля
string ragStoreName

Обязательно. Имя семантического поискового ресурса, из которого нужно извлечь данные. Пример: ragStores/my-rag-store-123

JSON-представление
{
  "ragStoreName": string
}

RetrievalConfig

Конфигурация семантического поиска.

Поля
metadataFilter string

Необязательно. Фильтр метаданных для применения к документам и фрагментам семантического поиска.

topK integer

Необязательно. Количество семантических фрагментов для извлечения.

JSON-представление
{
  "metadataFilter": string,
  "topK": integer
}

ToolConfig

Конфигурация инструмента, содержащая параметры для указания использования Tool в запросе.

Поля
object ( FunctionCallingConfig ) functionCallingConfig ( FunctionCallingConfig )

Необязательно. Функция, вызывающая конфигурацию.

JSON-представление
{
  "functionCallingConfig": {
    object (FunctionCallingConfig)
  }
}

FunctionCallingConfig

Конфигурация для указания поведения вызова функций.

Поля
mode enum ( Mode )

Необязательный параметр. Указывает режим, в котором должен выполняться вызов функции. Если параметр не указан, по умолчанию будет установлено значение AUTO.

string allowedFunctionNames[]

Необязательный. Набор имён функций, который, если указан, ограничивает функции, вызываемые моделью.

Этот параметр следует устанавливать только в том случае, если режим имеет значение ANY или VALIDATED. Имена функций должны соответствовать [FunctionDeclaration.name]. Если этот параметр установлен, модель будет предсказывать вызов функции только на основе разрешённых имён функций.

JSON-представление
{
  "mode": enum (Mode),
  "allowedFunctionNames": [
    string
  ]
}

Режим

Определяет поведение выполнения для вызова функции путем определения режима выполнения.

Перечисления
MODE_UNSPECIFIED Не указан режим вызова функции. Это значение не следует использовать.
AUTO Поведение модели по умолчанию: модель решает предсказать либо вызов функции, либо ответ на естественном языке.
ANY Модель ограничена тем, что всегда предсказывает только вызов функции. Если задано "allowedFunctionNames", то предсказанным вызовом функции будет любое из "allowedFunctionNames", в противном случае предсказанным вызовом функции будет любое из предоставленных "functionDeclarations".
NONE Модель не предскажет ни одного вызова функции. Поведение модели такое же, как и без объявления функций.
VALIDATED Модель решает предсказать либо вызов функции, либо ответ на естественном языке, но будет проверять вызовы функций с ограниченным декодированием. Если задано "allowedFunctionNames", предсказанный вызов функции будет ограничен любым из "allowedFunctionNames", в противном случае предсказанный вызов функции будет любым из предоставленных "functionDeclarations".

ИспользованиеМетаданных

Метаданные об использовании кэшированного контента.

Поля
totalTokenCount integer

Общее количество токенов, потребляемых кэшированным контентом.

JSON-представление
{
  "totalTokenCount": integer
}
,

Кэширование контекста позволяет сохранять и повторно использовать предварительно вычисленные входные токены, которые вы хотите использовать повторно, например, при постановке разных вопросов об одном и том же медиафайле. Это может привести к экономии средств и повышению скорости, в зависимости от использования. Подробное введение см. в руководстве по кэшированию контекста .

Метод: cachedContents.create

Создает ресурс CachedContent.

Конечная точка

пост https: / /generativelanguage.googleapis.com /v1beta /cachedContents

Текст запроса

Тело запроса содержит экземпляр CachedContent .

Поля
contents[] object ( Content )

Необязательно. Только ввод. Неизменяемо. Содержимое для кэширования.

tools[] object ( Tool )

Необязательный. Только входные данные. Неизменяемый. Список Tools модель может использовать для генерации следующего ответа.

expiration Union type
Указывает, когда истекает срок действия этого ресурса. expiration может быть только одним из следующих:
строка expireTime string ( Timestamp format)

Временная метка в формате UTC, когда ресурс считается истёкшим. Она всегда указывается на выходе, независимо от того, что было отправлено на входе.

Использует RFC 3339, согласно которому сгенерированный вывод всегда будет нормализован по оси Z и содержать 0, 3, 6 или 9 знаков после запятой. Также допускаются смещения, отличные от «Z». Примеры: "2014-10-02T15:01:23Z" , "2014-10-02T15:01:23.045123456Z" или "2014-10-02T15:01:23+05:30" .

строка ttl string ( Duration format)

Только ввод. Новый TTL для этого ресурса, только ввод.

Длительность в секундах, содержащая до девяти знаков после запятой, заканчивается на « s ». Пример: "3.5s" .

string displayName

Необязательно. Неизменяемо. Созданное пользователем осмысленное отображаемое имя кэшированного контента. Максимум 128 символов Unicode.

string model

Обязательно. Неизменяемо. Имя Model , используемой для кэшированного контента. Формат: models/{model}

object ( Content ) systemInstruction (Контент)

Необязательно. Только ввод. Неизменяемо. Системная инструкция, заданная разработчиком. В настоящее время только текст.

объект toolConfig object ( ToolConfig )

Optional. Input only. Immutable. Tool config. This config is shared for all tools.

Пример запроса

Базовый

Питон

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)

Node.js

// 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)

Оболочка

wget https://storage.googleapis.com/generativeai-downloads/data/a11.txt
echo '{
  "model": "models/gemini-1.5-flash-001",
  "contents":[
    {
      "parts":[
        {
          "inline_data": {
            "mime_type":"text/plain",
            "data": "'$(base64 $B64FLAGS a11.txt)'"
          }
        }
      ],
    "role": "user"
    }
  ],
  "systemInstruction": {
    "parts": [
      {
        "text": "You are an expert at analyzing transcripts."
      }
    ]
  },
  "ttl": "300s"
}' > request.json

curl -X POST "https://generativelanguage.googleapis.com/v1beta/cachedContents?key=$GEMINI_API_KEY" \
 -H 'Content-Type: application/json' \
 -d @request.json \
 > cache.json

CACHE_NAME=$(cat cache.json | grep '"name":' | cut -d '"' -f 4 | head -n 1)

curl -X POST "https://generativelanguage.googleapis.com/v1beta/models/gemini-1.5-flash-001:generateContent?key=$GEMINI_API_KEY" \
-H 'Content-Type: application/json' \
-d '{
      "contents": [
        {
          "parts":[{
            "text": "Please summarize this transcript"
          }],
          "role": "user"
        },
      ],
      "cachedContent": "'$CACHE_NAME'"
    }'

От имени

Питон

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.",
    ),
)
cache_name = cache.name  # Save the name for later

# Later retrieve the cache
cache = client.caches.get(name=cache_name)
response = client.models.generate_content(
    model=model_name,
    contents="Find a lighthearted moment from this transcript",
    config=types.GenerateContentConfig(cached_content=cache.name),
)
print(response.text)

Node.js

// 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.",
  },
});
const cacheName = cache.name; // Save the name for later

// Later retrieve the cache
const retrievedCache = await ai.caches.get({ name: cacheName });
const response = await ai.models.generateContent({
  model: modelName,
  contents: "Find a lighthearted moment from this transcript",
  config: { cachedContent: retrievedCache.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)
}
cacheName := cache.Name

// Later retrieve the cache.
cache, err = client.Caches.Get(ctx, cacheName, &genai.GetCachedContentConfig{})
if err != nil {
	log.Fatal(err)
}

response, err := client.Models.GenerateContent(
	ctx,
	modelName,
	genai.Text("Find a lighthearted moment from this transcript"),
	&genai.GenerateContentConfig{
		CachedContent: cache.Name,
	},
)
if err != nil {
	log.Fatal(err)
}
fmt.Println("Response from cache (create from name):")
printResponse(response)

From chat

Питон

from google import genai
from google.genai import types

client = genai.Client()
model_name = "gemini-1.5-flash-001"
system_instruction = "You are an expert analyzing transcripts."

# Create a chat session with the given system instruction.
chat = client.chats.create(
    model=model_name,
    config=types.GenerateContentConfig(system_instruction=system_instruction),
)
document = client.files.upload(file=media / "a11.txt")

response = chat.send_message(
    message=["Hi, could you summarize this transcript?", document]
)
print("\n\nmodel:  ", response.text)
response = chat.send_message(
    message=["Okay, could you tell me more about the trans-lunar injection"]
)
print("\n\nmodel:  ", response.text)

# To cache the conversation so far, pass the chat history as the list of contents.
cache = client.caches.create(
    model=model_name,
    config={
        "contents": chat.get_history(),
        "system_instruction": system_instruction,
    },
)
# Continue the conversation using the cached content.
chat = client.chats.create(
    model=model_name,
    config=types.GenerateContentConfig(cached_content=cache.name),
)
response = chat.send_message(
    message="I didn't understand that last part, could you explain it in simpler language?"
)
print("\n\nmodel:  ", response.text)

Node.js

// Make sure to include the following import:
// import {GoogleGenAI} from '@google/genai';
const ai = new GoogleGenAI({ apiKey: process.env.GEMINI_API_KEY });
const modelName = "gemini-1.5-flash-001";
const systemInstruction = "You are an expert analyzing transcripts.";

// Create a chat session with the system instruction.
const chat = ai.chats.create({
  model: modelName,
  config: { systemInstruction: systemInstruction },
});
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);

let response = await chat.sendMessage({
  message: createUserContent([
    "Hi, could you summarize this transcript?",
    createPartFromUri(document.uri, document.mimeType),
  ]),
});
console.log("\n\nmodel:", response.text);

response = await chat.sendMessage({
  message: "Okay, could you tell me more about the trans-lunar injection",
});
console.log("\n\nmodel:", response.text);

// To cache the conversation so far, pass the chat history as the list of contents.
const chatHistory = chat.getHistory();
const cache = await ai.caches.create({
  model: modelName,
  config: {
    contents: chatHistory,
    systemInstruction: systemInstruction,
  },
});

// Continue the conversation using the cached content.
const chatWithCache = ai.chats.create({
  model: modelName,
  config: { cachedContent: cache.name },
});
response = await chatWithCache.sendMessage({
  message:
    "I didn't understand that last part, could you explain it in simpler language?",
});
console.log("\n\nmodel:", 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"
systemInstruction := "You are an expert analyzing transcripts."

// Create initial chat with a system instruction.
chat, err := client.Chats.Create(ctx, modelName, &genai.GenerateContentConfig{
	SystemInstruction: genai.NewContentFromText(systemInstruction, genai.RoleUser),
}, nil)
if err != nil {
	log.Fatal(err)
}

document, err := client.Files.UploadFromPath(
	ctx, 
	filepath.Join(getMedia(), "a11.txt"), 
	&genai.UploadFileConfig{
		MIMEType : "text/plain",
	},
)
if err != nil {
	log.Fatal(err)
}

// Send first message with the transcript.
parts := make([]genai.Part, 2)
parts[0] = genai.Part{Text: "Hi, could you summarize this transcript?"}
parts[1] = genai.Part{
	FileData: &genai.FileData{
		FileURI :      document.URI,
		MIMEType: document.MIMEType,
	},
}

// Send chat message.
resp, err := chat.SendMessage(ctx, parts...)
if err != nil {
	log.Fatal(err)
}
fmt.Println("\n\nmodel: ", resp.Text())

resp, err = chat.SendMessage(
	ctx, 
	genai.Part{
		Text: "Okay, could you tell me more about the trans-lunar injection",
	},
)
if err != nil {
	log.Fatal(err)
}
fmt.Println("\n\nmodel: ", resp.Text())

// To cache the conversation so far, pass the chat history as the list of contents.
cache, err := client.Caches.Create(ctx, modelName, &genai.CreateCachedContentConfig{
	Contents:          chat.History(false),
	SystemInstruction: genai.NewContentFromText(systemInstruction, genai.RoleUser),
})
if err != nil {
	log.Fatal(err)
}

// Continue the conversation using the cached history.
chat, err = client.Chats.Create(ctx, modelName, &genai.GenerateContentConfig{
	CachedContent: cache.Name,
}, nil)
if err != nil {
	log.Fatal(err)
}

resp, err = chat.SendMessage(
	ctx, 
	genai.Part{
		Text: "I didn't understand that last part, could you explain it in simpler language?",
	},
)
if err != nil {
	log.Fatal(err)
}
fmt.Println("\n\nmodel: ", resp.Text())

Тело ответа

If successful, the response body contains a newly created instance of CachedContent .

Method: cachedContents.list

Lists CachedContents.

Конечная точка

get https: / /generativelanguage.googleapis.com /v1beta /cachedContents

Параметры запроса

pageSize integer

Optional. The maximum number of cached contents to return. The service may return fewer than this value. If unspecified, some default (under maximum) number of items will be returned. The maximum value is 1000; values above 1000 will be coerced to 1000.

pageToken string

Optional. A page token, received from a previous cachedContents.list call. Provide this to retrieve the subsequent page.

When paginating, all other parameters provided to cachedContents.list must match the call that provided the page token.

Текст запроса

Тело запроса должно быть пустым.

Тело ответа

Response with CachedContents list.

В случае успеха тело ответа содержит данные со следующей структурой:

Поля
cachedContents[] object ( CachedContent )

List of cached contents.

nextPageToken string

A token, which can be sent as pageToken to retrieve the next page. If this field is omitted, there are no subsequent pages.

JSON-представление
{
  "cachedContents": [
    {
      object (CachedContent)
    }
  ],
  "nextPageToken": string
}

Method: cachedContents.get

Reads CachedContent resource.

Конечная точка

get https: / /generativelanguage.googleapis.com /v1beta /{name=cachedContents /*}

Параметры пути

string name

Required. The resource name referring to the content cache entry. Format: cachedContents/{id} It takes the form cachedContents/{cachedcontent} .

Текст запроса

Тело запроса должно быть пустым.

Пример запроса

Питон

from google import genai

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={
        "contents": [document],
        "system_instruction": "You are an expert analyzing transcripts.",
    },
)
print(client.caches.get(name=cache.name))

Node.js

// 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.",
  },
});
const retrievedCache = await ai.caches.get({ name: cache.name });
console.log("Retrieved Cache:", retrievedCache);

Идти

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)
}

cache, err = client.Caches.Get(ctx, cache.Name, &genai.GetCachedContentConfig{})
if err != nil {
	log.Fatal(err)
}
fmt.Println("Retrieved cache:")
fmt.Println(cache)

Оболочка

curl "https://generativelanguage.googleapis.com/v1beta/$CACHE_NAME?key=$GEMINI_API_KEY"

Тело ответа

If successful, the response body contains an instance of CachedContent .

Method: cachedContents.patch

Updates CachedContent resource (only expiration is updatable).

Конечная точка

patch https: / /generativelanguage.googleapis.com /v1beta /{cachedContent.name=cachedContents /*}
PATCH https://generativelanguage.googleapis.com/v1beta/{cachedContent.name=cachedContents/*}

Параметры пути

cachedContent.name string

Output only. Identifier. The resource name referring to the cached content. Format: cachedContents/{id} It takes the form cachedContents/{cachedcontent} .

Параметры запроса

updateMask string ( FieldMask format)

The list of fields to update.

This is a comma-separated list of fully qualified names of fields. Example: "user.displayName,photo" .

Текст запроса

The request body contains an instance of CachedContent .

Поля
expiration Union type
Specifies when this resource will expire. expiration can be only one of the following:
expireTime string ( Timestamp format)

Timestamp in UTC of when this resource is considered expired. This is always provided on output, regardless of what was sent on input.

Uses RFC 3339, where generated output will always be Z-normalized and use 0, 3, 6 or 9 fractional digits. Offsets other than "Z" are also accepted. Examples: "2014-10-02T15:01:23Z" , "2014-10-02T15:01:23.045123456Z" or "2014-10-02T15:01:23+05:30" .

ttl string ( Duration format)

Input only. New TTL for this resource, input only.

A duration in seconds with up to nine fractional digits, ending with ' s '. Example: "3.5s" .

Пример запроса

Питон

from google import genai
from google.genai import types
import datetime

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={
        "contents": [document],
        "system_instruction": "You are an expert analyzing transcripts.",
    },
)

# Update the cache's time-to-live (ttl)
ttl = f"{int(datetime.timedelta(hours=2).total_seconds())}s"
client.caches.update(
    name=cache.name, config=types.UpdateCachedContentConfig(ttl=ttl)
)
print(f"After update:\n {cache}")

# Alternatively, update the expire_time directly
# Update the expire_time directly in valid RFC 3339 format (UTC with a "Z" suffix)
expire_time = (
    (
        datetime.datetime.now(datetime.timezone.utc)
        + datetime.timedelta(minutes=15)
    )
    .isoformat()
    .replace("+00:00", "Z")
)
client.caches.update(
    name=cache.name,
    config=types.UpdateCachedContentConfig(expire_time=expire_time),
)

Node.js

// 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)),
];

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

// Update the cache's time-to-live (ttl)
const ttl = `${2 * 3600}s`; // 2 hours in seconds
cache = await ai.caches.update({
  name: cache.name,
  config: { ttl },
});
console.log("After update (TTL):", cache);

// Alternatively, update the expire_time directly (in RFC 3339 format with a "Z" suffix)
const expireTime = new Date(Date.now() + 15 * 60000)
  .toISOString()
  .replace(/\.\d{3}Z$/, "Z");
cache = await ai.caches.update({
  name: cache.name,
  config: { expireTime: expireTime },
});
console.log("After update (expire_time):", cache);

Идти

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)
}

_, err = client.Caches.Delete(ctx, cache.Name, &genai.DeleteCachedContentConfig{})
if err != nil {
	log.Fatal(err)
}
fmt.Println("Cache deleted:", cache.Name)

Оболочка

curl -X PATCH "https://generativelanguage.googleapis.com/v1beta/$CACHE_NAME?key=$GEMINI_API_KEY" \
 -H 'Content-Type: application/json' \
 -d '{"ttl": "600s"}'

Тело ответа

If successful, the response body contains an instance of CachedContent .

Method: cachedContents.delete

Deletes CachedContent resource.

Конечная точка

delete https: / /generativelanguage.googleapis.com /v1beta /{name=cachedContents /*}

Параметры пути

string name

Required. The resource name referring to the content cache entry Format: cachedContents/{id} It takes the form cachedContents/{cachedcontent} .

Текст запроса

Тело запроса должно быть пустым.

Пример запроса

Питон

from google import genai

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={
        "contents": [document],
        "system_instruction": "You are an expert analyzing transcripts.",
    },
)
client.caches.delete(name=cache.name)

Node.js

// 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.",
  },
});
await ai.caches.delete({ name: cache.name });
console.log("Cache deleted:", cache.name);

Идти

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)
}

_, err = client.Caches.Delete(ctx, cache.Name, &genai.DeleteCachedContentConfig{})
if err != nil {
	log.Fatal(err)
}
fmt.Println("Cache deleted:", cache.Name)

Оболочка

curl -X DELETE "https://generativelanguage.googleapis.com/v1beta/$CACHE_NAME?key=$GEMINI_API_KEY"

Тело ответа

В случае успеха тело ответа представляет собой пустой объект JSON.

REST Resource: cachedContents

Resource: CachedContent

Content that has been preprocessed and can be used in subsequent request to GenerativeService.

Cached content can be only used with model it was created for.

Поля
contents[] object ( Content )

Optional. Input only. Immutable. The content to cache.

tools[] object ( Tool )

Optional. Input only. Immutable. A list of Tools the model may use to generate the next response

createTime string ( Timestamp format)

Output only. Creation time of the cache entry.

Uses RFC 3339, where generated output will always be Z-normalized and use 0, 3, 6 or 9 fractional digits. Offsets other than "Z" are also accepted. Examples: "2014-10-02T15:01:23Z" , "2014-10-02T15:01:23.045123456Z" or "2014-10-02T15:01:23+05:30" .

updateTime string ( Timestamp format)

Output only. When the cache entry was last updated in UTC time.

Uses RFC 3339, where generated output will always be Z-normalized and use 0, 3, 6 or 9 fractional digits. Offsets other than "Z" are also accepted. Examples: "2014-10-02T15:01:23Z" , "2014-10-02T15:01:23.045123456Z" or "2014-10-02T15:01:23+05:30" .

usageMetadata object ( UsageMetadata )

Output only. Metadata on the usage of the cached content.

expiration Union type
Specifies when this resource will expire. expiration can be only one of the following:
expireTime string ( Timestamp format)

Timestamp in UTC of when this resource is considered expired. This is always provided on output, regardless of what was sent on input.

Uses RFC 3339, where generated output will always be Z-normalized and use 0, 3, 6 or 9 fractional digits. Offsets other than "Z" are also accepted. Examples: "2014-10-02T15:01:23Z" , "2014-10-02T15:01:23.045123456Z" or "2014-10-02T15:01:23+05:30" .

ttl string ( Duration format)

Input only. New TTL for this resource, input only.

A duration in seconds with up to nine fractional digits, ending with ' s '. Example: "3.5s" .

string name

Output only. Identifier. The resource name referring to the cached content. Format: cachedContents/{id}

displayName string

Optional. Immutable. The user-generated meaningful display name of the cached content. Maximum 128 Unicode characters.

model string

Required. Immutable. The name of the Model to use for cached content Format: models/{model}

systemInstruction object ( Content )

Optional. Input only. Immutable. Developer set system instruction. Currently text only.

toolConfig object ( ToolConfig )

Optional. Input only. Immutable. Tool config. This config is shared for all tools.

JSON-представление
{
  "contents": [
    {
      object (Content)
    }
  ],
  "tools": [
    {
      object (Tool)
    }
  ],
  "createTime": string,
  "updateTime": string,
  "usageMetadata": {
    object (UsageMetadata)
  },

  // expiration
  "expireTime": string,
  "ttl": string
  // Union type
  "name": string,
  "displayName": string,
  "model": string,
  "systemInstruction": {
    object (Content)
  },
  "toolConfig": {
    object (ToolConfig)
  }
}

Содержание

The base structured datatype containing multi-part content of a message.

A Content includes a role field designating the producer of the Content and a parts field containing multi-part data that contains the content of the message turn.

Поля
parts[] object ( Part )

Ordered Parts that constitute a single message. Parts may have different MIME types.

role string

Optional. The producer of the content. Must be either 'user' or 'model'.

Useful to set for multi-turn conversations, otherwise can be left blank or unset.

JSON-представление
{
  "parts": [
    {
      object (Part)
    }
  ],
  "role": string
}

Часть

A datatype containing media that is part of a multi-part Content message.

A Part consists of data which has an associated datatype. A Part can only contain one of the accepted types in Part.data .

A Part must have a fixed IANA MIME type identifying the type and subtype of the media if the inlineData field is filled with raw bytes.

Поля
thought boolean

Optional. Indicates if the part is thought from the model.

thoughtSignature string ( bytes format)

Optional. An opaque signature for the thought so it can be reused in subsequent requests.

A base64-encoded string.

partMetadata object ( Struct format)

Custom metadata associated with the Part. Agents using genai.Part as content representation may need to keep track of the additional information. For example it can be name of a file/source from which the Part originates or a way to multiplex multiple Part streams.

data Union type
data can be only one of the following:
text string

Inline text.

inlineData object ( Blob )

Inline media bytes.

functionCall object ( FunctionCall )

A predicted FunctionCall returned from the model that contains a string representing the FunctionDeclaration.name with the arguments and their values.

functionResponse object ( FunctionResponse )

The result output of a FunctionCall that contains a string representing the FunctionDeclaration.name and a structured JSON object containing any output from the function is used as context to the model.

fileData object ( FileData )

URI based data.

executableCode object ( ExecutableCode )

Code generated by the model that is meant to be executed.

codeExecutionResult object ( CodeExecutionResult )

Result of executing the ExecutableCode .

metadata Union type
Controls extra preprocessing of data. metadata can be only one of the following:
videoMetadata object ( VideoMetadata )

Optional. Video metadata. The metadata should only be specified while the video data is presented in inlineData or fileData.

JSON-представление
{
  "thought": boolean,
  "thoughtSignature": string,
  "partMetadata": {
    object
  },

  // data
  "text": string,
  "inlineData": {
    object (Blob)
  },
  "functionCall": {
    object (FunctionCall)
  },
  "functionResponse": {
    object (FunctionResponse)
  },
  "fileData": {
    object (FileData)
  },
  "executableCode": {
    object (ExecutableCode)
  },
  "codeExecutionResult": {
    object (CodeExecutionResult)
  }
  // Union type

  // metadata
  "videoMetadata": {
    object (VideoMetadata)
  }
  // Union type
}

Клякса

Raw media bytes.

Text should not be sent as raw bytes, use the 'text' field.

Поля
mimeType string

The IANA standard MIME type of the source data. Examples: - image/png - image/jpeg If an unsupported MIME type is provided, an error will be returned. For a complete list of supported types, see Supported file formats .

data string ( bytes format)

Raw bytes for media formats.

A base64-encoded string.

JSON-представление
{
  "mimeType": string,
  "data": string
}

FunctionCall

A predicted FunctionCall returned from the model that contains a string representing the FunctionDeclaration.name with the arguments and their values.

Поля
string id

Optional. The unique id of the function call. If populated, the client to execute the functionCall and return the response with the matching id .

string name

Required. The name of the function to call. Must be az, AZ, 0-9, or contain underscores and dashes, with a maximum length of 64.

args object ( Struct format)

Optional. The function parameters and values in JSON object format.

JSON-представление
{
  "id": string,
  "name": string,
  "args": {
    object
  }
}

FunctionResponse

The result output from a FunctionCall that contains a string representing the FunctionDeclaration.name and a structured JSON object containing any output from the function is used as context to the model. This should contain the result of a FunctionCall made based on model prediction.

Поля
string id

Optional. The id of the function call this response is for. Populated by the client to match the corresponding function call id .

string name

Required. The name of the function to call. Must be az, AZ, 0-9, or contain underscores and dashes, with a maximum length of 64.

response object ( Struct format)

Required. The function response in JSON object format. Callers can use any keys of their choice that fit the function's syntax to return the function output, eg "output", "result", etc. In particular, if the function call failed to execute, the response can have an "error" key to return error details to the model.

parts[] object ( FunctionResponsePart )

Optional. Ordered Parts that constitute a function response. Parts may have different IANA MIME types.

willContinue boolean

Optional. Signals that function call continues, and more responses will be returned, turning the function call into a generator. Is only applicable to NON_BLOCKING function calls, is ignored otherwise. If set to false, future responses will not be considered. It is allowed to return empty response with willContinue=False to signal that the function call is finished. This may still trigger the model generation. To avoid triggering the generation and finish the function call, additionally set scheduling to SILENT .

scheduling enum ( Scheduling )

Optional. Specifies how the response should be scheduled in the conversation. Only applicable to NON_BLOCKING function calls, is ignored otherwise. Defaults to WHEN_IDLE.

JSON-представление
{
  "id": string,
  "name": string,
  "response": {
    object
  },
  "parts": [
    {
      object (FunctionResponsePart)
    }
  ],
  "willContinue": boolean,
  "scheduling": enum (Scheduling)
}

ФункцияОтветЧасть

A datatype containing media that is part of a FunctionResponse message.

A FunctionResponsePart consists of data which has an associated datatype. A FunctionResponsePart can only contain one of the accepted types in FunctionResponsePart.data .

A FunctionResponsePart must have a fixed IANA MIME type identifying the type and subtype of the media if the inlineData field is filled with raw bytes.

Поля
data Union type
The data of the function response part. data can be only one of the following:
inlineData object ( FunctionResponseBlob )

Inline media bytes.

JSON-представление
{

  // data
  "inlineData": {
    object (FunctionResponseBlob)
  }
  // Union type
}

FunctionResponseBlob

Raw media bytes for function response.

Text should not be sent as raw bytes, use the 'FunctionResponse.response' field.

Поля
mimeType string

The IANA standard MIME type of the source data. Examples: - image/png - image/jpeg If an unsupported MIME type is provided, an error will be returned. For a complete list of supported types, see Supported file formats .

data string ( bytes format)

Raw bytes for media formats.

A base64-encoded string.

JSON-представление
{
  "mimeType": string,
  "data": string
}

Планирование

Specifies how the response should be scheduled in the conversation.

Перечисления
SCHEDULING_UNSPECIFIED This value is unused.
SILENT Only add the result to the conversation context, do not interrupt or trigger generation.
WHEN_IDLE Add the result to the conversation context, and prompt to generate output without interrupting ongoing generation.
INTERRUPT Add the result to the conversation context, interrupt ongoing generation and prompt to generate output.

FileData

URI based data.

Поля
mimeType string

Optional. The IANA standard MIME type of the source data.

fileUri string

Required. URI.

JSON-представление
{
  "mimeType": string,
  "fileUri": string
}

ExecutableCode

Code generated by the model that is meant to be executed, and the result returned to the model.

Only generated when using the CodeExecution tool, in which the code will be automatically executed, and a corresponding CodeExecutionResult will also be generated.

Поля
language enum ( Language )

Required. Programming language of the code .

code string

Required. The code to be executed.

JSON-представление
{
  "language": enum (Language),
  "code": string
}

Язык

Supported programming languages for the generated code.

Перечисления
LANGUAGE_UNSPECIFIED Unspecified language. This value should not be used.
PYTHON Python >= 3.10, with numpy and simpy available.

CodeExecutionResult

Result of executing the ExecutableCode .

Only generated when using the CodeExecution , and always follows a part containing the ExecutableCode .

Поля
outcome enum ( Outcome )

Required. Outcome of the code execution.

output string

Optional. Contains stdout when code execution is successful, stderr or other description otherwise.

JSON-представление
{
  "outcome": enum (Outcome),
  "output": string
}

Исход

Enumeration of possible outcomes of the code execution.

Перечисления
OUTCOME_UNSPECIFIED Unspecified status. This value should not be used.
OUTCOME_OK Code execution completed successfully.
OUTCOME_FAILED Code execution finished but with a failure. stderr should contain the reason.
OUTCOME_DEADLINE_EXCEEDED Code execution ran for too long, and was cancelled. There may or may not be a partial output present.

ВидеоМетаданные

Metadata describes the input video content.

Поля
startOffset string ( Duration format)

Optional. The start offset of the video.

A duration in seconds with up to nine fractional digits, ending with ' s '. Example: "3.5s" .

endOffset string ( Duration format)

Optional. The end offset of the video.

A duration in seconds with up to nine fractional digits, ending with ' s '. Example: "3.5s" .

fps number

Optional. The frame rate of the video sent to the model. If not specified, the default value will be 1.0. The fps range is (0.0, 24.0].

JSON-представление
{
  "startOffset": string,
  "endOffset": string,
  "fps": number
}

Инструмент

Tool details that the model may use to generate response.

A Tool is a piece of code that enables the system to interact with external systems to perform an action, or set of actions, outside of knowledge and scope of the model.

Next ID: 12

Поля
functionDeclarations[] object ( FunctionDeclaration )

Optional. A list of FunctionDeclarations available to the model that can be used for function calling.

The model or system does not execute the function. Instead the defined function may be returned as a FunctionCall with arguments to the client side for execution. The model may decide to call a subset of these functions by populating FunctionCall in the response. The next conversation turn may contain a FunctionResponse with the Content.role "function" generation context for the next model turn.

googleSearchRetrieval object ( GoogleSearchRetrieval )

Optional. Retrieval tool that is powered by Google search.

codeExecution object ( CodeExecution )

Optional. Enables the model to execute code as part of generation.

computerUse object ( ComputerUse )

Optional. Tool to support the model interacting directly with the computer. If enabled, it automatically populates computer-use specific Function Declarations.

urlContext object ( UrlContext )

Optional. Tool to support URL context retrieval.

JSON-представление
{
  "functionDeclarations": [
    {
      object (FunctionDeclaration)
    }
  ],
  "googleSearchRetrieval": {
    object (GoogleSearchRetrieval)
  },
  "codeExecution": {
    object (CodeExecution)
  },
  "googleSearch": {
    object (GoogleSearch)
  },
  "computerUse": {
    object (ComputerUse)
  },
  "urlContext": {
    object (UrlContext)
  },
  "fileSearch": {
    object (FileSearch)
  }
}

Объявление функции

Structured representation of a function declaration as defined by the OpenAPI 3.03 specification . Included in this declaration are the function name and parameters. This FunctionDeclaration is a representation of a block of code that can be used as a Tool by the model and executed by the client.

Поля
string name

Required. The name of the function. Must be az, AZ, 0-9, or contain underscores, colons, dots, and dashes, with a maximum length of 64.

string description

Required. A brief description of the function.

behavior enum ( Behavior )

Optional. Specifies the function Behavior. Currently only supported by the BidiGenerateContent method.

parameters object ( Schema )

Optional. Describes the parameters to this function. Reflects the Open API 3.03 Parameter Object string Key: the name of the parameter. Parameter names are case sensitive. Schema Value: the Schema defining the type used for the parameter.

parametersJsonSchema value ( Value format)

Optional. Describes the parameters to the function in JSON Schema format. The schema must describe an object where the properties are the parameters to the function. For example:

{
  "type": "object",
  "properties": {
    "name": { "type": "string" },
    "age": { "type": "integer" }
  },
  "additionalProperties": false,
  "required": ["name", "age"],
  "propertyOrdering": ["name", "age"]
}

This field is mutually exclusive with parameters .

response object ( Schema )

Optional. Describes the output from this function in JSON Schema format. Reflects the Open API 3.03 Response Object. The Schema defines the type used for the response value of the function.

responseJsonSchema value ( Value format)

Optional. Describes the output from this function in JSON Schema format. The value specified by the schema is the response value of the function.

This field is mutually exclusive with response .

JSON-представление
{
  "name": string,
  "description": string,
  "behavior": enum (Behavior),
  "parameters": {
    object (Schema)
  },
  "parametersJsonSchema": value,
  "response": {
    object (Schema)
  },
  "responseJsonSchema": value
}

Схема

The Schema object allows the definition of input and output data types. These types can be objects, but also primitives and arrays. Represents a select subset of an OpenAPI 3.0 schema object .

Поля
type enum ( Type )

Required. Data type.

format string

Optional. The format of the data. Any value is allowed, but most do not trigger any special functionality.

string title

Optional. The title of the schema.

string description

Optional. A brief description of the parameter. This could contain examples of use. Parameter description may be formatted as Markdown.

boolean nullable

Optional. Indicates if the value may be null.

enum[] string

Optional. Possible values of the element of Type.STRING with enum format. For example we can define an Enum Direction as : {type:STRING, format:enum, enum:["EAST", NORTH", "SOUTH", "WEST"]}

maxItems string ( int64 format)

Optional. Maximum number of the elements for Type.ARRAY.

minItems string ( int64 format)

Optional. Minimum number of the elements for Type.ARRAY.

properties map (key: string, value: object ( Schema ))

Optional. Properties of Type.OBJECT.

An object containing a list of "key": value pairs. Example: { "name": "wrench", "mass": "1.3kg", "count": "3" } .

required[] string

Optional. Required properties of Type.OBJECT.

minProperties string ( int64 format)

Optional. Minimum number of the properties for Type.OBJECT.

maxProperties string ( int64 format)

Optional. Maximum number of the properties for Type.OBJECT.

minLength string ( int64 format)

Optional. SCHEMA FIELDS FOR TYPE STRING Minimum length of the Type.STRING

maxLength string ( int64 format)

Optional. Maximum length of the Type.STRING

pattern string

Optional. Pattern of the Type.STRING to restrict a string to a regular expression.

example value ( Value format)

Optional. Example of the object. Will only populated when the object is the root.

anyOf[] object ( Schema )

Optional. The value should be validated against any (one or more) of the subschemas in the list.

propertyOrdering[] string

Optional. The order of the properties. Not a standard field in open api spec. Used to determine the order of the properties in the response.

default value ( Value format)

Optional. Default value of the field. Per JSON Schema, this field is intended for documentation generators and doesn't affect validation. Thus it's included here and ignored so that developers who send schemas with a default field don't get unknown-field errors.

items object ( Schema )

Optional. Schema of the elements of Type.ARRAY.

minimum number

Optional. SCHEMA FIELDS FOR TYPE INTEGER and NUMBER Minimum value of the Type.INTEGER and Type.NUMBER

maximum number

Optional. Maximum value of the Type.INTEGER and Type.NUMBER

JSON-представление
{
  "type": enum (Type),
  "format": string,
  "title": string,
  "description": string,
  "nullable": boolean,
  "enum": [
    string
  ],
  "maxItems": string,
  "minItems": string,
  "properties": {
    string: {
      object (Schema)
    },
    ...
  },
  "required": [
    string
  ],
  "minProperties": string,
  "maxProperties": string,
  "minLength": string,
  "maxLength": string,
  "pattern": string,
  "example": value,
  "anyOf": [
    {
      object (Schema)
    }
  ],
  "propertyOrdering": [
    string
  ],
  "default": value,
  "items": {
    object (Schema)
  },
  "minimum": number,
  "maximum": number
}

Тип

Type contains the list of OpenAPI data types as defined by https://spec.openapis.org/oas/v3.0.3#data-types

Перечисления
TYPE_UNSPECIFIED Not specified, should not be used.
STRING String type.
NUMBER Number type.
INTEGER Целочисленный тип.
BOOLEAN Булевский тип.
ARRAY Array type.
OBJECT Object type.
NULL Null type.

Поведение

Defines the function behavior. Defaults to BLOCKING .

Перечисления
UNSPECIFIED This value is unused.
BLOCKING If set, the system will wait to receive the function response before continuing the conversation.
NON_BLOCKING If set, the system will not wait to receive the function response. Instead, it will attempt to handle function responses as they become available while maintaining the conversation between the user and the model.

GoogleSearchRetrieval

Tool to retrieve public web data for grounding, powered by Google.

Поля
dynamicRetrievalConfig object ( DynamicRetrievalConfig )

Specifies the dynamic retrieval configuration for the given source.

JSON-представление
{
  "dynamicRetrievalConfig": {
    object (DynamicRetrievalConfig)
  }
}

DynamicRetrievalConfig

Describes the options to customize dynamic retrieval.

Поля
mode enum ( Mode )

The mode of the predictor to be used in dynamic retrieval.

dynamicThreshold number

The threshold to be used in dynamic retrieval. If not set, a system default value is used.

JSON-представление
{
  "mode": enum (Mode),
  "dynamicThreshold": number
}

Режим

The mode of the predictor to be used in dynamic retrieval.

Перечисления
MODE_UNSPECIFIED Always trigger retrieval.
MODE_DYNAMIC Run retrieval only when system decides it is necessary.

CodeExecution

Этот тип не имеет полей.

Tool that executes code generated by the model, and automatically returns the result to the model.

See also ExecutableCode and CodeExecutionResult which are only generated when using this tool.

GoogleSearch

GoogleSearch tool type. Tool to support Google Search in Model. Powered by Google.

Поля
timeRangeFilter object ( Interval )

Optional. Filter search results to a specific time range. If customers set a start time, they must set an end time (and vice versa).

JSON-представление
{
  "timeRangeFilter": {
    object (Interval)
  }
}

Интервал

Представляет временной интервал, закодированный как начальная метка времени (включительно) и конечная метка времени (исключая).

Начало должно быть меньше или равно концу. Если начало равно концу, интервал пустой (не соответствует ни одному времени). Если ни начало, ни конец не указаны, интервал соответствует любому времени.

Поля
startTime string ( Timestamp format)

Необязательно. Начало интервала включено.

Если указано, то соответствующая этому интервалу временная метка должна быть такой же или более поздней.

Uses RFC 3339, where generated output will always be Z-normalized and use 0, 3, 6 or 9 fractional digits. Offsets other than "Z" are also accepted. Examples: "2014-10-02T15:01:23Z" , "2014-10-02T15:01:23.045123456Z" or "2014-10-02T15:01:23+05:30" .

endTime string ( Timestamp format)

Необязательно. Исключительный конец интервала.

Если указано, то соответствующая этому интервалу временная метка должна быть до конца.

Uses RFC 3339, where generated output will always be Z-normalized and use 0, 3, 6 or 9 fractional digits. Offsets other than "Z" are also accepted. Examples: "2014-10-02T15:01:23Z" , "2014-10-02T15:01:23.045123456Z" or "2014-10-02T15:01:23+05:30" .

JSON-представление
{
  "startTime": string,
  "endTime": string
}

ComputerUse

Computer Use tool type.

Поля
environment enum ( Environment )

Required. The environment being operated.

excludedPredefinedFunctions[] string

Optional. By default, predefined functions are included in the final model call. Some of them can be explicitly excluded from being automatically included. This can serve two purposes: 1. Using a more restricted / different action space. 2. Improving the definitions / instructions of predefined functions.

JSON-представление
{
  "environment": enum (Environment),
  "excludedPredefinedFunctions": [
    string
  ]
}

Среда

Represents the environment being operated, such as a web browser.

Перечисления
ENVIRONMENT_UNSPECIFIED Defaults to browser.
ENVIRONMENT_BROWSER Operates in a web browser.

UrlContext

Этот тип не имеет полей.

Tool to support URL context retrieval.

FileSearch

The FileSearch tool that retrieves knowledge from Semantic Retrieval corpora. Files are imported to Semantic Retrieval corpora using the ImportFile API.

Поля
retrievalResources[] object ( RetrievalResource )

Required. Semantic retrieval resources to retrieve from. Currently only supports one corpus. In the future we may open up multiple corpora support.

retrievalConfig object ( RetrievalConfig )

Optional. The configuration for the retrieval.

JSON-представление
{
  "retrievalResources": [
    {
      object (RetrievalResource)
    }
  ],
  "retrievalConfig": {
    object (RetrievalConfig)
  }
}

RetrievalResource

The semantic retrieval resource to retrieve from.

Поля
ragStoreName string

Required. The name of the semantic retrieval resource to retrieve from. Example: ragStores/my-rag-store-123

JSON-представление
{
  "ragStoreName": string
}

RetrievalConfig

Semantic retrieval configuration.

Поля
metadataFilter string

Optional. Metadata filter to apply to the semantic retrieval documents and chunks.

topK integer

Optional. The number of semantic retrieval chunks to retrieve.

JSON-представление
{
  "metadataFilter": string,
  "topK": integer
}

ToolConfig

The Tool configuration containing parameters for specifying Tool use in the request.

Поля
functionCallingConfig object ( FunctionCallingConfig )

Optional. Function calling config.

JSON-представление
{
  "functionCallingConfig": {
    object (FunctionCallingConfig)
  }
}

FunctionCallingConfig

Configuration for specifying function calling behavior.

Поля
mode enum ( Mode )

Optional. Specifies the mode in which function calling should execute. If unspecified, the default value will be set to AUTO.

allowedFunctionNames[] string

Optional. A set of function names that, when provided, limits the functions the model will call.

This should only be set when the Mode is ANY or VALIDATED. Function names should match [FunctionDeclaration.name]. When set, model will predict a function call from only allowed function names.

JSON-представление
{
  "mode": enum (Mode),
  "allowedFunctionNames": [
    string
  ]
}

Режим

Defines the execution behavior for function calling by defining the execution mode.

Перечисления
MODE_UNSPECIFIED Unspecified function calling mode. This value should not be used.
AUTO Default model behavior, model decides to predict either a function call or a natural language response.
ANY Model is constrained to always predicting a function call only. If "allowedFunctionNames" are set, the predicted function call will be limited to any one of "allowedFunctionNames", else the predicted function call will be any one of the provided "functionDeclarations".
NONE Model will not predict any function call. Model behavior is same as when not passing any function declarations.
VALIDATED Model decides to predict either a function call or a natural language response, but will validate function calls with constrained decoding. If "allowedFunctionNames" are set, the predicted function call will be limited to any one of "allowedFunctionNames", else the predicted function call will be any one of the provided "functionDeclarations".

ИспользованиеМетаданных

Metadata on the usage of the cached content.

Поля
totalTokenCount integer

Total number of tokens that the cached content consumes.

JSON-представление
{
  "totalTokenCount": integer
}
,

Context caching allows you to save and reuse precomputed input tokens that you wish to use repeatedly, for example when asking different questions about the same media file. This can lead to cost and speed savings, depending on the usage. For a detailed introduction, see the Context caching guide.

Method: cachedContents.create

Creates CachedContent resource.

Конечная точка

post https: / /generativelanguage.googleapis.com /v1beta /cachedContents

Текст запроса

The request body contains an instance of CachedContent .

Поля
contents[] object ( Content )

Optional. Input only. Immutable. The content to cache.

tools[] object ( Tool )

Optional. Input only. Immutable. A list of Tools the model may use to generate the next response

expiration Union type
Specifies when this resource will expire. expiration can be only one of the following:
expireTime string ( Timestamp format)

Timestamp in UTC of when this resource is considered expired. This is always provided on output, regardless of what was sent on input.

Uses RFC 3339, where generated output will always be Z-normalized and use 0, 3, 6 or 9 fractional digits. Offsets other than "Z" are also accepted. Examples: "2014-10-02T15:01:23Z" , "2014-10-02T15:01:23.045123456Z" or "2014-10-02T15:01:23+05:30" .

ttl string ( Duration format)

Input only. New TTL for this resource, input only.

A duration in seconds with up to nine fractional digits, ending with ' s '. Example: "3.5s" .

displayName string

Optional. Immutable. The user-generated meaningful display name of the cached content. Maximum 128 Unicode characters.

model string

Required. Immutable. The name of the Model to use for cached content Format: models/{model}

systemInstruction object ( Content )

Optional. Input only. Immutable. Developer set system instruction. Currently text only.

toolConfig object ( ToolConfig )

Optional. Input only. Immutable. Tool config. This config is shared for all tools.

Пример запроса

Базовый

Питон

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)

Node.js

// 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)

Оболочка

wget https://storage.googleapis.com/generativeai-downloads/data/a11.txt
echo '{
  "model": "models/gemini-1.5-flash-001",
  "contents":[
    {
      "parts":[
        {
          "inline_data": {
            "mime_type":"text/plain",
            "data": "'$(base64 $B64FLAGS a11.txt)'"
          }
        }
      ],
    "role": "user"
    }
  ],
  "systemInstruction": {
    "parts": [
      {
        "text": "You are an expert at analyzing transcripts."
      }
    ]
  },
  "ttl": "300s"
}' > request.json

curl -X POST "https://generativelanguage.googleapis.com/v1beta/cachedContents?key=$GEMINI_API_KEY" \
 -H 'Content-Type: application/json' \
 -d @request.json \
 > cache.json

CACHE_NAME=$(cat cache.json | grep '"name":' | cut -d '"' -f 4 | head -n 1)

curl -X POST "https://generativelanguage.googleapis.com/v1beta/models/gemini-1.5-flash-001:generateContent?key=$GEMINI_API_KEY" \
-H 'Content-Type: application/json' \
-d '{
      "contents": [
        {
          "parts":[{
            "text": "Please summarize this transcript"
          }],
          "role": "user"
        },
      ],
      "cachedContent": "'$CACHE_NAME'"
    }'

От имени

Питон

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.",
    ),
)
cache_name = cache.name  # Save the name for later

# Later retrieve the cache
cache = client.caches.get(name=cache_name)
response = client.models.generate_content(
    model=model_name,
    contents="Find a lighthearted moment from this transcript",
    config=types.GenerateContentConfig(cached_content=cache.name),
)
print(response.text)

Node.js

// 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.",
  },
});
const cacheName = cache.name; // Save the name for later

// Later retrieve the cache
const retrievedCache = await ai.caches.get({ name: cacheName });
const response = await ai.models.generateContent({
  model: modelName,
  contents: "Find a lighthearted moment from this transcript",
  config: { cachedContent: retrievedCache.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)
}
cacheName := cache.Name

// Later retrieve the cache.
cache, err = client.Caches.Get(ctx, cacheName, &genai.GetCachedContentConfig{})
if err != nil {
	log.Fatal(err)
}

response, err := client.Models.GenerateContent(
	ctx,
	modelName,
	genai.Text("Find a lighthearted moment from this transcript"),
	&genai.GenerateContentConfig{
		CachedContent: cache.Name,
	},
)
if err != nil {
	log.Fatal(err)
}
fmt.Println("Response from cache (create from name):")
printResponse(response)

From chat

Питон

from google import genai
from google.genai import types

client = genai.Client()
model_name = "gemini-1.5-flash-001"
system_instruction = "You are an expert analyzing transcripts."

# Create a chat session with the given system instruction.
chat = client.chats.create(
    model=model_name,
    config=types.GenerateContentConfig(system_instruction=system_instruction),
)
document = client.files.upload(file=media / "a11.txt")

response = chat.send_message(
    message=["Hi, could you summarize this transcript?", document]
)
print("\n\nmodel:  ", response.text)
response = chat.send_message(
    message=["Okay, could you tell me more about the trans-lunar injection"]
)
print("\n\nmodel:  ", response.text)

# To cache the conversation so far, pass the chat history as the list of contents.
cache = client.caches.create(
    model=model_name,
    config={
        "contents": chat.get_history(),
        "system_instruction": system_instruction,
    },
)
# Continue the conversation using the cached content.
chat = client.chats.create(
    model=model_name,
    config=types.GenerateContentConfig(cached_content=cache.name),
)
response = chat.send_message(
    message="I didn't understand that last part, could you explain it in simpler language?"
)
print("\n\nmodel:  ", response.text)

Node.js

// Make sure to include the following import:
// import {GoogleGenAI} from '@google/genai';
const ai = new GoogleGenAI({ apiKey: process.env.GEMINI_API_KEY });
const modelName = "gemini-1.5-flash-001";
const systemInstruction = "You are an expert analyzing transcripts.";

// Create a chat session with the system instruction.
const chat = ai.chats.create({
  model: modelName,
  config: { systemInstruction: systemInstruction },
});
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);

let response = await chat.sendMessage({
  message: createUserContent([
    "Hi, could you summarize this transcript?",
    createPartFromUri(document.uri, document.mimeType),
  ]),
});
console.log("\n\nmodel:", response.text);

response = await chat.sendMessage({
  message: "Okay, could you tell me more about the trans-lunar injection",
});
console.log("\n\nmodel:", response.text);

// To cache the conversation so far, pass the chat history as the list of contents.
const chatHistory = chat.getHistory();
const cache = await ai.caches.create({
  model: modelName,
  config: {
    contents: chatHistory,
    systemInstruction: systemInstruction,
  },
});

// Continue the conversation using the cached content.
const chatWithCache = ai.chats.create({
  model: modelName,
  config: { cachedContent: cache.name },
});
response = await chatWithCache.sendMessage({
  message:
    "I didn't understand that last part, could you explain it in simpler language?",
});
console.log("\n\nmodel:", 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"
systemInstruction := "You are an expert analyzing transcripts."

// Create initial chat with a system instruction.
chat, err := client.Chats.Create(ctx, modelName, &genai.GenerateContentConfig{
	SystemInstruction: genai.NewContentFromText(systemInstruction, genai.RoleUser),
}, nil)
if err != nil {
	log.Fatal(err)
}

document, err := client.Files.UploadFromPath(
	ctx, 
	filepath.Join(getMedia(), "a11.txt"), 
	&genai.UploadFileConfig{
		MIMEType : "text/plain",
	},
)
if err != nil {
	log.Fatal(err)
}

// Send first message with the transcript.
parts := make([]genai.Part, 2)
parts[0] = genai.Part{Text: "Hi, could you summarize this transcript?"}
parts[1] = genai.Part{
	FileData: &genai.FileData{
		FileURI :      document.URI,
		MIMEType: document.MIMEType,
	},
}

// Send chat message.
resp, err := chat.SendMessage(ctx, parts...)
if err != nil {
	log.Fatal(err)
}
fmt.Println("\n\nmodel: ", resp.Text())

resp, err = chat.SendMessage(
	ctx, 
	genai.Part{
		Text: "Okay, could you tell me more about the trans-lunar injection",
	},
)
if err != nil {
	log.Fatal(err)
}
fmt.Println("\n\nmodel: ", resp.Text())

// To cache the conversation so far, pass the chat history as the list of contents.
cache, err := client.Caches.Create(ctx, modelName, &genai.CreateCachedContentConfig{
	Contents:          chat.History(false),
	SystemInstruction: genai.NewContentFromText(systemInstruction, genai.RoleUser),
})
if err != nil {
	log.Fatal(err)
}

// Continue the conversation using the cached history.
chat, err = client.Chats.Create(ctx, modelName, &genai.GenerateContentConfig{
	CachedContent: cache.Name,
}, nil)
if err != nil {
	log.Fatal(err)
}

resp, err = chat.SendMessage(
	ctx, 
	genai.Part{
		Text: "I didn't understand that last part, could you explain it in simpler language?",
	},
)
if err != nil {
	log.Fatal(err)
}
fmt.Println("\n\nmodel: ", resp.Text())

Тело ответа

If successful, the response body contains a newly created instance of CachedContent .

Method: cachedContents.list

Lists CachedContents.

Конечная точка

get https: / /generativelanguage.googleapis.com /v1beta /cachedContents

Параметры запроса

pageSize integer

Optional. The maximum number of cached contents to return. The service may return fewer than this value. If unspecified, some default (under maximum) number of items will be returned. The maximum value is 1000; values above 1000 will be coerced to 1000.

pageToken string

Optional. A page token, received from a previous cachedContents.list call. Provide this to retrieve the subsequent page.

When paginating, all other parameters provided to cachedContents.list must match the call that provided the page token.

Текст запроса

Тело запроса должно быть пустым.

Тело ответа

Response with CachedContents list.

В случае успеха тело ответа содержит данные со следующей структурой:

Поля
cachedContents[] object ( CachedContent )

List of cached contents.

nextPageToken string

A token, which can be sent as pageToken to retrieve the next page. If this field is omitted, there are no subsequent pages.

JSON-представление
{
  "cachedContents": [
    {
      object (CachedContent)
    }
  ],
  "nextPageToken": string
}

Method: cachedContents.get

Reads CachedContent resource.

Конечная точка

get https: / /generativelanguage.googleapis.com /v1beta /{name=cachedContents /*}

Параметры пути

string name

Required. The resource name referring to the content cache entry. Format: cachedContents/{id} It takes the form cachedContents/{cachedcontent} .

Текст запроса

Тело запроса должно быть пустым.

Пример запроса

Питон

from google import genai

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={
        "contents": [document],
        "system_instruction": "You are an expert analyzing transcripts.",
    },
)
print(client.caches.get(name=cache.name))

Node.js

// 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.",
  },
});
const retrievedCache = await ai.caches.get({ name: cache.name });
console.log("Retrieved Cache:", retrievedCache);

Идти

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)
}

cache, err = client.Caches.Get(ctx, cache.Name, &genai.GetCachedContentConfig{})
if err != nil {
	log.Fatal(err)
}
fmt.Println("Retrieved cache:")
fmt.Println(cache)

Оболочка

curl "https://generativelanguage.googleapis.com/v1beta/$CACHE_NAME?key=$GEMINI_API_KEY"

Тело ответа

If successful, the response body contains an instance of CachedContent .

Method: cachedContents.patch

Updates CachedContent resource (only expiration is updatable).

Конечная точка

patch https: / /generativelanguage.googleapis.com /v1beta /{cachedContent.name=cachedContents /*}
PATCH https://generativelanguage.googleapis.com/v1beta/{cachedContent.name=cachedContents/*}

Параметры пути

cachedContent.name string

Output only. Identifier. The resource name referring to the cached content. Format: cachedContents/{id} It takes the form cachedContents/{cachedcontent} .

Параметры запроса

updateMask string ( FieldMask format)

The list of fields to update.

This is a comma-separated list of fully qualified names of fields. Example: "user.displayName,photo" .

Текст запроса

The request body contains an instance of CachedContent .

Поля
expiration Union type
Specifies when this resource will expire. expiration can be only one of the following:
expireTime string ( Timestamp format)

Timestamp in UTC of when this resource is considered expired. This is always provided on output, regardless of what was sent on input.

Uses RFC 3339, where generated output will always be Z-normalized and use 0, 3, 6 or 9 fractional digits. Offsets other than "Z" are also accepted. Examples: "2014-10-02T15:01:23Z" , "2014-10-02T15:01:23.045123456Z" or "2014-10-02T15:01:23+05:30" .

ttl string ( Duration format)

Input only. New TTL for this resource, input only.

A duration in seconds with up to nine fractional digits, ending with ' s '. Example: "3.5s" .

Пример запроса

Питон

from google import genai
from google.genai import types
import datetime

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={
        "contents": [document],
        "system_instruction": "You are an expert analyzing transcripts.",
    },
)

# Update the cache's time-to-live (ttl)
ttl = f"{int(datetime.timedelta(hours=2).total_seconds())}s"
client.caches.update(
    name=cache.name, config=types.UpdateCachedContentConfig(ttl=ttl)
)
print(f"After update:\n {cache}")

# Alternatively, update the expire_time directly
# Update the expire_time directly in valid RFC 3339 format (UTC with a "Z" suffix)
expire_time = (
    (
        datetime.datetime.now(datetime.timezone.utc)
        + datetime.timedelta(minutes=15)
    )
    .isoformat()
    .replace("+00:00", "Z")
)
client.caches.update(
    name=cache.name,
    config=types.UpdateCachedContentConfig(expire_time=expire_time),
)

Node.js

// 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)),
];

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

// Update the cache's time-to-live (ttl)
const ttl = `${2 * 3600}s`; // 2 hours in seconds
cache = await ai.caches.update({
  name: cache.name,
  config: { ttl },
});
console.log("After update (TTL):", cache);

// Alternatively, update the expire_time directly (in RFC 3339 format with a "Z" suffix)
const expireTime = new Date(Date.now() + 15 * 60000)
  .toISOString()
  .replace(/\.\d{3}Z$/, "Z");
cache = await ai.caches.update({
  name: cache.name,
  config: { expireTime: expireTime },
});
console.log("After update (expire_time):", cache);

Идти

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)
}

_, err = client.Caches.Delete(ctx, cache.Name, &genai.DeleteCachedContentConfig{})
if err != nil {
	log.Fatal(err)
}
fmt.Println("Cache deleted:", cache.Name)

Оболочка

curl -X PATCH "https://generativelanguage.googleapis.com/v1beta/$CACHE_NAME?key=$GEMINI_API_KEY" \
 -H 'Content-Type: application/json' \
 -d '{"ttl": "600s"}'

Тело ответа

If successful, the response body contains an instance of CachedContent .

Method: cachedContents.delete

Deletes CachedContent resource.

Конечная точка

delete https: / /generativelanguage.googleapis.com /v1beta /{name=cachedContents /*}

Параметры пути

string name

Required. The resource name referring to the content cache entry Format: cachedContents/{id} It takes the form cachedContents/{cachedcontent} .

Текст запроса

Тело запроса должно быть пустым.

Пример запроса

Питон

from google import genai

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={
        "contents": [document],
        "system_instruction": "You are an expert analyzing transcripts.",
    },
)
client.caches.delete(name=cache.name)

Node.js

// 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.",
  },
});
await ai.caches.delete({ name: cache.name });
console.log("Cache deleted:", cache.name);

Идти

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)
}

_, err = client.Caches.Delete(ctx, cache.Name, &genai.DeleteCachedContentConfig{})
if err != nil {
	log.Fatal(err)
}
fmt.Println("Cache deleted:", cache.Name)

Оболочка

curl -X DELETE "https://generativelanguage.googleapis.com/v1beta/$CACHE_NAME?key=$GEMINI_API_KEY"

Тело ответа

В случае успеха тело ответа представляет собой пустой объект JSON.

REST Resource: cachedContents

Resource: CachedContent

Content that has been preprocessed and can be used in subsequent request to GenerativeService.

Cached content can be only used with model it was created for.

Поля
contents[] object ( Content )

Optional. Input only. Immutable. The content to cache.

tools[] object ( Tool )

Optional. Input only. Immutable. A list of Tools the model may use to generate the next response

createTime string ( Timestamp format)

Output only. Creation time of the cache entry.

Uses RFC 3339, where generated output will always be Z-normalized and use 0, 3, 6 or 9 fractional digits. Offsets other than "Z" are also accepted. Examples: "2014-10-02T15:01:23Z" , "2014-10-02T15:01:23.045123456Z" or "2014-10-02T15:01:23+05:30" .

updateTime string ( Timestamp format)

Output only. When the cache entry was last updated in UTC time.

Uses RFC 3339, where generated output will always be Z-normalized and use 0, 3, 6 or 9 fractional digits. Offsets other than "Z" are also accepted. Examples: "2014-10-02T15:01:23Z" , "2014-10-02T15:01:23.045123456Z" or "2014-10-02T15:01:23+05:30" .

usageMetadata object ( UsageMetadata )

Output only. Metadata on the usage of the cached content.

expiration Union type
Specifies when this resource will expire. expiration can be only one of the following:
expireTime string ( Timestamp format)

Timestamp in UTC of when this resource is considered expired. This is always provided on output, regardless of what was sent on input.

Uses RFC 3339, where generated output will always be Z-normalized and use 0, 3, 6 or 9 fractional digits. Offsets other than "Z" are also accepted. Examples: "2014-10-02T15:01:23Z" , "2014-10-02T15:01:23.045123456Z" or "2014-10-02T15:01:23+05:30" .

ttl string ( Duration format)

Input only. New TTL for this resource, input only.

A duration in seconds with up to nine fractional digits, ending with ' s '. Example: "3.5s" .

string name

Output only. Identifier. The resource name referring to the cached content. Format: cachedContents/{id}

displayName string

Optional. Immutable. The user-generated meaningful display name of the cached content. Maximum 128 Unicode characters.

model string

Required. Immutable. The name of the Model to use for cached content Format: models/{model}

systemInstruction object ( Content )

Optional. Input only. Immutable. Developer set system instruction. Currently text only.

toolConfig object ( ToolConfig )

Optional. Input only. Immutable. Tool config. This config is shared for all tools.

JSON-представление
{
  "contents": [
    {
      object (Content)
    }
  ],
  "tools": [
    {
      object (Tool)
    }
  ],
  "createTime": string,
  "updateTime": string,
  "usageMetadata": {
    object (UsageMetadata)
  },

  // expiration
  "expireTime": string,
  "ttl": string
  // Union type
  "name": string,
  "displayName": string,
  "model": string,
  "systemInstruction": {
    object (Content)
  },
  "toolConfig": {
    object (ToolConfig)
  }
}

Содержание

The base structured datatype containing multi-part content of a message.

A Content includes a role field designating the producer of the Content and a parts field containing multi-part data that contains the content of the message turn.

Поля
parts[] object ( Part )

Ordered Parts that constitute a single message. Parts may have different MIME types.

role string

Optional. The producer of the content. Must be either 'user' or 'model'.

Useful to set for multi-turn conversations, otherwise can be left blank or unset.

JSON-представление
{
  "parts": [
    {
      object (Part)
    }
  ],
  "role": string
}

Часть

A datatype containing media that is part of a multi-part Content message.

A Part consists of data which has an associated datatype. A Part can only contain one of the accepted types in Part.data .

A Part must have a fixed IANA MIME type identifying the type and subtype of the media if the inlineData field is filled with raw bytes.

Поля
thought boolean

Optional. Indicates if the part is thought from the model.

thoughtSignature string ( bytes format)

Optional. An opaque signature for the thought so it can be reused in subsequent requests.

A base64-encoded string.

partMetadata object ( Struct format)

Custom metadata associated with the Part. Agents using genai.Part as content representation may need to keep track of the additional information. For example it can be name of a file/source from which the Part originates or a way to multiplex multiple Part streams.

data Union type
data can be only one of the following:
text string

Inline text.

inlineData object ( Blob )

Inline media bytes.

functionCall object ( FunctionCall )

A predicted FunctionCall returned from the model that contains a string representing the FunctionDeclaration.name with the arguments and their values.

functionResponse object ( FunctionResponse )

The result output of a FunctionCall that contains a string representing the FunctionDeclaration.name and a structured JSON object containing any output from the function is used as context to the model.

fileData object ( FileData )

URI based data.

executableCode object ( ExecutableCode )

Code generated by the model that is meant to be executed.

codeExecutionResult object ( CodeExecutionResult )

Result of executing the ExecutableCode .

metadata Union type
Controls extra preprocessing of data. metadata can be only one of the following:
videoMetadata object ( VideoMetadata )

Optional. Video metadata. The metadata should only be specified while the video data is presented in inlineData or fileData.

JSON-представление
{
  "thought": boolean,
  "thoughtSignature": string,
  "partMetadata": {
    object
  },

  // data
  "text": string,
  "inlineData": {
    object (Blob)
  },
  "functionCall": {
    object (FunctionCall)
  },
  "functionResponse": {
    object (FunctionResponse)
  },
  "fileData": {
    object (FileData)
  },
  "executableCode": {
    object (ExecutableCode)
  },
  "codeExecutionResult": {
    object (CodeExecutionResult)
  }
  // Union type

  // metadata
  "videoMetadata": {
    object (VideoMetadata)
  }
  // Union type
}

Клякса

Raw media bytes.

Text should not be sent as raw bytes, use the 'text' field.

Поля
mimeType string

The IANA standard MIME type of the source data. Examples: - image/png - image/jpeg If an unsupported MIME type is provided, an error will be returned. For a complete list of supported types, see Supported file formats .

data string ( bytes format)

Raw bytes for media formats.

A base64-encoded string.

JSON-представление
{
  "mimeType": string,
  "data": string
}

FunctionCall

A predicted FunctionCall returned from the model that contains a string representing the FunctionDeclaration.name with the arguments and their values.

Поля
string id

Optional. The unique id of the function call. If populated, the client to execute the functionCall and return the response with the matching id .

string name

Required. The name of the function to call. Must be az, AZ, 0-9, or contain underscores and dashes, with a maximum length of 64.

args object ( Struct format)

Optional. The function parameters and values in JSON object format.

JSON-представление
{
  "id": string,
  "name": string,
  "args": {
    object
  }
}

FunctionResponse

The result output from a FunctionCall that contains a string representing the FunctionDeclaration.name and a structured JSON object containing any output from the function is used as context to the model. This should contain the result of a FunctionCall made based on model prediction.

Поля
string id

Optional. The id of the function call this response is for. Populated by the client to match the corresponding function call id .

string name

Required. The name of the function to call. Must be az, AZ, 0-9, or contain underscores and dashes, with a maximum length of 64.

response object ( Struct format)

Required. The function response in JSON object format. Callers can use any keys of their choice that fit the function's syntax to return the function output, eg "output", "result", etc. In particular, if the function call failed to execute, the response can have an "error" key to return error details to the model.

parts[] object ( FunctionResponsePart )

Optional. Ordered Parts that constitute a function response. Parts may have different IANA MIME types.

willContinue boolean

Optional. Signals that function call continues, and more responses will be returned, turning the function call into a generator. Is only applicable to NON_BLOCKING function calls, is ignored otherwise. If set to false, future responses will not be considered. It is allowed to return empty response with willContinue=False to signal that the function call is finished. This may still trigger the model generation. To avoid triggering the generation and finish the function call, additionally set scheduling to SILENT .

scheduling enum ( Scheduling )

Optional. Specifies how the response should be scheduled in the conversation. Only applicable to NON_BLOCKING function calls, is ignored otherwise. Defaults to WHEN_IDLE.

JSON-представление
{
  "id": string,
  "name": string,
  "response": {
    object
  },
  "parts": [
    {
      object (FunctionResponsePart)
    }
  ],
  "willContinue": boolean,
  "scheduling": enum (Scheduling)
}

ФункцияОтветЧасть

A datatype containing media that is part of a FunctionResponse message.

A FunctionResponsePart consists of data which has an associated datatype. A FunctionResponsePart can only contain one of the accepted types in FunctionResponsePart.data .

A FunctionResponsePart must have a fixed IANA MIME type identifying the type and subtype of the media if the inlineData field is filled with raw bytes.

Поля
data Union type
The data of the function response part. data can be only one of the following:
inlineData object ( FunctionResponseBlob )

Inline media bytes.

JSON-представление
{

  // data
  "inlineData": {
    object (FunctionResponseBlob)
  }
  // Union type
}

FunctionResponseBlob

Raw media bytes for function response.

Text should not be sent as raw bytes, use the 'FunctionResponse.response' field.

Поля
mimeType string

The IANA standard MIME type of the source data. Examples: - image/png - image/jpeg If an unsupported MIME type is provided, an error will be returned. For a complete list of supported types, see Supported file formats .

data string ( bytes format)

Raw bytes for media formats.

A base64-encoded string.

JSON-представление
{
  "mimeType": string,
  "data": string
}

Планирование

Specifies how the response should be scheduled in the conversation.

Перечисления
SCHEDULING_UNSPECIFIED This value is unused.
SILENT Only add the result to the conversation context, do not interrupt or trigger generation.
WHEN_IDLE Add the result to the conversation context, and prompt to generate output without interrupting ongoing generation.
INTERRUPT Add the result to the conversation context, interrupt ongoing generation and prompt to generate output.

FileData

URI based data.

Поля
mimeType string

Optional. The IANA standard MIME type of the source data.

fileUri string

Required. URI.

JSON-представление
{
  "mimeType": string,
  "fileUri": string
}

ExecutableCode

Code generated by the model that is meant to be executed, and the result returned to the model.

Only generated when using the CodeExecution tool, in which the code will be automatically executed, and a corresponding CodeExecutionResult will also be generated.

Поля
language enum ( Language )

Required. Programming language of the code .

code string

Required. The code to be executed.

JSON-представление
{
  "language": enum (Language),
  "code": string
}

Язык

Supported programming languages for the generated code.

Перечисления
LANGUAGE_UNSPECIFIED Unspecified language. This value should not be used.
PYTHON Python >= 3.10, with numpy and simpy available.

CodeExecutionResult

Result of executing the ExecutableCode .

Only generated when using the CodeExecution , and always follows a part containing the ExecutableCode .

Поля
outcome enum ( Outcome )

Required. Outcome of the code execution.

output string

Optional. Contains stdout when code execution is successful, stderr or other description otherwise.

JSON-представление
{
  "outcome": enum (Outcome),
  "output": string
}

Исход

Enumeration of possible outcomes of the code execution.

Перечисления
OUTCOME_UNSPECIFIED Unspecified status. This value should not be used.
OUTCOME_OK Code execution completed successfully.
OUTCOME_FAILED Code execution finished but with a failure. stderr should contain the reason.
OUTCOME_DEADLINE_EXCEEDED Code execution ran for too long, and was cancelled. There may or may not be a partial output present.

ВидеоМетаданные

Metadata describes the input video content.

Поля
startOffset string ( Duration format)

Optional. The start offset of the video.

A duration in seconds with up to nine fractional digits, ending with ' s '. Example: "3.5s" .

endOffset string ( Duration format)

Optional. The end offset of the video.

A duration in seconds with up to nine fractional digits, ending with ' s '. Example: "3.5s" .

fps number

Optional. The frame rate of the video sent to the model. If not specified, the default value will be 1.0. The fps range is (0.0, 24.0].

JSON-представление
{
  "startOffset": string,
  "endOffset": string,
  "fps": number
}

Инструмент

Tool details that the model may use to generate response.

A Tool is a piece of code that enables the system to interact with external systems to perform an action, or set of actions, outside of knowledge and scope of the model.

Next ID: 12

Поля
functionDeclarations[] object ( FunctionDeclaration )

Optional. A list of FunctionDeclarations available to the model that can be used for function calling.

The model or system does not execute the function. Instead the defined function may be returned as a FunctionCall with arguments to the client side for execution. The model may decide to call a subset of these functions by populating FunctionCall in the response. The next conversation turn may contain a FunctionResponse with the Content.role "function" generation context for the next model turn.

googleSearchRetrieval object ( GoogleSearchRetrieval )

Optional. Retrieval tool that is powered by Google search.

codeExecution object ( CodeExecution )

Optional. Enables the model to execute code as part of generation.

computerUse object ( ComputerUse )

Optional. Tool to support the model interacting directly with the computer. If enabled, it automatically populates computer-use specific Function Declarations.

urlContext object ( UrlContext )

Optional. Tool to support URL context retrieval.

JSON-представление
{
  "functionDeclarations": [
    {
      object (FunctionDeclaration)
    }
  ],
  "googleSearchRetrieval": {
    object (GoogleSearchRetrieval)
  },
  "codeExecution": {
    object (CodeExecution)
  },
  "googleSearch": {
    object (GoogleSearch)
  },
  "computerUse": {
    object (ComputerUse)
  },
  "urlContext": {
    object (UrlContext)
  },
  "fileSearch": {
    object (FileSearch)
  }
}

Объявление функции

Structured representation of a function declaration as defined by the OpenAPI 3.03 specification . Included in this declaration are the function name and parameters. This FunctionDeclaration is a representation of a block of code that can be used as a Tool by the model and executed by the client.

Поля
string name

Required. The name of the function. Must be az, AZ, 0-9, or contain underscores, colons, dots, and dashes, with a maximum length of 64.

string description

Required. A brief description of the function.

behavior enum ( Behavior )

Optional. Specifies the function Behavior. Currently only supported by the BidiGenerateContent method.

parameters object ( Schema )

Optional. Describes the parameters to this function. Reflects the Open API 3.03 Parameter Object string Key: the name of the parameter. Parameter names are case sensitive. Schema Value: the Schema defining the type used for the parameter.

parametersJsonSchema value ( Value format)

Optional. Describes the parameters to the function in JSON Schema format. The schema must describe an object where the properties are the parameters to the function. For example:

{
  "type": "object",
  "properties": {
    "name": { "type": "string" },
    "age": { "type": "integer" }
  },
  "additionalProperties": false,
  "required": ["name", "age"],
  "propertyOrdering": ["name", "age"]
}

This field is mutually exclusive with parameters .

response object ( Schema )

Optional. Describes the output from this function in JSON Schema format. Reflects the Open API 3.03 Response Object. The Schema defines the type used for the response value of the function.

responseJsonSchema value ( Value format)

Optional. Describes the output from this function in JSON Schema format. The value specified by the schema is the response value of the function.

This field is mutually exclusive with response .

JSON-представление
{
  "name": string,
  "description": string,
  "behavior": enum (Behavior),
  "parameters": {
    object (Schema)
  },
  "parametersJsonSchema": value,
  "response": {
    object (Schema)
  },
  "responseJsonSchema": value
}

Схема

The Schema object allows the definition of input and output data types. These types can be objects, but also primitives and arrays. Represents a select subset of an OpenAPI 3.0 schema object .

Поля
type enum ( Type )

Required. Data type.

format string

Optional. The format of the data. Any value is allowed, but most do not trigger any special functionality.

string title

Optional. The title of the schema.

string description

Optional. A brief description of the parameter. This could contain examples of use. Parameter description may be formatted as Markdown.

boolean nullable

Optional. Indicates if the value may be null.

enum[] string

Optional. Possible values of the element of Type.STRING with enum format. For example we can define an Enum Direction as : {type:STRING, format:enum, enum:["EAST", NORTH", "SOUTH", "WEST"]}

maxItems string ( int64 format)

Optional. Maximum number of the elements for Type.ARRAY.

minItems string ( int64 format)

Optional. Minimum number of the elements for Type.ARRAY.

properties map (key: string, value: object ( Schema ))

Optional. Properties of Type.OBJECT.

An object containing a list of "key": value pairs. Example: { "name": "wrench", "mass": "1.3kg", "count": "3" } .

required[] string

Optional. Required properties of Type.OBJECT.

minProperties string ( int64 format)

Optional. Minimum number of the properties for Type.OBJECT.

maxProperties string ( int64 format)

Optional. Maximum number of the properties for Type.OBJECT.

minLength string ( int64 format)

Optional. SCHEMA FIELDS FOR TYPE STRING Minimum length of the Type.STRING

maxLength string ( int64 format)

Optional. Maximum length of the Type.STRING

pattern string

Optional. Pattern of the Type.STRING to restrict a string to a regular expression.

example value ( Value format)

Optional. Example of the object. Will only populated when the object is the root.

anyOf[] object ( Schema )

Optional. The value should be validated against any (one or more) of the subschemas in the list.

propertyOrdering[] string

Optional. The order of the properties. Not a standard field in open api spec. Used to determine the order of the properties in the response.

default value ( Value format)

Optional. Default value of the field. Per JSON Schema, this field is intended for documentation generators and doesn't affect validation. Thus it's included here and ignored so that developers who send schemas with a default field don't get unknown-field errors.

items object ( Schema )

Optional. Schema of the elements of Type.ARRAY.

minimum number

Optional. SCHEMA FIELDS FOR TYPE INTEGER and NUMBER Minimum value of the Type.INTEGER and Type.NUMBER

maximum number

Optional. Maximum value of the Type.INTEGER and Type.NUMBER

JSON-представление
{
  "type": enum (Type),
  "format": string,
  "title": string,
  "description": string,
  "nullable": boolean,
  "enum": [
    string
  ],
  "maxItems": string,
  "minItems": string,
  "properties": {
    string: {
      object (Schema)
    },
    ...
  },
  "required": [
    string
  ],
  "minProperties": string,
  "maxProperties": string,
  "minLength": string,
  "maxLength": string,
  "pattern": string,
  "example": value,
  "anyOf": [
    {
      object (Schema)
    }
  ],
  "propertyOrdering": [
    string
  ],
  "default": value,
  "items": {
    object (Schema)
  },
  "minimum": number,
  "maximum": number
}

Тип

Type contains the list of OpenAPI data types as defined by https://spec.openapis.org/oas/v3.0.3#data-types

Перечисления
TYPE_UNSPECIFIED Not specified, should not be used.
STRING String type.
NUMBER Number type.
INTEGER Целочисленный тип.
BOOLEAN Булевский тип.
ARRAY Array type.
OBJECT Object type.
NULL Null type.

Поведение

Defines the function behavior. Defaults to BLOCKING .

Перечисления
UNSPECIFIED This value is unused.
BLOCKING If set, the system will wait to receive the function response before continuing the conversation.
NON_BLOCKING If set, the system will not wait to receive the function response. Instead, it will attempt to handle function responses as they become available while maintaining the conversation between the user and the model.

GoogleSearchRetrieval

Tool to retrieve public web data for grounding, powered by Google.

Поля
dynamicRetrievalConfig object ( DynamicRetrievalConfig )

Specifies the dynamic retrieval configuration for the given source.

JSON representation
{
  "dynamicRetrievalConfig": {
    object (DynamicRetrievalConfig)
  }
}

DynamicRetrievalConfig

Describes the options to customize dynamic retrieval.

Поля
mode enum ( Mode )

The mode of the predictor to be used in dynamic retrieval.

dynamicThreshold number

The threshold to be used in dynamic retrieval. If not set, a system default value is used.

JSON representation
{
  "mode": enum (Mode),
  "dynamicThreshold": number
}

Режим

The mode of the predictor to be used in dynamic retrieval.

Перечисления
MODE_UNSPECIFIED Always trigger retrieval.
MODE_DYNAMIC Run retrieval only when system decides it is necessary.

CodeExecution

Этот тип не имеет полей.

Tool that executes code generated by the model, and automatically returns the result to the model.

See also ExecutableCode and CodeExecutionResult which are only generated when using this tool.

GoogleSearch

GoogleSearch tool type. Tool to support Google Search in Model. Powered by Google.

Поля
timeRangeFilter object ( Interval )

Optional. Filter search results to a specific time range. If customers set a start time, they must set an end time (and vice versa).

JSON representation
{
  "timeRangeFilter": {
    object (Interval)
  }
}

Интервал

Представляет временной интервал, закодированный как начальная метка времени (включительно) и конечная метка времени (исключая).

Начало должно быть меньше или равно концу. Если начало равно концу, интервал пустой (не соответствует ни одному времени). Если ни начало, ни конец не указаны, интервал соответствует любому времени.

Поля
startTime string ( Timestamp format)

Необязательно. Начало интервала включено.

Если указано, то соответствующая этому интервалу временная метка должна быть такой же или более поздней.

Uses RFC 3339, where generated output will always be Z-normalized and use 0, 3, 6 or 9 fractional digits. Offsets other than "Z" are also accepted. Examples: "2014-10-02T15:01:23Z" , "2014-10-02T15:01:23.045123456Z" or "2014-10-02T15:01:23+05:30" .

endTime string ( Timestamp format)

Optional. Exclusive end of the interval.

Если указано, то соответствующая этому интервалу временная метка должна быть до конца.

Uses RFC 3339, where generated output will always be Z-normalized and use 0, 3, 6 or 9 fractional digits. Offsets other than "Z" are also accepted. Examples: "2014-10-02T15:01:23Z" , "2014-10-02T15:01:23.045123456Z" or "2014-10-02T15:01:23+05:30" .

JSON-представление
{
  "startTime": string,
  "endTime": string
}

ComputerUse

Computer Use tool type.

Поля
environment enum ( Environment )

Required. The environment being operated.

excludedPredefinedFunctions[] string

Optional. By default, predefined functions are included in the final model call. Some of them can be explicitly excluded from being automatically included. This can serve two purposes: 1. Using a more restricted / different action space. 2. Improving the definitions / instructions of predefined functions.

JSON representation
{
  "environment": enum (Environment),
  "excludedPredefinedFunctions": [
    string
  ]
}

Среда

Represents the environment being operated, such as a web browser.

Перечисления
ENVIRONMENT_UNSPECIFIED Defaults to browser.
ENVIRONMENT_BROWSER Operates in a web browser.

UrlContext

Этот тип не имеет полей.

Tool to support URL context retrieval.

FileSearch

The FileSearch tool that retrieves knowledge from Semantic Retrieval corpora. Files are imported to Semantic Retrieval corpora using the ImportFile API.

Поля
retrievalResources[] object ( RetrievalResource )

Required. Semantic retrieval resources to retrieve from. Currently only supports one corpus. In the future we may open up multiple corpora support.

retrievalConfig object ( RetrievalConfig )

Optional. The configuration for the retrieval.

JSON representation
{
  "retrievalResources": [
    {
      object (RetrievalResource)
    }
  ],
  "retrievalConfig": {
    object (RetrievalConfig)
  }
}

RetrievalResource

The semantic retrieval resource to retrieve from.

Поля
ragStoreName string

Required. The name of the semantic retrieval resource to retrieve from. Example: ragStores/my-rag-store-123

JSON representation
{
  "ragStoreName": string
}

RetrievalConfig

Semantic retrieval configuration.

Поля
metadataFilter string

Optional. Metadata filter to apply to the semantic retrieval documents and chunks.

topK integer

Optional. The number of semantic retrieval chunks to retrieve.

JSON representation
{
  "metadataFilter": string,
  "topK": integer
}

ToolConfig

The Tool configuration containing parameters for specifying Tool use in the request.

Поля
functionCallingConfig object ( FunctionCallingConfig )

Optional. Function calling config.

JSON representation
{
  "functionCallingConfig": {
    object (FunctionCallingConfig)
  }
}

FunctionCallingConfig

Configuration for specifying function calling behavior.

Поля
mode enum ( Mode )

Optional. Specifies the mode in which function calling should execute. If unspecified, the default value will be set to AUTO.

allowedFunctionNames[] string

Optional. A set of function names that, when provided, limits the functions the model will call.

This should only be set when the Mode is ANY or VALIDATED. Function names should match [FunctionDeclaration.name]. When set, model will predict a function call from only allowed function names.

JSON representation
{
  "mode": enum (Mode),
  "allowedFunctionNames": [
    string
  ]
}

Режим

Defines the execution behavior for function calling by defining the execution mode.

Перечисления
MODE_UNSPECIFIED Unspecified function calling mode. This value should not be used.
AUTO Default model behavior, model decides to predict either a function call or a natural language response.
ANY Model is constrained to always predicting a function call only. If "allowedFunctionNames" are set, the predicted function call will be limited to any one of "allowedFunctionNames", else the predicted function call will be any one of the provided "functionDeclarations".
NONE Model will not predict any function call. Model behavior is same as when not passing any function declarations.
VALIDATED Model decides to predict either a function call or a natural language response, but will validate function calls with constrained decoding. If "allowedFunctionNames" are set, the predicted function call will be limited to any one of "allowedFunctionNames", else the predicted function call will be any one of the provided "functionDeclarations".

ИспользованиеМетаданных

Metadata on the usage of the cached content.

Поля
totalTokenCount integer

Total number of tokens that the cached content consumes.

JSON-представление
{
  "totalTokenCount": integer
}
,

Context caching allows you to save and reuse precomputed input tokens that you wish to use repeatedly, for example when asking different questions about the same media file. This can lead to cost and speed savings, depending on the usage. For a detailed introduction, see the Context caching guide.

Method: cachedContents.create

Creates CachedContent resource.

Конечная точка

post https: / /generativelanguage.googleapis.com /v1beta /cachedContents

Текст запроса

The request body contains an instance of CachedContent .

Поля
contents[] object ( Content )

Optional. Input only. Immutable. The content to cache.

tools[] object ( Tool )

Optional. Input only. Immutable. A list of Tools the model may use to generate the next response

expiration Union type
Specifies when this resource will expire. expiration can be only one of the following:
expireTime string ( Timestamp format)

Timestamp in UTC of when this resource is considered expired. This is always provided on output, regardless of what was sent on input.

Uses RFC 3339, where generated output will always be Z-normalized and use 0, 3, 6 or 9 fractional digits. Offsets other than "Z" are also accepted. Examples: "2014-10-02T15:01:23Z" , "2014-10-02T15:01:23.045123456Z" or "2014-10-02T15:01:23+05:30" .

ttl string ( Duration format)

Input only. New TTL for this resource, input only.

A duration in seconds with up to nine fractional digits, ending with ' s '. Example: "3.5s" .

displayName string

Optional. Immutable. The user-generated meaningful display name of the cached content. Maximum 128 Unicode characters.

model string

Required. Immutable. The name of the Model to use for cached content Format: models/{model}

systemInstruction object ( Content )

Optional. Input only. Immutable. Developer set system instruction. Currently text only.

toolConfig object ( ToolConfig )

Optional. Input only. Immutable. Tool config. This config is shared for all tools.

Пример запроса

Базовый

Питон

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)

Node.js

// 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)

Оболочка

wget https://storage.googleapis.com/generativeai-downloads/data/a11.txt
echo '{
  "model": "models/gemini-1.5-flash-001",
  "contents":[
    {
      "parts":[
        {
          "inline_data": {
            "mime_type":"text/plain",
            "data": "'$(base64 $B64FLAGS a11.txt)'"
          }
        }
      ],
    "role": "user"
    }
  ],
  "systemInstruction": {
    "parts": [
      {
        "text": "You are an expert at analyzing transcripts."
      }
    ]
  },
  "ttl": "300s"
}' > request.json

curl -X POST "https://generativelanguage.googleapis.com/v1beta/cachedContents?key=$GEMINI_API_KEY" \
 -H 'Content-Type: application/json' \
 -d @request.json \
 > cache.json

CACHE_NAME=$(cat cache.json | grep '"name":' | cut -d '"' -f 4 | head -n 1)

curl -X POST "https://generativelanguage.googleapis.com/v1beta/models/gemini-1.5-flash-001:generateContent?key=$GEMINI_API_KEY" \
-H 'Content-Type: application/json' \
-d '{
      "contents": [
        {
          "parts":[{
            "text": "Please summarize this transcript"
          }],
          "role": "user"
        },
      ],
      "cachedContent": "'$CACHE_NAME'"
    }'

От имени

Питон

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.",
    ),
)
cache_name = cache.name  # Save the name for later

# Later retrieve the cache
cache = client.caches.get(name=cache_name)
response = client.models.generate_content(
    model=model_name,
    contents="Find a lighthearted moment from this transcript",
    config=types.GenerateContentConfig(cached_content=cache.name),
)
print(response.text)

Node.js

// 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.",
  },
});
const cacheName = cache.name; // Save the name for later

// Later retrieve the cache
const retrievedCache = await ai.caches.get({ name: cacheName });
const response = await ai.models.generateContent({
  model: modelName,
  contents: "Find a lighthearted moment from this transcript",
  config: { cachedContent: retrievedCache.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)
}
cacheName := cache.Name

// Later retrieve the cache.
cache, err = client.Caches.Get(ctx, cacheName, &genai.GetCachedContentConfig{})
if err != nil {
	log.Fatal(err)
}

response, err := client.Models.GenerateContent(
	ctx,
	modelName,
	genai.Text("Find a lighthearted moment from this transcript"),
	&genai.GenerateContentConfig{
		CachedContent: cache.Name,
	},
)
if err != nil {
	log.Fatal(err)
}
fmt.Println("Response from cache (create from name):")
printResponse(response)

From chat

Питон

from google import genai
from google.genai import types

client = genai.Client()
model_name = "gemini-1.5-flash-001"
system_instruction = "You are an expert analyzing transcripts."

# Create a chat session with the given system instruction.
chat = client.chats.create(
    model=model_name,
    config=types.GenerateContentConfig(system_instruction=system_instruction),
)
document = client.files.upload(file=media / "a11.txt")

response = chat.send_message(
    message=["Hi, could you summarize this transcript?", document]
)
print("\n\nmodel:  ", response.text)
response = chat.send_message(
    message=["Okay, could you tell me more about the trans-lunar injection"]
)
print("\n\nmodel:  ", response.text)

# To cache the conversation so far, pass the chat history as the list of contents.
cache = client.caches.create(
    model=model_name,
    config={
        "contents": chat.get_history(),
        "system_instruction": system_instruction,
    },
)
# Continue the conversation using the cached content.
chat = client.chats.create(
    model=model_name,
    config=types.GenerateContentConfig(cached_content=cache.name),
)
response = chat.send_message(
    message="I didn't understand that last part, could you explain it in simpler language?"
)
print("\n\nmodel:  ", response.text)

Node.js

// Make sure to include the following import:
// import {GoogleGenAI} from '@google/genai';
const ai = new GoogleGenAI({ apiKey: process.env.GEMINI_API_KEY });
const modelName = "gemini-1.5-flash-001";
const systemInstruction = "You are an expert analyzing transcripts.";

// Create a chat session with the system instruction.
const chat = ai.chats.create({
  model: modelName,
  config: { systemInstruction: systemInstruction },
});
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);

let response = await chat.sendMessage({
  message: createUserContent([
    "Hi, could you summarize this transcript?",
    createPartFromUri(document.uri, document.mimeType),
  ]),
});
console.log("\n\nmodel:", response.text);

response = await chat.sendMessage({
  message: "Okay, could you tell me more about the trans-lunar injection",
});
console.log("\n\nmodel:", response.text);

// To cache the conversation so far, pass the chat history as the list of contents.
const chatHistory = chat.getHistory();
const cache = await ai.caches.create({
  model: modelName,
  config: {
    contents: chatHistory,
    systemInstruction: systemInstruction,
  },
});

// Continue the conversation using the cached content.
const chatWithCache = ai.chats.create({
  model: modelName,
  config: { cachedContent: cache.name },
});
response = await chatWithCache.sendMessage({
  message:
    "I didn't understand that last part, could you explain it in simpler language?",
});
console.log("\n\nmodel:", 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"
systemInstruction := "You are an expert analyzing transcripts."

// Create initial chat with a system instruction.
chat, err := client.Chats.Create(ctx, modelName, &genai.GenerateContentConfig{
	SystemInstruction: genai.NewContentFromText(systemInstruction, genai.RoleUser),
}, nil)
if err != nil {
	log.Fatal(err)
}

document, err := client.Files.UploadFromPath(
	ctx, 
	filepath.Join(getMedia(), "a11.txt"), 
	&genai.UploadFileConfig{
		MIMEType : "text/plain",
	},
)
if err != nil {
	log.Fatal(err)
}

// Send first message with the transcript.
parts := make([]genai.Part, 2)
parts[0] = genai.Part{Text: "Hi, could you summarize this transcript?"}
parts[1] = genai.Part{
	FileData: &genai.FileData{
		FileURI :      document.URI,
		MIMEType: document.MIMEType,
	},
}

// Send chat message.
resp, err := chat.SendMessage(ctx, parts...)
if err != nil {
	log.Fatal(err)
}
fmt.Println("\n\nmodel: ", resp.Text())

resp, err = chat.SendMessage(
	ctx, 
	genai.Part{
		Text: "Okay, could you tell me more about the trans-lunar injection",
	},
)
if err != nil {
	log.Fatal(err)
}
fmt.Println("\n\nmodel: ", resp.Text())

// To cache the conversation so far, pass the chat history as the list of contents.
cache, err := client.Caches.Create(ctx, modelName, &genai.CreateCachedContentConfig{
	Contents:          chat.History(false),
	SystemInstruction: genai.NewContentFromText(systemInstruction, genai.RoleUser),
})
if err != nil {
	log.Fatal(err)
}

// Continue the conversation using the cached history.
chat, err = client.Chats.Create(ctx, modelName, &genai.GenerateContentConfig{
	CachedContent: cache.Name,
}, nil)
if err != nil {
	log.Fatal(err)
}

resp, err = chat.SendMessage(
	ctx, 
	genai.Part{
		Text: "I didn't understand that last part, could you explain it in simpler language?",
	},
)
if err != nil {
	log.Fatal(err)
}
fmt.Println("\n\nmodel: ", resp.Text())

Тело ответа

If successful, the response body contains a newly created instance of CachedContent .

Method: cachedContents.list

Lists CachedContents.

Конечная точка

get https: / /generativelanguage.googleapis.com /v1beta /cachedContents

Параметры запроса

pageSize integer

Optional. The maximum number of cached contents to return. The service may return fewer than this value. If unspecified, some default (under maximum) number of items will be returned. The maximum value is 1000; values above 1000 will be coerced to 1000.

pageToken string

Optional. A page token, received from a previous cachedContents.list call. Provide this to retrieve the subsequent page.

When paginating, all other parameters provided to cachedContents.list must match the call that provided the page token.

Текст запроса

Тело запроса должно быть пустым.

Тело ответа

Response with CachedContents list.

В случае успеха тело ответа содержит данные со следующей структурой:

Поля
cachedContents[] object ( CachedContent )

List of cached contents.

nextPageToken string

A token, which can be sent as pageToken to retrieve the next page. If this field is omitted, there are no subsequent pages.

JSON representation
{
  "cachedContents": [
    {
      object (CachedContent)
    }
  ],
  "nextPageToken": string
}

Method: cachedContents.get

Reads CachedContent resource.

Конечная точка

get https: / /generativelanguage.googleapis.com /v1beta /{name=cachedContents /*}

Параметры пути

string name

Required. The resource name referring to the content cache entry. Format: cachedContents/{id} It takes the form cachedContents/{cachedcontent} .

Текст запроса

Тело запроса должно быть пустым.

Пример запроса

Питон

from google import genai

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={
        "contents": [document],
        "system_instruction": "You are an expert analyzing transcripts.",
    },
)
print(client.caches.get(name=cache.name))

Node.js

// 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.",
  },
});
const retrievedCache = await ai.caches.get({ name: cache.name });
console.log("Retrieved Cache:", retrievedCache);

Идти

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)
}

cache, err = client.Caches.Get(ctx, cache.Name, &genai.GetCachedContentConfig{})
if err != nil {
	log.Fatal(err)
}
fmt.Println("Retrieved cache:")
fmt.Println(cache)

Оболочка

curl "https://generativelanguage.googleapis.com/v1beta/$CACHE_NAME?key=$GEMINI_API_KEY"

Тело ответа

If successful, the response body contains an instance of CachedContent .

Method: cachedContents.patch

Updates CachedContent resource (only expiration is updatable).

Конечная точка

patch https: / /generativelanguage.googleapis.com /v1beta /{cachedContent.name=cachedContents /*}
PATCH https://generativelanguage.googleapis.com/v1beta/{cachedContent.name=cachedContents/*}

Параметры пути

cachedContent.name string

Output only. Identifier. The resource name referring to the cached content. Format: cachedContents/{id} It takes the form cachedContents/{cachedcontent} .

Параметры запроса

updateMask string ( FieldMask format)

The list of fields to update.

This is a comma-separated list of fully qualified names of fields. Example: "user.displayName,photo" .

Текст запроса

The request body contains an instance of CachedContent .

Поля
expiration Union type
Specifies when this resource will expire. expiration can be only one of the following:
expireTime string ( Timestamp format)

Timestamp in UTC of when this resource is considered expired. This is always provided on output, regardless of what was sent on input.

Uses RFC 3339, where generated output will always be Z-normalized and use 0, 3, 6 or 9 fractional digits. Offsets other than "Z" are also accepted. Examples: "2014-10-02T15:01:23Z" , "2014-10-02T15:01:23.045123456Z" or "2014-10-02T15:01:23+05:30" .

ttl string ( Duration format)

Input only. New TTL for this resource, input only.

A duration in seconds with up to nine fractional digits, ending with ' s '. Example: "3.5s" .

Пример запроса

Питон

from google import genai
from google.genai import types
import datetime

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={
        "contents": [document],
        "system_instruction": "You are an expert analyzing transcripts.",
    },
)

# Update the cache's time-to-live (ttl)
ttl = f"{int(datetime.timedelta(hours=2).total_seconds())}s"
client.caches.update(
    name=cache.name, config=types.UpdateCachedContentConfig(ttl=ttl)
)
print(f"After update:\n {cache}")

# Alternatively, update the expire_time directly
# Update the expire_time directly in valid RFC 3339 format (UTC with a "Z" suffix)
expire_time = (
    (
        datetime.datetime.now(datetime.timezone.utc)
        + datetime.timedelta(minutes=15)
    )
    .isoformat()
    .replace("+00:00", "Z")
)
client.caches.update(
    name=cache.name,
    config=types.UpdateCachedContentConfig(expire_time=expire_time),
)

Node.js

// 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)),
];

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

// Update the cache's time-to-live (ttl)
const ttl = `${2 * 3600}s`; // 2 hours in seconds
cache = await ai.caches.update({
  name: cache.name,
  config: { ttl },
});
console.log("After update (TTL):", cache);

// Alternatively, update the expire_time directly (in RFC 3339 format with a "Z" suffix)
const expireTime = new Date(Date.now() + 15 * 60000)
  .toISOString()
  .replace(/\.\d{3}Z$/, "Z");
cache = await ai.caches.update({
  name: cache.name,
  config: { expireTime: expireTime },
});
console.log("After update (expire_time):", cache);

Идти

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)
}

_, err = client.Caches.Delete(ctx, cache.Name, &genai.DeleteCachedContentConfig{})
if err != nil {
	log.Fatal(err)
}
fmt.Println("Cache deleted:", cache.Name)

Оболочка

curl -X PATCH "https://generativelanguage.googleapis.com/v1beta/$CACHE_NAME?key=$GEMINI_API_KEY" \
 -H 'Content-Type: application/json' \
 -d '{"ttl": "600s"}'

Тело ответа

If successful, the response body contains an instance of CachedContent .

Method: cachedContents.delete

Deletes CachedContent resource.

Конечная точка

delete https: / /generativelanguage.googleapis.com /v1beta /{name=cachedContents /*}

Параметры пути

string name

Required. The resource name referring to the content cache entry Format: cachedContents/{id} It takes the form cachedContents/{cachedcontent} .

Текст запроса

Тело запроса должно быть пустым.

Пример запроса

Питон

from google import genai

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={
        "contents": [document],
        "system_instruction": "You are an expert analyzing transcripts.",
    },
)
client.caches.delete(name=cache.name)

Node.js

// 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.",
  },
});
await ai.caches.delete({ name: cache.name });
console.log("Cache deleted:", cache.name);

Идти

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)
}

_, err = client.Caches.Delete(ctx, cache.Name, &genai.DeleteCachedContentConfig{})
if err != nil {
	log.Fatal(err)
}
fmt.Println("Cache deleted:", cache.Name)

Оболочка

curl -X DELETE "https://generativelanguage.googleapis.com/v1beta/$CACHE_NAME?key=$GEMINI_API_KEY"

Тело ответа

В случае успеха тело ответа представляет собой пустой объект JSON.

REST Resource: cachedContents

Resource: CachedContent

Content that has been preprocessed and can be used in subsequent request to GenerativeService.

Cached content can be only used with model it was created for.

Поля
contents[] object ( Content )

Optional. Input only. Immutable. The content to cache.

tools[] object ( Tool )

Optional. Input only. Immutable. A list of Tools the model may use to generate the next response

createTime string ( Timestamp format)

Output only. Creation time of the cache entry.

Uses RFC 3339, where generated output will always be Z-normalized and use 0, 3, 6 or 9 fractional digits. Offsets other than "Z" are also accepted. Examples: "2014-10-02T15:01:23Z" , "2014-10-02T15:01:23.045123456Z" or "2014-10-02T15:01:23+05:30" .

updateTime string ( Timestamp format)

Output only. When the cache entry was last updated in UTC time.

Uses RFC 3339, where generated output will always be Z-normalized and use 0, 3, 6 or 9 fractional digits. Offsets other than "Z" are also accepted. Examples: "2014-10-02T15:01:23Z" , "2014-10-02T15:01:23.045123456Z" or "2014-10-02T15:01:23+05:30" .

usageMetadata object ( UsageMetadata )

Output only. Metadata on the usage of the cached content.

expiration Union type
Specifies when this resource will expire. expiration can be only one of the following:
expireTime string ( Timestamp format)

Timestamp in UTC of when this resource is considered expired. This is always provided on output, regardless of what was sent on input.

Uses RFC 3339, where generated output will always be Z-normalized and use 0, 3, 6 or 9 fractional digits. Offsets other than "Z" are also accepted. Examples: "2014-10-02T15:01:23Z" , "2014-10-02T15:01:23.045123456Z" or "2014-10-02T15:01:23+05:30" .

ttl string ( Duration format)

Input only. New TTL for this resource, input only.

A duration in seconds with up to nine fractional digits, ending with ' s '. Example: "3.5s" .

string name

Output only. Identifier. The resource name referring to the cached content. Format: cachedContents/{id}

displayName string

Optional. Immutable. The user-generated meaningful display name of the cached content. Maximum 128 Unicode characters.

model string

Required. Immutable. The name of the Model to use for cached content Format: models/{model}

systemInstruction object ( Content )

Optional. Input only. Immutable. Developer set system instruction. Currently text only.

toolConfig object ( ToolConfig )

Optional. Input only. Immutable. Tool config. This config is shared for all tools.

JSON representation
{
  "contents": [
    {
      object (Content)
    }
  ],
  "tools": [
    {
      object (Tool)
    }
  ],
  "createTime": string,
  "updateTime": string,
  "usageMetadata": {
    object (UsageMetadata)
  },

  // expiration
  "expireTime": string,
  "ttl": string
  // Union type
  "name": string,
  "displayName": string,
  "model": string,
  "systemInstruction": {
    object (Content)
  },
  "toolConfig": {
    object (ToolConfig)
  }
}

Содержание

The base structured datatype containing multi-part content of a message.

A Content includes a role field designating the producer of the Content and a parts field containing multi-part data that contains the content of the message turn.

Поля
parts[] object ( Part )

Ordered Parts that constitute a single message. Parts may have different MIME types.

role string

Optional. The producer of the content. Must be either 'user' or 'model'.

Useful to set for multi-turn conversations, otherwise can be left blank or unset.

JSON representation
{
  "parts": [
    {
      object (Part)
    }
  ],
  "role": string
}

Часть

A datatype containing media that is part of a multi-part Content message.

A Part consists of data which has an associated datatype. A Part can only contain one of the accepted types in Part.data .

A Part must have a fixed IANA MIME type identifying the type and subtype of the media if the inlineData field is filled with raw bytes.

Поля
thought boolean

Optional. Indicates if the part is thought from the model.

thoughtSignature string ( bytes format)

Optional. An opaque signature for the thought so it can be reused in subsequent requests.

A base64-encoded string.

partMetadata object ( Struct format)

Custom metadata associated with the Part. Agents using genai.Part as content representation may need to keep track of the additional information. For example it can be name of a file/source from which the Part originates or a way to multiplex multiple Part streams.

data Union type
data can be only one of the following:
text string

Inline text.

inlineData object ( Blob )

Inline media bytes.

functionCall object ( FunctionCall )

A predicted FunctionCall returned from the model that contains a string representing the FunctionDeclaration.name with the arguments and their values.

functionResponse object ( FunctionResponse )

The result output of a FunctionCall that contains a string representing the FunctionDeclaration.name and a structured JSON object containing any output from the function is used as context to the model.

fileData object ( FileData )

URI based data.

executableCode object ( ExecutableCode )

Code generated by the model that is meant to be executed.

codeExecutionResult object ( CodeExecutionResult )

Result of executing the ExecutableCode .

metadata Union type
Controls extra preprocessing of data. metadata can be only one of the following:
videoMetadata object ( VideoMetadata )

Optional. Video metadata. The metadata should only be specified while the video data is presented in inlineData or fileData.

JSON representation
{
  "thought": boolean,
  "thoughtSignature": string,
  "partMetadata": {
    object
  },

  // data
  "text": string,
  "inlineData": {
    object (Blob)
  },
  "functionCall": {
    object (FunctionCall)
  },
  "functionResponse": {
    object (FunctionResponse)
  },
  "fileData": {
    object (FileData)
  },
  "executableCode": {
    object (ExecutableCode)
  },
  "codeExecutionResult": {
    object (CodeExecutionResult)
  }
  // Union type

  // metadata
  "videoMetadata": {
    object (VideoMetadata)
  }
  // Union type
}

Клякса

Raw media bytes.

Text should not be sent as raw bytes, use the 'text' field.

Поля
mimeType string

The IANA standard MIME type of the source data. Examples: - image/png - image/jpeg If an unsupported MIME type is provided, an error will be returned. For a complete list of supported types, see Supported file formats .

data string ( bytes format)

Raw bytes for media formats.

A base64-encoded string.

JSON representation
{
  "mimeType": string,
  "data": string
}

FunctionCall

A predicted FunctionCall returned from the model that contains a string representing the FunctionDeclaration.name with the arguments and their values.

Поля
string id

Optional. The unique id of the function call. If populated, the client to execute the functionCall and return the response with the matching id .

string name

Required. The name of the function to call. Must be az, AZ, 0-9, or contain underscores and dashes, with a maximum length of 64.

args object ( Struct format)

Optional. The function parameters and values in JSON object format.

JSON representation
{
  "id": string,
  "name": string,
  "args": {
    object
  }
}

FunctionResponse

The result output from a FunctionCall that contains a string representing the FunctionDeclaration.name and a structured JSON object containing any output from the function is used as context to the model. This should contain the result of a FunctionCall made based on model prediction.

Поля
string id

Optional. The id of the function call this response is for. Populated by the client to match the corresponding function call id .

string name

Required. The name of the function to call. Must be az, AZ, 0-9, or contain underscores and dashes, with a maximum length of 64.

response object ( Struct format)

Required. The function response in JSON object format. Callers can use any keys of their choice that fit the function's syntax to return the function output, eg "output", "result", etc. In particular, if the function call failed to execute, the response can have an "error" key to return error details to the model.

parts[] object ( FunctionResponsePart )

Optional. Ordered Parts that constitute a function response. Parts may have different IANA MIME types.

willContinue boolean

Optional. Signals that function call continues, and more responses will be returned, turning the function call into a generator. Is only applicable to NON_BLOCKING function calls, is ignored otherwise. If set to false, future responses will not be considered. It is allowed to return empty response with willContinue=False to signal that the function call is finished. This may still trigger the model generation. To avoid triggering the generation and finish the function call, additionally set scheduling to SILENT .

scheduling enum ( Scheduling )

Optional. Specifies how the response should be scheduled in the conversation. Only applicable to NON_BLOCKING function calls, is ignored otherwise. Defaults to WHEN_IDLE.

JSON representation
{
  "id": string,
  "name": string,
  "response": {
    object
  },
  "parts": [
    {
      object (FunctionResponsePart)
    }
  ],
  "willContinue": boolean,
  "scheduling": enum (Scheduling)
}

ФункцияОтветЧасть

A datatype containing media that is part of a FunctionResponse message.

A FunctionResponsePart consists of data which has an associated datatype. A FunctionResponsePart can only contain one of the accepted types in FunctionResponsePart.data .

A FunctionResponsePart must have a fixed IANA MIME type identifying the type and subtype of the media if the inlineData field is filled with raw bytes.

Поля
data Union type
The data of the function response part. data can be only one of the following:
inlineData object ( FunctionResponseBlob )

Inline media bytes.

JSON-представление
{

  // data
  "inlineData": {
    object (FunctionResponseBlob)
  }
  // Union type
}

FunctionResponseBlob

Raw media bytes for function response.

Text should not be sent as raw bytes, use the 'FunctionResponse.response' field.

Поля
mimeType string

The IANA standard MIME type of the source data. Examples: - image/png - image/jpeg If an unsupported MIME type is provided, an error will be returned. For a complete list of supported types, see Supported file formats .

data string ( bytes format)

Raw bytes for media formats.

A base64-encoded string.

JSON representation
{
  "mimeType": string,
  "data": string
}

Планирование

Specifies how the response should be scheduled in the conversation.

Перечисления
SCHEDULING_UNSPECIFIED This value is unused.
SILENT Only add the result to the conversation context, do not interrupt or trigger generation.
WHEN_IDLE Add the result to the conversation context, and prompt to generate output without interrupting ongoing generation.
INTERRUPT Add the result to the conversation context, interrupt ongoing generation and prompt to generate output.

FileData

URI based data.

Поля
mimeType string

Optional. The IANA standard MIME type of the source data.

fileUri string

Required. URI.

JSON representation
{
  "mimeType": string,
  "fileUri": string
}

ExecutableCode

Code generated by the model that is meant to be executed, and the result returned to the model.

Only generated when using the CodeExecution tool, in which the code will be automatically executed, and a corresponding CodeExecutionResult will also be generated.

Поля
language enum ( Language )

Required. Programming language of the code .

code string

Required. The code to be executed.

JSON representation
{
  "language": enum (Language),
  "code": string
}

Язык

Supported programming languages for the generated code.

Перечисления
LANGUAGE_UNSPECIFIED Unspecified language. This value should not be used.
PYTHON Python >= 3.10, with numpy and simpy available.

CodeExecutionResult

Result of executing the ExecutableCode .

Only generated when using the CodeExecution , and always follows a part containing the ExecutableCode .

Поля
outcome enum ( Outcome )

Required. Outcome of the code execution.

output string

Optional. Contains stdout when code execution is successful, stderr or other description otherwise.

JSON representation
{
  "outcome": enum (Outcome),
  "output": string
}

Исход

Enumeration of possible outcomes of the code execution.

Перечисления
OUTCOME_UNSPECIFIED Unspecified status. This value should not be used.
OUTCOME_OK Code execution completed successfully.
OUTCOME_FAILED Code execution finished but with a failure. stderr should contain the reason.
OUTCOME_DEADLINE_EXCEEDED Code execution ran for too long, and was cancelled. There may or may not be a partial output present.

ВидеоМетаданные

Metadata describes the input video content.

Поля
startOffset string ( Duration format)

Optional. The start offset of the video.

A duration in seconds with up to nine fractional digits, ending with ' s '. Example: "3.5s" .

endOffset string ( Duration format)

Optional. The end offset of the video.

A duration in seconds with up to nine fractional digits, ending with ' s '. Example: "3.5s" .

fps number

Optional. The frame rate of the video sent to the model. If not specified, the default value will be 1.0. The fps range is (0.0, 24.0].

JSON representation
{
  "startOffset": string,
  "endOffset": string,
  "fps": number
}

Инструмент

Tool details that the model may use to generate response.

A Tool is a piece of code that enables the system to interact with external systems to perform an action, or set of actions, outside of knowledge and scope of the model.

Next ID: 12

Поля
functionDeclarations[] object ( FunctionDeclaration )

Optional. A list of FunctionDeclarations available to the model that can be used for function calling.

The model or system does not execute the function. Instead the defined function may be returned as a FunctionCall with arguments to the client side for execution. The model may decide to call a subset of these functions by populating FunctionCall in the response. The next conversation turn may contain a FunctionResponse with the Content.role "function" generation context for the next model turn.

googleSearchRetrieval object ( GoogleSearchRetrieval )

Optional. Retrieval tool that is powered by Google search.

codeExecution object ( CodeExecution )

Optional. Enables the model to execute code as part of generation.

computerUse object ( ComputerUse )

Optional. Tool to support the model interacting directly with the computer. If enabled, it automatically populates computer-use specific Function Declarations.

urlContext object ( UrlContext )

Optional. Tool to support URL context retrieval.

JSON representation
{
  "functionDeclarations": [
    {
      object (FunctionDeclaration)
    }
  ],
  "googleSearchRetrieval": {
    object (GoogleSearchRetrieval)
  },
  "codeExecution": {
    object (CodeExecution)
  },
  "googleSearch": {
    object (GoogleSearch)
  },
  "computerUse": {
    object (ComputerUse)
  },
  "urlContext": {
    object (UrlContext)
  },
  "fileSearch": {
    object (FileSearch)
  }
}

Объявление функции

Structured representation of a function declaration as defined by the OpenAPI 3.03 specification . Included in this declaration are the function name and parameters. This FunctionDeclaration is a representation of a block of code that can be used as a Tool by the model and executed by the client.

Поля
string name

Required. The name of the function. Must be az, AZ, 0-9, or contain underscores, colons, dots, and dashes, with a maximum length of 64.

string description

Required. A brief description of the function.

behavior enum ( Behavior )

Optional. Specifies the function Behavior. Currently only supported by the BidiGenerateContent method.

parameters object ( Schema )

Optional. Describes the parameters to this function. Reflects the Open API 3.03 Parameter Object string Key: the name of the parameter. Parameter names are case sensitive. Schema Value: the Schema defining the type used for the parameter.

parametersJsonSchema value ( Value format)

Optional. Describes the parameters to the function in JSON Schema format. The schema must describe an object where the properties are the parameters to the function. For example:

{
  "type": "object",
  "properties": {
    "name": { "type": "string" },
    "age": { "type": "integer" }
  },
  "additionalProperties": false,
  "required": ["name", "age"],
  "propertyOrdering": ["name", "age"]
}

This field is mutually exclusive with parameters .

response object ( Schema )

Optional. Describes the output from this function in JSON Schema format. Reflects the Open API 3.03 Response Object. The Schema defines the type used for the response value of the function.

responseJsonSchema value ( Value format)

Optional. Describes the output from this function in JSON Schema format. The value specified by the schema is the response value of the function.

This field is mutually exclusive with response .

JSON representation
{
  "name": string,
  "description": string,
  "behavior": enum (Behavior),
  "parameters": {
    object (Schema)
  },
  "parametersJsonSchema": value,
  "response": {
    object (Schema)
  },
  "responseJsonSchema": value
}

Схема

The Schema object allows the definition of input and output data types. These types can be objects, but also primitives and arrays. Represents a select subset of an OpenAPI 3.0 schema object .

Поля
type enum ( Type )

Required. Data type.

format string

Optional. The format of the data. Any value is allowed, but most do not trigger any special functionality.

string title

Optional. The title of the schema.

string description

Optional. A brief description of the parameter. This could contain examples of use. Parameter description may be formatted as Markdown.

boolean nullable

Optional. Indicates if the value may be null.

enum[] string

Optional. Possible values of the element of Type.STRING with enum format. For example we can define an Enum Direction as : {type:STRING, format:enum, enum:["EAST", NORTH", "SOUTH", "WEST"]}

maxItems string ( int64 format)

Optional. Maximum number of the elements for Type.ARRAY.

minItems string ( int64 format)

Optional. Minimum number of the elements for Type.ARRAY.

properties map (key: string, value: object ( Schema ))

Optional. Properties of Type.OBJECT.

An object containing a list of "key": value pairs. Example: { "name": "wrench", "mass": "1.3kg", "count": "3" } .

required[] string

Optional. Required properties of Type.OBJECT.

minProperties string ( int64 format)

Optional. Minimum number of the properties for Type.OBJECT.

maxProperties string ( int64 format)

Optional. Maximum number of the properties for Type.OBJECT.

minLength string ( int64 format)

Optional. SCHEMA FIELDS FOR TYPE STRING Minimum length of the Type.STRING

maxLength string ( int64 format)

Optional. Maximum length of the Type.STRING

pattern string

Optional. Pattern of the Type.STRING to restrict a string to a regular expression.

example value ( Value format)

Optional. Example of the object. Will only populated when the object is the root.

anyOf[] object ( Schema )

Optional. The value should be validated against any (one or more) of the subschemas in the list.

propertyOrdering[] string

Optional. The order of the properties. Not a standard field in open api spec. Used to determine the order of the properties in the response.

default value ( Value format)

Optional. Default value of the field. Per JSON Schema, this field is intended for documentation generators and doesn't affect validation. Thus it's included here and ignored so that developers who send schemas with a default field don't get unknown-field errors.

items object ( Schema )

Optional. Schema of the elements of Type.ARRAY.

minimum number

Optional. SCHEMA FIELDS FOR TYPE INTEGER and NUMBER Minimum value of the Type.INTEGER and Type.NUMBER

maximum number

Optional. Maximum value of the Type.INTEGER and Type.NUMBER

JSON representation
{
  "type": enum (Type),
  "format": string,
  "title": string,
  "description": string,
  "nullable": boolean,
  "enum": [
    string
  ],
  "maxItems": string,
  "minItems": string,
  "properties": {
    string: {
      object (Schema)
    },
    ...
  },
  "required": [
    string
  ],
  "minProperties": string,
  "maxProperties": string,
  "minLength": string,
  "maxLength": string,
  "pattern": string,
  "example": value,
  "anyOf": [
    {
      object (Schema)
    }
  ],
  "propertyOrdering": [
    string
  ],
  "default": value,
  "items": {
    object (Schema)
  },
  "minimum": number,
  "maximum": number
}

Тип

Type contains the list of OpenAPI data types as defined by https://spec.openapis.org/oas/v3.0.3#data-types

Перечисления
TYPE_UNSPECIFIED Not specified, should not be used.
STRING String type.
NUMBER Number type.
INTEGER Целочисленный тип.
BOOLEAN Булевский тип.
ARRAY Array type.
OBJECT Object type.
NULL Null type.

Поведение

Defines the function behavior. Defaults to BLOCKING .

Перечисления
UNSPECIFIED This value is unused.
BLOCKING If set, the system will wait to receive the function response before continuing the conversation.
NON_BLOCKING If set, the system will not wait to receive the function response. Instead, it will attempt to handle function responses as they become available while maintaining the conversation between the user and the model.

GoogleSearchRetrieval

Tool to retrieve public web data for grounding, powered by Google.

Поля
dynamicRetrievalConfig object ( DynamicRetrievalConfig )

Specifies the dynamic retrieval configuration for the given source.

JSON-представление
{
  "dynamicRetrievalConfig": {
    object (DynamicRetrievalConfig)
  }
}

DynamicRetrievalConfig

Describes the options to customize dynamic retrieval.

Поля
mode enum ( Mode )

The mode of the predictor to be used in dynamic retrieval.

dynamicThreshold number

The threshold to be used in dynamic retrieval. If not set, a system default value is used.

JSON representation
{
  "mode": enum (Mode),
  "dynamicThreshold": number
}

Режим

The mode of the predictor to be used in dynamic retrieval.

Перечисления
MODE_UNSPECIFIED Always trigger retrieval.
MODE_DYNAMIC Run retrieval only when system decides it is necessary.

CodeExecution

Этот тип не имеет полей.

Tool that executes code generated by the model, and automatically returns the result to the model.

See also ExecutableCode and CodeExecutionResult which are only generated when using this tool.

GoogleSearch

GoogleSearch tool type. Tool to support Google Search in Model. Powered by Google.

Поля
timeRangeFilter object ( Interval )

Optional. Filter search results to a specific time range. If customers set a start time, they must set an end time (and vice versa).

JSON representation
{
  "timeRangeFilter": {
    object (Interval)
  }
}

Интервал

Представляет временной интервал, закодированный как начальная метка времени (включительно) и конечная метка времени (исключая).

Начало должно быть меньше или равно концу. Если начало равно концу, интервал пустой (не соответствует ни одному времени). Если ни начало, ни конец не указаны, интервал соответствует любому времени.

Поля
startTime string ( Timestamp format)

Необязательно. Начало интервала включено.

Если указано, то соответствующая этому интервалу временная метка должна быть такой же или более поздней.

Uses RFC 3339, where generated output will always be Z-normalized and use 0, 3, 6 or 9 fractional digits. Offsets other than "Z" are also accepted. Examples: "2014-10-02T15:01:23Z" , "2014-10-02T15:01:23.045123456Z" or "2014-10-02T15:01:23+05:30" .

endTime string ( Timestamp format)

Optional. Exclusive end of the interval.

Если указано, то соответствующая этому интервалу временная метка должна быть до конца.

Uses RFC 3339, where generated output will always be Z-normalized and use 0, 3, 6 or 9 fractional digits. Offsets other than "Z" are also accepted. Examples: "2014-10-02T15:01:23Z" , "2014-10-02T15:01:23.045123456Z" or "2014-10-02T15:01:23+05:30" .

JSON representation
{
  "startTime": string,
  "endTime": string
}

ComputerUse

Computer Use tool type.

Поля
environment enum ( Environment )

Required. The environment being operated.

excludedPredefinedFunctions[] string

Optional. By default, predefined functions are included in the final model call. Some of them can be explicitly excluded from being automatically included. This can serve two purposes: 1. Using a more restricted / different action space. 2. Improving the definitions / instructions of predefined functions.

JSON representation
{
  "environment": enum (Environment),
  "excludedPredefinedFunctions": [
    string
  ]
}

Среда

Represents the environment being operated, such as a web browser.

Перечисления
ENVIRONMENT_UNSPECIFIED Defaults to browser.
ENVIRONMENT_BROWSER Operates in a web browser.

UrlContext

Этот тип не имеет полей.

Tool to support URL context retrieval.

FileSearch

The FileSearch tool that retrieves knowledge from Semantic Retrieval corpora. Files are imported to Semantic Retrieval corpora using the ImportFile API.

Поля
retrievalResources[] object ( RetrievalResource )

Required. Semantic retrieval resources to retrieve from. Currently only supports one corpus. In the future we may open up multiple corpora support.

retrievalConfig object ( RetrievalConfig )

Optional. The configuration for the retrieval.

JSON representation
{
  "retrievalResources": [
    {
      object (RetrievalResource)
    }
  ],
  "retrievalConfig": {
    object (RetrievalConfig)
  }
}

RetrievalResource

The semantic retrieval resource to retrieve from.

Поля
ragStoreName string

Required. The name of the semantic retrieval resource to retrieve from. Example: ragStores/my-rag-store-123

JSON representation
{
  "ragStoreName": string
}

RetrievalConfig

Semantic retrieval configuration.

Поля
metadataFilter string

Optional. Metadata filter to apply to the semantic retrieval documents and chunks.

topK integer

Optional. The number of semantic retrieval chunks to retrieve.

JSON representation
{
  "metadataFilter": string,
  "topK": integer
}

ToolConfig

The Tool configuration containing parameters for specifying Tool use in the request.

Поля
functionCallingConfig object ( FunctionCallingConfig )

Optional. Function calling config.

JSON representation
{
  "functionCallingConfig": {
    object (FunctionCallingConfig)
  }
}

FunctionCallingConfig

Configuration for specifying function calling behavior.

Поля
mode enum ( Mode )

Optional. Specifies the mode in which function calling should execute. If unspecified, the default value will be set to AUTO.

allowedFunctionNames[] string

Optional. A set of function names that, when provided, limits the functions the model will call.

This should only be set when the Mode is ANY or VALIDATED. Function names should match [FunctionDeclaration.name]. When set, model will predict a function call from only allowed function names.

JSON representation
{
  "mode": enum (Mode),
  "allowedFunctionNames": [
    string
  ]
}

Режим

Defines the execution behavior for function calling by defining the execution mode.

Перечисления
MODE_UNSPECIFIED Unspecified function calling mode. This value should not be used.
AUTO Default model behavior, model decides to predict either a function call or a natural language response.
ANY Model is constrained to always predicting a function call only. If "allowedFunctionNames" are set, the predicted function call will be limited to any one of "allowedFunctionNames", else the predicted function call will be any one of the provided "functionDeclarations".
NONE Model will not predict any function call. Model behavior is same as when not passing any function declarations.
VALIDATED Model decides to predict either a function call or a natural language response, but will validate function calls with constrained decoding. If "allowedFunctionNames" are set, the predicted function call will be limited to any one of "allowedFunctionNames", else the predicted function call will be any one of the provided "functionDeclarations".

ИспользованиеМетаданных

Metadata on the usage of the cached content.

Поля
totalTokenCount integer

Total number of tokens that the cached content consumes.

JSON representation
{
  "totalTokenCount": integer
}