Skip to content

Commit

Permalink
fix a6319fd
Browse files Browse the repository at this point in the history
**Why:**
I just copy-pasted the code in a6319fd from a pr comment without
thinking about what it did

#122
  • Loading branch information
tillydray committed Oct 30, 2024
1 parent a6319fd commit 895aec2
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 14 deletions.
28 changes: 17 additions & 11 deletions org-ai-openai-image-query.el
Original file line number Diff line number Diff line change
Expand Up @@ -60,13 +60,13 @@ Calls CALLBACK with the response."

(defun org-ai--get-image-path-or-url ()
"Prompt the user for a non-empty image path or URL."
;; TODO make file path or url nicer to use
(let ((image-path-or-url ""))
(while (or (string-empty-p image-path-or-url)
(and (not (string-match-p "^https?://" image-path-or-url))
(not (file-exists-p image-path-or-url))))
(setq image-path-or-url (read-string "Image file path or URL (must be non-empty): ")))
image-path-or-url))
;; TODO ensure non-nil
(let ((default (or (thing-at-point 'url)
(thing-at-point 'filename))))
(read-string (if default
(format "Enter image URL or file path (default %s): " default)
"Enter image URL or file path: ")
nil 'minibuffer-history default)))

(defun org-ai--get-question ()
"Prompt the user for a non-empty question."
Expand All @@ -78,10 +78,16 @@ Calls CALLBACK with the response."
;;;###autoload
(defun org-ai-query-image ()
"Query OpenAI API with a BASE64 encoded image path or URL and a QUESTION."
(interactive (list
(read-file-name "Image file path: " nil nil nil (or (thing-at-point 'existing-filename)
(thing-at-point 'url)))
(read-string "Question: " nil 'minibuffer-history))))
(interactive)
(let ((image-path-or-url (org-ai--get-image-path-or-url))
(question (org-ai--get-question)))
(if (string-match-p "^https?://" image-path-or-url)
(org-ai--send-url-image-query image-path-or-url question #'org-ai--handle-openai-response)
(let ((base64-image (with-temp-buffer
(insert-file-contents-literally image-path-or-url)
(base64-encode-region (point-min) (point-max))
(buffer-string))))
(org-ai--send-base64-image-query base64-image question #'org-ai--handle-openai-response)))))

(provide 'org-ai-openai-image-query)

Expand Down
6 changes: 3 additions & 3 deletions org-ai.el
Original file line number Diff line number Diff line change
Expand Up @@ -187,10 +187,10 @@ It's designed to \"do the right thing\":

;; -=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-

(defun org-ai-query-image-and-display (image-path question)
(defun org-ai-query-image-and-display ()
"Query OpenAI API with a BASE64 encoded IMAGE-PATH and a QUESTION, and display the result."
(interactive "fImage file path: \nsQuestion: ")
(org-ai-query-image image-path question))
(interactive)
(org-ai-query-image))

(defvar org-ai-mode-map (make-sparse-keymap)
"Keymap for `org-ai-mode'.")
Expand Down

0 comments on commit 895aec2

Please sign in to comment.