助理代码解释器
试用版

Code Interpreter 允许 Assistants 在沙盒执行环境中编写和运行 Python 代码。该工具可以处理具有不同数据和格式的文件,并生成包含图表数据和图像的文件。Code Interpreter 允许您的 Assistant 以迭代方式运行代码,以解决具有挑战性的代码和数学问题。当 Google 助理编写无法运行的代码时,它可以通过尝试运行不同的代码来迭代此代码,直到代码执行成功。source

在此处查看有关如何开始使用 Code Interpreter 快速入门。source

运作方式

Code Interpreter 的收费标准为每次会话 0.03 USD。如果您的 Assistant 在两个不同的线程中同时调用 Code Interpreter(例如,每个最终用户一个线程),则会创建两个 Code Interpreter 会话。默认情况下,每个会话的激活时间为 1 小时,这意味着如果用户在同一线程中与 Code Interpreter 交互长达 1 小时,则您只需为每个会话支付 1 个会话的费用。source

启用 Code Interpreter

通过code_interpretertools参数来启用 Code Interpreter:source

1
2
3
4
5
assistant = client.beta.assistants.create(
  instructions="You are a personal math tutor. When asked a math question, write and run code to answer the question.",
  model="gpt-4o",
  tools=[{"type": "code_interpreter"}]
)

然后,模型根据用户请求的性质决定何时在 Run 中调用 Code Interpreter。可以通过在 Assistant 的instructions(例如,“编写代码来解决此问题”)。source

将文件传递给 Code Interpreter

在 Assistant 级别传递的文件可由具有此 Assistant 的所有运行访问:source

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
# Upload a file with an "assistants" purpose
file = client.files.create(
  file=open("mydata.csv", "rb"),
  purpose='assistants'
)

# Create an assistant using the file ID
assistant = client.beta.assistants.create(
  instructions="You are a personal math tutor. When asked a math question, write and run code to answer the question.",
  model="gpt-4o",
  tools=[{"type": "code_interpreter"}],
  tool_resources={
    "code_interpreter": {
      "file_ids": [file.id]
    }
  }
)

文件也可以在 Thread 级别传递。这些文件只能在特定线程中访问。使用 File upload 端点上传文件,然后将 File ID 作为 Message creation 请求的一部分传递:source

1
2
3
4
5
6
7
8
9
10
11
12
13
14
thread = client.beta.threads.create(
  messages=[
    {
      "role": "user",
      "content": "I need to solve the equation `3x + 11 = 14`. Can you help me?",
      "attachments": [
        {
          "file_id": file.id,
          "tools": [{"type": "code_interpreter"}]
        }
      ]
    }
  ]
)

文件的最大大小为 512 MB。Code Interpreter 支持多种文件格式,包括.csv,.pdf,.json以及更多。有关支持的文件扩展名(及其相应的 MIME 类型)的更多详细信息,请参阅下面的 Supported files 部分。source

读取 Code Interpreter 生成的图像和文件

API 中的 Code Interpreter 还会输出文件,例如生成图像图、CSV 和 PDF。生成两种类型的文件:source

  1. 图像
  2. 数据文件(例如csv文件,其中包含 Assistant 生成的数据)

当 Code Interpreter 生成图像时,您可以在file_id字段中:source

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
{
	"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"
      }
    }
  ]
  # ...
}

然后,可以通过将文件 ID 传递给文件 API 来下载文件内容:source

1
2
3
4
5
6
7
8
9
from openai import OpenAI

client = OpenAI()

image_data = client.files.content("file-abc123")
image_data_bytes = image_data.read()

with open("./my-image.png", "wb") as file:
    file.write(image_data_bytes)

当 Code Interpreter 引用文件路径(例如,“Download this csv file”)时,文件路径将作为注释列出。您可以将这些注释转换为链接以下载文件:source

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
{
  "id": "msg_abc123",
  "object": "thread.message",
  "created_at": 1699073585,
  "thread_id": "thread_abc123",
  "role": "assistant",
  "content": [
    {
      "type": "text",
      "text": {
        "value": "The rows of the CSV file have been shuffled and saved to a new CSV file. You can download the shuffled CSV file from the following link:\n\n[Download Shuffled CSV File](sandbox:/mnt/data/shuffled_file.csv)",
        "annotations": [
          {
            "type": "file_path",
            "text": "sandbox:/mnt/data/shuffled_file.csv",
            "start_index": 167,
            "end_index": 202,
            "file_path": {
              "file_id": "file-abc123"
            }
          }
          ...

Code Interpreter 的输入和输出日志

通过列出名为 Code Interpreter 的 Run 的步骤,您可以检查代码inputoutputsCode Interpreter 的日志:source

1
2
3
4
run_steps = client.beta.threads.runs.steps.list(
  thread_id=thread.id,
  run_id=run.id
)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
{
  "object": "list",
  "data": [
    {
      "id": "step_abc123",
      "object": "thread.run.step",
      "type": "tool_calls",
      "run_id": "run_abc123",
      "thread_id": "thread_abc123",
      "status": "completed",
      "step_details": {
        "type": "tool_calls",
        "tool_calls": [
          {
            "type": "code",
            "code": {
              "input": "# Calculating 2 + 2\nresult = 2 + 2\nresult",
              "outputs": [
                {
                  "type": "logs",
                  "logs": "4"
                }
						...
 }

支持的文件

文件格式MIME 类型
.ctext/x-c
.cstext/x-csharp
.cpptext/x-c++
.csvtext/csv
.docapplication/msword
.docxapplication/vnd.openxmlformats-officedocument.wordprocessingml.document
.htmltext/html
.javatext/x-java
.jsonapplication/json
.mdtext/markdown
.pdfapplication/pdf
.phptext/x-php
.pptxapplication/vnd.openxmlformats-officedocument.presentationml.presentation
.pytext/x-python
.pytext/x-script.python
.rbtext/x-ruby
.textext/x-tex
.txttext/plain
.csstext/css
.jstext/javascript
.shapplication/x-sh
.tsapplication/typescript
.csvapplication/csv
.jpegimage/jpeg
.jpgimage/jpeg
.gifimage/gif
.pklapplication/octet-stream
.pngimage/png
.tarapplication/x-tar
.xlsxapplication/vnd.openxmlformats-officedocument.spreadsheetml.sheet
.xmlapplication/xml or "text/xml"
.zipapplication/zip