Class: Riffer::Agent Abstract
- Inherits:
-
Object
- Object
- Riffer::Agent
- Extended by:
- Helpers::ClassNameConverter, 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”).
-
.model_options(options = nil) ⇒ Hash
Gets or sets model options passed to generate_text/stream_text.
-
.provider_options(options = nil) ⇒ Hash
Gets or sets provider options passed to the provider client.
-
.uses_tools(tools_or_lambda = nil) ⇒ Array<Class>, ...
Gets or sets the tools used by this agent.
Instance Method Summary collapse
-
#generate(prompt_or_messages, tool_context: nil) ⇒ String
Generates a response from the agent.
-
#initialize ⇒ void
constructor
Initializes a new agent.
-
#stream(prompt_or_messages, tool_context: nil) ⇒ Enumerator
Streams a response from the agent.
Methods included from Messages::Converter
Constructor Details
#initialize ⇒ void
Initializes a new agent
90 91 92 93 94 95 96 97 98 99 100 101 |
# File 'lib/riffer/agent.rb', line 90 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
85 86 87 |
# File 'lib/riffer/agent.rb', line 85 def @messages end |
Class Method Details
.all ⇒ Array<Class>
Returns all agent subclasses
78 79 80 |
# File 'lib/riffer/agent.rb', line 78 def all subclasses end |
.find(identifier) ⇒ Class?
Finds an agent class by identifier
72 73 74 |
# File 'lib/riffer/agent.rb', line 72 def find(identifier) subclasses.find { |agent_class| agent_class.identifier == identifier.to_s } end |
.identifier(value = nil) ⇒ String
Gets or sets the agent identifier
22 23 24 25 |
# File 'lib/riffer/agent.rb', line 22 def identifier(value = nil) return @identifier || class_name_to_path(name) if value.nil? @identifier = value.to_s end |
.instructions(instructions_text = nil) ⇒ String
Gets or sets the agent instructions
39 40 41 42 43 |
# File 'lib/riffer/agent.rb', line 39 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”)
30 31 32 33 34 |
# File 'lib/riffer/agent.rb', line 30 def model(model_string = nil) return @model if model_string.nil? validate_is_string!(model_string, "model") @model = model_string end |
.model_options(options = nil) ⇒ Hash
Gets or sets model options passed to generate_text/stream_text
56 57 58 59 |
# File 'lib/riffer/agent.rb', line 56 def ( = nil) return @model_options || {} if .nil? @model_options = end |
.provider_options(options = nil) ⇒ Hash
Gets or sets provider options passed to the provider client
48 49 50 51 |
# File 'lib/riffer/agent.rb', line 48 def ( = nil) return @provider_options || {} if .nil? @provider_options = end |
.uses_tools(tools_or_lambda = nil) ⇒ Array<Class>, ...
Gets or sets the tools used by this agent
64 65 66 67 |
# File 'lib/riffer/agent.rb', line 64 def uses_tools(tools_or_lambda = nil) return @tools_config if tools_or_lambda.nil? @tools_config = tools_or_lambda end |
Instance Method Details
#generate(prompt_or_messages, tool_context: nil) ⇒ String
Generates a response from the agent
107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 |
# File 'lib/riffer/agent.rb', line 107 def generate(, tool_context: nil) @tool_context = tool_context @resolved_tools = nil () 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, tool_context: nil) ⇒ Enumerator
Streams a response from the agent
128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 |
# File 'lib/riffer/agent.rb', line 128 def stream(, tool_context: nil) @tool_context = tool_context @resolved_tools = nil () Enumerator.new do |yielder| loop do accumulated_content = "" accumulated_tool_calls = [] current_tool_call = nil 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 when Riffer::StreamEvents::ToolCallDelta current_tool_call ||= {item_id: event.item_id, name: event.name, arguments: ""} current_tool_call[:arguments] += event.arguments_delta current_tool_call[:name] ||= event.name when Riffer::StreamEvents::ToolCallDone accumulated_tool_calls << { id: event.item_id, call_id: event.call_id, name: event.name, arguments: event.arguments } current_tool_call = nil end end response = Riffer::Messages::Assistant.new(accumulated_content, tool_calls: accumulated_tool_calls) @messages << response break unless has_tool_calls?(response) execute_tool_calls(response) end end end |