Skip to main content

πŸ“ Retrieval Augmented Generation (RAG)

Description​

< What is it? >​

Retrieval Augmented Generation (RAG) is a technique that combines the power of large language models (LLMs) with external knowledge sources to improve the quality and relevance of generated content. By retrieving relevant information from a knowledge base or database, RAG enhances the model's ability to generate accurate and contextually appropriate responses.

Langchain helps to implement RAG by providing tools and frameworks for integrating LLMs with external data sources, enabling developers to build applications that leverage both the generative capabilities of LLMs and the structured information from databases.

< Main advantages >​

  1. Up-to-date knowledge β€” you don't need to retrain the LLM when documents change.
  2. Private knowledge β€” company documents can be supplied at query time.
  3. Reduced hallucination β€” the model can ground its answer in retrieved documents.
  4. Citations/sources β€” you can tell the user which documents support the answer.

Key points​

Architecture​

RAG architecture​

RAG architecture diagramRAG architecture diagram

Neo4j graph database​

graph db

Comparison​

< Python frameworks for a RAG backend and frontend >​

  • Suggested starting stack: use LlamaIndex + Chainlit for a RAG chatbot. Write the retrieval logic and chat handlers in Python; Chainlit provides the browser interface.

    LayerFrameworkResponsibility
    RAG logicLlamaIndexLoad documents, create indexes, retrieve evidence, and generate answers. Docs
    Backend API, when neededFastAPIExpose the Python application to clients through APIs and WebSockets. Docs
    Chat frontendChainlitProvide a chat UI, stream responses, and display images or other elements through Python code. LlamaIndex integration

    A small application does not need a separate FastAPI service: Chainlit's Python handlers can call LlamaIndex directly. Add FastAPI when other applications also need to call the RAG backend.

    Browser: Chainlit chat UI
    ↕
    Python: Chainlit message handlers
    ↓
    LlamaIndex
    ↓
    Vector database + LLM
  • Frontend choices: both frameworks let you build a browser UI using Python.

    FrameworkWhen to choose itMain tradeoff
    ChainlitA RAG chatbot with streaming responses and supporting documents/imagesFocused on conversational interfaces
    StreamlitA RAG demo or internal dashboard with chat, tables, filters, and chartsLess control over a highly customized chat experience

    Streamlit provides st.chat_input, st.chat_message, and st.write_stream. See the Streamlit API.

  • Agentic RAG: add LangGraph when you need explicit control over branching workflows, state, and repeated tool calls. It handles orchestration; retrieval components and a UI are still needed. See the LangGraph overview.

  • Example β€” images in streamed answers: Chainlit's Image element accepts a URL, local path, or bytes. The backend can send a completed image element separately from streamed text, avoiding image replacement based on incomplete Markdown such as [Figure-1](partial-url. Exact placement between paragraphs still needs application logic. See the Chainlit image documentation.

Video Tutorial​

Reference​