Files
RAG-Lambdas/ingest.js
2026-08-12 19:41:32 -04:00

37 lines
1.3 KiB
JavaScript

import { S3Client, GetObjectCommand } from "@aws-sdk/client-s3";
import { Pinecone } from "@pinecone-database/pinecone";
import { chunkText } from "./lib/chunk.js";
import { embedTexts } from "./lib/voyage.js";
const s3 = new S3Client({});
const pc = new Pinecone({ apiKey: process.env.PINECONE_API_KEY });
const UPSERT_BATCH_SIZE = 100;
export const handler = async (event) => {
const bucket = event.Records[0].s3.bucket.name;
const key = decodeURIComponent(event.Records[0].s3.object.key.replace(/\+/g, " "));
const s3Response = await s3.send(new GetObjectCommand({ Bucket: bucket, Key: key }));
const rawText = await s3Response.Body.transformToString();
const chunks = chunkText(rawText);
if (chunks.length === 0) {
return { status: "Success", processedChunks: 0 };
}
const embeddings = await embedTexts(chunks, "document");
const index = pc.index(process.env.PINECONE_INDEX_NAME);
const vectors = chunks.map((chunk, i) => ({
id: `${key}_chunk_${i}`,
values: embeddings[i],
metadata: { text: chunk, source: key, chunkIndex: i },
}));
for (let i = 0; i < vectors.length; i += UPSERT_BATCH_SIZE) {
await index.upsert(vectors.slice(i, i + UPSERT_BATCH_SIZE));
}
return { status: "Success", processedChunks: chunks.length };
};