Class: OxAiWorkers::ModuleRequest
- Inherits:
-
Object
- Object
- OxAiWorkers::ModuleRequest
- Defined in:
- lib/oxaiworkers/module_request.rb
Direct Known Subclasses
Instance Attribute Summary collapse
-
#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.
-
#max_tokens ⇒ Object
Returns the value of attribute max_tokens.
-
#messages ⇒ Object
Returns the value of attribute messages.
-
#model ⇒ Object
Returns the value of attribute model.
-
#result ⇒ Object
Returns the value of attribute result.
-
#temperature ⇒ Object
Returns the value of attribute temperature.
-
#tool_calls ⇒ Object
Returns the value of attribute tool_calls.
-
#tool_calls_raw ⇒ Object
Returns the value of attribute tool_calls_raw.
-
#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: nil, max_tokens: nil, temperature: nil) ⇒ Object
- #not_found_is_ok ⇒ Object
- #params ⇒ Object
- #parse_choices(response) ⇒ Object
Instance Attribute Details
#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 |
#max_tokens ⇒ Object
Returns the value of attribute max_tokens.
5 6 7 |
# File 'lib/oxaiworkers/module_request.rb', line 5 def max_tokens @max_tokens end |
#messages ⇒ Object
Returns the value of attribute messages.
5 6 7 |
# File 'lib/oxaiworkers/module_request.rb', line 5 def @messages 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 |
#temperature ⇒ Object
Returns the value of attribute temperature.
5 6 7 |
# File 'lib/oxaiworkers/module_request.rb', line 5 def temperature @temperature 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 |
#tool_calls_raw ⇒ Object
Returns the value of attribute tool_calls_raw.
5 6 7 |
# File 'lib/oxaiworkers/module_request.rb', line 5 def tool_calls_raw @tool_calls_raw 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
36 37 38 39 |
# File 'lib/oxaiworkers/module_request.rb', line 36 def append(role: nil, content: nil, messages: nil) @messages << { role:, content: } if role.present? and content.present? @messages += if .present? end |
#cleanup ⇒ Object
24 25 26 27 28 29 30 31 32 33 34 |
# File 'lib/oxaiworkers/module_request.rb', line 24 def cleanup @client ||= OpenAI::Client.new( access_token: OxAiWorkers.configuration.access_token, log_errors: true # Highly recommended in development, so you can see what errors OpenAI is returning. Not recommended in production because it could leak private data to your logs. ) @result = nil @errors = nil @messages = [] @tool_calls = nil @tool_calls_raw = nil end |
#initialize_requests(model: nil, max_tokens: nil, temperature: nil) ⇒ Object
8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 |
# File 'lib/oxaiworkers/module_request.rb', line 8 def initialize_requests(model: nil, max_tokens: nil, temperature: nil) @max_tokens = max_tokens || OxAiWorkers.configuration.max_tokens @custom_id = SecureRandom.uuid @model = model || OxAiWorkers.configuration.model @temperature = temperature || OxAiWorkers.configuration.temperature @client = nil OxAiWorkers.configuration.access_token ||= ENV['OPENAI'] if OxAiWorkers.configuration.access_token.nil? error_text = 'OpenAi access token missing!' raise OxAiWorkers::ConfigurationError, error_text end cleanup end |
#not_found_is_ok ⇒ Object
55 56 57 58 59 |
# File 'lib/oxaiworkers/module_request.rb', line 55 def not_found_is_ok yield rescue Faraday::ResourceNotFound => e nil end |
#params ⇒ Object
41 42 43 44 45 46 47 48 49 50 51 52 53 |
# File 'lib/oxaiworkers/module_request.rb', line 41 def params parameters = { model: @model, messages: @messages, temperature: @temperature, max_tokens: @max_tokens } if @tools.present? parameters[:tools] = @tools parameters[:tool_choice] = 'required' end parameters end |
#parse_choices(response) ⇒ Object
61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 |
# File 'lib/oxaiworkers/module_request.rb', line 61 def parse_choices(response) @tool_calls = [] @result = response.dig('choices', 0, 'message', 'content') @tool_calls_raw = response.dig('choices', 0, 'message', 'tool_calls') @tool_calls_raw.each do |tool| function = tool['function'] args = JSON.parse(YAML.load(function['arguments']).to_json, { symbolize_names: true }) @tool_calls << { class: function['name'].split('__').first, name: function['name'].split('__').last, args: } end @tool_calls end |