Spaces:
Build error
Build error
File size: 2,223 Bytes
2c10495 | 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 56 57 58 59 60 61 62 63 64 65 66 67 |
import { GoogleGenerativeAIEmbeddings } from "@langchain/google-genai";
import { HNSWLib } from "@langchain/community/vectorstores/hnswlib";
import * as dotenv from "dotenv";
import path from "path";
import { HttpsProxyAgent } from "https-proxy-agent";
import nodeFetch from "node-fetch";
dotenv.config({ path: ".env.local" });
dotenv.config();
// Proxy setup
if (process.env.HTTPS_PROXY) {
const agent = new HttpsProxyAgent(process.env.HTTPS_PROXY);
(global as any).fetch = (url: any, init: any) => {
return nodeFetch(url, { ...init, agent }) as any;
};
}
const VECTOR_STORE_PATH = path.join(process.cwd(), "vector_store");
const run = async () => {
const query = process.argv[2];
if (!query) {
console.log("Usage: npm run query 'your search term'");
return;
}
if (!process.env.GOOGLE_GENERATIVE_AI_API_KEY) {
throw new Error("GOOGLE_GENERATIVE_AI_API_KEY is missing");
}
console.log(`Loading vector store from ${VECTOR_STORE_PATH}...`);
const embeddings = new GoogleGenerativeAIEmbeddings({
modelName: "text-embedding-004",
apiKey: process.env.GOOGLE_GENERATIVE_AI_API_KEY,
});
try {
const vectorStore = await HNSWLib.load(VECTOR_STORE_PATH, embeddings);
console.log(`Searching for: "${query}"...`);
const results = await vectorStore.similaritySearch(query, 3);
if (results.length === 0) {
console.log("No results found.");
return;
}
console.log(`\nFound ${results.length} relevant documents:\n`);
results.forEach((doc, i) => {
console.log(`[Result ${i + 1}] Score: (Implicit via retrieval)`);
console.log(`Title: ${doc.metadata.title || 'Untitled'}`);
console.log(`Source: ${doc.metadata.source || 'Unknown'}`);
console.log(`Preview: ${doc.pageContent.substring(0, 150).replace(/\n/g, ' ')}...`);
console.log("-".repeat(50));
});
} catch (error) {
console.error("Error loading vector store. Have you run the ingestion script?");
console.error(`Path checked: ${VECTOR_STORE_PATH}`);
console.error(error);
}
};
run().catch(console.error);
|