emacs-elpa-diffs
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

[nongnu] elpa/gptel dade9ec8e1 231/273: gptel: Add introspection command


From: ELPA Syncer
Subject: [nongnu] elpa/gptel dade9ec8e1 231/273: gptel: Add introspection commands
Date: Wed, 1 May 2024 10:02:39 -0400 (EDT)

branch: elpa/gptel
commit dade9ec8e1a8e129ce9530d3f48faa1504b6d8ad
Author: Karthik Chikmagalur <karthikchikmagalur@gmail.com>
Commit: Karthik Chikmagalur <karthikchikmagalur@gmail.com>

    gptel: Add introspection commands
    
    * gptel.el (gptel--inspect-query): Add a function to inspect the
    query that will be sent.  This is useful to ensure that (only and
    all of) what you expect to be sent is being sent.
    
    * gptel-transient.el (gptel-menu): Allow the query in progress to
    be inspected from `gptel-menu` as an elisp or JSON object.
---
 gptel-transient.el | 15 ++++++++++++++-
 gptel.el           | 25 +++++++++++++++++++++++++
 2 files changed, 39 insertions(+), 1 deletion(-)

diff --git a/gptel-transient.el b/gptel-transient.el
index 971711c1a1..119c5ad93f 100644
--- a/gptel-transient.el
+++ b/gptel-transient.el
@@ -174,7 +174,20 @@ which see."
      :if gptel--at-response-history-p
      :transient t)
     ("E" "Ediff previous" gptel--ediff
-     :if gptel--at-response-history-p)]]
+     :if gptel--at-response-history-p)]
+   ["Inspect"
+    ("I" "Query as Lisp"
+     (lambda ()
+       "Inspect the query that will be sent as a lisp object."
+       (interactive)
+       (gptel--sanitize-model)
+       (gptel--inspect-query)))
+    ("J" "Query as JSON"
+     (lambda ()
+       "Inspect the query that will be sent as a JSON object."
+       (interactive)
+       (gptel--sanitize-model)
+       (gptel--inspect-query 'json)))]]
   (interactive)
   (gptel--sanitize-model)
   (transient-setup 'gptel-menu))
diff --git a/gptel.el b/gptel.el
index a75d905e82..bdf89d16e9 100644
--- a/gptel.el
+++ b/gptel.el
@@ -955,6 +955,31 @@ waiting for the response."
   (gptel-request nil :stream gptel-stream)
   (gptel--update-status " Waiting..." 'warning)))
 
+(declare-function json-pretty-print-buffer "json")
+(defun gptel--inspect-query (&optional arg)
+  "Show the full LLM query to be sent in a new buffer.
+
+This functions as a dry run of `gptel-send'.  If prefix ARG is
+the symbol json, show the encoded JSON query instead of the lisp
+structure gptel uses."
+  (let* ((request-data
+          (gptel-request nil :stream gptel-stream :dry-run t)))
+    (with-current-buffer (get-buffer-create "*gptel-query*")
+      (let ((standard-output (current-buffer))
+            (inhibit-read-only t))
+        (buffer-disable-undo)
+        (erase-buffer)
+        (if (eq arg 'json)
+            (progn (fundamental-mode)
+                   (insert (gptel--json-encode request-data))
+                   (json-pretty-print-buffer))
+          (lisp-data-mode)
+          (prin1 request-data)
+          (pp-buffer))
+        (goto-char (point-min))
+        (view-mode 1)
+        (display-buffer (current-buffer) gptel-display-buffer-action)))))
+
 (defun gptel--insert-response (response info)
   "Insert the LLM RESPONSE into the gptel buffer.
 



reply via email to

[Prev in Thread] Current Thread [Next in Thread]