Class: Layered::Assistant::ChunkParser
- Inherits:
-
Object
- Object
- Layered::Assistant::ChunkParser
- Defined in:
- app/services/layered/assistant/chunk_parser.rb
Instance Method Summary collapse
- #finished?(chunk) ⇒ Boolean
-
#initialize(protocol) ⇒ ChunkParser
constructor
A new instance of ChunkParser.
- #input_tokens(chunk) ⇒ Object
- #output_tokens(chunk) ⇒ Object
- #text(chunk) ⇒ Object
- #usage_ready?(chunk) ⇒ Boolean
Constructor Details
#initialize(protocol) ⇒ ChunkParser
Returns a new instance of ChunkParser.
4 5 6 |
# File 'app/services/layered/assistant/chunk_parser.rb', line 4 def initialize(protocol) @openai = protocol == "openai" end |
Instance Method Details
#finished?(chunk) ⇒ Boolean
17 18 19 20 21 22 23 |
# File 'app/services/layered/assistant/chunk_parser.rb', line 17 def finished?(chunk) if @openai chunk.dig("choices", 0, "finish_reason").present? else chunk["type"] == "message_stop" end end |
#input_tokens(chunk) ⇒ Object
29 30 31 32 33 34 35 |
# File 'app/services/layered/assistant/chunk_parser.rb', line 29 def input_tokens(chunk) if @openai chunk.dig("usage", "prompt_tokens")&.to_i if usage_ready?(chunk) elsif chunk["type"] == "message_start" chunk.dig("message", "usage", "input_tokens")&.to_i end end |
#output_tokens(chunk) ⇒ Object
37 38 39 40 41 42 43 |
# File 'app/services/layered/assistant/chunk_parser.rb', line 37 def output_tokens(chunk) if @openai chunk.dig("usage", "completion_tokens")&.to_i if usage_ready?(chunk) elsif chunk["type"] == "message_delta" chunk.dig("usage", "output_tokens")&.to_i end end |
#text(chunk) ⇒ Object
8 9 10 11 12 13 14 15 |
# File 'app/services/layered/assistant/chunk_parser.rb', line 8 def text(chunk) t = if @openai chunk.dig("choices", 0, "delta", "content") else chunk.dig("delta", "text") if chunk["type"] == "content_block_delta" end t unless t.nil? || t.empty? end |
#usage_ready?(chunk) ⇒ Boolean
25 26 27 |
# File 'app/services/layered/assistant/chunk_parser.rb', line 25 def usage_ready?(chunk) @openai && chunk["usage"].present? && chunk.dig("choices")&.empty? end |