凸透镜
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
- Yarn
- pnpm
npm install @langchain/openai @langchain/community @langchain/core
yarn add @langchain/openai @langchain/community @langchain/core
pnpm add @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 参考
- ConvexVectorStore 来自
@langchain/community/vectorstores/convex
- OpenAIEmbeddings 来自
@langchain/openai
搜索
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 参考
- ConvexVectorStore 来自
@langchain/community/vectorstores/convex
- OpenAIEmbeddings 来自
@langchain/openai