Web 搜索允许代理在 Web 中搜索 up-to日期信息。 此工具使代理能够回答有关当前事件的问题、查找文档以及访问其训练数据之外的信息。
注释
Web 搜索可用性取决于基础代理提供程序。 有关提供程序特定的支持,请参阅 提供程序概述 。
以下示例演示如何使用 Web 搜索工具创建代理:
using System;
using Azure.AI.OpenAI;
using Azure.Identity;
using Microsoft.Agents.AI;
using Microsoft.Extensions.AI;
// Requires: dotnet add package Microsoft.Agents.AI.OpenAI --prerelease
var endpoint = Environment.GetEnvironmentVariable("AZURE_OPENAI_ENDPOINT")
?? throw new InvalidOperationException("AZURE_OPENAI_ENDPOINT is not set.");
var deploymentName = Environment.GetEnvironmentVariable("AZURE_OPENAI_DEPLOYMENT_NAME") ?? "gpt-4o-mini";
// Create an agent with the web search (Bing grounding) tool
AIAgent agent = new AzureOpenAIClient(new Uri(endpoint), new AzureCliCredential())
.GetChatClient(deploymentName)
.AsAIAgent(
instructions: "You are a helpful assistant that can search the web for current information.",
tools: [new WebSearchToolDefinition()]);
Console.WriteLine(await agent.RunAsync("What is the current weather in Seattle?"));
以下示例演示如何使用 Web 搜索工具创建代理:
# Copyright (c) Microsoft. All rights reserved.
import asyncio
from agent_framework import Agent
from agent_framework.openai import OpenAIResponsesClient
"""
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 = OpenAIResponsesClient()
# Create web search tool with location context
web_search_tool = client.get_web_search_tool(
user_location={"city": "Seattle", "region": "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())