Published on

How to Use the Code Interpreter tool with the OpenAI Assistants API

Authors

How to Use the Code Interpreter tool with the OpenAI Assistants API

For general information on how to use the Assistants API, check out my guide on getting set up here: https://www.jimmymills.io/blog/openai-assistants-api

In this post, we'll look at the Code Interpreter tool, a feature of the OpenAI Assistants API that allows your assistant to start up a python interpreter to help it answer questions. The code interpreter has a number of libraries included out of the box, which you can find a list of on this excellent post by WFH Brian

The interpreter can be passed data or code files to help guide it to the kind of answers you want it to generate.

Setting Up Code Interpreter

To use the Code Interpreter tool, you need to enable it on your assistant and optionally provide one or more files. These files can be attached to either the assistant itself, allowing them to be used in every run, or to a specific message, making them available only within that thread.

Use Cases

Some good use cases for the Code Interpreter tool would be:

  • Data Analysis
  • Answering questions that require any math to properly answer
  • Answers that could benefit from a spreadsheet of data for context
  • Plotting data

Example: Data Analysis Assistant

Here is an example giving the assistant a code interpreter and a csv data file (be sure to populate data.csv in the current working directory) to plot the data in the file:

from openai import OpenAI

client = OpenAI()

file = client.files.create(file=open("data.csv", "rb"), purpose="assistants")

assistant = client.beta.assistants.create(
  name="Data Analysis Assistant",
  instructions="You are a helpful data analyst. When asked a question, you will parse your CSV file to provide the requested analysis.",
  model="gpt-4-1106-preview",
  tools=[{"type": "code_interpreter"}],
  file_ids=[file.id]
)

# Create a new thread
thread = client.beta.threads.create()

# Place your first message into your thread
client.beta.threads.messages.create(
  thread_id=thread.id,
  role="user",
  # Code Interpeter has access to matplotlib, letting you plot values
  # in addition to answering questions
  content="Plot a histogram of the values of the ratings column",
)

# Create a run
run = client.beta.threads.runs.create(
  thread_id=thread.id,
  assistant_id=assistant.id,
)

# Wait for your run to finish
while run.status != 'completed':
  time.sleep(5) # Sleep for 5 seconds between checks to limit calls
  run = client.beta.threads.retrieve(
    thread_id=thread.id,
    run_id=run.id,
  )

# Print out the response
messages = client.beta.threads.messages.list(thread_id=thread.id)
print(messages[0].content[0].text.value)

Conclusion

The Code Interpeter tool allows you to break your assistant out of the limit of simple text generation and allows it to run calculations to find specific answers to your queries.