How return id from ConvexVectorStore.fromDocuments #6350
-
import { v } from "convex/values";
import { action } from "../_generated/server";
import { Document } from "@langchain/core/documents";
import { RecursiveCharacterTextSplitter } from "langchain/text_splitter";
import { CacheBackedEmbeddings } from "langchain/embeddings/cache_backed";
import { ConvexKVStore } from "@langchain/community/storage/convex";
import { ConvexVectorStore } from "@langchain/community/vectorstores/convex";
import { OpenAIEmbeddings } from "@langchain/openai";
export const loadEmbeddings = action({
args: {
text: v.string(),
},
handler: async (ctx, { text }) => {
const textSplitter = new RecursiveCharacterTextSplitter({
chunkSize: 1000,
chunkOverlap: 200,
});
const splitDocs = await textSplitter.splitDocuments([
new Document({ pageContent: text }),
]);
const embeddings = new CacheBackedEmbeddings({
underlyingEmbeddings: new OpenAIEmbeddings(),
documentEmbeddingStore: new ConvexKVStore({ ctx }),
});
await ConvexVectorStore.fromDocuments(splitDocs, embeddings, { ctx });
},
}); I write embedding using using LangchainJS in Next.js and Convex |
Beta Was this translation helpful? Give feedback.
Replies: 1 comment
-
To determine the ID provided by the database for the embedding created using await withVectorStore(async (vectorStore: MomentoVectorIndex) => {
const pageContent = faker.lorem.sentence(5);
// Add documents without providing IDs, so UUIDs will be generated
await vectorStore.addDocuments([{ pageContent, metadata: {} }]);
await sleep();
// Perform a similarity search to retrieve the document
const results = await vectorStore.similaritySearch(pageContent, 1);
// The results will contain the document with the generated ID
console.log(results);
}); If you want to provide your own IDs, you can do so like this: await withVectorStore(async (vectorStore: MomentoVectorIndex) => {
const pageContent = faker.lorem.sentence(5);
const documentId = "custom-id";
// Add documents with a custom ID
await vectorStore.addDocuments([{ pageContent, metadata: {} }], {
ids: [documentId],
});
await sleep();
// Perform a similarity search to retrieve the document
const results = await vectorStore.similaritySearch(pageContent, 1);
// The results will contain the document with the provided ID
console.log(results);
}); In both cases, the Additionally, if you are using the const embeddings = ...; // Your embeddings instance
const docs = [{ pageContent: "example content", metadata: {} }];
const dbConfig = { collection: yourMongoDBCollection, apiKey: "your-api-key" };
const vectorStore = await MongoDBAtlasVectorSearch.fromDocuments(docs, embeddings, dbConfig);
const ids = await vectorStore.addDocuments(docs);
console.log(ids); // This will print the IDs of the added documents In this example, the |
Beta Was this translation helpful? Give feedback.
To determine the ID provided by the database for the embedding created using
ConvexVectorStore.fromDocuments
, you can use theaddDocuments
method and provide your own IDs. If you do not provide IDs, the system will generate UUIDs for you. Here is an example of how you can retrieve the IDs: