chatgpt_cli 0.1.0 → 0.1.1

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: 18ebf57184dbc956922778541ca01af6fd63574a1f520073e7ff8a4dda6001c5
4
- data.tar.gz: 6441aed15113f4509e50f8e59b98d4d4b0ec226a06a59252d3dc70aee49e1c6f
3
+ metadata.gz: 2db01f06350e322e2ce5723bce754dd6dfc210242b602b35bd0a7fa53a1f0a37
4
+ data.tar.gz: eaced5ad27bde6185d29ed8d86ea17fe028b34805496894fe5307be6b0070c95
5
5
  SHA512:
6
- metadata.gz: d2951e7aabc888939c3d171525c45624b6b3d21831bd471f6fe7b0395a9ec722ba8a45b0754bfcee0a88f3bf76cfbbeca4cfa3a4074ab0c84723d09859f9a8c9
7
- data.tar.gz: '087481736b6fbd3b19bea7fd8c567130f014d5bc6ad119731878f053e8af35b3fe11ea4e5d35faec8296398434d897c2c210fc3e974358972b6b6135947561e4'
6
+ metadata.gz: d18e1243cac286daafd2df3d5c920e1a79e30902cf7148f607307caa37beb6f941a67a6874487199e3ef956c18ba5537b62e39c47f2d4315dcedbceb0d866974
7
+ data.tar.gz: b26a1d944aa91fa74812d40fcf2236030b5b0076c0d29354731bac5e354ea36260c461db6a6601e8f403877a1a4c11c6c7dd880df3d715b6135015797305e81e
data/README.md CHANGED
@@ -21,8 +21,14 @@ Or install it yourself as:
21
21
  $ gem install chatgpt_cli
22
22
 
23
23
  ## Usage
24
+ ```
25
+ export CHATGPT_OPENAI_KEY=XXX
26
+ export CHATGPT_OPENAI_ORGANIZATION=XXX
27
+ ```
24
28
 
25
- TODO: Write usage instructions here
29
+ ```
30
+ 在终端执行 gptcli(需要自行架梯子)
31
+ ```
26
32
 
27
33
  ## Development
28
34
 
data/chatgpt_cli.gemspec CHANGED
@@ -12,7 +12,7 @@ Gem::Specification.new do |spec|
12
12
  spec.description = "CLI of ChatGPT"
13
13
  spec.homepage = "https://github.com/sanm1992/chatgpt_cli"
14
14
  spec.license = "MIT"
15
- spec.required_ruby_version = Gem::Requirement.new(">= 2.3.0")
15
+ spec.required_ruby_version = Gem::Requirement.new(">= 2.5.0")
16
16
 
17
17
  spec.metadata["allowed_push_host"] = "https://rubygems.org"
18
18
 
@@ -9,8 +9,6 @@ module ChatgptCli
9
9
  CHATGPT_OPENAI_MODEL = 'gpt-3.5-turbo'
10
10
  CHATGPT_OPENAI_ROLE = 'user'
11
11
 
12
- attr_accessor :openai_key, :openai_organization
13
-
14
12
  def fetch_dependences
15
13
  self.openai_key = ENV.fetch('CHATGPT_OPENAI_KEY', nil)
16
14
  self.openai_organization = ENV.fetch('CHATGPT_OPENAI_ORGANIZATION', nil)
@@ -25,8 +23,14 @@ module ChatgptCli
25
23
  params = { model: CHATGPT_OPENAI_MODEL, messages: [{ role: CHATGPT_OPENAI_ROLE, content: content }] }
26
24
  headers = { 'Authorization': "Bearer #{self.openai_key}", 'content-type': 'application/json', 'OpenAI-Organization': self.openai_organization }
27
25
  res = Net::HTTP.post(URI(CHATGPT_OPENAI_URL), params.to_json, headers)
28
- res = JSON.parse res.body
29
- res['choices']&.first&.[]('message')&.[]('content')
26
+ body = JSON.parse res.body
27
+ messages = body.dig('error', 'message') || body&.[]('choices')&.first&.dig('message', 'content')
28
+
29
+ [res.code.to_i.eql?(200), messages]
30
30
  end
31
+
32
+ private
33
+
34
+ attr_accessor :openai_key, :openai_organization
31
35
  end
32
36
  end
