chatgpt-rb 0.1.4 → 0.1.6

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: ccefcb04218cda4636349cd9576030b42c7106d0d00fd9aadbbd2bc508678c0a
4
- data.tar.gz: 10044ebaa750c3a1dae7a9b484abb7ad5d4ef9e6a9a9797a51286f57f077e2d5
3
+ metadata.gz: 9c3a20f294a53407477460d0bcf1efc53524a4986cf1129c365f597a93766adc
4
+ data.tar.gz: 75999a3494afb6b8726a1510c6e16ec29e8dc55015212175befef7f5fac3a766
5
5
  SHA512:
6
- metadata.gz: 41d2d11e8eeedfc065483d19c19c495d480954c99a98ac3256ec0a76a079562ab9f8524f654aa2fc238c4f867e02a8d1f5520f34ee9b510d5f97075f17e27c4e
7
- data.tar.gz: 9dff14b6052aae029620e5d7e1f75ada65cf91418dcaada97c5231f799f183f90c851ea706bd4b76b681e4202e05b6bf302bef1aee84f92a98d2546756d5a930
6
+ metadata.gz: 5a4aaa0890191fdd4c4b5221c49a3ce094e4d69f0ffd699207fa8987515fb4e12dd6ffcb349cd2015205e99ee65660b57c72f7c8c05e6a471703d557f50dbcaa
7
+ data.tar.gz: 409b9eb640141bdd46ebbaf8b131c43b8f72d6e1e3b4da75c0f729ff9cf5bba1cb2b87a2536ddcf4fc906f6008f360a8694a5029ba57f43d55263f08783e10c6
data/bin/chatgpt-rb CHANGED
@@ -6,16 +6,12 @@ require "reline"
6
6
  require "optparse"
7
7
  require_relative "./../lib/chatgpt_rb"
8
8
 
9
- begin
10
- stty_save = `stty -g`.chomp
11
- rescue
12
- end
13
-
14
9
  options = {
15
10
  key: ENV["OPEN_AI_KEY"],
16
11
  model: "gpt-3.5-turbo",
17
12
  base_uri: "https://api.openai.com/v1",
18
13
  functions_files: [],
14
+ temperature: 0.7,
19
15
  }
20
16
 
21
17
  OptionParser.new do |opts|
@@ -44,9 +40,15 @@ OptionParser.new do |opts|
44
40
  opts.on("-p", "--prompt PROMPT", "Declare the PROMPT for your conversation") do |prompt|
45
41
  options[:prompt] = prompt
46
42
  end
43
+
44
+ opts.on("-t", "--temperature TEMPERATURE", "Set the temperature for the conversation") do |temperature|
45
+ options[:temperature] = temperature.to_f
46
+ end
47
47
  end.parse!
48
48
 
49
49
  begin
50
+ stty_save = `stty -g`.chomp
51
+
50
52
  puts "Type any message to talk with ChatGPT. Type '\\help' for a list of commands."
51
53
 
52
54
  functions = options[:functions_files].flat_map do |function_file|
@@ -64,32 +66,59 @@ begin
64
66
  end
65
67
 
66
68
  if options[:prompt]
69
+ options[:prompt] = File.exist?(options[:prompt]) ? File.read(options[:prompt]) : options[:prompt]
67
70
  puts "prompt> ".colorize(:blue) + options[:prompt]
68
71
  end
69
72
 
