branch: elpa/gptel commit da01adfff462f8302ffb6e5076b84d31d6e0ac83 Author: Karthik Chikmagalur <karthikchikmaga...@gmail.com> Commit: Karthik Chikmagalur <karthikchikmaga...@gmail.com>
gptel: (Testing) Don't (un)format tool ids (#792) It appears that the OpenAI and Anthropic APIs do not complain any more about tool IDs being in the wrong format for the API (e.g. call_abc when it expects toolu_abc). Temporarily disable tool-id unformatting and formatting. * gptel-anthropic.el (gptel-curl--parse-stream, gptel--parse-response, gptel--parse-tool-results, gptel--anthropic-format-tool-id, gptel--parse-buffer): Disable tool-id formatting. * gptel-openai.el (gptel-curl--parse-stream, gptel--parse-response, gptel--parse-tool-results, gptel--parse-buffer): Disable tool-id formatting. --- gptel-anthropic.el | 15 +++++++-------- gptel-openai.el | 10 +++++----- 2 files changed, 12 insertions(+), 13 deletions(-) diff --git a/gptel-anthropic.el b/gptel-anthropic.el index 7ec53be442..2e2433d88f 100644 --- a/gptel-anthropic.el +++ b/gptel-anthropic.el @@ -144,8 +144,7 @@ information if the stream contains it. Not my best work, I know." (mapc (lambda (tool-call) (plist-put tool-call :args (plist-get tool-call :input)) (plist-put tool-call :input nil) - (plist-put tool-call :id (gptel--anthropic-unformat-tool-id - (plist-get tool-call :id)))) + (plist-put tool-call :id (plist-get tool-call :id))) tool-use)) (plist-put info :output-tokens (map-nested-elt response '(:usage :output_tokens))) @@ -198,8 +197,7 @@ Mutate state INFO with response metadata." for call = (copy-sequence call-raw) do (plist-put call :args (plist-get call :input)) (plist-put call :input nil) - (plist-put call :id (gptel--anthropic-unformat-tool-id - (plist-get call :id))) + (plist-put call :id (plist-get call :id)) collect call into calls finally do (plist-put info :tool-use calls))) finally return @@ -287,8 +285,7 @@ TOOL-USE is a list of plists containing tool names, arguments and call results." (let* ((result (plist-get tool-call :result)) (formatted (list :type "tool_result" - :tool_use_id (gptel--anthropic-format-tool-id - (plist-get tool-call :id)) + :tool_use_id (plist-get tool-call :id) :content (if (stringp result) result (prin1-to-string result))))) (prog1 formatted @@ -299,12 +296,14 @@ TOOL-USE is a list of plists containing tool names, arguments and call results." ;; NOTE: No `gptel--inject-prompt' method required for gptel-anthropic, since ;; this is handled by its defgeneric implementation +;; TODO: Remove these functions (#792) (defun gptel--anthropic-format-tool-id (tool-id) (unless tool-id (setq tool-id (substring (md5 (format "%s%s" (random) (float-time))) nil 24))) - (if (string-prefix-p "toolu_" tool-id) + (if (or (string-prefix-p "call_" tool-id) + (string-prefix-p "toolu_" tool-id)) tool-id (format "toolu_%s" tool-id))) @@ -354,7 +353,7 @@ TOOL-USE is a list of plists containing tool names, arguments and call results." (save-excursion (condition-case nil (let* ((tool-call (read (current-buffer))) - (id (gptel--anthropic-format-tool-id id)) + ;; (id (gptel--anthropic-format-tool-id id)) (name (plist-get tool-call :name)) (arguments (plist-get tool-call :args))) (plist-put tool-call :id id) diff --git a/gptel-openai.el b/gptel-openai.el index 9347527b5f..c659d74df1 100644 --- a/gptel-openai.el +++ b/gptel-openai.el @@ -201,7 +201,7 @@ information if the stream contains it." (cl-loop for tool-call in tool-use ; Construct the call specs for running the function calls for spec = (plist-get tool-call :function) - collect (list :id (gptel--openai-unformat-tool-id (plist-get tool-call :id)) + collect (list :id (plist-get tool-call :id) :name (plist-get spec :name) :args (ignore-errors (gptel--json-read-string (plist-get spec :arguments)))) @@ -274,7 +274,7 @@ Mutate state INFO with response metadata." (gptel--json-read-string (plist-get call-spec :arguments)))) (plist-put call-spec :arguments nil) - (plist-put call-spec :id (gptel--openai-unformat-tool-id (plist-get tool-call :id))) + (plist-put call-spec :id (plist-get tool-call :id)) collect call-spec into tool-use finally (plist-put info :tool-use tool-use))))))) @@ -322,11 +322,11 @@ Mutate state INFO with response metadata." (lambda (tool-call) (list :role "tool" - :tool_call_id (gptel--openai-format-tool-id - (plist-get tool-call :id)) + :tool_call_id (plist-get tool-call :id) :content (plist-get tool-call :result))) tool-use)) +;; TODO: Remove these functions (#792) (defun gptel--openai-format-tool-id (tool-id) "Format TOOL-ID for OpenAI. @@ -384,7 +384,7 @@ If the ID has the format used by a different backend, use as-is." (push (list :role "assistant" :tool_calls (vector (list :type "function" - :id (gptel--openai-format-tool-id id) + :id id :function `( :name ,name :arguments ,arguments)))) prompts))