Class: YPChatAI

Inherits:
Object
  • Object
show all
Defined in:
lib/yptools/chatai/yp_chatai.rb

Class Method Summary collapse

Class Method Details

.message(message) ⇒ Object



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 
    # 设置请求头
    headers = {
        'Content-Type' => 'application/json',
        'Authorization' => 'Bearer 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'
    }
    # 设置请body
    data = {
        'model' => 'text-davinci-003', # 然后GPT-3模型会根据您的输入文本自动生成文本补全或摘要。
        'prompt' => message, # 问的问题
        'max_tokens' => 999, # 设置回答最多多少个字符 
        'temperature' => 0.9, # 文本创意度,默认 1
    }

    request = Net::HTTP::Post.new(url.path, headers)
    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