ask-ai 0.0.4 → 0.0.5

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: b9aac9c44ecfb8fd4a68b1329f7dc48e37b09910011b7a1d1ad085f2afd48407
4
- data.tar.gz: c3b58592fce1e23ae7184a3b6d37d58451b8da82992c90dd503c4705a1fb94ff
3
+ metadata.gz: f43705932c791d2b495230b8ac87567a23dafddd7c76fbbdc9cd0d88d960aac7
4
+ data.tar.gz: c8376477e6a4261f04ced1c99ccc07d433551e1613c317b51ab1b21b53e50b58
5
5
  SHA512:
6
- metadata.gz: f075fc93f55659c6f8d9331f5e508dcc6ac9f77e52cf4a8e2df2d25e3e727a8bef5f8f253588a4de06b8c67f7f2704792ed62f7c7e98d4337b5df0b843f34652
7
- data.tar.gz: 4d74731c56a046231dc11bd889c37c42e591dfcaebef7e1aaba11d6a0dd64c4942e01474e67f526b2a061a1681d806befa9934f60c070d2b0ead4fa45bc75d8e
6
+ metadata.gz: 26841ece9e0ee1cb05d1df0d6cbf1b20db94d18daf74be4decf82599bf3614f5a6823a2ae3398a26d9a4df480c9eedb51285934bd674d4f3b2eac5355b617da1
7
+ data.tar.gz: 0ecfbc09c3944f62ef1cfdc3ddbef975d7b2cf3ea69913bff416ba25f6df063436bdbf7359bf40ca9b80fa4ce0d4dfc74d54d79b487f1f8c9b22365a2027c70e
data/config/config.yml CHANGED
@@ -0,0 +1,3 @@
1
+ ---
2
+ TEMPERATURE: 0.7
3
+ CONTEXT_LENGTH: 12
data/lib/config.rb ADDED
@@ -0,0 +1,65 @@
1
+ require_relative './files.rb'
2
+
3
+ module Config
4
+ include Files
5
+ def self.load_key()
6
+ config = YAML.load_file(Files.config_path)
7
+ config['OPENAI_API_KEY']
8
+ end
9
+
10
+ def self.save_key(api_key)
11
+ config = YAML.load_file(Files.config_path)
12
+ config['OPENAI_API_KEY'] = api_key
13
+ File.open(Files.config_path, 'w') { |f| YAML.dump(config, f) }
14
+ end
15
+
16
+ def self.load_temperature
17
+ config = YAML.load_file(Files.config_path)
18
+ config['TEMPERATURE']
19
+ end
20
+
21
+ def self.save_temperature(temperature)
22
+ config = YAML.load_file(Files.config_path)
23
+ config['TEMPERATURE'] = temperature.to_f
24
+ File.open(Files.config_path, 'w') { |f| YAML.dump(config, f) }
25
+ end
26
+
27
+ def self.load_context_length
28
+ config = YAML.load_file(Files.config_path)
29
+ config['CONTEXT_LENGTH']
30
+ end
31
+
32
+ def self.save_context_length(context_length)
33
+ config = YAML.load_file(Files.config_path)
34
+ config['CONTEXT_LENGTH'] = context_length.to_i
35
+ File.open(Files.config_path, 'w') { |f| YAML.dump(config, f) }
36
+ end
37
+
38
+ def self.set_config(value)
39
+ puts 'value',value
40
+ if value.include?('key')
41
+ stript_value = value.sub(/^key/, '').strip
42
+ if stript_value.empty?
43
+ puts 'No API key given'
44
+ exit
45
+ end
46
+ save_key(stript_value)
47
+ elsif value.include?('temp')
48
+ stript_value = value.sub(/^temp/, '').strip
49
+ if stript_value.empty?
50
+ puts 'No temperature given'
51
+ exit
52
+ end
53
+ save_temperature(stript_value)
54
+ elsif value.include?('context')
55
+ stript_value = value.sub(/^context/, '').strip
56
+ if stript_value.empty?
57
+ puts 'No context length given'
58
+ exit
59
+ end
60
+ save_context_length(stript_value)
61
+ else
62
+ puts 'Invalid config value'
63
+ end
64
+ end
65
+ end
data/lib/context.rb CHANGED
@@ -1,7 +1,7 @@
1
1
  require_relative './files.rb'
2
-
2
+ require_relative './config.rb'
3
3
  class Context
4
- include Files
4
+ include Files, Config
5
5
  def self.load_context()
6
6
  if File.exist?(Files.context_path)
7
7
  conversation = File.readlines(Files.context_path).map { |line| JSON.parse(line) }
@@ -32,7 +32,8 @@ class Context
32
32
  File.open(Files.context_path, "w") {}
33
33
  end
34
34
  File.readlines(Files.context_path).map { |line| tmp_arr.push(JSON.parse(line)) }
