Skip to main content

Convex

LangChain.js 支持 Convex 作为 向量存储,并支持标准的相似性搜索。

安装设置

创建项目

首先创建一个可用的 Convex 项目,例如使用以下命令:

npm create convex@latest

添加数据库访问器

将查询和变更的辅助工具添加到 convex/langchain/db.ts 文件中:

convex/langchain/db.ts
export * from "@langchain/community/utils/convex";

配置你的数据库模式

配置你的模式(用于向量索引):

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,
}),
});

使用

:::提示 请参阅安装集成包的一般说明部分。 :::

npm install @langchain/openai @langchain/community @langchain/core

数据导入

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:

相关内容


Was this page helpful?


You can also leave detailed feedback on GitHub.