Skip to content

Chat Completion (Gemini native API)

1. Overview

A multimodal model family from Google that can process text, images, audio, video, and code.

2. Request

  • Method:POST
  • Endpoint:

    https://gateway.serevixai.ai/v1/v1beta/models/{model}:generateContent
    
  • Streaming endpoint:

    https://gateway.serevixai.ai/v1/v1beta/models/{model}:streamGenerateContent
    

3. Parameters

3.1 Header Parameters

Parameter Type Required Description Example
Content-Type string Yes Sets the request content type. It must be application/json application/json
Accept string Yes Sets the response content type. The recommended value is application/json application/json
x-goog-api-key string Yes API key required for authentication, in the format $YOUR_API_KEY. $YOUR_API_KEY

3.2 Body Parameters (application/json)

Parameter Type Required Description Example
contents array Yes The content exchanged with the model. For a single request, provide one item. For multi-turn conversations, include the prior history together with the latest request. [{"role":"user","parts":[{"text":"A cute baby sea otter"}]}]
content.role string Yes Message role. Must be user or model. user
content.parts array No Ordered parts that make up a single message. Parts can use different MIME types. [{"text":"A cute baby sea otter"}]}]
content.parts.text string No Inline text content. A cute baby sea otter
content.parts.inlineData struct No Inline media bytes.
content.parts.inlineData.mimeType string Yes The IANA standard MIME type of the source data. image/png
content.parts.inlineData.data string Yes Raw bytes of the media payload, encoded as a base64 string.
generationConfig struct No Configuration options for generation and output.

4. Request Examples

4.1 Chat

POST /v1/v1beta/models/gemini-2.5-flash:generateContent
Content-Type: application/json
Accept: application/json
x-goog-api-key: $YOUR_API_KEY

{
    "contents": [{
        "role": "user",
        "parts": [{
            "text": "Hello, can you explain quantum mechanics to me?"
        }]
    }],
    "generationConfig": {
        "temperature": 0.7,
        "maxOutputTokens": 1024
    }
}
curl https://gateway.serevixai.ai/v1/v1beta/models/gemini-2.5-flash:generateContent \
    -H "Content-Type: application/json" \
    -H "Accept: application/json" \
    -H "x-goog-api-key: $YOUR_API_KEY" \
    -d "{
    \"contents\": [{
        \"role\": \"user\",
        \"parts\": [{
            \"text\": \"Hello, can you explain quantum mechanics to me?\"
        }]
    }],
    \"generationConfig\": {
        \"temperature\": 0.7,
        \"maxOutputTokens\": 1024
    }
}"
package main

import (
    "context"
    "fmt"

    "google.golang.org/genai"
)

func main() {

    apiKey := "sk-123456789012345678901234567890123456789012345678"

    client, err := genai.NewClient(
        context.Background(),
        &genai.ClientConfig{
            APIKey:  apiKey,
            Backend: genai.BackendGeminiAPI,
            HTTPOptions: genai.HTTPOptions{
                BaseURL: "https://gateway.serevixai.ai",
            },
        })
    if err != nil {
        fmt.Println("error creating client:", err)
        return
    }

    resp, err := client.Models.GenerateContent(
        context.Background(),
        "gemini-2.5-flash",
        []*genai.Content{
            {
                Role: "user",
                Parts: []*genai.Part{
                    {Text: "Hello, can you explain quantum mechanics to me?"},
                },
            },
        },
        &genai.GenerateContentConfig{
            Temperature:     genai.Ptr(float32(0.7)),
            MaxOutputTokens: 1024,
        },
    )
    if err != nil {
        fmt.Println("error:", err)
        return
    }

    if len(resp.Candidates) > 0 && len(resp.Candidates[0].Content.Parts) > 0 {
        fmt.Println("πŸ’¬ Assistant reply:")
        for _, part := range resp.Candidates[0].Content.Parts {
            if part.Text != "" {
                fmt.Println(part.Text)
            }
        }
    }

    if resp.UsageMetadata != nil {
        fmt.Println("\nπŸ“Š Token usage:")
        fmt.Printf("  - Prompt tokens: %d\n", resp.UsageMetadata.PromptTokenCount)
        fmt.Printf("  - Completion tokens: %d\n", resp.UsageMetadata.CandidatesTokenCount)
        fmt.Printf("  - Total tokens: %d\n", resp.UsageMetadata.TotalTokenCount)
    }
}
#!/usr/bin/env python3

from google import genai

def main():
    api_key = "sk-123456789012345678901234567890123456789012345678"

    client = genai.Client(
        api_key=api_key,
        http_options={
            "base_url": "https://gateway.serevixai.ai"
        }
    )

    response = client.models.generate_content(
        model="gemini-2.5-flash",
        contents="Hello, can you explain quantum mechanics to me?",
        config=genai.types.GenerateContentConfig(
            temperature=0.7,
            max_output_tokens=1024
        )
    )

    print("πŸ’¬ Assistant reply:")
    print(response.text)

    if response.usage_metadata:
        print("\nπŸ“Š Token usage:")
        print(f"  - Prompt tokens: {response.usage_metadata.prompt_token_count}")
        print(f"  - Completion tokens: {response.usage_metadata.candidates_token_count}")
        print(f"  - Total tokens: {response.usage_metadata.total_token_count}")

if __name__ == "__main__":
    main()

5. Response Example

{
  "candidates": [
    {
      "content": {
        "role": "model",
        "parts": [
          {
            "text": "Quantum mechanics is the branch of physics that studies the microscopic world..."
          }
        ]
      },
      "finishReason": "MAX_TOKENS",
      "avgLogprobs": -2.1121876037198732
    }
  ],
  "usageMetadata": {
    "promptTokenCount": 5,
    "candidatesTokenCount": 153,
    "totalTokenCount": 1027,
    "trafficType": "ON_DEMAND",
    "promptTokensDetails": [
      {
        "modality": "TEXT",
        "tokenCount": 5
      }
    ],
    "candidatesTokensDetails": [
      {
        "modality": "TEXT",
        "tokenCount": 153
      }
    ],
    "thoughtsTokenCount": 869
  },
  "modelVersion": "gemini-2.5-flash",
  "createTime": "2025-12-11T10:01:58.402576Z",
  "responseId": "lpY6aZDJGOTCgeAP1J-e4Qg"
}