# Code Interpreter

代码解释器是一个强大的工具，它允许Afarensis助手API在安全的沙盒执行环境中编写和运行Python代码。此工具不仅可以处理包含多种数据和格式的文件，还能生成包含数据和图表图像的文件，使您的助手能够迭代地运行代码以解决复杂的编程和数学问题。

启用代码解释器 要启用代码解释器，只需在创建助手对象时，在 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-4-turbo-preview", tools=\[{"type": "code\_interpreter"}] ) 这样，模型就会根据用户请求的性质，在运行中决定何时调用代码解释器。您可以通过在助手的指令中加入特定提示（例如，“编写代码来解决这个问题”）来促进此行为。

费用 每个代码解释器会话的费用为 $0.03。如果您的助手在不同的线索中同时调用代码解释器，比如每个终端用户一个线索，则会为每个线索创建一个独立的会话。

向代码解释器传递文件 代码解释器可以从文件中解析数据，这对于向助手提供大量数据或允许用户上传自己的文件进行分析非常有用。文件可以在助手级别传递，使所有该助手的运行都能访问这些文件：

## 上传一个目的为 "assistants" 的文件

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

## 使用文件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-4-turbo-preview", tools=\[{"type": "code\_interpreter"}], file\_ids=\[file.id] ) 文件也可以在线索级别传递，使这些文件仅在特定线索中可访问。

读取代码解释器生成的图像和文件 代码解释器还可以输出文件，比如生成图像图表、CSV和PDF文件。当代码解释器生成图像时，您可以在助手消息响应的 file\_id 字段中查找并下载此文件。此外，当代码解释器引用文件路径时，路径会被列为注释，您可以将这些注释转换为下载文件的链接。

代码解释器的输入和输出日志 通过查看调用代码解释器的运行步骤，您可以检查代码解释器的代码和日志，了解代码执行的具体情况和结果。


---

# Agent Instructions: Querying This Documentation

If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://doc.afarensis.com/gong-ju/code-interpreter.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
