File size: 1,403 Bytes
a9dff2c | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 | import fs from "fs";
import readline from "readline";
import { QdrantClient } from "@qdrant/js-client-rest";
import path from "path";
if (!process.env.QDRANT_URL || !process.env.QDRANT_COLLECTION) {
throw new Error("Please set QDRANT_URL and QDRANT_COLLECTION env variables");
}
/**
* RUN EXAMPLE:
* ╰─❯ QDRANT_URL=http://localhost:6333/ QDRANT_COLLECTION=swiss-civil-code node insert-into-qdrant.js
*/
export async function RUN() {
const client = new QdrantClient({ url: process.env.QDRANT_URL });
let index = 0;
const file = path.join(
process.cwd(),
"data",
"swiss-code-of-obligations-en-paraphrase-multilingual-mpnet-base-v2.jsonl"
);
readLines(file, async (line) => {
index++;
const title = line.headings.slice(-1)[0];
const { vector, ...payload } = line;
console.log(index, title);
await client.upsert(process.env.QDRANT_COLLECTION, {
wait: true,
points: [{ id: index, vector, payload: { title, ...payload } }],
});
});
}
// Run the script
await RUN();
// async function for reading a file line by line and invoking a callback async sequentially
export async function readLines(filePath, func) {
const fileStream = fs.createReadStream(filePath);
const rl = readline.createInterface({
input: fileStream,
crlfDelay: Infinity,
});
for await (const line of rl) {
await func(JSON.parse(line));
}
}
|