-
-
Notifications
You must be signed in to change notification settings - Fork 212
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
feat: allow to load tools from external modules #336
Merged
Merged
Changes from all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
e3a956d
feat: allow to load tools from external modules
jrmi 79ff5c2
fix: s/TOOL_ALLOW_LIST/TOOL_ALLOWLIST
ErikBjare 15cbf2d
docs: fixed formatting
ErikBjare b786a51
fix: remove ancient workaround
jrmi 9e2af72
fix: refactor _discover_tools to avoid code duplication
jrmi 05e10bb
docs: fix TOOL_ALLOWLIST suggested config
ErikBjare File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,84 @@ | ||
Creating a Custom Tool for gptme | ||
================================= | ||
|
||
Introduction | ||
------------ | ||
In gptme, a custom tool allows you to extend the functionality of the assistant by | ||
defining new tools that can be executed. | ||
This guide will walk you through the process of creating and registering a custom tool. | ||
|
||
Creating a Custom Tool | ||
----------------------- | ||
To create a custom tool, you need to define a new instance of the ``ToolSpec`` class. | ||
This class requires several parameters: | ||
|
||
- **name**: The name of the tool. | ||
- **desc**: A description of what the tool does. | ||
- **instructions**: Instructions on how to use the tool. | ||
- **examples**: Example usage of the tool. | ||
- **execute**: A function that defines the tool's behavior when executed. | ||
- **block_types**: The block types to detects. | ||
- **parameters**: A list of parameters that the tool accepts. | ||
|
||
Here is a basic example of defining a custom tool: | ||
|
||
.. code-block:: python | ||
|
||
import random | ||
from gptme.tools import ToolSpec, Parameter, ToolUse | ||
from gptme.message import Message | ||
|
||
def execute(code, args, kwargs, confirm): | ||
|
||
if code is None and kwargs is not None: | ||
code = kwargs.get('side_count') | ||
|
||
yield Message('system', f"Result: {random.randint(1,code)}") | ||
|
||
def examples(tool_format): | ||
return f""" | ||
> User: Throw a dice and give me the result. | ||
> Assistant: | ||
{ToolUse("dice", [], "6").to_output(tool_format)} | ||
> System: 3 | ||
> assistant: The result is 3 | ||
""".strip() | ||
|
||
tool = ToolSpec( | ||
name="dice", | ||
desc="A dice simulator.", | ||
instructions="This tool generate a random integer value like a dice.", | ||
examples=examples, | ||
execute=execute, | ||
block_types=["dice"], | ||
parameters=[ | ||
Parameter( | ||
name="side_count", | ||
type="integer", | ||
description="The number of faces of the dice to throw.", | ||
required=True, | ||
), | ||
], | ||
) | ||
|
||
Registering the Tool | ||
--------------------- | ||
To ensure your tool is available for use, you can specify the module in the ``TOOL_MODULES`` env variable or | ||
setting in your :doc:`project configuration file <config>`, which will automatically load your custom tools. | ||
|
||
.. code-block:: toml | ||
|
||
TOOL_MODULES = "gptme.tools,path.to.your.custom_tool_module" | ||
|
||
Don't remove the ``gptme.tools`` package unless you know exactly what you are doing. | ||
|
||
Ensure your module is in the Python path by either installing it | ||
(e.g. with ``pip install .`` or ``pipx runpip gptme install .``, depending on installation method) | ||
or by temporarily modifying the `PYTHONPATH` environment variable. For example: | ||
|
||
.. code-block:: bash | ||
|
||
export PYTHONPATH=$PYTHONPATH:/path/to/your/module | ||
|
||
|
||
This lets Python locate your module during development and testing without requiring installation. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
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.
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 think we should go into a bit more detail about the options people have when wanting to make custom tools, and the differences.
While we didn't have custom tools before, it was easy to write scripts which effectively served as tools when included in the context. I've tended to build tools like that for gptme agents like @TimeToBuildBob, and it works really well.
You can for example write Python scripts with uv script dependencies and use a
#!/usr/bin/env -S uv run
shebang line to make the scripts self-contained with isolated dependencies. Or use any other programming language (doesn't have to be Python).Not quite sure how to write it out. I think scripts are preferable in many situations since they can be run and tested independent of gptme.
But you need to write a custom tool for:
tools
section of the system promptshell
tool (althoughfunctions
still depend onipython
...)I could do this in a separate PR though.
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.
Yeah that's true, I haven't considered that option and we should add this to the documentation as it's simpler to implement a new tool that way.
With custom tool you can also replace an existing tool with a slightly (or heavily) modified one if you want.
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.
Doing so in #391