Module: Config

Extended by:
Files
Included in:
Context, Main, Prompt
Defined in:
lib/config.rb

Instance Method Summary collapse

Methods included from Files

config_path, context_file_path, context_path, file_path, root

Instance Method Details

#load_context_lengthObject



35
36
37
38
39
40
# File 'lib/config.rb', line 35

def load_context_length
  config = YAML.load_file(config_path)
  unless (config == false || config.nil?)
    config['CONTEXT_LENGTH']
  end
end

#load_envObject



103
104
105
106
107
108
109
# File 'lib/config.rb', line 103

def load_env()
  #Config.load_key()
  YAML.load(File.read(config_path))

rescue Errno::ENOENT
  log("No config.yml found.")
end

#load_keyObject



5
6
7
8
# File 'lib/config.rb', line 5

def load_key()
  config = YAML.load_file(config_path)
  config['OPENAI_API_KEY']
end

#load_temperatureObject



19
20
21
22
23
24
# File 'lib/config.rb', line 19

def load_temperature
  config = YAML.load_file(config_path)
  unless (config == false || config.nil?)
    config['TEMPERATURE']
  end
end

#save_context_length(context_length) ⇒ Object



42
43
44
45
46
47
48
49
# File 'lib/config.rb', line 42

def save_context_length(context_length)
  config = YAML.load_file(config_path)
  if config == false
    config = {}
  end
  config['CONTEXT_LENGTH'] = context_length.to_i
  File.open(config_path, 'w') { |f| YAML.dump(config, f) }
end

#save_key(api_key) ⇒ Object



10
11
12
13
14
15
16
17
# File 'lib/config.rb', line 10

def save_key(api_key)
  config = YAML.load_file(config_path)
  if (config == false || config.nil?)
    config = {}
  end
  config['OPENAI_API_KEY'] = api_key
  File.open(config_path, 'w') { |f| YAML.dump(config, f) }
end

#save_temperature(temperature) ⇒ Object



26
27
28
29
30
31
32
33
# File 'lib/config.rb', line 26

def save_temperature(temperature)
  config = YAML.load_file(config_path)
  if config == false
    config = {}
  end
  config['TEMPERATURE'] = temperature.to_f
  File.open(config_path, 'w') { |f| YAML.dump(config, f) }
end

#set_config(value) ⇒ Object



51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
# File 'lib/config.rb', line 51

def set_config(value)
  if value.include?('key')
    stript_value = value.sub(/^key/, '').strip
    if stript_value.empty?
      return 'No API key given'
    end
    save_key(stript_value)
    return 'API key saved'
  elsif value.include?('temp')
    stript_value = value.sub(/^temp/, '').strip.to_f
    if stript_value.to_f > 1.0 || stript_value.to_f < 0.1
      return 'Temperature must be between 0.1 and 1.0'
    end
    save_temperature(stript_value)
    return 'Temperature saved'
  elsif value.include?('context')
    stript_value = value.sub(/^context/, '').strip.to_i
    if stript_value.to_i > 100 || stript_value.to_i < 1
      return 'Context length must be between 1 and 100'
    end
    save_context_length(stript_value)
    return 'Context length saved'
  else
    return 'Invalid config value'
  end
end

#set_key(api_key: nil) ⇒ Object



78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
# File 'lib/config.rb', line 78

def set_key(api_key: nil)
  if api_key.nil?
    log("Setting API key...")
    log("Enter API key: (or press enter to exit)")

    while input = Readline.readline("> ", true) do
      if input.empty?
        log("Exiting.")
        exit
      else
        api_key = input.strip
        break
      end
    end
    log("Saving API key...")
  end

  FileUtils.mkdir_p(File.dirname(config_path))
  File.open(config_path, "w") do |f|
    f.write(YAML.dump({ "OPENAI_API_KEY" => api_key }))
  end
  log("API key saved.")
  log("")
end