Sha256: bc950a40e146fd3d44c230dfe0d7cd1ad3c8795ec98e97194f231699b719c459

Contents?: true

Size: 1.12 KB

Versions: 2

Compression:

Stored size: 1.12 KB

Contents

# frozen_string_literal: true

module GenAI
  module Api
    module Format
      module OpenAI
        def extract_embeddings(response)
          response['data'].map { |datum| datum['embedding'] }
        end

        def extract_completions(response)
          response['choices'].map { |choice| choice.dig('message', 'content') }
        end

        def chunk_params_from_streaming(chunk)
          {
            model: chunk['model'],
            index: chunk.dig('choices', 0, 'index'),
            value: chunk.dig('choices', 0, 'delta', 'content'),
            raw: chunk
          }
        end

        def build_raw_response(chunks)
          { 'choices' => build_raw_choices(chunks),
            'usage' => { 'completion_tokens' => chunks.values.map(&:size).sum } }
        end

        def build_raw_choices(chunks)
          chunks.map do |key, values|
            {
              'index' => key,
              'logprobs' => nil,
              'finish_reason' => 'stop',
              'message' => { 'role' => 'asssistant', 'content' => values.map(&:value).join }
            }
          end
        end
      end
    end
  end
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
gen-ai-0.4.3 lib/gen_ai/api/format/open_ai.rb
gen-ai-0.4.2 lib/gen_ai/api/format/open_ai.rb