Skip to content

Commit 87f1cd6

Browse files
committed
Update AI-ad-gen.md
1 parent de264dd commit 87f1cd6

File tree

1 file changed

+10
-12
lines changed

1 file changed

+10
-12
lines changed

articles/cosmos-db/mongodb/vcore/AI-ad-gen.md

Lines changed: 10 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ In this guide, we demonstrate how to create dynamic advertising content that res
2626
> [!VIDEO https://www.youtube.com/live/MLY5Pc_tSXw?si=fQmAuQcZkVauhmu-&t=1078]
2727
2828
## Prerequisites
29-
- Azure OpenAI: Let's setup the Azure OpenAI resource. Currently, access to this service is granted only by application. You can apply for access to Azure OpenAI by completing the form at https://aka.ms/oai/access. Once you have access, complete the following steps:
29+
- Azure OpenAI: Let's setup the Azure OpenAI resource. Access to this service is currently available by application only. You can apply for access to Azure OpenAI by completing the form at https://aka.ms/oai/access. Once you have access, complete the following steps:
3030
- Create an Azure OpenAI resource following this [quickstart](../../../ai-services/openai/how-to/create-resource.md?pivots=web-portal).
3131
- Deploy a `completions` and an `embeddings` model
3232
- For more information on `completions`, go [here](../../../ai-services/openai/how-to/completions.md).
@@ -121,7 +121,7 @@ The function takes a text input — like a product description — and uses the
121121
With our embeddings ready, the next step is to store and index them in a database that supports vector similarity search. Azure Cosmos DB for MongoDB vCore is a perfect fit for this task.
122122

123123
### Set up the connection
124-
To connect to Cosmos DB, we use the pymongo library, which allows us to interact with MongoDB easily. Below is the code snippet to establish a connection with our Cosmos DB instance:
124+
To connect to Cosmos DB, we use the pymongo library, which allows us to interact with MongoDB easily. The following code snippet establishes a connection with our Cosmos DB for MongoDB vCore instance:
125125
```python
126126
import pymongo
127127

@@ -132,15 +132,13 @@ mongo_client = pymongo.MongoClient(mongo_conn)
132132

133133
Replace `<USERNAME>`, `<PASSWORD>`, and `<VCORE_CLUSTER_NAME>` with your actual MongoDB username, password, and vCore cluster name, respectively.
134134

135-
By completing these steps, you've successfully created vector embeddings from your inventory descriptions and set up a connection to Azure Cosmos DB. These foundational elements enable us to move on to storing these embeddings, performing similarity searches, and generating AI-enhanced advertisements.
136-
137135
## Setting Up the Database and Vector Index in Cosmos DB
138136

139137
Once you've established a connection to Azure Cosmos DB, the next steps involve setting up your database and collection, and then creating a vector index to enable efficient vector similarity searches. Let's walk through these steps.
140138

141139
### Set Up the Database and Collection
142140

143-
First, we create a database and a collection within our Cosmos DB instance. If they already exist, we'll use them as they are. Here’s how:
141+
First, we create a database and a collection within our Cosmos DB instance. Here’s how:
144142
```python
145143
DATABASE_NAME = "AdgenDatabase"
146144
COLLECTION_NAME = "AdgenCollection"
@@ -158,10 +156,10 @@ else:
158156
```
159157

160158
### Create the vector index
161-
To perform efficient vector similarity searches within our collection, we need to create a vector index. Cosmos DB supports different types of vector indexes, and here we'll discuss two: IVF and HNSW.
159+
To perform efficient vector similarity searches within our collection, we need to create a vector index. Cosmos DB supports different types of vector indexes, and here we discuss two: IVF and HNSW.
162160

163161
### IVF
164-
IVF is the default vector indexing algorithm, which works on all cluster tiers. It's an approximate nearest neighbors (ANN) approach that uses clustering to speeding up the search for similar vectors in a dataset. To create an IVF index, use the following command:
162+
IVF stands for Inverted File Index, is the default vector indexing algorithm, which works on all cluster tiers. It's an approximate nearest neighbors (ANN) approach that uses clustering to speeding up the search for similar vectors in a dataset. To create an IVF index, use the following command:
165163

166164
```
167165
db.command({
@@ -188,9 +186,7 @@ db.command({
188186
189187
### HNSW
190188

191-
HNSW stands for Hierarchical Navigable Small World, a graph-based data structure that partitions vectors into clusters and subclusters. With HNSW, you can perform fast approximate nearest neighbor search at higher speeds with greater accuracy. HNSW is an approximate (ANN) method.
192-
193-
Note that HNSW indexing is only available on M40 cluster tiers and higher. Here's how to set it up:
189+
HNSW stands for Hierarchical Navigable Small World, a graph-based data structure that partitions vectors into clusters and subclusters. With HNSW, you can perform fast approximate nearest neighbor search at higher speeds with greater accuracy. HNSW is an approximate (ANN) method. Here's how to set it up:
194190

195191
```
196192
db.command(
@@ -214,6 +210,8 @@ db.command(
214210
}
215211
)
216212
```
213+
> [!NOTE]
214+
> HNSW indexing is only available on M40 cluster tiers and higher.
217215
218216
## Insert data to the collection
219217
Now insert the inventory data, which includes descriptions and their corresponding vector embeddings, into the newly created collection. To insert data into our collection, we use the `insert_many()` method provided by the `pymongo` library. The method allows us to insert multiple documents into the collection at once. Our data is stored in a JSON file, which we'll load and then insert into the database.
@@ -231,7 +229,7 @@ print(f"Number of data points added: {len(result.inserted_ids)}")
231229
```
232230

233231
## Vector Search in Cosmos DB for MongoDB vCore
234-
With our data successfully uploaded, we can now leverage the power of vector search to find the most relevant items based on a query. The vector index we created earlier enables us to perform semantic searches within our dataset.
232+
With our data successfully uploaded, we can now apply the power of vector search to find the most relevant items based on a query. The vector index we created earlier enables us to perform semantic searches within our dataset.
235233

236234
### Conducting a Vector Search
237235
To perform a vector search, we define a function `vector_search` that takes a query and the number of results to return. The function generates a vector for the query using the `generate_embeddings` function we defined earlier, then uses Cosmos DB's `$search` functionality to find the closest matching items based on their vector embeddings.
@@ -277,7 +275,7 @@ for result in results:
277275

278276
## Generating Ad content with GPT-4 and DALL.E
279277

280-
We combine all developed components to craft compelling ads, employing OpenAI's GPT-4 for text and DALL·E 3 for images. Together with vector search results, they form a complete ad. We also introduce Heelie, our intelligent assistant, tasked with creating engaging ad taglines. Through the upcoming code, you'll see Heelie in action, enhancing our ad creation process.
278+
We combine all developed components to craft compelling ads, employing OpenAI's GPT-4 for text and DALL·E 3 for images. Together with vector search results, they form a complete ad. We also introduce Heelie, our intelligent assistant, tasked with creating engaging ad taglines. Through the upcoming code, you see Heelie in action, enhancing our ad creation process.
281279

282280
```python
283281
from openai import OpenAI

0 commit comments

Comments
 (0)