Skip to main content

使用 Azure Cognitive Search 和 Azure Identity 创建助手

在 Colab 中打开 在 GitHub 上打开

本笔记本演示了如何使用助手代理与 Azure Cognitive Search 和 Azure Identity 结合使用。助手代理使用与 Azure Cognitive Search 交互的工具来提取相关数据。

先决条件

在运行此笔记本之前,请确保满足以下先决条件:

依赖项

  1. Autogen
  2. Azure SDK
  3. Cognitive Search/AI Search

如果在 Azure 门户中启用了 AI 搜索,您可以使用以下代码创建一个可以搜索 Azure Cognitive Search 的助手代理。

AI 搜索设置详细信息:

安装 Azure CLI

此笔记本需要 Azure CLI 进行身份验证。按照以下步骤安装和配置 Azure CLI:

  1. 下载并安装 Azure CLI
    • 访问 Azure CLI 安装页面 并按照适用于您的操作系统的说明进行操作。
    • Mac 用户可以使用 Homebrew 安装 Azure CLI,命令为 brew install azure-cli
  2. 验证安装
    • 在下面的单元格中执行 az --version,检查 Azure CLI 是否正确安装。
  3. 登录 Azure
    • 在下面的单元格中执行 az login,以登录到您的 Azure 帐户。此步骤是必需的,因为笔记本使用 AzureCliCredential,该凭据基于当前登录的 Azure 帐户检索令牌。

检查 Azure CLI 安装

运行下面的单元格,检查 Azure CLI 是否已安装并正确配置在您的系统上。

检查 Azure CLI 安装和登录状态

# 检查 Azure CLI 安装和登录状态
# !az --version
# !az login

安装所需的软件包

运行下面的单元格,安装此笔记本所需的软件包。

!pip3 install pyautogen==0.2.16
!pip3 install python-dotenv==1.0.1
!pip3 install pyautogen[graph]>=0.2.11
!pip3 install azure-search-documents==11.4.0b8
!pip3 install azure-identity==1.12.0
!pip3 install pyautogen==0.2.16
!pip3 install python-dotenv==1.0.1
!pip3 install pyautogen[graph]>=0.2.11
!pip3 install azure-search-documents==11.4.0b8
!pip3 install azure-identity==1.12.0

这是一些安装 Python 包的命令。你可以使用 pip3 命令来安装这些包。这些包分别是 pyautogen 版本为 0.2.16,python-dotenv 版本为 1.0.1,pyautogen[graph] 版本大于等于 0.2.11,azure-search-documents 版本为 11.4.0b8,以及 azure-identity 版本为 1.12.0。 接下来,您将导入此笔记本所需的包。

import json
import os

import requests
from azure.identity import DefaultAzureCredential
from azure.search.documents import SearchClient
from dotenv import load_dotenv

import autogen
from autogen import AssistantAgent, UserProxyAgent, register_function
from autogen.cache import Cache

load_dotenv()

# 导入认知搜索索引环境变量
AZURE_SEARCH_SERVICE = os.getenv("AZURE_SEARCH_SERVICE")
AZURE_SEARCH_INDEX = os.getenv("AZURE_SEARCH_INDEX")
AZURE_SEARCH_KEY = os.getenv("AZURE_SEARCH_KEY")
AZURE_SEARCH_API_VERSION = os.getenv("AZURE_SEARCH_API_VERSION")
AZURE_SEARCH_SEMANTIC_SEARCH_CONFIG = os.getenv("AZURE_SEARCH_SEMANTIC_SEARCH_CONFIG")
AZURE_SEARCH_SERVICE_ENDPOINT = os.getenv("AZURE_SEARCH_SERVICE_ENDPOINT")

接下来,您需要进行身份验证并创建一个 SearchClient 实例。

credential = DefaultAzureCredential()
endpoint = AZURE_SEARCH_SERVICE_ENDPOINT

from azure.identity import AzureCliCredential

credential = AzureCliCredential()
token = credential.get_token("https://cognitiveservices.azure.com/.default")

print("TOKEN", token.token)

client = SearchClient(endpoint=endpoint, index_name="test-index", credential=credential)

然后,加载配置列表并为 AssistantAgent 定义配置。

config_list = autogen.config_list_from_json(
env_or_file="OAI_CONFIG_LIST",
)

gpt4_config = {
"cache_seed": 42,
"temperature": 0,
"config_list": config_list,
"timeout": 120,
}

