yptools 1.0.16 → 1.0.17

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: 05c1d548417627949ac94ebf616c8fa43f617effa7abe15c6696f0fd7a4ebc98
4
+ data.tar.gz: 77b047b1c9e4a24dbe2ca490884459a715a4f522d25b6ade0d117de59495745f
5
5
  SHA512:
6
- metadata.gz: 19f54c0bc031663974d6115540de9bf6f0001b12815a9691a2d85774e90618888d1e7674704778d10d90f38f1e695bd5a20c606ebe0d9c505f4e5b10815b2aaf
7
- data.tar.gz: eba0155e8c4030b679fb400efd9397aae196fbfdef9523cf074f2c7195ac83ab0d4a2f177a270eaf4c3d9858452e4cdbe35a01e2f4b48370621ba2fff3f214e8
6
+ metadata.gz: 30bb630cb7d84c1e80dec1bf59f0c667b53f21ba96ee0284c92013af3421eed6bea1290ddccd6fd85cf3d9d0f9ac2ddf34a4a2ebba9c323b8a7e34a4dc2c8380
7
+ data.tar.gz: fa7e14ba1b2a495849921bb73b64d71446a0bed3b4c09a50bafa928c415e325f994c8ee1208810f04fb54b73f2fd0f53ef065dcbc06e679e5710dfe18ff301f3
@@ -5,50 +5,114 @@ 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岁。'
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
12
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
  }
33
-
75
+
34
76
  request = Net::HTTP::Post.new(url.path, headers)
35
77
  request.body = data.to_json
36
- response = http.request(request)
37
78
 
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")
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
45
94
  else
46
- yp_log_fail "请求失败," + yp_message_response
95
+ message = yp_message_response["error"]["message"]
96
+ yp_log_fail "请求失败,请稍后再试!!!\n" + message
97
+ return ""
47
98
  end
48
- else
49
- message = yp_message_response["error"]["message"]
50
- yp_log_fail "请求失败,请稍后再试!!!\n" + message
99
+ rescue StandardError => e
100
+ # 处理异常
101
+ yp_log_fail "请求的次数太多了,请稍后再试!!!"
102
+ yp_log_fail "发生异常:#{e.message}"
51
103
  end
52
104
  end
53
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
+
54
118
  end
@@ -3,7 +3,8 @@ class YPHelp
3
3
  def self.message
4
4
  puts %q{
5
5
 
6
- chatai: use [yptools chatai ...] 快速与 chatgpt 沟通(科学上网)
6
+ chatai: use [yptools chatai] 创建会话列表与 chatgpt 聊天,会记录上下内容(科学上网)
7
+ [yptools chatai ...] 快速与 chatgpt 沟通,不会记录上下内容
7
8
 
8
9
  autocre: use [yptools autocre ...] 自动化工具命令
9
10
  use [yptools autocre -objc ...] 根据 json 自动创建 Objective-C 数据库操作文件 .h|.m 文件。(依赖三方库 FMDB )
data/lib/yptools.rb CHANGED
@@ -23,8 +23,7 @@ class YPTools
23
23
  name = argvs[1]
24
24
  self.chatai name
25
25
  else
26
- yp_log_fail "'yptools chatai ..' 参数缺失"
27
- self.help
26
+ self.startChat
28
27
  end
29
28
  when 'autocre'
30
29
  if argvs.size > 1
@@ -141,6 +140,10 @@ class YPTools
141
140
  def self.autoinit
142
141
  YPAutoInit.createObjcInitJson
143
142
  end
143
+
144
+ def self.startChat
145
+ YPChatAI.startChatAI()
146
+ end
144
147
 
145
148
  end
146
149
 
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.17
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-07 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: bundler