Class: Mistri::Providers::Gemini

Inherits:
Object
  • Object
show all
Defined in:
lib/mistri/providers/gemini.rb,
lib/mistri/providers/gemini/assembler.rb,
lib/mistri/providers/gemini/serializer.rb

Overview

The Gemini API (v1beta generateContent), streamed over SSE and stateless: the full history replays every turn.

Thinking is deliberately unconstrained: no budget, no level, only includeThoughts so summaries stream for the UI. The model's own defaults decide how much to think, and a host override passes through verbatim. maxOutputTokens is omitted for the same reason: the API defaults to the model's ceiling.

Defined Under Namespace

Modules: Serializer Classes: Assembler

Constant Summary collapse

DEFAULT_THINKING =
{ includeThoughts: true }.freeze

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(api_key:, model: "gemini-2.5-flash", origin: "https://generativelanguage.googleapis.com", thinking: DEFAULT_THINKING, **transport_options) ⇒ Gemini

Returns a new instance of Gemini.



16
17
18
19
20
21
22
23
# File 'lib/mistri/providers/gemini.rb', line 16

def initialize(api_key:, model: "gemini-2.5-flash",
               origin: "https://generativelanguage.googleapis.com",
               thinking: DEFAULT_THINKING, **transport_options)
  @api_key = api_key
  @model = model
  @thinking = thinking
  @transport = Transport.new(origin: origin, **transport_options)
end

Instance Attribute Details

#modelObject (readonly)

Returns the value of attribute model.



25
26
27
# File 'lib/mistri/providers/gemini.rb', line 25

def model
  @model
end

Instance Method Details

#closeObject



42
# File 'lib/mistri/providers/gemini.rb', line 42

def close = @transport.close

#stream(messages:, system: nil, tools: [], signal: nil, **overrides, &emit) ⇒ Object



27
28
29
30
31
32
33
34
35
36
37
38
39
40
# File 'lib/mistri/providers/gemini.rb', line 27

def stream(messages:, system: nil, tools: [], signal: nil, **overrides, &emit)
  model = overrides.fetch(:model, @model)
  assembler = Gemini::Assembler.new(model: model)
  body = build_body(messages, system, tools, overrides)
  path = "/v1beta/models/#{model}:streamGenerateContent?alt=sse"
  outcome = @transport.stream_post(path, body: body, headers: headers,
                                         signal: signal) do |record|
    assembler.feed(record,
                   &emit)
  end
  outcome == :aborted ? assembler.abort(&emit) : assembler.finish(&emit)
rescue Error => e
  assembler.fail_stream(e, &emit)
end