Class: Langchain::Assistant::LLM::Adapters::GoogleGemini

Inherits:
Base
  • Object
show all
Defined in:
lib/langchain/assistant/llm/adapters/google_gemini.rb

Instance Method Summary collapse

Instance Method Details

#allowed_tool_choicesObject

Get the allowed assistant.tool_choice values for Google Gemini



69
70
71
# File 'lib/langchain/assistant/llm/adapters/google_gemini.rb', line 69

def allowed_tool_choices
  ["auto", "none"]
end

#available_tool_names(tools) ⇒ Object

Get the available tool names for Google Gemini



74
75
76
# File 'lib/langchain/assistant/llm/adapters/google_gemini.rb', line 74

def available_tool_names(tools)
  build_tools(tools).map { |tool| tool.dig(:name) }
end

#build_chat_params(messages:, instructions:, tools:, tool_choice:, parallel_tool_calls:) ⇒ Hash

Build the chat parameters for the Google Gemini LLM

Parameters:

  • messages (Array)

    The messages

  • instructions (String)

    The system instructions

  • tools (Array)

    The tools to use

  • tool_choice (String)

    The tool choice

  • parallel_tool_calls (Boolean)

    Whether to make parallel tool calls

Returns:

  • (Hash)

    The chat parameters



16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
# File 'lib/langchain/assistant/llm/adapters/google_gemini.rb', line 16

def build_chat_params(
  messages:,
  instructions:,
  tools:,
  tool_choice:,
  parallel_tool_calls:
)
  Langchain.logger.warn "WARNING: `parallel_tool_calls:` is not supported by Google Gemini currently"

  params = {messages: messages}
  if tools.any?
    params[:tools] = build_tools(tools)
    params[:system] = instructions if instructions
    params[:tool_choice] = build_tool_config(tool_choice)
  end
  params
end

#build_message(role:, content: nil, image_url: nil, tool_calls: [], tool_call_id: nil) ⇒ Messages::GoogleGeminiMessage

Build a Google Gemini message

Parameters:

  • role (String)

    The role of the message

  • content (String) (defaults to: nil)

    The content of the message

  • image_url (String) (defaults to: nil)

    The image URL

  • tool_calls (Array) (defaults to: [])

    The tool calls

  • tool_call_id (String) (defaults to: nil)

    The tool call ID

Returns:



42
43
44
45
46
# File 'lib/langchain/assistant/llm/adapters/google_gemini.rb', line 42

def build_message(role:, content: nil, image_url: nil, tool_calls: [], tool_call_id: nil)
  Langchain.logger.warn "Image URL is not supported by Google Gemini" if image_url

  Messages::GoogleGeminiMessage.new(role: role, content: content, tool_calls: tool_calls, tool_call_id: tool_call_id)
end

#build_tools(tools) ⇒ Array

Build the tools for the Google Gemini LLM

Parameters:

  • tools (Array<Langchain::Tool::Base>)

    The tools

Returns:

  • (Array)

    The tools in Google Gemini format



64
65
66
# File 'lib/langchain/assistant/llm/adapters/google_gemini.rb', line 64

def build_tools(tools)
  tools.map { |tool| tool.class.function_schemas.to_google_gemini_format }.flatten
end

#extract_tool_call_args(tool_call:) ⇒ Array

Extract the tool call information from the Google Gemini tool call hash

Parameters:

  • tool_call (Hash)

    The tool call hash, format: “args”=>{“input”=>“NYC”}}

Returns:

  • (Array)

    The tool call information



52
53
54
55
56
57
58
# File 'lib/langchain/assistant/llm/adapters/google_gemini.rb', line 52

def extract_tool_call_args(tool_call:)
  tool_call_id = tool_call.dig("functionCall", "name")
  function_name = tool_call.dig("functionCall", "name")
  tool_name, method_name = function_name.split("__")
  tool_arguments = tool_call.dig("functionCall", "args").transform_keys(&:to_sym)
  [tool_call_id, tool_name, method_name, tool_arguments]
end

#support_system_message?Boolean

Returns:

  • (Boolean)


82
83
84
# File 'lib/langchain/assistant/llm/adapters/google_gemini.rb', line 82

def support_system_message?
  Messages::GoogleGeminiMessage::ROLES.include?("system")
end

#tool_roleObject



78
79
80
# File 'lib/langchain/assistant/llm/adapters/google_gemini.rb', line 78

def tool_role
  Messages::GoogleGeminiMessage::TOOL_ROLE
end