Skip to content

Gemini

ChatGoogleGenerativeAI

Pricing

60 QPM (queries per minute)

Details: https://ai.google.dev/pricing

Set up API Key

  1. Set project id

    PROJECT=xxx
    KEY_ID=gemini-api-key
    

  2. Enable service generativelanguage.googleapis.com

    gcloud services list --available --project $PROJECT --filter 'generativelanguage'
    NAME                               TITLE
    generativelanguage.googleapis.com  Generative Language API
    
    gcloud services enable generativelanguage.googleapis.com --project $PROJECT
    
  3. Set alias (ref)

    alias gcurl='curl -H "Authorization: Bearer $(gcloud auth print-access-token)" -H "Content-Type: application/json"'
    
  4. Create API key with API target restriction.

    DISPLAY_NAME="Gemini API Key"
    gcloud services api-keys create --display-name "$DISPLAY_NAME" --project $PROJECT --api-target=service=generativelanguage.googleapis.com
    
  5. Check API Key

    gcloud services api-keys list --project $PROJECT --format json | jq ".[] | select(.displayName == \"$DISPLAY_NAME\")"
    {
      "createTime": "2024-02-23T12:58:04.334290Z",
      "displayName": "Gemini API Key",
      "etag": "W/\"dMMHgHoJFaTkh6z89v139Q==\"",
      "name": "projects/xxxxx/locations/global/keys/xxxxxxx",
      "restrictions": {
        "apiTargets": [
          {
            "service": "generativelanguage.googleapis.com"
          }
        ]
      },
      "uid": "xxxxxxxxxxxxxxx",
      "updateTime": "2024-02-23T12:58:04.358444Z"
    }
    

  6. Get Key string

    gcloud services api-keys get-key-string projects/xxxxxxx/locations/global/keys/xxxxxxxxxxxxxxxx --project $PROJECT
    keyString: XXXXXXXXXXXXXXXXXXXXX
    

    Set this value in .env

Ref

  1. https://cloud.google.com/vertex-ai/docs/generative-ai/model-reference/gemini
  2. https://cloud.google.com/api-keys/docs/add-restrictions-api-keys
  3. https://ai.google.dev/pricing
  4. https://makersuite.google.com/app/
  5. [langchain] ChatGoogleGenerativeAI
  6. https://qiita.com/nakamasato/items/b8d76da6751df8d7bddc