Class: Context
Class Method Summary
collapse
Methods included from Files
config_path, context_file_path, context_path, file_path, root
Methods included from Config
load_context_length, load_key, load_temperature, save_context_length, save_key, save_temperature, set_config
Class Method Details
.delete_context ⇒ Object
44
45
46
47
|
# File 'lib/context.rb', line 44
def self.delete_context()
Logging.log("Deleting previous context.")
File.truncate(Files.context_path, 0)
end
|
.load_context ⇒ Object
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?(Files.context_path)
conversation = File.readlines(Files.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_file ⇒ Object
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(Files.context_file_path, 'r')
file_as_string = ""
file.each do |line|
file_as_string += line
end
return file_as_string
rescue Errno::ENOENT
Logging.log("No file at '#{Files.context_file_path}' found.")
Logging.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.
.save_context_file(file_path) ⇒ Object
49
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 49
def self.save_context_file(file_path)
puts "File path: #{file_path}"
unless file_path.nil?
file_in = File.open(file_path, 'r')
file_out = File.open(Files.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
Logging.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
Logging.log("No file path given.")
end
rescue Errno::ENOENT
Logging.log("No file at '#{file_path}' found.")
end
|