步驟一:你的第一位經紀人

建立一個客服,只需幾行程式碼就能獲得回應。

dotnet add package Azure.AI.Projects --prerelease
dotnet add package Azure.Identity
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");

警告

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);
}

小提示

請見 處的完整可執行範例應用。

pip install agent-framework azure-identity

建立並執行代理人:

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}")

或者串流回應:

# 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()

備註

代理框架 不會 自動載入 .env 檔案。 若要使用 .env 檔案進行設定,請在腳本開頭呼叫 load_dotenv()

from dotenv import load_dotenv
load_dotenv()

或者,直接在你的 shell 或 IDE 裡設定環境變數。 詳情請參閱 設定遷移說明

小提示

完整範例可執行檔案請參閱 完整範例

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",
            },
        },
    )

警告

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)
    }
}

小提示

完整範例可執行檔案請參閱 完整範例

後續步驟

深入探討: