Skip to content

Commit da01adf

Browse files
committed
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.
1 parent f08065d commit da01adf

File tree

2 files changed

+12
-13
lines changed

2 files changed

+12
-13
lines changed

gptel-anthropic.el

Lines changed: 7 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -144,8 +144,7 @@ information if the stream contains it. Not my best work, I know."
144144
(mapc (lambda (tool-call)
145145
(plist-put tool-call :args (plist-get tool-call :input))
146146
(plist-put tool-call :input nil)
147-
(plist-put tool-call :id (gptel--anthropic-unformat-tool-id
148-
(plist-get tool-call :id))))
147+
(plist-put tool-call :id (plist-get tool-call :id)))
149148
tool-use))
150149
(plist-put info :output-tokens
151150
(map-nested-elt response '(:usage :output_tokens)))
@@ -198,8 +197,7 @@ Mutate state INFO with response metadata."
198197
for call = (copy-sequence call-raw) do
199198
(plist-put call :args (plist-get call :input))
200199
(plist-put call :input nil)
201-
(plist-put call :id (gptel--anthropic-unformat-tool-id
202-
(plist-get call :id)))
200+
(plist-put call :id (plist-get call :id))
203201
collect call into calls
204202
finally do (plist-put info :tool-use calls)))
205203
finally return
@@ -287,8 +285,7 @@ TOOL-USE is a list of plists containing tool names, arguments and call results."
287285
(let* ((result (plist-get tool-call :result))
288286
(formatted
289287
(list :type "tool_result"
290-
:tool_use_id (gptel--anthropic-format-tool-id
291-
(plist-get tool-call :id))
288+
:tool_use_id (plist-get tool-call :id)
292289
:content (if (stringp result) result
293290
(prin1-to-string result)))))
294291
(prog1 formatted
@@ -299,12 +296,14 @@ TOOL-USE is a list of plists containing tool names, arguments and call results."
299296
;; NOTE: No `gptel--inject-prompt' method required for gptel-anthropic, since
300297
;; this is handled by its defgeneric implementation
301298

299+
;; TODO: Remove these functions (#792)
302300
(defun gptel--anthropic-format-tool-id (tool-id)
303301
(unless tool-id
304302
(setq tool-id (substring
305303
(md5 (format "%s%s" (random) (float-time)))
306304
nil 24)))
307-
(if (string-prefix-p "toolu_" tool-id)
305+
(if (or (string-prefix-p "call_" tool-id)
306+
(string-prefix-p "toolu_" tool-id))
308307
tool-id
309308
(format "toolu_%s" tool-id)))
310309

@@ -354,7 +353,7 @@ TOOL-USE is a list of plists containing tool names, arguments and call results."
354353
(save-excursion
355354
(condition-case nil
356355
(let* ((tool-call (read (current-buffer)))
357-
(id (gptel--anthropic-format-tool-id id))
356+
;; (id (gptel--anthropic-format-tool-id id))
358357
(name (plist-get tool-call :name))
359358
(arguments (plist-get tool-call :args)))
360359
(plist-put tool-call :id id)

gptel-openai.el

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -201,7 +201,7 @@ information if the stream contains it."
201201
(cl-loop
202202
for tool-call in tool-use ; Construct the call specs for running the function calls
203203
for spec = (plist-get tool-call :function)
204-
collect (list :id (gptel--openai-unformat-tool-id (plist-get tool-call :id))
204+
collect (list :id (plist-get tool-call :id)
205205
:name (plist-get spec :name)
206206
:args (ignore-errors (gptel--json-read-string
207207
(plist-get spec :arguments))))
@@ -274,7 +274,7 @@ Mutate state INFO with response metadata."
274274
(gptel--json-read-string
275275
(plist-get call-spec :arguments))))
276276
(plist-put call-spec :arguments nil)
277-
(plist-put call-spec :id (gptel--openai-unformat-tool-id (plist-get tool-call :id)))
277+
(plist-put call-spec :id (plist-get tool-call :id))
278278
collect call-spec into tool-use
279279
finally (plist-put info :tool-use tool-use)))))))
280280

@@ -322,11 +322,11 @@ Mutate state INFO with response metadata."
322322
(lambda (tool-call)
323323
(list
324324
:role "tool"
325-
:tool_call_id (gptel--openai-format-tool-id
326-
(plist-get tool-call :id))
325+
:tool_call_id (plist-get tool-call :id)
327326
:content (plist-get tool-call :result)))
328327
tool-use))
329328

329+
;; TODO: Remove these functions (#792)
330330
(defun gptel--openai-format-tool-id (tool-id)
331331
"Format TOOL-ID for OpenAI.
332332
@@ -384,7 +384,7 @@ If the ID has the format used by a different backend, use as-is."
384384
(push (list :role "assistant"
385385
:tool_calls
386386
(vector (list :type "function"
387-
:id (gptel--openai-format-tool-id id)
387+
:id id
388388
:function `( :name ,name
389389
:arguments ,arguments))))
390390
prompts))

0 commit comments

Comments
 (0)