Skip to main content

Gemini API Reference

Generate Content

Endpoint

POST https://api.rockapi.ru/google-ai-studio/v1beta/{model=models/*}:generateContent

Path Parameters

NameTypeDescription
modelstringRequired. The name of the Model to use for generating the completion. Format: name=models/{model}.

Request Body

The request body contains data with the following structure:

Fields

NameTypeDescription
contents[]objectRequired. The content of the current conversation with the model. For single-turn queries, this is a single instance. For multi-turn queries, this is a repeated field that contains conversation history + latest request.
tools[]objectOptional. A list of Tools the model may use to generate the next 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. The only supported tool is currently Function.
toolConfigobjectOptional. Tool configuration for any Tool specified in the request.
safetySettings[]objectOptional. A list of unique SafetySetting instances for blocking unsafe content. This will be enforced on the GenerateContentRequest.contents and GenerateContentResponse.candidates. There should not be more than one setting for each SafetyCategory type. The API will block any contents and responses that fail to meet the thresholds set by these settings. This list overrides the default settings for each SafetyCategory specified in the safetySettings. If there is no SafetySetting for a given SafetyCategory provided in the list, the API will use the default safety setting for that category. Supported harm categories: HARM_CATEGORY_HATE_SPEECH, HARM_CATEGORY_SEXUALLY_EXPLICIT, HARM_CATEGORY_DANGEROUS_CONTENT, HARM_CATEGORY_HARASSMENT.
systemInstructionobjectOptional. Developer set system instruction. Currently, text only.
generationConfigobjectOptional. Configuration options for model generation and outputs.
cachedContentstringOptional. The name of the cached content used as context to serve the prediction. Note: only used in explicit caching, where users can have control over caching (e.g., what content to cache) and enjoy guaranteed cost savings. Format: cachedContents/{cachedContent}.

Example Request

import google.generativeai as genai
from google.api_core.client_options import ClientOptions

genai.configure(
api_key='$ROCKAPI_API_KEY',
transport='rest',
client_options=ClientOptions(api_endpoint='https://api.rockapi.ru/google-ai-studio')
)

model = genai.GenerativeModel("gemini-1.5-flash")
response = model.generate_content("Write a story about a magic backpack.")
print(response.text)

Response Body

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


Stream Generate Content

Endpoint

POST https://api.rockapi.ru/google-ai-studio/v1beta/{model=models/*}:streamGenerateContent

Path Parameters

NameTypeDescription
modelstringRequired. The name of the Model to use for generating the completion. Format: name=models/{model}.

Request Body

The request body contains data with the following structure:

Fields

NameTypeDescription
contents[]objectRequired. The content of the current conversation with the model. For single-turn queries, this is a single instance. For multi-turn queries, this is a repeated field that contains conversation history + latest request.
tools[]objectOptional. A list of Tools the model may use to generate the next 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. The only supported tool is currently Function.
toolConfigobjectOptional. Tool configuration for any Tool specified in the request.
safetySettings[]objectOptional. A list of unique SafetySetting instances for blocking unsafe content. This will be enforced on the GenerateContentRequest.contents and GenerateContentResponse.candidates. There should not be more than one setting for each SafetyCategory type. The API will block any contents and responses that fail to meet the thresholds set by these settings. This list overrides the default settings for each SafetyCategory specified in the safetySettings. If there is no SafetySetting for a given SafetyCategory provided in the list, the API will use the default safety setting for that category. Supported harm categories: HARM_CATEGORY_HATE_SPEECH, HARM_CATEGORY_SEXUALLY_EXPLICIT, HARM_CATEGORY_DANGEROUS_CONTENT, HARM_CATEGORY_HARASSMENT.
systemInstructionobjectOptional. Developer set system instruction. Currently, text only.
generationConfigobjectOptional. Configuration options for model generation and outputs.
cachedContentstringOptional. The name of the cached content used as context to serve the prediction. Note: only used in explicit caching, where users can have control over caching (e.g., what content to cache) and enjoy guaranteed cost savings. Format: cachedContents/{cachedContent}.

