你当前正在访问 Microsoft Azure Global Edition 技术文档网站。 如果需要访问由世纪互联运营的 Microsoft Azure 中国技术文档网站,请访问 https://docs.azure.cn。
借助代码解释器,代理可以在沙盒化执行环境中编写和运行 Python 代码。 启用代码解释器后,代理可以迭代运行代码,以解决更具挑战性的代码、数学和数据分析问题,或者创建图形和图表。 如果代理编写了无法运行的代码,它可以通过修改并运行不同的代码来循环访问此代码,直到代码执行成功。
重要
除了与使用 Azure OpenAI 相关的基于令牌的费用之外,代码解释器还有额外的费用。 如果代理在两个不同的线程中同时调用代码解释器,则会创建两个代码解释器会话。 默认情况下,每个会话处于活动状态 1 小时,空闲超时为 30 分钟。
先决条件
代码示例
使用代码解释器创建代理
code_interpreter = CodeInterpreterTool()
# An agent is created with the Code Interpreter capabilities:
agent = project_client.agents.create_agent(
model=os.environ["MODEL_DEPLOYMENT_NAME"],
name="my-agent",
instructions="You are helpful agent",
tools=code_interpreter.definitions,
tool_resources=code_interpreter.resources,
)
附加文件供代码解释器使用
如果希望文件与代码解释器一起使用,可以使用该 upload_and_poll 函数。
file = agents_client.files.upload_and_poll(file_path=asset_file_path, purpose=FilePurpose.AGENTS)
print(f"Uploaded file, file ID: {file.id}")
code_interpreter = CodeInterpreterTool(file_ids=[file.id])
使用代码解释器创建代理
var projectEndpoint = System.Environment.GetEnvironmentVariable("ProjectEndpoint");
var modelDeploymentName = System.Environment.GetEnvironmentVariable("ModelDeploymentName");
PersistentAgentsClient client = new(projectEndpoint, new DefaultAzureCredential());
PersistentAgent agent = client.Administration.CreateAgent(
model: modelDeploymentName,
name: "My Friendly Test Agent",
instructions: "You politely help with math questions. Use the code interpreter tool when asked to visualize numbers.",
tools: [new CodeInterpreterToolDefinition()]
);
附加文件供代码解释器使用
如果希望文件与代码解释器一起使用,可以将其附加到邮件中。
PersistentAgentFileInfo uploadedAgentFile = client.Files.UploadFile(
filePath: "sample_file_for_upload.txt",
purpose: PersistentAgentFilePurpose.Agents);
var fileId = uploadedAgentFile.Id;
var attachment = new MessageAttachment(
fileId: fileId,
tools: tools
);
// attach the file to the message
PersistentThreadMessage message = client.Messages.CreateMessage(
threadId: thread.Id,
role: MessageRole.User,
content: "Can you give me the documented information in this file?",
attachments: [attachment]
);
使用代码解释器创建代理
// Create the code interpreter tool
const codeInterpreterTool = ToolUtility.createCodeInterpreterTool();
// Enable the code interpreter tool during agent creation
const agent = await client.createAgent("gpt-4o", {
name: "my-agent",
instructions: "You are a helpful agent",
tools: [codeInterpreterTool.definition],
toolResources: codeInterpreterTool.resources,
});
console.log(`Created agent, agent ID: ${agent.id}`);
附加文件供代码解释器使用
如果希望文件与代码解释器一起使用,可以将其附加到该工具。
// Upload file and wait for it to be processed
const filePath = "./examplefile.csv";
const localFileStream = fs.createReadStream(filePath);
const localFile = await client.files.upload(localFileStream, "assistants", {
fileName: "localFile",
});
// Create code interpreter tool
const codeInterpreterTool = ToolUtility.createCodeInterpreterTool([localFile.id]);
使用代码解释器工具创建代理
curl --request POST \
--url $AZURE_AI_FOUNDRY_PROJECT_ENDPOINT/assistants?api-version=$API_VERSION \
-H "Authorization: Bearer $AGENT_TOKEN" \
-H "Content-Type: application/json" \
-d '{
"instructions": "You are an AI assistant that can write code to help answer math questions.",
"tools": [
{ "type": "code_interpreter" }
],
"model": "gpt-4o-mini",
"tool_resources"{
"code interpreter": {
}
}
}'
String agentName = "code_interpreter_agent";
CodeInterpreterToolDefinition ciTool = new CodeInterpreterToolDefinition();
CreateAgentOptions createAgentOptions = new CreateAgentOptions(modelName).setName(agentName).setInstructions("You are a helpful agent").setTools(Arrays.asList(ciTool));
PersistentAgent agent = administrationClient.createAgent(createAgentOptions);
附加文件供代码解释器使用
如果希望文件与代码解释器一起使用,可以将其附加到该工具。
FileInfo uploadedFile = filesClient.uploadFile(new UploadFileRequest(
new FileDetails(BinaryData.fromFile(htmlFile))
.setFilename("sample.html"), FilePurpose.AGENTS));
MessageAttachment messageAttachment = new MessageAttachment(Arrays.asList(BinaryData.fromObject(ciTool))).setFileId(uploadedFile.getId());
PersistentAgentThread thread = threadsClient.createThread();
ThreadMessage createdMessage = messagesClient.createMessage(
thread.getId(),
MessageRole.USER,
"What does the attachment say?",
Arrays.asList(messageAttachment),
null);
支持的模型
模型页包含有关支持代理和代码解释器的区域/模型的最新信息。
建议使用具有最新模型的代理来利用新功能、更大的上下文窗口和更新的训练数据。
支持的文件类型
| 文件格式 | MIME 类型 |
|---|---|
.c |
text/x-c |
.cpp |
text/x-c++ |
.csv |
application/csv |
.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 |
.jpeg |
image/jpeg |
.jpg |
image/jpeg |
.js |
text/javascript |
.gif |
image/gif |
.png |
image/png |
.tar |
application/x-tar |
.ts |
application/typescript |
.xlsx |
application/vnd.openxmlformats-officedocument.spreadsheetml.sheet |
.xml |
application/xml 或 text/xml |
.zip |
application/zip |