Security News
Research
Data Theft Repackaged: A Case Study in Malicious Wrapper Packages on npm
The Socket Research Team breaks down a malicious wrapper package that uses obfuscation to harvest credentials and exfiltrate sensitive data.
This guide explains how to seamlessly integrate the powerful Gemini AI API into your Ruby projects. Utilize Gemini's cutting-edge language capabilities for generating text, translating languages, and more.
Add this line to your application's Gemfile:
gem "ruby-gemini-ai"
And then execute:
$ bundle install
Or install with:
$ gem install ruby-gemini-ai
and require with:
require "gemini-ai"
Obtain an API Key from your Google Cloud project: Google Cloud through the Google Cloud Console: https://console.cloud.google.com/apis/credentials.
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, which will automatically create a project for you.
For the Vertex AI API, create a Google Cloud Project and a Service Account. Enable the [Vertex AI] (https://cloud.google.com/vertex-ai) API for your project here.
Generate credentials for your Service Account here and download a JSON file named google-credentials.json.
{
"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/..."
}
Ensure the necessary policies (roles/aiplatform.user
and possibly roles/ml.admin
) are in place 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:YOUR@MAIL.COM' \
--role='roles/aiplatform.user'
gcloud projects add-iam-policy-binding PROJECT_ID \
--member='user:YOUR@MAIL.COM' \
--role='roles/ml.admin'
Similar to Option 2, but you don't need to download a google-credentials.json
. These automatically find credentials based on your environment. Application Default Credentials.
Generate them using the gcloud CLI before local development. gcloud CLI:
gcloud auth application-default login
For more details about alternative methods and different environments, check the official documentation: Set up Application Default Credentials
For a quick test you can pass your token directly to a new client:
client = GeminiAi::Client.new(api_key: "gemini_api_key")
We can configure Gemini with Ruby using three options.
Option 1, API KEY
For a more robust setup, you can configure the gem with your API keys, for example in an gemini.rb
initializer file. Never hardcode secrets into your codebase - instead use something like dotenv to pass the keys safely into your environments.
GeminiAi.configure do |config|
config.api_key = ENV.fetch("GEMINI_API_KEY")
config.service = ENV.fetch("GEMINI_API_SERVICE")
end
Option 2, Service Account
For the Service Account, provide a google-credentials.json
file and a REGION
:
GeminiAi.configure do |config|
config.service = 'vertex-ai-api'
config.region = 'us-east4'
config.file_path = 'google-credentials.json'
end
Option 3, Default Credentials
For Application Default Credentials, omit both the api_key
and the file_path
:
GeminiAi.configure do |config|
config.region = 'us-east4'
config.service = 'vertex-ai-api'
end
Then you can create a client like this:
client = GeminiAi::Client.new
client = GeminiAi::Client.new
# Assuming you configured with your API key or credentials
contents = {
contents: {
role: 'user',
parts: {
text: 'Write a poem about the ocean.'
}
}
}
stream = client.stream_generate_content(contents, model: 'gemini-pro')
In this case, the result will be an array with all the received events:
[{ 'candidates' =>
[{ 'content' => {
'role' => 'model',
'parts' => [{ 'text' => 'exmaple poem content.......' }]
},
'finishReason' => 'STOP',
'safetyRatings' =>
[{ 'category' => 'HARM_CATEGORY_HARASSMENT', 'probability' => 'NEGLIGIBLE' },
{ 'category' => 'HARM_CATEGORY_HATE_SPEECH', 'probability' => 'NEGLIGIBLE' },
{ 'category' => 'HARM_CATEGORY_SEXUALLY_EXPLICIT', 'probability' => 'NEGLIGIBLE' },
{ 'category' => 'HARM_CATEGORY_DANGEROUS_CONTENT', 'probability' => 'NEGLIGIBLE' }] }],
'usageMetadata' => {
'promptTokenCount' => 2,
'candidatesTokenCount' => 8,
'totalTokenCount' => 10
} }]
client = GeminiAi::Client.new
# Assuming you configured with your API key or credentials
contents = {
contents: {
role: 'user',
parts: {
text: 'Write a poem about the ocean.'
}
}
}
client.stream_generate_content(contents, model: 'gemini-pro', stream: true) do |part_text, event, parsed, raw|
puts text
end
OR
client = GeminiAi::Client.new
# Assuming you configured with your API key or credentials
contents = {
contents: {
role: 'user',
parts: {
text: 'Write a poem about the ocean.'
}
}
}
# Assuming you have a block or procedure (proc) defined
stream_proc = Proc.new do |part_text, event, parsed, raw|
puts part_text
end
client.stream_generate_content(contents, model: 'gemini-pro', stream: true, &stream_proc)
In this case, the result will be an array with all the received events:
'exmaple poem content.......'
result = client.generate_content(
{ contents: { role: 'user', parts: { text: 'hi!' } } }, model: 'gemini-pro'
)
client = GeminiAi::Client.new
# Assuming you configured with your API key or credentials
contents = {
contents: {
role: 'user',
parts: {
text: 'Write a poem about the ocean.'
}
}
}
stream = client.generate_content(contents, model: 'gemini-pro')
Result:
{ 'candidates' =>
[{ 'content' => { 'parts' => [{ 'text' => 'exampled poem.......' }], 'role' => 'model' },
'finishReason' => 'STOP',
'index' => 0,
'safetyRatings' =>
[{ 'category' => 'HARM_CATEGORY_SEXUALLY_EXPLICIT', 'probability' => 'NEGLIGIBLE' },
{ 'category' => 'HARM_CATEGORY_HATE_SPEECH', 'probability' => 'NEGLIGIBLE' },
{ 'category' => 'HARM_CATEGORY_HARASSMENT', 'probability' => 'NEGLIGIBLE' },
{ 'category' => 'HARM_CATEGORY_DANGEROUS_CONTENT', 'probability' => 'NEGLIGIBLE' }] }],
'promptFeedback' =>
{ 'safetyRatings' =>
[{ 'category' => 'HARM_CATEGORY_SEXUALLY_EXPLICIT', 'probability' => 'NEGLIGIBLE' },
{ 'category' => 'HARM_CATEGORY_HATE_SPEECH', 'probability' => 'NEGLIGIBLE' },
{ 'category' => 'HARM_CATEGORY_HARASSMENT', 'probability' => 'NEGLIGIBLE' },
{ 'category' => 'HARM_CATEGORY_DANGEROUS_CONTENT', 'probability' => 'NEGLIGIBLE' }] } }
You can pass Faraday middleware to the client in a block, eg. to enable verbose logging with Ruby's Logger:
client = GeminiAi::Client.new do |f|
f.response :logger, Logger.new($stdout), bodies: true
end
ruby-gemini-ai gem is compatible with Ruby versions 2.6.7 and higher.
The gem is available as open source under the terms of the MIT License.
Explore the following curated list of resources and references to enhance your understanding throughout the learning process:
These resources collectively provide a comprehensive foundation for your exploration of the Gemini API and Vertex AI services.
vertex-ai-api
service.FAQs
Unknown package
We found that ruby-gemini-ai demonstrated a healthy version release cadence and project activity because the last version was released less than a year ago. It has 1 open source maintainer collaborating on the project.
Did you know?
Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.
Security News
Research
The Socket Research Team breaks down a malicious wrapper package that uses obfuscation to harvest credentials and exfiltrate sensitive data.
Research
Security News
Attackers used a malicious npm package typosquatting a popular ESLint plugin to steal sensitive data, execute commands, and exploit developer systems.
Security News
The Ultralytics' PyPI Package was compromised four times in one weekend through GitHub Actions cache poisoning and failure to rotate previously compromised API tokens.