02. Naver News Agency QA (Question-Answer)
Understanding RAG basic structure
One. Pre-processing -1~4 steps
In the pre-work phase, we proceed to a four-step that loads-split-embedding-save documents to the Vector DB (storage) of the data source.
1st level document load: brings up the document content.
2nd stage split (Text Split): Split documents to specific criteria (Chunk).
Step 3 Embedding: Embed and save the split (Chunk).
Step 4 Save VectorDB: Save embedded Chunk to DB.
2. RAG performance (RunTime) -5~8 steps
5-step searcher (Retriever): Based on Query, search in DB to define the retriever to get results. Retriever is a search algorithm (Dense, Sparse) and is divided into retrievers.
Dense : Similarity based search (FAISS, DPR)
Sparse : Keyword-based search (BM25, TF-IDF)
6-step prompt: Generates a prompt to perform RAG. In the prompt context, the content retrieved from the document is entered. You can format the answer through prompt engineering.
Step 7 LLM: Define the model (GPT-3.5, GPT-4, Claude, etc..)
Step 8 Chain: Create a chain leading to the prompt - LLM - output.
Preferences
Set API KEY.
Copy
Copy
Applications built with LangChain will use LLM calls multiple times over multiple stages. As these applications become more and more complex, the ability to investigate exactly what is happening inside a chain or agent becomes very important. The best way to do this LangSmith Is to use.
LangSmith is not required, but useful. If you want to use LangSmith, after signing up from the link above, you need to set an environment variable to start logging tracking.
Copy
Copy
Naver News-based QA (Question-Answering) chatbot
In this tutorial, you can ask about the content of Naver News Agency. News Agency QA App Will build.
In this guide, we will use OpenAI chat models, embedding, and Chroma vector stores.
First, you can implement a simple indexing pipeline and RAG chain with about 20 lines of code through the following process. library
bs4Is a library for parsing web pages.langchainIs a library that provides a variety of features related to AI, especially text splitting hereRecursiveCharacterTextSplitter), document loading (WebBaseLoader), vector storage (Chroma,FAISS), output parsing (StrOutputParser), a viable pass-through (RunnablePassthrough) Etc.langchain_openaiOpenAI's chatbot through modules (ChatOpenAI) And embedding (OpenAIEmbeddings) You can use the function.
Copy
Copy
After loading the contents of the web page, dividing the text into chunks, indexing it, we implement the process of creating new content by searching for related text snippets.
WebBaseLoader to parse only what is needed on the specified web page bs4.SoupStrainer Use.
[Note]
bs4.SoupStrainerConveniently allows you to get the elements you want from the web.
(Example)
Copy
Copy
Copy
Copy
RecursiveCharacterTextSplitter Divide the document into the specified size of chunks.
Copy
Copy
FAISS hump Chroma Vectorstore like this creates a vector representation of the document based on these chunks.
Copy
vectorstore.as_retriever() The finder generated through hub.pull With the prompt brought ChatOpenAI Use the model to create new content.
Finally, StrOutputParser Parse the generated result into a string.
Copy
in hub teddynote/rag-prompt-korean You can download and enter the prompt. In this case, a separate prompting process is omitted.
Copy
Copy
For streaming output stream_response Use.
Copy
Copy
Copy
Copy
Copy
Copy
Copy
Copy
Copy
Last updated