-
Notifications
You must be signed in to change notification settings - Fork 162
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
[Looking for feedback] Add support for Function Calls #209
base: master
Are you sure you want to change the base?
Conversation
@@ -386,6 +386,9 @@ To set the model for a chat session interactively call | |||
(const :tag "GPT 4 32k" "gpt-4-32k") | |||
(const :tag "GPT 4 1106 (preview)" "gpt-4-1106-preview"))) | |||
|
|||
(defcustom gptel-callable-functions nil |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I wasn't sure where to put this
@@ -73,16 +74,24 @@ | |||
(apply #'concat (nreverse content-strs)))) | |||
|
|||
(cl-defmethod gptel--parse-response ((_backend gptel-openai) response _info) | |||
(map-nested-elt response '(:choices 0 :message :content))) | |||
;; If the reply specifies a function call, parse and return it instead of the message |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
If GPT decides a function should be called, the structured data is included in tool_calls
instead of content
so that parsing needs to be updated.
For now I'm only parsing the full response, streaming is not supported yet (but could be)
(when gptel-callable-functions | ||
(plist-put prompts-plist :tools gptel-callable-functions)) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This is the main important change. This could be toggled on or off based on a setting in the transient. A certain function call can also be forced by using the tool_choice
parameter
|
||
;;; Callable function schema | ||
(setq! gptel-callable-functions | ||
;; Hard coded variable specifying callable functions. This could be defined in a user's configuration |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Given a list of emacs functions, we should be able to introspect them to generate this schema data if that's preferable.
(cdr item)))) | ||
plist)) | ||
|
||
(cl-defun gptel-run-function-on-region (beg end) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
For now this is just a post-response hook function, but this would likely be more appropriate in the core gptel source. I'm not sure exactly where though.
This is fantastic -- and quite a small change too! Thanks for the PR, this looks quite promising. I'm not familiar with the OpenAI function-calling API. I'll take a look at it soon and then think about the UI, it'll probably be a few days from now. |
440da06
to
9a5a4a6
Compare
This PR would be nice to have for my use case. Anything I can do to push it along? |
Function calling is planned to be part of the features I'm working on in the If you're interested, you could switch to the |
I've now implemented function calling - details in #514. Looking for testers! |
Demo
I made a quick screen recording showing the proof of concept:
https://screenapp.io/app/#/shared/6d146099-4bc3-4b34-afd7-bff3d16ee0ed
Overview
I've added support for Function Calling to gptel. This is just a proof of concept that I wanted to share before going any further.
First I want to thank you for making this package! It's become a seamless part of my emacs workflow because of how well it blends in, in particular using
gptel-send
for some of my own scripts. I think that supporting function calls would fit in well with the design philosophy of this package, allowing users to define their own callable functions for their own use cases. Some use cases I can think of off hand:I think that because of how flexible emacs is, this could be the basis of a copilot that's more capable than any other editor.
I should note that function calls are not specific to OpenAI, they're available many if not all of the other models you support.
Before moving on, I'd like to know if this is even something you'd be interested in including in gptel. I'd also like to know your thoughts on how the user would use this feature. So far this is what I'm thinking:
How to test this PR
test-user-config.el
Let me know what you think! Feel free to open a discussion if you think that's a more appropriate venue for this.
Related issue: #76