你当前正在访问 Microsoft Azure Global Edition 技术文档网站。 如果需要访问由世纪互联运营的 Microsoft Azure 中国技术文档网站,请访问 https://docs.azure.cn

数据源 - Azure Cosmos DB for MongoDB vCore

使用基于自有数据的 Azure OpenAI 时的 Azure Cosmos DB for MongoDB vCore 的可配置选项。 API 版本 2024-02-01 支持此数据源。

名称 类型​​ 必需 说明
parameters Parameters True 配置 Azure Cosmos DB for MongoDB vCore 时要使用的参数。
type string True 必须是 azure_cosmos_db

参数

客户 类型​​ 必需 说明
database_name 字符串 True 用于 Azure Cosmos DB 的 MongoDB vCore 数据库名称。
container_name string True Azure Cosmos DB 资源容器的名称。
index_name string True 用于 Azure Cosmos DB 的 MongoDB vCore 索引名称。
fields_mapping FieldsMappingOptions True 与搜索索引交互时要使用的自定义字段映射行为。
authentication ConnectionStringAuthenticationOptions True 访问定义的数据源时要使用的身份验证方法。
embedding_dependency DeploymentNameVectorizationSource之一,EndpointVectorizationSource True 矢量搜索的嵌入依赖项。
in_scope boolean False 是否应将查询限制为使用索引数据。 默认值为 True
role_information string False 为模型提供有关它应该如何运行以及在生成回复时应引用的任何上下文的说明。 你可以描述助手的个性,告诉它如何设置回复的格式。
strictness integer False 搜索相关性筛选的已配置严格性。 严格度越高,精准率越高,但回复的召回率越低。 默认值为 3
top_n_documents integer False 为配置的查询提供的已配置最多文档数。 默认值为 5

连接字符串验证选项

使用连接字符串时,基于自有数据的 Azure OpenAI 的验证选项。

名称 类型​​ 必需 说明
connection_string 字符串 True 用于身份验证的连接字符串。
type string True 必须是 connection_string

部署名称矢量化源

应用矢量搜索时基于自有数据的 Azure OpenAI 使用的矢量化源的详细信息。 此矢量化源基于同一 Azure OpenAI 资源中的内部嵌入模型部署名称。 此矢量化可以在没有 Azure OpenAI API 密钥和 Azure OpenAI 公用网络访问的情况下使用矢量搜索。

名称 类型​​ 必需 说明
deployment_name 字符串 True 同一 Azure OpenAI 资源中的嵌入模型部署名称。
type string True 必须是 deployment_name

终结点矢量化源

应用矢量搜索时基于自有数据的 Azure OpenAI 使用的矢量化源的详细信息。 此矢量化源基于 Azure OpenAI 嵌入 API 终结点。

名称 类型​​ 必需 说明
endpoint 字符串 True 指定应从中检索嵌入的资源终结点 URL。 其格式应为:https://{YOUR_RESOURCE_NAME}.openai.azure.com/openai/deployments/YOUR_DEPLOYMENT_NAME/embeddings。 不允许 api-version 查询参数。
authentication ApiKeyAuthenticationOptions True 指定从指定终结点检索嵌入时要使用的验证选项。
type string True 必须是 endpoint

API 密钥验证选项

使用 API 密钥时,基于自有数据的 Azure OpenAI 的验证选项。

名称 类型​​ 必需 说明
key 字符串 True 用于身份验证的 API 密钥。
type string True 必须是 api_key

字段映射选项

用于控制字段处理方式的设置。

名称 类型​​ 必需 说明
content_fields string[] True 应被视为内容的索引字段的名称。
vector_fields string[] True 表示矢量数据的字段的名称。
content_fields_separator string False 内容字段应使用的分隔符模式。 默认值为 \n
filepath_field string False 要用作文件路径的索引字段的名称。
title_field string False 要用作标题的索引字段的名称。
url_field string False 要用作 URL 的索引字段的名称。

示例

先决条件:

  • 配置用户到 Azure OpenAI 资源的角色分配。 所需角色:Cognitive Services OpenAI User
  • 安装 Az CLI 并运行 az login
  • 定义以下环境变量:AzureOpenAIEndpointChatCompletionsDeploymentNameConnectionStringDatabaseContainerIndexEmbeddingDeploymentName
export AzureOpenAIEndpoint=https://example.openai.azure.com/
export ChatCompletionsDeploymentName=turbo
export ConnectionString='mongodb+srv://username:***@example.mongocluster.cosmos.azure.com/?tls=true&authMechanism=SCRAM-SHA-256&retrywrites=false&maxIdleTimeMS=120000'
export Database=testdb
export Container=testcontainer
export Index=testindex
export EmbeddingDeploymentName=ada

安装最新的 pip 包 openaiazure-identity


import os
from openai import AzureOpenAI
from azure.identity import DefaultAzureCredential, get_bearer_token_provider

endpoint = os.environ.get("AzureOpenAIEndpoint")
deployment = os.environ.get("ChatCompletionsDeploymentName")
connection_string = os.environ.get("ConnectionString")
database = os.environ.get("Database")
container = os.environ.get("Container")
index = os.environ.get("Index")
embedding_deployment_name = os.environ.get("EmbeddingDeploymentName")

token_provider = get_bearer_token_provider(
    DefaultAzureCredential(), "https://cognitiveservices.azure.com/.default")

client = AzureOpenAI(
    azure_endpoint=endpoint,
    azure_ad_token_provider=token_provider,
    api_version="2024-02-01",
)

completion = client.chat.completions.create(
    model=deployment,
    messages=[
        {
            "role": "user",
            "content": "Who is DRI?",
        },
    ],
    extra_body={
        "data_sources": [
            {
                "type": "azure_cosmos_db",
                "parameters": {
                    "authentication": {
                        "type": "connection_string",
                        "connection_string": connection_string
                    },
                    "database_name": database,
                    "container_name": container,
                    "index_name": index,
                    "fields_mapping": {
                        "content_fields": [
                            "content"
                        ],
                        "vector_fields": [
                            "contentvector"
                        ]
                    },
                    "embedding_dependency": {
                        "type": "deployment_name",
                        "deployment_name": embedding_deployment_name
                    }
                }
            }
        ],
    }
)

print(completion.model_dump_json(indent=2))