定义您的工具函数 search,用于与 Azure 认知搜索服务进行交互。

def search(query: str):
payload = json.dumps(
{
"search": query,
"vectorQueries": [{"kind": "text", "text": query, "k": 5, "fields": "vector"}],
"queryType": "semantic",
"semanticConfiguration": AZURE_SEARCH_SEMANTIC_SEARCH_CONFIG,
"captions": "extractive",
"answers": "extractive|count-3",
"queryLanguage": "en-US",
}
)

response = list(client.search(payload))

output = []
for result in response:
result.pop("titleVector")
result.pop("contentVector")
output.append(result)

return output

定义 AssistantAgentUserProxyAgent 实例,并将 search 函数注册到它们中。

cog_search = AssistantAgent(
name="COGSearch",
system_message="你是一个有用的AI助手。"
"你可以帮助处理Azure认知搜索。"
"完成任务后返回'TERMINATE'。",
llm_config=gpt4_config,
)

user_proxy = UserProxyAgent(
name="用户",
llm_config=False,
is_termination_msg=lambda msg: msg.get("content") is not None and "TERMINATE" in msg["content"],
human_input_mode="NEVER",
)

register_function(
search,
caller=cog_search,
executor=user_proxy,
name="search",
description="一个用于搜索认知搜索索引的工具",
)

最后,开始一个聊天。

if __name__ == "__main__":
import asyncio

async def main():
with Cache.disk() as cache:
await user_proxy.a_initiate_chat(
cog_search,
message="在'test-index'索引中搜索'Azure是什么?'",
cache=cache,
)

await main()
User (to COGSearch):

Search for 'What is Azure?' in the 'test-index' index

--------------------------------------------------------------------------------
COGSearch (to User):

***** Suggested tool Call (call_6Db6DFPNEp7J7Dz5dkAbbjDY): search *****
Arguments:
{"query":"What is Azure?"}
***********************************************************************

--------------------------------------------------------------------------------

>>>>>>>> EXECUTING ASYNC FUNCTION search...
User (to COGSearch):

User (to COGSearch):

