Configuring local tool execution

Often times, tool definitions will rely on importing code from other files or packages:

1def my_tool():
2 # import code from other files
3 from my_repo.subfolder1.module import my_function
4
5 # import packages
6 import cowsay
7
8 # custom code

To ensure that your tools are able to run, you need to make sure that the files and packages they rely on are accessible from the Letta server. When running Letta locally, the tools are executed inside of the Docker container running the Letta service, and the files and packages they rely on must be accessible from the Docker container.

Importing modules from external files

Tool definitions will often rely on importing code from other files. For example, say you have a repo with the following structure:

my_repo/
├── requirements.txt
├── subfolder1/
└── module.py

We want to import code from module.py in a custom tool as follows:

1def my_tool():
2 from my_repo.subfolder1.module import my_function # MUST be inside the function scope
3 return my_function()

Any imports MUST be inside the function scope, since only the code inside the function scope is executed.
To ensure you can properly import my_function, you need to mount your repository in the Docker container and also explicitly set the location of tool execution by setting the TOOL_EXEC_DIR environment variable.

1docker run \
2 -v /path/to/my_repo:/app/my_repo \ # mount the volume
3 -e TOOL_EXEC_DIR="/app/my_repo" \ # specify the directory
4 -v ~/.letta/.persist/pgdata:/var/lib/postgresql/data \
5 -p 8283:8283 \
6 letta/letta:latest

This will ensure that tools are executed inside of /app/my_repo and the files inside of my_repo are accessible via the volume.

Specifying pip packages

You can specify packages to be installed in the tool execution environment by setting the TOOL_EXEC_VENV_NAME environment variable. This will enable Letta to explicitly create a virtual environment and and install packages specified by requirements.txt at the server start time.

1docker run \
2 -v /path/to/my_repo:/app/my_repo \ # mount the volume
3 -e TOOL_EXEC_DIR="/app/my_repo" \ # specify the directory
4 -e TOOL_EXEC_VENV_NAME="env" \ # specify the virtual environment name
5 -v ~/.letta/.persist/pgdata:/var/lib/postgresql/data \
6 -p 8283:8283 \
7 letta/letta:latest

This will ensure that the packages specified in /app/my_repo/requirements.txt are installed in the virtual environment where the tools are executed.

Letta needs to create and link the virtual environment, so do not create a virtual environment manually with the same name as TOOL_EXEC_VENV_NAME.