7
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
40
41
42
43
44
45
46
47
|
# File 'lib/yptools/chatai/yp_chatai.rb', line 7
def self.message(message)
yp_log_doing "你的问题:#{message}"
yp_log_msg "请求中,请等待..."
yp_chataiURL = 'https://api.openai.com/v1/completions'
url = URI.parse(yp_chataiURL)
http = Net::HTTP.new(url.host, url.port)
http.use_ssl = true
= {
'Content-Type' => 'application/json',
'Authorization' => 'Bearer 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'
}
data = {
'model' => 'text-davinci-003', 'prompt' => message, 'max_tokens' => 999, 'temperature' => 0.9, }
request = Net::HTTP::Post.new(url.path, )
request.body = data.to_json
response = http.request(request)
yp_message_response = JSON(response.body)
if !yp_message_response["error"]
created = yp_message_response["created"]
choices = yp_message_response["choices"]
if !choices.empty?
text = choices.first["text"]
yp_log_success "chatGPT:" + text.gsub(/\n+/, "\n")
else
yp_log_fail "请求失败," + yp_message_response
end
else
message = yp_message_response["error"]["message"]
yp_log_fail "请求失败,请稍后再试!!!\n" + message
end
end
|