Tools#

Tools available in gptme.

The main tools can be grouped in the following categories:

Shell#

The assistant can execute shell commands by outputting code blocks with bash or sh as the language.

Example:

User
How can I list the files in the current directory?
Assistant
To list the files in the current directory, use the `ls` command:
```bash
ls
```
System
Ran command: `ls`
stdout:
```
file1.txt
file2.txt
```

The user can also run shell code with the /shell command:

User
/shell ls
System
Ran command: `ls`
stdout:
```
file1.txt
file2.txt
```
gptme.tools.shell.execute_shell(cmd: str, ask=True) Generator[Message, None, None]

Executes a shell command and returns the output.

Python#

The assistant can execute Python code blocks.

It uses IPython to do so, and persists the IPython instance between calls to give a REPL-like experience.

User
What is 2 + 2?
Assistant
```python
2 + 2
```
System
Executed code block.
stdout:
```
4
```

The user can also run Python code with the /python command:

User
/python 2 + 2
System
Executed code block.
stdout:
```
4
```
gptme.tools.python.check_available_packages()

Checks that essentials like numpy, pandas, matplotlib are available.

gptme.tools.python.execute_python(code: str, ask: bool) Generator[Message, None, None]

Executes a python codeblock and returns the output.

gptme.tools.python.register_function(func: T) T

Decorator to register a function to be available in the IPython instance.

Save#

Gives the assistant the ability to save code to a file.

Example:

User
write hello world to hello.py
Assistant
```hello.py
print("hello world")
```
System
Saved to hello.py
gptme.tools.save.execute_save(fn: str, code: str, ask: bool, append: bool = False) Generator[Message, None, None]

Save the code to a file.

Patch#

Gives the LLM agent the ability to patch files, by using a adapted version git conflict markers.

Example:

User
patch the file `hello.py` to ask for the name of the user
Assistant
```patch hello.py
<<<<<<< ORIGINAL
print("Hello world")
=======
name = input("What is your name? ")
print(f"hello {name}")
>>>>>>> UPDATED
```
System
Patch applied

Inspired by aider.

gptme.tools.patch.apply(codeblock: str, content: str) str

Applies the patch in codeblock to content.

gptme.tools.patch.execute_patch(codeblock: str, fn: str, ask: bool) Generator[Message, None, None]

Applies the patch.

Browser#

Tools to let the assistant control a browser, including reading webpages and searching.

Note

This is an experimental feature. It needs some work to be more robust and useful.

gptme.tools.browser.read_url(url: str) str

Read the text of a webpage and return the text in Markdown format.

gptme.tools.browser.search(query: str, engine: Literal['google', 'duckduckgo'] = 'google') str

Search for a query on a search engine.

Edit#

Tool that lets the user edit something in a temporary file using their $EDITOR.

This is typically used to edit a conversation log with /edit.

gptme.tools.useredit.edit_text_with_editor(initial_text: str, ext=None) str

Edit some text in a temporary file using the user’s $EDITOR.

Reduce#

Tools to reduce a log to a smaller size.

Typically used when the log exceeds a token limit and needs to be shortened.

gptme.tools.reduce.limit_log(log: list[gptme.message.Message]) list[gptme.message.Message]

Picks messages until the total number of tokens exceeds limit, then removes the last message to get below the limit. Will always pick the first few system messages.

gptme.tools.reduce.reduce_log(log: list[gptme.message.Message], limit=None, prev_len=None) Generator[Message, None, None]

Reduces log until it is below limit tokens by continually summarizing the longest messages until below the limit.

gptme.tools.reduce.truncate_msg(msg: Message, lines_pre=10, lines_post=10) Message | None

Truncates message codeblocks to the first and last lines_pre and lines_post lines, keeping the rest as […].

Context#

Generate context information for a conversation.

Can include the current working directory, git status, and ctags output.

gptme.tools.context.ctags() str

Generate ctags output for project in working dir.

gptme.tools.context.gen_context_msg() Message

Generate a message with context information from output of pwd and git status.

Summarize#

A tool to summarize long outputs.

gptme.tools.summarize.summarize(msg: Message | list[gptme.message.Message]) Message

Uses a cheap LLM to summarize long outputs.