35
- if tmp_arr.length > 9
35
+ length = Config.load_context_length().nil? ? 10 : Config.load_context_length()
36
+ if tmp_arr.length > length
36
37
  tmp_arr.shift()
37
38
  end
38
39
  File.truncate(Files.context_path, 0)
@@ -46,14 +47,18 @@ class Context
46
47
  end
47
48
 
48
49
  def self.save_context_file(file_path)
50
+ puts "File path: #{file_path}"
49
51
  unless file_path.nil?
50
52
  file_in = File.open(file_path, 'r')
51
53
  file_out = File.open(Files.context_file_path, 'w')
52
54
  char_count = 0
53
55
  file_in.each do |line|
56
+ puts "Line: #{line}"
54
57
  char_count += line.length
55
58
  file_out.write(line)
56
59
  end
60
+ file_in.close
61
+ file_out.close
57
62
 
58
63
  if char_count > 10000
59
64
  Logging.log("Warning: The file you are trying to feed to the API is #{char_count} characters long. This consumes a lot of tokens.")
data/lib/help.rb CHANGED
@@ -45,4 +45,41 @@ class Help
45
45
 
46
46
  Logging.log("There are two types of options, flags and arguments.")
47
47
  end
48
+
49
+ def self.interactive_help(command)
50
+ case command
51
+ when '-w'
52
+ Logging.log("Ex: -w /home/name/sound_file.m4a")
53
+ Logging.log("Will transcribe the audio file.")
54
+ when '-t'
55
+ Logging.log("Ex: -t /home/name/sound_file.m4a")
56
+ Logging.log("Will translate the audio file to English.")
57
+ when '-lf'
58
+ Logging.log("Ex: -lf /home/name/some_text_file.txt'")
59
+ Logging.log("Will load the file into context.")
60
+ Logging.log("The file should a [txt, CSV]. More formats coming soon.")
61
+ when '-f'
62
+ Logging.log("Ex: -f Can you describe the file i provided?")
63
+ when 'config'
64
+ Logging.log("Ex: config key <your API key>")
65
+ Logging.log("Ex: config temp <0.0 - 1.0>")
66
+ Logging.log("Ex: config context <0 - 100>")
67
+ Logging.log("Beaware that the more context you use, the more expensive it will be.")
68
+ else
69
+ Logging.log("No help for: #{command}")
70
+ end
71
+
72
+ end
73
+
74
+ def self.interactive_desc()
75
+ Logging.log("Type 'exit' or 'quit' to exit.")
76
+ Logging.log("Type 'clear' to clear context.")
77
+ Logging.log("Type 'show' to show context.")
78
+ Logging.log("Type 'help' to show help.")
79
+ Logging.log("Type 'config [key, temp, context]' to change config.")
80
+ Logging.log("Type '-w <filepath>' to whisper transcribe.")
81
+ Logging.log("Type '-t' <filepath> to whisper translate.")
82
+ Logging.log("Type '-lf' <filepath> to load file.")
83
+ Logging.log("Type '-f' to use loaded file as context.")
84
+ end
48
85
  end
data/lib/main.rb CHANGED
@@ -10,15 +10,16 @@ require_relative './handle_args.rb'
10
10
  require_relative './help.rb'
11
11
  require_relative './context.rb'
12
12
  require_relative './files.rb'
13
+ require_relative './config.rb'
13
14
 
14
15
  class Main
15
- include Logging
16
- include Files
16
+ include Logging, Files, Config
17
+
17
18
  def self.run()
18
19
  config = load_env()
19
20
 
20
21
  ## This does not work. Need to fix this.
21
- if config == false
22
+ if (config == false || config.nil?)
22
23
  Logging.log("No API key found.")
23
24
  Help.display_api_key()
24
25
  Logging.log('If you want to set your API key now, type (y)es and press enter.')
@@ -28,7 +29,7 @@ class Main
28
29
  exit
29
30
  elsif input == "y" || input == "yes" || input == "Y" || input == "Yes"
30
31
  set_key()
31
- exit
32
+ break
32
33
  else
33
34
  Logging.log('Invalid input (y)es or press enter to exit.')
34
35
  end
@@ -92,14 +93,7 @@ class Main
92
93
  Logging.log(Prompt.whisper_translate(input))
93
94
  when "-i", "--interactive"
94
95
  Logging.log("Interactive mode...")
95
- Logging.log("Type 'exit' or 'quit' to exit.")
96
- Logging.log("Type 'clear' to clear context.")
97
- Logging.log("Type 'show' to show context.")
98
- Logging.log("Type 'help' to show help.")
99
- Logging.log("Type 'config' to change config.")
100
- Logging.log("Type '-w' to whisper transcribe.")
101
- Logging.log("Type '-t' to whisper translate.")
102
-
96
+ Help.interactive_desc()
103
97
  while input = Readline.readline("\n> ", true) do
104
98
  case input
105
99
  when "exit", "quit"
