branch: elpa/gptel commit 8b04be27c85f1ef0c72af0b75a7dcc4ad34c3181 Author: Karthik Chikmagalur <karthikchikmaga...@gmail.com> Commit: Karthik Chikmagalur <karthikchikmaga...@gmail.com>
gptel-org: Check Org version for branching context gptel-org.el (gptel-org--create-prompt): If `org-element-lineage-map` is not available, ignore the value of `gptel-org-branching-context` and display a warning (#294). README: Mention Org version requirement for `gptel-org-branching-context` and the ai-org-chat package as an alternative. --- README.org | 1 + gptel-org.el | 73 ++++++++++++++++++++++++++++++++---------------------------- 2 files changed, 40 insertions(+), 34 deletions(-) diff --git a/README.org b/README.org index 9aa0a9a1ae..1ba1d3119e 100644 --- a/README.org +++ b/README.org @@ -623,6 +623,7 @@ gptel offers a few extra conveniences in Org mode. - You can limit the conversation context to an Org heading with the command =gptel-org-set-topic=. - You can have branching conversations in Org mode, where each hierarchical outline path through the document is a separate conversation branch. This is also useful for limiting the context size of each query. See the variable =gptel-org-branching-context=. + Note: using this option requires Org 9.6.7 or higher to be available. The [[https://github.com/ultronozm/ai-org-chat.el][ai-org-chat]] package uses gptel to provide this branching conversation behavior for older versions of Org. - You can declare the gptel model, backend, temperature, system message and other parameters as Org properties with the command =gptel-org-set-properties=. gptel queries under the corresponding heading will always use these settings, allowing you to create mostly reproducible LLM chat notebooks, and to have simultaneous chats with different models, model settings and directives under different Org headings. diff --git a/gptel-org.el b/gptel-org.el index 18879a803f..9f8ca25487 100644 --- a/gptel-org.el +++ b/gptel-org.el @@ -163,40 +163,45 @@ value of `gptel-org-branching-context', which see." (narrow-to-region topic-start prompt-end)) (if gptel-org-branching-context ;; Create prompt from direct ancestors of point - (save-excursion - (let* ((org-buf (current-buffer)) - (start-bounds (gptel-org--element-lineage-map - (org-element-at-point) #'org-element-begin - '(headline org-data) 'with-self)) - (end-bounds - (cl-loop - for pos in (cdr start-bounds) - while - (and (>= pos (point-min)) ;respect narrowing - (goto-char pos) - ;; org-element-lineage always returns an extra - ;; (org-data) element at point 1. If there is also a - ;; heading here, it is either a false positive or we - ;; would be double counting it. So we reject this node - ;; when also at a heading. - (not (and (eq pos 1) (org-at-heading-p)))) - do (outline-next-heading) - collect (point) into ends - finally return (cons prompt-end ends)))) - (with-temp-buffer - (setq-local gptel-backend - (buffer-local-value 'gptel-backend org-buf) - gptel--system-message - (buffer-local-value 'gptel--system-message org-buf) - gptel-model - (buffer-local-value 'gptel-model org-buf)) - (cl-loop for start in start-bounds - for end in end-bounds - do (insert-buffer-substring org-buf start end) - (goto-char (point-min))) - (goto-char (point-max)) - (let ((major-mode 'org-mode)) - (gptel--parse-buffer gptel-backend max-entries))))) + (if (fboundp 'org-element-lineage-map) + (save-excursion + (let* ((org-buf (current-buffer)) + (start-bounds (gptel-org--element-lineage-map + (org-element-at-point) #'org-element-begin + '(headline org-data) 'with-self)) + (end-bounds + (cl-loop + for pos in (cdr start-bounds) + while + (and (>= pos (point-min)) ;respect narrowing + (goto-char pos) + ;; org-element-lineage always returns an extra + ;; (org-data) element at point 1. If there is also a + ;; heading here, it is either a false positive or we + ;; would be double counting it. So we reject this node + ;; when also at a heading. + (not (and (eq pos 1) (org-at-heading-p)))) + do (outline-next-heading) + collect (point) into ends + finally return (cons prompt-end ends)))) + (with-temp-buffer + (setq-local gptel-backend + (buffer-local-value 'gptel-backend org-buf) + gptel--system-message + (buffer-local-value 'gptel--system-message org-buf) + gptel-model + (buffer-local-value 'gptel-model org-buf)) + (cl-loop for start in start-bounds + for end in end-bounds + do (insert-buffer-substring org-buf start end) + (goto-char (point-min))) + (goto-char (point-max)) + (let ((major-mode 'org-mode)) + (gptel--parse-buffer gptel-backend max-entries))))) + (display-warning + '(gptel org) + "Using `gptel-org-branching-context' requires Org version 9.6.7 or higher, it will be ignored.") + (gptel--parse-buffer gptel-backend max-entries)) ;; Create prompt the usual way (gptel--parse-buffer gptel-backend max-entries))))