Module: RubyLLM::Protocols::Responses::Compaction

Included in:
RubyLLM::Providers::Azure::Responses, RubyLLM::Providers::OpenAI::Responses, RubyLLM::Providers::XAI::Responses
Defined in:
lib/ruby_llm/protocols/responses/compaction.rb

Overview

Manual context compaction through the Responses API.

Instance Method Summary collapse

Instance Method Details

#compaction_urlObject



8
9
10
# File 'lib/ruby_llm/protocols/responses/compaction.rb', line 8

def compaction_url
  "#{completion_url}/compact"
end

#parse_compaction_response(response) ⇒ Object



17
18
19
20
21
22
23
24
25
# File 'lib/ruby_llm/protocols/responses/compaction.rb', line 17

def parse_compaction_response(response)
  body = response.body
  unless body['object'] == 'response.compaction' && body['output'].is_a?(Array)
    raise Error.new('The provider returned an invalid compaction response', response:)
  end

  Message.new(role: :assistant, content: '', model: model.id, finish_reason: :stop,
              raw_content: body, raw: response, **parse_usage(body['usage'] || {}))
end

#render_compaction_payload(messages) ⇒ Object



12
13
14
15
# File 'lib/ruby_llm/protocols/responses/compaction.rb', line 12

def render_compaction_payload(messages)
  { model: model.id, input: format_input(messages, caching: false),
    instructions: format_instructions(messages, caching: false) }.compact
end