Skip to main content

Convex

LangChain.js supports Convex as a vector store, and supports the standard similarity search.

Setup

Create project

Get a working Convex project set up, for example by using:

npm create convex@latest

Add database accessors

Add query and mutation helpers to convex/langchain/db.ts:

convex/langchain/db.ts
export * from "langchain/util/convex";

Configure your schema

Set up your schema (for vector indexing):

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

export default defineSchema({
documents: defineTable({
embedding: v.array(v.number()),
text: v.string(),
metadata: v.any(),
}).vectorIndex("byEmbedding", {
vectorField: "embedding",
dimensions: 1536,
}),
});

Usage

npm install @langchain/openai @langchain/community

Ingestion

convex/myActions.ts
"use node";

import { ConvexVectorStore } from "@langchain/community/vectorstores/convex";
import { OpenAIEmbeddings } from "@langchain/openai";
import { action } from "./_generated/server.js";

export const ingest = action({
args: {},
handler: async (ctx) => {
await ConvexVectorStore.fromTexts(
["Hello world", "Bye bye", "What's this?"],
[{ prop: 2 }, { prop: 1 }, { prop: 3 }],
new OpenAIEmbeddings(),
{ ctx }
);
},
});

API Reference:

convex/myActions.ts
"use node";

import { ConvexVectorStore } from "@langchain/community/vectorstores/convex";
import { OpenAIEmbeddings } from "@langchain/openai";
import { v } from "convex/values";
import { action } from "./_generated/server.js";

export const search = action({
args: {
query: v.string(),
},
handler: async (ctx, args) => {
const vectorStore = new ConvexVectorStore(new OpenAIEmbeddings(), { ctx });

const resultOne = await vectorStore.similaritySearch(args.query, 1);
console.log(resultOne);
},
});

API Reference:


Help us out by providing feedback on this documentation page: