Class: LLM::ActiveRecord::Message
- Inherits:
-
ActiveRecord::Base
- Object
- ActiveRecord::Base
- LLM::ActiveRecord::Message
- Defined in:
- lib/llm/active_record/message.rb
Overview
The Message class represents messages from the jsonb column belonging to an agent as a SQL view that can be accessed via an ActiveRecord model whose table never materializes in the database.
Class Method Summary collapse
-
.load_schema!
private
The derived table does not exist.
- .for(record:) ⇒ ActiveRecord::Relation
Instance Method Summary collapse
-
#tool_call? ⇒ Boolean
Returns true when the message requests a function call.
-
#tool_return? ⇒ Boolean
Returns true when the message represents a function return.
- #unwrap! ⇒ LLM::Message
Methods included from ActsAsLLM
Methods included from ActsAsAgent
Class Method Details
.load_schema!
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.
This method returns an undefined value.
The derived table does not exist. There is no schema to load.
18 19 20 |
# File 'lib/llm/active_record/message.rb', line 18 def self.load_schema! @columns_hash = {}.freeze end |
.for(record:) ⇒ ActiveRecord::Relation
26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 |
# File 'lib/llm/active_record/message.rb', line 26 def self.for(record:) klass = record.class connection = klass.connection table = connection.quote_table_name(klass.table_name) = klass. column = connection.quote_column_name(.fetch(:data_column)) from(<<~SQL).where(agent_id: record&.id) (SELECT #{table}.id AS agent_id, (message ->> 'id') AS id, (message ->> 'role') AS role, (message ->> 'content') AS content, (message -> 'tools') AS tools, ordinality AS position, message AS data FROM #{table}, jsonb_array_elements(#{table}.#{column} -> 'messages') WITH ORDINALITY AS each(message, ordinality)) AS llm_agent_messages SQL end |
Instance Method Details
#tool_call? ⇒ Boolean
Returns true when the message requests a function call
49 50 51 |
# File 'lib/llm/active_record/message.rb', line 49 def tool_call? unwrap!.tool_call? end |
#tool_return? ⇒ Boolean
Returns true when the message represents a function return
55 56 57 |
# File 'lib/llm/active_record/message.rb', line 55 def tool_return? unwrap!.tool_return? end |
#unwrap! ⇒ LLM::Message
61 62 63 64 65 66 67 68 69 70 71 |
# File 'lib/llm/active_record/message.rb', line 61 def unwrap! @message ||= begin stored = Hash === data ? data : {} extra = stored.each_with_object({}) { |(key, value), acc| acc[key.to_sym] = value } extra[:id] = stored["id"] || id extra[:role] ||= role extra[:content] ||= content extra[:tool_calls] = stored["tools"] || tools LLM::Message.new(extra[:role], extra[:content], extra) end end |