程式碼解譯器可讓代理程式在沙箱化執行環境中撰寫和執行 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 |