Example Request

import google.generativeai as genai
from google.api_core.client_options import ClientOptions

genai.configure(
api_key='$ROCKAPI_API_KEY',
transport='rest',
client_options=ClientOptions(api_endpoint='https://api.rockapi.ru/google-ai-studio')
)

model = genai.GenerativeModel("gemini-1.5-flash")
response = model.generate_content("Write a story about a magic backpack.", stream=True)
for chunk in response:
print(chunk.text)
print("_" * 80)

Response Body

If successful, the response body contains a stream of GenerateContentResponse instances.


GenerateContentResponse

Response from the model supporting multiple candidates.

JSON Representation

Note on safety ratings and content filtering. They are reported for both prompt in GenerateContentResponse.prompt_feedback and for each candidate in finishReason and in safetyRatings. The API contract is that: - either all requested candidates are returned or no candidates at all - no candidates are returned only if there was something wrong with the prompt (see promptFeedback) - feedback on each candidate is reported on finishReason and safetyRatings.

{
"candidates": [
{
object (Candidate)
}
],
"promptFeedback": {
object (PromptFeedback)
},
"usageMetadata": {
object (UsageMetadata)
}
}

Fields

NameTypeDescription
candidates[]objectCandidate responses from the model.
promptFeedbackobjectReturns the prompt's feedback related to the content filters.
usageMetadataobjectMetadata on the generation requests' token usage.

PromptFeedback

A set of the feedback metadata the prompt specified in GenerateContentRequest.content.

JSON Representation

{
"blockReason": "enum (BlockReason)",
"safetyRatings": [
{
"object": "SafetyRating"
}
]
}

Fields

NameTypeDescription
blockReasonenumOptional. If set, the prompt was blocked and no candidates are returned. Rephrase your prompt.
safetyRatings[]objectRatings for safety of the prompt. There is at most one rating per category.

BlockReason

Specifies what was the reason why the prompt was blocked.

Enums

NameDescription
BLOCK_REASON_UNSPECIFIEDDefault value. This value is unused.
SAFETYPrompt was blocked due to safety reasons. You can inspect safetyRatings to understand which safety category blocked it.
OTHERPrompt was blocked due to unknown reasons.

UsageMetadata

Metadata on the generation request's token usage.

JSON Representation

{
"promptTokenCount": "integer",
"cachedContentTokenCount": "integer",
"candidatesTokenCount": "integer",
"totalTokenCount": "integer"
}

Fields

NameTypeDescription
promptTokenCountintegerNumber of tokens in the prompt. When cachedContent is set, this is still the total effective prompt size. I.e., this includes the number of tokens in the cached content.
cachedContentTokenCountintegerNumber of tokens in the cached part of the prompt, i.e., in the cached content.
candidatesTokenCountintegerTotal number of tokens across the generated candidates.
totalTokenCountintegerTotal token count for the generation request (prompt + candidates).

Candidate

A response candidate generated from the model.

JSON Representation

{
"content": {
"object": "Content"
},
"finishReason": "enum (FinishReason)",
"safetyRatings": [
{
"object": "SafetyRating"
}
],
"citationMetadata": {
"object": "CitationMetadata"
},
"tokenCount": "integer",
"groundingAttributions": [
{
"object": "GroundingAttribution"
}
],
"index": "integer"
}

Fields

NameTypeDescription
contentobjectGenerated content returned from the model.
finishReasonenumOptional. The reason why the model stopped generating tokens.
safetyRatings[]objectList of ratings for the safety of a response candidate. There is at most one rating per category.
citationMetadataobjectCitation information for model-generated candidate.
tokenCountintegerToken count for this candidate.
groundingAttributions[]objectAttribution information for sources that contributed to a grounded answer.
indexintegerIndex of the candidate in the list of candidates.

FinishReason

Defines the reason why the model stopped generating tokens.

Enums

