متقدم
البحث فيالملخصات
طريقة البحث
  • الكلمات تجد عناوين الدروس والنص والكود. صفِّ حسب النوع (أدلة، دورات، برامج، كتب) أو ابقَ في هذا الكتاب.
  • book:rhcsa و ch:8 يضيّقان البحث؛ ‎#code يجد الكلمة داخل أوامر الدروس فقط.
  • "كلمتان" تجد العبارة بالضبط.
  • الكلمات العادية تجد الأسماء والنصوص والأمثلة.
  • ↑ ↓ للتنقل · Enter للفتح · Esc للإغلاق · / للبحث
جرّب:

الدورة 13الاسم في الدورة: Project: Generative AI Applications with RAG and LangChain

Project: Generative AI Applications with RAG and LangChain

Course 13 of 13 in the IBM AI Engineering certificate. Load documents from different sources with LangChain.

Course page: Coursera

  • 7 الدروس
  • 0 الأوامر
  • 1 المعامل

هذا الجزء لم يُترجم بعد. أنت ترى النص الإنجليزي.

$ مستخدم عادي# بصلاحية root (أو مع sudo)

13.1

What the course covers

Load documents from different sources with LangChain.

  • Split text well, so retrieval returns useful chunks.
  • Embed and store chunks in Chroma and FAISS.
  • Use advanced retrievers, then compare RAG with fine-tuning.
  • Build and submit a QA bot with a Gradio web page.
ModuleMain focus
Module 1 · about 3 hoursDocument Loader Using LangChain: loaders, ingestion best practices, text splitters
Module 2 · about 3 hoursRAG Using LangChain: watsonx embeddings, Chroma and FAISS, retrievers, RAG vs InstructLab
Module 3 · about 4 hoursBuild a QA Bot: Gradio interface, RAG pipeline, final project and submission

13.2

Module 1: Document Loader Using LangChain

Document Loader Using LangChain: loaders, ingestion best practices, text splitters.

  • A document loader reads a source and returns LangChain Document objects: page_content plus metadata.
  • Common sources: PDF, plain text, web pages, CSV, JSON and databases.
  • Good practice: clean the text, keep the source and page in metadata, remove duplicates and check extraction quality.
  • Text splitters cut long text into chunks. chunk_size sets the length and chunk_overlap shares text between neighbours.
  • A recursive splitter tries separators in order: paragraphs, then lines, then words.
  • A token splitter counts tokens, which matches the model limit better than counting characters.
  • Chunks that are too big add noise. Chunks that are too small lose the context of the idea.
  • Put a whole document in the prompt only when it fits the context window and you need the full text.

13.3

Module 2: RAG Using LangChain

RAG Using LangChain: watsonx embeddings, Chroma and FAISS, retrievers, RAG vs InstructLab.

  • The embedding model turns each chunk into a vector. Use the same model for documents and queries.
  • Vector stores keep the vectors and search them: Chroma is a local, persistent store; FAISS is a fast similarity index.
  • A retriever takes a query and returns the top-k documents. It is the bridge between the store and the prompt.
  • Vector store retriever is the basic one: embed the query and find the nearest chunks.
  • Multi-query retriever asks the LLM for several versions of the query, searches with each and merges the results.
  • Self-query retriever turns part of the question into metadata filters, such as a year or a source.
  • Parent document retriever embeds small chunks but returns the larger parent chunk, so the answer has context.
  • RAG vs fine-tuning with InstructLab: RAG is best for facts that change and for citations. Fine-tuning is best for style and behaviour.
  • The two can work together: fine-tune the tone, then add RAG for the facts.

13.4

Module 3: Create a QA Bot to Read Your Document

