Class: YPChatAI
- Inherits:
-
Object
- Object
- YPChatAI
- Defined in:
- lib/yptools/chatai/yp_chatai.rb
Class Method Summary collapse
- .chatGPTWithQuestion(content) ⇒ Object
- .message(message) ⇒ Object
- .openaiimg(message) ⇒ Object
- .startChatAI ⇒ Object
Class Method Details
.chatGPTWithQuestion(content) ⇒ Object
41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 |
# File 'lib/yptools/chatai/yp_chatai.rb', line 41 def self.chatGPTWithQuestion(content) yp_chataiURL = 'https://api.openai.com/v1/completions' # yp_chataiURL = 'https://api.openai.com/v1/engines/davinci-codex/completions' # 开始发送网络请求 url = URI.parse(yp_chataiURL) http = Net::HTTP.new(url.host, url.port) http.use_ssl = true # 简单了加了下密,免费的apikey,就不要扒去用了(我的代码是开源放到GitHub,加密的目的是ChatGPT会检测秘钥是否在网络泄露) yp_token = "c2stTVRVMVZqeUdpNWxzYlU1TmNlU1pUM0JsYmtGSmNYam5iUk5ROENVYUd2QVR4WXpp" # 将Base64字符串解码为原始二进制数据 decoded_data = Base64.decode64(yp_token).force_encoding("UTF-8") # 设置请求头 headers = { 'Content-Type' => 'application/json', 'Authorization' => 'Bearer ' + decoded_data } # 设置ai根据上下文 question = '' for key in content user = key['USER'] ai = key['AI'] question += '\nUSER: ' + user + '\nAI: ' + ai end # 设置请body data = { # 'engine' => 'davinci', 'model' => 'text-davinci-003', # 然后GPT-3模型会根据您的输入文本自动生成文本补全或摘要。 'prompt' => question, # 问的问题 'max_tokens' => 999, # 设置回答最多多少个字符 'temperature' => 0.7, # 文本创意度,默认 1 "n": 1, #几个回答 "stop": "\n" } request = Net::HTTP::Post.new(url.path, headers) request.body = data.to_json begin response = http.request(request) # 处理响应数据 = JSON(response.body) if !["error"] created = ["created"] choices = ["choices"] if !choices.empty? text = choices.first["text"] text = text.gsub(/\n+/, "") return text else yp_log_fail "请求失败," + return "" end else = ["error"]["message"] yp_log_fail "请求失败,请稍后再试!!!\n" + return "" end rescue StandardError => e # 处理异常 yp_log_fail "请求的次数太多了,请稍后再试!!!" yp_log_fail "发生异常:#{e.}" end end |
.message(message) ⇒ Object
106 107 108 109 110 111 112 113 114 115 116 |
# File 'lib/yptools/chatai/yp_chatai.rb', line 106 def self.() yp_log_doing "你的问题:#{}" yp_log_msg "请求中,请等待..." yp_contents = Array.new yp_contents.push({ 'USER' => , 'AI' => "" }) yp_answer = self.chatGPTWithQuestion(yp_contents) yp_log_success "chatGPT:" + yp_answer.gsub(/\n+/, "\n") end |
.openaiimg(message) ⇒ Object
118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 |
# File 'lib/yptools/chatai/yp_chatai.rb', line 118 def self.openaiimg() yp_chataiURL = 'https://api.openai.com/v1/images/generations' # 开始发送网络请求 url = URI.parse(yp_chataiURL) http = Net::HTTP.new(url.host, url.port) http.use_ssl = true # 简单了加了下密,免费的apikey,就不要扒去用了(我的代码是开源放到GitHub,加密的目的是ChatGPT会检测秘钥是否在网络泄露) yp_token = "c2stTVRVMVZqeUdpNWxzYlU1TmNlU1pUM0JsYmtGSmNYam5iUk5ROENVYUd2QVR4WXpp" # 将Base64字符串解码为原始二进制数据 decoded_data = Base64.decode64(yp_token).force_encoding("UTF-8") # 设置请求头 headers = { 'Content-Type' => 'application/json', 'Authorization' => 'Bearer ' + decoded_data } # 设置ai根据上下文 question = # 设置请body data = { # 'model' => 'text-davinci-003', # 然后GPT-3模型会根据您的输入文本自动生成文本补全或摘要。 'prompt' => question, # 问的问题 "n": 3, #几个回答 "size": "512x512" } request = Net::HTTP::Post.new(url.path, headers) request.body = data.to_json begin response = http.request(request) # 处理响应数据 = JSON(response.body) if !["error"] created = ["created"] data = ["data"] puts "" if !data.empty? index = 1 for item in data yp_log_success "图#{index} 【复制下面链接到浏览器打开或者 command + 鼠标左键快速打开 】" yp_log_msg item["url"] index = index + 1 puts "" end else yp_log_fail "请求失败," + end else = ["error"]["message"] yp_log_fail "请求失败,请稍后再试!!!\n" + end rescue StandardError => e # 处理异常 yp_log_fail "请求的次数太多了,请稍后再试!!!" yp_log_fail "发生异常:#{e.}" end end |
.startChatAI ⇒ Object
8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 |
# File 'lib/yptools/chatai/yp_chatai.rb', line 8 def self.startChatAI system('clear') yp_log_msg "\n欢迎使用 YPTools & ChatGPT" yp_log_msg "YPTools源码地址:https://github.com/HansenCCC/YPTools" yp_log_msg "OpenAI地址:https://github.com/openai\n" yp_log_success "ChatGPT" yp_log_doing "输入 'q' 或者 'quit' 退出当前会话" yp_log_msg '我是一个非常聪明的ChatGPT机器人。如果您问我一个根源于真理的问题,我会给您答案。' yp_log_msg 'Q: 全球人类的平均寿命是多少?' yp_log_msg 'A: 根据世界卫生组织公布的数据,2019年全球人类的平均寿命为71.4岁。' yp_contents = Array.new while true Encoding.default_external = Encoding::UTF_8 Encoding.default_internal = Encoding::UTF_8 # 使用while循环 print "Q: " yp_question = STDIN.gets.chomp if (yp_question.upcase == 'Q' || yp_question.upcase == 'QUIT') break end yp_contents.push({ 'USER' => yp_question, 'AI' => "" }) yp_answer = self.chatGPTWithQuestion(yp_contents) print "A: " + yp_answer + "\n" yp_item = yp_contents.pop yp_item["AI"] = yp_answer yp_contents.push(yp_item) end end |