NameDescription
FINISH_REASON_UNSPECIFIEDDefault value. This value is unused.
STOPNatural stop point of the model or provided stop sequence.
MAX_TOKENSThe maximum number of tokens as specified in the request was reached.
SAFETYThe candidate content was flagged for safety reasons.
RECITATIONThe candidate content was flagged for recitation reasons.
LANGUAGEThe candidate content was flagged for using an unsupported language.
OTHERUnknown reason.

GroundingAttribution

Attribution for a source that contributed to an answer.

JSON Representation

{
"sourceId": {
"object": "AttributionSourceId"
},
"content": {
"object": "Content"
}
}

Fields

NameTypeDescription
sourceIdobjectIdentifier for the source contributing to this attribution.
contentobjectGrounding source content that makes up this attribution.

AttributionSourceId

Identifier for the source contributing to this attribution.

JSON Representation

{
"groundingPassage": {
"object": "GroundingPassageId"
},
"semanticRetrieverChunk": {
"object": "SemanticRetrieverChunk"
}
}

Fields

NameTypeDescription
groundingPassageobjectIdentifier for an inline passage.
semanticRetrieverChunkobjectIdentifier for a Chunk fetched via Semantic Retriever.

GroundingPassageId

Identifier for a part within a GroundingPassage.

JSON Representation

{
"passageId": "string",
"partIndex": "integer"
}

Fields

NameTypeDescription
passageIdstringID of the passage matching the GenerateAnswerRequest's GroundingPassage.id.
partIndexintegerIndex of the part within the GenerateAnswerRequest's GroundingPassage.content.

SemanticRetrieverChunk

Identifier for a Chunk retrieved via Semantic Retriever specified in the GenerateAnswerRequest using SemanticRetrieverConfig.

JSON Representation

{
"source": "string",
"chunk": "string"
}

Fields

NameTypeDescription
sourcestringName of the source matching the request's SemanticRetrieverConfig.source. Example: corpora/123 or corpora/123/documents/abc.
chunkstringName of the Chunk containing the attributed text. Example: corpora/123/documents/abc/chunks/xyz.

CitationMetadata

A collection of source attributions for a piece of content.

JSON Representation

{
"citationSources": [
{
"object": "CitationSource"
}
]
}

Fields

NameTypeDescription
citationSources[]objectCitations to sources for a specific response.

CitationSource

A citation to a source for a portion of a specific response.

JSON Representation

{
"startIndex": "integer",
"endIndex": "integer",
"uri": "string",
"license": "string"
}

Fields

NameTypeDescription
startIndexintegerOptional. Start of the segment of the response that is attributed to this source. Index indicates the start of the segment, measured in bytes.
endIndexintegerOptional. End of the attributed segment, exclusive.
uristringOptional. URI that is attributed as a source for a portion of the text.
licensestringOptional. License for the GitHub project that is attributed as a source for the segment. License info is required for code citations.

GenerationConfig

Configuration options for model generation and outputs. Not all parameters may be configurable for every model.

JSON Representation

{
"stopSequences": [
"string"
],
"responseMimeType": "string",
"responseSchema": {
"object": "Schema"
},
"candidateCount": "integer",
"maxOutputTokens": "integer",
"temperature": "number",
"topP": "number",
"topK": "integer"
}

Fields

