yptools 1.0.16 → 1.0.18

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: 9cb3e39055391054fe343255192ab05476b7e579002f2749e52b664293c47347
4
- data.tar.gz: df8a95a939692f3f62da1e04a8240c84feadd2adb2da6a3110445dbb2144a9ce
3
+ metadata.gz: 5b0c0ac65980ab31a4c668af14aea11d9c2d7ab7dcf0b8c20b072c797b0bea35
4
+ data.tar.gz: 7451143c8b5d160ff9176532a2570cfef65461e5f78dfa25f0b3627f28bf3653
5
5
  SHA512:
6
- metadata.gz: 19f54c0bc031663974d6115540de9bf6f0001b12815a9691a2d85774e90618888d1e7674704778d10d90f38f1e695bd5a20c606ebe0d9c505f4e5b10815b2aaf
7
- data.tar.gz: eba0155e8c4030b679fb400efd9397aae196fbfdef9523cf074f2c7195ac83ab0d4a2f177a270eaf4c3d9858452e4cdbe35a01e2f4b48370621ba2fff3f214e8
6
+ metadata.gz: 6664fa97f413646ca8926eef8df0d4a227e46b87af6f87ed53b96b0900e5596931fbc3f1cafb2e4bb4afccc1cbaefdd9c6e54788afeaf5c0e48c93314f603445
7
+ data.tar.gz: 05036757b34371ad309a41e42b8d2835720571aea0063efeff899dda29d163960c8a4adf6b99a9960aa1800abd04d750a2670ca9b311381001e375f213651c9e
@@ -5,49 +5,172 @@ require 'base64'
5
5
 
6
6
  class YPChatAI
7
7
 
8
- def self.message(message)
9
- yp_log_doing "你的问题:#{message}"
10
- yp_log_msg "请求中,请等待..."
11
- yp_chataiURL = 'https://api.openai.com/v1/completions'
8
+ def self.startChatAI
9
+ system('clear')
10
+ yp_log_msg "\n欢迎使用 YPTools & ChatGPT"
11
+ yp_log_msg "YPTools源码地址:https://github.com/HansenCCC/YPTools"
12
+ yp_log_msg "OpenAI地址:https://github.com/openai\n"
13
+ yp_log_success "ChatGPT"
14
+ yp_log_doing "输入 'q' 或者 'quit' 退出当前会话"
15
+ yp_log_msg '我是一个非常聪明的ChatGPT机器人。如果您问我一个根源于真理的问题,我会给您答案。'
16
+ yp_log_msg 'Q: 全球人类的平均寿命是多少?'
17
+ yp_log_msg 'A: 根据世界卫生组织公布的数据,2019年全球人类的平均寿命为71.4岁。'
12
18
 
19
+ yp_contents = Array.new
20
+ while true
21
+ Encoding.default_external = Encoding::UTF_8
22
+ Encoding.default_internal = Encoding::UTF_8
23
+ # 使用while循环
24
+ print "Q: "
25
+ yp_question = STDIN.gets.chomp
26
+ if (yp_question.upcase == 'Q' || yp_question.upcase == 'QUIT')
27
+ break
28
+ end
29
+ yp_contents.push({
30
+ 'USER' => yp_question,
31
+ 'AI' => ""
32
+ })
33
+ yp_answer = self.chatGPTWithQuestion(yp_contents)
34
+ print "A: " + yp_answer + "\n"
35
+ yp_item = yp_contents.pop
36
+ yp_item["AI"] = yp_answer
37
+ yp_contents.push(yp_item)
38
+ end
39
+ end
40
+
41
+ def self.chatGPTWithQuestion(content)
42
+ yp_chataiURL = 'https://api.openai.com/v1/completions'
43
+ # yp_chataiURL = 'https://api.openai.com/v1/engines/davinci-codex/completions'
13
44
  # 开始发送网络请求
14
45
  url = URI.parse(yp_chataiURL)
15
46
  http = Net::HTTP.new(url.host, url.port)
16
47
  http.use_ssl = true
17
- # 设置请求头
48
+ # 简单了加了下密,免费的apikey,就不要扒去用了(我的代码是开源放到GitHub,加密的目的是ChatGPT会检测秘钥是否在网络泄露)
18
49
  yp_token = "c2stTVRVMVZqeUdpNWxzYlU1TmNlU1pUM0JsYmtGSmNYam5iUk5ROENVYUd2QVR4WXpp"
