跳至主要内容

凸透镜

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 参考

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 参考


此页面是否有帮助?


你也可以在 GitHub 上留下详细的反馈 在 GitHub 上.