다음을 통해 공유


Web Search

Web Search를 사용하면 에이전트가 웹에서 up-to날짜 정보를 검색할 수 있습니다. 이 도구를 사용하면 에이전트가 현재 이벤트에 대한 질문에 답변하고, 설명서를 찾고, 학습 데이터 이외의 정보에 액세스할 수 있습니다.

비고

웹 검색 가용성은 기본 에이전트 공급자에 따라 달라집니다. 공급자별 지원은 공급자 개요를 참조하세요.

다음 예제에서는 웹 검색 도구를 사용하여 에이전트를 만드는 방법을 보여줍니다.

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?"));

다음 예제에서는 웹 검색 도구를 사용하여 에이전트를 만드는 방법을 보여줍니다.

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

다음 단계: