ask-ai 1.0.0 → 1.0.1

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 3d0033fda00a5a8399a6e9b71c75338b02269df54edb791bd82215e939492d15
4
- data.tar.gz: 391d4cc9fe240289358f2489b61224b8d0706254174f9ad4a5ec7a109b7b3a89
3
+ metadata.gz: e73628fad8025e8de63e07c4256f42426eb9651fb62a418f8f73c6b7b4db432d
4
+ data.tar.gz: 4575f5535ddedd005e846cf598c3f464717130b2ce065b7c29e2e174cadebd30
5
5
  SHA512:
6
- metadata.gz: 6f749624e465e35a8ebcae8b7b9b312d1f4ffdbd8e390043a0fc87dae5eaf680931975eb87369eba855043eff52206c2daf47af95fc5136e7fb0134520275a28
7
- data.tar.gz: 81d6f56f465808352087d5f53bab44b7a1af2d63292dbba4b5fab7f91b44fd3d78c6a591718d142f6348c6ab78bae8a54d84b2cba73e2164efd32dd3b27d4e1d
6
+ metadata.gz: a2fccb29fba0174e10556377b2422fb6d3de99eef58820af2289ef77dd0379029c5d989d745eb388ef0901c153de2ce22163a9fb8b11d1f133d84e0d3bdb6b62
7
+ data.tar.gz: 45bc0ea0ef5e78a465b196fe262a2b1b88cdeb2bd9b15af86300d72ac2cf220c4b1ebeb860b41782e02bad51dc87d34e32a7522c1ff3129bc1902995c0dfcebf
data/lib/config.rb CHANGED
@@ -9,7 +9,7 @@ module Config
9
9
 
10
10
  def save_key(api_key)
11
11
  config = YAML.load_file(config_path)
12
- if config == false
12
+ if (config == false || config.nil?)
13
13
  config = {}
14
14
  end
15
15
  config['OPENAI_API_KEY'] = api_key
@@ -18,7 +18,7 @@ module Config
18
18
 
19
19
  def load_temperature
20
20
  config = YAML.load_file(config_path)
21
- unless config == false
21
+ unless (config == false || config.nil?)
22
22
  config['TEMPERATURE']
23
23
  end
24
24
  end
@@ -34,7 +34,7 @@ module Config
34
34
 
35
35
  def load_context_length
36
36
  config = YAML.load_file(config_path)
37
- unless config == false
37
+ unless (config == false || config.nil?)
38
38
  config['CONTEXT_LENGTH']
39
39
  end
40
40
  end
data/lib/context.rb CHANGED
@@ -1,8 +1,9 @@
1
1
  require_relative './files.rb'
2
2
  require_relative './config.rb'
3
+ require_relative './file_format_error.rb'
3
4
  class Context
4
5
  extend Files, Config, Logging
5
- def self.load_context()
6
+ def self.load_context(file_with_context: false)
6
7
  if File.exist?(context_path)
7
8
  conversation = File.readlines(context_path).map { |line| JSON.parse(line) }
8
9
  else
@@ -21,6 +22,10 @@ class Context
21
22
  end
22
23
  end
23
24
 
25
+ if file_with_context
26
+ return load_context_file() + context_as_string
27
+ end
28
+
24
29
  return context_as_string
25
30
  end
26
31
 
@@ -48,12 +53,19 @@ class Context
48
53
  end
49
54
 
50
55
  def self.save_context_file(file_path)
56
+ ## If the file extenstion is pdf or docx raise an error.
57
+ ## This is not a complete list of file extensions.
58
+ if file_path.include?(".pdf") || file_path.include?(".docx")
59
+ raise FileFormatError, "File type not supported."
60
+ end
51
61
  unless file_path.nil?
62
+ conter = 0
52
63
  file_in = File.open(file_path, 'r')
53
- file_out = File.open(context_file_path, 'w')
64
+ file_out = File.open(context_file_path, 'a')
65
+ file_out.write("loaded_context_file_path=#{file_path}\n")
54
66
  char_count = 0
