Module: Geminize::RequestBuilder

Defined in:
lib/geminize/request_builder.rb

Overview

Utility module for building requests to the Gemini API

Class Method Summary collapse

Class Method Details

.build_chat_request(chat_request, message_history = []) ⇒ Hash

Build a chat request for the Gemini API

Parameters:

  • chat_request (Geminize::Models::ChatRequest) —

    The chat request

  • message_history (Array<Hash>) (defaults to: []) —

    The message history from the conversation

Returns:

  • (Hash) —

    The complete request hash ready to send to the API



24
25
26
27
28
29
30
31
32
# File 'lib/geminize/request_builder.rb', line 24

def build_chat_request(chat_request, message_history = [])
  model_name = chat_request.model_name
  Validators.validate_not_empty!(model_name, "Model name")

  {
    model: model_name,
    **chat_request.to_hash(message_history)
  }
end

.build_embedding_endpoint(model_name) ⇒ String

Build the endpoint for embedding generation

Parameters:

  • model_name (String) —

    The name of the model

Returns:

  • (String) —

    The complete API endpoint path for embedding generation



81
82
83
# File 'lib/geminize/request_builder.rb', line 81

def build_embedding_endpoint(model_name)
  build_model_endpoint(model_name, "embedContent")
end

.build_get_model_endpoint(model_name) ⇒ String

Build a specific model endpoint for the get model info API

Parameters:

  • model_name (String) —

    The model name or ID to get info for

Returns:

  • (String) —

    The complete API endpoint path for getting model info



70
71
72
73
74
75
76
# File 'lib/geminize/request_builder.rb', line 70

def build_get_model_endpoint(model_name)
  # Handle both formats: "models/gemini-2.0-flash" or just "gemini-2.0-flash"
  unless model_name.start_with?("models/")
    model_name = "models/#{model_name}"
  end
  model_name
end

.build_model_endpoint(model_name, action) ⇒ String

Build a complete API endpoint path for a model

Parameters:

  • model_name (String) —

    The name of the model

  • action (String) —

    The action to perform (e.g., "generateContent")

Returns:

  • (String) —

    The complete API endpoint path



38
39
40
# File 'lib/geminize/request_builder.rb', line 38

def build_model_endpoint(model_name, action)
  "models/#{model_name}:#{action}"
end

.build_models_list_params(page_size = nil, page_token = nil) ⇒ Hash

Build a models list request parameters

Parameters:

  • page_size (Integer, nil) (defaults to: nil) —

    Number of models to return per page

  • page_token (String, nil) (defaults to: nil) —

    Token for retrieving a specific page

Returns:

  • (Hash) —

    The query parameters for models listing



60
61
62
63
64
65
# File 'lib/geminize/request_builder.rb', line 60

def build_models_list_params(page_size = nil, page_token = nil)
  params = {}
  params[:pageSize] = page_size if page_size
  params[:pageToken] = page_token if page_token
  params
end

.build_streaming_endpoint(model_name) ⇒ String

Build the streaming text generation endpoint for a specific model

Parameters:

  • model_name (String) —

    The name of the model

Returns:

  • (String) —

    The complete API endpoint path for streaming text generation



52
53
54
# File 'lib/geminize/request_builder.rb', line 52

def build_streaming_endpoint(model_name)
  build_model_endpoint(model_name, "streamGenerateContent")
end

.build_text_generation_endpoint(model_name) ⇒ String

Build the text generation endpoint for a specific model

Parameters:

  • model_name (String) —

    The name of the model

Returns:

  • (String) —

    The complete API endpoint path for text generation



45
46
47
# File 'lib/geminize/request_builder.rb', line 45

def build_text_generation_endpoint(model_name)
  build_model_endpoint(model_name, "generateContent")
end

.build_text_generation_request(content_request) ⇒ Hash

Build a text generation request for the Gemini API

Parameters:

Returns:

  • (Hash) —

    The complete request hash ready to send to the API



10
11
12
13
14
15
16
17
18
# File 'lib/geminize/request_builder.rb', line 10

def build_text_generation_request(content_request)
  model_name = content_request.model_name
  Validators.validate_not_empty!(model_name, "Model name")

  {
    model: model_name,
    **content_request.to_hash
  }
end