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

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

[elpa] externals/llm 0bc9c88782: Fix response breaking when prompts run


From: ELPA Syncer
Subject: [elpa] externals/llm 0bc9c88782: Fix response breaking when prompts run afoul of Gemini safety checks
Date: Sun, 28 Jan 2024 00:58:04 -0500 (EST)

branch: externals/llm
commit 0bc9c88782e0a72b46aa4759708c4b04baa70f86
Author: Andrew Hyatt <ahyatt@gmail.com>
Commit: Andrew Hyatt <ahyatt@gmail.com>

    Fix response breaking when prompts run afoul of Gemini safety checks
    
    This should fix https://github.com/ahyatt/llm/issues/19
---
 NEWS.org      |  1 +
 llm-vertex.el | 37 +++++++++++++++++++++++++------------
 2 files changed, 26 insertions(+), 12 deletions(-)

diff --git a/NEWS.org b/NEWS.org
index 891c1c1025..ade9e7f72b 100644
--- a/NEWS.org
+++ b/NEWS.org
@@ -1,5 +1,6 @@
 * Version 0.9.1
 - Default to the new "text-embedding-3-small" model for Open AI.  *Important*: 
Anyone who has stored embeddings should either regenerate embeddings 
(recommended) or hard-code the old embedding model ("text-embedding-ada-002").
+- Fix response breaking when prompts run afoul of Gemini / Vertex's safety 
checks.
 * Version 0.9
 - Add =llm-chat-token-limit= to find the token limit based on the model.
 - Add request timeout customization.
diff --git a/llm-vertex.el b/llm-vertex.el
index 87e4465cab..a48b4434ef 100644
--- a/llm-vertex.el
+++ b/llm-vertex.el
@@ -151,12 +151,15 @@ This handles different kinds of models."
   (pcase (type-of response)
     ('vector (mapconcat #'llm-vertex--get-chat-response-streaming
                         response ""))
-    ('cons (let ((parts (assoc-default 'parts
-                                       (assoc-default 'content 
-                                                      (aref (assoc-default 
'candidates response) 0)))))
-             (if parts
-                 (assoc-default 'text (aref parts 0))
-               "")))))
+    ('cons (if (assoc-default 'candidates response)
+               (let ((parts (assoc-default
+                             'parts
+                             (assoc-default 'content
+                                            (aref (assoc-default 'candidates 
response) 0)))))
+                 (if parts
+                     (assoc-default 'text (aref parts 0))
+                   ""))
+             "NOTE: No response was sent back by the LLM, the prompt may have 
violated safety checks."))))
 
 (defun llm-vertex--get-partial-chat-ui-repsonse (response)
   "Return the partial response from as much of RESPONSE as we can parse.
@@ -175,12 +178,22 @@ If the response is not parseable, return nil."
         (condition-case nil
             (when-let
                 ((json (ignore-errors
-                        (json-read-from-string
-                         (concat
-                          (buffer-substring-no-properties
-                           start end-of-valid-chunk)
-                          ;; Close off the json
-                          "]")))))
+                         (or
+                          (json-read-from-string
+                           (concat
+                            (buffer-substring-no-properties
+                             start end-of-valid-chunk)
+                            ;; Close off the json
+                            "]"))
+                          ;; Needed when we only get a promptFeedback back,
+                          ;; which happens when the prompt violates safety
+                          ;; checks.
+                          (json-read-from-string
+                           (buffer-substring-no-properties
+                            start (save-excursion
+                                    (goto-char (point-max))
+                                    (search-backward "}" nil t)
+                                    (1+ (point)))))))))
               (llm-vertex--get-chat-response-streaming json))
           (error (message "Unparseable buffer saved to 
*llm-vertex-unparseable*")
                  (with-current-buffer (get-buffer-create 
"*llm-vertex-unparseable*")



reply via email to

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