Skip to content

Python Interpreter Tool

David-Andrew Samson edited this page Jun 22, 2023 · 9 revisions

Archytas provides a (currently a work in progress) python tool that allows the LLM to write/run scripts in a persistent environment.

Usage

bare-bones python tool with nothing extra added

from archytas.tools import PythonTool

agent = ReActAgent(tools=[PythonTool], verbose=True)

# query agent
response = agent.query('write me a script that prints out the first 100 prime numbers')
print(response)
#etc...

Pre-initializing the python environment

In the future, you'll be able to include pre-initialized custom local variables (including @tools and @toolsets) in the python environment, as well as provide prelude code to run on initialization

from archytas.demo_tools import fib_n, jackpot, ModelSimulation
python = PythonTool(
    prelude='import numpy as np\nfrom matplotlib import pyplot as plt',
    locals={'fib_n': fib_n, 'jackpot': jackpot, 'ModelSimulation': ModelSimulation},
)
tools = [python]
agent = ReActAgent(tools=tools, verbose=True)

# do your queries ...

Demo

Watch the video
click to watch original video on youtube

The user input in the demo follows the following script:

  1. write a python script to download the csv from this link: https://covid.ourworldindata.org/data/owid-covid-data.csv
  2. now load the csv into a dataframe and print out the columns and head
  3. that's a lot of data, can you filter for just new US cases?
  4. can you plot this
  5. in the plot, there are too many dates on the x-axis, so they all just overlap into one big black blob. Can you fix that?
  6. there seem to be some cyclicalities and artifacts in the raw data. Can you plot a 7-day moving average?
  7. looks good. Can you tell me what day had the most new cases?
  8. What was the mean and median number of new cases? use the 7-day moving average
  9. Can you plot a histogram of the new daily cases (from the 7-day average) and mark the mean and median

The only special libraries that are in the environment are pandas, and matplotlib. At each step the agent is able to write a script to run in the python tool that solves the current prompt, making use of persistent variables in the tool during successive calls.

TODO

  • passing in python modules, classes, functions, variables, etc.
  • having the python tool run a default prelude script (e.g. for imports, etc)
  • having a way for the LLM to lookup a provided local variable's usage docs (esp. if it's an @tool that the LLM wouldn't be familiar with)
  • oneshot version of the tool with no persistence