19
50
  # 将Base64字符串解码为原始二进制数据
20
51
  decoded_data = Base64.decode64(yp_token).force_encoding("UTF-8")
21
-
52
+ # 设置请求头
22
53
  headers = {
23
54
  'Content-Type' => 'application/json',
24
55
  'Authorization' => 'Bearer ' + decoded_data
25
56
  }
57
+ # 设置ai根据上下文
58
+ question = ''
59
+ for key in content
60
+ user = key['USER']
61
+ ai = key['AI']
62
+ question += '\nUSER: ' + user + '\nAI: ' + ai
63
+ end
64
+
26
65
  # 设置请body
27
66
  data = {
67
+ # 'engine' => 'davinci',
28
68
  'model' => 'text-davinci-003', # 然后GPT-3模型会根据您的输入文本自动生成文本补全或摘要。
29
- 'prompt' => message, # 问的问题
69
+ 'prompt' => question, # 问的问题
30
70
  'max_tokens' => 999, # 设置回答最多多少个字符
31
- 'temperature' => 0.9, # 文本创意度,默认 1
71
+ 'temperature' => 0.7, # 文本创意度,默认 1
72
+ "n": 1, #几个回答
73
+ "stop": "\n"
32
74
  }
75
+
76
+ request = Net::HTTP::Post.new(url.path, headers)
77
+ request.body = data.to_json
33
78
 
79
+ begin
80
+ response = http.request(request)
81
+ # 处理响应数据
82
+ yp_message_response = JSON(response.body)
83
+ if !yp_message_response["error"]
84
+ created = yp_message_response["created"]
85
+ choices = yp_message_response["choices"]
86
+ if !choices.empty?
87
+ text = choices.first["text"]
88
+ text = text.gsub(/\n+/, "")
89
+ return text
90
+ else
91
+ yp_log_fail "请求失败," + yp_message_response
92
+ return ""
93
+ end
94
+ else
95
+ message = yp_message_response["error"]["message"]
96
+ yp_log_fail "请求失败,请稍后再试!!!\n" + message
97
+ return ""
98
+ end
99
+ rescue StandardError => e
100
+ # 处理异常
101
+ yp_log_fail "请求的次数太多了,请稍后再试!!!"
102
+ yp_log_fail "发生异常:#{e.message}"
103
+ end
104
+ end
105
+
106
+ def self.message(message)
107
+ yp_log_doing "你的问题:#{message}"
108
+ yp_log_msg "请求中,请等待..."
109
+ yp_contents = Array.new
110
+ yp_contents.push({
111
+ 'USER' => message,
112
+ 'AI' => ""
113
+ })
114
+ yp_answer = self.chatGPTWithQuestion(yp_contents)
115
+ yp_log_success "chatGPT:" + yp_answer.gsub(/\n+/, "\n")
116
+ end
117
+
118
+ def self.openaiimg(message)
119
+ yp_chataiURL = 'https://api.openai.com/v1/images/generations'
120
+ # 开始发送网络请求
121
+ url = URI.parse(yp_chataiURL)
122
+ http = Net::HTTP.new(url.host, url.port)
123
+ http.use_ssl = true
124
+ # 简单了加了下密,免费的apikey,就不要扒去用了(我的代码是开源放到GitHub,加密的目的是ChatGPT会检测秘钥是否在网络泄露)
125
+ yp_token = "c2stTVRVMVZqeUdpNWxzYlU1TmNlU1pUM0JsYmtGSmNYam5iUk5ROENVYUd2QVR4WXpp"
126
+ # 将Base64字符串解码为原始二进制数据
127
+ decoded_data = Base64.decode64(yp_token).force_encoding("UTF-8")
128
+ # 设置请求头
129
+ headers = {
130
+ 'Content-Type' => 'application/json',
131
+ 'Authorization' => 'Bearer ' + decoded_data
132
+ }
133
+ # 设置ai根据上下文
134
+ question = message
135
+
136
+ # 设置请body
137
+ data = {
138
+ # 'model' => 'text-davinci-003', # 然后GPT-3模型会根据您的输入文本自动生成文本补全或摘要。
139
+ 'prompt' => question, # 问的问题
140
+ "n": 3, #几个回答
141
+ "size": "512x512"
142
+ }
143
+
34
144
  request = Net::HTTP::Post.new(url.path, headers)
35
145
  request.body = data.to_json
36
- response = http.request(request)
37
-
38
- yp_message_response = JSON(response.body)
39
- if !yp_message_response["error"]
40
- created = yp_message_response["created"]
41
- choices = yp_message_response["choices"]
42
- if !choices.empty?
43
- text = choices.first["text"]
44
- yp_log_success "chatGPT:" + text.gsub(/\n+/, "\n")
146
+
147
+ begin
148
+ response = http.request(request)
149
+ # 处理响应数据
150
+ yp_message_response = JSON(response.body)
151
+ if !yp_message_response["error"]
152
+ created = yp_message_response["created"]
153
+ data = yp_message_response["data"]
154
+ puts ""
155
+ if !data.empty?
156
+ index = 1
157
+ for item in data
158
+ yp_log_success "图#{index} 【复制下面链接到浏览器打开或者 command + 鼠标左键快速打开 】"
159
+ yp_log_msg item["url"]
160
+ index = index + 1
161
+ puts ""
162
+ end
163
+ else
164
+ yp_log_fail "请求失败," + yp_message_response
165
+ end
45
166
  else
46
- yp_log_fail "请求失败," + yp_message_response
167
+ message = yp_message_response["error"]["message"]
168
+ yp_log_fail "请求失败,请稍后再试!!!\n" + message
47
169
  end
48
- else
49
- message = yp_message_response["error"]["message"]
50
- yp_log_fail "请求失败,请稍后再试!!!\n" + message
170
+ rescue StandardError => e
171
+ # 处理异常
172
+ yp_log_fail "请求的次数太多了,请稍后再试!!!"
173
+ yp_log_fail "发生异常:#{e.message}"
51
174
  end
52
175
  end
53
176
 
@@ -3,7 +3,9 @@ class YPHelp
3
3
  def self.message
4
4
  puts %q{
5
5
 
6
- chatai: use [yptools chatai ...] 快速与 chatgpt 沟通(科学上网)
6
+ chatgpt: use [yptools chatgpt] 创建会话列表与 chatgpt 聊天,会记录上下内容(科学上网)
7
+ [yptools chatgpt ...] 快速与 chatgpt 沟通,不会记录上下内容
8
+ openai: use [yptools openaiimg ...] 根据文本描述生成图像(eg: yptools openaiimg '高冷 少妇 黑丝 短裤 真人照片' )
7
9
 
8
10
  autocre: use [yptools autocre ...] 自动化工具命令
9
11
  use [yptools autocre -objc ...] 根据 json 自动创建 Objective-C 数据库操作文件 .h|.m 文件。(依赖三方库 FMDB )
data/lib/yptools.rb CHANGED
@@ -18,12 +18,20 @@ class YPTools
18
18
  cmd = argvs[0]
19
19
  case cmd
20
20
 
21
- when 'chatai'
21
+ when 'chatgpt'
22
22
  if argvs.size > 1
23
23
  name = argvs[1]
24
24
  self.chatai name
25
25
  else
26
- yp_log_fail "'yptools chatai ..' 参数缺失"
26
+ self.startChat
27
+ end
28
+ when 'openaiimg'
29
+ if argvs.size > 1
30
+ name = argvs[1..-1]
31
+ name = name.join(' ')
32
+ self.openaiimg name
33
+ else
34
+ yp_log_fail "'yptools openaiimg ..' 参数缺失"
27
35
  self.help
28
36
  end
29
37
  when 'autocre'
@@ -141,6 +149,14 @@ class YPTools
141
149
  def self.autoinit
142
150
  YPAutoInit.createObjcInitJson
143
151
  end
152
+
153
+ def self.startChat
154
+ YPChatAI.startChatAI()
155
+ end
156
+
157
+ def self.openaiimg(message)
158
+ YPChatAI.openaiimg(message)
159
+ end
144
160
 
145
161
  end
146
162
 
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: yptools
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.0.16
4
+ version: 1.0.18
5
5
  platform: ruby
6
6
  authors:
7
7
  - chenghengsheng
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2023-03-05 00:00:00.000000000 Z
11
+ date: 2023-03-09 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: bundler