Skip to content

Instantly share code, notes, and snippets.

@togakangaroo
Last active March 21, 2023 18:49
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
Star You must be signed in to star a gist
Save togakangaroo/8b0ca3a4d71f6c31ce22d2f40aed7932 to your computer and use it in GitHub Desktop.
ChatGPT telling me how to implement a chatgpt interface (untested so far)

ChatGPT Conversation

User: Write me an emacs lisp script that defines an interactive gim/chatgpt-query function. When this function is invoked, it will open up a comint buffer named *ChatGPT Query*.

When a query is entered into the buffer, use the chatgpt api to send the query to chatgpt using the gpt-4 model. Then show the response as output in the buffer.

The user might continue the conversation with chatgpt by entering more queries in the same buffer

ChatGPT: To create an Emacs Lisp script that defines an interactive gim/chatgpt-query function, which opens a comint buffer named *ChatGPT Query*, and sends the user's query to the ChatGPT API using the GPT-4 model, follow these steps:

Make sure you have a proper API key to access the ChatGPT API.Install the request library to handle API requests: M-x package-install RET request RET

Then, add the following Emacs Lisp code to your Emacs configuration file or a separate .el file:

(require 'comint)
(require 'request)

(defvar gim/chatgpt-api-key "your-api-key-here"
  "Your OpenAI API key for accessing the ChatGPT API.")

(defvar gim/chatgpt-api-url "https://api.openai.com/v1/engines/davinci-codex/completions"
  "The ChatGPT API URL for sending requests.")

(defun gim/chatgpt-send-query (query callback)
  "Send QUERY to the ChatGPT API and call CALLBACK with the response."
  (request
    gim/chatgpt-api-url
    :type "POST"
    :headers `(("Content-Type" . "application/json")
               ("Authorization" . ,(concat "Bearer " gim/chatgpt-api-key)))
    :data (json-encode `(("prompt" . ,query)
                         ("max_tokens" . 50)
                         ("n" . 1)
                         ("stop" . "\n")
                         ("temperature" . 0.5)))
    :parser 'json-read
    :success (cl-function
              (lambda (&key data &allow-other-keys)
                (funcall callback (elt (alist-get 'choices data) 0))))))

(defun gim/chatgpt-process-input ()
  "Process input in the *ChatGPT Query* buffer."
  (interactive)
  (let ((query (comint-get-old-input)))
    (comint-clear-input)
    (gim/chatgpt-send-query query (lambda (response)
                                     (let ((result (cdr (assq 'text response))))
                                       (with-current-buffer "*ChatGPT Query*"
                                         (comint-output-filter (get-buffer-process (current-buffer))
                                                               (concat result "\n"))))))))

(define-derived-mode gim/chatgpt-mode comint-mode "ChatGPT"
  "Major mode for interacting with ChatGPT."
  (setq comint-prompt-read-only t)
  (setq comint-input-sender 'ignore)
  (define-key gim/chatgpt-mode-map (kbd "RET") 'gim/chatgpt-process-input))

(defun gim/chatgpt-query ()
  "Open a *ChatGPT Query* buffer for interacting with ChatGPT."
  (interactive)
  (let ((buffer (get-buffer-create "*ChatGPT Query*")))
    (pop-to-buffer buffer)
    (unless (eq major-mode 'gim/chatgpt-mode)
      (gim/chatgpt-mode)
      (comint-output-filter (get-buffer-process (current-buffer))
                            "Type your query and press RET to get a response from ChatGPT.\n"))))
`

Make sure to replace "your-api-key-here" with your actual API key.

With this script, you can use M-x gim/chatgpt-query to open a *ChatGPT Query* buffer and interact with the ChatGPT API using the GPT-4 model. Enter your query, and press RET to send it to the API. The response will be displayed in the buffer, and you can continue the conversation by entering more queries.

Please note that this code

User: I want to keep the api key in the authinfo file - how do I get the initial version of it from there?

ChatGPT: You can use the auth-source-search function to retrieve the API key from your authinfo or authinfo.gpg file. Add an entry to your authinfo file in the following format:

machine api.openai.com login chatgpt-api-key password your-api-key-here

Make sure to replace "your-api-key-here" with your actual API key.

Then, update your Emacs Lisp script to retrieve the API key from the authinfo file using the auth-source-search function:

(require 'auth-source)

(defun gim/get-chatgpt-api-key ()
  "Retrieve the ChatGPT API key from the authinfo file."
  (let ((auth (nth 0 (auth-source-search
                      :host "api.openai.com"
                      :user "chatgpt-api-key"
                      :max 1
                      :require '(:secret)))))
    (if auth
        (funcall (plist-get auth :secret))
      (error "No ChatGPT API key found in authinfo file"))))

(defvar gim/chatgpt-api-key (gim/get-chatgpt-api-key)
  "Your OpenAI API key for accessing the ChatGPT API.")
`

This code defines a gim/get-chatgpt-api-key function that retrieves the API key from your authinfo file and sets the gim/chatgpt-api-key variable to the retrieved key. If the API key is not found, it raises an error.

Now, your script will use the API key from your authinfo file when making requests to the ChatGPT API.

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