|
| 1 | +# frozen_string_literal: true |
| 2 | + |
| 3 | +module RubyLLM |
| 4 | + module Providers |
| 5 | + module Cohere |
| 6 | + # Streaming methods of the Cohere API integration |
| 7 | + # - https://docs.cohere.com/docs/streaming |
| 8 | + module Streaming |
| 9 | + private |
| 10 | + |
| 11 | + def stream_url |
| 12 | + completion_url |
| 13 | + end |
| 14 | + |
| 15 | + def build_chunk(data) |
| 16 | + Chunk.new( |
| 17 | + role: :assistant, |
| 18 | + model_id: extract_model_id(data), |
| 19 | + content: extract_content(data), |
| 20 | + input_tokens: extract_input_tokens(data), |
| 21 | + output_tokens: extract_output_tokens(data), |
| 22 | + tool_calls: extract_tool_calls(data) |
| 23 | + ) |
| 24 | + end |
| 25 | + |
| 26 | + def extract_model_id(data) |
| 27 | + data['response_id'] || data['id'] |
| 28 | + end |
| 29 | + |
| 30 | + def extract_content(data) |
| 31 | + case data['type'] |
| 32 | + when 'content-delta' |
| 33 | + data.dig('delta', 'message', 'content', 'text') |
| 34 | + when 'message-end' |
| 35 | + # Final message content |
| 36 | + data.dig('delta', 'message', 'content', 0, 'text') |
| 37 | + end |
| 38 | + end |
| 39 | + |
| 40 | + def extract_input_tokens(data) |
| 41 | + return unless data['type'] == 'message-end' |
| 42 | + |
| 43 | + data.dig('delta', 'usage', 'tokens', 'input_tokens') |
| 44 | + end |
| 45 | + |
| 46 | + def extract_output_tokens(data) |
| 47 | + return unless data['type'] == 'message-end' |
| 48 | + |
| 49 | + data.dig('delta', 'usage', 'tokens', 'output_tokens') |
| 50 | + end |
| 51 | + |
| 52 | + def extract_tool_calls(data) |
| 53 | + case data['type'] |
| 54 | + when 'tool-call-start' |
| 55 | + tool_call_data = data.dig('delta', 'message', 'tool_calls') |
| 56 | + return {} unless tool_call_data |
| 57 | + |
| 58 | + tool_call = ToolCall.new( |
| 59 | + id: tool_call_data['id'], |
| 60 | + name: tool_call_data.dig('function', 'name'), |
| 61 | + arguments: tool_call_data.dig('function', 'arguments') || '' |
| 62 | + ) |
| 63 | + { tool_call.id => tool_call } |
| 64 | + when 'tool-call-delta' |
| 65 | + # Handle streaming tool call arguments |
| 66 | + argument_delta = data.dig('delta', 'message', 'tool_calls', 'function', 'arguments') |
| 67 | + return {} unless argument_delta |
| 68 | + |
| 69 | + { nil => ToolCall.new(id: nil, name: nil, arguments: argument_delta) } |
| 70 | + when 'message-end' |
| 71 | + tool_calls = data.dig('delta', 'message', 'tool_calls') |
| 72 | + return {} unless tool_calls |
| 73 | + |
| 74 | + result = {} |
| 75 | + tool_calls.each do |call| |
| 76 | + tool_call = ToolCall.new( |
| 77 | + id: call['id'], |
| 78 | + name: call.dig('function', 'name'), |
| 79 | + arguments: call.dig('function', 'parameters') |
| 80 | + ) |
| 81 | + result[tool_call.id] = tool_call |
| 82 | + end |
| 83 | + result |
| 84 | + else |
| 85 | + {} |
| 86 | + end |
| 87 | + end |
| 88 | + |
| 89 | + def parse_streaming_error(data) |
| 90 | + error_data = JSON.parse(data) |
| 91 | + return unless error_data['type'] == 'error' |
| 92 | + |
| 93 | + message = error_data.dig('error', 'message') || 'Unknown error' |
| 94 | + [500, message] |
| 95 | + rescue JSON::ParserError |
| 96 | + [500, 'Failed to parse error response'] |
| 97 | + end |
| 98 | + end |
| 99 | + end |
| 100 | + end |
| 101 | +end |
0 commit comments