swiss-code-of-obligations / script /insert-into-qdrant.js
brunnolou's picture
Upload insert-into-qdrant.js
a9dff2c
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));
}
}