You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: articles/cosmos-db/mongodb/vcore/AI-ad-gen.md
+15-15Lines changed: 15 additions & 15 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -40,7 +40,7 @@ In this guide, we demonstrate how to create dynamic advertising content that res
40
40
## Running the Script
41
41
Before we dive into the exciting part of generating AI-enhanced advertisements, we need to set up our environment. This setup involves installing the necessary packages to ensure our script runs smoothly. Here’s a step-by-step guide to get everything ready.
42
42
43
-
### Install Necessary Packages
43
+
### 1.1 Install Necessary Packages
44
44
45
45
Firstly, we need to install a few Python packages. Open your terminal and run the following commands:
46
46
@@ -56,7 +56,7 @@ Firstly, we need to install a few Python packages. Open your terminal and run th
56
56
pip show openai
57
57
```
58
58
59
-
### Setting Up the OpenAI and Azure Client
59
+
### 1.2 Setting Up the OpenAI and Azure Client
60
60
After installing the necessary packages, the next step involves setting up our OpenAI and Azure clients for the script, which is crucial for authenticating our requests to the OpenAI API and Azure services.
## 2. Creating Embeddings and Setting up Cosmos DB
88
88
89
89
After setting up our environment and OpenAI client, we move to the core part of our AI-enhanced advertisement generation project. The following code creates vector embeddings from text descriptions of products and sets up our database in Azure Cosmos DB for MongoDB vCore to store and search these embeddings.
90
90
91
-
### Create Embeddings
91
+
### 2.1 Create Embeddings
92
92
93
93
To generate compelling advertisements, we first need to understand the items in our inventory. We do this by creating vector embeddings from descriptions of our items, which allows us to capture their semantic meaning in a form that machines can understand and process. Here's how you can create vector embeddings for an item description using Azure OpenAI:
94
94
@@ -113,10 +113,10 @@ if embeddings is not None:
113
113
114
114
The function takes a text input — like a product description — and uses the `client.embeddings.create` method from the OpenAI API to generate a vector embedding for that text. We're using the `text-embedding-ada-002` model here, but you can choose other models based on your requirements. If the process is successful, it prints the generated embeddings; otherwise, it handles exceptions by printing an error message.
115
115
116
-
## Connect and setup Cosmos DB for MongoDB vCore
116
+
## 3. Connect and setup Cosmos DB for MongoDB vCore
117
117
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.
118
118
119
-
### Set up the connection
119
+
### 3.1 Set up the connection
120
120
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:
Replace `<USERNAME>`, `<PASSWORD>`, and `<VCORE_CLUSTER_NAME>` with your actual MongoDB username, password, and vCore cluster name, respectively.
130
130
131
-
## Setting Up the Database and Vector Index in Cosmos DB
131
+
## 4. Setting Up the Database and Vector Index in Cosmos DB
132
132
133
133
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.
134
134
135
-
### Set Up the Database and Collection
135
+
### 4.1 Set Up the Database and Collection
136
136
137
137
First, we create a database and a collection within our Cosmos DB instance. Here’s how:
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.
156
156
157
157
### IVF
@@ -209,7 +209,7 @@ db.command(
209
209
> [!NOTE]
210
210
> HNSW indexing is only available on M40 cluster tiers and higher.
211
211
212
-
## Insert data to the collection
212
+
## 5. Insert data to the collection
213
213
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.
214
214
215
215
Download the [shoes_with_vectors.json](https://github.com/jayanta-mondal/ignite-demo/blob/main/data/shoes_with_vectors.json) file from the GitHub repository and store it in a `data` directory within your project folder.
@@ -224,10 +224,10 @@ result = collection.insert_many(data)
224
224
print(f"Number of data points added: {len(result.inserted_ids)}")
225
225
```
226
226
227
-
## Vector Search in Cosmos DB for MongoDB vCore
227
+
## 6. Vector Search in Cosmos DB for MongoDB vCore
228
228
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.
229
229
230
-
### Conducting a Vector Search
230
+
### 6.1 Conducting a Vector Search
231
231
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.
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.
To make our advertisement generation interactive, we employ Gradio, a Python library for creating simple web UIs. We define a UI that allows users to input ad topics and then dynamically generates and displays the resulting advertisement.
0 commit comments