凸透镜
LangChain.js 支持 凸透镜 作为 向量存储,并支持标准相似性搜索。
设置
创建项目
获得一个可工作的 凸透镜 项目设置,例如通过使用
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
yarn add @langchain/openai @langchain/community
pnpm add @langchain/openai @langchain/community
摄取
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