Class: PatientLLM::Agent::Response

Inherits:
Object
  • Object
show all
Defined in:
lib/patient_llm/agent/response.rb

Overview

The response delivered to an agent's hooks. Wraps the parsed PromptBuilder::Response together with the rest of the invocation state so hooks have one object with everything they need: the text, the parsed structured output, usage, the HTTP exchange, the context passed to ask/continue, and the serializable conversation state for multi-turn persistence.

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(llm_response, session:, output_schema: nil, http_response: nil, http_request_id: nil, context: nil) ⇒ Response

Returns a new instance of Response.



29
30
31
32
33
34
35
36
# File 'lib/patient_llm/agent/response.rb', line 29

def initialize(llm_response, session:, output_schema: nil, http_response: nil, http_request_id: nil, context: nil)
  @llm_response = llm_response
  @session = session
  @output_schema = output_schema
  @http_response = http_response
  @http_request_id = http_request_id
  @context = context || PatientHttp::CallbackArgs.new
end

Instance Attribute Details

#contextPatientHttp::CallbackArgs (readonly)

Returns the context passed to ask/continue.

Returns:

  • (PatientHttp::CallbackArgs)

    the context passed to ask/continue



27
28
29
# File 'lib/patient_llm/agent/response.rb', line 27

def context
  @context
end

#http_request_idString? (readonly)

Returns the request id of the HTTP exchange.

Returns:

  • (String, nil)

    the request id of the HTTP exchange



24
25
26
# File 'lib/patient_llm/agent/response.rb', line 24

def http_request_id
  @http_request_id
end

#http_responsePatientHttp::Response? (readonly)

Returns the HTTP response that produced this LLM response. In completed this is the final request's response; in tool_round it is that round's response.

Returns:

  • (PatientHttp::Response, nil)

    the HTTP response that produced this LLM response. In completed this is the final request's response; in tool_round it is that round's response.



21
22
23
# File 'lib/patient_llm/agent/response.rb', line 21

def http_response
  @http_response
end

#llm_responsePromptBuilder::Response (readonly)

Returns the underlying LLM response.

Returns:

  • (PromptBuilder::Response)

    the underlying LLM response



13
14
15
# File 'lib/patient_llm/agent/response.rb', line 13

def llm_response
  @llm_response
end

#sessionPromptBuilder::Session (readonly)

Returns the session including this response.

Returns:

  • (PromptBuilder::Session)

    the session including this response



16
17
18
# File 'lib/patient_llm/agent/response.rb', line 16

def session
  @session
end

Instance Method Details

#[](key) ⇒ Object

A value from the context passed to ask/continue. Shorthand for response.context[key].

Parameters:

  • key (String, Symbol)

    the context key

Returns:

  • (Object)

    the context value

Raises:

  • (KeyError)

    if the key is not in the context



44
45
46
# File 'lib/patient_llm/agent/response.rb', line 44

def [](key)
  context[key]
end

#has_tool_calls?Boolean

Whether the response contains tool calls.

Returns:

  • (Boolean)


99
100
101
# File 'lib/patient_llm/agent/response.rb', line 99

def has_tool_calls?
  llm_response.has_tool_calls?
end

#modelString?

The model that produced the response.

Returns:

  • (String, nil)


92
93
94
# File 'lib/patient_llm/agent/response.rb', line 92

def model
  llm_response.model
end

#objectHash, ...

The structured output parsed per the agent's output schema.

Returns:

  • (Hash, Array, Object)

    the parsed JSON value

Raises:

  • (StructuredOutputError)

    if the agent declares no output schema or the response text cannot be parsed as JSON



60
61
62
63
64
65
66
67
68
69
70
71
72
# File 'lib/patient_llm/agent/response.rb', line 60

def object
  return @object if defined?(@object)

  unless @output_schema
    raise StructuredOutputError.new("The agent has no output schema; declare one with `output` or use `text`", text: text)
  end

  @object = begin
    llm_response.parsed_json!
  rescue PromptBuilder::ParseError => e
    raise StructuredOutputError.new(e.message, text: text)
  end
end

#stateHash

The JSON-native session state for persistence. Store this and pass it to Agent.continue to resume the conversation later.

Returns:

  • (Hash)


78
79
80
# File 'lib/patient_llm/agent/response.rb', line 78

def state
  session.to_h
end

#textString?

The text of the response.

Returns:

  • (String, nil)


51
52
53
# File 'lib/patient_llm/agent/response.rb', line 51

def text
  llm_response.text
end

#tool_callsArray<PromptBuilder::Items::FunctionCall>

The tool calls in the response.

Returns:

  • (Array<PromptBuilder::Items::FunctionCall>)


106
107
108
# File 'lib/patient_llm/agent/response.rb', line 106

def tool_calls
  llm_response.tool_calls
end

#usagePromptBuilder::Usage?

Token usage for the response.

Returns:

  • (PromptBuilder::Usage, nil)


85
86
87
# File 'lib/patient_llm/agent/response.rb', line 85

def usage
  llm_response.usage
end