TechnologyMay 22, 2024

Astra Vectorize: Generate Vector Embeddings with 1 Line of Code

Astra Vectorize: Generate Vector Embeddings with 1 Line of Code

When developing generative AI applications, especially those involving retrieval-augmented generation (RAG), developers almost inevitably face the task of indexing unstructured data (like text) for further semantic search. An increasingly common way to achieve this is to convert text into vector embeddings and store them in a vector database.

But this can present a challenge: developers must perform this process within their application code by manually generating embeddings for any CRUD operation, whether it’s an insert, update, or search query. This leads to an error-prone and time-consuming development process.

Astra Vectorize speeds up this process. By handling embedding generation directly at the database level, Astra Vectorize ensures your database not only stores but also intelligently indexes and searches your data. This means you, as a GenAI developer, can focus on building innovative GenAI applications without getting bogged down by the technicalities of embedding generation.

You can focus more on the logic of your application, rather than the intricacies of your data’s intermediate states, one of which is vector embeddings. It’s not even necessary to know what embeddings are and how they work! Just choose an embedding service, configure it with Astra DB, and you’re good to go.

With Astra Vectorize, you can:

  • Accelerate development - Automatically generate embeddings during CRUD operations, freeing you from manual data-processing tasks.
  • Improve performance - Experience faster indexing and search capabilities, boosting your application's responsiveness and user experience while reducing the likelihood of errors.

How does Astra Vectorize work?

We implemented Astra Vectorize as an addition to our Data API so that you can use it seamlessly both for new and existing applications developed with Astra DB. The best way to demonstrate how it works is with code.

First, let’s take a look at a piece of code that would be required to insert a record without Astra Vectorize. I’m using the TypeScript client: 

const client = new DataAPIClient(process.env.ASTRA_DB_TOKEN);
const database = client.db(process.env.ASTRA_DB_ENDPOINT);
const movies = database.collection("movies");

const description =
  "Marty McFly, a 17-year-old high school student, is accidentally sent 30 years into the past in a time-traveling DeLorean invented by his close friend, the maverick scientist Doc Brown.";

await movies.insertOne({
  title: "Back to the Future",
  year: 1985,
  genre: "Comedy",
  description,
  $vector: await embedding(description),
});

// Function that generates embeddings.
async function embedding(text) {
  const response = await fetch("https://api.openai.com/v1/embeddings", {
    method: "POST",
    headers: {
      "Content-Type": "application/json",
      Authorization: `Bearer ${process.env.OPENAI_API_KEY}`,
    },
    body: JSON.stringify({
      input: text,
      model: "text-embedding-3-small",
    }),
  });

  const result = await response.json();

  return result.data[0].embedding;
}

Here, the application implements the logic to call an embedding model and generate a vector from a piece of text, and then explicitly provides a vector to the database as a part of the insert operation.

It’s far simpler with Astra Vectorize:

await movies.insertOne({
  title: "Back to the Future",
  year: 1985,
  genre: "Comedy",
  description,
  $vectorize:
    "Marty McFly, a 17-year-old high school student, is accidentally sent 30 years into the past in a time-traveling DeLorean invented by his close friend, the maverick scientist Doc Brown.",
});

Just a single line of code! No need to understand the intricacies of embeddings—just choose your preferred embedding service and let Astra Vectorize handle the rest. This powerful tool not only simplifies your workflow but also enhances the overall performance and scalability of your GenAI projects.

You can similarly use Astra Vectorize for search queries as well: 

const similarMovies = await movies
  .find(
    {},
    {
      $vectorize: "Time travel",
      limit: 3,
      projection: { $vector: 0 },
    }
  )
  .toArray();

console.log(similarMovies);

Choose your vector embedding provider

To give developers freedom to choose embedding models they’re familiar with and would like to use for their applications, Astra Vectorize seamlessly integrates with multiple embedding providers. Switching from one provider or a model to another is a matter of a simple configuration change–the application code always stays the same.

OpenAI and Azure OpenAI

OpenAI provides by far the most popular set of embedding models at the moment, and you can use any of those models with Astra Vectorize. All you need to do is create an OpenAI account, generate an API key, and register this key with Astra DB. Our native OpenAI integration takes care of the rest.

If you are running on Microsoft Azure, you can also opt for the Azure OpenAI version, for better integration with the Azure ecosystem.

NVIDIA

We partnered with NVIDIA to integrate with their GenAI embedding models. Unlike other integrations, it’s hosted within Astra DB, so your data never leaves the database. This architecture enables us to generate embeddings faster and with lower cost, on top of improved developer experience.

And many more!

Besides the OpenAI, Azure OpenAI, and NVIDIA integrations, we've partnered with Hugging Face, Jina AI, Mistral AI, Upstage, and Voyage AI. For the latest information, please refer to our vector embedding providers documentation.

Focus on what matters

Astra Vectorize represents a significant leap forward in the way GenAI developers handle and utilize unstructured data within their applications. Astra Vectorize is a game-changer for developers looking to streamline their processes and focus on what truly matters: creating cutting-edge AI applications. Experience the future of GenAI development with Astra Vectorize.

For more details, visit our vector embedding providers documentation and see how Astra Vectorize can revolutionize your development workflow.

Share

One-Stop Data API for Production GenAI

Astra DB gives developers a complete data API and out-of-the-box integrations that make it easier to build production RAG apps with high relevancy and low latency.