建立一個客服,只需幾行程式碼就能獲得回應。
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 metapackage 也會安裝 agent-framework-core,以及許多其他可選的整合功能。
請用 Azure CLI 登入,執行 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
完整範例可執行檔案請參閱 完整範例 。
下一步
深入探討: