57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
|
# File 'lib/intelligence/adapters/google/chat_request_methods.rb', line 57
def chat_request_body( conversation, options = {} )
tools = options.delete( :tools ) || []
options = merge_options( @options, build_options( options ) )
gc = options[ :generationConfig ]&.dup || {}
gc.delete( :model )
gc.delete( :stream )
tools_object = gc.delete( :abilities )
tool_functions = to_google_tools( ( gc.delete( :tools ) || [] ).concat( tools ) )
if tool_functions&.any?
tools_object ||= {}
tools_object[ :function_declarations ] ||= []
tools_object[ :function_declarations ].concat( tool_functions )
end
if tool_config = gc.delete( :tool_config )
mode = tool_config[ :function_calling_config ]&.[]( :mode )
tool_config[ :function_calling_config ][ :mode ] = mode.to_s.upcase if mode
end
result = {}
result[ :generationConfig ] = gc
result[ :tools ] = tools_object if tools_object
result[ :tool_config ] = tool_config if tools && tool_config
system_instructions = to_google_system_message( conversation[ :system_message ] )
result[ :systemInstruction ] = system_instructions if system_instructions
result[ :contents ] = []
conversation[ :messages ]&.each do | message |
result_message = { role: message[ :role ] == :user ? 'user' : 'model' }
result_message_parts = []
message[ :contents ]&.each do | content |
case content[ :type ]
when :text
result_message_parts << { text: content[ :text ] }
when :binary
content_type = content[ :content_type ]
bytes = content[ :bytes ]
if content_type && bytes
mime_type = MIME::Types[ content_type ].first
if SUPPORTED_BINARY_MEDIA_TYPES.include?( mime_type&.media_type ) ||
SUPPORTED_BINARY_CONTENT_TYPES.include?( content_type )
result_message_parts << {
inline_data: {
mime_type: content_type,
data: Base64.strict_encode64( bytes )
}
}
else
raise UnsupportedContentError.new(
:google,
"does not support #{content_type} content type"
)
end
else
raise UnsupportedContentError.new(
:google,
'requires binary content to include content type and ( packed ) bytes'
)
end
when :file
content_type = content[ :content_type ]
uri = content[ :uri ]
if content_type && uri
mime_type = MIME::Types[ content_type ].first
if SUPPORTED_FILE_MEDIA_TYPES.include?( mime_type&.media_type ) ||
SUPPORTED_FILE_CONTENT_TYPES.include?( content_type )
result_message_parts << {
file_data: {
mime_type: content_type,
file_uri: uri
}
}
else
raise UnsupportedContentError.new(
:google,
"does not support #{content_type} content type"
)
end
else
raise UnsupportedContentError.new(
:google,
'requires file content to include content type and uri'
)
end
when :tool_call
result_message_parts << {
functionCall: {
name: content[ :tool_name ],
args: content[ :tool_parameters ]
}
}
when :tool_result
result_message_parts << {
functionResponse: {
name: content[ :tool_name ],
response: {
name: content[ :tool_name ],
content: content[ :tool_result ]
}
}
}
else
raise InvalidContentError.new( :google )
end
end
result_message[ :parts ] = result_message_parts
result[ :contents ] << result_message
end
JSON.generate( result )
end
|