|
| 1 | +--- |
| 2 | +title: Azure OpenAI on your Mongo DB Atlas data Python & REST API reference |
| 3 | +titleSuffix: Azure OpenAI |
| 4 | +description: Learn how to use Azure OpenAI on your Mongo DB Atlas data with Python & REST API. |
| 5 | +manager: nitinme |
| 6 | +ms.service: azure-ai-openai |
| 7 | +ms.topic: conceptual |
| 8 | +ms.date: 10/25/2024 |
| 9 | +author: aahill |
| 10 | +ms.author: aahi |
| 11 | +recommendations: false |
| 12 | +ms.custom: devx-track-python |
| 13 | +--- |
| 14 | + |
| 15 | +# Data source - Mongo DB Atlas |
| 16 | + |
| 17 | +The configurable options of Mongo DB Atlas when using Azure OpenAI On Your Data. This data source is supported starting in API version `2024-08-01`. |
| 18 | + |
| 19 | + |
| 20 | +|Name | Type | Required | Description | |
| 21 | +|--- | --- | --- | --- | |
| 22 | +|`parameters`| [Parameters](#parameters)| True| The parameters to use when configuring Mongo DB Atlas.| |
| 23 | +| `type`| string| True | Must be `mongo_db`. | |
| 24 | + |
| 25 | +## Parameters |
| 26 | + |
| 27 | +|Name | Type | Required | Description | |
| 28 | +|--- | --- | --- | --- | |
| 29 | +| `authentication` | object | True | The [authentication options](#authentication) for Azure OpenAI On Your Data when using a username and a password. | |
| 30 | +| `app_name` | string | True | The name of the Mongo DB Atlas Application. | |
| 31 | +| `collection_name` | string | True | The name of the Mongo DB Atlas Collection. | |
| 32 | +| `database_name` | string | True | The name of the Mongo DB Atlas database. | |
| 33 | +| `endpoint` | string | True | The name of the Mongo DB Atlas cluster endpoint. | |
| 34 | +| `embedding_dependency` | One of [DeploymentNameVectorizationSource](#deployment-name-vectorization-source), [EndpointVectorizationSource](#endpoint-vectorization-source) | True | The embedding dependency for vector search.| |
| 35 | +| `fields_mapping` | object | True | [Settings](#field-mapping-options) to control how fields are processed when using a configured Mongo DB Atlas resource. | |
| 36 | +| `index_name` | string | True | The name of the Mongo DB Atlas index.| |
| 37 | +| `top_n_documents` | integer | False | The configured top number of documents to feature for the configured query.| |
| 38 | +| `max_search_queries` | integer | False | The max number of rewritten queries should be sent to search provider for one user message. If not specified, the system will decide the number of queries to send.| |
| 39 | +| `allow_partial_result` | boolean | False | If specified as true, the system will allow partial search results to be used and the request fails if all the queries fail. If not specified, or specified as false, the request will fail if any search query fails.| |
| 40 | +| `in_scope` | boolean | False | Whether queries should be restricted to use indexed data. | |
| 41 | +| `strictness` | integer | False | The configured strictness of the search relevance filtering, from 1 to 5. The higher the strictness, the higher precision but lower recall of the answer. | |
| 42 | +| `include_contexts` | array | False | The included properties of the output context. If not specified, the default value is `citations` and `intent`. Valid properties are `all_retrieved_documents`, `citations` and `intent`. | |
| 43 | + |
| 44 | +## Authentication |
| 45 | + |
| 46 | +The authentication options for Azure OpenAI On Your Data when using a username and a password. |
| 47 | + |
| 48 | +|Name | Type | Required | Description | |
| 49 | +|--- | --- | --- | --- | |
| 50 | +| `type` | string | True | Must be `username_and_password`. | |
| 51 | +| `username` | string | True | The username to use for authentication. | |
| 52 | +| `password` | string | True | The password to use for authentication. | |
| 53 | + |
| 54 | +## Deployment name vectorization source |
| 55 | + |
| 56 | +The details of the vectorization source, used by Azure OpenAI On Your Data when applying vector search. This vectorization source is based on an internal embeddings model deployment name in the same Azure OpenAI resource. This vectorization source enables you to use vector search without Azure OpenAI api-key and without Azure OpenAI public network access. |
| 57 | + |
| 58 | +|Name | Type | Required | Description | |
| 59 | +|--- | --- | --- | --- | |
| 60 | +| `deployment_name`|string|True|The embedding model deployment name within the same Azure OpenAI resource. | |
| 61 | +| `type`|string|True| Must be `deployment_name`.| |
| 62 | + |
| 63 | +## Endpoint vectorization source |
| 64 | + |
| 65 | +The details of the vectorization source, used by Azure OpenAI On Your Data when applying vector search. This vectorization source is based on the Azure OpenAI embedding API endpoint. |
| 66 | + |
| 67 | +|Name | Type | Required | Description | |
| 68 | +|--- | --- | --- | --- | |
| 69 | +| `endpoint`|string|True|Specifies the resource endpoint URL from which embeddings should be retrieved. It should be in the format of `https://{YOUR_RESOURCE_NAME}.openai.azure.com/openai/deployments/YOUR_DEPLOYMENT_NAME/embeddings`. The api-version query parameter isn't allowed.| |
| 70 | +| `authentication`| [ApiKeyAuthenticationOptions](#authentication)|True | Specifies the authentication options to use when retrieving embeddings from the specified endpoint.| |
| 71 | +| `type`|string|True| Must be `endpoint`.| |
| 72 | + |
| 73 | +## Field mapping options |
| 74 | + |
| 75 | +Optional settings to control how fields are processed when using a configured Mongo DB Atlas resource. |
| 76 | + |
| 77 | +|Name | Type | Required | Description | |
| 78 | +|--- | --- | --- | --- | |
| 79 | +| `content_fields`|string[] |True| The names of index fields that should be treated as content. | |
| 80 | +| `vector_fields`|string[] |True| The names of fields that represent vector data. | |
| 81 | +| `title_field`|string |False | The name of the index field to use as a title. | |
| 82 | +| `url_field`|string |False | The name of the index field to use as a URL. | |
| 83 | +| `filepath_field`|string |False | The name of the index field to use as a filepath. | |
| 84 | +| `content_fields_separator` | string | False | The separator pattern that content fields should use.| |
| 85 | + |
| 86 | +## Examples |
| 87 | + |
| 88 | + |
| 89 | +# [Python 1.x](#tab/python) |
| 90 | + |
| 91 | +Install the latest pip packages `openai`, `azure-identity`. |
| 92 | + |
| 93 | +```python |
| 94 | +import os |
| 95 | +from openai import AzureOpenAI |
| 96 | +from azure.identity import DefaultAzureCredential, get_bearer_token_provider |
| 97 | + |
| 98 | +endpoint = os.environ.get("AzureOpenAIEndpoint") |
| 99 | +deployment = os.environ.get("ChatCompletionsDeploymentName") |
| 100 | +index_name = os.environ.get("IndexName") |
| 101 | +key = os.environ.get("Key") |
| 102 | +embedding_name = os.environ.get("EmbeddingName") |
| 103 | +embedding_type = os.environ.get("EmbeddingType") |
| 104 | + |
| 105 | +# Additional variables for Mongo DB Atlas |
| 106 | +mongo_db_username = os.environ.get("MongoDBUsername") |
| 107 | +mongo_db_password = os.environ.get("MongoDBPassword") |
| 108 | +mongo_db_endpoint = os.environ.get("MongoDBEndpoint") |
| 109 | +mongo_db_app_name = os.environ.get("MongoDBAppName") |
| 110 | +mongo_db_database_name = os.environ.get("MongoDBName") |
| 111 | +mongo_db_collection = os.environ.get("MongoDBCollection") |
| 112 | +mongo_db_index = os.environ.get("MongoDBIndex") |
| 113 | + |
| 114 | +token_provider = get_bearer_token_provider( |
| 115 | + DefaultAzureCredential(), "https://cognitiveservices.azure.com/.default") |
| 116 | + |
| 117 | +client = AzureOpenAI( |
| 118 | + azure_endpoint=endpoint, |
| 119 | + azure_ad_token_provider=token_provider, |
| 120 | + api_version="2024-05-01-preview", |
| 121 | +) |
| 122 | + |
| 123 | +completion = client.chat.completions.create( |
| 124 | + model=deployment, |
| 125 | + messages=[ |
| 126 | + { |
| 127 | + "role": "user", |
| 128 | + "content": "Who is DRI?", |
| 129 | + }, |
| 130 | + ], |
| 131 | + extra_body={ |
| 132 | + "data_sources": [ |
| 133 | + { |
| 134 | + "type": "mongo_db", |
| 135 | + "parameters": { |
| 136 | + "authentication": { |
| 137 | + "type": "username_and_password", |
| 138 | + "username": mongo_db_username, |
| 139 | + "password": mongo_db_password |
| 140 | + }, |
| 141 | + "endpoint": mongo_db_endpoint, |
| 142 | + "app_name": mongo_db_app_name, |
| 143 | + "database_name": mongo_db_database_name, |
| 144 | + "collection_name": mongo_db_collection, |
| 145 | + "index_name": mongo_db_index, |
| 146 | + "embedding_dependency": { |
| 147 | + "type": embedding_type, |
| 148 | + "deployment_name": embedding_name |
| 149 | + }, |
| 150 | + "fields_mapping": { |
| 151 | + "content_fields": [ |
| 152 | + "content" |
| 153 | + ], |
| 154 | + "vector_fields": [ |
| 155 | + "contentvector" |
| 156 | + ] |
| 157 | + } |
| 158 | + } |
| 159 | + } |
| 160 | + ] |
| 161 | + } |
| 162 | +) |
| 163 | + |
| 164 | +print(completion.model_dump_json(indent=2)) |
| 165 | + |
| 166 | +``` |
| 167 | + |
| 168 | +# [REST](#tab/rest) |
| 169 | + |
| 170 | +```bash |
| 171 | +az rest --method POST \ |
| 172 | + --uri $AzureOpenAIEndpoint/openai/deployments/$ChatCompletionsDeploymentName/chat/completions?api-version=2024-05-01-preview \ |
| 173 | + --resource https://cognitiveservices.azure.com/ \ |
| 174 | + --body \ |
| 175 | +' |
| 176 | +{ |
| 177 | + "data_sources": [ |
| 178 | + { |
| 179 | + "type": "mongo_db", |
| 180 | + "parameters": { |
| 181 | + "authentication": { |
| 182 | + "type": "username_and_password", |
| 183 | + "username": "<username>", |
| 184 | + "password": "<password>" |
| 185 | + }, |
| 186 | + "endpoint": "<endpoint_name>", |
| 187 | + "app_name": "<application name>", |
| 188 | + "database_name": "sampledb", |
| 189 | + "collection_name": "samplecollection", |
| 190 | + "index_name": "sampleindex", |
| 191 | + "embedding_dependency": { |
| 192 | + "type": "deployment_name", |
| 193 | + "deployment_name": "{embedding deployment name}" |
| 194 | + }, |
| 195 | + "fields_mapping": { |
| 196 | + "content_fields": [ |
| 197 | + "content" |
| 198 | + ], |
| 199 | + "vector_fields": [ |
| 200 | + "contentvector" |
| 201 | + ] |
| 202 | + } |
| 203 | + } |
| 204 | + } |
| 205 | + ] |
| 206 | +} |
| 207 | +' |
| 208 | +``` |
| 209 | + |
| 210 | +--- |
0 commit comments