@@ -107,9 +101,12 @@ class Main
107
101
  when "clear"
108
102
  Logging.log("Clearing context...")
109
103
  Context.delete_context()
110
- when "help"
104
+ when 'help'
105
+ Help.interactive_desc()
106
+ when /^help/
107
+ strip_input = input.sub(/^help/, "").strip
111
108
  #TODO: This should be a specific help for interactive.
112
- Help.display_help()
109
+ Help.interactive_help(strip_input)
113
110
  when "show"
114
111
  Logging.log("\n")
115
112
  Logging.log(Context.load_context())
@@ -119,8 +116,26 @@ class Main
119
116
  when /^-t/
120
117
  stript_input = input.sub(/^-t/, "").strip
121
118
  Logging.log(Prompt.whisper_translate(stript_input, interactive: true))
122
- when "config"
123
- set_key(api_key: nil)
119
+ when /^-lf/
120
+ stript_input = input.sub(/^-lf/, "").strip
121
+ Logging.log("Loading File #{stript_input}")
122
+ Context.save_context_file(stript_input)
123
+ when /^-f/
124
+ stript_input = input.sub(/^-f/, "").strip
125
+ file_as_string = Context.load_context_file()
126
+ if file_as_string.empty?
127
+ Logging.log("No file loaded.")
128
+ next
129
+ end
130
+ Logging.log("")
131
+ Prompt.stream_prompt(stript_input, file_as_string)
132
+ Logging.log("")
133
+ when ""
134
+ Logging.log("No input given.")
135
+ when /^config/
136
+ strip_input = input.sub(/^config/, "").strip
137
+ Config.set_config(strip_input)
138
+ #set_key(api_key: nil)
124
139
  else
125
140
  #options_and_input = HandleArgs.handle_args()
126
141
  context = Context.load_context()
@@ -168,9 +183,11 @@ class Main
168
183
  f.write(YAML.dump({ "OPENAI_API_KEY" => api_key }))
169
184
  end
170
185
  Logging.log("API key saved.")
186
+ Logging.log("")
171
187
  end
172
188
 
173
189
  def self.load_env()
190
+ #Config.load_key()
174
191
  YAML.load(File.read(Files.config_path))
175
192
 
176
193
  rescue Errno::ENOENT
data/lib/prompt.rb CHANGED
@@ -1,10 +1,15 @@
1
1
  require "openai"
2
2
  require_relative './files.rb'
3
+ require_relative './config.rb'
3
4
 
4
5
  class Prompt
5
6
  include Files
7
+ include Config
6
8
  ## Streams the response, VERY NICE
7
- def self.stream_prompt(input, conversation = '', temp = 0.7)
9
+ def self.stream_prompt(input, conversation = '', temp = Config.load_temperature())
10
+ if temp.nil?
11
+ temp = 0.7
12
+ end
8
13
  if conversation.length == 0
9
14
  conversation += input
10
15
  else
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: 0.0.4
4
+ version: 0.0.5
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-03 00:00:00.000000000 Z
11
+ date: 2023-10-04 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: ruby-openai
@@ -36,13 +36,13 @@ files:
36
36
  - config/config.yml
37
37
  - files/context.jsonl
38
38
  - files/context_file.txt
39
+ - lib/config.rb
39
40
  - lib/context.rb
40
41
  - lib/files.rb
41
42
  - lib/handle_args.rb
42
43
  - lib/help.rb
43
44
  - lib/logging.rb
44
45
  - lib/main.rb
45
- - lib/paths.rb
46
46
  - lib/prompt.rb
47
47
  homepage: https://github.com/OskarFranck/terminal_chat
48
48
  licenses:
@@ -63,7 +63,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
63
63
  - !ruby/object:Gem::Version
64
64
  version: '0'
65
65
  requirements: []
66
- rubygems_version: 3.2.33
66
+ rubygems_version: 3.3.7
67
67
  signing_key:
68
68
  specification_version: 4
69
69
  summary: A simple CLI for OpenAI's GPT-3 API.
data/lib/paths.rb DELETED
@@ -1,26 +0,0 @@
1
-
2
- module Files
3
- def self.root
4
- File.expand_path("./../", __dir__)
5
- end
6
-
7
- def self.context
8
- File.expand_path("./../files/context.jsonl", __dir__)
9
- end
10
-
11
- def self.files
12
- FILE_PATH
13
- end
14
-
15
- def self.config
16
- CONFIG_PATH
17
- end
18
-
19
- def self.context_file
20
- CONTEXT_FILE_PATH
21
- end
22
- end
23
- CONTEXT_PATH = File.expand_path("./../files/context.jsonl", __dir__)
24
- FILE_PATH = File.expand_path("./../files/", __dir__)
25
- CONFIG_PATH = File.expand_path("./../config/config.yml", __dir__)
26
- CONTEXT_FILE_PATH = File.expand_path("./../files/context_file.txt", __dir__)