Skip to content

Instantly share code, notes, and snippets.

@d1egoaz
Created March 2, 2023 16:36
Show Gist options
  • Save d1egoaz/1a02e709105bd3a2cd141c573018154b to your computer and use it in GitHub Desktop.
Save d1egoaz/1a02e709105bd3a2cd141c573018154b to your computer and use it in GitHub Desktop.
First attempt to use chatgpt using Emacs
(require 'request)
(require 'json)
(defun chatgpt-query (input)
"Interact with the ChatGPT API and return the response."
(let* ((api-key "<YOUR API KEY>")
(url "https://api.openai.com/v1/chat/completions")
(model "gpt-3.5-turbo")
(headers `(("Content-Type" . "application/json")
("Authorization" . ,(concat "Bearer " api-key))))
(response (request
url
:type "POST"
:headers headers
:data (json-encode
`(:model ,model :messages [(:role "user" :content ,input)]))
:parser 'json-read
:success (cl-function
(lambda (&key data &allow-other-keys)
(message "🤖%s" (let ((message-content (aref (cdr (assoc 'choices data)) 0)))
(cdr (assoc 'content (cdr (assoc 'message message-content)))))))))))))
(defun chatgpt ()
"Interact with the ChatGPT API and display the response."
(interactive)
(let ((input (read-string "Enter your input: ")))
(chatgpt-query input)))
@d1egoaz
Copy link
Author

d1egoaz commented Mar 3, 2023

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment