Categories
GPT-40 LLM Code Generation

How to Use GPT Code Interpreter via API

The GPT Code Interpreter is a powerful tool that allows Chat Assistants created using OpenAI APIs to write and run Python code in a sandboxed environment. This tool is highly suited for handling tasks such as processing files with various data formats, solving complex coding problems, generating data files, and creating graph images. The Code Interpreter is particularly valuable for iterating on code, as it can make multiple attempts at running slightly different code until the desired outcome is achieved.

For leveraging this code interpreter, you will require using APIs and creating your own application. If you are looking to use Code Interface via a UI interface, you can use ChatGPT Code Generator by Bind AI

Here’s how you can leverage GPT Code Interpreter in your workflow via Assistant APIs:

Setting Up GPT Code Interpreter

Enabling Code Interpreter

To get started, you need to enable the Code Interpreter for your Assistant. You can do this by specifying the "code_interpreter" tool in the Assistant’s configuration.

Example:
Bash
curl https://api.openai.com/v1/assistants \
-u :$OPENAI_API_KEY \
-H 'Content-Type: application/json' \
-H 'OpenAI-Beta: assistants=v2' \
-d '{
"instructions": "You are a personal math tutor. When asked a math question, write and run code to answer the question.",
"tools": [
{ "type": "code_interpreter" }
],
"model": "gpt-4o"
}'

By including the "code_interpreter" in the tools list, the model can decide when to invoke it based on the user’s requests. This is useful for scenarios requiring mathematical computation or code execution.

Passing Files to GPT Code Interpreter

You can pass files to the Code Interpreter in two ways: at the Assistant level or the Thread level.

  1. At the Assistant Level: Files uploaded with an “assistants” purpose are accessible by all Runs with this Assistant.
Bash
curl https://api.openai.com/v1/files \
-H "Authorization: Bearer $OPENAI_API_KEY" \
-F purpose="assistants" \
-F file="@/path/to/mydata.csv"

curl https://api.openai.com/v1/assistants \
-u :$OPENAI_API_KEY \
-H ‘Content-Type: application/json’ \
-H ‘OpenAI-Beta: assistants=v2’ \
-d ‘{
“instructions”: “You are a personal math tutor. When asked a math question, write and run code to answer the question.”,
“tools”: [{“type”: “code_interpreter”}],
“model”: “gpt-4o”,
“tool_resources”: {
“code_interpreter”: {
“file_ids”: [“file-BK7bzQj3FfZFXr7DbL6xJwfo”]
}
}
}’

  1. At the Thread Level: Files uploaded to a specific Thread are only accessible within that Thread.
Bash
curl https://api.openai.com/v1/threads/thread_abc123/messages \
-u :$OPENAI_API_KEY \
-H 'Content-Type: application/json' \
-H 'OpenAI-Beta: assistants=v2' \
-d '{
"role": "user",
"content": "I need to solve the equation `3x + 11 = 14`. Can you help me?",
"attachments": [
{
"file_id": "file-ACq8OjcLQm2eIG0BvRM4z5qX",
"tools": [{"type": "code_interpreter"}]
}
]
}'

Interpreting Files and Outputs

Reading Files Generated by Code Interpreter

When Code Interpreter runs your code and generates outputs such as images or data files, you can retrieve these files by referencing the file_id in the Assistant’s Message response.

Example:
Json
{
"id": "msg_abc123",
"object": "thread.message",
"created_at": 1698964262,
"thread_id": "thread_abc123",
"role": "assistant",
"content": [
{
"type": "image_file",
"image_file": {
"file_id": "file-abc123"
}
}
]
}

Download the file by passing the file_id to the Files API:

Bash
curl https://api.openai.com/v1/files/file-abc123/content \
-H "Authorization: Bearer $OPENAI_API_KEY" \
--output image.png

Use Cases

Data Analysis and Visualization

You can use the Code Interpreter to perform data analysis tasks. For instance, upload a CSV file containing your dataset and instruct the Assistant to generate summary statistics or visualization:

Json
{
"role": "user",
"content": "Analyze the data in the provided CSV and create a bar chart of the results.",
"attachments": [
{
"file_id": "file-xyz789",
"tools": [{"type": "code_interpreter"}]
}
]
}

Mathematical Problem Solving

If you’re faced with a complex mathematical problem, you can leverage the Code Interpreter to write and run Python code to solve it:

Json
{
"role": "user",
"content": "Solve the quadratic equation `3x^2 + 5x - 8 = 0` using the quadratic formula.",
"tools": [{"type": "code_interpreter"}]
}

Conclusion

The Code Interpreter is a versatile tool that empowers Assistants to handle a wide array of coding tasks efficiently. By enabling it, passing the necessary files, and understanding how to retrieve the outputs, you can greatly enhance the capabilities of your AI Assistant to serve diverse and complex user queries.