通过Vertex AI,Generative Language API,或AI Studio,谷歌的Google基因AI服务与Gemini互动的Ruby Gem.
通过Vertex AI,Generative Language API,或AI Studio,谷歌的Google基因AI服务与Gemini互动的Ruby Gem.
A Ruby Gem for interacting with Gemini through Vertex AI, Generative Language API, or AI Studio, Google's generative AI services.
This Gem is designed to provide low-level access to Gemini, enabling people to build abstractions on top of it. If you are interested in more high-level abstractions or more user-friendly tools, you may want to consider Nano Bots .
gem 'gemini-ai', '~> 4.3.0'…Google now supports a global endpoint:
Gemini.new(credentials: { region: 'global' })Result:
…gem install gemini-ai -v 4.3.0gem 'gemini-ai', '~> 4.3.0'⚠️ DISCLAIMER: Be careful with what you are doing, and never trust others' code related to this. These commands and instructions alter the level of access to your Google Cloud Account, and running them naively can lead to security risks as well as financial risks. People with access to your account can use it to steal data or incur charges. Run these commands at your own responsibility and due diligence; expect no warranties from the contributors of this project.
You need a Google Cloud Project, and then you can generate an API Key through the Google Cloud Console here.
You also need to enable the Generative Language API service in your Google Cloud Console, which can be done here.
Alternatively, you can generate an API Key through Google AI Studio here. However, this approach will automatically create a project for you in your Google Cloud Account.
You need a Google Cloud Project and a Service Account to use Vertex AI API.
After creating them, you need to enable the Vertex AI API for your project by clicking Enable here: Vertex AI API.
You can create credentials for your Service Account here, where you will be able to download a JSON file named google-credentials.json that should have content similar to this:
{
"type": "service_account",
"project_id": "YOUR_PROJECT_ID",
"private_key_id": "a00...",
"private_key": "-----BEGIN PRIVATE KEY-----\n...\n-----END PRIVATE KEY-----\n",
"client_email": "PROJECT_ID@PROJECT_ID.iam.gserviceaccount.com",
"client_id": "000...",
"auth_uri": "https://accounts.google.com/o/oauth2/auth",
"token_uri": "https://oauth2.googleapis.com/token",
"auth_provider_x509_cert_url": "https://www.googleapis.com/oauth2/v1/certs",
"client_x509_cert_url": "https://www.googleapis.com/robot/v1/metadata/x509/..."
}You need to have the necessary policies (roles/aiplatform.user and possibly roles/ml.admin) in place to use the Vertex AI API.
You can add them by navigating to the IAM Console and clicking on the "Edit principal" (✏️ pencil icon) next to your Service Account.
Alternatively, you can add them through the gcloud CLI as follows:
gcloud projects add-iam-policy-binding PROJECT_ID \
--member='serviceAccount:PROJECT_ID@PROJECT_ID.iam.gserviceaccount.com' \
--role='roles/aiplatform.user'Some people reported having trouble accessing the API, and adding the role roles/ml.admin fixed it:
gcloud projects add-iam-policy-binding PROJECT_ID \
--member='serviceAccount:PROJECT_ID@PROJECT_ID.iam.gserviceaccount.com' \
--role='roles/ml.admin'If you are not using a Service Account:
gcloud projects add-iam-policy-binding PROJECT_ID \
--member='user:[email protected]' \
--role='roles/aiplatform.user'
gcloud projects add-iam-policy-binding PROJECT_ID \
--member='user:[email protected]' \
--role='roles/ml.admin'Similar to Option 2, but you don't need to download a google-credentials.json. Application Default Credentials automatically find credentials based on the application environment.
For local development, you can generate your default credentials using the gcloud CLI as follows:
gcloud auth application-default loginFor more details about alternative methods and different environments, check the official documentation: Set up Application Default Credentials
After choosing an option, you should have all the necessary data and access to use Gemini.
Option 1, for API Key:
{
service: 'generative-language-api',
api_key: 'GOOGLE_API_KEY'
}Remember that hardcoding your API key in code is unsafe; it's preferable to use environment variables:
{
service: 'generative-language-api',
api_key: ENV['GOOGLE_API_KEY']
}Option 2: For the Service Account, provide a google-credentials.json file and a REGION:
{
service: 'vertex-ai-api',
file_path: 'google-credentials.json',
region: 'us-east4'
}Alternatively, you can pass the file contents instead of the path:
{
service: 'vertex-ai-api',
file_contents: File.read('google-credentials.json'),
region: 'us-east4'
}{
service: 'vertex-ai-api',
file_contents: ENV['GOOGLE_CREDENTIALS_FILE_CONTENTS'],
region: 'us-east4'
}Option 3: For Application Default Credentials, omit both the api_key and the file_path:
{
service: 'vertex-ai-api',
region: 'us-east4'
}As of the writing of this README, the following regions support Gemini:
Iowa (us-central1)
Las Vegas, Nevada (us-west4)
Montréal, Canada (northamerica-northeast1)
Northern Virginia (us-east4)
Oregon (us-west1)
Seoul, Korea (asia-northeast3)
Singapore (asia-southeast1)
Tokyo, Japan (asia-northeast1)You can follow here if new regions are available: Gemini API
You might want to explicitly set a Google Cloud Project ID, which you can do as follows:
{
service: 'vertex-ai-api',
project_id: 'PROJECT_ID'
}By default, the gem uses the v1 version of the APIs. You may want to use a different version:
…Google now supports a global endpoint:
Gemini.new(credentials: { region: 'global' })These models are accessible to the repository author as of June 2025 in the us-east4 region. Access to models may vary by region, user, and account. All models here are expected to work, if you can access them. This is just a reference of what a "typical" user may expect to have access to right away:
| Model | Vertex AI | Generative Language |
|---|---|---|
| gemini-pro-vision | ✅ | |
| gemini-pro | ✅ | ✅ |
| gemini-1.5-pro-preview-0514 | ✅ | |
| gemini-1.5-pro-preview-0409 | ✅ | |
| gemini-1.5-pro | ✅ | ✅ |
| gemini-1.5-flash-preview-0514 | ✅ | |
| gemini-1.5-flash | ✅ | ✅ |
| gemini-1.0-pro-vision-latest | ||
| gemini-1.0-pro-vision-001 | ✅ | |
| gemini-1.0-pro-vision | ✅ | |
| gemini-1.0-pro-latest | ✅ | |
| gemini-1.0-pro-002 | ✅ | |
| gemini-1.0-pro-001 | ✅ | ✅ |
| gemini-1.0-pro | ✅ | ✅ |
| gemini-ultra | ||
| gemini-1.0-ultra | ||
| gemini-1.0-ultra-001 |
暂无开放 Issues,或尚未同步最近议题。