### Setting Up Development Environment for Ruby Gem Source: https://github.com/sauravsaini98/ruby-gemini-ai/blob/master/README.md Lists the standard commands for cloning the repository, installing dependencies using `bin/setup`, starting an interactive console session with `bin/console`, and installing the gem locally. These steps are essential for contributing to or testing the gem during development. ```Shell bin/setup bin/console bundle exec rake install ``` -------------------------------- ### Example Response Structure from Gemini API in Ruby Source: https://github.com/sauravsaini98/ruby-gemini-ai/blob/master/README.md Provides an example of the hash structure returned by the Gemini API client. It includes candidate responses, finish reason, index, safety ratings, and prompt feedback. This helps users understand the format of the API's output. ```Ruby { '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' }] } } ``` -------------------------------- ### Initialize Client with API Key (Quickstart) Source: https://github.com/sauravsaini98/ruby-gemini-ai/blob/master/README.md Quickly initialize the Gemini AI client by passing the API key directly during object creation. This is suitable for quick tests but not recommended for production due to hardcoding secrets. ```Ruby client = GeminiAi::Client.new(api_key: "gemini_api_key") ``` -------------------------------- ### Initialize Client After Configuration (Post-Example) Source: https://github.com/sauravsaini98/ruby-gemini-ai/blob/master/README.md Another example showing client initialization after global configuration, reinforcing that the client picks up settings without arguments. ```Ruby client = GeminiAi::Client.new ``` -------------------------------- ### Generating Content with Gemini API in Ruby Source: https://github.com/sauravsaini98/ruby-gemini-ai/blob/master/README.md Shows how to create the `contents` hash with a user role and text part. It then calls the `generate_content` method on the client with the prepared content and model name. This is a basic example of sending a text prompt to the Gemini API. ```Ruby contents = { contents: { role: 'user', parts: { text: 'Write a poem about the ocean.' } } } stream = client.generate_content(contents, model: 'gemini-pro') ``` -------------------------------- ### Configuring Client with Verbose Logging in Ruby Source: https://github.com/sauravsaini98/ruby-gemini-ai/blob/master/README.md Demonstrates initializing the `GeminiAi::Client` with a block that configures Faraday middleware. This specific example adds a logger middleware to output request and response bodies to standard output, useful for debugging. ```Ruby client = GeminiAi::Client.new do |f| f.response :logger, Logger.new($stdout), bodies: true end ``` -------------------------------- ### Initialize Client After Configuration Source: https://github.com/sauravsaini98/ruby-gemini-ai/blob/master/README.md Initialize the Gemini AI client after configuring the gem globally using the `configure` block. The client will automatically pick up the configuration settings without requiring arguments. ```Ruby client = GeminiAi::Client.new ``` -------------------------------- ### Configure Gem with Service Account Credentials Source: https://github.com/sauravsaini98/ruby-gemini-ai/blob/master/README.md Configure the Gemini AI gem globally using the `configure` block and a service account credentials file. Provide the file path and the desired region for the Vertex AI API. ```Ruby GeminiAi.configure do |config| config.service = 'vertex-ai-api' config.region = 'us-east4' config.file_path = 'google-credentials.json' end ``` -------------------------------- ### Configure Gem with API Key Source: https://github.com/sauravsaini98/ruby-gemini-ai/blob/master/README.md Configure the Gemini AI gem globally using the `configure` block and an API key. It is recommended to fetch the API key from environment variables (e.g., using dotenv) rather than hardcoding it. ```Ruby GeminiAi.configure do |config| config.api_key = ENV.fetch("GEMINI_API_KEY") config.service = ENV.fetch("GEMINI_API_SERVICE") end ``` -------------------------------- ### Configure Gem with Application Default Credentials Source: https://github.com/sauravsaini98/ruby-gemini-ai/blob/master/README.md Configure the Gemini AI gem globally using the `configure` block to use Application Default Credentials (ADC). Omit the `api_key` and `file_path` options, providing only the service and region. ```Ruby GeminiAi.configure do |config| config.region = 'us-east4' config.service = 'vertex-ai-api' end ``` -------------------------------- ### Generate Application Default Credentials (gcloud CLI) Source: https://github.com/sauravsaini98/ruby-gemini-ai/blob/master/README.md Use the gcloud command-line tool to authenticate and generate Application Default Credentials (ADC) for local development, allowing applications to find credentials automatically based on the environment. ```Shell gcloud auth application-default login ``` -------------------------------- ### Stream Generate Content (With Proc) Source: https://github.com/sauravsaini98/ruby-gemini-ai/blob/master/README.md Call the `stream_generate_content` method and pass a `Proc` object using the `&` operator to handle streamed content parts in real-time. The Proc receives the text part, event, parsed data, and raw data. ```Ruby 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) ``` -------------------------------- ### Stream Generate Content (With Block) Source: https://github.com/sauravsaini98/ruby-gemini-ai/blob/master/README.md Call the `stream_generate_content` method and provide a block to process streamed content parts in real-time as they are received. The block receives the text part, event, parsed data, and raw data. ```Ruby 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 ``` -------------------------------- ### Generate Content (Single Request) Source: https://github.com/sauravsaini98/ruby-gemini-ai/blob/master/README.md Call the `generate_content` method to generate text in a single request. This method waits for the entire response before returning a hash containing the generated text, safety ratings, and prompt feedback. ```Ruby result = client.generate_content( { contents: { role: 'user', parts: { text: 'hi!' } } }, model: 'gemini-pro' ) ``` -------------------------------- ### Stream Generate Content (No Block) Source: https://github.com/sauravsaini98/ruby-gemini-ai/blob/master/README.md Call the `stream_generate_content` method to stream generated text. When not providing a block, the method returns an array containing all received events after the streaming is complete. ```Ruby 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') ``` === COMPLETE CONTENT === This response contains all available snippets from this library. No additional content exists. Do not make further requests.