***** Response from calling tool "call_6Db6DFPNEp7J7Dz5dkAbbjDY" *****
[{"id": "40", "title": "Azure Cognitive Search", "category": "AI + Machine Learning", "content": "Azure Cognitive Search is a fully managed search-as-a-service that enables you to build rich search experiences for your applications. It provides features like full-text search, faceted navigation, and filters. Azure Cognitive Search supports various data sources, such as Azure SQL Database, Azure Blob Storage, and Azure Cosmos DB. You can use Azure Cognitive Search to index your data, create custom scoring profiles, and integrate with other Azure services. It also integrates with other Azure services, such as Azure Cognitive Services and Azure Machine Learning.", "@search.score": 9.1308, "@search.reranker_score": null, "@search.highlights": null, "@search.captions": null}, {"id": "90", "title": "Azure Cognitive Services", "category": "AI + Machine Learning", "content": "Azure Cognitive Services is a collection of AI services and APIs that enable you to build intelligent applications using pre-built models and algorithms. It provides features like computer vision, speech recognition, and natural language processing. Cognitive Services supports various platforms, such as .NET, Java, Node.js, and Python. You can use Azure Cognitive Services to build chatbots, analyze images and videos, and process and understand text. It also integrates with other Azure services, such as Azure Machine Learning and Azure Cognitive Search.", "@search.score": 5.9858904, "@search.reranker_score": null, "@search.highlights": null, "@search.captions": null}, {"id": "68", "title": "Azure Database for MariaDB", "category": "Databases", "content": "Azure Database for MariaDB is a fully managed, scalable, and secure relational database service that enables you to build and manage MariaDB applications in Azure. It provides features like automatic backups, monitoring, and high availability. Database for MariaDB supports various data types, such as JSON, spatial, and full-text. You can use Azure Database for MariaDB to migrate your existing applications, build new applications, and ensure the performance and security of your data. It also integrates with other Azure services, such as Azure App Service and Azure Data Factory.", "@search.score": 3.9424267, "@search.reranker_score": null, "@search.highlights": null, "@search.captions": null}, {"id": "69", "title": "Azure SQL Managed Instance", "category": "Databases", "content": "Azure SQL Managed Instance is a fully managed, scalable, and secure SQL Server instance hosted in Azure. It provides features like automatic backups, monitoring, and high availability. SQL Managed Instance supports various data types, such as JSON, spatial, and full-text. You can use Azure SQL Managed Instance to migrate your existing applications, build new applications, and ensure the performance and security of your data. It also integrates with other Azure services, such as Azure App Service and Azure Data Factory.", "@search.score": 3.2041788, "@search.reranker_score": null, "@search.highlights": null, "@search.captions": null}, {"id": "66", "title": "Azure Database for MySQL", "category": "Databases", "content": "Azure Database for MySQL is a fully managed, scalable, and secure relational database service that enables you to build and manage MySQL applications in Azure. It provides features like automatic backups, monitoring, and high availability. Database for MySQL supports various data types, such as JSON, spatial, and full-text. You can use Azure Database for MySQL to migrate your existing applications, build new applications, and ensure the performance and security of your data. It also integrates with other Azure services, such as Azure App Service and Azure Data Factory.", "@search.score": 3.1852448, "@search.reranker_score": null, "@search.highlights": null, "@search.captions": null}, {"id": "67", "title": "Azure Database for PostgreSQL", "category": "Databases", "content": "Azure Database for PostgreSQL is a fully managed, scalable, and secure relational database service that enables you to build and manage PostgreSQL applications in Azure. It provides features like automatic backups, monitoring, and high availability. Database for PostgreSQL supports various data types, such as JSON, spatial, and full-text. You can use Azure Database for PostgreSQL to migrate your existing applications, build new applications, and ensure the performance and security of your data. It also integrates with other Azure services, such as Azure App Service and Azure Data Factory.", "@search.score": 2.8028796, "@search.reranker_score": null, "@search.highlights": null, "@search.captions": null}, {"id": "3", "title": "Azure Cognitive Services", "category": "AI + Machine Learning", "content": "Azure Cognitive Services are a set of AI services that enable you to build intelligent applications with powerful algorithms using just a few lines of code. These services cover a wide range of capabilities, including vision, speech, language, knowledge, and search. They are designed to be easy to use and integrate into your applications. Cognitive Services are fully managed, scalable, and continuously improved by Microsoft. It allows developers to create AI-powered solutions without deep expertise in machine learning.", "@search.score": 1.9905571, "@search.reranker_score": null, "@search.highlights": null, "@search.captions": null}]
**********************************************************************

--------------------------------------------------------------------------------
COGSearch (to User):

Here are the search results for "What is Azure?" from the index:

1. **Azure Cognitive Search**
- Category: AI + Machine Learning
- Content: Azure Cognitive Search is a fully managed search-as-a-service that enables you to build rich search experiences for your applications. It provides features like full-text search, faceted navigation, and filters. Azure Cognitive Search supports various data sources, such as Azure SQL Database, Azure Blob Storage, and Azure Cosmos DB. You can use Azure Cognitive Search to index your data, create custom scoring profiles, and integrate with other Azure services. It also integrates with Azure Cognitive Services and Azure Machine Learning.
- Search Score: 9.1308

2. **Azure Cognitive Services**
- Category: AI + Machine Learning
- Content: Azure Cognitive Services is a collection of AI services and APIs that enable you to build intelligent applications using pre-built models and algorithms. It provides features like computer vision, speech recognition, and natural language processing. Cognitive Services supports various platforms, such as .NET, Java, Node.js, and Python. You can use Azure Cognitive Services to build chatbots, analyze images and videos, and process and understand text. It also integrates with other Azure services, such as Azure Machine Learning and Azure Cognitive Search.
- Search Score: 5.9858904

3. **Azure Database for MariaDB**
- Category: Databases
- Content: Azure Database for MariaDB is a fully managed, scalable, and secure relational database service that enables you to build and manage MariaDB applications in Azure. It provides features like automatic backups, monitoring, and high availability. Database for MariaDB supports various data types, such as JSON, spatial, and full-text. You can use Azure Database for MariaDB to migrate your existing applications, build new applications, and ensure the performance and security of your data. It also integrates with other Azure services, such as Azure App Service and Azure Data Factory.
- Search Score: 3.9424267

