代码编辑器 (Beta)
代码编辑器 是一个工具,允许助手编写和运行 Python 代码在沙盒执行环境中。该工具可以处理各种格式和数据的文件,并生成包含数据和图像的文件。Code Interpreter 允许您的助手以迭代方式运行代码来解决复杂的代码和数学问题。当您的助手编写的代码无法运行时,它可以尝试运行不同的代码,直到代码执行成功。
有关如何开始使用 代码编辑器 的快速入门,请参阅创建助手。
工作原理
代码编辑器 的使用费用为每个会话 0.03 美元。如果您的助手在两个不同的线程中(例如,每个端用户一个线程)同时调用 Code Interpreter,则会创建两个 Code Interpreter 会话。每个会话默认最长持续一小时,这意味着您仅需为每个用户最多一小时的交互支付一次会话费用。
启用 代码编辑器
要启用 代码编辑器,请在助手对象的 tools 参数中传递 code_interpreter:
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"}]
)
然后,助手会在运行中根据用户请求的性质决定是否调用 Code Interpreter。这种行为可以通过在助手的 instructions 中进行提示来促进(例如,“编写代码来解决这个问题”)。
向 代码编辑器 传递文件
在助手级别传递的文件可以由所有运行访问。要将文件传递给 Code Interpreter,请首先使用文件上传端点上传文件,然后将文件 ID 作为消息创建请求的一部分进行传递:
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 类型的详细信息,请参阅支持的文件部分。
读取由 Code Interpreter 生成的图像和文件
Code Interpreter 在 API 中还会输出文件,例如生成图像图表、CSV 文件和 PDF 文件。有两种类型的输出文件:
- 图像
- 数据文件(例如,由助手生成的 CSV 文件)
当 Code Interpreter 生成图像时,您可以在助手消息响应的 file_id 字段中查找并下载该文件:
{
  "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"
      }
    }
  ]
  // ...
}
然后,可以使用 Files API 下载文件内容:
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)
当 代码编辑器 引用文件路径(例如,“下载此 CSV 文件”)时,文件路径将列为注释。您可以将这些注释转换为可以下载文件的链接:
{
  "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 的 input 和 outputs 日志:
run_steps = client.beta.threads.runs.steps.list(
  thread_id=thread.id,
  run_id=run.id
)
这将返回一个包含有关每个步骤的信息的对象,包括 input 和 outputs 日志:
{
  "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"
                }
                // ...
              ]
            }
          }
        ]
      }
    }
  ]
}
支持的文件
以下是 Code Interpreter 支持的文件格式和 MIME 类型的列表。对于 text/ MIME 类型,编码必须是 utf-8,utf-16 或 ascii。
| 文件格式 | MIME 类型 | 
|---|---|
| .c | text/x-c | 
| .cs | text/x-csharp | 
| .cpp | text/x-c++ | 
| .doc | application/msword | 
| .docx | application/vnd.openxmlformats-officedocument.wordprocessingml.document | 
| .html | text/html | 
| .java | text/x-java | 
| .json | application/json | 
| .md | text/markdown | 
| .pdf | application/pdf | 
| .php | text/x-php | 
| .pptx | application/vnd.openxmlformats-officedocument.presentationml.presentation | 
| .py | text/x-python | 
| .py | text/x-script.python | 
| .rb | text/x-ruby | 
| .tex | text/x-tex | 
| .txt | text/plain | 
| .css | text/css | 
| .js | text/javascript | 
| .sh | application/x-sh | 
| .ts | application/typescript | 
| .csv | application/csv | 
| .jpeg | image/jpeg | 
| .jpg | image/jpeg | 
| .gif | image/gif | 
| .png | image/png | 
| .tar | application/x-tar | 
| .xlsx | application/vnd.openxmlformats-officedocument.spreadsheetml.sheet | 
| .xml | application/xml or "text/xml" | 
| .zip | application/zip |