Module: GreatAxe::AiSupport::OpenAiHelper

Defined in:
lib/great_axe/ai_support/open_ai_helper.rb

Class Method Summary collapse

Class Method Details

.clientObject



8
9
10
# File 'lib/great_axe/ai_support/open_ai_helper.rb', line 8

def client
  @client ||= create_client
end

.configure_clientObject



17
18
19
20
21
22
# File 'lib/great_axe/ai_support/open_ai_helper.rb', line 17

def configure_client
  OpenAI.configure do |config|
    config.access_token = ENV.fetch('OPENAI_ACCESS_TOKEN')
    config.organization_id = ENV.fetch('OPENAI_ORGANIZATION_ID', nil)
  end
end

.create_clientObject



12
13
14
15
# File 'lib/great_axe/ai_support/open_ai_helper.rb', line 12

def create_client
  configure_client
  OpenAI::Client.new
end

.create_file(options) ⇒ Object



33
34
35
36
# File 'lib/great_axe/ai_support/open_ai_helper.rb', line 33

def create_file(options)
  path, request, choice = options.values_at(:path, :request, :choice)
  File.write(path, output(request: request, choice: choice))
end

.edit(options) ⇒ Object



51
52
53
54
55
56
57
58
59
60
# File 'lib/great_axe/ai_support/open_ai_helper.rb', line 51

def edit(options)
  content, request = options.values_at(:content, :request)
  client.edits(
    parameters: {
      model: @model,
      input: content,
      instruction: request
    }
  )
end

.edit_file(options) ⇒ Object



44
45
46
47
48
49
# File 'lib/great_axe/ai_support/open_ai_helper.rb', line 44

def edit_file(options)
  path, request, choice = options.values_at(:path, :request, :choice)
  content = File.read(path)
  response = edit(content: content, request: request)
  File.write(path, extract_text(response, 'choices', choice, 'text'))
end

.input(request) ⇒ Object



24
25
26
27
28
29
30
31
# File 'lib/great_axe/ai_support/open_ai_helper.rb', line 24

def input(request)
  client.chat(
    parameters: {
      model: model,
      messages: [{ role: role, content: request }],
      temperature: temperature
    })
end

.list_modelsObject



69
70
71
# File 'lib/great_axe/ai_support/open_ai_helper.rb', line 69

def list_models
  client.models.list
end

.model_info(model) ⇒ Object



73
74
75
# File 'lib/great_axe/ai_support/open_ai_helper.rb', line 73

def model_info(model)
  client.models.retrieve(id: model)
end

.output(options) ⇒ Object



38
39
40
41
42
# File 'lib/great_axe/ai_support/open_ai_helper.rb', line 38

def output(options)
  request, choice = options.values_at(:request, :choice)
  choice ||= 0
  extract_text(input(request), 'choices', choice, 'message', 'content')
end

.update_setting(key, value) ⇒ Object



62
63
64
65
66
67
# File 'lib/great_axe/ai_support/open_ai_helper.rb', line 62

def update_setting(key, value)
  raise 'Invalid key' unless %w[model temperature role].include?(key)

  load_settings[key] = value
  overwrite_settings
end