In diesem Schnellstart erstellen Sie einen Prompt-Agenten im Foundry-Agentendienst und führen eine Konversation mit ihm. Ein Eingabeaufforderungs-Agent ist ein deklarativ definierter Agent, der ein Modell aus dem Foundry-Modellkatalog, Anweisungen, Tools und Aufforderungen in natürlicher Sprache kombiniert, um das Verhalten zu fördern.
Tipp
Verwenden Sie für ein verwaltetes Echtzeit-Spracherlebnis einen sprachbasierten Prompt-Agent. Sprachbasierte Prompt-Agents verwenden Voice Live für gesprochene Unterhaltungen und erfordern nicht, dass Sie den Sprach-Orchestrierungscode hosten.
Wenn Sie nicht über ein Azure-Abonnement verfügen, erstellen Sie ein free-Konto.
Voraussetzungen
Rufen Sie Ihren Projektendpunkt ab
Kopieren Sie Ihren Projektendpunkt vom Willkommensbildschirm im Foundry-Portal.
Die Codebeispiele in dieser Schnellstartanleitung deklarieren ihre Werte als Konstanten am Anfang jeder Datei. Ersetzen Sie vor dem Ausführen eines Beispiels diese Platzhalter:
-
your_project_endpoint: Ihr Projektendpunkt im Format https://<resource-name>.services.ai.azure.com/api/projects/<project-name>.
-
your_agent_name: Ein Name für Ihren Agenten, z. B. MyAgent.
Installieren von Paketen und Authentifizieren
Stellen Sie sicher, dass Sie die richtige Version der Pakete installieren, wie hier gezeigt.
Installieren Sie die aktuelle Version von azure-ai-projects. Diese Version verwendet die Foundry projects (new) API. Die Beispiele authentifizieren sich mit DefaultAzureCredential, das von azure-identity stammt.
pip install "azure-ai-projects>=2.3.0"
Melden Sie sich mit dem BEFEHL CLI az login an, um sich zu authentifizieren, bevor Sie Ihre Python-Skripts ausführen.
Installieren von Paketen:
Fügen Sie NuGet-Pakete mithilfe der .NET CLI im integrierten Terminal hinzu: Diese Pakete verwenden die API für Foundry-Projekte (neu).
dotnet add package Azure.AI.Projects
dotnet add package Azure.AI.Projects.Agents
dotnet add package Azure.AI.Extensions.OpenAI
dotnet add package Azure.Identity
Melden Sie sich mit dem CLI-Befehl az login an, um sich zu authentifizieren, bevor Sie Ihre C#-Skripts ausführen.
Installieren Sie die aktuelle Version von @azure/ai-projects. Diese Version verwendet die API für Foundry-Projekte (neue Projekte).
npm install @azure/ai-projects @azure/identity
Melden Sie sich mit dem CLI-Befehl az login an, um sich zu authentifizieren, bevor Sie Ihre TypeScript-Skripts ausführen.
<dependency>
<groupId>com.azure</groupId>
<artifactId>azure-ai-agents</artifactId>
<version>2.2.0</version>
</dependency>
<dependency>
<groupId>com.azure</groupId>
<artifactId>azure-core</artifactId>
<version>1.57.0</version>
</dependency>
<dependency>
<groupId>com.azure</groupId>
<artifactId>azure-identity</artifactId>
<version>1.18.1</version>
</dependency>
- Melden Sie sich mit dem BEFEHL CLI
az login an, um sich zu authentifizieren, bevor Sie Ihre Java-Skripts ausführen.
Melden Sie sich mit dem CLI-Befehl az login an, um sich zu authentifizieren, bevor Sie den nächsten Befehl ausführen.
Rufen Sie ein temporäres Zugriffstoken ab. Es läuft in 60-90 Minuten ab, Sie müssen es danach aktualisieren.
az account get-access-token --scope https://ai.azure.com/.default
Speichern Sie die Ergebnisse als Umgebungsvariable AZURE_AI_AUTH_TOKEN.
Es ist keine Installation erforderlich, um das Foundry-Portal zu verwenden.
Erstellen eines Prompt-Agents
Erstellen Sie einen Prompt-Agenten mit Ihrem bereitgestellten Modell. Der Agent verwendet eine PromptAgentDefinition mit Anweisungen, die das Verhalten des Agents definieren. Sie können Agents jederzeit aktualisieren oder löschen.
from azure.identity import DefaultAzureCredential
from azure.ai.projects import AIProjectClient
from azure.ai.projects.models import PromptAgentDefinition
# Format: "https://resource_name.services.ai.azure.com/api/projects/project_name"
FOUNDRY_PROJECT_ENDPOINT = "your_project_endpoint"
FOUNDRY_AGENT_NAME = "your-agent-name"
# Create project client to call Foundry API
project = AIProjectClient(
endpoint=FOUNDRY_PROJECT_ENDPOINT,
credential=DefaultAzureCredential(),
)
# Create an agent with a model and instructions
agent = project.agents.create_version(
agent_name=FOUNDRY_AGENT_NAME,
definition=PromptAgentDefinition(
model="gpt-5-mini", # supports all Foundry direct models
instructions="You are a helpful assistant that answers general questions",
),
)
print(f"Agent created (id: {agent.id}, name: {agent.name}, version: {agent.version})")
using Azure.Identity;
using Azure.AI.Projects;
using Azure.AI.Projects.Agents;
using Azure.AI.Extensions.OpenAI;
// Format: "https://resource_name.services.ai.azure.com/api/projects/project_name"
var foundryProjectEndpoint = "your_project_endpoint";
var foundryAgentName = "your-agent-name";
// Create project client to call Foundry API
AIProjectClient projectClient = new(
endpoint: new Uri(foundryProjectEndpoint),
tokenProvider: new DefaultAzureCredential());
// Create an agent with a model and instructions
ProjectsAgentDefinition agentDefinition = new DeclarativeAgentDefinition("gpt-5-mini") // supports all Foundry direct models
{
Instructions = "You are a helpful assistant that answers general questions",
};
ProjectsAgentVersion agent = projectClient.AgentAdministrationClient.CreateAgentVersion(
foundryAgentName,
options: new(agentDefinition));
Console.WriteLine($"Agent created (id: {agent.Id}, name: {agent.Name}, version: {agent.Version})");
import { DefaultAzureCredential } from "@azure/identity";
import { AIProjectClient } from "@azure/ai-projects";
// Format: "https://resource_name.services.ai.azure.com/api/projects/project_name"
const FOUNDRY_PROJECT_ENDPOINT = "your_project_endpoint";
const FOUNDRY_AGENT_NAME = "your-agent-name";
async function main(): Promise<void> {
const project = new AIProjectClient(FOUNDRY_PROJECT_ENDPOINT, new DefaultAzureCredential());
const agent = await project.agents.createVersion(FOUNDRY_AGENT_NAME, {
kind: "prompt",
model: "gpt-5-mini", // supports all Foundry direct models
instructions: "You are a helpful assistant that answers general questions",
});
console.log(`Agent created: ${agent.name}, version: ${agent.version}`);
}
main().catch((error) => {
console.error(error);
process.exitCode = 1;
});
package com.azure.ai.foundry.samples;
import com.azure.ai.agents.AgentsClient;
import com.azure.ai.agents.AgentsClientBuilder;
import com.azure.ai.agents.models.AgentVersionDetails;
import com.azure.ai.agents.models.PromptAgentDefinition;
import com.azure.identity.DefaultAzureCredentialBuilder;
public class CreateAgent {
public static void main(String[] args) {
// Format: "https://resource_name.services.ai.azure.com/api/projects/project_name"
String foundryProjectEndpoint = "your_project_endpoint";
String foundryAgentName = "your-agent-name";
// Create agents client to call Foundry API
AgentsClient agentsClient = new AgentsClientBuilder()
.credential(new DefaultAzureCredentialBuilder().build())
.endpoint(foundryProjectEndpoint)
.buildAgentsClient();
// Create an agent with a model and instructions
PromptAgentDefinition request = new PromptAgentDefinition("gpt-5-mini") // supports all Foundry direct models
.setInstructions("You are a helpful assistant that answers general questions");
AgentVersionDetails agent = agentsClient.createAgentVersion(foundryAgentName, request);
System.out.println("Agent ID: " + agent.getId());
System.out.println("Agent Name: " + agent.getName());
System.out.println("Agent Version: " + agent.getVersion());
}
}
Ersetzen Sie YOUR-FOUNDRY-RESOURCE-NAME mit Ihren Werten.
curl -X POST https://YOUR-FOUNDRY-RESOURCE-NAME.services.ai.azure.com/api/projects/YOUR-PROJECT-NAME/agents?api-version=v1 \
-H "Content-Type: application/json" \
-H "Authorization: Bearer $AZURE_AI_AUTH_TOKEN" \
-d '{
"name": "MyAgent",
"definition": {
"kind": "prompt",
"model": "gpt-5-mini",
"instructions": "You are a helpful assistant that answers general questions"
}
}'
Die Ausgabe bestätigt, dass ein Agent erstellt wurde. Der Name und ID des Agents werden zur Konsole ausgegeben.
Chatten mit dem Agent
Verwenden Sie den von Ihnen erstellten Agent, um zu interagieren, indem Sie eine Frage und eine zugehörige Nachverfolgung stellen. Die Konversation behält den Verlauf dieser Interaktionen bei.
from azure.identity import DefaultAzureCredential
from azure.ai.projects import AIProjectClient
from azure.ai.projects.models import PromptAgentDefinition
# Format: "https://resource_name.services.ai.azure.com/api/projects/project_name"
FOUNDRY_PROJECT_ENDPOINT = "your_project_endpoint"
FOUNDRY_AGENT_NAME = "your-agent-name"
# Create project and openai clients to call Foundry API
project = AIProjectClient(
endpoint=FOUNDRY_PROJECT_ENDPOINT,
credential=DefaultAzureCredential(),
)
# Create the agent (or a new version, if it already exists)
project.agents.create_version(
agent_name=FOUNDRY_AGENT_NAME,
definition=PromptAgentDefinition(
model="gpt-5-mini",
instructions="You are a helpful assistant that answers general questions",
),
)
# Get an OpenAI client pre-bound to the specified agent
openai = project.get_openai_client(agent_name=FOUNDRY_AGENT_NAME)
# Create a conversation for multi-turn chat
conversation = openai.conversations.create()
# Chat with the agent to answer questions
response = openai.responses.create(
conversation=conversation.id,
input="What is the size of France in square miles?",
)
print(response.output_text)
# Ask a follow-up question in the same conversation
response = openai.responses.create(
conversation=conversation.id,
input="And what is the capital city?",
)
print(response.output_text)
using Azure.Identity;
using Azure.AI.Projects;
using Azure.AI.Projects.Agents;
using Azure.AI.Extensions.OpenAI;
using OpenAI.Responses;
#pragma warning disable OPENAI001
// Format: "https://resource_name.services.ai.azure.com/api/projects/project_name"
var foundryProjectEndpoint = "your_project_endpoint";
var foundryAgentName = "your-agent-name";
// Create project client to call Foundry API
AIProjectClient projectClient = new(
endpoint: new Uri(foundryProjectEndpoint),
tokenProvider: new DefaultAzureCredential());
// Create the agent (or a new version, if it already exists)
ProjectsAgentDefinition agentDefinition = new DeclarativeAgentDefinition("gpt-5-mini") // supports all Foundry direct models
{
Instructions = "You are a helpful assistant that answers general questions",
};
projectClient.AgentAdministrationClient.CreateAgentVersion(
foundryAgentName,
options: new(agentDefinition));
// Create a conversation for multi-turn chat
ProjectConversation conversation = projectClient.ProjectOpenAIClient.GetProjectConversationsClient().CreateProjectConversation();
// Chat with the agent to answer questions
ProjectResponsesClient responsesClient = projectClient.ProjectOpenAIClient.GetProjectResponsesClientForAgent(
defaultAgent: foundryAgentName,
defaultConversationId: conversation.Id);
ResponseResult response = responsesClient.CreateResponse("What is the size of France in square miles?");
Console.WriteLine(response.GetOutputText());
// Ask a follow-up question in the same conversation
response = responsesClient.CreateResponse("And what is the capital city?");
Console.WriteLine(response.GetOutputText());
import { DefaultAzureCredential } from "@azure/identity";
import { AIProjectClient } from "@azure/ai-projects";
// Format: "https://resource_name.services.ai.azure.com/api/projects/project_name"
const FOUNDRY_PROJECT_ENDPOINT = "your_project_endpoint";
const FOUNDRY_AGENT_NAME = "your-agent-name";
async function main(): Promise<void> {
const credential = new DefaultAzureCredential();
const project = new AIProjectClient(FOUNDRY_PROJECT_ENDPOINT, credential);
const agent = await project.agents.createVersion(FOUNDRY_AGENT_NAME, {
kind: "prompt",
model: "gpt-5-mini", // supports all Foundry direct models
instructions: "You are a helpful assistant that answers general questions",
});
if (!agent.name || !agent.version) {
throw new Error("The created agent did not include a name and version.");
}
const agentName = agent.name;
const agentVersion = agent.version;
const openai = project.getOpenAIClient();
try {
// Create a conversation for multi-turn chat
const conversation = await openai.conversations.create();
try {
// Chat with the agent to answer questions
const response = await openai.responses.create(
{
conversation: conversation.id,
input: "What is the size of France in square miles?",
},
{
body: { agent_reference: { name: agentName, type: "agent_reference" } },
},
);
console.log(response.output_text);
// Ask a follow-up question in the same conversation
const response2 = await openai.responses.create(
{
conversation: conversation.id,
input: "And what is the capital city?",
},
{
body: { agent_reference: { name: agentName, type: "agent_reference" } },
},
);
console.log(response2.output_text);
} finally {
await openai.conversations.delete(conversation.id);
}
} finally {
await project.agents.deleteVersion(agentName, agentVersion);
}
}
main().catch((error) => {
console.error(error);
process.exitCode = 1;
});
package com.azure.ai.foundry.samples;
import com.azure.ai.agents.AgentsClient;
import com.azure.ai.agents.AgentsClientBuilder;
import com.azure.ai.agents.models.PromptAgentDefinition;
import com.azure.identity.DefaultAzureCredentialBuilder;
import com.openai.client.OpenAIClient;
import com.openai.models.conversations.Conversation;
import com.openai.models.responses.Response;
import com.openai.models.responses.ResponseCreateParams;
public class ChatWithAgent {
public static void main(String[] args) {
// Format: "https://resource_name.services.ai.azure.com/api/projects/project_name"
String foundryProjectEndpoint = "your_project_endpoint";
String foundryAgentName = "your-agent-name";
AgentsClientBuilder builder = new AgentsClientBuilder()
.credential(new DefaultAzureCredentialBuilder().build())
.endpoint(foundryProjectEndpoint);
// Create the agent (or a new version, if it already exists)
AgentsClient agentsClient = builder.buildAgentsClient();
PromptAgentDefinition agentDefinition = new PromptAgentDefinition("gpt-5-mini") // supports all Foundry direct models
.setInstructions("You are a helpful assistant that answers general questions");
agentsClient.createAgentVersion(foundryAgentName, agentDefinition);
// Create an OpenAI client bound to the agent endpoint
OpenAIClient openai = builder.buildAgentScopedOpenAIClient(foundryAgentName);
// Create a conversation for multi-turn chat
Conversation conversation = openai.conversations().create();
// Chat with the agent to answer questions
Response response = openai.responses().create(
ResponseCreateParams.builder()
.conversation(conversation.id())
.input("What is the size of France in square miles?")
.build());
printResponse(response);
// Ask a follow-up question in the same conversation
Response followUp = openai.responses().create(
ResponseCreateParams.builder()
.conversation(conversation.id())
.input("And what is the capital city?")
.build());
printResponse(followUp);
}
private static void printResponse(Response response) {
response.output().forEach(item -> item.message().ifPresent(message ->
message.content().forEach(content -> content.outputText().ifPresent(
text -> System.out.println(text.text())))));
}
}
Ersetzen Sie YOUR-FOUNDRY-RESOURCE-NAME sie durch Ihre Werte, und legen Sie die FOUNDRY_AGENT_NAME Umgebungsvariable auf den verwendeten Agentnamen fest:
# Generate a response using the agent
curl -X POST "https://YOUR-FOUNDRY-RESOURCE-NAME.services.ai.azure.com/api/projects/YOUR-PROJECT-NAME/agents/${FOUNDRY_AGENT_NAME}/endpoint/protocols/openai/responses?api-version=v1" \
-H "Content-Type: application/json" \
-H "Authorization: Bearer $AZURE_AI_AUTH_TOKEN" \
-d '{
"input": [{"role": "user", "content": "What is the size of France in square miles?"}]
}'
# Optional Step: Create a conversation to use with the agent
curl -X POST "https://YOUR-FOUNDRY-RESOURCE-NAME.services.ai.azure.com/api/projects/YOUR-PROJECT-NAME/agents/${FOUNDRY_AGENT_NAME}/endpoint/protocols/openai/conversations?api-version=v1" \
-H "Content-Type: application/json" \
-H "Authorization: Bearer $AZURE_AI_AUTH_TOKEN" \
-d '{
"items": [
{
"type": "message",
"role": "user",
"content": [
{
"type": "input_text",
"text": "What is the size of France in square miles?"
}
]
}
]
}'
# Lets say Conversation ID created is conv_123456789. Use this in the next step
#Optional Step: Ask a follow-up question in the same conversation
curl -X POST "https://YOUR-FOUNDRY-RESOURCE-NAME.services.ai.azure.com/api/projects/YOUR-PROJECT-NAME/agents/${FOUNDRY_AGENT_NAME}/endpoint/protocols/openai/responses?api-version=v1" \
-H "Content-Type: application/json" \
-H "Authorization: Bearer $AZURE_AI_AUTH_TOKEN" \
-d '{
"conversation": "<CONVERSATION_ID>",
"input": [{"role": "user", "content": "And what is the capital?"}]
}'
Sie sehen die Antworten des Agents auf beide Eingabeaufforderungen. Die Folgeantwort zeigt, dass der Agent den Gesprächsverlauf über mehrere Dialogschritte hinweg beibehält.
Überprüfen Sie als Nächstes Ihre erste Agent-Ablaufverfolgung im Foundry-Portal.
Bereinigen von Ressourcen
Wenn Sie keine der von Ihnen erstellten Ressourcen mehr benötigen, löschen Sie die dem Projekt zugeordnete Ressourcengruppe.
- Wählen Sie im portal Azure die Ressourcengruppe aus, und wählen Sie dann Delete aus. Vergewissern Sie sich, dass Sie die Ressourcengruppe löschen möchten.
Verwandte Inhalte