docs.sheetjs.com/docz/static/loadofsheet/query.mjs
2024-06-30 23:59:01 -04:00

54 lines
1.9 KiB
JavaScript

/* NOTE: hnswlib-node@3.0.0 does not install on a fresh Windows 11 setup */
// import { existsSync } from 'fs';
import { ChatOllama } from "@langchain/community/chat_models/ollama";
import { OllamaEmbeddings } from "@langchain/community/embeddings/ollama"
// import { HNSWLib } from "@langchain/community/vectorstores/hnswlib";
import { MemoryVectorStore } from "langchain/vectorstores/memory";
import { SelfQueryRetriever } from "langchain/retrievers/self_query";
import { FunctionalTranslator } from "@langchain/core/structured_query";
import LoadOfSheet from "./loadofsheet.mjs";
const modelName = "llama3-chatqa:8b-v1.5-q8_0";
console.log(`Using model ${modelName}`);
const model = new ChatOllama({ baseUrl: "http://localhost:11434", model: modelName });
const embeddings = new OllamaEmbeddings({model: modelName});
console.time("load of sheet");
const loader = new LoadOfSheet("./cd.xls");
const docs = await loader.load();
console.timeEnd("load of sheet");
console.time("vector store");
const vectorstore = await MemoryVectorStore.fromDocuments(docs, embeddings);
/*
const vectorstore = await (async() => {
if(!existsSync("store/hnswlib.index")) {
const vectorstore = await HNSWLib.fromDocuments(docs, embeddings);
await vectorstore.save("store");
return vectorstore;
}
return await HNSWLib.load("store", embeddings);
})();
*/
console.timeEnd("vector store");
console.time("query");
const selfQueryRetriever = SelfQueryRetriever.fromLLM({
llm: model,
vectorStore: vectorstore,
documentContents: "Data rows from a worksheet",
attributeInfo: loader.attributes,
structuredQueryTranslator: new FunctionalTranslator(),
searchParams: { k: 1024 } // default is 4
});
const res = await selfQueryRetriever.invoke(
"Which rows have over 40 miles per gallon?"
);
console.timeEnd("query");
res.forEach(({metadata}) => { console.log({ Name: metadata.Name, MPG: metadata.Miles_per_Gallon }); });