Class: PatientLLM::Agent::Response
- Inherits:
-
Object
- Object
- PatientLLM::Agent::Response
- 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
-
#context ⇒ PatientHttp::CallbackArgs
readonly
The context passed to ask/continue.
-
#http_request_id ⇒ String?
readonly
The request id of the HTTP exchange.
-
#http_response ⇒ PatientHttp::Response?
readonly
The HTTP response that produced this LLM response.
-
#llm_response ⇒ PromptBuilder::Response
readonly
The underlying LLM response.
-
#session ⇒ PromptBuilder::Session
readonly
The session including this response.
Instance Method Summary collapse
-
#[](key) ⇒ Object
A value from the context passed to ask/continue.
-
#has_tool_calls? ⇒ Boolean
Whether the response contains tool calls.
-
#initialize(llm_response, session:, output_schema: nil, http_response: nil, http_request_id: nil, context: nil) ⇒ Response
constructor
A new instance of Response.
-
#model ⇒ String?
The model that produced the response.
-
#object ⇒ Hash, ...
The structured output parsed per the agent's output schema.
-
#state ⇒ Hash
The JSON-native session state for persistence.
-
#text ⇒ String?
The text of the response.
-
#tool_calls ⇒ Array<PromptBuilder::Items::FunctionCall>
The tool calls in the response.
-
#usage ⇒ PromptBuilder::Usage?
Token usage for the response.
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
#context ⇒ PatientHttp::CallbackArgs (readonly)
Returns the context passed to ask/continue.
27 28 29 |
# File 'lib/patient_llm/agent/response.rb', line 27 def context @context end |
#http_request_id ⇒ String? (readonly)
Returns 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_response ⇒ PatientHttp::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.
21 22 23 |
# File 'lib/patient_llm/agent/response.rb', line 21 def http_response @http_response end |
#llm_response ⇒ PromptBuilder::Response (readonly)
Returns the underlying LLM response.
13 14 15 |
# File 'lib/patient_llm/agent/response.rb', line 13 def llm_response @llm_response end |
#session ⇒ PromptBuilder::Session (readonly)
Returns 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].
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.
99 100 101 |
# File 'lib/patient_llm/agent/response.rb', line 99 def has_tool_calls? llm_response.has_tool_calls? end |
#model ⇒ String?
The model that produced the response.
92 93 94 |
# File 'lib/patient_llm/agent/response.rb', line 92 def model llm_response.model end |
#object ⇒ Hash, ...
The structured output parsed per the agent's output schema.
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., text: text) end end |
#state ⇒ Hash
The JSON-native session state for persistence. Store this and pass it
to Agent.continue to resume the conversation later.
78 79 80 |
# File 'lib/patient_llm/agent/response.rb', line 78 def state session.to_h end |
#text ⇒ String?
The text of the response.
51 52 53 |
# File 'lib/patient_llm/agent/response.rb', line 51 def text llm_response.text end |
#tool_calls ⇒ Array<PromptBuilder::Items::FunctionCall>
The tool calls in the response.
106 107 108 |
# File 'lib/patient_llm/agent/response.rb', line 106 def tool_calls llm_response.tool_calls end |
#usage ⇒ PromptBuilder::Usage?
Token usage for the response.
85 86 87 |
# File 'lib/patient_llm/agent/response.rb', line 85 def usage llm_response.usage end |