本文介绍Google Gemini 大模型API SDK,包括最新的Gemini2大模型。

Gemini 2.0 介绍:
https://blog.google/technology/google-deepmind/google-gemini-ai-update-december-2024/

Gemini 2.0 Flash is now available as an experimental preview release through the Gemini Developer API and Google AI Studio. The model introduces new features and enhanced core capabilities:

  • Multimodal Live API: This new API helps you create real-time vision and audio streaming applications with tool use.
  • Speed and performance: Gemini 2.0 has a significantly improved time to first token (TTFT) over 1.5 Flash.
  • Quality: Better performance across most benchmarks than Gemini 1.5 Pro.
  • Improved agentic capabilities: Gemini 2.0 delivers improvements to multimodal understanding, coding, complex instruction following, and function calling.
  • New modalities: Gemini 2.0 introduces native image generation and controllable text-to-speech capabilities.

To provide a better developer experience, we're also shipping a new SDK. For Gemini 2.0 technical details, see Gemini models.

API 文档:
https://ai.google.dev/gemini-api/docs/models/gemini-v2

Curl Request:

curl "https://generativelanguage.googleapis.com/v1beta/models/gemini-2.0-flash-exp:generateContent?key=$YOUR_API_KEY" \
-H 'Content-Type: application/json' \
-X POST \
-d '{
  "contents": [{
    "parts":[{"text": "Explain how AI works"}]
    }]
   }'

Python SDK

# Source: https://blog.zhexuan.org/archives/Gemini-sdk.html

import requests
import json
import datetime, time

class GEMINI:
    _api_url = "https://generativelanguage.googleapis.com"

    def __init__(self, api_key):
        self.api_key = api_key

    # 
    def chat(self, model, text):
        endpoint = f"{self._api_url}/v1beta/models/{model}:generateContent?key={self.api_key}"
        request_payload = {"contents": {"parts": {"text": text}}}
        response = self.http_request_v2(endpoint, method="POST", params=json.dumps(request_payload))
        return response

    # 生成headers头
    def headers(self, params=None):
        headers = {}
        headers['Content-Type'] = 'application/json'
        return headers

    # 
    def http_request_v2(self, url, method="GET", headers=None, params=None):
        headers = {}
        headers['User-Agent'] = 'Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/39.0.2171.71 Safari/537.36'

        max_seconds, max_times = 10, 3
        for attempt in range(max_times):
            try:
                if method == "GET":
                    response = requests.get(url, headers=headers, timeout=max_seconds)
                elif method == "POST":
                    response = requests.post(url, data=params, headers=headers, timeout=max_seconds)
                elif method == "DELETE":
                    response = requests.delete(url, data=params, headers=headers, timeout=max_seconds)

                return response.json()
            
            except Exception as e:
                print([datetime.datetime.now().strftime('%Y-%m-%d %H:%M:%S'), attempt, url, method, params, repr(e)])
                time.sleep(max_seconds * (attempt + 1))

请求方式如下:

    google_api_key = ""
    model_name = "gemini-2.0-flash-exp"  # 这里指定使用的模型

    google_request = GEMINI(api_key=google_api_key)
    prompt_text = "what is AES"  # 你想传入的文本内容
    response = google_request.chat(model=model_name, text=prompt_text)
    print(response)

返回如下:

