Module: GenAI::Api::Format::OpenAI

Included in:
Language::OpenAI
Defined in:
lib/gen_ai/api/format/open_ai.rb

Instance Method Summary collapse

Instance Method Details

#build_raw_choices(chunks) ⇒ Object



29
30
31
32
33
34
35
36
37
38
# File 'lib/gen_ai/api/format/open_ai.rb', line 29

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

#build_raw_response(chunks) ⇒ Object



24
25
26
27
# File 'lib/gen_ai/api/format/open_ai.rb', line 24

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

#chunk_params_from_streaming(chunk) ⇒ Object



15
16
17
18
19
20
21
22
# File 'lib/gen_ai/api/format/open_ai.rb', line 15

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

#extract_completions(response) ⇒ Object



11
12
13
# File 'lib/gen_ai/api/format/open_ai.rb', line 11

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

#extract_embeddings(response) ⇒ Object



7
8
9
# File 'lib/gen_ai/api/format/open_ai.rb', line 7

def extract_embeddings(response)
  response['data'].map { |datum| datum['embedding'] }
end