Class: Context

Inherits:
Object
  • Object
show all
Extended by:
Config, Files, Logging
Defined in:
lib/context.rb

Class Method Summary collapse

Methods included from Logging

log

Methods included from Config

load_context_length, load_env, load_key, load_temperature, save_context_length, save_key, save_temperature, set_config, set_key

Methods included from Files

config_path, context_file_path, context_path, file_path, root

Class Method Details

.delete_contextObject



45
46
47
48
# File 'lib/context.rb', line 45

def self.delete_context()
  log("Deleting previous context.")
  File.truncate(context_path, 0)
end

.load_contextObject



5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
# File 'lib/context.rb', line 5

def self.load_context()
  if File.exist?(context_path)
    conversation = File.readlines(context_path).map { |line| JSON.parse(line) }
  else
    conversation = []
  end

  if conversation.length > 0
    context_as_string = "This is our previous conversation:\n"
  else
    context_as_string = ""
  end

  if conversation.length > 0
    conversation.each_with_index do |v, i|
      context_as_string += "My #{i + 1} input was: #{v['input']}\nYour #{i + 1} response was: #{v['response']}\n"
    end
  end

  return context_as_string
end

.load_context_fileObject



73
74
75
76
77
78
79
80
81
82
83
84
85
# File 'lib/context.rb', line 73

def self.load_context_file()
  file = File.open(context_file_path, 'r')
  file_as_string = ""
  file.each do |line|
    file_as_string += line
  end

  return file_as_string
rescue Errno::ENOENT
  log("No file at '#{context_file_path}' found.")
  log("Load a file with 'aa -lf <file_path>'")
  return ""
end

.save_context(context) ⇒ Object

Here we save the context to a file. Max 10 previous Q / A to save tokens.



29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
# File 'lib/context.rb', line 29

def self.save_context(context)
  return if context.nil?
  tmp_arr = []
  unless File.exist?(context_path)
    File.open(context_path, "w") {}
  end
  File.readlines(context_path).map { |line| tmp_arr.push(JSON.parse(line)) }
  length = load_context_length().nil? ? 10 : load_context_length()
  if tmp_arr.length > length
    tmp_arr.shift()
  end
  File.truncate(context_path, 0)
  tmp_arr.each { |line| File.open(context_path, "a") { |file| file.write("#{line.to_json}\n") } }
  File.open(context_path, "a") { |file| file.write("#{context.to_json}\n") }
end

.save_context_file(file_path) ⇒ Object



50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
# File 'lib/context.rb', line 50

def self.save_context_file(file_path)
  unless file_path.nil?
    file_in = File.open(file_path, 'r')
    file_out = File.open(context_file_path, 'w')
    char_count = 0
    file_in.each do |line|
      puts "Line: #{line}"
      char_count += line.length
      file_out.write(line)
    end
    file_in.close
    file_out.close

    if char_count > 10000
      log("Warning: The file you are trying to feed to the API is #{char_count} characters long. This consumes a lot of tokens.")
    end
  else
    log("No file path given.")
  end
rescue Errno::ENOENT
  log("No file at '#{file_path}' found.")
end