RAG in Python Made Easy with PostgresML
Key Takeaways
The video demonstrates how to use PostgresML for Retrieval-Augmented Generation (RAG) in Python, leveraging tools like Postgres, PG Vector, and OpenAI API for vector search and large language model chat completion. It provides a step-by-step guide on setting up a PostgresML database, installing necessary extensions, and using the Wikipedia API to retrieve information.
Full Transcript
what is going on guys welcome back in this video today we're going to learn how to easily do retrieval augmented generation in Python using postgress and postgress ml so let us get right into [Music] it all right so we're going to learn how to easily do rack how to easily do retrieval augmented generation in Python today using postgress and postgress ml now postgress ml are sponsoring this video today so you can check them out at the link in the description down below and the goal of this video is to implement and guide you through a basic example to show you a basic mini project of how we can use our own custom information our own custom knowledge base in combination with some llm like uh GPT for example so we can combine the intelligence of GPT gp4 uh with our own knowledge base with custom documents with scrape data with text files PDF files whatever with content in database and then the um large language model can use that knowledge in addition to all the intelligence it has already from the training data that it's trained on that's the basic idea and we're going to see how to do that using post ml in this video today so the first thing we want to do is we want to go to the postgress ml uh website to documentation as I said you will find a link in the description down below and you want to go to your operating system for the installation instructions now for this video I'm not going to show you the process of installing in postgress itself I'm going to only guide you through the postgress ml installation because uh I will expect you to have postgress ml already on your system or to figure out a way to install it yourself because that's not part of this tutorial today uh also the installation is not that difficult so basically what you want to do is you want to scroll down to your operating system in my case this is an auntu based distribution and I have to basically just copy this so you copy um this code here you run it in the terminal this adds the repository to your system then you run this code to install the actual uh post gml package and then you can also run this part here to uh clone PG vector and to install it on your system as well so basically that's it for uh auntu based distributions you copy this you run it you copy this you run it and you copy this you run it and then you're basically done on the operating side of things uh operating system side of things and what we need to do next is we need to set up a database that we want to use and in addition to that database we also need to then install extensions in that database the pgml so the postgress ml extension the uh PG Vector extension and then we also need to install a python package um that connects to the database so this is what we're going to do in this video today that's the basic road map um so the first step is to open up a terminal and to use postgress to create a new database so we're going to call this uh database we're going to use the create DB command um and we're going to call this database PG uh to tutorial uh yeah let's call it just PG tutorial and then we're going to go to a specific file I'm going to use pseudo privileges here so pseudo enim to edit the following file uh in ETC postgress then 14 then Main and then PG hbaa conf we need to add the line that allows us to actually connect to this so we're going to say that um we want to allow for the postgress or what was it pgml tutorial did I call it PG tutorial or pgml tutorial not sure now um but basically to add this line here actually called it PG tutorial so actually let's create another database create DB pgml tutorial um and now we're going to say service postgress restart I'm going to enter my credentials here and restart the postgress database once this is done we can connect to that database using the psql command so we can say connect to the host and now you have to provide your private IP address in my case this is 1 192 168 0164 this is basically what you get when you type an if config um into your terminal or on Windows ip config um but that is just the host this is just my private IP address here I'm going to connect with the username postgress I'm going to connect using a password and I'm going to connect to the DB with a name PG um ml tutorial and the password is postgress and then I'm in the database all I have to do here now is I have to run two commands create extension vector and create extension pgml once this is done I can get out of this database and we can move on with a python code now in my case I'm going to use a Jupiter notebook environment here so Jupiter lap uh you can also use just a basic python file but what we're going to do first is we're going to install a couple of packages I'm going to do this uh in the notebook you can also do that in a terminal using pip or pip 3 um we're going to install the following packages first of all pgml then we're going to also install the open AI package because we're going use open AI for the GPT access uh which is also important let me show you that if you don't have an open AI API key um it makes sense to go to the open AI page to go to the API to uh go to your platform to navigate to the API keys and to create an API key that you can use here um either you have free credit uh credits or you can uh use a couple of sense to to play around with this if you want to but I have my API key here stored in a file um and the third package that we're going to install is the Wikipedia API package just so we can get some Wikipedia information here uh and I'm going to flag this or I'm going to provide the upgrade flag so that we can make sure all of this is oh actually pip install uh all of this is up to date in my case it is and then we can start with the Imports we're going to start by saying from open AI import open AI I'm going to then also say from pgml we want to import two things the collection and the pipeline The Collection is basically a collection of documents and the documents are just text um and then we also want to say that from or we want to import just the Wikipedia API all right so what we want to do now is we want to create a collection of documents and we want to create a pipeline for splitting content and for searching for Content so we're going to say collection is going to be a collection and this collection will have the name uh tutorial collection and it's going to be linked actually I need to do that before this uh we need to Define two things first of all the URL to the database so the pgml database URL this is going to be equal to the following connection string postgress um colon SL SL and then we're going to use postgress as the username postgress is the password and we're going to connect to 192168 0164 my private IP address in your case it's going to be a different one probably Port four uh 5432 the basic postgress Port I think and then it's going to be pgml uh tutorial the database and the open AI API key will be equal to opening the API key file and reading the content so these are the two things that we need the connection string and the API key for chat GPT or for GPT in general um here now for the collection we're going to pass the URL of the database so we're going to pass pgml database URL and then we're going to Define our pipeline our pipeline is going to be a pipeline object and it's going to be called tutorial Pipeline and now we're going to have to add a dictionary here and this dictionary will contain a field text or a key value pair text which points to a uh or a key text that points to a value which is a dictionary which then contains two Fields called splitter which is again pointing to a dictionary and ALS also a field uh semantic search which is pointing to a dictionary and here now we're going to define the model for splitting is going to be recursive cursive character we're going to also pass some parameters these parameters are going to be again a dictionary we want to pass a chunk size so what is the size of one chunk of splitting the content 1,500 is what we're going to go here here uh with here and then we're going to also have a chunk overlap and a chunk overlap will have will be 40 now for the semantic search we're going to use a hugging face model um which is from inflat we're going to use the E5 small model uh yeah this is the model that we use to search the data that is part of our collection so I'm going to close this now and then what we're going to do is we're going to say await and we're going to say collection at pipeline we're going to add our pipeline to the uh collection so once this is done we can now add content to our collection so once we have the pipeline we can add content to our uh collection and this can be done for example with a Wikipedia API I'm going to show you the example and then I'm also going to provide some custom data uh what we can do for example if you want to add certain Wikipedia Pages as part of your knowledge base you can say uh let's call this Wiki API is going to be equal to Wikipedia api. Wikipedia uh we're going to say here tutorial and it's going to be in English language and what we're going to do is we're going to get the page text for the postgress uh Wikipedia entry so we're going to say wiki api. page post like this. text and this gets us basically the text of the respective Wikipedia entry now if we want to add this as an entity as a document uh in our collection we can do that by saying the following we can say uh document is going to be equal to a dictionary we have to provide a unique ID let's call this one here postgress um wiki page and the text is going to be page text and to add this document now we just have to do await collection upsert document oh actually documents we need to pass a collection going to pass a collection with one element the document and then we can run this and now it's part of our knowledge base now what we can also do is we can Define our custom text so for example I can say custom text is going to be equal to a multi-line string here and I can say something uh let's say something imaginary now the price list of the ABC store at pythia interpreted Street 10 for example and then I can say a coffee mug in is um $15 then I can say a shirt is $40 and I can say a keyboard is uh $70 or actually let's go with $120 and then a mouse is going to be I don't know uh $80 like this and uh this is now information that is not part of the internet obviously I made it up right now but this store now has certain pric prices and the model knows about these prices if it looks them up in the database so I can go ahead um actually this is not a plus this is an equal sign I can copy this and I can do the same thing that I did with the Wikipedia page I can do ABC uh prices for example and now I can add custom text and I can add this document as well now I can copy these two cells can paste them and I can do the same thing now with another custom text uh I can call it the XYZ store which is at interpreted Street 12 and it has different prices let's say the coffee Muk is 50 the shirt is 90 the keyboard is 220 and the mouse is let's say this one is cheaper let's say 30 um and then I can say that this now has the ID uh XYZ prices all right so I added a couple of documents to our database and now we can use GPT uh to answer certain prompts now before we use GPT I can show you what happens when we ask for certain uh queries So based on the query the most useful the the most likely to be useful information is gathered from the database so we can have something like a query text um where let's say our question is um what is postgress for example um this information will obviously be part of the Wikipedia page so what we can do is we can do something like results equal await collection Vector search so we perform a vector search on our um database and we have the following query so we have a field query uh which is pointing to a dictionary and this has some fields which again dictionary the structure is a little bit uh unusual here so we have a text and then we have uh again a dictionary and this is the quy and the query is pointing to the query text um now what we can also provide here is a limit let's say we're interested in a maximum of three chunks or three pieces of information here um we can do it like this and then we can also add or we also have to add the pipeline in the end so this will get me results and I can look at these results like this or I can iterate over them which is what I'm going to do here now so I'm going to say for are in results what I want to do is I want to um I want to actually print the chunk and I want to say that I want to have three line breaks so that I can better read this and then you can see I have uh three different chunks here that are interesting to my query now this is not using any large language model yet this is just looking up in the database based on the query what is postgress uh it looks up the pieces of information that are most likely to give me the information I'm looking for if I ask for example now a different query how much are coffee mugs in pythia I will get different results I will get the price list of the XYZ store the price list of the ABC store and then some postgress stuff because I only have two documents related to Coffee Muk so you can see that depending on my query I will get different information and now we can combine that with a chat GPT uh or with a GPT query so I can copy this again here um and then I can say um that I want to initialize a client the client is going to be an open AI object so open AI the API key is going to be equal to the open AI API key that I read from the file then we're going to provide some context the context is going to be uh providing the information that we get from our results so we are going to join the information here so the chunks of information here that we get from the results for R in results uh this is our context we pass this context to the uh neural or to the large language model so that it actually I need to save this uh so that it can actually look it up and then we just use the basic chat completion here so we can say chat completion is equal to client chat completions create and then we're going to provide a message history so messages is going to be equal to a list of stuff that happened so we're going to say that first of all there is the following thing the role is oh now I read randis uh we need to provide a system prompt so the role here is going to be system and the content of the message is just going to be a simple instruction you are a helpful chatbot given the following context answer the users question um question to the best of your abilities and then we can uh say stay brief context um and then we can pass the context and uh maybe we should use two line breaks before that and uh we of course need to make this an F string otherwise it's not going to work here with the context in the end but that is basically our system prompt providing the context and then we can ask uh the question as a user so we can then say we have now a second message here which is going to be our um user so we're going to say roll user is asking now the following question so we have the following content here quy text um so you can see why this already Works basically I'm using the vector search to find information that's necessary or that's that's helpful uh for my query then I pass it to the open AI uh GPT model to see that as information it's basically me putting it there and then it can answer uh based on that information and here we're going to say that the model we want to use is just a simple GPT actually let's use double quotations GPT 3.5 uh turbo and then we can print our chat completion so to actually get the message itself we need to say chat completion choices uh zero and then what was the correct way to address this it was message. content and in this case coffee marck are $50 at the XYZ store in pythona and $15 at the ABC Store in Python then maybe we can also say um what's the only or can I get anything cheaper at XYZ than at ABC and then I will get an answer yes you can get the mouse cheaper at XYZ store $30 compared to ABC Store store $80 now of course I made this list up but you can imagine what you can do with this you can feed in PDF files as long as you can get the text out of them you can take any source of information and add it to your database and now your GPT model is not just um using its own intelligence it uses all the data that you provide it uh with so you can literally build your own knowledge base and then pass it as context to your prompts and this is how easily you can do that with postgress postgress ML and open AI so that's it for today's video I hope you enjoyed it and hope you learned something if so let me know by hitting a like button and leaving a comment in the comment section down below also don't forget to check out post chml at the link in the description down below and besides that uh don't forget to subscribe to this Channel and hit the not notification Bell to not miss a single future video for free other than that thank you much for watching see you in the next video and bye
Original Description
Today we learn how to easily do Retrieval-Augmented Generation (RAG) in Python using PostgresML.
PostgresML: https://rebrand.ly/NeuralNinePostgresML
◾◾◾◾◾◾◾◾◾◾◾◾◾◾◾◾◾
📚 Programming Books & Merch 📚
🐍 The Python Bible Book: https://www.neuralnine.com/books/
💻 The Algorithm Bible Book: https://www.neuralnine.com/books/
👕 Programming Merch: https://www.neuralnine.com/shop
💼 Services 💼
💻 Freelancing & Tutoring: https://www.neuralnine.com/services
🌐 Social Media & Contact 🌐
📱 Website: https://www.neuralnine.com/
📷 Instagram: https://www.instagram.com/neuralnine
🐦 Twitter: https://twitter.com/neuralnine
🤵 LinkedIn: https://www.linkedin.com/company/neuralnine/
📁 GitHub: https://github.com/NeuralNine
🎙 Discord: https://discord.gg/JU4xr8U3dm
Watch on YouTube ↗
(saves to browser)
Sign in to unlock AI tutor explanation · ⚡30
Playlist
Uploads from NeuralNine · NeuralNine · 0 of 60
← Previous
Next →
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
Visualizing Stock Data With Candlestick Charts in Python
NeuralNine
Python Beginner Tutorial #1 - Installation and First Program
NeuralNine
Python Beginner Tutorial #2 - Variables and Data Types
NeuralNine
Python Beginner Tutorial #3 - Operators and User Input
NeuralNine
Python Beginner Tutorial #4 - If Statements and Conditions
NeuralNine
Python Beginner Tutorial #5 - Loops
NeuralNine
Python Beginner Tutorial #6 - Sequences and Collections
NeuralNine
Python Beginner Tutorial #7 - Functions
NeuralNine
Python Beginner Tutorial #8 - Exception Handling
NeuralNine
Python Beginner Tutorial #9 - File Operations
NeuralNine
Python Beginner Tutorial #10 - String Functions
NeuralNine
Python Intermediate Tutorial #1 - Classes and Objects
NeuralNine
Python Intermediate Tutorial #2 - Inheritance
NeuralNine
Python Intermediate Tutorial #3 - Multithreading
NeuralNine
Python Intermediate Tutorial #4 - Synchronizing Threads
NeuralNine
Python Intermediate Tutorial #5 - Events and Daemon Threads
NeuralNine
Python Intermediate Tutorial #6 - Queues
NeuralNine
Python Intermediate Tutorial #7 - Sockets and Network Programming
NeuralNine
Python Intermediate Tutorial #8 - Database Programming
NeuralNine
Python Intermediate Tutorial #9 - Recursion
NeuralNine
Python Intermediate Tutorial #10 - XML Processing
NeuralNine
Python Intermediate Tutorial #11 - Logging
NeuralNine
Python Data Science Tutorial #1 - Anaconda and PyCharm Setup
NeuralNine
Python Data Science Tutorial #2 - NumPy Arrays
NeuralNine
Python Data Science Tutorial #3 - Numpy Functions
NeuralNine
Python Data Science Tutorial #4 - Plotting Functions With Matplotlib
NeuralNine
Python Data Science Tutorial #5 - Subplots and Multiple Windows
NeuralNine
Python Data Science Tutorial #6 - Matplotlib Styling
NeuralNine
Python Data Science Tutorial #7 - Bar Charts with Matplotlib
NeuralNine
Python Data Science Tutorial #8 - Pie Charts with Matplotlib
NeuralNine
Python Data Science Tutorial #9 - Plotting Histograms with Matplotlib
NeuralNine
Python Data Science Tutorial #10 - Scatter Plots with Matplotlib
NeuralNine
Python Data Science Tutorial #11 - 3D Plotting with Matplotlib
NeuralNine
Python Data Science Tutorial #12 - Pandas Series
NeuralNine
Python Data Science Tutorial #13 - Pandas Data Frames
NeuralNine
Python Data Science Tutorial #14 - Pandas Statistics
NeuralNine
Python Data Science Tutorial #15 - Pandas Sorting and Functions
NeuralNine
Python Data Science Tutorial #16 - Pandas Merging Data Frames
NeuralNine
Python Data Science Tutorial #17 - Pandas Queries
NeuralNine
Python Machine Learning Tutorial #1 - What is Machine Learning?
NeuralNine
Python Machine Learning Tutorial #2 - Linear Regression
NeuralNine
Python Machine Learning Tutorial #3 - K-Nearest Neighbors Classification
NeuralNine
Python Machine Learning #4 - Support Vector Machines
NeuralNine
Python Machine Learning Tutorial #5 - Decision Trees and Random Forest Classification
NeuralNine
Python Machine Learning Tutorial #6 - K-Means Clustering
NeuralNine
Python Machine Learning Tutorial #7 - Neural Networks
NeuralNine
Python Machine Learning Tutorial #8 - Handwritten Digit Recognition with Tensorflow
NeuralNine
Generating Poetic Texts with Recurrent Neural Networks in Python
NeuralNine
Stock Portfolio Visualization with Matplotlib in Python
NeuralNine
Analyzing Coronavirus with Python (COVID-19)
NeuralNine
Making Text Images Readable Again with Python and OpenCV
NeuralNine
Neural Networks Simply Explained (Theory)
NeuralNine
Motion Filtering with OpenCV in Python
NeuralNine
Top 5 Programming Languages To Learn in 2020
NeuralNine
Simple TCP Chat Room in Python
NeuralNine
Image Classification with Neural Networks in Python
NeuralNine
Edge Detection with OpenCV in Python
NeuralNine
S&P 500 Web Scraping with Python
NeuralNine
Simple Sentiment Text Analysis in Python
NeuralNine
Introduction - Algorithms & Data Structures #1
NeuralNine
More on: RAG Basics
View skill →
🎓
Tutor Explanation
DeepCamp AI