Spaces:
Build error
Build error
| 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); | |