Class: OxAiWorkers::ModuleRequest

Inherits:
Object
  • Object
show all
Defined in:
lib/oxaiworkers/module_request.rb

Direct Known Subclasses

Request, StateBatch

Instance Attribute Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#clientObject

Returns the value of attribute client.



5
6
7
# File 'lib/oxaiworkers/module_request.rb', line 5

def client
  @client
end

#custom_idObject

Returns the value of attribute custom_id.



5
6
7
# File 'lib/oxaiworkers/module_request.rb', line 5

def custom_id
  @custom_id
end

#errorsObject

Returns the value of attribute errors.



5
6
7
# File 'lib/oxaiworkers/module_request.rb', line 5

def errors
  @errors
end

#max_tokensObject

Returns the value of attribute max_tokens.



5
6
7
# File 'lib/oxaiworkers/module_request.rb', line 5

def max_tokens
  @max_tokens
end

#messagesObject

Returns the value of attribute messages.



5
6
7
# File 'lib/oxaiworkers/module_request.rb', line 5

def messages
  @messages
end

#modelObject

Returns the value of attribute model.



5
6
7
# File 'lib/oxaiworkers/module_request.rb', line 5

def model
  @model
end

#resultObject

Returns the value of attribute result.



5
6
7
# File 'lib/oxaiworkers/module_request.rb', line 5

def result
  @result
end

#temperatureObject

Returns the value of attribute temperature.



5
6
7
# File 'lib/oxaiworkers/module_request.rb', line 5

def temperature
  @temperature
end

#tool_callsObject

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_rawObject

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

#toolsObject

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 += messages if messages.present?
end

#cleanupObject



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_okObject



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

#paramsObject



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