yptools 1.0.15 → 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: c441f47e39864bee85572ea544f706e132046da59558541e96fa4c92767a087f
4
- data.tar.gz: 7803c926317702d8072b1ff42c7b35beb275caa11b06f99d985be92d4c396c85
3
+ metadata.gz: 05c1d548417627949ac94ebf616c8fa43f617effa7abe15c6696f0fd7a4ebc98
4
+ data.tar.gz: 77b047b1c9e4a24dbe2ca490884459a715a4f522d25b6ade0d117de59495745f
5
5
  SHA512:
6
- metadata.gz: 52c7d88b1effa8b95c23426df53ea49e6aa65bf4071189c482a833d3b326fe507c4fd1d04357581d64e27ac863ae1598377eceddfbcf944101f940a55fb64b6e
7
- data.tar.gz: 3f2595618b09f5965386b9a0ed45c751bd44f07924f6da3c27ada7bc836859eb6da27902a9912b88c1db9d57a13ddb2fa1afa0b4e84c0491979549cd2f4f8240
6
+ metadata.gz: 30bb630cb7d84c1e80dec1bf59f0c667b53f21ba96ee0284c92013af3421eed6bea1290ddccd6fd85cf3d9d0f9ac2ddf34a4a2ebba9c323b8a7e34a4dc2c8380
7
+ data.tar.gz: fa7e14ba1b2a495849921bb73b64d71446a0bed3b4c09a50bafa928c415e325f994c8ee1208810f04fb54b73f2fd0f53ef065dcbc06e679e5710dfe18ff301f3
@@ -1,49 +1,118 @@
1
1
  require_relative '../log/yp_log'
2
2
  require 'net/http'
3
3
  require 'json'
4
+ require 'base64'
4
5
 
5
6
  class YPChatAI
6
7
 
7
- def self.message(message)
8
- yp_log_doing "你的问题:#{message}"
9
- yp_log_msg "请求中,请等待..."
10
- 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岁。'
11
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'
12
44
  # 开始发送网络请求
13
45
  url = URI.parse(yp_chataiURL)
14
46
  http = Net::HTTP.new(url.host, url.port)
15
47
  http.use_ssl = true
48
+ # 简单了加了下密,免费的apikey,就不要扒去用了(我的代码是开源放到GitHub,加密的目的是ChatGPT会检测秘钥是否在网络泄露)
49
+ yp_token = "c2stTVRVMVZqeUdpNWxzYlU1TmNlU1pUM0JsYmtGSmNYam5iUk5ROENVYUd2QVR4WXpp"
50
+ # 将Base64字符串解码为原始二进制数据
51
+ decoded_data = Base64.decode64(yp_token).force_encoding("UTF-8")
16
52
  # 设置请求头
17
53
  headers = {
18
54
  'Content-Type' => 'application/json',
19
- 'Authorization' => 'Bearer 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'
55
+ 'Authorization' => 'Bearer ' + decoded_data
20
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
+
21
65
  # 设置请body
22
66
  data = {
67
+ # 'engine' => 'davinci',
23
68
  'model' => 'text-davinci-003', # 然后GPT-3模型会根据您的输入文本自动生成文本补全或摘要。
24
- 'prompt' => message, # 问的问题
69
+ 'prompt' => question, # 问的问题
25
70
  'max_tokens' => 999, # 设置回答最多多少个字符
26
- 'temperature' => 0.9, # 文本创意度,默认 1
71
+ 'temperature' => 0.7, # 文本创意度,默认 1
72
+ "n": 1, #几个回答
73
+ "stop": "\n"
27
74
  }
28
-
75
+
29
76
  request = Net::HTTP::Post.new(url.path, headers)
30
77
  request.body = data.to_json
31
- response = http.request(request)
32
78
 
33
- yp_message_response = JSON(response.body)
34
- if !yp_message_response["error"]
35
- created = yp_message_response["created"]
36
- choices = yp_message_response["choices"]
37
- if !choices.empty?
38
- text = choices.first["text"]
39
- 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
40
94
  else
41
- yp_log_fail "请求失败," + yp_message_response
95
+ message = yp_message_response["error"]["message"]
96
+ yp_log_fail "请求失败,请稍后再试!!!\n" + message
97
+ return ""
42
98
  end
43
- else
44
- message = yp_message_response["error"]["message"]
45
- yp_log_fail "请求失败,请稍后再试!!!\n" + message
99
+ rescue StandardError => e
100
+ # 处理异常
101
+ yp_log_fail "请求的次数太多了,请稍后再试!!!"
102
+ yp_log_fail "发生异常:#{e.message}"
46
103
  end
47
104
  end
48
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
+
49
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.15
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