Created
March 2, 2023 16:36
-
-
Save d1egoaz/1a02e709105bd3a2cd141c573018154b to your computer and use it in GitHub Desktop.
First attempt to use chatgpt using Emacs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
(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))) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Updated version moved to https://github.com/d1egoaz/dotemacs/blob/master/modules/diego-chatgpt.el