diff --git a/apps/simple/pg-vector-store/README.md b/apps/simple/pg-vector-store/README.md new file mode 100644 index 0000000000000000000000000000000000000000..7bcf6a854430165398eafdb80159d769645e4715 --- /dev/null +++ b/apps/simple/pg-vector-store/README.md @@ -0,0 +1,28 @@ +# Postgres Vector Store +There are two scripts available here: load-docs.ts and query.ts + +## Prerequisites +You'll need a postgres database instance against which to run these scripts. A simple docker command would look like this: + +>`docker run -d --rm --name vector-db -p 5432:5432 -e "POSTGRES_HOST_AUTH_METHOD=trust" ankane/pgvector` + +Set the PGHOST and PGUSER (and PGPASSWORD) environment variables to match your database setup. + +You'll also need a value for OPENAI_API_KEY in your environment. + +**NOTE:** Using `--rm` in the example docker command above means that the vector store will be deleted every time the container is stopped. For production purposes, use a volume to ensure persistence across restarts. + +## Setup and Loading Docs +Read and follow the instructions in the README.md file located one directory up to make sure your JS/TS dependencies are set up. The commands listed below are also run from that parent directory. + +To import documents and save the embedding vectors to your database: +>`npx ts-node pg-vector-store/load-docs.ts data` + +where data is the directory containing your input files. Using the *data* directory in the example above will read all of the files in that directory using the llamaindexTS default readers for each file type. + +## RAG Querying +To query using the resulting vector store: + +>`npx ts-node pg-vector-store/query.ts` + +The script will prompt for a question, then process and present the answer using the PGVectorStore data and your OpenAI API key. It will continue to prompt until you enter `q`, `quit` or `exit` as the next query. \ No newline at end of file diff --git a/apps/simple/pg-vector-store/load-docs.ts b/apps/simple/pg-vector-store/load-docs.ts index 7baca34b78becb6cada2a16e2cac282709b6adbd..2a5eb863b3035287549d1faa08a94170b13cd9f1 100755 --- a/apps/simple/pg-vector-store/load-docs.ts +++ b/apps/simple/pg-vector-store/load-docs.ts @@ -1,11 +1,7 @@ // load-docs.ts import fs from 'fs/promises'; import { SimpleDirectoryReader, storageContextFromDefaults, VectorStoreIndex } from 'llamaindex'; -// import { VectorStoreIndex } from "../../../packages/core/src/indices/vectorStore/VectorStoreIndex"; -// import { SimpleDirectoryReader } from "../../../packages/core/src/readers/SimpleDirectoryReader"; import { PGVectorStore } from "../../../packages/core/src/storage/vectorStore/PGVectorStore"; -// import { storageContextFromDefaults } from '../../../packages/core/src/storage'; - async function getSourceFilenames(sourceDir: string) { return await fs.readdir(sourceDir) @@ -25,7 +21,6 @@ async function main(args: any) { const sourceDir: string = args.length > 2 ? args[2] : '../data'; - // Create Document object with essay console.log(`Finding documents in ${sourceDir}`); const fileList = await getSourceFilenames(sourceDir); const count = fileList.length; @@ -35,24 +30,23 @@ async function main(args: any) { var fileName = ''; try { - // Passing callback fn to the ctor here - // will enable looging to console. - // See callback fn, defined above. - const rdr = new SimpleDirectoryReader(callback); - const docs = await rdr.loadData({ directoryPath: sourceDir }); - - const pgvs = new PGVectorStore(); - pgvs.setCollection(sourceDir); - pgvs.clearCollection(); + // Passing callback fn to the ctor here + // will enable looging to console. + // See callback fn, defined above. + const rdr = new SimpleDirectoryReader(callback); + const docs = await rdr.loadData({ directoryPath: sourceDir }); - const ctx = await storageContextFromDefaults( - { vectorStore: pgvs } - ); + const pgvs = new PGVectorStore(); + pgvs.setCollection(sourceDir); + pgvs.clearCollection(); - console.debug(' - creating vector store'); - const index = await VectorStoreIndex.fromDocuments(docs, { storageContext: ctx }); - console.debug(' - done.'); + const ctx = await storageContextFromDefaults( + { vectorStore: pgvs } + ); + console.debug(' - creating vector store'); + const index = await VectorStoreIndex.fromDocuments(docs, { storageContext: ctx }); + console.debug(' - done.'); } catch (err) { console.error(fileName, err); console.log("If your PGVectorStore init failed, make sure to set env vars for PGUSER or USER, PGHOST, PGPORT and PGPASSWORD as needed.");