55
67
  file_in.each do |line|
56
- puts "Line: #{line}"
68
+ #puts "Line: #{line}"
57
69
  char_count += line.length
58
70
  file_out.write(line)
59
71
  end
@@ -68,6 +80,8 @@ class Context
68
80
  end
69
81
  rescue Errno::ENOENT
70
82
  log("No file at '#{file_path}' found.")
83
+ rescue FileFormatError => e
84
+ log(e.message)
71
85
  end
72
86
 
73
87
  def self.load_context_file()
@@ -83,5 +97,70 @@ class Context
83
97
  log("Load a file with 'aa -lf <file_path>'")
84
98
  return ""
85
99
  end
100
+ ## This first class pasta method need to be refactored.
101
+ ## It's a mess.
102
+ def self.delete_file_context()
103
+ counter = 1
104
+ delete_lines = []
105
+ last_line = 0
106
+ File.open(context_file_path, "r") { |file|
107
+ if file.size == 0
108
+ log("No files loaded.")
109
+ return
110
+ end
111
+ file.readlines.each_with_index { |line, index|
112
+ if line.include?("loaded_context_file_path=")
113
+ log("#{counter}: #{line.gsub("loaded_context_file_path=", "")}")
114
+ delete_lines.push(index)
115
+ counter += 1
116
+ end
117
+ last_line = index
118
+ }
119
+ }
120
+ if counter == 1 + 1
121
+ log "One file loaded. Enter '1' or 'all' to delete it."
122
+ log "Enter 'a' to abort."
123
+ else
124
+ log("Which file do you want to delete? (1-#{counter - 1}) or 'all'")
125
+ log("Enter 'a' to abort.")
126
+ end
127
+
128
+ delete_counter = 0
129
+ while input = Readline.readline("\nDelete --> ", true) do
130
+ if input == "a"
131
+ log("Aborting.")
132
+ return
133
+ elsif input == "all"
134
+ File.truncate(context_file_path, 0)
135
+ log("Deleted all files.")
136
+ return
137
+ elsif input.to_i >= 1 && input.to_i < counter
138
+ lines_to_save = []
139
+ File.open(context_file_path, "r") { |file|
140
+ file.readlines.each_with_index { |line, index|
141
+ start_line = delete_lines[input.to_i - 1]
142
+ end_line = delete_lines[input.to_i]
143
+ if end_line.nil?
144
+ end_line = last_line + 1
145
+ end
146
+ if index < start_line || index > end_line - 1
147
+ lines_to_save.push(line)
148
+ end
149
+ }
150
+ }
151
+ File.truncate(context_file_path, 0)
152
+ File.open(context_file_path, "a") { |file|
153
+ lines_to_save.each { |line|
154
+ file.write(line)
155
+ }
156
+ }
157
+ log("Deleting file")
158
+ return
159
+ elsif input.to_i <= 0 || input.to_i > counter
160
+ log("Please enter a number between 1 and #{counter - 1}")
161
+ log("Enter 'a' to abort.")
162
+ end
163
+ end
164
+ end
86
165
 
87
166
  end
@@ -0,0 +1,7 @@
1
+ class FileFormatError < StandardError
2
+
3
+ def initialize(msg="File format not supported")
4
+ super
5
+ end
6
+
7
+ end
data/lib/help.rb CHANGED
@@ -44,6 +44,7 @@ class Help
44
44
  log("Type '-w <filepath>' to whisper transcribe.")
45
45
  log("Type '-t' <filepath> to whisper translate.")
46
46
  log("Type '-lf' <filepath> to load file.")
47
+ log "Type '-df' to delete file context."
47
48
  log("Type '-f' to use loaded file as context.")
48
49
  end
49
50
  end
data/lib/main.rb CHANGED
@@ -15,7 +15,7 @@ class Main
15
15
  extend Logging, Files, Config
16
16
  LIST = [
17
17
  "exit", "quit", "version", "clear", "help", "show",
18
- "-w", "-t", "-lf", "-f", "config", "temp", "context"
18
+ "-w", "-t", "-lf", "-f", "-df", "config", "temp", "context"
19
19
  ].sort
20
20
 
21
21
  def self.run()
@@ -27,6 +27,21 @@ class Main
27
27
  #Readline.completion_append_character = ""
28
28
  #Readline.completion_proc = comp
29
29
 
30
+ ## Function that when called activates completion from LIST
31
+ # def self.list_auto_complete()
32
+ # Readline.completion_append_character = ""
33
+ # Readline.completion_proc = proc do |s|
34
+ # if s.start_with?("-w", "-t", "-lf", "-f")
35
+ # pattern = s.sub(/^-w/, "") + "*"
36
+ # Dir.glob(pattern).grep(/^#{Regexp.escape('')}/)
37
+ # else
38
+ # LIST.grep(/^#{Regexp.escape(s)}/)
39
+ # end
40
+ # end
41
+ # end
42
+
43
+ # list_auto_complete()
44
+
30
45
  Help.interactive_desc()
31
46
  while input = Readline.readline("\n> ", true) do
32
47
  case input
@@ -55,6 +70,8 @@ class Main
55
70
  stript_input = input.sub(/^-lf/, "").strip
56
71
  log("Loading File #{stript_input}")
57
72
  Context.save_context_file(stript_input)
73
+ when /^-df/
74
+ Context.delete_file_context()
58
75
  when /^-f/
59
76
  stript_input = input.sub(/^-f/, "").strip
60
77
  file_as_string = Context.load_context_file()
@@ -62,8 +79,9 @@ class Main
62
79
  log("No file loaded.")
63
80
  next
64
81
  end
82
+ context = Context.load_context(file_with_context: true)
65
83
  log("")
66
- Prompt.stream_prompt(stript_input, file_as_string)
84
+ Context.save_context(Prompt.stream_prompt(stript_input, context))
67
85
  log("")
68
86
  when ""
69
87
  log("No input given.")
data/lib/prompt.rb CHANGED
@@ -20,7 +20,7 @@ class Prompt
20
20
  parameters: {
21
21
  model: "gpt-3.5-turbo",
22
22
  messages: [{ role: "user", content: conversation}],
23
- temperature: temp, ## Should be a parameter
23
+ temperature: temp,
24
24
  stream: proc do |chunk, _bytesize|
25
25
  response += chunk.dig("choices", 0, "delta", "content") unless chunk.dig("choices", 0, "delta", "content").nil?
26
26
  print chunk.dig("choices", 0, "delta", "content")
@@ -119,7 +119,7 @@ class Prompt
119
119
 
120
120
  def self.client()
121
121
  conf = YAML.load(File.read(config_path))
122
- unless conf == false
122
+ unless (conf == false || conf.nil?)
123
123
  key = conf["OPENAI_API_KEY"]
124
124
  end
125
125
 
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: ask-ai
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.0.0
4
+ version: 1.0.1
5
5
  platform: ruby
6
6
  authors:
7
7
  - Oskar Franck
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2023-10-08 00:00:00.000000000 Z
11
+ date: 2023-10-25 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: ruby-openai
@@ -38,6 +38,7 @@ files:
38
38
  - files/context_file.txt
39
39
  - lib/config.rb
40
40
  - lib/context.rb
41
+ - lib/file_format_error.rb
41
42
  - lib/files.rb
42
43
  - lib/help.rb
43
44
  - lib/logging.rb
@@ -62,7 +63,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
62
63
  - !ruby/object:Gem::Version
63
64
  version: '0'
64
65
  requirements: []
65
- rubygems_version: 3.2.33
66
+ rubygems_version: 3.3.7
66
67
  signing_key:
67
68
  specification_version: 4
68
69
  summary: A simple CLI for OpenAI's GPT-3 API.