70
- conversation = ChatgptRb::Conversation.new(api_key: options.fetch(:key), model: options.fetch(:model), base_uri: options.fetch(:base_uri), messages:, functions:, prompt: options[:prompt])
73
+ conversation = ChatgptRb::Conversation.new(api_key: options.fetch(:key), model: options.fetch(:model), base_uri: options.fetch(:base_uri), messages:, functions:, prompt: options[:prompt], temperature: options.fetch(:temperature))
74
+
75
+ commands = [
76
+ {
77
+ names: ["s", "save"],
78
+ description: "Save this conversation to a JSON file that can be reloaded later with the `-f` argument",
79
+ implementation: ->() {
80
+ filename = /^\\save (.+)/.match(message)[1]
81
+ File.open(filename, "w") { |f| f.write(conversation.messages.to_json) }
82
+ puts "saved to #{filename} ".colorize(:blue)
83
+ }
84
+ },
85
+ {
86
+ names: ["q", "quit", "exit"],
87
+ description: "Exit the program",
88
+ implementation: ->() {
89
+ exit
90
+ }
91
+ },
92
+ {
93
+ names: ["d", "dump"],
94
+ description: "Print out all messages in this converastion.",
95
+ implementation: ->() {
96
+ puts "dump> ".colorize(:blue) + conversation.messages.to_json
97
+ }
98
+ },
99
+ {
100
+ names: ["f", "functions"],
101
+ description: "List all available functions.",
102
+ implementation: ->() {
103
+ puts "available functions:".colorize(:blue)
104
+ functions.each do |function|
105
+ puts "- `#{function.name}` #{function.description}".colorize(:blue)
106
+ end
107
+ }
108
+ },
109
+ {
110
+ names: ["h", "help"],
111
+ description: "List all commands and their description",
112
+ implementation: ->() {
113
+ puts commands.map { |command| " - #{command[:names].map { |str| "`\\#{str}`".colorize(:yellow) }.join(", ")}: #{command[:description].colorize(:blue)}" }.join("\n")
114
+ }
115
+ }
116
+ ]
71
117
 
72
118
  while message = Reline.readline("me> ".colorize(:red), true) do
73
- case message.chomp
74
- when "\\help", "\\h"
75
- puts <<~COMMANDS.colorize(:blue)
76
- - `\\quit` Exit
77
- - `\\save <filename>` Save this conversation to a JSON file that can be reloaded later with the `-f` argument
78
- - `\\functions` Get a list of configured functions
79
- COMMANDS
80
- when "\\q", "\\quit", "\\exit"
81
- exit
82
- when "\\dump"
83
- puts "dump> ".colorize(:blue) + conversation.messages.to_json
84
- when "\\functions"
85
- puts "available functions:".colorize(:blue)
86
- functions.each do |function|
87
- puts "- `#{function.name}` #{function.description}".colorize(:blue)
88
- end
89
- when /^\\save .+/
90
- filename = /^\\save (.+)/.match(message)[1]
91
- File.open(filename, "w") { |f| f.write(conversation.messages.to_json) }
92
- puts "saved to #{filename} ".colorize(:blue)
119
+ input = message.chomp
120
+ if (command = commands.find { |command| command[:names].any? { |name| "\\#{name}" == input } })
121
+ command[:implementation].call
93
122
  else
94
123
  print("ai> ".colorize(:yellow))
95
124
  conversation.ask(message) { |fragment| print(fragment) }
@@ -23,7 +23,15 @@ module ChatgptRb
23
23
  @api_key = api_key
24
24
  @model = model
25
25
  @functions = functions.each_with_object({}) do |function, hash|
26
- func = function.is_a?(ChatgptRb::Function) ? function : ChatgptRb::Function.new(**function)
26
+ func = if function.is_a?(ChatgptRb::Function)
27
+ function
28
+ else
29
+ parameters = function.dig(:parameters, :properties)&.map do |name, definition|
30
+ required = function.dig(:parameters, :required)&.include?(name)
31
+ ChatgptRb::Parameter.new(name:, required:, **definition)
32
+ end || []
33
+ ChatgptRb::Function.new(parameters:, **function.except(:parameters))
34
+ end
27
35
  hash[func.name] = func
28
36
  end
29
37
  @temperature = temperature
@@ -139,6 +147,8 @@ module ChatgptRb
139
147
  end
140
148
  end
141
149
 
150
+ raise APIError.new, response.body unless response.success?
151
+
142
152
  error_buffer.each { |e| $stderr.puts("Error: #{e}") }
143
153
 
144
154
  @messages << if block_given? && streamed_content != ""
@@ -164,4 +174,7 @@ module ChatgptRb
164
174
  end
165
175
  end
166
176
  end
177
+
178
+ # Raised when the API responds with an error.
179
+ class APIError < StandardError; end
167
180
  end
@@ -1,3 +1,3 @@
1
1
  module ChatgptRb
2
- VERSION = "0.1.4"
2
+ VERSION = "0.1.6"
3
3
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: chatgpt-rb
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.4
4
+ version: 0.1.6
5
5
  platform: ruby
6
6
  authors:
7
7
  - Aaron Breckenridge
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2023-08-08 00:00:00.000000000 Z
11
+ date: 2023-09-05 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: httparty