Module: LLM::Context::Deserializer Private

Included in:
LLM::Context
Defined in:
lib/llm/context/deserializer.rb

This module is part of a private API. You should avoid using this module if possible, as it may be removed or be changed in the future.

Instance Method Summary collapse

Instance Method Details

#deserialize(path: nil, string: nil, data: nil) ⇒ LLM::Context Also known as: restore

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Restore a saved context state

Parameters:

  • path (String, nil) (defaults to: nil)

    The path to a JSON file

  • string (String, nil) (defaults to: nil)

    A raw JSON string

  • data (Hash, nil) (defaults to: nil)

    A parsed context payload

Returns:

Raises:

  • (SystemCallError)

    Might raise a number of SystemCallError subclasses



18
19
20
21
22
23
24
25
26
27
28
29
30
# File 'lib/llm/context/deserializer.rb', line 18

def deserialize(path: nil, string: nil, data: nil)
  ctx = if data
    data
  elsif path.nil? and string.nil?
    raise ArgumentError, "a path, string, or data payload is required"
  elsif path
    LLM.json.load(::File.binread(path))
  else
    LLM.json.load(string)
  end
  @messages.concat [*ctx["messages"]].map { deserialize_message(_1) }
  self
end

#deserialize_message(payload) ⇒ LLM::Message

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Parameters:

  • payload (Hash)

Returns:



36
37
38
39
40
41
42
43
44
45
# File 'lib/llm/context/deserializer.rb', line 36

def deserialize_message(payload)
  tool_calls = deserialize_tool_calls(payload["tools"])
  returns = deserialize_returns(payload["content"]) if returns.nil?
  original_tool_calls = payload["original_tool_calls"]
  usage = payload["usage"]
  reasoning_content = payload["reasoning_content"]
  extra = {tool_calls:, original_tool_calls:, tools: @params[:tools], usage:, reasoning_content:}.compact
  content = returns.nil? ? deserialize_content(payload["content"]) : returns
  LLM::Message.new(payload["role"], content, extra)
end