创建代理并获取响应 - 只需几行代码即可。
dotnet add package Microsoft.Agents.AI.Foundry --prerelease
创建代理:
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 对于开发来说很方便,但在生产中需要仔细考虑。 在生产环境中,请考虑使用特定凭据(例如), ManagedIdentityCredential以避免延迟问题、意外凭据探测以及回退机制的潜在安全风险。
运行它:
Console.WriteLine(await agent.RunAsync("What is the largest city in France?"));
或流式传输响应:
await foreach (var update in agent.RunStreamingAsync("Tell me a one-sentence fun fact."))
{
Console.Write(update);
}
Tip
有关完整的可运行示例应用程序,请参阅 此处 。
pip install agent-framework-foundry azure-identity
agent-framework-foundry 包安装带有 Microsoft Foundry 集成的 agent-framework-core。 元包 agent-framework 还会安装 agent-framework-core 以及许多其他可选集成。
通过运行使用az login登录。 在以下示例中,将 project_endpoint 替换为 Microsoft Foundry 项目终结点,并将 model 替换为模型部署名称。
将完整示例保存为 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())
运行示例:
python hello_agent.py
注释
代理框架 不会 自动加载 .env 文件。 若要使用 .env 文件进行配置,请在脚本开头调用 load_dotenv() :
from dotenv import load_dotenv
load_dotenv()
或者,直接在 shell 或 IDE 中设置环境变量。 有关详细信息,请参阅 设置迁移说明 。
Tip
请查看完整示例以获取完整的可运行文件。
go get github.com/microsoft/agent-framework-go
创建代理:
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 对于开发来说很方便,但在生产中需要仔细考虑。 在生产环境中,请考虑使用特定的凭据,例如 azidentity.NewManagedIdentityCredential,避免延迟问题、意外凭据探测以及回退机制的潜在安全风险。
运行它:
ctx := context.Background()
resp, err := a.RunText(ctx, "What is the largest city in France?").Collect()
fmt.Println(resp, err)
或流式传输响应:
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
请查看完整示例以获取完整的可运行文件。
后续步骤
更深入: