共用方式為


使用 Bing 搜尋進行基礎

使用 Bing 搜尋 進行基礎,可讓您的 Azure AI 代理程式在產生回應時納入即時公用 Web 數據。 您必須使用 Bing 搜尋資源建立地面,然後將此資源連線到您的 Azure AI 代理程式。 當使用者傳送查詢時,Azure AI 代理程式會決定是否應該利用 Bing 搜尋進行地面處理。 如果是,它會利用 Bing 來搜尋公用 Web 數據並傳回相關的區塊。 最後,Azure AI 代理程式會使用傳回的區塊來產生回應。

您可以詢問「今日熱門新聞是什麼」或「美國零售行業的最新更新為何?」問題,這些更新需要即時的公用數據。

開發人員和終端用戶無法存取使用 Bing 搜尋從 Grounding 傳回的原始內容。 不過,回應包含引文,其中包含用來產生回應的網站連結,以及用於搜尋的 Bing 查詢連結。 這兩 個參考 必須保留並顯示在Microsoft所提供的確切窗體,根據 Bing 搜尋的使用 和顯示需求如需詳細數據,請參閱如何使用 Bing 搜尋結果顯示地面。

重要

  1. 使用 Bing 搜尋使用地面可能會產生成本。 如需詳細資料,請參閱定價頁面
  2. 透過 Azure CLI 等程式代碼優先體驗建立和使用以 Bing 搜尋資源為依據,或透過部署範本進行部署,即表示您同意受約束,並遵守可用的條款,這些條款 https://www.microsoft.com/en-us/bing/apis/grounding-legal可能會不時更新。

設定

注意

  1. 使用 Bing 搜尋的基礎僅適用於下列 Azure OpenAI 模型:gpt-3.5-turbo-0125、、gpt-4-0125-previewgpt-4-turbo-2024-04-09gpt-4o-0513
  1. 依照快速入門中的步驟建立 Azure AI 代理程式。

  2. 手動 將 Bing 搜尋註冊 為 Azure 資源提供者。 您必須擁有對資源提供者執行 /register/action 作業的權限。 許可權包含在參與者和擁有者角色中

        az provider register --namespace 'Microsoft.Bing'
    
  3. 開發人員可以使用 Bing 搜尋資源建立新的地面。 在 Azure 入口網站 中使用 Bing 搜尋資源建立新的 [定位],然後在建立表單中選取不同的欄位。 請務必在與 Azure AI 代理程式、AI 專案和其他資源相同的資源群組中使用 Bing 搜尋資源來建立此基礎。

  4. 使用 Bing 搜尋資源建立地面之後,您可以在 Azure 入口網站 中找到它。 流覽至您已在 中建立資源的資源群組,使用您已建立的 Bing 搜尋資源來搜尋 [地面]。

    Azure 入口網站 中 Bing 資源的螢幕快照。

  5. 選取您已建立的 Bing 搜尋資源,並複製任何 API 金鑰。

    Azure 入口網站 中 Bing 資源的金鑰和端點畫面螢幕快照。

  6. 移至 Azure AI Foundry 入口網站 ,然後選取 AI 專案(請確定它位於與 Bing 搜尋資源相同的基礎資源群組中)。 按兩下 [管理中心]。

    AI 專案的 [設定] 按鈕螢幕快照。

  7. 在 [設定] 頁面中選取 [+ 新增連線 ]。

    注意

    如果您在稍後重新產生 API 金鑰,則必須使用新的金鑰更新連線。

    AI 專案的連線畫面螢幕快照。

  8. 選取其他資源類型的 API 金鑰

    可供連線使用之資源類型的螢幕快照。

  9. 輸入下列資訊,然後使用 Bing 搜尋資源建立與 Grounding 的新連線。

    • 端點:https://api.bing.microsoft.com/
    • 索引鍵:YOUR_API_KEY
    • 線上名稱: YOUR_CONNECTION_NAME (您將在此範例程式代碼中使用此連線名稱。)
    • 存取:您可以選擇此專案,或共用至所有專案。 只要確定在下列範例程式代碼中,您輸入的專案 連接字串 即可存取此連線。

如何使用 Bing 搜尋結果顯示地面

根據 Bing 的使用規定和使用和顯示需求,您必須在自定義介面中同時顯示網站 URL 和 Bing 搜尋查詢 URL。 您可以透過 annotationsrunstep API 回應中的 參數,以及透過詳細資料來尋找 Bing 搜尋查詢 URL 的網站 URL。 若要轉譯網頁,建議您將 Bing 搜尋查詢 URL 的端點取代為 www.bing.com ,而您的 Bing 搜尋查詢 URL 看起來會像 “https://www.bing.com/search?q={search query}”

run_steps = project_client.agents.list_run_steps(run_id=run.id, thread_id=thread.id)
run_steps_data = run_steps['data']
print(f"Last run step detail: {run_steps_data}")

顯示 Bing 搜尋結果引文的螢幕快照。

Microsoft會使用您透過 Bing 傳送至 Grounding 的數據來改善Microsoft產品和服務。 當您將個人資料傳送至此服務時,您必須負責從數據主體取得足夠的同意。 在線服務條款中的數據保護條款不適用於使用 Bing 的停駐。

使用使用 Bing 搜尋時,將會受到使用規定所規範。 藉由搭配 Bing 搜尋使用地面,您同意受約束並遵守這些使用規定。

建立客戶端物件,其中包含連線到 AI 專案和其他資源的 連接字串。

import os
from azure.ai.projects import AIProjectClient
from azure.identity import DefaultAzureCredential
from azure.ai.projects.models import BingGroundingTool


# Create an Azure AI Client from a connection string, copied from your Azure AI Foundry project.
# At the moment, it should be in the format "<HostName>;<AzureSubscriptionId>;<ResourceGroup>;<HubName>"
# Customer needs to login to Azure subscription via Azure CLI and set the environment variables

project_client = AIProjectClient.from_connection_string(
    credential=DefaultAzureCredential(),
    conn_str=os.environ["PROJECT_CONNECTION_STRING"],
)

步驟 2:使用 Bing 搜尋工具啟用地面

若要讓具有 Bing 搜尋工具的地面可供您的代理程式使用,請使用連線來初始化工具,並將它附加至代理程式。 您可以在 Azure AI Foundry 入口網站中專案的 [ 已連線資源 ] 區段中找到您的連線。

bing_connection = project_client.connections.get(
    connection_name=os.environ["BING_CONNECTION_NAME"]
)
conn_id = bing_connection.id

print(conn_id)

# Initialize agent bing tool and add the connection id
bing = BingGroundingTool(connection_id=conn_id)

# Create agent with the bing tool and process assistant run
with project_client:
    agent = project_client.agents.create_agent(
        model="gpt-4o",
        name="my-assistant",
        instructions="You are a helpful assistant",
        tools=bing.definitions,
        headers={"x-ms-enable-preview": "true"}
    )
    print(f"Created agent, ID: {agent.id}")

步驟 3:建立線程

# Create thread for communication
thread = project_client.agents.create_thread()
print(f"Created thread, ID: {thread.id}")

# Create message to thread
message = project_client.agents.create_message(
    thread_id=thread.id,
    role="user",
    content="What is the top news today",
)
print(f"Created message, ID: {message.id}")

步驟 4:建立執行並檢查輸出

建立執行,並觀察模型使用以 Bing 搜尋工具來提供用戶問題的回應。

# Create and process agent run in thread with tools
run = project_client.agents.create_and_process_run(thread_id=thread.id, assistant_id=agent.id)
print(f"Run finished with status: {run.status}")

# Retrieve run step details to get Bing Search query link
# To render the webpage, we recommend you replace the endpoint of Bing search query URLs with `www.bing.com` and your Bing search query URL would look like "https://www.bing.com/search?q={search query}"
run_steps = project_client.agents.list_run_steps(run_id=run.id, thread_id=thread.id)
run_steps_data = run_steps['data']
print(f"Last run step detail: {run_steps_data}")

if run.status == "failed":
    print(f"Run failed: {run.last_error}")

# Delete the assistant when done
project_client.agents.delete_agent(agent.id)
print("Deleted agent")

# Fetch and log all messages
messages = project_client.agents.list_messages(thread_id=thread.id)
print(f"Messages: {messages}")

下一步

請參閱使用 Bing 搜尋進行基礎的完整範例。