12 Tweets 5 reads Jun 05, 2023
It takes 10 lines of code to talk to a PDF using AI with @LangChainAI
Here's the breakdown:
What's going on:
- We load the PDF file
- Split the long document into smaller chunks
- Embed each chunk using OpenAI's ada-002
- Store embeddings into an in-memory Faiss Vector DB
- Ask it questions
All using Langchain's utility methods,
And here's a quick refresher on Embeddings.
LLMs like GPT-4 don't "understand English" in the traditional sense.
They take the input tokens (e.g. English), then turn them into a vector (e.g. a list of numbers)
Specifically, ada returns a 1536-dimension array.
Each input in English gets transformed into a list of 1536 numbers that the machine can "understand".
We store these numbers in a vector DB. In our case, we're using Faiss.
It's hard to visualize, but here's the gist.
Now here's where the magic happens.
When the user asks a question, we use OpenAI's embedding API to first transform the user's question into a series of numbers. Just like we did for the input PDF.
We then use vector algebra to find the most relevant documents in our Database.
By default, most vector DBs will return the top results from a cosine similarity search (i.e. which documents are "closest" to the user's question in this graph?")
The last step is to package & forward this to a LLM like GPT-3.5-turbo.
We combine the user's original question and pass it along with the most similar documents as context.
As a quick reminder, here's the code again.
@LangChainAI does all of this for us.
Obviously this isn't a full app. But that's not the point.
The point is that the "Core AI Functionality" just takes 10 lines of code to implement.
If we wanted to make this a full app, we'll need to:
• Separate the embed & query steps
• Store the embeddings permanently onto disk or cloud
• Create API endpoints
• Create a UI
But the good news? Those are things we already know how to do as developers.
Take this example.
From start to finish, this PDF-to-AI chatbot in NextJS took me about 4 hours to build. It even scrolls to the exact page in the PDF where the AI found the answer.
In summary, utility libraries like Langchain supercharge our capabilities as developers.
"Integrating AI" just became a simple npm install. You don't need to be an expert in Machine Learning to build incredible AI apps anymore.
I'm going to walk through a full-stack AI project (and share its source code) for a more in-depth Langchain + OpenAI guide later.
Follow my acc if this would be interesting to you!
I'll cover more advanced topics like custom chains, agents & tools, and interacting with the web!

Loading suggestions...