create-embeddings-manual

For the complete MongoDB documentation index, see www.mongodb.com/docs/llms.txt

<!-- Tab options on this page. Append to the .md URL to filter: ?tabs=<id,...> select specific tabs (e.g. ?tabs=nodejs,shell) ?allTabs=true include every tab (no param) default: one tab per tabset

Available tabs: other tabs: cloud, local, maven, gradle, open-source, openai, voyage-ai -->

How to Create Vector Embeddings Manually

You can store [vector embeddings](https://www.mongodb.com/docs/vector-search/index.md#std-term-vector-embeddings alongside your other MongoDB data. These embeddings capture meaningful relationships in your data and allow you to perform semantic search and implement [RAG.](https://www.mongodb.com/docs/vector-search/tutorials/rag.md#std-label-avs-rag

Get Started

Use the following tutorial to learn how to create vector embeddings and query them using vector search. Specifically, you perform the following actions:

  1. Define a function that uses an [embedding model](https://www.mongodb.com/docs/vector-search/crud-embeddings/create-embeddings-manual.md#std-label-choose-embedding-method to generate vector embeddings.

Select whether you want to use a proprietary or open-source model. For state-of-the-art embeddings, use Voyage AI.

  1. Create embeddings from your data and store them in MongoDB.

Select whether you want to create embeddings from new data or from existing data that you already have in a MongoDB collection.

  1. Create embeddings from your search terms and run a vector search query.

For production applications, you typically write a script to generate vector embeddings. You can start with the sample code on this page and customize it for your use case.

Note:

MongoDB Vector Search Automated Embedding service is a one-click way to enable semantic search on the text data in your collection. With Automated Embedding, you don't need to generate, store, or manage vector embeddings yourself. Instead, MongoDB Vector Search automatically generates and manages vector embeddings for you. To learn more, see [Automated Embedding Overview.](https://www.mongodb.com/docs/vector-search/crud-embeddings/automated-embedding.md#std-label-avs-auto-embeddings

Work with a runnable version of this tutorial as a Python notebook.

Work with a runnable version of this tutorial as a Python notebook.

Work with a runnable version of this tutorial as a Python notebook.

Work with a runnable version of this tutorial as a Python notebook.

Work with a runnable version of this tutorial as a Python notebook.

Work with a runnable version of this tutorial as a Python notebook.

Prerequisites

To complete this tutorial, you must have the following:

  • One of the following MongoDB cluster types:
  • An Atlas clustert

  • A local Atlas deployment created using Python and Docker. Install atlas-local-lib-py (pip install atlas-local-lib-py) to programmatically create and manage local deployments. To learn more, see the atlas-local-lib-py repository.

  • A MongoDB Community or Enterprise cluster with [Search and Vector Search](https://www.mongodb.com/docs/search/self-managed/current/installation/linux.md#std-label-community-search-deploy installed.

  • An environment to run interactive Python notebooks such as VS Code or Colab.

  • A Voyage AI API (Application Programming Interface) key. To create an API (Application Programming Interface) key, see [Manage Voyage AI Model API Keys.](https://www.mongodb.com/docs/voyageai/management/api-keys.md#std-label-voyage-api-keys

  • One of the following MongoDB cluster types:

  • An Atlas clustert

  • A local Atlas deployment created using Python and Docker. Install atlas-local-lib-py (pip install atlas-local-lib-py) to programmatically create and manage local deployments. To learn more, see the atlas-local-lib-py repository.

  • A MongoDB Community or Enterprise cluster with [Search and Vector Search](https://www.mongodb.com/docs/search/self-managed/current/installation/linux.md#std-label-community-search-deploy installed.

  • An environment to run interactive Python notebooks such as VS Code or Colab.

  • A Voyage AI API (Application Programming Interface) key. To create an API (Application Programming Interface) key, see [Manage Voyage AI Model API Keys.](https://www.mongodb.com/docs/voyageai/management/api-keys.md#std-label-voyage-api-keys

  • One of the following MongoDB cluster types:

  • An Atlas clustert

  • A local Atlas deployment created using Python and Docker. Install atlas-local-lib-py (pip install atlas-local-lib-py) to programmatically create and manage local deployments. To learn more, see the atlas-local-lib-py repository.

  • A MongoDB Community or Enterprise cluster with [Search and Vector Search](https://www.mongodb.com/docs/search/self-managed/current/installation/linux.md#std-label-community-search-deploy installed.

  • A terminal and code editor to run your Node.js project.

  • npm and Node.js installed.

  • A Voyage AI API (Application Programming Interface) key. To create an API (Application Programming Interface) key, see [Manage Voyage AI Model API Keys.](https://www.mongodb.com/docs/voyageai/management/api-keys.md#std-label-voyage-api-keys

  • One of the following MongoDB cluster types:

  • An Atlas clustert

  • A local Atlas deployment created using Python and Docker. Install atlas-local-lib-py (pip install atlas-local-lib-py) to programmatically create and manage local deployments. To learn more, see the atlas-local-lib-py repository.

  • A MongoDB Community or Enterprise cluster with [Search and Vector Search](https://www.mongodb.com/docs/search/self-managed/current/installation/linux.md#std-label-community-search-deploy installed.

  • A terminal and code editor to run your Node.js project.

  • npm and Node.js installed.

  • A Voyage AI API (Application Programming Interface) key. To create an API (Application Programming Interface) key, see [Manage Voyage AI Model API Keys.](https://www.mongodb.com/docs/voyageai/management/api-keys.md#std-label-voyage-api-keys

  • One of the following MongoDB cluster types:

  • An Atlas clustert

  • A local Atlas deployment created using Python and Docker. Install atlas-local-lib-py (pip install atlas-local-lib-py) to programmatically create and manage local deployments. To learn more, see the atlas-local-lib-py repository.

  • A MongoDB Community or Enterprise cluster with [Search and Vector Search](https://www.mongodb.com/docs/search/self-managed/current/installation/linux.md#std-label-community-search-deploy installed.

  • A terminal and code editor to run your Go project.

  • Go installed.

  • A Voyage AI API (Application Programming Interface) key. To create an API (Application Programming Interface) key, see [Manage Voyage AI Model API Keys.](https://www.mongodb.com/docs/voyageai/management/api-keys.md#std-label-voyage-api-keys

  • One of the following MongoDB cluster types:

  • An Atlas clustert

  • A local Atlas deployment created using Python and Docker. Install atlas-local-lib-py (pip install atlas-local-lib-py) to programmatically create and manage local deployments. To learn more, see the atlas-local-lib-py repository.

  • A MongoDB Community or Enterprise cluster with [Search and Vector Search](https://www.mongodb.com/docs/search/self-managed/current/installation/linux.md#std-label-community-search-deploy installed.

  • A terminal and code editor to run your Go project.

  • Go installed.

  • A Voyage AI API (Application Programming Interface) key. To create an API (Application Programming Interface) key, see [Manage Voyage AI Model API Keys.](https://www.mongodb.com/docs/voyageai/management/api-keys.md#std-label-voyage-api-keys

  • One of the following MongoDB cluster types:

  • An Atlas clustert

  • A local Atlas deployment created using Python and Docker. Install atlas-local-lib-py (pip install atlas-local-lib-py) to programmatically create and manage local deployments. To learn more, see the atlas-local-lib-py repository.

  • A MongoDB Community or Enterprise cluster with [Search and Vector Search](https://www.mongodb.com/docs/search/self-managed/current/installation/linux.md#std-label-community-search-deploy installed.

  • Java Development Kit (JDK) version 8 or later.

  • An environment to set up and run a Java application. We recommend that you use an integrated development environment (IDE) such as IntelliJ IDEA or Eclipse IDE to configure Maven or Gradle to build and run your project.

  • A Voyage AI API (Application Programming Interface) key. To create an API (Application Programming Interface) key, see [Manage Voyage AI Model API Keys.](https://www.mongodb.com/docs/voyageai/management/api-keys.md#std-label-voyage-api-keys

  • One of the following MongoDB cluster types:

  • An Atlas clustert

  • A local Atlas deployment created using Python and Docker. Install atlas-local-lib-py (pip install atlas-local-lib-py) to programmatically create and manage local deployments. To learn more, see the atlas-local-lib-py repository.

  • A MongoDB Community or Enterprise cluster with [Search and Vector Search](https://www.mongodb.com/docs/search/self-managed/current/installation/linux.md#std-label-community-search-deploy installed.

  • Java Development Kit (JDK) version 8 or later.

  • An environment to set up and run a Java application. We recommend that you use an integrated development environment (IDE) such as IntelliJ IDEA or Eclipse IDE to configure Maven or Gradle to build and run your project.

  • A Voyage AI API (Application Programming Interface) key. To create an API (Application Programming Interface) key, see [Manage Voyage AI Model API Keys.](https://www.mongodb.com/docs/voyageai/management/api-keys.md#std-label-voyage-api-keys

  • One of the following MongoDB cluster types:

  • An Atlas clustert

  • A local Atlas deployment created using Python and Docker. Install atlas-local-lib-py (pip install atlas-local-lib-py) to programmatically create and manage local deployments. To learn more, see the atlas-local-lib-py repository.

  • A MongoDB Community or Enterprise cluster with [Search and Vector Search](https://www.mongodb.com/docs/search/self-managed/current/installation/linux.md#std-label-community-search-deploy installed.

  • A terminal and code editor to run your C# project.

  • .NET 8.0 or higher installed.

  • A Voyage AI API (Application Programming Interface) key. To create an API (Application Programming Interface) key, see [Manage Voyage AI Model API Keys.](https://www.mongodb.com/docs/voyageai/management/api-keys.md#std-label-voyage-api-keys

  • One of the following MongoDB cluster types:

  • An Atlas clustert

  • A local Atlas deployment created using Python and Docker. Install atlas-local-lib-py (pip install atlas-local-lib-py) to programmatically create and manage local deployments. To learn more, see the atlas-local-lib-py repository.

  • A MongoDB Community or Enterprise cluster with [Search and Vector Search](https://www.mongodb.com/docs/search/self-managed/current/installation/linux.md#std-label-community-search-deploy installed.

  • A terminal and code editor to run your C# project.

  • .NET 8.0 or higher installed.

  • A Voyage AI API (Application Programming Interface) key. To create an API (Application Programming Interface) key, see [Manage Voyage AI Model API Keys.](https://www.mongodb.com/docs/voyageai/management/api-keys.md#std-label-voyage-api-keys

  • One of the following MongoDB cluster types:

  • An Atlas clustert

  • A local Atlas deployment created using Python and Docker. Install atlas-local-lib-py (pip install atlas-local-lib-py) to programmatically create and manage local deployments. To learn more, see the atlas-local-lib-py repository.

  • A MongoDB Community or Enterprise cluster with [Search and Vector Search](https://www.mongodb.com/docs/search/self-managed/current/installation/linux.md#std-label-community-search-deploy installed.

  • An environment to run interactive Python notebooks such as VS Code or Colab.

  • An OpenAI API Key. You must have an OpenAI account with credits available for API requests. To learn more about registering an OpenAI account, see the OpenAI API website.

  • One of the following MongoDB cluster types:

  • An Atlas clustert

  • A local Atlas deployment created using Python and Docker. Install atlas-local-lib-py (pip install atlas-local-lib-py) to programmatically create and manage local deployments. To learn more, see the atlas-local-lib-py repository.

  • A MongoDB Community or Enterprise cluster with [Search and Vector Search](https://www.mongodb.com/docs/search/self-managed/current/installation/linux.md#std-label-community-search-deploy installed.

  • An environment to run interactive Python notebooks such as VS Code or Colab.

  • An OpenAI API Key. You must have an OpenAI account with credits available for API requests. To learn more about registering an OpenAI account, see the OpenAI API website.

  • One of the following MongoDB cluster types:

  • An Atlas clustert

  • A local Atlas deployment created using Python and Docker. Install atlas-local-lib-py (pip install atlas-local-lib-py) to programmatically create and manage local deployments. To learn more, see the atlas-local-lib-py repository.

  • A MongoDB Community or Enterprise cluster with [Search and Vector Search](https://www.mongodb.com/docs/search/self-managed/current/installation/linux.md#std-label-community-search-deploy installed.

  • A terminal and code editor to run your Node.js project.

  • npm and Node.js installed.

  • An OpenAI API Key. You must have an OpenAI account with credits available for API requests. To learn more about registering an OpenAI account, see the OpenAI API website.

  • One of the following MongoDB cluster types:

  • An Atlas clustert

  • A local Atlas deployment created using Python and Docker. Install atlas-local-lib-py (pip install atlas-local-lib-py) to programmatically create and manage local deployments. To learn more, see the atlas-local-lib-py repository.

  • A MongoDB Community or Enterprise cluster with [Search and Vector Search](https://www.mongodb.com/docs/search/self-managed/current/installation/linux.md#std-label-community-search-deploy installed.

  • A terminal and code editor to run your Node.js project.

  • npm and Node.js installed.

  • An OpenAI API Key. You must have an OpenAI account with credits available for API requests. To learn more about registering an OpenAI account, see the OpenAI API website.

  • One of the following MongoDB cluster types:

  • An Atlas clustert

  • A local Atlas deployment created using Python and Docker. Install atlas-local-lib-py (pip install atlas-local-lib-py) to programmatically create and manage local deployments. To learn more, see the atlas-local-lib-py repository.

  • A MongoDB Community or Enterprise cluster with [Search and Vector Search](https://www.mongodb.com/docs/search/self-managed/current/installation/linux.md#std-label-community-search-deploy installed.

  • A terminal and code editor to run your Go project.

  • Go installed.

  • An OpenAI API Key. You must have an OpenAI account with credits available for API requests. To learn more about registering an OpenAI account, see the OpenAI API website.

  • One of the following MongoDB cluster types:

  • An Atlas clustert

  • A local Atlas deployment created using Python and Docker. Install atlas-local-lib-py (pip install atlas-local-lib-py) to programmatically create and manage local deployments. To learn more, see the atlas-local-lib-py repository.

  • A MongoDB Community or Enterprise cluster with [Search and Vector Search](https://www.mongodb.com/docs/search/self-managed/current/installation/linux.md#std-label-community-search-deploy installed.

  • A terminal and code editor to run your Go project.

  • Go installed.

  • An OpenAI API Key. You must have an OpenAI account with credits available for API requests. To learn more about registering an OpenAI account, see the OpenAI API website.

  • One of the following MongoDB cluster types:

  • An Atlas clustert

  • A local Atlas deployment created using Python and Docker. Install atlas-local-lib-py (pip install atlas-local-lib-py) to programmatically create and manage local deployments. To learn more, see the atlas-local-lib-py repository.

  • A MongoDB Community or Enterprise cluster with [Search and Vector Search](https://www.mongodb.com/docs/search/self-managed/current/installation/linux.md#std-label-community-search-deploy installed.

  • Java Development Kit (JDK) version 8 or later.

  • An environment to set up and run a Java application. We recommend that you use an integrated development environment (IDE) such as IntelliJ IDEA or Eclipse IDE to configure Maven or Gradle to build and run your project.

  • An OpenAI API Key. You must have an OpenAI account with credits available for API requests. To learn more about registering an OpenAI account, see the OpenAI API website.

  • One of the following MongoDB cluster types:

  • An Atlas clustert

  • A local Atlas deployment created using Python and Docker. Install atlas-local-lib-py (pip install atlas-local-lib-py) to programmatically create and manage local deployments. To learn more, see the atlas-local-lib-py repository.

  • A MongoDB Community or Enterprise cluster with [Search and Vector Search](https://www.mongodb.com/docs/search/self-managed/current/installation/linux.md#std-label-community-search-deploy installed.

  • Java Development Kit (JDK) version 8 or later.

  • An environment to set up and run a Java application. We recommend that you use an integrated development environment (IDE) such as IntelliJ IDEA or Eclipse IDE to configure Maven or Gradle to build and run your project.

  • An OpenAI API Key. You must have an OpenAI account with credits available for API requests. To learn more about registering an OpenAI account, see the OpenAI API website.

  • One of the following MongoDB cluster types:

  • An Atlas clustert

  • A local Atlas deployment created using Python and Docker. Install atlas-local-lib-py (pip install atlas-local-lib-py) to programmatically create and manage local deployments. To learn more, see the atlas-local-lib-py repository.

  • A MongoDB Community or Enterprise cluster with [Search and Vector Search](https://www.mongodb.com/docs/search/self-managed/current/installation/linux.md#std-label-community-search-deploy installed.

  • A terminal and code editor to run your C# project.

  • .NET 8.0 or higher installed.

  • An OpenAI API Key. You must have an OpenAI account with credits available for API requests. To learn more about registering an OpenAI account, see the OpenAI API website.

  • One of the following MongoDB cluster types:

  • An Atlas clustert

  • A local Atlas deployment created using Python and Docker. Install atlas-local-lib-py (pip install atlas-local-lib-py) to programmatically create and manage local deployments. To learn more, see the atlas-local-lib-py repository.

  • A MongoDB Community or Enterprise cluster with [Search and Vector Search](https://www.mongodb.com/docs/search/self-managed/current/installation/linux.md#std-label-community-search-deploy installed.

  • A terminal and code editor to run your C# project.

  • .NET 8.0 or higher installed.

  • An OpenAI API Key. You must have an OpenAI account with credits available for API requests. To learn more about registering an OpenAI account, see the OpenAI API website.

  • One of the following MongoDB cluster types:

  • An Atlas clustert

  • A local Atlas deployment created using Python and Docker. Install atlas-local-lib-py (pip install atlas-local-lib-py) to programmatically create and manage local deployments. To learn more, see the atlas-local-lib-py repository.

  • A MongoDB Community or Enterprise cluster with [Search and Vector Search](https://www.mongodb.com/docs/search/self-managed/current/installation/linux.md#std-label-community-search-deploy installed.

  • An environment to run interactive Python notebooks such as VS Code or Colab.

  • A Hugging Face Access Token.

  • One of the following MongoDB cluster types:

  • An Atlas clustert

  • A local Atlas deployment created using Python and Docker. Install atlas-local-lib-py (pip install atlas-local-lib-py) to programmatically create and manage local deployments. To learn more, see the atlas-local-lib-py repository.

  • A MongoDB Community or Enterprise cluster with [Search and Vector Search](https://www.mongodb.com/docs/search/self-managed/current/installation/linux.md#std-label-community-search-deploy installed.

  • An environment to run interactive Python notebooks such as VS Code or Colab.

  • A Hugging Face Access Token.

  • One of the following MongoDB cluster types:

  • An Atlas clustert

  • A local Atlas deployment created using Python and Docker. Install atlas-local-lib-py (pip install atlas-local-lib-py) to programmatically create and manage local deployments. To learn more, see the atlas-local-lib-py repository.

  • A MongoDB Community or Enterprise cluster with [Search and Vector Search](https://www.mongodb.com/docs/search/self-managed/current/installation/linux.md#std-label-community-search-deploy installed.

  • A terminal and code editor to run your Node.js project.

  • npm and Node.js installed.

  • A Hugging Face Access Token.

  • One of the following MongoDB cluster types:

  • An Atlas clustert

  • A local Atlas deployment created using Python and Docker. Install atlas-local-lib-py (pip install atlas-local-lib-py) to programmatically create and manage local deployments. To learn more, see the atlas-local-lib-py repository.

  • A MongoDB Community or Enterprise cluster with [Search and Vector Search](https://www.mongodb.com/docs/search/self-managed/current/installation/linux.md#std-label-community-search-deploy installed.

  • A terminal and code editor to run your Node.js project.

  • npm and Node.js installed.

  • A Hugging Face Access Token.

  • One of the following MongoDB cluster types:

  • An Atlas clustert

  • A local Atlas deployment created using Python and Docker. Install atlas-local-lib-py (pip install atlas-local-lib-py) to programmatically create and manage local deployments. To learn more, see the atlas-local-lib-py repository.

  • A MongoDB Community or Enterprise cluster with [Search and Vector Search](https://www.mongodb.com/docs/search/self-managed/current/installation/linux.md#std-label-community-search-deploy installed.

  • A terminal and code editor to run your Go project.

  • Go installed.

  • A Hugging Face Access Token.

  • One of the following MongoDB cluster types:

  • An Atlas clustert

  • A local Atlas deployment created using Python and Docker. Install atlas-local-lib-py (pip install atlas-local-lib-py) to programmatically create and manage local deployments. To learn more, see the atlas-local-lib-py repository.

  • A MongoDB Community or Enterprise cluster with [Search and Vector Search](https://www.mongodb.com/docs/search/self-managed/current/installation/linux.md#std-label-community-search-deploy installed.

  • A terminal and code editor to run your Go project.

  • Go installed.

  • A Hugging Face Access Token.

  • One of the following MongoDB cluster types:

  • An Atlas clustert

  • A local Atlas deployment created using Python and Docker. Install atlas-local-lib-py (pip install atlas-local-lib-py) to programmatically create and manage local deployments. To learn more, see the atlas-local-lib-py repository.

  • A MongoDB Community or Enterprise cluster with [Search and Vector Search](https://www.mongodb.com/docs/search/self-managed/current/installation/linux.md#std-label-community-search-deploy installed.

  • Java Development Kit (JDK) version 8 or later.

  • An environment to set up and run a Java application. We recommend that you use an integrated development environment (IDE) such as IntelliJ IDEA or Eclipse IDE to configure Maven or Gradle to build and run your project.

  • A Hugging Face Access Token.

  • One of the following MongoDB cluster types:

  • An Atlas clustert

  • A local Atlas deployment created using Python and Docker. Install atlas-local-lib-py (pip install atlas-local-lib-py) to programmatically create and manage local deployments. To learn more, see the atlas-local-lib-py repository.

  • A MongoDB Community or Enterprise cluster with [Search and Vector Search](https://www.mongodb.com/docs/search/self-managed/current/installation/linux.md#std-label-community-search-deploy installed.

  • Java Development Kit (JDK) version 8 or later.

  • An environment to set up and run a Java application. We recommend that you use an integrated development environment (IDE) such as IntelliJ IDEA or Eclipse IDE to configure Maven or Gradle to build and run your project.

  • A Hugging Face Access Token.

  • One of the following MongoDB cluster types:

  • An Atlas clustert

  • A local Atlas deployment created using Python and Docker. Install atlas-local-lib-py (pip install atlas-local-lib-py) to programmatically create and manage local deployments. To learn more, see the atlas-local-lib-py repository.

  • A MongoDB Community or Enterprise cluster with [Search and Vector Search](https://www.mongodb.com/docs/search/self-managed/current/installation/linux.md#std-label-community-search-deploy installed.

  • A terminal and code editor to run your C# project.

  • .NET 8.0 or higher installed.

  • A Hugging Face Access Token.

  • One of the following MongoDB cluster types:

  • An Atlas clustert

  • A local Atlas deployment created using Python and Docker. Install atlas-local-lib-py (pip install atlas-local-lib-py) to programmatically create and manage local deployments. To learn more, see the atlas-local-lib-py repository.

  • A MongoDB Community or Enterprise cluster with [Search and Vector Search](https://www.mongodb.com/docs/search/self-managed/current/installation/linux.md#std-label-community-search-deploy installed.

  • A terminal and code editor to run your C# project.

  • .NET 8.0 or higher installed.

  • A Hugging Face Access Token.

Use an Embedding Model

  1. Set up the environment.

Create an interactive Python notebook by saving a file with the .ipynb extension, and then run the following command in the notebook to install the dependencies:

python pip install --quiet --upgrade voyageai pymongo

  1. Define and test the function to generate vector embeddings.

Paste and run the following code in your notebook to create a function named get_embedding() that generates vector embeddings by using an embedding model from Voyage AI. Replace <api-key> with your Voyage API key.

The function specifies the following:

  • voyage-4-large as the embedding model to use.

  • input_type parameter to optimize your embeddings for retrieval. To learn more, see [Voyage AI Python Client.](https://www.mongodb.com/docs/voyageai/api-and-clients.md#std-label-voyage-python-client

Tip:

For all models and parameters, see [Voyage AI Text Embeddings.](https://www.mongodb.com/docs/voyageai/models/text-embeddings.md#std-label-voyage-text-embeddings

```python import os import voyageai

# Specify your Voyage API key and embedding model os.environ["VOYAGE_API_KEY"] = "" model = "voyage-4-large" vo = voyageai.Client()

# Define a function to generate embeddings def get_embedding(data, input_type = "document"): embeddings = vo.embed( data, model = model, input_type = input_type ).embeddings return embeddings[0]

# Generate an embedding embedding = get_embedding("foo") print(embedding) ```

Output:

shell [-0.0680396631360054, 0.03951127082109451, 0.010895480401813984, -0.041856683790683746, ... 0.0393928699195385, 0.0013418495655059814, -0.005869182292371988, -0.014275475405156612]

###### (Advanced) Compress your embeddings.

###### Expand this section to define a function that converts your embeddings to BSON binary format.

Optionally, you can compress your embeddings by converting them to BSON (Binary Javascript Object Notation) binary format, also called binData vectors, for efficient storage and retrieval. To learn more, see [Vector Compression.](https://www.mongodb.com/docs/vector-search/crud-embeddings/create-embeddings-manual.md#std-label-avs-vector-compression

This code does the following:

  • Defines a function named generate_bson_vector to convert embeddings to binData vectors by using binary tools from the PyMongo driver.

  • Converts the embeddings you generated for the string foo to binData vectors. The exact embedding varies depending on the model you use.

Note:

When you convert your embeddings to binData vectors, your embeddings appear in binary format.

```python from bson.binary import Binary from bson.binary import BinaryVectorDtype

# Define a function to generate BSON vectors def generate_bson_vector(vector, vector_dtype): return Binary.from_vector(vector, vector_dtype)

# Generate BSON vector from the sample float32 embedding bson_float32_embedding = generate_bson_vector(embedding, BinaryVectorDtype.FLOAT32)

# Print the converted embedding print(f"The converted BSON embedding is: {bson_float32_embedding}") ```

Output:

shell The converted BSON embedding is: b'\'\x00aX\x8b\xbd\x92\xd6!=\xf5\x822<\x ... \xc0\xbb\xaf\xe3i\xbc'

1) Set up the environment.

Create an interactive Python notebook by saving a file with the .ipynb extension, and then run the following command in the notebook to install the dependencies:

python pip install --quiet --upgrade voyageai pymongo

2) Define and test the function to generate vector embeddings.

Paste and run the follo

(本文超出站点单文档体积上限,已截断)