vault-ai README

The project's README from github.com/pashpashpash/vault-ai. As one markdown file: README.md.

OP Vault

OP Vault uses the OP Stack (OpenAI + Pinecone Vector Database) to enable users to upload their own custom knowledgebase files and ask questions about their contents.

vault.pash.city

Screen Shot 2023-04-09 at 1 53 33 AM

With quick setup, you can launch your own version of this Golang server along with a user-friendly React frontend that allows users to ask OpenAI questions about the specific knowledge base provided. The primary focus is on human-readable content like books, letters, and other documents, making it a practical and valuable tool for knowledge extraction and question-answering. You can upload an entire library's worth of books and documents and recieve pointed answers along with the name of the file and specific section within the file that the answer is based on!

Screen Shot 2023-04-17 at 6 23 00 PM

What can you do with OP Vault?

With The Vault, you can:

Manual Dependencies

Setup

Install manual dependencies

  1. Install go:

Follow the go docs here

  1. Install node v19

I recommend installing nvm and using it to install node v19

  1. Install poppler

sudo apt-get install -y poppler-utils on Ubuntu, or brew install poppler on Mac

Set up your API keys and endpoints in the secret folder

  1. Create a new file secret/openai_api_key and paste your OpenAI API key into it:

echo "your_openai_api_key_here" > secret/openai_api_key

  1. Create a new file secret/pinecone_api_key and paste your Pinecone API key into it:

echo "your_pinecone_api_key_here" > secret/pinecone_api_key

When setting up your pinecone index, use a vector size of 1536 and keep all the default settings the same.

  1. Create a new file secret/pinecone_api_endpoint and paste your Pinecone API endpoint into it:

echo "https://example-50709b5.svc.asia-southeast1-gcp.pinecone.io" > secret/pinecone_api_endpoint

Running the development environment

  1. Install javascript package dependencies:

    npm install

  2. Run the golang webserver (default port :8100):

    npm start

  3. In another terminal window, run webpack to compile the js code and create a bundle.js file:

    npm run dev

  4. Visit the local version of the site at http://localhost:8100

Screenshots:

In the example screenshots, I uploaded a couple of books by Plato and some letters by Alexander Hamilton, showcasing the ability of OP Vault to answer questions based on the uploaded content.

Uploading files

Screen Shot 2023-04-17 at 6 16 40 PM Screen Shot 2023-04-17 at 6 17 29 PM

Asking questions

Screen Shot 2023-04-17 at 6 20 25 PM Screen Shot 2023-04-17 at 6 20 58 PM Screen Shot 2023-04-17 at 6 23 00 PM

Under the hood

The golang server uses POST APIs to process incoming uploads and respond to questions:

  1. /upload for uploading files

  2. /api/question for answering questions

All api endpoints are declared in the vault-web-server/main.go file.

Uploading files and processing them into embeddings

The vault-web-server/postapi/fileupload.go file contains the UploadHandler logic for handling incoming uploads on the backend. The UploadHandler function in the postapi package is responsible for handling file uploads (with a maximum total upload size of 300 MB) and processing them into embeddings to store in Pinecone. It accepts PDF, epub, .docx, and plain text files, extracts text from them, and divides the content into chunks. Using OpenAI API, it obtains embeddings for each chunk and upserts (inserts or updates) the embeddings into Pinecone. The function returns a JSON response containing information about the uploaded files and their processing status.

  1. Limit the size of the request body to MAX_TOTAL_UPLOAD_SIZE (300 MB).
  2. Parse the incoming multipart form data with a maximum allowed size of 300 MB.
  3. Initialize response data with fields for successful and failed file uploads.
  4. Iterate over the uploaded files, and for each file: a. Check if the file size is within the allowed limit (MAX_FILE_SIZE, 300 MB). b. Read the file into memory. c. If the file is a PDF, extract the text from it; otherwise, read the contents as plain text. d. Divide the file contents into chunks. e. Use OpenAI API to obtain embeddings for each chunk. f. Upsert (insert or update) the embeddings into Pinecone. g. Update the response data with information about successful and failed uploads.
  5. Return a JSON response containing information about the uploaded files and their processing status.

Storing embeddings into Pinecone db

After getting OpenAI embeddings for each chunk of an uploaded file, the server stores all of the embeddings, along with metadata associated for each embedding in Pinecone DB. The metadata for each embedding is created in the upsertEmbeddingsToPinecone function, with the following keys and values:

This metadata is useful for providing context to the embeddings and is used to display additional information about the matched embeddings when retrieving results from the Pinecone database.

Answering questions

The QuestionHandler function in vault-web-server/postapi/questions.go is responsible for handling all incoming questions. When a question is entered on the frontend and the user presses "search" (or enter), the server uses the OpenAI embeddings API once again to get an embedding for the question (a.k.a. query vector). This query vector is used to query Pinecone db to get the most relevant context for the question. Finally, a prompt is built by packing the most relevant context + the question in a prompt string that adheres to OpenAI token limits (the go tiktoken library is used to estimate token count).

Frontend info

The frontend is built using React.js and less for styling.

Generative question-answering with long-term memory

If you'd like to read more about this topic, I recommend this post from the pinecone blog:

I hope you enjoy it (:

Uploading larger files

I currently have the max individual file size set to 3MB. If you want to increase this limit, edit the MAX_FILE_SIZE and MAX_TOTAL_UPLOAD_SIZE constants in fileupload.go.

Supported Filetypes

PDFs, .txt, .rtf, .docx, .epub, and plaintext.

New Pinecone Free Tier Restrictions

Recently, Pinecone limited the use of namespaces for free tier users. If you're on a newly created free tier, these restrictions will apply to you.

vault-ai (pashpashpash/vault-ai)

Searching Database…

这是什么?OP Vault 让 ChatGPT 能长期记住你自己的文档内容:你上传一个由书籍、信件或 PDF 组成的知识库,然后就能就其中的内容提问,底层由 OpenAI 和 Pinecone 向量数据库驱动。
这是需要注册使用的在线服务,还是我自己搭建运行的东西?你需要自己搭建:快速安装后会启动你自己的 Golang 网页服务器,并带一个易用的 React 前端。另外在 vault.pash.city 也有一个在线实例,你可以先试用看看。
我怎么把它跑起来?创建一个向量维度为 1536 的 Pinecone 索引,把你的密钥放进 secret/openaiapikey、secret/pineconeapikey 和 secret/pineconeapiendpoint,然后运行 npm install 和 npm start——Go 网页服务器会在 :8100 端口启动,npm run dev 则用 webpack 构建前端包。
我需要有自己的 OpenAI 和 Pinecone 账号吗?需要——服务器会调用 OpenAI API 来生成嵌入(embedding)和回答,并把向量存储到你的 Pinecone 索引中,所以你要提供 OpenAI API 密钥以及 Pinecone 的密钥和 endpoint。工具方面,你还需要 npm 和一套 Go 工具链(npm install 会通过 postinstall 脚本顺带编译 Go 服务器)。
上传文件有什么限制?默认单个文件上限为 3MB——可以修改 fileupload.go 中的 MAXFILESIZE 和 MAXTOTALUPLOADSIZE 常量来调高(总上传大小限制为 300MB)。新注册的 Pinecone 免费套餐账号还会受到命名空间(namespace)的限制。
我的文档内容会留在我自己的电脑上吗?不会——上传的文件会被切分成小块,发送到 OpenAI API 生成嵌入(embedding),这些嵌入会连同对应的文本和元数据一起存进你的 Pinecone 索引,所以这两家服务都能看到你的内容。
我可以上传哪些类型的文件?PDF、.txt、.rtf、.docx、.epub 和纯文本。服务器会提取其中的文字,切成小块,为每一块生成一个 OpenAI 嵌入(embedding),然后把它们 upsert 到 Pinecone 里。
自己搭建之前能先试用吗?可以——在 vault.pach.city 有一个在线实例。截图展示了它针对柏拉图的几本书和亚历山大·汉密尔顿的一些信件回答问题的效果。
回答会告诉我是从哪里找到的信息吗?会——你得到的回答会附带文件名,以及文件中回答所依据的具体段落,因为每个存储的文本块都会把文件名和字符位置保存为元数据。
它内部是怎么回答一个问题的?服务器先用 OpenAI API 为你的问题生成嵌入(embedding),用这个查询向量从 Pinecone 中取出最相关的文本块,然后把上下文连同问题一起打包进一个符合 OpenAI token 上限的提示词(prompt),token 数量是用 go tiktoken 库来计算的。
Size
1.5 MB
Stars
3.4K
Updated
CommitJul 08
Age
3y
Commits · 90 Days