4. **Azure SQL Managed Instance**
- Category: Databases
- Content: Azure SQL Managed Instance is a fully managed, scalable, and secure SQL Server instance hosted in Azure. It provides features like automatic backups, monitoring, and high availability. SQL Managed Instance supports various data types, such as JSON, spatial, and full-text. You can use Azure SQL Managed Instance to migrate your existing applications, build new applications, and ensure the performance and security of your data. It also integrates with other Azure services, such as Azure App Service and Azure Data Factory.
- Search Score: 3.2041788

5. **Azure Database for MySQL**
- Category: Databases
- Content: Azure Database for MySQL is a fully managed, scalable, and secure relational database service that enables you to build and manage MySQL applications in Azure. It provides features like automatic backups, monitoring, and high availability. Database for MySQL supports various data types, such as JSON, spatial, and full-text. You can use Azure Database for MySQL to migrate your existing applications, build new applications, and ensure the performance and security of your data. It also integrates with other Azure services, such as Azure App Service and Azure Data Factory.
- Search Score: 3.1852448

6. **Azure Database for PostgreSQL**
- Category: Databases
- Content: Azure Database for PostgreSQL is a fully managed, scalable, and secure relational database service that enables you to build and manage PostgreSQL applications in Azure. It provides features like automatic backups, monitoring, and high availability. Database for PostgreSQL supports various data types, such as JSON, spatial, and full-text. You can use Azure Database for PostgreSQL to migrate your existing applications, build new applications, and ensure the performance and security of your data. It also integrates with other Azure services, such as Azure App Service and Azure Data Factory.
- Search Score: 2.8028796

7. **Azure Cognitive Services**
- Category: AI + Machine Learning
- Content: Azure Cognitive Services are a set of AI services that enable you to build intelligent applications with powerful algorithms using just a few lines of code. These services cover a wide range of capabilities, including vision, speech, language, knowledge, and search. They are designed to be easy to use and integrate into your applications. Cognitive Services are fully managed, scalable, and continuously improved by Microsoft. It allows developers to create AI-powered solutions without deep expertise in machine learning.
- Search Score: 1.9905571

The search scores indicate the relevance of each result to the query "What is Azure?" with higher scores representing greater relevance. The top result provides a detailed explanation of Azure Cognitive Search, which is a part of the Azure platform.

--------------------------------------------------------------------------------
User (to COGSearch):



--------------------------------------------------------------------------------
COGSearch (to User):

TERMINATE

--------------------------------------------------------------------------------

标题一

这是一篇科普文章,将介绍一些关于人工智能(AI)的基本概念和应用。我们将从最基础的定义开始,逐步深入探讨AI的不同方面。

什么是人工智能?

人工智能是一种模拟人类智能的技术。它可以使计算机系统执行类似于人类的智能任务,例如学习、推理、问题解决和语言理解。人工智能的目标是使计算机能够模仿人类的思维和行为。

人工智能的历史

人工智能的概念可以追溯到上世纪50年代。当时,科学家们开始研究如何使计算机能够模拟人类的智能。然而,由于计算能力和算法的限制,人工智能的发展进展缓慢。

直到最近几十年,随着计算机技术的飞速发展和大数据的兴起,人工智能才取得了巨大的突破。现在,人工智能已经成为许多领域的关键技术,如医疗保健、金融、交通和娱乐。

人工智能的应用

人工智能在各个领域都有广泛的应用。以下是一些常见的应用领域:

  • 自然语言处理:人工智能可以帮助计算机理解和处理人类语言,例如语音识别和机器翻译。

  • 机器学习:机器学习是人工智能的一个重要分支,它使用算法和统计模型来使计算机能够从数据中学习和改进性能。

  • 计算机视觉:计算机视觉是人工智能的另一个重要领域,它使计算机能够理解和解释图像和视频。

  • 智能机器人:人工智能可以使机器人具备感知、决策和执行任务的能力,从而在各种环境中执行复杂的任务。

  • 自动驾驶:自动驾驶技术利用人工智能和传感器来使汽车能够自动驾驶,提高交通安全和效率。

这些只是人工智能应用的一小部分,随着技术的不断发展,人工智能的应用领域将会越来越广泛。

结论

人工智能是一项引人注目的技术,它正在改变我们的生活和工作方式。随着技术的不断进步,人工智能将在各个领域发挥越来越重要的作用。我们期待着未来人工智能的发展,以及它给我们带来的更多创新和便利。

参考文献: [20] Smith, J. et al. (2018). Artificial Intelligence: A Comprehensive Guide. Journal of AI Research, 25(3), 123-145.