Muistiinpano
Tämän sivun käyttö edellyttää valtuutusta. Voit yrittää kirjautua sisään tai vaihtaa hakemistoa.
Tämän sivun käyttö edellyttää valtuutusta. Voit yrittää vaihtaa hakemistoa.
Create an agent and get a response — in just a few lines of code.
dotnet add package Microsoft.Agents.AI.Foundry --prerelease
Create the agent:
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");
Warning
DefaultAzureCredential is convenient for development but requires careful consideration in production. In production, consider using a specific credential (e.g., ManagedIdentityCredential) to avoid latency issues, unintended credential probing, and potential security risks from fallback mechanisms.
Run it:
Console.WriteLine(await agent.RunAsync("What is the largest city in France?"));
Or stream the response:
await foreach (var update in agent.RunStreamingAsync("Tell me a one-sentence fun fact."))
{
Console.Write(update);
}
Tip
See here for a full runnable sample application.
pip install agent-framework-foundry azure-identity
The agent-framework-foundry package installs agent-framework-core with the Microsoft Foundry integration. The agent-framework metapackage also installs agent-framework-core, together with many other optional integrations.
Sign in with the Azure CLI by running az login. In the following example, replace project_endpoint with your Microsoft Foundry project endpoint and model with your model deployment name.
Save the complete example as hello_agent.py:
import asyncio
from agent_framework import Agent
from agent_framework.foundry import FoundryChatClient
from azure.identity import AzureCliCredential
async def main() -> None:
agent = Agent(
client=FoundryChatClient(
project_endpoint="https://your-account.services.ai.azure.com/api/projects/your-project",
model="gpt-6-luna",
credential=AzureCliCredential(),
),
instructions="You are a friendly assistant. Keep your answers brief.",
)
print(await agent.run("What is the largest city of France?"))
if __name__ == "__main__":
asyncio.run(main())
Run the example:
python hello_agent.py
Note
Agent Framework does not automatically load .env files. To use a .env file for configuration, call load_dotenv() at the start of your script:
from dotenv import load_dotenv
load_dotenv()
Alternatively, set environment variables directly in your shell or IDE. See the settings migration note for details.
Tip
See the full sample for the complete runnable file.
go get github.com/microsoft/agent-framework-go
Create the agent:
package main
import (
"context"
"fmt"
"os"
"github.com/microsoft/agent-framework-go/agent"
"github.com/microsoft/agent-framework-go/provider/foundryprovider"
"github.com/Azure/azure-sdk-for-go/sdk/azidentity"
)
func main() {
endpoint := os.Getenv("FOUNDRY_PROJECT_ENDPOINT")
model := os.Getenv("FOUNDRY_MODEL")
token, err := azidentity.NewDefaultAzureCredential(nil)
if err != nil {
panic(err)
}
a := foundryprovider.NewAgent(
endpoint,
token,
foundryprovider.ModelDeployment(model),
foundryprovider.AgentConfig{
Instructions: "You are a friendly assistant. Keep your answers brief.",
Config: agent.Config{
Name: "HelloAgent",
},
},
)
Warning
azidentity.NewDefaultAzureCredential is convenient for development but requires careful consideration in production. In production, consider using a specific credential, such as azidentity.NewManagedIdentityCredential, to avoid latency issues, unintended credential probing, and potential security risks from fallback mechanisms.
Run it:
ctx := context.Background()
resp, err := a.RunText(ctx, "What is the largest city in France?").Collect()
fmt.Println(resp, err)
Or stream the response:
for update, err := range a.RunText(ctx, "Tell me a one-sentence fun fact.", agent.Stream(true)) {
if err != nil {
panic(err)
}
fmt.Print(update)
}
}
Tip
See the full sample for the complete runnable file.
Next steps
Go deeper: