docs.sheetjs.com/docz/static/loadofsheet/query.mjs

54 lines
1.9 KiB
JavaScript
Raw Permalink Normal View History

2024-07-01 03:59:01 +00:00
/* NOTE: hnswlib-node@3.0.0 does not install on a fresh Windows 11 setup */
// import { existsSync } from 'fs';
2024-06-19 11:22:00 +00:00
import { ChatOllama } from "@langchain/community/chat_models/ollama";
import { OllamaEmbeddings } from "@langchain/community/embeddings/ollama"
2024-07-01 03:59:01 +00:00
// import { HNSWLib } from "@langchain/community/vectorstores/hnswlib";
import { MemoryVectorStore } from "langchain/vectorstores/memory";
2024-06-19 11:22:00 +00:00
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";
2024-07-01 03:59:01 +00:00
console.log(`Using model ${modelName}`);
2024-06-19 11:22:00 +00:00
const model = new ChatOllama({ baseUrl: "http://localhost:11434", model: modelName });
const embeddings = new OllamaEmbeddings({model: modelName});
2024-07-01 03:59:01 +00:00
console.time("load of sheet");
2024-06-19 11:22:00 +00:00
const loader = new LoadOfSheet("./cd.xls");
const docs = await loader.load();
2024-07-01 03:59:01 +00:00
console.timeEnd("load of sheet");
2024-06-19 11:22:00 +00:00
2024-07-01 03:59:01 +00:00
console.time("vector store");
const vectorstore = await MemoryVectorStore.fromDocuments(docs, embeddings);
/*
2024-06-19 11:22:00 +00:00
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);
})();
2024-07-01 03:59:01 +00:00
*/
console.timeEnd("vector store");
2024-06-19 11:22:00 +00:00
2024-07-01 03:59:01 +00:00
console.time("query");
2024-06-19 11:22:00 +00:00
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?"
);
2024-07-01 03:59:01 +00:00
console.timeEnd("query");
2024-06-19 11:22:00 +00:00
res.forEach(({metadata}) => { console.log({ Name: metadata.Name, MPG: metadata.Miles_per_Gallon }); });