branch: elpa/gptel
commit 1d0b3d112bd74ecd9f0789b5ce2526d0e9992fb6
Author: Karthik Chikmagalur <karthikchikmaga...@gmail.com>
Commit: Karthik Chikmagalur <karthikchikmaga...@gmail.com>

    gptel-ollama: Parse tool results from buffer
    
    * gptel-ollama.el (gptel--parse-buffer):
    - Create tool call and result messages from each recorded call
    
    Parse tool messages in the buffer.  This commit covers the
    Ollama backend.  The strategy is to pcase on the gptel
    property.  Since tool insertions have one distinct gptel text
    property, the call and response message are recovered at the same
    time.
    
    Introduce support for the `ignore' property in the Ollama
    prompt creator.  Users can make use of it to have non-conversation
    content intermingled with chat.
    
    NOTE: Ollama does not use tool-call ids, so no ids are stored in
    the gptel text property.  This can cause problems if the user
    switches from Ollama to a backend that does use ids.
---
 gptel-ollama.el | 21 ++++++++++++++++++++-
 1 file changed, 20 insertions(+), 1 deletion(-)

diff --git a/gptel-ollama.el b/gptel-ollama.el
index cb62108b12..1b530ed0a7 100644
--- a/gptel-ollama.el
+++ b/gptel-ollama.el
@@ -141,7 +141,7 @@ Store response metadata in state INFO."
            if text collect
            (list :role (if role "user" "assistant") :content text)))
 
-(cl-defmethod gptel--parse-buffer ((_backend gptel-ollama) &optional 
max-entries)
+(cl-defmethod gptel--parse-buffer ((backend gptel-ollama) &optional 
max-entries)
   (let ((prompts) (prev-pt (point))
         (include-media (and gptel-track-media (or (gptel--model-capable-p 
'media)
                                                   (gptel--model-capable-p 
'url)))))
@@ -155,6 +155,25 @@ Store response metadata in state INFO."
              (when-let* ((content (gptel--trim-prefixes
                                    (buffer-substring-no-properties (point) 
prev-pt))))
                (push (list :role "assistant" :content content) prompts)))
+            (`(tool . ,_id)
+             (save-excursion
+               (condition-case nil
+                   (let* ((tool-call (read (current-buffer)))
+                          (name (plist-get tool-call :name))
+                          (arguments (plist-get tool-call :args)))
+                     (plist-put tool-call :result
+                                (string-trim (buffer-substring-no-properties
+                                              (point) prev-pt)))
+                     (push (car (gptel--parse-tool-results backend (list 
tool-call)))
+                           prompts)
+                     (push (list :role "assistant"
+                                 :content ""
+                                 :tool_calls `[(:function (:name ,name 
:arguments ,arguments))])
+                           prompts))
+                 ((end-of-file invalid-read-syntax)
+                  (message (format "Could not parse tool-call on line %s"
+                                   (line-number-at-pos (point))))))))
+            ('ignore)
             ('nil
              (if include-media
                  (when-let* ((content (gptel--ollama-parse-multipart

Reply via email to