Class: OxAiWorkers::ModuleRequest
- Inherits:
-
Object
- Object
- OxAiWorkers::ModuleRequest
- Defined in:
- lib/oxaiworkers/module_request.rb
Direct Known Subclasses
Instance Attribute Summary collapse
-
#call_stack ⇒ Object
Returns the value of attribute call_stack.
-
#client ⇒ Object
Returns the value of attribute client.
-
#custom_id ⇒ Object
Returns the value of attribute custom_id.
-
#errors ⇒ Object
Returns the value of attribute errors.
-
#is_truncated ⇒ Object
Returns the value of attribute is_truncated.
-
#last_call ⇒ Object
Returns the value of attribute last_call.
-
#messages ⇒ Object
Returns the value of attribute messages.
-
#model ⇒ Object
Returns the value of attribute model.
-
#result ⇒ Object
Returns the value of attribute result.
-
#stop_double_calls ⇒ Object
Returns the value of attribute stop_double_calls.
-
#tool_calls ⇒ Object
Returns the value of attribute tool_calls.
-
#tools ⇒ Object
Returns the value of attribute tools.
Instance Method Summary collapse
- #append(role: nil, content: nil, messages: nil) ⇒ Object
- #cleanup ⇒ Object
- #initialize_requests(model:, call_stack: nil) ⇒ Object
- #not_found_is_ok ⇒ Object
- #params ⇒ Object
- #parse_choices(response) ⇒ Object
Instance Attribute Details
#call_stack ⇒ Object
Returns the value of attribute call_stack.
5 6 7 |
# File 'lib/oxaiworkers/module_request.rb', line 5 def call_stack @call_stack end |
#client ⇒ Object
Returns the value of attribute client.
5 6 7 |
# File 'lib/oxaiworkers/module_request.rb', line 5 def client @client end |
#custom_id ⇒ Object
Returns the value of attribute custom_id.
5 6 7 |
# File 'lib/oxaiworkers/module_request.rb', line 5 def custom_id @custom_id end |
#errors ⇒ Object
Returns the value of attribute errors.
5 6 7 |
# File 'lib/oxaiworkers/module_request.rb', line 5 def errors @errors end |
#is_truncated ⇒ Object
Returns the value of attribute is_truncated.
5 6 7 |
# File 'lib/oxaiworkers/module_request.rb', line 5 def is_truncated @is_truncated end |
#last_call ⇒ Object
Returns the value of attribute last_call.
5 6 7 |
# File 'lib/oxaiworkers/module_request.rb', line 5 def last_call @last_call end |
#messages ⇒ Object
Returns the value of attribute messages.
5 6 7 |
# File 'lib/oxaiworkers/module_request.rb', line 5 def end |
#model ⇒ Object
Returns the value of attribute model.
5 6 7 |
# File 'lib/oxaiworkers/module_request.rb', line 5 def model @model end |
#result ⇒ Object
Returns the value of attribute result.
5 6 7 |
# File 'lib/oxaiworkers/module_request.rb', line 5 def result @result end |
#stop_double_calls ⇒ Object
Returns the value of attribute stop_double_calls.
5 6 7 |
# File 'lib/oxaiworkers/module_request.rb', line 5 def stop_double_calls @stop_double_calls end |
#tool_calls ⇒ Object
Returns the value of attribute tool_calls.
5 6 7 |
# File 'lib/oxaiworkers/module_request.rb', line 5 def tool_calls @tool_calls end |
#tools ⇒ Object
Returns the value of attribute tools.
5 6 7 |
# File 'lib/oxaiworkers/module_request.rb', line 5 def tools @tools end |
Instance Method Details
#append(role: nil, content: nil, messages: nil) ⇒ Object
35 36 37 38 |
# File 'lib/oxaiworkers/module_request.rb', line 35 def append(role: nil, content: nil, messages: nil) << { role:, content: } if role.present? && content.present? += if .present? end |
#cleanup ⇒ Object
25 26 27 28 29 30 31 32 33 |
# File 'lib/oxaiworkers/module_request.rb', line 25 def cleanup @client ||= @model.client @result = nil @errors = nil = [] @tool_calls = nil @is_truncated = false # @last_call = nil end |
#initialize_requests(model:, call_stack: nil) ⇒ Object
9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 |
# File 'lib/oxaiworkers/module_request.rb', line 9 def initialize_requests(model:, call_stack: nil) @custom_id = SecureRandom.uuid @model = model @client = nil @is_truncated = false @call_stack = call_stack @last_call = nil if @model.api_key.nil? error_text = "#{@model.model} access token missing!" raise OxAiWorkers::ConfigurationError, error_text end cleanup end |
#not_found_is_ok ⇒ Object
71 72 73 74 75 |
# File 'lib/oxaiworkers/module_request.rb', line 71 def not_found_is_ok yield rescue Faraday::ResourceNotFound => e nil end |
#params ⇒ Object
40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 |
# File 'lib/oxaiworkers/module_request.rb', line 40 def params .each do || content = [:content] if content.is_a?(String) truncated = content.length > 500 ? "#{content[0...500]}..." : content OxAiWorkers.logger.warn "Request (String)[#{message[:role]}]: #{truncated}" elsif content.is_a?(Array) types = content.map do |item| text = item[:content] truncated = text && text.length > 500 ? "#{text[0...500]}..." : text "#{item[:type]}: #{truncated}" end.compact.join(', ') OxAiWorkers.logger.warn "Request (Array): [#{types}]" else OxAiWorkers.logger.warn "Request (Other): #{message.inspect}" end end filtered_functions = [] filtered_functions << @last_call if @stop_double_calls.include?(@last_call) tool_choice = (@call_stack.first if @call_stack&.any?) @model.build_parameters( messages: , tools: @tools, filtered_functions:, tool_choice: ) end |
#parse_choices(response) ⇒ Object
77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 |
# File 'lib/oxaiworkers/module_request.rb', line 77 def parse_choices(response) # Reset instance variables before processing choices @result = nil @tool_calls = [] @is_truncated = false @model.parse_response(response) do |result, is_truncated, tool_calls| @result = result @is_truncated = is_truncated @tool_calls += tool_calls end @last_call = @tool_calls.last[:name] if @tool_calls.any? # Remove the first element from call_stack only after successful tool call if @tool_calls.any? && @call_stack&.any? @call_stack = @call_stack.drop(1) end end |