Class: RCrewAI::LLMClients::Google

Inherits:
Base
  • Object
show all
Defined in:
lib/rcrewai/llm_clients/google.rb

Constant Summary collapse

BASE_URL =
'https://generativelanguage.googleapis.com/v1beta'
FINISH_REASON_MAP =
{
  'STOP' => :stop,
  'MAX_TOKENS' => :length,
  'SAFETY' => :stop,
  'RECITATION' => :stop
}.freeze

Instance Attribute Summary

Attributes inherited from Base

#config, #logger

Instance Method Summary collapse

Methods inherited from Base

#complete

Constructor Details

#initialize(config = RCrewAI.configuration) ⇒ Google

Returns a new instance of Google.



23
24
25
26
# File 'lib/rcrewai/llm_clients/google.rb', line 23

def initialize(config = RCrewAI.configuration)
  super
  @base_url = BASE_URL
end

Instance Method Details

#chat(messages:, tools: nil, tool_choice: :auto, stream: nil, **options) ⇒ Object

rubocop:disable Lint/UnusedMethodArgument



28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
# File 'lib/rcrewai/llm_clients/google.rb', line 28

def chat(messages:, tools: nil, tool_choice: :auto, stream: nil, **options) # rubocop:disable Lint/UnusedMethodArgument
  contents = format_messages(messages)
  payload = {
    contents: contents,
    generationConfig: {
      temperature: options[:temperature] || config.temperature,
      maxOutputTokens: options[:max_tokens] || config.max_tokens || 2048
    }.compact
  }
  payload[:generationConfig][:topP] = options[:top_p] if options[:top_p]
  payload[:generationConfig][:topK] = options[:top_k] if options[:top_k]
  payload[:generationConfig][:stopSequences] = options[:stop_sequences] if options[:stop_sequences]
  payload[:safetySettings] = options[:safety_settings] if options[:safety_settings]

  if tools && !tools.empty?
    payload[:tools] = [ProviderSchema.for_many(:google, tools)]
    # tool_choice: Google has toolConfig; left as default unless explicitly set
  end

  api_key = config.google_api_key || config.api_key
  if stream
    url = "#{@base_url}/models/#{config.model}:streamGenerateContent?alt=sse&key=#{api_key}"
    stream_chat(url, payload, stream)
  else
    url = "#{@base_url}/models/#{config.model}:generateContent?key=#{api_key}"
    plain_chat(url, payload)
  end
end

#modelsObject



61
62
63
# File 'lib/rcrewai/llm_clients/google.rb', line 61

def models
  %w[gemini-pro gemini-1.5-pro gemini-1.5-flash gemini-pro-vision]
end

#supports_native_tools?(model: config.model) ⇒ Boolean

rubocop:disable Lint/UnusedMethodArgument

Returns:

  • (Boolean)


57
58
59
# File 'lib/rcrewai/llm_clients/google.rb', line 57

def supports_native_tools?(model: config.model) # rubocop:disable Lint/UnusedMethodArgument
  true
end