@@ -23,6 +23,8 @@ module ChatgptCli
23
23
 
24
24
  welcome
25
25
 
26
+ Signal.trap('INT') { quit! }
27
+
26
28
  loop do
27
29
  get_instruction
28
30
 
@@ -36,6 +38,10 @@ module ChatgptCli
36
38
  exit(1)
37
39
  end
38
40
 
41
+ def error(message = nil)
42
+ out(message.to_s.red)
43
+ end
44
+
39
45
  private
40
46
 
41
47
  attr_accessor :line_index, :instruction, :search_content, :collback
@@ -49,8 +55,7 @@ module ChatgptCli
49
55
  end
50
56
 
51
57
  def start_tag
52
- line_index ||= 0
53
- "chatgpt:#{line_index += 1}> "
58
+ "chatgpt:#{self.line_index += 1}> "
54
59
  end
55
60
 
56
61
  def get_instruction
@@ -81,9 +86,11 @@ module ChatgptCli
81
86
  return true
82
87
  end
83
88
 
84
- out "正在为你查找 #{search_content.join('')} 的答案"
85
89
  begin
90
+ out 'ChatGPT >>>>>>>'.magenta
91
+
86
92
  collback&.call(search_content.join(''))
93
+
87
94
  puts "\n"
88
95
  rescue => ex
89
96
  out ex.message
@@ -106,7 +113,7 @@ module ChatgptCli
106
113
  def out(str)
107
114
  print start_tag
108
115
 
109
- str.each_char do |c|
116
+ str.to_s.each_char do |c|
110
117
  print c
111
118
  $stdout.flush
112
119
  sleep 0.05
@@ -114,5 +121,15 @@ module ChatgptCli
114
121
 
115
122
  puts "\n"
116
123
  end
124
+
125
+ # def loading
126
+ # chars = %w[| / - \\]
127
+ # i = 0
128
+ # while i>=0 do
129
+ # print "#{chars[i % 4]} loading.. "
130
+ # i += 1
131
+ # sleep(0.1)
132
+ # end
133
+ # end
117
134
  end
118
135
  end
@@ -22,7 +22,7 @@ module ChatgptCli
22
22
  private
23
23
 
24
24
  def show_by_default(content)
25
- content.each_char do |c|
25
+ content&.each_char do |c|
26
26
  print c
27
27
  $stdout.flush
28
28
  sleep 0.05
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module ChatgptCli
4
- VERSION = "0.1.0"
4
+ VERSION = "0.1.1"
5
5
  end
data/lib/chatgpt_cli.rb CHANGED
@@ -9,10 +9,9 @@ require_relative 'chatgpt_cli/show_tool'
9
9
 
10
10
  module ChatgptCli
11
11
  class Error < StandardError; end
12
- # Your code goes here...
12
+
13
13
  class << self
14
14
  def exec
15
- chatgpt_ai = ChatgptCli::Chatgpt.new
16
15
  cli = ChatgptCli::CLI.new
17
16
  cli.exec do |content|
18
17
  show_tool = ChatgptCli::ShowTool.new('default')
@@ -23,7 +22,8 @@ module ChatgptCli
23
22
  ai_ready, error = chatgpt_ai.fetch_dependences
24
23
  cli.quit!(error) unless ai_ready
25
24
 
26
- show_tool.show chatgpt_ai.completion(content)
25
+ success, messages = chatgpt_ai.completion(content)
26
+ success ? show_tool.show(messages) : cli.error(messages)
27
27
  end
28
28
  end
29
29
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: chatgpt_cli
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.0
4
+ version: 0.1.1
5
5
  platform: ruby
6
6
  authors:
7
7
  - sanm
8
8
  autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2023-03-16 00:00:00.000000000 Z
11
+ date: 2023-03-17 00:00:00.000000000 Z
12
12
  dependencies: []
13
13
  description: CLI of ChatGPT
14
14
  email:
@@ -51,7 +51,7 @@ required_ruby_version: !ruby/object:Gem::Requirement
51
51
  requirements:
52
52
  - - ">="
53
53
  - !ruby/object:Gem::Version
54
- version: 2.3.0
54
+ version: 2.5.0
55
55
  required_rubygems_version: !ruby/object:Gem::Requirement
56
56
  requirements:
57
57
  - - ">="