Module: RubyLLM::Providers::Bedrock::Streaming::ContentExtraction

Defined in:
lib/ruby_llm/providers/bedrock/streaming/content_extraction.rb

Overview

Module for handling content extraction from AWS Bedrock streaming responses.

Instance Method Summary collapse

Instance Method Details

#extract_cache_creation_tokens(data) ⇒ Object



39
40
41
42
43
44
45
46
47
48
# File 'lib/ruby_llm/providers/bedrock/streaming/content_extraction.rb', line 39

def extract_cache_creation_tokens(data)
  direct = data.dig('message', 'usage',
                    'cache_creation_input_tokens') || data.dig('usage', 'cache_creation_input_tokens')
  return direct if direct

  breakdown = data.dig('message', 'usage', 'cache_creation') || data.dig('usage', 'cache_creation')
  return unless breakdown.is_a?(Hash)

  breakdown.values.compact.sum
end

#extract_cached_tokens(data) ⇒ Object



35
36
37
# File 'lib/ruby_llm/providers/bedrock/streaming/content_extraction.rb', line 35

def extract_cached_tokens(data)
  data.dig('message', 'usage', 'cache_read_input_tokens') || data.dig('usage', 'cache_read_input_tokens')
end

#extract_input_tokens(data) ⇒ Object



27
28
29
# File 'lib/ruby_llm/providers/bedrock/streaming/content_extraction.rb', line 27

def extract_input_tokens(data)
  data.dig('message', 'usage', 'input_tokens')
end

#extract_model_id(data) ⇒ Object



23
24
25
# File 'lib/ruby_llm/providers/bedrock/streaming/content_extraction.rb', line 23

def extract_model_id(data)
  data.dig('message', 'model') || @model_id
end

#extract_output_tokens(data) ⇒ Object



31
32
33
# File 'lib/ruby_llm/providers/bedrock/streaming/content_extraction.rb', line 31

def extract_output_tokens(data)
  data.dig('message', 'usage', 'output_tokens') || data.dig('usage', 'output_tokens')
end

#extract_streaming_content(data) ⇒ Object



13
14
15
16
17
# File 'lib/ruby_llm/providers/bedrock/streaming/content_extraction.rb', line 13

def extract_streaming_content(data)
  return '' unless data.is_a?(Hash)

  extract_content_by_type(data)
end

#extract_tool_calls(data) ⇒ Object



19
20
21
# File 'lib/ruby_llm/providers/bedrock/streaming/content_extraction.rb', line 19

def extract_tool_calls(data)
  data.dig('message', 'tool_calls') || data['tool_calls']
end

#json_delta?(data) ⇒ Boolean

Returns:

  • (Boolean)


9
10
11
# File 'lib/ruby_llm/providers/bedrock/streaming/content_extraction.rb', line 9

def json_delta?(data)
  data['type'] == 'content_block_delta' && data.dig('delta', 'type') == 'input_json_delta'
end