Class: Geminize::Models::EmbeddingRequest

Inherits:
Object
  • Object
show all
Defined in:
lib/geminize/models/embedding_request.rb

Overview

Represents a request for embedding generation from the Gemini API

Constant Summary collapse

TASK_TYPE_UNSPECIFIED =

Task type constants

"TASK_TYPE_UNSPECIFIED"
RETRIEVAL_QUERY =
"RETRIEVAL_QUERY"
RETRIEVAL_DOCUMENT =
"RETRIEVAL_DOCUMENT"
SEMANTIC_SIMILARITY =
"SEMANTIC_SIMILARITY"
CLASSIFICATION =
"CLASSIFICATION"
CLUSTERING =
"CLUSTERING"
QUESTION_ANSWERING =
"QUESTION_ANSWERING"
FACT_VERIFICATION =
"FACT_VERIFICATION"
CODE_RETRIEVAL_QUERY =
"CODE_RETRIEVAL_QUERY"
TASK_TYPES =

Supported task types for embeddings

[
  TASK_TYPE_UNSPECIFIED, # Default unspecified type
  RETRIEVAL_QUERY,       # For embedding queries for retrieval
  RETRIEVAL_DOCUMENT,    # For embedding documents for retrieval
  SEMANTIC_SIMILARITY,   # For embeddings that will be compared for similarity
  CLASSIFICATION,        # For embeddings that will be used for classification
  CLUSTERING,            # For embeddings that will be clustered
  QUESTION_ANSWERING,    # For embeddings used in question answering
  FACT_VERIFICATION,     # For embeddings used in fact verification
  CODE_RETRIEVAL_QUERY   # For embeddings used in code retrieval
].freeze

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(text = nil, model_name = nil, **options) ⇒ EmbeddingRequest

Initialize a new embedding request

Parameters:

  • model_name (String) (defaults to: nil)

    The model name to use

  • text (String, Array<String>) (defaults to: nil)

    The input text(s) to embed

  • task_type (String)

    The embedding task type

  • params (Hash)

    Additional parameters



40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
# File 'lib/geminize/models/embedding_request.rb', line 40

def initialize(text = nil, model_name = nil, **options)
  # Support both old positional params and new named params
  if text.nil? && model_name.nil?
    # New named parameters style
    @model_name = options.delete(:model_name)
    @text = options.delete(:text)
  else
    # Old positional parameters style
    @text = text
    @model_name = model_name
  end

  @task_type = options[:task_type] || RETRIEVAL_DOCUMENT
  @dimensions = options[:dimensions]
  @title = options[:title]

  validate!
end

Instance Attribute Details

#dimensionsObject (readonly)

Returns the value of attribute dimensions.



7
8
9
# File 'lib/geminize/models/embedding_request.rb', line 7

def dimensions
  @dimensions
end

#model_nameObject (readonly)

Returns the value of attribute model_name.



7
8
9
# File 'lib/geminize/models/embedding_request.rb', line 7

def model_name
  @model_name
end

#task_typeObject (readonly)

Returns the value of attribute task_type.



7
8
9
# File 'lib/geminize/models/embedding_request.rb', line 7

def task_type
  @task_type
end

#textObject (readonly)

Returns the value of attribute text.



7
8
9
# File 'lib/geminize/models/embedding_request.rb', line 7

def text
  @text
end

#titleObject (readonly)

Returns the value of attribute title.



7
8
9
# File 'lib/geminize/models/embedding_request.rb', line 7

def title
  @title
end

Instance Method Details

#batch?Boolean Also known as: multiple?

Check if this request contains multiple texts

Returns:

  • (Boolean)

    True if the request contains multiple texts



76
77
78
# File 'lib/geminize/models/embedding_request.rb', line 76

def batch?
  @text.is_a?(Array)
end

#single_request_hash(text_input) ⇒ Hash

Create a single request hash for the given text

Parameters:

  • text_input (String)

    The text to create a request for

Returns:

  • (Hash)

    The request hash for a single text



86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
# File 'lib/geminize/models/embedding_request.rb', line 86

def single_request_hash(text_input)
  request = {
    model: @model_name,
    content: {
      parts: [
        {
          text: text_input.to_s
        }
      ]
    },
    taskType: @task_type
  }

  # Add title if provided
  request[:content][:title] = @title if @title

  # Add optional parameters if they exist
  request[:dimensions] = @dimensions if @dimensions

  request
end

#to_hObject

Alias for to_hash



70
71
72
# File 'lib/geminize/models/embedding_request.rb', line 70

def to_h
  to_hash
end

#to_hashHash

Get the request as a hash

Returns:

  • (Hash)

    The request hash



61
62
63
64
65
66
67
# File 'lib/geminize/models/embedding_request.rb', line 61

def to_hash
  if batch?
    build_batch_request
  else
    build_single_request
  end
end