NameTypeDescription
stopSequences[]stringOptional. The set of character sequences (up to 5) that will stop output generation. If specified, the API will stop at the first appearance of a stop sequence. The stop sequence will not be included as part of the response.
responseMimeTypestringOptional. Output response MIME type of the generated candidate text. Supported MIME types: text/plain (default), application/json (JSON response in the candidates).
responseSchemaobjectOptional. Output response schema of the generated candidate text when response MIME type can have schema. Schema can be objects, primitives, or arrays and is a subset of OpenAPI schema. If set, a compatible responseMimeType must also be set. Compatible MIME types: application/json (Schema for JSON response).
candidateCountintegerOptional. Number of generated responses to return. Currently, this value can only be set to 1. If unset, this will default to 1.
maxOutputTokensintegerOptional. The maximum number of tokens to include in a candidate. Note: The default value varies by model, see the Model.output_token_limit attribute of the Model returned from the getModel function.
temperaturenumberOptional. Controls the randomness of the output. Note: The default value varies by model, see the Model.temperature attribute of the Model returned from the getModel function. Values can range from [0.0, 2.0].
topPnumberOptional. The maximum cumulative probability of tokens to consider when sampling. The model uses combined Top-k and nucleus sampling. Tokens are sorted based on their assigned probabilities so that only the most likely tokens are considered. Top-k sampling directly limits the maximum number of tokens to consider, while Nucleus sampling limits the number of tokens based on the cumulative probability. Note: The default value varies by model, see the Model.top_p attribute of the Model returned from the getModel function.
topKintegerOptional. The maximum number of tokens to consider when sampling. Models use nucleus sampling or combined Top-k and nucleus sampling. Top-k sampling considers the set of topK most probable tokens. Models running with nucleus sampling don't allow topK setting. Note: The default value varies by model, see the Model.top_k attribute of the Model returned from the getModel function. An empty topK field in Model indicates the model doesn't apply top-k sampling and doesn't allow setting topK on requests.

HarmCategory

The category of a rating. These categories cover various kinds of harms that developers may wish to adjust.

Enums

NameDescription
HARM_CATEGORY_UNSPECIFIEDCategory is unspecified.
HARM_CATEGORY_DEROGATORYNegative or harmful comments targeting identity and/or protected attribute.
HARM_CATEGORY_TOXICITYContent that is rude, disrespectful, or profane.
HARM_CATEGORY_VIOLENCEDescribes scenarios depicting violence against an individual or group, or general descriptions of gore.
HARM_CATEGORY_SEXUALContains references to sexual acts or other lewd content.
HARM_CATEGORY_MEDICALPromotes unchecked medical advice.
HARM_CATEGORY_DANGEROUSDangerous content that promotes, facilitates, or encourages harmful acts.
HARM_CATEGORY_HARASSMENTHarassment content.
HARM_CATEGORY_HATE_SPEECHHate speech and content.
HARM_CATEGORY_SEXUALLY_EXPLICITSexually explicit content.
HARM_CATEGORY_DANGEROUS_CONTENTDangerous content.

SafetyRating

Safety rating for a piece of content. The safety rating contains the category of harm and the harm probability level in that category for a piece of content. Content is classified

for safety across a number of harm categories and the probability of the harm classification is included here.

JSON Representation

{
"category": "enum (HarmCategory)",
"probability": "enum (HarmProbability)",
"blocked": "boolean"
}

Fields

NameTypeDescription
categoryenumRequired. The category for this rating.
probabilityenumRequired. The probability of harm for this content.
blockedbooleanWas this content blocked because of this rating?

HarmProbability

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

Enums

NameDescription
HARM_PROBABILITY_UNSPECIFIEDProbability is unspecified.
NEGLIGIBLEContent has a negligible chance of being unsafe.
LOWContent has a low chance of being unsafe.
MEDIUMContent has a medium chance of being unsafe.
HIGHContent has a high chance of being unsafe.

SafetySetting

Safety setting, affecting the safety-blocking behavior. Passing a safety setting for a category changes the allowed probability that content is blocked.

JSON Representation

{
"category": "enum (HarmCategory)",
"threshold": "enum (HarmBlockThreshold)"
}

Fields

NameTypeDescription
categoryenumRequired. The category for this setting.
thresholdenumRequired. Controls the probability threshold at which harm is blocked.

HarmBlockThreshold

Block at and beyond a specified harm probability.

Enums

NameDescription
HARM_BLOCK_THRESHOLD_UNSPECIFIEDThreshold is unspecified.
BLOCK_LOW_AND_ABOVEContent with NEGLIGIBLE will be allowed.
BLOCK_MEDIUM_AND_ABOVEContent with NEGLIGIBLE and LOW will be allowed.
BLOCK_ONLY_HIGHContent with NEGLIGIBLE, LOW, and MEDIUM will be allowed.
BLOCK_NONEAll content will be allowed.