# Apply the embedding function with a progress bar
tqdm.pandas(desc="Generating embeddings")
dataset_df["embedding"] = dataset_df[
    "combined_attributes"
].progress_apply(get_embedding)

print(f"We just computed {len(dataset_df['embedding'])} embeddings.")
```

We just computed 63 embeddings.

```python
dataset_df.head()
```

<div>
  <table border="1" class="dataframe fern-table">
    <thead>
      <tr>
        <th />

        <th>
          recent_news
        </th>

        <th>
          reports
        </th>

        <th>
          company
        </th>

        <th>
          ticker
        </th>

        <th>
          key_metrics
        </th>

        <th>
          sector
        </th>

        <th>
          combined_attributes
        </th>

        <th>
          embedding
        </th>
      </tr>
    </thead>

    <tbody>
      <tr>
        <th>
          0
        </th>

        <td>
          \[{'date': '2024-06-09', 'headline': 'CyberDefe...
        </td>

        <td>
          \[{'author': 'Taylor Smith, Technology Sector L...
        </td>

        <td>
          CyberDefense Dynamics
        </td>

        <td>
          CDDY
        </td>

        <td>
          \{'52_week_range': {'high': 387.3, 'low': 41.63...
        </td>

        <td>
          Information Technology
        </td>

        <td>
          CyberDefense Dynamics Information Technology 2...
        </td>

        <td>
          \[0.01210022, -0.03466797, -0.017562866, -0.025...
        </td>
      </tr>

      <tr>
        <th>
          1
        </th>

        <td>
          \[{'date': '2024-07-04', 'headline': 'CloudComp...
        </td>

        <td>
          \[{'author': 'Casey Jones, Chief Market Strateg...
        </td>

        <td>
          CloudCompute Pro
        </td>

        <td>
          CCPR
        </td>

        <td>
          \{'52_week_range': {'high': 524.23, 'low': 171....
        </td>

        <td>
          Information Technology
        </td>

        <td>
          CloudCompute Pro Information Technology 2023 C...
        </td>

        <td>
          \[-0.058563232, -0.06323242, -0.037139893, -0.0...
        </td>
      </tr>

      <tr>
        <th>
          2
        </th>

        <td>
          \[{'date': '2024-06-27', 'headline': 'VirtualRe...
        </td>

        <td>
          \[{'author': 'Sam Brown, Head of Equity Researc...
        </td>

        <td>
          VirtualReality Systems
        </td>

        <td>
          VRSY
        </td>

        <td>
          \{'52_week_range': {'high': 530.59, 'low': 56.4...
        </td>

        <td>
          Information Technology
        </td>

        <td>
          VirtualReality Systems Information Technology ...
        </td>

        <td>
          \[0.024154663, -0.022872925, -0.01751709, -0.05...
        </td>
      </tr>

      <tr>
        <th>
          3
        </th>

        <td>
          \[{'date': '2024-07-06', 'headline': 'BioTech I...
        </td>

        <td>
          \[{'author': 'Riley Smith, Senior Tech Analyst'...
        </td>

        <td>
          BioTech Innovations
        </td>

        <td>
          BTCI
        </td>

        <td>
          \{'52_week_range': {'high': 366.55, 'low': 124....
        </td>

        <td>
          Information Technology
        </td>

        <td>
          BioTech Innovations Information Technology 202...
        </td>

        <td>
          \[0.020736694, -0.041046143, -0.0029773712, -0....
        </td>
      </tr>

      <tr>
        <th>
          4
        </th>

        <td>
          \[{'date': '2024-06-26', 'headline': 'QuantumCo...
        </td>

        <td>
          \[{'author': 'Riley Garcia, Senior Tech Analyst...
        </td>

        <td>
          QuantumComputing Inc
        </td>

        <td>
          QCMP
        </td>

        <td>
          \{'52_week_range': {'high': 231.91, 'low': 159....
        </td>

        <td>
          Information Technology
        </td>

        <td>
          QuantumComputing Inc Information Technology 20...
        </td>

        <td>
          \[-0.009757996, -0.04815674, 0.039611816, 0.023...
        </td>
      </tr>
    </tbody>
  </table>
</div>

## Step 4: MongoDB Vector Database and Connection Setup

MongoDB acts as both an operational and a vector database for the RAG system.
MongoDB Atlas specifically provides a database solution that efficiently stores, queries and retrieves vector embeddings.

Creating a database and collection within MongoDB is made simple with MongoDB Atlas.

1. First, register for a [MongoDB Atlas account](https://www.mongodb.com/cloud/atlas/register). For existing users, sign into MongoDB Atlas.
2. [Follow the instructions](https://www.mongodb.com/docs/atlas/tutorial/deploy-free-tier-cluster/). Select Atlas UI as the procedure to deploy your first cluster.
3. Create the database: `asset_management_use_case`.
4. Within the database ` asset_management_use_case`, create the collection `market_reports`.
5. Create a [vector search index](https://www.mongodb.com/docs/atlas/atlas-vector-search/create-index/#procedure/) named vector\_index for the ‘listings\_reviews’ collection. This index enables the RAG application to retrieve records as additional context to supplement user queries via vector search. Below is the JSON definition of the data collection vector search index.

Your vector search index created on MongoDB Atlas should look like below:

```
{
  "fields": [
    {
      "numDimensions": 1024,
      "path": "embedding",
      "similarity": "cosine",
      "type": "vector"
    }
  ]
}

```

Follow MongoDB’s [steps to get the connection](https://www.mongodb.com/docs/manual/reference/connection-string/) string from the Atlas UI. After setting up the database and obtaining the Atlas cluster connection URI, securely store the URI within your development environment.

```python
import os

os.environ["MONGO_URI"] = ""
```

```python
import pymongo


def get_mongo_client(mongo_uri):
    """Establish and validate connection to the MongoDB."""

    client = pymongo.MongoClient(
        mongo_uri, appname="devrel.showcase.rag.cohere_mongodb.python"
    )

    # Validate the connection
    ping_result = client.admin.command("ping")
    if ping_result.get("ok") == 1.0:
        # Connection successful
        print("Connection to MongoDB successful")
        return client
    else:
        print("Connection to MongoDB failed")
    return None


MONGO_URI = os.environ["MONGO_URI"]

if not MONGO_URI:
    print("MONGO_URI not set in environment variables")

mongo_client = get_mongo_client(MONGO_URI)

DB_NAME = "asset_management_use_case"
COLLECTION_NAME = "market_reports"

db = mongo_client.get_database(DB_NAME)
collection = db.get_collection(COLLECTION_NAME)
```

Connection to MongoDB successful

```python