Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Implement a gptel-define-tool macro #685

Draft
wants to merge 1 commit into
base: master
Choose a base branch
from
Draft
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
53 changes: 53 additions & 0 deletions gptel.el
Original file line number Diff line number Diff line change
Expand Up @@ -1602,6 +1602,59 @@ implementation, used by OpenAI-compatible APIs and Ollama."
:additionalProperties :json-false))))))
(ensure-list tools))))

(cl-defmacro gptel-define-tool (function-name
(&rest args-list)
(&key name
description
async
category
confirm
include
&allow-other-keys)
&body body
&aux
(docstring (when (stringp (car body))
(car body))))
"Define an LLM-callable tool, FUNCTION-NAME.

TODO: Write docstring

Note, this macro will define the function, and install it as a tool. If
it is re-run, any changes will be made as necessary."
(declare (indent 3)
(doc-string 4))
(let* ((tool-name (or name
(replace-regexp-in-string "-" "_" (format "%s" function-name))))
(function-arguments (mapcar (lambda (arg-defn)
(if (listp arg-defn)
(car arg-defn)
arg-defn))
args-list))
(argument-descriptions (mapcar (lambda (arg-defn)
(when (listp arg-defn)
(cons :name
(cons (replace-regexp-in-string "-" "_" (format "%s" (car arg-defn)))
(cdr arg-defn)))))
args-list))
(description (or description
(and (stringp docstring)
(car (string-split docstring "\n"))))))
(unless (stringp description)
(error "A description of tool %s must be provided" function-name))
`(progn
(defun ,function-name (,@function-arguments)
,@body)
(gptel-make-tool
:name ,tool-name
:args ',argument-descriptions
:description ,description
:function ',function-name
,@(when async (list :async async))
,@(when category (list :category category))
,@(when confirm (list :confirm confirm))
,@(when include (list :include include)))
',function-name)))

(cl-defgeneric gptel--parse-tool-results (backend results)
"Return a BACKEND-appropriate prompt containing tool call RESULTS.

Expand Down