Fix Hallucination in Retrieval Augmented Generation AI Applications Using Schema and Output Parser
Tech stack used includes LangChain, Pinecone, Typescript, Openai, and Next.js. LangChain is a framework that makes it easier to build scalable AI/LLM apps and chatbots. Pinecone is a vectorstore for storing embeddings and your PDF in text to later retrieve similar docs.
- Clone the repo or download the ZIP
git clone https://github.com/hellokellyworld/RAG-schema-retrieval-automation.git
- Install packages
First run npm install yarn -g to install yarn globally (if you haven't already).
Then run:
yarn install
After installation, you should now see a node_modules folder.
- Set up your
.envfile
- Copy
.env.exampleinto.envYour.envfile should look like this:
OPENAI_API_KEY=
PINECONE_API_KEY=
PINECONE_ENVIRONMENT=
PINECONE_INDEX_NAME=
- Visit openai to retrieve API keys and insert into your
.envfile. - Visit pinecone to create and retrieve your API keys, and also retrieve your environment and index name from the dashboard.
Make sure you set the dimension size to be 1536 and use cosine similarity when creating index in Pinecone.
-
In the
configfolder, replace thePINECONE_NAME_SPACEwith anamespacewhere you'd like to store your embeddings on Pinecone when you runyarn run ingest. This namespace will later be used for queries and retrieval. -
In
utils/makechain.tschain change theQA_PROMPTfor your own usecase. ChangemodelNameinnew OpenAItogpt-4, if you have access togpt-4api. Please verify outside this repo that you have access togpt-4api, otherwise the application will not work. Forgpt-4, you will need to prepay OpenAI before you can activiate it. https://help.openai.com/en/articles/7102672-how-can-i-access-gpt-4
This repo can load multiple PDF files
-
Inside
docsfolder, add your pdf files or folders that contain pdf files. -
Run the script
yarn run ingestto 'ingest' and embed your docs. If you run into errors troubleshoot below. -
Check Pinecone dashboard to verify your namespace and vectors have been added.
Once you've verified that the embeddings and content have been successfully added to your Pinecone, you can build the app yarn build. Then, you can run the app yarn start to launch the local dev environment at http://localhost:8081 in browser, and then define fields to extract.
The source code was inspired by this project here gpt4-pdf-chatbot-langchain.
Frontend of this repo is inspired by langchain-chat-nextjs
The article about this project can be read here Fix Hallucination in Retrieval Augmented Generation AI Applications Using Schema and Output Parser.