in my previous post we had some theory about Retrieval Augmented Generation. Let’s practice a bit. We will use ChatGPT to create RAG prompt and ask a few questions about the previous article:
The chat is under this link
First, I copied the article to chat:
in simple words, not from scientific article. RAG is a prompt. You just look for some content, anywhere around, and use it as prompt. That’s it. RAG. You must find content to improve your own prompt with RAG prompt. You can use text documents, images, websites, PDFs… Anything with textual information. For image, you need to use image-to-text deep learning model to find what is in the image. For text, you can use it directly, just adding it to your prompt. Sometimes a simple RAG prompt is too big. Let’s say we want to ask AI about a book. Obviously, you cannot use the whole book as a prompt. AI will cut or rather truncate it. So, you can cut the book yourself into chunks. Then you have to find the chunk which is most relevant to your question. Lets use Alice in Wonderland. You will cut it into chunks, using some method. We will talk about how to cut later. So, you have asked a question – who smiled in the book. You find a few chunks, where smile was mentioned, use them as RAG prompt, use system prompt which says something like “use this RAG prompt to answer question” and ask your question. In the selected chunks there will be probably Cheshire cat who smiles. AI will happily answer that in the book, Cheshire cat smiles. RAG worked. Now, you can search for relevant chunks in few ways, you can even preprocess each chunk using AI prompt asking to summarize or/and tag the chunk (contextual RAG) You can use different methods to cut text into chunks. Python langchain library, a Swiss Army knife of language model has many methods to cut, search for relevant chunks and lots of other tools. If chapters are well sized, you can use them to cut. I hope you got a good read. Tomorrow I will use this blog post as RAG chunk and ask ChatGPT some tough questions. ai contextual RAG RAG retrieval augmented generation Wawer waves
Then, instructed in one prompt to use it as context. This is system prompt. The name is to clarify only, it’s just part of prompt
The text above is an article. Try to answer questions about this article. If question is not relevant to article say I don’t know
I got a nice answer
Sure! Feel free to ask any questions about the article you provided. If it’s related to the content of the article, I’ll do my best to answer it. If the question isn’t related, I’ll let you know that I don’t have the answer. What’s your first questio
so I asked:
Is there Alice mentioned?
I don’t need complex questions – in system prompt it’s clearly stated, that we talk the article
Yes, Alice is mentioned in the article in the context of the book Alice in Wonderland. The example is used to explain how you can cut a book into chunks and use relevant parts as a prompt when asking a question, such as identifying the character who smiled in the story (which refers to the Cheshire Cat).
great! Let’s ask another one!
Is John Wayne an autor of this article?
Answer is relevant
No, John Wayne is not mentioned as the author of this article. The article does not specify the author at all.
I hope you see how RAG works. Your homework – upload some document add system prompt and ask a question.
in the next article I will show how to program RAG in python in simplest possible way