Class: Riffer::Agent Abstract
- Inherits:
-
Object
- Object
- Riffer::Agent
- Extended by:
- Helpers::Validations
- Includes:
- Messages::Converter
- Defined in:
- lib/riffer/agent.rb
Overview
Riffer::Agent is the base class for all agents in the Riffer framework.
Provides orchestration for LLM calls, tool use, and message management.
Instance Attribute Summary collapse
-
#messages ⇒ Array<Riffer::Messages::Base>
readonly
The message history for the agent.
Class Method Summary collapse
-
.all ⇒ Array<Class>
Returns all agent subclasses.
-
.find(identifier) ⇒ Class?
Finds an agent class by identifier.
-
.identifier(value = nil) ⇒ String
Gets or sets the agent identifier.
-
.instructions(instructions_text = nil) ⇒ String
Gets or sets the agent instructions.
-
.model(model_string = nil) ⇒ String
Gets or sets the model string (e.g., “openai/gpt-4”).
Instance Method Summary collapse
-
#generate(prompt_or_messages) ⇒ String
Generates a response from the agent.
-
#initialize ⇒ void
constructor
Initializes a new agent.
-
#stream(prompt_or_messages) ⇒ Enumerator
Streams a response from the agent.
Methods included from Messages::Converter
Constructor Details
#initialize ⇒ void
Initializes a new agent
63 64 65 66 67 68 69 70 71 72 73 74 |
# File 'lib/riffer/agent.rb', line 63 def initialize @messages = [] @model_string = self.class.model @instructions_text = self.class.instructions provider_name, model_name = @model_string.split("/", 2) raise Riffer::ArgumentError, "Invalid model string: #{@model_string}" unless [provider_name, model_name].all? { |part| part.is_a?(String) && !part.strip.empty? } @provider_name = provider_name @model_name = model_name end |
Instance Attribute Details
#messages ⇒ Array<Riffer::Messages::Base> (readonly)
The message history for the agent
58 59 60 |
# File 'lib/riffer/agent.rb', line 58 def @messages end |
Class Method Details
.all ⇒ Array<Class>
Returns all agent subclasses
51 52 53 |
# File 'lib/riffer/agent.rb', line 51 def all subclasses end |
.find(identifier) ⇒ Class?
Finds an agent class by identifier
45 46 47 |
# File 'lib/riffer/agent.rb', line 45 def find(identifier) subclasses.find { |agent_class| agent_class.identifier == identifier.to_s } end |
.identifier(value = nil) ⇒ String
Gets or sets the agent identifier
19 20 21 22 |
# File 'lib/riffer/agent.rb', line 19 def identifier(value = nil) return @identifier if value.nil? @identifier = value.to_s end |
.instructions(instructions_text = nil) ⇒ String
Gets or sets the agent instructions
36 37 38 39 40 |
# File 'lib/riffer/agent.rb', line 36 def instructions(instructions_text = nil) return @instructions if instructions_text.nil? validate_is_string!(instructions_text, "instructions") @instructions = instructions_text end |
.model(model_string = nil) ⇒ String
Gets or sets the model string (e.g., “openai/gpt-4”)
27 28 29 30 31 |
# File 'lib/riffer/agent.rb', line 27 def model(model_string = nil) return @model if model_string.nil? validate_is_string!(model_string, "model") @model = model_string end |
Instance Method Details
#generate(prompt_or_messages) ⇒ String
Generates a response from the agent
79 80 81 82 83 84 85 86 87 88 89 90 91 92 |
# File 'lib/riffer/agent.rb', line 79 def generate() () loop do response = call_llm @messages << response break unless has_tool_calls?(response) execute_tool_calls(response) end extract_final_response end |
#stream(prompt_or_messages) ⇒ Enumerator
Streams a response from the agent
97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 |
# File 'lib/riffer/agent.rb', line 97 def stream() () Enumerator.new do |yielder| accumulated_content = "" call_llm_stream.each do |event| yielder << event case event when Riffer::StreamEvents::TextDelta accumulated_content += event.content when Riffer::StreamEvents::TextDone accumulated_content = event.content end end response = Riffer::Messages::Assistant.new(accumulated_content) @messages << response end end |