Python

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.

Instructions

Use this tool to execute Python code in an interactive IPython session.
It responds with the execution output and final result.

### When to use the python tool

Use `python` for computation, structured data, and file-processing automation.
Prefer it over the shell for pure computation or when you need persistent
state or Python libraries.

Examples

Result of the last expression will be returned

User
What is 2 + 2?
Assistant
ipython
2 + 2
System
Executed code block.
`result
4
`

Write a function and call it

User
compute fib 10
Assistant
To compute the 10th Fibonacci number, we can run the following code:
ipython
def fib(n):
    if n <= 1:
        return n
    return fib(n - 1) + fib(n - 2)
fib(10)
System
Executed code block.
`result
55
`
class gptme.tools.python.TeeIO
__init__(original_stream)
write(s)

Write string to file.

Returns the number of characters written, which is always equal to the length of the string.

gptme.tools.python.execute_python(code: str | None, args: list[str] | None, kwargs: dict[str, str] | None) Generator[Message, None, None]

Executes a python codeblock and returns the output.

gptme.tools.python.get_installed_python_libraries() list[str]

Check if a select list of Python libraries are installed.

gptme.tools.python.register_function(func: T, *, tool_name: str | None = None) T

Register a function for IPython and optionally record its owning tool.