Skip to content

Instantly share code, notes, and snippets.

@lambdaofgod
Created March 14, 2023 10:33
Show Gist options
  • Save lambdaofgod/df72486b0247a587dc5f7c764a166448 to your computer and use it in GitHub Desktop.
Save lambdaofgod/df72486b0247a587dc5f7c764a166448 to your computer and use it in GitHub Desktop.
ChatGPT in org mode
;; USING CHATGPT IN ORG MODE
;; how-to
;; relevant mlutil file - https://github.com/lambdaofgod/mlutil/blob/master/mlutil/chatgpt_api.py
;; - install mlutil (or copy relevant file somewhere so you can import it easily)
;; - M-x initialize-chatgpt-code-block
;; - M-x insert-chatgpt-response-code-block
;; - C-c C-c - you get the response
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;; babel code blocks in org mode
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
(defun insert-org-mode-code-block-with-content (code-block-args code-block-content)
(let* (
(code-block-start "#+BEGIN_SRC")
(code-block-end "#+END_SRC")
(n-backward (+ 1 (length code-block-end))))
(progn
(insert
(concat code-block-start " " code-block-args "\n" code-block-content "\n" code-block-end))
(backward-char n-backward))))
(defvar default-code-block-args " :exports both")
;;;;;;;;;;
;; ChatGPT
;;;;;;;;;;
;; put key here or change it to your key path
(defvar openai-key-path "~/.keys/openai_key.txt")
(defvar chatgpt-init-python-code (format "
from mlutil import chatgpt_api
api_key_path = '%s' # specify file path if OPENAI_API_KEY is not in env
chatgpt_client = chatgpt_api.ChatGPTClient(api_key_path)
" openai-key-path)
(defvar chatgpt-args (concat "python :session chatgpt " default-code-block-args))
(defun initialize-chatgpt-code-block ()
(interactive)
(insert-org-mode-code-block-with-content chatgpt-args chatgpt-init-python-code))
(defun insert-chatgpt-response-code-block (chatgpt-query)
(interactive "sAsk ChatGPT: ")
(insert-org-mode-code-block-with-content chatgpt-args
(format "chatgpt_client.get_chatgpt_response_from_text('%s')" chatgpt-query)))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment