Introducing vector search with Convex!

Convex’s vector search enables you to provide useful context to LLMs for AI powered applications, recommendations for similar content and more. Pictured above is a demo showing how to search rap verses: https://rapgenie.net/

Vector search (BETA)

Vector search allows you to find Convex documents similar to a provided vector. Typically, vectors will be embeddings which are numerical representations of text, images, or audio. This can be used to build AI powered apps, recommendations for similar content, and more!

To use vector search, first add a vector index to your schema in convex/schema.ts:

import { defineSchema, defineTable } from "convex/server";
import { v } from "convex/values";

export default defineSchema({
  foods: defineTable({
    description: v.string(),
    cuisine: v.string(),
    embedding: v.array(v.float64()),
  }).vectorIndex("by_embedding", {
    vectorField: "embedding",
    dimensions: 1536,
  }),
});

Then, you can query this index to find similar documents from a Convex action:

import { v } from "convex/values";
import { action } from "./_generated/server";

export const similarFoods = action({
  args: {
    descriptionQuery: v.string(),
  },
  handler: async (ctx, args) => {
    // 1. Generate an embedding from you favorite third party API:
    const embedding = await embed(args.descriptionQuery);
    // 2. Then search for similar foods!
    const results = await ctx.vectorSearch("foods", "by_embedding", {
      vector: embedding,
    });
    // ...
  },
});

This action tells Convex to find documents in the foods table where the embedding field contains a vector similar to the vector generated from the provided description (descriptionQuery).

Convex vector search is consistent and fully up-to-date, meaning you can write a vector and immediately read it from a vector search. Unlike full text search, however, vector search is only available in Convex actions.

To learn more about vector search, check out the docs.

Vector search is a beta feature. Only up to the first 100,000 documents in a table can be indexed by a vector index. You can read more about the limits here. If you have feedback or feature requests, let us know in Discord!