エージェントを作成し、わずか数行のコードで応答を取得します。
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");
Warnung
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
エージェントを作成して実行します。
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()
注
Agent Framework は、 ファイルを自動的に読み込.env。
.env ファイルを構成に使用するには、スクリプトの開始時にload_dotenv()を呼び出します。
from dotenv import load_dotenv
load_dotenv()
または、シェルまたは IDE で環境変数を直接設定します。 詳細については、 設定移行に関するメモ を参照してください。
ヒント
完全な実行可能ファイルについては、 完全なサンプル を参照してください。
次のステップ
より深く進む: