Class: Langchain::Assistant::Messages::GoogleGeminiMessage
- Defined in:
- lib/langchain/assistant/messages/google_gemini_message.rb
Constant Summary collapse
- ROLES =
Google Gemini uses the following roles:
[ "user", "model", "function" ].freeze
- TOOL_ROLE =
"function"
Instance Attribute Summary
Attributes inherited from Base
#content, #image_url, #role, #tool_call_id, #tool_calls
Instance Method Summary collapse
-
#build_parts ⇒ Array<Hash>
Builds the part value for the message hash.
-
#function? ⇒ Boolean
Check if the message is a tool call.
-
#initialize(role:, content: nil, tool_calls: [], tool_call_id: nil) ⇒ GoogleGeminiMessage
constructor
Initialize a new Google Gemini message.
-
#llm? ⇒ Boolean
Check if the message came from an LLM.
-
#model? ⇒ Boolean
Check if the message came from an LLM.
-
#model_hash ⇒ Hash
Convert the message to an GoogleGemini API-compatible hash.
-
#system? ⇒ Boolean
Google Gemini does not implement system prompts.
-
#to_hash ⇒ Hash
Convert the message to a Google Gemini API-compatible hash.
-
#tool? ⇒ Boolean
Check if the message is a tool call.
-
#tool_hash ⇒ Hash
Convert the message to an GoogleGemini API-compatible hash.
-
#user? ⇒ Boolean
Check if the message is a user call.
-
#user_hash ⇒ Hash
Convert the message to an GoogleGemini API-compatible hash.
Methods inherited from Base
Constructor Details
#initialize(role:, content: nil, tool_calls: [], tool_call_id: nil) ⇒ GoogleGeminiMessage
Initialize a new Google Gemini message
22 23 24 25 26 27 28 29 30 31 |
# File 'lib/langchain/assistant/messages/google_gemini_message.rb', line 22 def initialize(role:, content: nil, tool_calls: [], tool_call_id: nil) raise ArgumentError, "Role must be one of #{ROLES.join(", ")}" unless ROLES.include?(role) raise ArgumentError, "Tool calls must be an array of hashes" unless tool_calls.is_a?(Array) && tool_calls.all? { |tool_call| tool_call.is_a?(Hash) } @role = role # Some Tools return content as a JSON hence `.to_s` @content = content.to_s @tool_calls = tool_calls @tool_call_id = tool_call_id end |
Instance Method Details
#build_parts ⇒ Array<Hash>
Builds the part value for the message hash
116 117 118 119 120 121 122 |
# File 'lib/langchain/assistant/messages/google_gemini_message.rb', line 116 def build_parts if tool_calls.any? tool_calls else [{text: content}] end end |
#function? ⇒ Boolean
Check if the message is a tool call
75 76 77 |
# File 'lib/langchain/assistant/messages/google_gemini_message.rb', line 75 def function? role == "function" end |
#llm? ⇒ Boolean
Check if the message came from an LLM
36 37 38 |
# File 'lib/langchain/assistant/messages/google_gemini_message.rb', line 36 def llm? model? end |
#model? ⇒ Boolean
Check if the message came from an LLM
127 128 129 |
# File 'lib/langchain/assistant/messages/google_gemini_message.rb', line 127 def model? role == "model" end |
#model_hash ⇒ Hash
Convert the message to an GoogleGemini API-compatible hash
81 82 83 84 85 86 |
# File 'lib/langchain/assistant/messages/google_gemini_message.rb', line 81 def model_hash { role: role, parts: build_parts } end |
#system? ⇒ Boolean
Google Gemini does not implement system prompts
54 55 56 |
# File 'lib/langchain/assistant/messages/google_gemini_message.rb', line 54 def system? false end |
#to_hash ⇒ Hash
Convert the message to a Google Gemini API-compatible hash
43 44 45 46 47 48 49 50 51 |
# File 'lib/langchain/assistant/messages/google_gemini_message.rb', line 43 def to_hash if tool? tool_hash elsif model? model_hash elsif user? user_hash end end |
#tool? ⇒ Boolean
Check if the message is a tool call
61 62 63 |
# File 'lib/langchain/assistant/messages/google_gemini_message.rb', line 61 def tool? function? end |
#tool_hash ⇒ Hash
Convert the message to an GoogleGemini API-compatible hash
90 91 92 93 94 95 96 97 98 99 100 101 102 103 |
# File 'lib/langchain/assistant/messages/google_gemini_message.rb', line 90 def tool_hash { role: role, parts: [{ functionResponse: { name: tool_call_id, response: { name: tool_call_id, content: content } } }] } end |
#user? ⇒ Boolean
Check if the message is a user call
68 69 70 |
# File 'lib/langchain/assistant/messages/google_gemini_message.rb', line 68 def user? role == "user" end |
#user_hash ⇒ Hash
Convert the message to an GoogleGemini API-compatible hash
107 108 109 110 111 112 |
# File 'lib/langchain/assistant/messages/google_gemini_message.rb', line 107 def user_hash { role: role, parts: build_parts } end |