ChatGPTに聞いて教えてもらったChatGPT APIの使い方。まだ課金してないので実際に試していないですが、シンプルなhttp経由で色々できそうな雰囲気です。
require 'net/http'
require 'json'
url = URI("https://api.openai.com/v1/chat/completions")
api_key = "sk-(your api key)"
headers = {
"Content-Type" => "application/json",
"Authorization" => "Bearer #{api_key}"
}
data = {
"messages" => [{"role" => "system", "content" => "You are a helpful assistant."}],
"model" => "davinci"
}
http = Net::HTTP.new(url.host, url.port)
http.use_ssl = true
request = Net::HTTP::Post.new(url.path, headers)
request.body = data.to_json
response = http.request(request)
puts response.body