Oharra
Baimena behar duzu orria atzitzeko. Direktorioetan saioa has dezakezu edo haiek alda ditzakezu.
Baimena behar duzu orria atzitzeko. Direktorioak alda ditzakezu.
Cree un agente y obtenga una respuesta, en solo unas pocas líneas de código.
dotnet add package Azure.AI.Projects --prerelease
dotnet add package Azure.Identity
dotnet add package Microsoft.Agents.AI.Foundry --prerelease
Cree el agente:
using System;
using Azure.AI.Projects;
using Azure.Identity;
using Microsoft.Agents.AI;
var endpoint = Environment.GetEnvironmentVariable("AZURE_OPENAI_ENDPOINT")
?? throw new InvalidOperationException("Set AZURE_OPENAI_ENDPOINT");
var deploymentName = Environment.GetEnvironmentVariable("AZURE_OPENAI_DEPLOYMENT_NAME") ?? "gpt-4o-mini";
AIAgent agent = new AIProjectClient(new Uri(endpoint), new DefaultAzureCredential())
.AsAIAgent(
model: deploymentName,
instructions: "You are a friendly assistant. Keep your answers brief.",
name: "HelloAgent");
Advertencia
DefaultAzureCredential es conveniente para el desarrollo, pero requiere una consideración cuidadosa en producción. En producción, considere usar una credencial específica (por ejemplo, ManagedIdentityCredential) para evitar problemas de latencia, sondeos de credenciales no deseados y posibles riesgos de seguridad de los mecanismos de respaldo.
Ejecútelo:
Console.WriteLine(await agent.RunAsync("What is the largest city in France?"));
O bien transmita la respuesta:
await foreach (var update in agent.RunStreamingAsync("Tell me a one-sentence fun fact."))
{
Console.Write(update);
}
Sugerencia
Consulte aquí para obtener una aplicación de ejemplo ejecutable completa.
pip install agent-framework
Cree y ejecute un agente:
client = FoundryChatClient(
project_endpoint="https://your-project.services.ai.azure.com",
model="gpt-4o",
credential=AzureCliCredential(),
)
agent = Agent(
client=client,
name="HelloAgent",
instructions="You are a friendly assistant. Keep your answers brief.",
)
# Non-streaming: get the complete response at once
result = await agent.run("What is the capital of France?")
print(f"Agent: {result}")
O bien transmita la respuesta:
# Streaming: receive tokens as they are generated
print("Agent (streaming): ", end="", flush=True)
async for chunk in agent.run("Tell me a one-sentence fun fact.", stream=True):
if chunk.text:
print(chunk.text, end="", flush=True)
print()
Nota:
Agent Framework no carga .env automáticamente los archivos. Para usar un .env archivo para la configuración, llame load_dotenv() al principio del script:
from dotenv import load_dotenv
load_dotenv()
Como alternativa, establezca variables de entorno directamente en el shell o ide. Consulte la nota de migración de configuración para obtener más información.
Sugerencia
Consulte el ejemplo completo para obtener el archivo ejecutable completo.
Pasos siguientes
Vaya más profundamente:
- Introducción a los agentes : descripción de la arquitectura del agente
- Proveedores : consulte todos los proveedores admitidos.