From afeacbcec2abedea858d738fc8f5918b6a85bfa8 Mon Sep 17 00:00:00 2001 From: icebaker Date: Sat, 16 Dec 2023 11:17:17 -0300 Subject: fixing issues with gemini stream --- components/providers/google.rb | 42 +++++++++++++++++++++++------------------- 1 file changed, 23 insertions(+), 19 deletions(-) (limited to 'components') diff --git a/components/providers/google.rb b/components/providers/google.rb index b15dd1b..92a9cb2 100644 --- a/components/providers/google.rb +++ b/components/providers/google.rb @@ -70,6 +70,7 @@ module NanoBot end end + # TODO: Does Gemini have system messages? %i[backdrop directive].each do |key| next unless input[:behavior][key] @@ -79,7 +80,6 @@ module NanoBot _meta: { at: Time.now } } ) - # TODO: Does Gemini have system messages? messages.prepend( { role: 'user', parts: { text: input[:behavior][key] }, @@ -140,24 +140,9 @@ module NanoBot end if event.dig('candidates', 0, 'finishReason') - if tools&.size&.positive? - feedback.call( - { should_be_stored: true, - needs_another_round: true, - interaction: { who: 'AI', message: nil, meta: { tool_calls: tools } } } - ) - Tools.apply( - cartridge, input[:tools], tools, feedback, Logic::Google::Tools - ).each do |interaction| - feedback.call({ should_be_stored: true, needs_another_round: true, interaction: }) - end - end - - feedback.call( - { should_be_stored: !(content.nil? || content == ''), - interaction: content.nil? || content == '' ? nil : { who: 'AI', message: content }, - finished: true } - ) + # TODO: This does not have the same behavior as OpenAI, so you should + # not use it as a reference for the end of the streaming. + # Is this a bug from the Google Gemini REST API or expected behavior? end end @@ -166,6 +151,25 @@ module NanoBot Logic::Google::Tokens.apply_policies!(cartridge, payload), stream: true, &stream_call_back ) + + if tools&.size&.positive? + feedback.call( + { should_be_stored: true, + needs_another_round: true, + interaction: { who: 'AI', message: nil, meta: { tool_calls: tools } } } + ) + Tools.apply( + cartridge, input[:tools], tools, feedback, Logic::Google::Tools + ).each do |interaction| + feedback.call({ should_be_stored: true, needs_another_round: true, interaction: }) + end + end + + feedback.call( + { should_be_stored: !(content.nil? || content == ''), + interaction: content.nil? || content == '' ? nil : { who: 'AI', message: content }, + finished: true } + ) rescue StandardError => e raise e.class, e.response[:body] if e.response && e.response[:body] -- cgit v1.2.3 From 57f44096dd22c32b2575268a00fd467469400d44 Mon Sep 17 00:00:00 2001 From: icebaker Date: Sat, 16 Dec 2023 11:45:13 -0300 Subject: making google credentials and options flexible --- components/providers/google.rb | 20 ++------------------ 1 file changed, 2 insertions(+), 18 deletions(-) (limited to 'components') diff --git a/components/providers/google.rb b/components/providers/google.rb index 92a9cb2..a522d06 100644 --- a/components/providers/google.rb +++ b/components/providers/google.rb @@ -26,25 +26,9 @@ module NanoBot def initialize(options, settings, credentials, _environment) @settings = settings - gemini_credentials = if credentials[:'api-key'] - { - service: credentials[:service], - api_key: credentials[:'api-key'], - project_id: credentials[:'project-id'], - region: credentials[:region] - } - else - { - service: credentials[:service], - file_path: credentials[:'file-path'], - project_id: credentials[:'project-id'], - region: credentials[:region] - } - end - @client = Gemini.new( - credentials: gemini_credentials, - options: { model: options[:model], stream: options[:stream] } + credentials: credentials.transform_keys { |key| key.to_s.gsub('-', '_').to_sym }, + options: options.transform_keys { |key| key.to_s.gsub('-', '_').to_sym } ) end -- cgit v1.2.3