Freigeben über


Foundry Agent Service Code Interpreter (klassisch)

Hinweis

Dieses Dokument bezieht sich auf die Microsoft Foundry (klassische) Agenten.

🔍 Sehen Sie sich die neue Dokumentation zum Code-Interpreter an. Agents (klassisch) sind jetzt veraltet und werden am 31. März 2027 eingestellt. Verwenden Sie die neuen Agents im allgemein verfügbaren Microsoft Foundry Agents Service. Folgen Sie dem Migrationshandbuch , um Ihre Workloads zu aktualisieren.

Code-Interpreter ermöglicht es den Agents, Python Code in einer Sandkastenausführungsumgebung zu schreiben und auszuführen. Mit aktiviertem Code-Interpreter kann Ihr Agent Code iterativ ausführen, um anspruchsvollere Code-, mathematische und Datenanalyseprobleme zu lösen oder Diagramme und Grafiken zu erstellen. Wenn Ihr Agent Code schreibt, der nicht ausgeführt werden kann, kann er diesen Code durchlaufen, indem er unterschiedlichen Code ändert und ausführt, bis die Codeausführung erfolgreich ist.

Von Bedeutung

Code Interpreter verfügt über zusätzliche Gebühren über die tokenbasierten Gebühren für die Nutzung von Azure OpenAI hinaus. Wenn Ihr Agent Code Interpreter gleichzeitig in zwei verschiedenen Threads aufruft, werden zwei Code Interpreter-Sitzungen erstellt. Jede Sitzung ist standardmäßig für 1 Stunde mit einem Leerlauftimeout von 30 Minuten aktiv.

Voraussetzungen

Codebeispiele

Erstellen eines Agents mit Codedolmetscher

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,
)

Fügen Sie eine Datei für den Code-Interpreter hinzu

Wenn eine Datei mit Codedolmetscher verwendet werden soll, können Sie die upload_and_poll Funktion verwenden.

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])

Erstellen eines Agents mit Codedolmetscher

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()]
);

Fügen Sie eine Datei für den Code-Interpreter hinzu

Wenn Sie möchten, dass eine Datei mit Codedolmetscher verwendet wird, können Sie sie an Ihre Nachricht anfügen.

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]
);

Erstellen eines Agents mit Codedolmetscher

// 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}`);

Fügen Sie eine Datei für den Code-Interpreter hinzu

Wenn eine Datei mit Codedolmetscher verwendet werden soll, können Sie sie an das Tool anfügen.

// 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]);

Erstellen eines Agents mit dem Code-Interpreter-Tool

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);

Fügen Sie eine Datei für den Code-Interpreter hinzu

Wenn eine Datei mit Codedolmetscher verwendet werden soll, können Sie sie an das Tool anfügen.

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);

Unterstützte Modelle

Die Modellseite enthält die aktuellsten Informationen zu Regionen/Modellen, in denen Agents und Code Interpreter unterstützt werden.

Es wird empfohlen, Agents mit den neuesten Modellen zu verwenden, um die neuen Features sowie die größeren Kontextfenster und aktuellere Trainingsdaten zu nutzen.

Unterstützte Dateitypen

Dateiformat MIME-Typ
.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 oder text/xml
.zip application/zip