Web Search を使用すると、エージェントは Web で up-to-date 情報を検索できます。 このツールを使用すると、エージェントは現在のイベントに関する質問に答え、ドキュメントを見つけ、トレーニング データ以外の情報にアクセスできます。
注
Web Search の可用性は、基になるエージェント プロバイダーによって異なります。 プロバイダー固有のサポートについては、「 プロバイダーの概要 」を参照してください。
次の例は、Web Search ツールを使用してエージェントを作成する方法を示しています。
using System;
using Azure.AI.Projects;
using Azure.Identity;
using Microsoft.Agents.AI;
using Microsoft.Extensions.AI;
// Requires: dotnet add package Microsoft.Agents.AI.Foundry --prerelease
var endpoint = Environment.GetEnvironmentVariable("FOUNDRY_PROJECT_ENDPOINT")
?? throw new InvalidOperationException("FOUNDRY_PROJECT_ENDPOINT is not set.");
var deploymentName = Environment.GetEnvironmentVariable("FOUNDRY_MODEL") ?? "gpt-5.4-mini";
// Create an agent with hosted web search.
AIAgent agent = new AIProjectClient(new Uri(endpoint), new DefaultAzureCredential())
.AsAIAgent(
model: deploymentName,
instructions: "You are a helpful assistant that can search the web for current information.",
tools: [new HostedWebSearchTool()]);
Console.WriteLine(await agent.RunAsync("What is the current weather in Seattle?"));
Warnung
DefaultAzureCredential は開発には便利ですが、運用環境では慎重に考慮する必要があります。 運用環境では、待機時間の問題、意図しない資格情報のプローブ、フォールバック メカニズムによる潜在的なセキュリティ リスクを回避するために、特定の資格情報 ( ManagedIdentityCredential など) を使用することを検討してください。
次の例は、Web Search ツールを使用してエージェントを作成する方法を示しています。
# Copyright (c) Microsoft. All rights reserved.
import asyncio
from agent_framework import Agent
from agent_framework.openai import OpenAIChatClient
"""
OpenAI Responses Client with Web Search Example
This sample demonstrates using get_web_search_tool() with OpenAI Responses Client
for direct real-time information retrieval and current data access.
"""
async def main() -> None:
client = OpenAIChatClient()
# Create web search tool with location context
web_search_tool = client.get_web_search_tool(
user_location={"city": "Seattle", "country": "US"},
)
agent = Agent(
client=client,
instructions="You are a helpful assistant that can search the web for current information.",
tools=[web_search_tool],
)
message = "What is the current weather? Do not ask for my current location."
stream = False
print(f"User: {message}")
if stream:
print("Assistant: ", end="")
async for chunk in agent.run(message, stream=True):
if chunk.text:
print(chunk.text, end="")
print("")
else:
response = await agent.run(message)
print(f"Assistant: {response}")
if __name__ == "__main__":
asyncio.run(main())
Web 検索
hostedtool.WebSearchの種類では、サポートするプロバイダーを使用するときにサーバー側の Web 検索を有効にします。
import "github.com/microsoft/agent-framework-go/tool/hostedtool"
webSearch := &hostedtool.WebSearch{}
a := foundryprovider.NewAgent(endpoint, token, foundryprovider.ModelDeployment(model), foundryprovider.AgentConfig{
Config: agent.Config{
Tools: []tool.Tool{webSearch},
},
})
注
Web 検索はホステッド ツールであり、検索はローカルではなく AI サービスによって実行されます。
Harness Agent で Web 検索を使用する
プレーン エージェントの場合は、前に示したように、エージェントのツールに HostedWebSearchTool を追加します。
HarnessAgent では、既定で 1 つの HostedWebSearchTool が追加されるため、ツールの登録は必要ありません。
using Microsoft.Agents.AI;
AIAgent agent = chatClient.AsHarnessAgent(new HarnessAgentOptions
{
ChatOptions = new()
{
Instructions = "Use web search for current information and cite the sources you used.",
},
});
選択したプロバイダーがホスト型 Web 検索をサポートしていない場合、またはプロバイダー固有の検索ツールを自分で登録する場合は、ChatOptions.Toolsを使用してDisableWebSearch = trueを設定します。 既定を無効にせずに独自の Web 検索ツールを追加すると、エージェントは両方のツールを受け取ります。
Web 検索はモデル プロバイダーによってホストされます。Harness を管理するためのローカル検索クライアント ライフサイクルはありません。 可用性、サポートされているモデル、検索パラメーター、データ所在地、課金は、 IChatClient プロバイダーによって異なります。 サポートされていないクライアントは、要求の送信時にホストされているツールを拒否できます。
検索クエリと結果を外部信頼境界を越えるデータとして扱います。 クエリにシークレットを含めず、取得したページを、間接的なプロンプト挿入を含むことができる信頼されていないコンテンツとして扱います。 アクションを実行する前に、重要なクレームと引用を確認します。
HarnessAgent は、 Microsoft.Agents.AI.Harness パッケージから入手できます。
プレーン エージェントの場合は、前に示したように、 client.get_web_search_tool(...) 呼び出し、返されたツールを Agentに渡します。
create_harness_agentは、クライアントがSupportsWebSearchToolを実装するときに、既定では引数なしでclient.get_web_search_tool()を呼び出します。
from agent_framework import create_harness_agent
agent = create_harness_agent(client=client)
クライアントが SupportsWebSearchToolを実装していない場合、ファクトリは警告をログに記録し、Web 検索なしで続行します。 自動登録と警告を抑制するには、 disable_web_search=True を設定します。
プロバイダー固有の設定を渡すには、既定値を無効にして、構成済みのツールを明示的に登録します。
agent = create_harness_agent(
client=client,
disable_web_search=True,
tools=[
client.get_web_search_tool(
user_location={"city": "Seattle", "country": "US"},
search_context_size="medium",
)
],
)
プロバイダーは、hosted-search の実行とライフサイクルを所有しています。 サポートされるパラメーター、モデル、データ処理、課金は、クライアントの実装によって異なります。 アクションを実行する前に、クエリにシークレットを配置したり、取得したコンテンツを信頼できない入力として扱ったり、重要な要求や引用を確認したりしないでください。
create_harness_agent は agent-framework-coreでリリースされます。Web 検索は、 SupportsWebSearchToolを実装するクライアント経由でのみ使用できます。
パッケージ化された Go Harness は現在使用できません。 前に示すように、プレーンな Go エージェントに hostedtool.WebSearch を追加します。