Class: Gemini::Tokens

Inherits:
Object
  • Object
show all
Defined in:
lib/gemini/tokens.rb

Constant Summary collapse

DEFAULT_MODEL =
"gemini-2.5-flash".freeze

Instance Method Summary collapse

Constructor Details

#initialize(client:) ⇒ Tokens

Returns a new instance of Tokens.



5
6
7
# File 'lib/gemini/tokens.rb', line 5

def initialize(client:)
  @client = client
end

Instance Method Details

#count(input = nil, model: DEFAULT_MODEL, contents: nil, system_instruction: nil, tools: nil, generation_config: nil, cached_content: nil, **parameters) ⇒ Object

Count tokens for the given input.

input: String, Array of parts/contents, or Hash. Optional when ‘contents:` is given. contents: full Array of Content objects (overrides input). system_instruction: String or Content hash. tools: Array of tool definitions (passed via generateContentRequest form). generation_config: Hash forwarded as generationConfig. cached_content: cachedContents/* resource name.



17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
# File 'lib/gemini/tokens.rb', line 17

def count(input = nil, model: DEFAULT_MODEL, contents: nil, system_instruction: nil,
          tools: nil, generation_config: nil, cached_content: nil, **parameters)
  normalized_model = normalize_model(model)

  payload = build_payload(
    model: normalized_model,
    input: input,
    contents: contents,
    system_instruction: system_instruction,
    tools: tools,
    generation_config: generation_config,
    cached_content: cached_content
  ).merge(parameters)

  response = @client.json_post(
    path: "models/#{normalized_model}:countTokens",
    parameters: payload
  )
  Gemini::Response.new(response)
end