TechnologySeptember 19, 2023

Introducing the New JSON API for Astra DB: Develop AI Applications in JavaScript with Ease

Val Kulichenko
Val KulichenkoProduct
Introducing the New JSON API for Astra DB: Develop AI Applications in JavaScript with Ease
// 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,
});

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",
        },
      },
    },
  ),
);
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..."),
});
await Movie.find({})
  .sort({ $vector: { $meta: embedding("Something funny") } })
  .limit(3);
await Movie.find({ genre: "Drama" })
  .sort({ $vector: { $meta: embedding("Criminals and detectives") } })
  .limit(3);
Discover more
DataStax Astra DB
Share

One-stop Data API for Production GenAI

Astra DB gives JavaScript 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.