{
    'candidates': [{
        'content': {
            'parts': [{
                'text': "AES stands for **Advanced Encryption Standard**. It's a symmetric-key block cipher, meaning:\n\n* **Symmetric-key:** The same key is used for both encrypting and decrypting data.\n* **Block cipher:** It encrypts data in fixed-size blocks (128 bits for standard AES).\n\nHere's a breakdown of key concepts about AES:\n\n**Purpose:**\n\n* **Data Security:** AES is designed to securely encrypt sensitive digital data, making it unreadable to unauthorized individuals. It's widely used to protect data in transit (like over the internet) and data at rest (like on hard drives).\n* **Standardization:** It replaced the older Data Encryption Standard (DES) and became the widely adopted standard for encryption in numerous applications.\n\n**Key Features:**\n\n* **Block Size:** AES encrypts data in 128-bit blocks.\n* **Key Sizes:** AES supports three key sizes: 128 bits, 192 bits, and 256 bits. Larger key sizes provide stronger security.\n* **Rounds:** The encryption process involves multiple rounds of transformations, the number of which depends on the key size. More rounds make the encryption more secure.\n    * 128-bit key: 10 rounds\n    * 192-bit key: 12 rounds\n    * 256-bit key: 14 rounds\n* **Transformations:** Each round involves a series of mathematical operations like:\n    * **SubBytes:** A non-linear substitution step.\n    * **ShiftRows:** A cyclical shift of bytes in rows.\n    * **MixColumns:** A mixing operation on the columns of the state matrix.\n    * **AddRoundKey:** XORing the state with a round key derived from the main key.\n\n**How it Works (Simplified):**\n\n1. **Key Expansion:** The original key is expanded into a series of round keys, one for each round.\n2. **Initial Round Key Addition:** The data block is XORed with the first round key.\n3. **Rounds of Transformation:** The data block undergoes the four transformations (SubBytes, ShiftRows, MixColumns, and AddRoundKey) repeatedly for the specified number of rounds.\n4. **Final Round:** The last round excludes the MixColumns transformation.\n5. **Output:** The final result is the encrypted data block.\n\n**Decryption:**\n\nDecryption is essentially the reverse of the encryption process, using the same key and applying the inverse transformations in reverse order.\n\n**Why AES is Secure:**\n\n* **Mathematical Foundation:** AES is based on strong mathematical principles and is resistant to known cryptanalytic attacks.\n* **Key Size:** The various key sizes provide flexibility in balancing security and performance requirements. Longer keys offer higher security but can be slower.\n* **Rounds and Transformations:** The multiple rounds and intricate transformations make it computationally very expensive for an attacker to try all possible keys or reverse the encryption process.\n\n**Common Uses:**\n\n* **SSL/TLS:** Securing web traffic over HTTPS.\n* **VPNs:** Creating secure tunnels for network connections.\n* **File Encryption:** Protecting sensitive files stored on computers and devices.\n* **Database Encryption:** Securing sensitive data stored in databases.\n* **Wireless Security:** Protecting Wi-Fi networks using WPA2/3 protocols.\n* **Secure Communication Protocols:** Securing messages in various communication systems.\n\n**In Summary:**\n\nAES is a highly robust and widely adopted encryption algorithm that is crucial for maintaining data security in the digital world. Its combination of strong mathematical foundations, variable key sizes, and iterative transformation process makes it a reliable choice for protecting sensitive information.\n"
            }],
            'role': 'model'
        },
        'finishReason': 'STOP',
        'safetyRatings': [{
            'category': 'HARM_CATEGORY_HATE_SPEECH',
            'probability': 'NEGLIGIBLE'
        }, {
            'category': 'HARM_CATEGORY_DANGEROUS_CONTENT',
            'probability': 'NEGLIGIBLE'
        }, {
            'category': 'HARM_CATEGORY_HARASSMENT',
            'probability': 'NEGLIGIBLE'
        }, {
            'category': 'HARM_CATEGORY_SEXUALLY_EXPLICIT',
            'probability': 'NEGLIGIBLE'
        }],
        'citationMetadata': {
            'citationSources': [{
                'startIndex': 115,
                'endIndex': 244,
                'uri': 'https://github.com/h4ckd0tm3/project-athena'
            }]
        },
        'avgLogprobs': -0.3793827484013031
    }],
    'usageMetadata': {
        'promptTokenCount': 3,
        'candidatesTokenCount': 777,
        'totalTokenCount': 780
    },
    'modelVersion': 'gemini-2.0-flash-exp'
}