autogen_ext.tools.azure#
- pydantic 模型 AzureAISearchConfig[source]#
基类:
BaseModel
Azure AI 搜索工具的配置。
此类定义了
AzureAISearchTool
的配置参数。它提供了自定义搜索行为的选项,包括查询类型、字段选择、身份验证、重试策略和缓存策略。注意
此类需要
autogen-ext
包的azure
额外依赖。pip install -U "autogen-ext[azure]"
示例
from azure.core.credentials import AzureKeyCredential from autogen_ext.tools.azure import AzureAISearchConfig config = AzureAISearchConfig( name="doc_search", endpoint="https://my-search.search.windows.net", index_name="my-index", credential=AzureKeyCredential("<your-key>"), query_type="vector", vector_fields=["embedding"], )
- 有关更多详细信息,请参见
- 参数:
name (str) – 工具实例的名称,用于在代理的工具包中标识它。
description (Optional[str]) – 此工具的功能以及使用方法的易于理解的描述。
endpoint (str) – Azure AI 搜索服务的完整 URL,格式为“https://<service-name>.search.windows.net”。
index_name (str) – Azure AI 搜索服务中目标搜索索引的名称。索引必须预先创建并正确配置。
api_version (str) – 要使用的 Azure AI 搜索 REST API 版本。默认为“2023-11-01”。只有在需要来自不同 API 版本的特定功能时才更改。
credential (Union[AzureKeyCredential, TokenCredential]) – Azure 身份验证凭据:- AzureKeyCredential:用于 API 密钥身份验证(管理/查询密钥)- TokenCredential:用于 Azure AD 身份验证(例如,DefaultAzureCredential)
query_type (Literal["keyword", "fulltext", "vector", "semantic"]) – 要使用的搜索查询模式:- “keyword”:基本关键字搜索(默认)- “fulltext”:完整的 Lucene 查询语法- “vector”:向量相似度搜索- “semantic”:使用语义配置的语义搜索
search_fields (Optional[List[str]]) – 要在其中搜索的索引字段列表。如果未指定,则搜索所有可搜索字段。示例:[“title”, “content”]。
select_fields (Optional[List[str]]) – 要在搜索结果中返回的字段。如果未指定,则返回所有字段。用于优化响应大小。
vector_fields (Optional[List[str]]) – 向量搜索的向量字段名称。必须在搜索索引中配置为向量字段。向量搜索必需。
top (Optional[int]) – 要在搜索结果中返回的最大文档数。有助于控制响应大小和处理时间。
retry_enabled (bool) – 是否为瞬态错误启用重试策略。默认为 True。
retry_max_attempts (Optional[int]) – 失败请求的最大重试次数。默认为 3。
retry_mode (Literal["fixed", "exponential"]) – 重试退避策略:固定或指数。默认为“exponential”。
enable_caching (bool) – 是否启用搜索结果的客户端缓存。默认为 False。
cache_ttl_seconds (int) – 缓存的搜索结果的生存时间(以秒为单位)。默认为 300(5 分钟)。
filter (Optional[str]) – 用于优化搜索结果的 OData 筛选表达式。
显示 JSON 模式
{ "title": "AzureAISearchConfig", "description": "Configuration for Azure AI Search tool.\n\nThis class defines the configuration parameters for :class:`AzureAISearchTool`.\nIt provides options for customizing search behavior including query types,\nfield selection, authentication, retry policies, and caching strategies.\n\n.. note::\n\n This class requires the :code:`azure` extra for the :code:`autogen-ext` package.\n\n .. code-block:: bash\n\n pip install -U \"autogen-ext[azure]\"\n\nExample:\n .. code-block:: python\n\n from azure.core.credentials import AzureKeyCredential\n from autogen_ext.tools.azure import AzureAISearchConfig\n\n config = AzureAISearchConfig(\n name=\"doc_search\",\n endpoint=\"https://my-search.search.windows.net\",\n index_name=\"my-index\",\n credential=AzureKeyCredential(\"<your-key>\"),\n query_type=\"vector\",\n vector_fields=[\"embedding\"],\n )\n\nFor more details, see:\n * `Azure AI Search Overview <https://learn.microsoft.com/azure/search/search-what-is-azure-search>`_\n * `Vector Search <https://learn.microsoft.com/azure/search/vector-search-overview>`_\n\nArgs:\n name (str): Name for the tool instance, used to identify it in the agent's toolkit.\n description (Optional[str]): Human-readable description of what this tool does and how to use it.\n endpoint (str): The full URL of your Azure AI Search service, in the format\n 'https://<service-name>.search.windows.net'.\n index_name (str): Name of the target search index in your Azure AI Search service.\n The index must be pre-created and properly configured.\n api_version (str): Azure AI Search REST API version to use. Defaults to '2023-11-01'.\n Only change if you need specific features from a different API version.\n credential (Union[AzureKeyCredential, TokenCredential]): Azure authentication credential:\n - AzureKeyCredential: For API key authentication (admin/query key)\n - TokenCredential: For Azure AD authentication (e.g., DefaultAzureCredential)\n query_type (Literal[\"keyword\", \"fulltext\", \"vector\", \"semantic\"]): The search query mode to use:\n - 'keyword': Basic keyword search (default)\n - 'fulltext': Full Lucene query syntax\n - 'vector': Vector similarity search\n - 'semantic': Semantic search using semantic configuration\n search_fields (Optional[List[str]]): List of index fields to search within. If not specified,\n searches all searchable fields. Example: ['title', 'content'].\n select_fields (Optional[List[str]]): Fields to return in search results. If not specified,\n returns all fields. Use to optimize response size.\n vector_fields (Optional[List[str]]): Vector field names for vector search. Must be configured\n in your search index as vector fields. Required for vector search.\n top (Optional[int]): Maximum number of documents to return in search results.\n Helps control response size and processing time.\n retry_enabled (bool): Whether to enable retry policy for transient errors. Defaults to True.\n retry_max_attempts (Optional[int]): Maximum number of retry attempts for failed requests. Defaults to 3.\n retry_mode (Literal[\"fixed\", \"exponential\"]): Retry backoff strategy: fixed or exponential. Defaults to \"exponential\".\n enable_caching (bool): Whether to enable client-side caching of search results. Defaults to False.\n cache_ttl_seconds (int): Time-to-live for cached search results in seconds. Defaults to 300 (5 minutes).\n filter (Optional[str]): OData filter expression to refine search results.", "type": "object", "properties": { "name": { "description": "The name of the tool", "title": "Name", "type": "string" }, "description": { "anyOf": [ { "type": "string" }, { "type": "null" } ], "default": null, "description": "A description of the tool", "title": "Description" }, "endpoint": { "description": "The endpoint URL for your Azure AI Search service", "title": "Endpoint", "type": "string" }, "index_name": { "description": "The name of the search index to query", "title": "Index Name", "type": "string" }, "api_version": { "default": "2023-11-01", "description": "API version to use", "title": "Api Version", "type": "string" }, "credential": { "anyOf": [], "description": "The credential to use for authentication", "title": "Credential" }, "query_type": { "default": "keyword", "description": "Type of query to perform (keyword for classic, fulltext for Lucene, vector for embedding, semantic for semantic/AI search)", "enum": [ "keyword", "fulltext", "vector", "semantic" ], "title": "Query Type", "type": "string" }, "search_fields": { "anyOf": [ { "items": { "type": "string" }, "type": "array" }, { "type": "null" } ], "default": null, "description": "Optional list of fields to search in", "title": "Search Fields" }, "select_fields": { "anyOf": [ { "items": { "type": "string" }, "type": "array" }, { "type": "null" } ], "default": null, "description": "Optional list of fields to return in results", "title": "Select Fields" }, "vector_fields": { "anyOf": [ { "items": { "type": "string" }, "type": "array" }, { "type": "null" } ], "default": null, "description": "Optional list of vector fields for vector search", "title": "Vector Fields" }, "top": { "anyOf": [ { "type": "integer" }, { "type": "null" } ], "default": null, "description": "Optional number of results to return", "title": "Top" }, "filter": { "anyOf": [ { "type": "string" }, { "type": "null" } ], "default": null, "description": "Optional OData filter expression to refine search results", "title": "Filter" }, "retry_enabled": { "default": true, "description": "Whether to enable retry policy for transient errors", "title": "Retry Enabled", "type": "boolean" }, "retry_max_attempts": { "anyOf": [ { "type": "integer" }, { "type": "null" } ], "default": 3, "description": "Maximum number of retry attempts for failed requests", "title": "Retry Max Attempts" }, "retry_mode": { "default": "exponential", "description": "Retry backoff strategy: fixed or exponential", "enum": [ "fixed", "exponential" ], "title": "Retry Mode", "type": "string" }, "enable_caching": { "default": false, "description": "Whether to enable client-side caching of search results", "title": "Enable Caching", "type": "boolean" }, "cache_ttl_seconds": { "default": 300, "description": "Time-to-live for cached search results in seconds", "title": "Cache Ttl Seconds", "type": "integer" }, "embedding_provider": { "anyOf": [ { "type": "string" }, { "type": "null" } ], "default": null, "description": "Name of embedding provider to use (e.g., 'azure_openai', 'openai')", "title": "Embedding Provider" }, "embedding_model": { "anyOf": [ { "type": "string" }, { "type": "null" } ], "default": null, "description": "Model name to use for generating embeddings", "title": "Embedding Model" }, "embedding_dimension": { "anyOf": [ { "type": "integer" }, { "type": "null" } ], "default": null, "description": "Dimension of embedding vectors produced by the model", "title": "Embedding Dimension" } }, "required": [ "name", "endpoint", "index_name", "credential" ] }
- 字段:
api_version (str)
cache_ttl_seconds (int)
credential (azure.core.credentials.AzureKeyCredential | azure.core.credentials.TokenCredential)
description (str | None)
embedding_dimension (int | None)
embedding_model (str | None)
embedding_provider (str | None)
enable_caching (bool)
endpoint (str)
filter (str | None)
index_name (str)
name (str)
query_type (Literal['keyword', 'fulltext', 'vector', 'semantic'])
retry_enabled (bool)
retry_max_attempts (int | None)
retry_mode (Literal['fixed', 'exponential'])
search_fields (List[str] | None)
select_fields (List[str] | None)
top (int | None)
vector_fields (List[str] | None)
- field credential: AzureKeyCredential | TokenCredential [Required]#
用于身份验证的凭据
- field query_type: Literal['keyword', 'fulltext', 'vector', 'semantic'] = 'keyword'#
要执行的查询类型(keyword 代表经典查询,fulltext 代表 Lucene 查询,vector 代表嵌入查询,semantic 代表语义/AI 搜索)
- class AzureAISearchTool(name: str, endpoint: str, index_name: str, credential: AzureKeyCredential | TokenCredential | Dict[str, str], query_type: Literal['keyword', 'fulltext', 'vector', 'semantic'], search_fields: List[str] | None = None, select_fields: List[str] | None = None, vector_fields: List[str] | None = None, filter: str | None = None, top: int | None = 5, **kwargs: Any)[source]#
-
用于查询 Azure 搜索索引的 Azure AI 搜索工具。
该工具提供了一个简化的界面,用于使用各种搜索方法查询 Azure AI 搜索索引。 该工具支持四种主要的搜索类型
关键词搜索:使用 Azure 文本分析的传统文本搜索
全文搜索:使用特定于语言的分析器的增强型文本搜索
向量搜索:使用向量嵌入的语义相似性搜索
混合搜索:结合了全文和向量搜索,以获得全面的结果
你应该使用工厂方法来创建特定搜索类型的实例: - create_keyword_search() - create_full_text_search() - create_vector_search() - create_hybrid_search()
- classmethod create_full_text_search(name: str, endpoint: str, index_name: str, credential: AzureKeyCredential | TokenCredential | Dict[str, str], search_fields: List[str] | None = None, select_fields: List[str] | None = None, filter: str | None = None, top: int | None = 5, **kwargs: Any) AzureAISearchTool [source]#
工厂方法,用于创建全文搜索工具。
全文搜索使用高级文本分析(词干提取、词形还原等)来提供比基本关键字搜索更全面的文本匹配。
- 参数:
name (str) – 工具的名称
endpoint (str) – 你的 Azure AI 搜索服务的 URL
index_name (str) – 搜索索引的名称
credential (Union[AzureKeyCredential, TokenCredential, Dict[str, str]]) – 身份验证凭据
search_fields (Optional[List[str]]) – 要在其中搜索的字段
select_fields (Optional[List[str]]) – 要包含在结果中的字段
filter (Optional[str]) – 用于过滤结果的 OData 筛选表达式
top (Optional[int]) – 要返回的最大结果数
**kwargs (Any) – 其他配置选项
- 返回:
一个已初始化的全文搜索工具
- 使用示例
# type: ignore # Example of using full-text search with Azure AI Search from autogen_ext.tools.azure import AzureAISearchTool from azure.core.credentials import AzureKeyCredential # Create a full-text search tool full_text_search = AzureAISearchTool.create_full_text_search( name="document_search", endpoint="https://your-search-service.search.windows.net", index_name="your-index", credential=AzureKeyCredential("your-api-key"), search_fields=["title", "content"], select_fields=["title", "content", "category", "url"], top=10, ) # The search tool can be used with an Agent # assistant = Agent("assistant", tools=[full_text_search])
- classmethod create_hybrid_search(name: str, endpoint: str, index_name: str, credential: AzureKeyCredential | TokenCredential | Dict[str, str], vector_fields: List[str], search_fields: List[str] | None = None, select_fields: List[str] | None = None, filter: str | None = None, top: int | None = 5, **kwargs: Any) AzureAISearchTool [source]#
工厂方法,用于创建混合搜索工具(文本 + 向量)。
混合搜索将文本搜索(全文或语义)与向量相似性搜索相结合,以提供更全面的结果。 这是混合(文本 + 向量)搜索的推荐入口点。如果提供了 semantic_config_name,则 query_type 将为“semantic”,否则为“fulltext”。
- 参数:
name (str) – 工具的名称
endpoint (str) – 你的 Azure AI 搜索服务的 URL
index_name (str) – 搜索索引的名称
credential (Union[AzureKeyCredential, TokenCredential, Dict[str, str]]) – 身份验证凭据
vector_fields (List[str]) – 包含用于相似性搜索的向量嵌入的字段
search_fields (Optional[List[str]]) – 要在其中进行文本搜索的字段
select_fields (Optional[List[str]]) – 要包含在结果中的字段
filter (Optional[str]) – 用于过滤结果的 OData 筛选表达式
top (Optional[int]) – 要返回的最大结果数
**kwargs (Any) – 其他配置选项
- 返回:
一个已初始化的混合搜索工具
- 使用示例
# type: ignore # Example of using hybrid search with Azure AI Search from autogen_ext.tools.azure import AzureAISearchTool from azure.core.credentials import AzureKeyCredential # Create a hybrid search tool hybrid_search = AzureAISearchTool.create_hybrid_search( name="hybrid_search", endpoint="https://your-search-service.search.windows.net", index_name="your-index", credential=AzureKeyCredential("your-api-key"), vector_fields=["embedding_field"], search_fields=["title", "content"], select_fields=["title", "content", "url", "date"], top=10, ) # The search tool can be used with an Agent # assistant = Agent("researcher", tools=[hybrid_search])
警告
如果您设置
query_type="semantic"
,您还必须提供有效的semantic_config_name
。 如果没有,该工具将默认为配置名称"semantic"
。
- classmethod create_keyword_search(name: str, endpoint: str, index_name: str, credential: AzureKeyCredential | TokenCredential | Dict[str, str], search_fields: List[str] | None = None, select_fields: List[str] | None = None, filter: str | None = None, top: int | None = 5, **kwargs: Any) AzureAISearchTool [source]#
用于创建关键词搜索工具的工厂方法。
关键词搜索执行传统的基于文本的搜索,适合查找包含特定术语或与查询完全匹配的文档。
- 参数:
name (str) – 工具的名称
endpoint (str) – 你的 Azure AI 搜索服务的 URL
index_name (str) – 搜索索引的名称
credential (Union[AzureKeyCredential, TokenCredential, Dict[str, str]]) – 身份验证凭据
search_fields (Optional[List[str]]) – 要在其中进行文本搜索的字段
select_fields (Optional[List[str]]) – 要包含在结果中的字段
filter (Optional[str]) – 用于过滤结果的 OData 筛选表达式
top (Optional[int]) – 要返回的最大结果数
**kwargs (Any) – 其他配置选项
- 返回:
一个已初始化的关键词搜索工具
- 使用示例
# type: ignore # Example of using keyword search with Azure AI Search from autogen_ext.tools.azure import AzureAISearchTool from azure.core.credentials import AzureKeyCredential # Create a keyword search tool keyword_search = AzureAISearchTool.create_keyword_search( name="keyword_search", endpoint="https://your-service.search.windows.net", index_name="your-index", credential=AzureKeyCredential("your-api-key"), search_fields=["title", "content"], select_fields=["id", "title", "content", "category"], top=10, ) # The search tool can be used with an Agent # assistant = Agent("assistant", tools=[keyword_search])
- classmethod create_vector_search(name: str, endpoint: str, index_name: str, credential: AzureKeyCredential | TokenCredential | Dict[str, str], vector_fields: List[str], select_fields: List[str] | None = None, filter: str | None = None, top: int | None = 5, **kwargs: Any) AzureAISearchTool [source]#
用于创建向量搜索工具的工厂方法。
向量搜索使用嵌入向量来查找语义相似的内容,即使使用不同的术语,也能发现相关信息。
- 参数:
name (str) – 工具的名称
endpoint (str) – 你的 Azure AI 搜索服务的 URL
index_name (str) – 搜索索引的名称
credential (Union[AzureKeyCredential, TokenCredential, Dict[str, str]]) – 身份验证凭据
vector_fields (List[str]) – 包含用于相似性搜索的向量嵌入的字段
select_fields (Optional[List[str]]) – 要包含在结果中的字段
filter (Optional[str]) – 用于过滤结果的 OData 筛选表达式
top (Optional[int]) – 要返回的最大结果数
**kwargs (Any) – 其他配置选项
- 返回:
一个已初始化的向量搜索工具
- 使用示例
# type: ignore # Example of using vector search with Azure AI Search from autogen_ext.tools.azure import AzureAISearchTool from azure.core.credentials import AzureKeyCredential # Create a vector search tool vector_search = AzureAISearchTool.create_vector_search( name="vector_search", endpoint="https://your-search-service.search.windows.net", index_name="your-index", credential=AzureKeyCredential("your-api-key"), vector_fields=["embedding"], select_fields=["title", "content", "url"], top=5, ) # The search tool can be used with an Agent # assistant = Agent("assistant", tools=[vector_search])
- classmethod load_component(model: ComponentModel | Dict[str, Any], expected: None = None) AzureAISearchTool [source]#
- classmethod load_component(model: ComponentModel | Dict[str, Any], expected: Type[ExpectedType]) ExpectedType
从组件模型加载组件。
- 参数:
model – 组件模型或包含配置的字典
expected – 可选的预期返回类型
- 返回:
已初始化的 AzureAISearchTool 实例
- class BaseAzureAISearchTool(name: str, endpoint: str, index_name: str, credential: AzureKeyCredential | TokenCredential | Dict[str, str], description: str | None = None, api_version: str = '2023-11-01', query_type: Literal['keyword', 'fulltext', 'vector', 'semantic'] = 'keyword', search_fields: List[str] | None = None, select_fields: List[str] | None = None, vector_fields: List[str] | None = None, top: int | None = None, filter: str | None = None, semantic_config_name: str | None = None, enable_caching: bool = False, cache_ttl_seconds: int = 300)[source]#
基类:
BaseTool
[SearchQuery
,SearchResults
],ABC
Azure AI 搜索工具的抽象基类。
此类定义了所有 Azure AI 搜索工具的通用接口和功能。它处理配置管理、客户端初始化以及子类必须实现的抽象方法。
- search_config#
搜索服务的配置参数。
注意
这是一个抽象基类,不应直接实例化。 使用 AzureAISearchTool 中的具体实现或工厂方法。
- dump_component() ComponentModel [source]#
将工具序列化为组件模型。
- 返回:
ComponentModel – 工具的序列化表示
- classmethod load_component(model: ComponentModel | Dict[str, Any], expected: None = None) BaseAzureAISearchTool [source]#
- classmethod load_component(model: ComponentModel | Dict[str, Any], expected: Type[ExpectedType]) ExpectedType
从组件模型加载工具。
- 参数:
model (Union[ComponentModel, Dict[str, Any]]) – 组件配置。
expected (Optional[Type[ExpectedType]]) – 用于反序列化的可选组件类。
- 返回:
Union[BaseAzureAISearchTool, ExpectedType] – 工具的一个实例。
- 引发:
ValueError – 如果组件配置无效。
- return_value_as_string(value: SearchResults) str [source]#
将搜索结果转换为字符串表示形式。
此方法用于格式化搜索结果,使其适合向用户显示或供语言模型使用。
- 参数:
value (List[SearchResult]) – 要转换的搜索结果。
- 返回:
str – 搜索结果的格式化字符串表示形式。
- async run(args: str | Dict[str, Any] | SearchQuery, cancellation_token: CancellationToken | None = None) SearchResults [source]#
针对 Azure AI Search 索引执行搜索。
- 参数:
args – 搜索查询文本或 SearchQuery 对象
cancellation_token – 用于取消操作的可选令牌
- 返回:
搜索结果
- property schema: ToolSchema#
返回工具的 schema。
- pydantic model SearchQuery[source]#
基类:
BaseModel
搜索查询参数。
这种简化的界面只需要一个搜索查询字符串。所有其他参数(top、filters、vector fields 等)都在工具创建期间指定,而不是在查询时指定,从而使语言模型更容易生成结构化输出。
- 参数:
query (str) – 搜索查询文本。
显示 JSON 模式
{ "title": "SearchQuery", "description": "Search query parameters.\n\nThis simplified interface only requires a search query string.\nAll other parameters (top, filters, vector fields, etc.) are specified during tool creation\nrather than at query time, making it easier for language models to generate structured output.\n\nArgs:\n query (str): The search query text.", "type": "object", "properties": { "query": { "description": "Search query text", "title": "Query", "type": "string" } }, "required": [ "query" ] }
- 字段:
query (str)
- pydantic model SearchResult[source]#
基类:
BaseModel
搜索结果。
显示 JSON 模式
{ "title": "SearchResult", "description": "Search result.\n\nArgs:\n score (float): The search score.\n content (Dict[str, Any]): The document content.\n metadata (Dict[str, Any]): Additional metadata about the document.", "type": "object", "properties": { "score": { "description": "The search score", "title": "Score", "type": "number" }, "content": { "description": "The document content", "title": "Content", "type": "object" }, "metadata": { "description": "Additional metadata about the document", "title": "Metadata", "type": "object" } }, "required": [ "score", "content", "metadata" ] }
- 字段:
content (Dict[str, Any])
metadata (Dict[str, Any])
score (float)
- pydantic model SearchResults[source]#
基类:
BaseModel
搜索结果的容器。
- 参数:
results (List[SearchResult]) – 搜索结果列表。
显示 JSON 模式
{ "title": "SearchResults", "description": "Container for search results.\n\nArgs:\n results (List[SearchResult]): List of search results.", "type": "object", "properties": { "results": { "description": "List of search results", "items": { "$ref": "#/$defs/SearchResult" }, "title": "Results", "type": "array" } }, "$defs": { "SearchResult": { "description": "Search result.\n\nArgs:\n score (float): The search score.\n content (Dict[str, Any]): The document content.\n metadata (Dict[str, Any]): Additional metadata about the document.", "properties": { "score": { "description": "The search score", "title": "Score", "type": "number" }, "content": { "description": "The document content", "title": "Content", "type": "object" }, "metadata": { "description": "Additional metadata about the document", "title": "Metadata", "type": "object" } }, "required": [ "score", "content", "metadata" ], "title": "SearchResult", "type": "object" } }, "required": [ "results" ] }
- 字段:
results (List[autogen_ext.tools.azure._ai_search.SearchResult])
- field results: List[SearchResult] [Required]#
搜索结果列表