Class: GeminiCraft::Client

Inherits:
Object
  • Object
show all
Defined in:
lib/gemini_craft/client.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeClient

Returns a new instance of Client.



13
14
15
16
17
18
# File 'lib/gemini_craft/client.rb', line 13

def initialize
  @config = GeminiCraft.configuration
  @config.validate!
  @cache = Cache.new(@config)
  @logger = setup_logger
end

Instance Attribute Details

#cacheObject (readonly)

Returns the value of attribute cache.



11
12
13
# File 'lib/gemini_craft/client.rb', line 11

def cache
  @cache
end

#configObject (readonly)

Returns the value of attribute config.



11
12
13
# File 'lib/gemini_craft/client.rb', line 11

def config
  @config
end

#loggerObject (readonly)

Returns the value of attribute logger.



11
12
13
# File 'lib/gemini_craft/client.rb', line 11

def logger
  @logger
end

Instance Method Details

#generate_content(text, system_instruction = nil, options = {}, stream: false) ⇒ Object



20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
# File 'lib/gemini_craft/client.rb', line 20

def generate_content(text, system_instruction = nil, options = {}, stream: false)
  log(:info, "Generating content", { model: @config.model, stream: stream })

  cache_key = generate_cache_key(text, system_instruction, options) unless stream

  if !stream && @config.cache_enabled && (cached_response = @cache.get(cache_key))
    log(:debug, "Cache hit", { cache_key: cache_key })
    return cached_response
  end

  payload = build_payload(text, system_instruction, options, stream: stream)

  if stream
    generate_streaming_content(payload)
  else
    generate_standard_content(payload, cache_key)
  end
rescue StandardError => e
  log(:error, "Content generation failed", { error: e.message })
  raise
end

#generate_with_functions(text, functions, system_instruction = nil, options = {}) ⇒ Object



42
43
44
45
46
47
48
49
50
# File 'lib/gemini_craft/client.rb', line 42

def generate_with_functions(text, functions, system_instruction = nil, options = {})
  log(:info, "Generating content with functions", { function_count: functions.size })

  payload = build_payload(text, system_instruction, options)
  payload[:tools] = [{ function_declarations: functions }]

  response = make_request("models/#{@config.model}:generateContent", payload)
  process_function_response(response)
end