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



50
51
52
53
# File 'lib/context.rb', line 50

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

.delete_file_contextObject

This first class pasta method need to be refactored. It’s a mess.



102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
# File 'lib/context.rb', line 102

def self.delete_file_context()
  counter = 1
  delete_lines = []
  last_line = 0
  File.open(context_file_path, "r") { |file|
    if file.size == 0
      log("No files loaded.")
      return
    end
    file.readlines.each_with_index { |line, index|
      if line.include?("loaded_context_file_path=")
        log("#{counter}: #{line.gsub("loaded_context_file_path=", "")}")
        delete_lines.push(index)
        counter += 1
      end
      last_line = index
    }
  }
  if counter == 1 + 1
    log "One file loaded. Enter '1' or 'all' to delete it."
    log "Enter 'a' to abort."
  else
    log("Which file do you want to delete? (1-#{counter - 1}) or 'all'")
    log("Enter 'a' to abort.")
  end

  delete_counter = 0
  while input = Readline.readline("\nDelete --> ", true) do
    if input == "a"
      log("Aborting.")
      return
    elsif input == "all"
      File.truncate(context_file_path, 0)
      log("Deleted all files.")
      return
    elsif input.to_i >= 1 && input.to_i < counter
      lines_to_save = []
      File.open(context_file_path, "r") { |file|
        file.readlines.each_with_index { |line, index|
          start_line = delete_lines[input.to_i - 1]
          end_line = delete_lines[input.to_i]
          if end_line.nil?
            end_line = last_line + 1
          end
          if index < start_line || index > end_line - 1
            lines_to_save.push(line)
          end
        }
      }
      File.truncate(context_file_path, 0)
      File.open(context_file_path, "a") { |file|
        lines_to_save.each { |line|
          file.write(line)
        }
      }
      log("Deleting file")
      return
    elsif input.to_i <= 0 || input.to_i > counter
      log("Please enter a number between 1 and #{counter - 1}")
      log("Enter 'a' to abort.")
    end
  end
end

.load_context(file_with_context: false) ⇒ Object



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

def self.load_context(file_with_context: false)
  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

  if file_with_context
    return load_context_file() + context_as_string
  end

  return context_as_string
end

.load_context_fileObject



87
88
89
90
91
92
93
94
95
96
97
98
99
# File 'lib/context.rb', line 87

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.



34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
# File 'lib/context.rb', line 34

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



55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
# File 'lib/context.rb', line 55

def self.save_context_file(file_path)
  ## If the file extenstion is pdf or docx raise an error.
  ## This is not a complete list of file extensions.
  if file_path.include?(".pdf") || file_path.include?(".docx")
    raise FileFormatError, "File type not supported."
  end
  unless file_path.nil?
    conter = 0
    file_in = File.open(file_path, 'r')
    file_out = File.open(context_file_path, 'a')
    file_out.write("loaded_context_file_path=#{file_path}\n")
    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.")
rescue FileFormatError => e
  log(e.message)
end