chatgpt-rb 0.1.4 → 0.1.5

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: 4833d50d716b3f34e13dc2936f3b837ff0346ecfe35d42c2755f1c0d48989883
4
+ data.tar.gz: c2220ac57c216ea81125552274e65567b4076b127acdc1d62a54daaa427a665f
5
5
  SHA512:
6
- metadata.gz: 41d2d11e8eeedfc065483d19c19c495d480954c99a98ac3256ec0a76a079562ab9f8524f654aa2fc238c4f867e02a8d1f5520f34ee9b510d5f97075f17e27c4e
7
- data.tar.gz: 9dff14b6052aae029620e5d7e1f75ada65cf91418dcaada97c5231f799f183f90c851ea706bd4b76b681e4202e05b6bf302bef1aee84f92a98d2546756d5a930
6
+ metadata.gz: dc3972ef878ce27bb6576bbc7fcc9e39cbdb364abfa35b762fd7bb2e77b4d025389300630953c8ea8e1d88b7d57af7979116a4af1b63639ddaf3ecd7ad16adee
7
+ data.tar.gz: 8bb0f8541137f062bdbf716cb1de37114186caaa4d0bcd279ac4cda0d892698b454862af25f923dc854a870defef2425a3c891c5df5fcfd4a222e82e006d39be
data/bin/chatgpt-rb CHANGED
@@ -6,11 +6,6 @@ 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",
@@ -47,6 +42,8 @@ OptionParser.new do |opts|
47
42
  end.parse!
48
43
 
49
44
  begin
45
+ stty_save = `stty -g`.chomp
46
+
50
47
  puts "Type any message to talk with ChatGPT. Type '\\help' for a list of commands."
51
48
 
52
49
  functions = options[:functions_files].flat_map do |function_file|
@@ -69,27 +66,53 @@ begin
69
66
 
70
67
  conversation = ChatgptRb::Conversation.new(api_key: options.fetch(:key), model: options.fetch(:model), base_uri: options.fetch(:base_uri), messages:, functions:, prompt: options[:prompt])
71
68
 
69
+ commands = [
70
+ {
71
+ names: ["s", "save"],
72
+ description: "Save this conversation to a JSON file that can be reloaded later with the `-f` argument",
73
+ implementation: ->() {
74
+ filename = /^\\save (.+)/.match(message)[1]
75
+ File.open(filename, "w") { |f| f.write(conversation.messages.to_json) }
76
+ puts "saved to #{filename} ".colorize(:blue)
77
+ }
78
+ },
79
+ {
80
+ names: ["q", "quit", "exit"],
81
+ description: "Exit the program",
82
+ implementation: ->() {
83
+ exit
84
+ }
85
+ },
86
+ {
87
+ names: ["d", "dump"],
88
+ description: "Print out all messages in this converastion.",
89
+ implementation: ->() {
90
+ puts "dump> ".colorize(:blue) + conversation.messages.to_json
91
+ }
92
+ },
93
+ {
94
+ names: ["f", "functions"],
95
+ description: "List all available functions.",
96
+ implementation: ->() {
97
+ puts "available functions:".colorize(:blue)
98
+ functions.each do |function|
99
+ puts "- `#{function.name}` #{function.description}".colorize(:blue)
100
+ end
101
+ }
102
+ },
103
+ {
104
+ names: ["h", "help"],
105
+ description: "List all commands and their description",
106
+ implementation: ->() {
107
+ puts commands.map { |command| " - #{command[:names].map { |str| "`\\#{str}`".colorize(:yellow) }.join(", ")}: #{command[:description].colorize(:blue)}" }.join("\n")
108
+ }
109
+ }
110
+ ]
111
+
72
112
  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)
113
+ input = message.chomp
114
+ if (command = commands.find { |command| command[:names].any? { |name| "\\#{name}" == input } })
115
+ command[:implementation].call
93
116
  else
94
117
  print("ai> ".colorize(:yellow))
95
118
  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.5"
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.5
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-08-30 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: httparty