branch: externals/llm commit 1db38147a5af03aff19ab9ceed87b8697ce1315d Author: Andrew Hyatt <ahy...@gmail.com> Commit: GitHub <nore...@github.com>
Use multiple reasoning tags for ollama via llm-ollama-reasoning-tags (#175) This will fix https://github.com/ahyatt/llm/issues/170. --- llm-ollama.el | 38 ++++++++++++++++++++++++++++++-------- 1 file changed, 30 insertions(+), 8 deletions(-) diff --git a/llm-ollama.el b/llm-ollama.el index 4b34153388..40975e093b 100644 --- a/llm-ollama.el +++ b/llm-ollama.el @@ -105,6 +105,12 @@ PROVIDER is the llm-ollama provider." (cl-defmethod llm-provider-batch-embeddings-extract-result ((_ llm-ollama) response) (append (assoc-default 'embeddings response) nil)) +(eval-and-compile + (defconst llm-ollama-reasoning-tags '("think" "reasoning") + "A list of possibilities for reasoning tags in Ollama responses. + +These are just the text inside the tag, not the tag itself.")) + (cl-defmethod llm-provider-chat-extract-result ((_ llm-ollama) response) "Return the chat response from the server RESPONSE." (let ((raw-result (assoc-default 'content (assoc-default 'message response)))) @@ -113,7 +119,9 @@ PROVIDER is the llm-ollama provider." (with-temp-buffer (insert raw-result) (goto-char 0) - (if (search-forward "\n</think>" nil t) + (if (seq-find (lambda (tag) + (search-forward (format "</%s>" tag) nil t)) + llm-ollama-reasoning-tags) (string-trim (buffer-substring (point) (point-max))) raw-result)))) @@ -124,11 +132,19 @@ PROVIDER is the llm-ollama provider." (with-temp-buffer (insert raw-result) (goto-char 0) - (when (search-forward "<think>\n" nil t) - (let* ((endtag "\n</think>") - (end (save-excursion - (search-forward endtag)))) - (buffer-substring (point) (- end (length endtag)))))))) + (when (re-search-forward + (rx (seq (literal "<") + (group (eval `(or ,@llm-ollama-reasoning-tags))) + (literal ">"))) + nil t) + (when-let* ((end (save-excursion + (re-search-forward + (rx (seq + (literal "</") + (group (literal (match-string 1))) + (literal ">"))) nil t)))) + ;; +3 to account for the length of the two brackets and slash + (buffer-substring (point) (- end (+ 3 (length (match-string 1)))))))))) (defun llm-ollama--response-format (format) "Return the response format for FORMAT." @@ -219,9 +235,15 @@ PROVIDER is the llm-ollama provider." ;; The response from ollama should just have the tag and ;; nothing more. (cond - ((string-match "<think>" response) + ((string-match (rx + (seq "<" + (eval `(or ,@llm-ollama-reasoning-tags)) + ">")) response) (setq in-reasoning t)) - ((string-match "</think>" response) + ((string-match (rx + (seq "</" + (eval `(or ,@llm-ollama-reasoning-tags)) + ">")) response) (setq in-reasoning nil)) (t (funcall receiver (list (if in-reasoning :reasoning