Skip to main content

Code Interpreter

There is a builtin code interpreter that lets the assistant run code. It's not enabled by default.

[1]:
from chatlab import Chat
from chatlab.tools import run_python

chat = Chat()
chat.register(run_python)
await chat("Please calculate sin(793.1)")
[1]:
𝑓Ranrun_python
Input:
{ "code": "import math\nmath.sin(793.1)" }
Output:
0.9884482539459452

The value of sin(793.1) is approximately 0.9884482539459452.

Data Personas

You can let ChatGPT take on a data persona with a system message and then have it work with DataFrames you have in memory.

from chatlab import Chat, system
from chatlab.tools import run_python
import pandas as pd

# The Chicago Public Library location dataset
df = pd.read_json("https://data.cityofchicago.org/resource/x8fc-8rcq.json")

chat = Chat(
system(
"You are a data scientist running inside a Jupyter Notebook, "
"collaborating with a data engineer."
),
)
chat.register(run_python)
await chat("Please tell me about what's in `df`")

The hidden python hallucination

Sometimes GPT models will hallucinate a python tool that accepts a single string rather than the proper JSON object that run_python requests. You can include it in your Chat to handle these cases.

[1]:
from chatlab import Chat
from chatlab.tools import run_python

chat = Chat(
allow_hallucinated_python=True
)
chat.register(run_python)
await chat("Please calculate sin(793.1)")
[3]:
𝑓Ranpython
Input:
import math math.sin(793.1)
Output:
0.9884482539459452