Last update: 2023-11-17

ChatGPTにリクエストして結果を取得したい   Emacs EmacsLisp ChatGPT

Emacs LispUrlPackageを使ってPOSTし、ChatGPTに対してリクエストを送信してみた。

(defun my-url-http-post (url args)
  "Send ARGS to URL as a POST request."
  (let ((url-request-method "POST")
        (url-request-extra-headers
         `(("Content-Type" . "application/json")
           ("Authorization" . ,(format "Bearer %s" org-babel-chatgpt-api-token))))
        (url-request-data (json-encode-alist `(:model "gpt-3.5-turbo" :stream t :messages [((:role . "user")(:content . "hi"))]))))
    ;; if you want, replace `my-switch-to-url-buffer' with `my-kill-url-buffer'
    (url-retrieve-synchronously url)))

(with-current-buffer (my-url-http-post "https://api.openai.com/v1/chat/completions" nil)
  (buffer-substring url-http-end-of-headers (point-max)))

data: {"id":"chatcmpl-8LhbHQ2EI8WxDOExCMXemNAJoFHoQ","object":"chat.completion.chunk","created":1700183179,"model":"gpt-3.5-turbo-0613","choices":[{"index":0,"delta":{"role":"assistant","content":""},"finish_reason":null}]}

data: {"id":"chatcmpl-8LhbHQ2EI8WxDOExCMXemNAJoFHoQ","object":"chat.completion.chunk","created":1700183179,"model":"gpt-3.5-turbo-0613","choices":[{"index":0,"delta":{"content":"Hello"},"finish_reason":null}]}

data: {"id":"chatcmpl-8LhbHQ2EI8WxDOExCMXemNAJoFHoQ","object":"chat.completion.chunk","created":1700183179,"model":"gpt-3.5-turbo-0613","choices":[{"index":0,"delta":{"content":"!"},"finish_reason":null}]}

data: {"id":"chatcmpl-8LhbHQ2EI8WxDOExCMXemNAJoFHoQ","object":"chat.completion.chunk","created":1700183179,"model":"gpt-3.5-turbo-0613","choices":[{"index":0,"delta":{"content":" How"},"finish_reason":null}]}

data: {"id":"chatcmpl-8LhbHQ2EI8WxDOExCMXemNAJoFHoQ","object":"chat.completion.chunk","created":1700183179,"model":"gpt-3.5-turbo-0613","choices":[{"index":0,"delta":{"content":" can"},"finish_reason":null}]}

data: {"id":"chatcmpl-8LhbHQ2EI8WxDOExCMXemNAJoFHoQ","object":"chat.completion.chunk","created":1700183179,"model":"gpt-3.5-turbo-0613","choices":[{"index":0,"delta":{"content":" I"},"finish_reason":null}]}

data: {"id":"chatcmpl-8LhbHQ2EI8WxDOExCMXemNAJoFHoQ","object":"chat.completion.chunk","created":1700183179,"model":"gpt-3.5-turbo-0613","choices":[{"index":0,"delta":{"content":" assist"},"finish_reason":null}]}

data: {"id":"chatcmpl-8LhbHQ2EI8WxDOExCMXemNAJoFHoQ","object":"chat.completion.chunk","created":1700183179,"model":"gpt-3.5-turbo-0613","choices":[{"index":0,"delta":{"content":" you"},"finish_reason":null}]}

data: {"id":"chatcmpl-8LhbHQ2EI8WxDOExCMXemNAJoFHoQ","object":"chat.completion.chunk","created":1700183179,"model":"gpt-3.5-turbo-0613","choices":[{"index":0,"delta":{"content":" today"},"finish_reason":null}]}

data: {"id":"chatcmpl-8LhbHQ2EI8WxDOExCMXemNAJoFHoQ","object":"chat.completion.chunk","created":1700183179,"model":"gpt-3.5-turbo-0613","choices":[{"index":0,"delta":{"content":"?"},"finish_reason":null}]}

data: {"id":"chatcmpl-8LhbHQ2EI8WxDOExCMXemNAJoFHoQ","object":"chat.completion.chunk","created":1700183179,"model":"gpt-3.5-turbo-0613","choices":[{"index":0,"delta":{},"finish_reason":"stop"}]}

data: [DONE]