Build a QA Bot: Gradio interface, RAG pipeline, final project and submission.

  • The project combines loader, splitter, embeddings, vector store, retriever, prompt and LLM into one app.
  • Gradio builds a simple web UI in Python. gr.Interface or gr.ChatInterface plus .launch() is enough.
  • The QA prompt should say: answer only from the context, and say when the answer is not there.
  • A good answer shows the sources. Keep metadata through the pipeline so you can print them.
  • Test with questions that are in the document, and with questions that are not. Check both behaviours.
  • Tune chunk_size, chunk_overlap and k when answers miss the right text.
  • The final project is an app that uses RAG and LangChain to answer questions from a document.
  • Read the submission guidelines page. It lists the deliverables and the exact steps to submit.
  • Restart the kernel and run all cells before you submit, so the notebook works from top to bottom.

13.5

Check yourself

Answer each question first, then open it to check the answer.

1What does a LangChain document loader return? a) Document objects with page_content and metadata · b) A trained model · c) A Gradio app · d) A reward score

a) Document objects with page_content and metadata. A loader returns Documents with text in page_content and details in metadata.

2Which item belongs in document metadata? a) The learning rate · b) The source file and page number · c) The GPU model · d) The number of epochs

b) The source file and page number. Source and page keep a citation, and can be used as filters.

3What do chunk_size and chunk_overlap control? a) The learning rate and epochs · b) The number of vectors in FAISS · c) The size of each piece and how much neighbouring pieces share · d) The Gradio layout

c) The size of each piece and how much neighbouring pieces share. chunk_size sets the piece length; chunk_overlap repeats some text at the edges.

4Why avoid very small chunks? a) The model gets bigger · b) The embedding model stops working · c) Each chunk may lose the context around the idea · d) The GPU runs out of ports

c) Each chunk may lose the context around the idea. A tiny chunk may cut the idea in half, so the retriever brings weak context.

5When can you put a whole document in the prompt? a) Always · b) Never · c) Only for PDFs · d) When it fits inside the model's context window

d) When it fits inside the model's context window. Whole-document prompts only work when the text fits the context window.

6Why use the same embedding model for documents and queries? a) The vectors must live in the same space to compare them · b) It is faster to type · c) It saves disk space only · d) The law requires it

a) The vectors must live in the same space to compare them. Vectors from different models cannot be compared, so one model must do both jobs.

7Which vector store is the local, persistent database used in the labs? a) Gradio · b) Chroma · c) PPO · d) LoRA

b) Chroma. Chroma is the local, persistent vector store used in these labs.

8What does a multi-query retriever do? a) It makes several versions of the query and merges the results · b) It stores the model weights · c) It splits the document · d) It builds the UI

a) It makes several versions of the query and merges the results. The LLM rewrites the question several ways, then the results are combined.

9What does a self-query retriever do? a) It trains the model · b) It turns parts of the question into metadata filters · c) It creates a reward model · d) It quantizes the weights

b) It turns parts of the question into metadata filters. The question mentions a filter, and the retriever applies it to metadata.

10What does a parent document retriever return? a) Only the smallest sentence · b) The whole database · c) The model config · d) A larger parent chunk around the small matched chunk

d) A larger parent chunk around the small matched chunk. Small chunks are embedded, but the larger parent text is returned.

11What is Gradio used for in the project? a) To build a simple web interface for the QA bot · b) To train the LLM · c) To store vectors · d) To split PDFs

a) To build a simple web interface for the QA bot. Gradio gives a quick web page with inputs and outputs, with no front-end code.

12In a RAG QA prompt, what should the model do when the answer is not in the context? a) Invent a confident answer · b) Change the question · c) Say that it does not know from the provided documents · d) Call the tokenizer

c) Say that it does not know from the provided documents. The prompt should tell the model to admit when the context has no answer.

13For facts that change often, which approach is better? a) RAG, because you update the documents, not the weights · b) Fine-tuning, because weights never change · c) Neither works · d) Only Gradio

a) RAG, because you update the documents, not the weights. New facts just need new documents, with no retraining.

13.6

Study and exam tips

Keep the pipeline order: load, split, embed, store, retrieve, prompt, LLM, UI.

  • Link each retriever to its one-line job: basic, multi-query, self-query, parent.
  • Remember why the query and the documents need the same embedding model.
  • Know when RAG is better than fine-tuning, and when fine-tuning wins.
  • Check the project submission list before you submit.

تدرّب