Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Checklist:
I've been working on implementing tool/function calling for models that support it. This is a major feature that enables LLMs to use tools to do a lot of things they normally aren't very good at.
(I wanted something like a radio button for the selected tool vs checkboxes for the enabled ones, and this has the problem of requiring you to enable a tool to select it... Which I think I'd rather avoid given that currently it executes the tool code when it's enabled, to add the function to the globals.)and a dropdown menu for the tool filename, which is much saferExamples:

Gradio UI for tools (
roll_dice
example shown)(Model is Qwen 2.5 32B Instruct Q4 in these tests)
Dice example, showing before and after you confirm execution:

Code interpreter example, showing the displayed code:

Tutorial on how to test tool use:
roll_dice
.Models tested: Llama 3.2 Instruct (1B and 7B), Qwen 2.5 Instruct (7B and 32B). The 32B model obviously worked best for this, the small models tend to have issues with knowing when to use the tools and will often call them repeatedly or make up nonexistent ones.
The code interpreter tool looks at the output which was printed during execution, so if the code ends with just a variable without printing it (as if in a notebook), it won't work. This is something that needs to be fixed. Things like matplotlib also won't work, though if you save a plot to a file it will work (just make sure not to overwrite existing files...) The weather tool is just a placeholder, and the random number generation tools are also just examples to show how it works, but one could make useful tools for lots of different things.
This is a work in progress, but the basic functionality is in place to be able to create and run tools in the UI. I could reorganize it better and put some stuff in html_generator.py rather than directly in chat.py, so that it displays in a special way in the UI rather than just appearing as text with some markdown blocks. It also only works in Instruct mode right now. Token bias after detecting the start of a tool call would be nice to ensure it formats the tool call properly, though from what I've seen with these modern models, that hasn't really been an issue, surprisingly.
I still need to add the OpenAI API extension support for this. I've been focusing on getting it working in the UI, but I understand a lot of people want the API support. I haven't really looked at the extension though, so if anyone has any ideas on how to add it now that it's working in the UI, please leave a comment, and let's collaborate.
There are lots of things to add to improve this feature. But it is functional, at least, aside from the API, and people might like to test it and suggest improvements.
I don't have the option to merge to a new branch, so I set this PR to dev, but I suggest making a separate branch for this, as it changes quite a lot about the main generation code and should be tested more.
Relevant issues this (partially) addresses:
#4286
#4455
#6539
Please let me know what you think! :)