11.6 C
Pakistan
Wednesday, February 21, 2024

The New JSON API for Astra DB Makes It Simple to Create AI Applications in JavaScript

At DataStax, one of our objectives is to make it feasible for every developer, regardless of the language they use to create in, to quickly bring AI applications to production.

To assist developers in building AI applications, we have added vector search capabilities to DataStax Astra DB, our database-as-a-service built on Apache Cassandra, an open source platform.

The most potent, scalable, and production-ready database is known as Cassandra. Vector search has made Cassandra and Astra DB an essential building block for creating enterprise-grade Gen AI applications. But we also need to make sure that a wide range of developers, regardless of their preferred language or level of expertise, can use this potent technology.

Today, we take a significant step in that direction by launching the JSON API for Astra DB, which gives the vast community of JavaScript developers access to the most potent vector database in the world.

The JSON API is here!

Document databases are widely used in the JavaScript community. It should come as no surprise that the ability to store and retrieve JSON documents to and from the database greatly speeds up development since JSON is a native syntax in JavaScript.

The new JSON API is made to give JavaScript developers who are making new AI applications a comfortable development experience. We set out to make sure that if you are a JavaScript developer, you can launch an instance of Astra DB and begin creating right away using paradigms and frameworks you are accustomed to.

Astra DB’s exposure as a document database improves the developer experience in a number of ways:

Because you conceptualize in terms of JSON objects, you naturally fit with the JavaScript ecosystem.
Since the database handles data modeling, there is no separate process. Documents are simply saved and retrieved.
You may get started on the development process immediately and concentrate on the application logic rather than the backend operations.

Compatibility with Mongo JS

We have observed that a significant portion of the JavaScript community uses object data modeling (ODM) frameworks, particularly MongooseJS, to interact with document databases. A well-liked object modeling framework built on top of document databases is called MongooseJS. It doesn’t need an introduction with nearly 3.5 million dependent Github repositories and over 2 million weekly NPM downloads.

MongooseJS and the new JSON API for Astra DB work together flawlessly. This implies that configuring MongooseJS to connect to an Astra DB instance only requires a few lines of code:

// Import MongooseJS.
const mongoose = require("mongoose");
// Import the driver for Astra DB (shipped as a part of stargate.io).
const { driver } = require("stargate-mongoose");

// Tell MongooseJS to use the Astra DB driver instead of the default one.
mongoose.setDriver(driver);

// Connect to Astra DB.
await mongoose.connect(astraDbUri, {
  isAstra: true,
});

Once linked, you may use MongooseJS APIs while Astra DB handles the labor-intensive tasks of indexing, storing, and scaling out your documents when necessary.

Supporting vector searches
Astra DB Vector, the only database created for simultaneous search and update on distributed data and streaming workloads with extremely low latency, as well as highly relevant vector results that eliminate redundancies, is even more accessible when developing with MongooseJS supported by Astra DB. As a result, you get Astra DB’s broad vector support and scalability along with MongooseJS’s familiarity and convenience of use. JavaScript has never made creating AI apps easier!

Let’s look at a straightforward illustration of using Astra DB’s vector search in a MongooseJS application. In this example, we’ll put up a group of movies with text descriptions and other details like title, year of release, and genre. MongooseJS will also be told that we want to save vector embeddings for the descriptions. The model definition will appear as follows:

const Movie = mongoose.model(
  "Movie",
  new mongoose.Schema(
    {
      title: String,
      year: Number,
      genre: String,
      description: String,
      $vector: {
        type: [Number],
        validate: (vector) => vector && vector.length === 1536,
      },
    },
    {
      collectionOptions: {
        vector: {
          size: 1536,
          function: "cosine",
        },
      },
    },
  ),
);

Those familiar with MongooseJS will find this to be a typical MongooseJS model, except for two additional pieces that Astra DB’s driver allows for:

  1. The special $vector field that is used to store vector embeddings.
  2. The collectionOptions.vector object that tells Astra DB how to index the vector embedding field.

With the model above, you can insert documents along with the embeddings:

await Movie.insert({
  title: "In the Border States",
  year: 1910,
  genre: "Drama",
  description: "In the Border States is a 1910 American drama film...",   
  // Generate embedding for the description,
  // for example by invoking the OpenAI API.
  $vector: embedding("In the Border States is a 1910 American drama film..."),
});

By entering a free-form question, users of your application can now search for movies based on their descriptions. To do that, you will construct an embedding for the user’s query using the same model, and you will use Astra DB’s vector search to locate the most pertinent data in the database:

await Movie.find({})
  .sort({ $vector: { $meta: embedding("Something funny") } })
  .limit(3);

Of course, vector search isn’t always sufficient on its own; you may also need to combine it with filtering based on additional fields in the document. Here is an example of how to identify relevant films using the same search criteria as the previous example, but just among dramas:

await Movie.find({ genre: "Drama" })
  .sort({ $vector: { $meta: embedding("Criminals and detectives") } })
  .limit(3);

You are not constrained to straightforward CRUD activities when using MongooseJS and Astra DB. They can be used in conjunction with vector-based relevance searches, or you can even combine the two to create potent hybrid search queries.

Introduction to the JSON API
Anyone interested in testing out the new JSON API can do so on Astra DB while it is still in public preview. To get started, adhere to these 3 easy steps:

Create a vector database at Astra DB.

2. Once the database is active, switch to the “Connect” tab, choose “JSON API” as your preferred method, and follow the instructions.

  1. Have fun creating!

The documentation for the JSON API contains more information on how to utilize it.

Next, what?
Our goal in providing the JSON API is to make Astra DB the top option for JavaScript developers creating AI apps. Keep checking back for more features and enhancements because this is just the beginning.

Related Articles

LEAVE A REPLY

Please enter your comment!
Please enter your name here

Latest Articles