Sha256: f26fc7ea3e5e48a6b5cb8b779771b14c5650caafd25cab5737e26b2218043708

Contents?: true

Size: 1.53 KB

Versions: 1

Compression:

Stored size: 1.53 KB

Contents

# frozen_string_literal: true

require 'faraday'
require 'pry'

module GenAI
  class Language
    class Gemini < Base
      include GenAI::Api::Format::Gemini

      COMPLETION_MODEL = 'gemini-pro'

      def initialize(token:, options: {})
        depends_on 'gemini-ai'

        @client = ::Gemini.new(
          credentials: {
            service: 'generative-language-api',
            api_key: token
          },
          options: { model: model(options) }
        )
      end

      def complete(prompt, options = {}); end

      def chat(messages, options = {}, &block)
        if block_given?
          response = @client.stream_generate_content(
            generate_options(messages, options), server_sent_events: true, &chunk_process_block(block)
          )
          build_result(model: model(options), raw: response.first, parsed: extract_completions(response).flatten)
        else
          response = @client.generate_content(generate_options(messages, options))
          build_result(model: model(options), raw: response, parsed: extract_completions(response))
        end
      end

      private

      def generate_options(messages, options)
        {
          contents: format_messages(messages),
          generationConfig: options.except(:model)
        }
      end

      def model(options)
        options[:model] || COMPLETION_MODEL
      end

      def chunk_process_block(block)
        proc do |data|
          chunk = build_chunk(chunk_params_from_streaming(data))

          block.call chunk
        end
      end
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
gen-ai-0.4.2 lib/gen_ai/language/gemini.rb