{
  "source_file": "0930",
  "source_platform": "Hashnode",
  "author": "Muhammad Fahad Bashir",
  "title_listed": "7. Guide to the Final Stage: Response Generation in Simple RAG",
  "keywords": "",
  "domain": "大模型算法",
  "content_type": "深度技术解析",
  "is_paid_flag": "否",
  "url": "https://mfahadbashir.hashnode.dev/7-guide-to-the-final-stage-response-generation-in-simple-rag",
  "publish_date": "2024-10-10",
  "corpus_stage": "",
  "notes": "",
  "id": "28da496e0fcb",
  "collected_at": "2026-10-02T14:10:57",
  "title": "7. Guide to the Final Stage: Response Generation in Simple RAG",
  "body_markdown": "7. Guide to the Final Stage: Response Generation in Simple RAG\nPublished\nOctober 10, 2024\n•\n6\nmin read\n•\nView as Markdown\nM\nMuhammad Fahad Bashir\nMachine Learning, Artificial Intelligence\nPart of series\nImplementing RAG systems from Scratch in-depth\nWelcome to the seventh and final article in our series on Retrieval-Augmented Generation (RAG) explaining the stpes. In the previous articles, we thoroughly explored each step of the RAG process, from understanding what RAG is to retrieving data using various techniques.\nHere is the link\n.\nIn this article, we will discuss about the crucial step of response generation, specifically focusing on large language model (LLM) selection and the implementation of chain methods. By the end, you'll have the knowledge to develop a fully functional chatbot application capable of answering queries using the power of LLMs.\nUnderstanding Large Language Models (LLMs)\nLarge language models (LLMs) are advanced AI systems trained on extensive datasets to understand and generate human-like text. Some of the notable LLMs include:\nChatGPT from OpenAI\nLLaMA from Meta\nGemini from Google\nChoosing the Right LLM\nWhen selecting an LLM, several factors must be considered, depending on the nature of your task and budget:\nContext Window:\nThe context window defines how much information the model can consider at once. A larger context window can enhance the model's ability to generate coherent responses.\nNumber of Tokens:\nDifferent LLMs have varying token limits, which can impact how much text can be processed and generated in a single query.\nBusiness Use Case:\nYour choice of LLM should align with the specific task you need to address, such as text generation, sentiment analysis, or translation.\nYou can access LLMs through their official websites or via APIs. For instance, the\nGoogle Gemini API\nallows you to send queries and relevant context to the model for processing. For example here is a comparison according to June 2024. To know more read the\nsource\nMain Concept\nin our previous article\n6. Understanding Retrievers in detail -LangChain Retriever Methods\nwe focused on retrieving data from our documents. Now, we will take the next crucial step: passing this retrieved data to a Large Language Model (LLM) to generate responses. This article will explore how queries, LLMs, and context converge to produce meaningful outputs.\nTheme of Response Generation\nThe central theme of this revolves around the following components:\nQuery:\nThe question posed by the user.\nLLM:\nThe Large Language Model chosen for processing the query.\nContext:\nThe data retrieved from our documents, which provides the necessary background for the LLM.\nFinal Response:\nThe output generated by the LLM based on the input query and context.\nBy understanding how these elements interact, you will be better equipped to implement a functional chatbot application that leverages the capabilities of LLMs.\nyou can choose any llm model based on our preference. Here is the code for last step . we will be using Google Geimini as llm .\n# Install Google Generative AI library\n!pip install google-generativeai\n# Import the library\nimport\ngoogle.generativeai\nas\ngenai\n# Initialize the Google Gemini model\ngenai.configure(api_key=\n\"YOUR API KEY \"\n)\n# Set your API key here\nmodel = genai.GenerativeModel(\n\"gemini-1.5-flash\"\n)\n# Now let's retrieve and combine the context (similar to your earlier steps)\nretrieved_text =\n\"\\n\"\n.join([doc.page_content\nfor\ndoc\nin\nretrieved_docs])\n# Create a question prompt using the context and your query\nprompt =\nf\"Based on the following information, please provide the answer to:\n{query}\n\\n\\n\n{retrieved_text}\n\"\n# Generate an answer using the model (use the generate_content method)\nresponse = model.generate_content(prompt)\n# Output the response generated by Google Gemini\nprint(\n\"LLM Response:\"\n, response.text)\nAll steps together\nBelow are the all steps combined together\n# Required Imports\nimport\ngoogle.generativeai\nas\ngenai\nfrom\nlangchain.document_loaders\nimport\nPyPDFLoader\nfrom\nlangchain.text_splitter\nimport\nRecursiveCharacterTextSplitter\nfrom\nlangchain.vectorstores\nimport\nChroma\nfrom\nlangchain_huggingface\nimport\nHuggingFaceEmbeddings\n# Initialize Google Gemini\ngenai.configure(api_key=\n\"Your api key \"\n)\nmodel = genai.GenerativeModel(\n\"gemini-1.5-flash\"\n)\n# Load and prepare documents\nloader = PyPDFLoader(\n'/content/FYP Report PhysioFlex(25july).pdf'\n)\n# Load your document\ndoc = loader.load()\n# splitting doucment\ntext_splitter = RecursiveCharacterTextSplitter(chunk_size=\n300\n, chunk_overlap=\n50\n)\nsplit_docs = text_splitter.split_documents(doc)\n# making embedding\nembed_model = HuggingFaceEmbeddings(model_name=\n'BAAI/bge-small-en-v1.5'\n)\nvector_store = Chroma.from_documents(split_docs, embed_model)\n# Function to handle conversational context\ndef\nconversational_rag\n(\nquery, chat_history\n):\n# Combine user query with previous chat history\ncombined_input =\n\" \"\n.join(chat_history + [query])\n# Perform similarity search to retrieve relevant documents\nretrieved_docs = vector_store.similarity_search(combined_input, k=\n4\n)\n# Prepare context from retrieved documents\nretrieved_text =\n\"\\n\"\n.join([doc.page_content\nfor\ndoc\nin\nretrieved_docs])\n# Create a prompt for the LLM\nprompt =\nf\"Based on the following context, answer the question:\n{combined_input}\n\\n\\n\n{retrieved_text}\n\"\n# Generate the response\nresponse = model.generate_content(prompt)\n# Append the query and response to chat history\nchat_history.append(query)\n    chat_history.append(response.text)\nreturn\nresponse.text, chat_history\n# Example usage\nchat_history = []\nuser_query =\n\"Ask the quetsion that you want\"\nresponse, chat_history = conversational_rag(user_query, chat_history)\n\nprint(\n\"LLM Response:\"\n, response)\nConcept of chain\nIn the context of Retrieval-Augmented Generation (RAG), the concept of chains is vistal in organizing and streamlining the process of interacting with Large Language Models (LLMs). Chains refer to sequences of operations that involve multiple steps, allowing for efficient handling of tasks such as data retrieval, processing, and response generation.\nChains refer to sequences of calls made to an LLM, a tool, or a data preprocessing step. Different types of chains serve various purposes, including:\nQA Chain:\nFor answering questions directly.\nQA Retriever Chain:\nTo retrieve data and then generate an answer.\nConversational Chain:\nFor maintaining context in ongoing dialogues.\nQA Conversational Retriever Chain:\nA combination that retrieves data while managing conversational context.\nLangChain Expression Language (LCEL)\nThe LangChain Expression Language (LCEL) is an integral part of LangChain, a framework designed to simplify the development of applications that leverage LLMs and other AI tools. LCEL provides a concise syntax for defining chains and workflows, making it easier for developers to specify how data flows through their applications. By utilizing LCEL, developers can create more complex and efficient chains, facilitating seamless interactions between the user inputs, LLMs, and other processing steps. This powerful feature allows for greater flexibility and scalability in building RAG applications, ultimately enhancing their ability to deliver accurate and contextually relevant responses.\nFuture Directions\nAs we look ahead in developing Retrieval-Augmented Generation (RAG) applications, here are some exciting directions to consider:\nExperiment with Different Loaders:\nTry out various data loaders to implement different use cases. This will help improve how our applications work in diverse scenarios.\nIncorporate User Interaction History:\nUtilize user history to make responses more personalized and relevant. This can lead to a better overall experience for users.\nExplore Advanced Methods:\nLook into advanced techniques, such as fine-tuning models or using ensemble methods, to enhance the accuracy of the responses generated.\nAdd a Graphical User Interface (GUI):\nImplement a user-friendly interface using tools like\nStreamlit\nor\nGradio.\nThis will make it easier for users to interact with the application.\nDeploy on Cloud Platforms:\nConsider deploying the application on cloud platforms for better scalability and accessibility, allowing more users to take advantage of RAG technology.\nBy following these directions, we can make our applications more powerful, user-friendly, and innovative in the AI and machine learning landscape.\nConclusion\nResponse generation is the pivotal step in the RAG process, integrating user queries, LLM capabilities, and contextual information to produce insightful answers. By effectively combining these components, you can create a robust chatbot application that utilizes the power of retrieval-augmented generation to meet users' needs.\nThank you for joining us on this journey through the RAG series. We hope this article has provided you with valuable insights into response generation and has equipped you with the knowledge to develop your own intelligent applications.\n#\nrag\n#\ntutorial\n#\nstep-by-step\n#\nllm\n#\nlangchain\n#\ngemini\n#\narticles\n25\nviews",
  "metadata": {
    "title": "7. Guide to the Final Stage: Response Generation in Simple RAG",
    "description": "Learn how to generate responses with large language models, select the right model, and implement chain methods for efficient RAG applications",
    "keywords": null,
    "author": null,
    "og:title": "7. Guide to the Final Stage: Response Generation in Simple RAG",
    "og:description": "Learn how to generate responses with large language models, select the right model, and implement chain methods for efficient RAG applications",
    "og:url": "https://mfahadbashir.hashnode.dev/7-guide-to-the-final-stage-response-generation-in-simple-rag",
    "og:site_name": "Muhammad Fahad Bashir",
    "og:image": "https://cdn.hashnode.com/res/hashnode/image/upload/v1728573287802/4ab9c9d1-31cc-4f1b-8859-9ff531554402.png",
    "og:image:width": "1200",
    "og:image:height": "630",
    "og:type": "article",
    "twitter:card": "summary_large_image",
    "twitter:title": "7. Guide to the Final Stage: Response Generation in Simple RAG",
    "twitter:description": "Learn how to generate responses with large language models, select the right model, and implement chain methods for efficient RAG applications",
    "twitter:image": "https://cdn.hashnode.com/res/hashnode/image/upload/v1728573287802/4ab9c9d1-31cc-4f1b-8859-9ff531554402.png",
    "article:published_time": "2024-10-10T15:16:18.321Z",
    "article:author": "https://mfahadbashir.hashnode.dev/@mfahadbashir"
  },
  "paywall": {
    "listed_paid": false,
    "detected": false,
    "marker": "",
    "public_only": false,
    "policy": "付费/会员内容只保留页面公开可见部分，不绕过付费墙"
  },
  "crawl": {
    "success": true,
    "status_code": 200,
    "error": ""
  },
  "code_blocks": [
    {
      "file": "attachments/28da496e0fcb/code/body-000.txt",
      "chars": 803,
      "language": "text"
    },
    {
      "file": "attachments/28da496e0fcb/code/body-001.txt",
      "chars": 1855,
      "language": "text"
    }
  ],
  "images": [
    {
      "status": "ok",
      "url": "https://cdn.hashnode.com/res/hashnode/image/upload/v1728573287802/4ab9c9d1-31cc-4f1b-8859-9ff531554402.png",
      "bytes": 152192,
      "reused": true,
      "local_path": "attachments/28da496e0fcb/images/000.png",
      "src": "https://cdn.hashnode.com/res/hashnode/image/upload/v1728573287802/4ab9c9d1-31cc-4f1b-8859-9ff531554402.png",
      "alt": "7. Guide to the Final Stage: Response Generation in Simple RAG",
      "downloaded": true
    },
    {
      "status": "ok",
      "url": "https://bs-cms-media-prod.s3.ap-south-1.amazonaws.com/RAG_Pipeline_8133227bef.png",
      "bytes": 18173,
      "reused": true,
      "local_path": "attachments/28da496e0fcb/images/002.png",
      "src": "https://bs-cms-media-prod.s3.ap-south-1.amazonaws.com/RAG_Pipeline_8133227bef.png",
      "alt": "RAG Pipeline: How It Transforms Natural Language Processing",
      "downloaded": true
    },
    {
      "status": "ok",
      "url": "https://cdn.hashnode.com/res/hashnode/image/upload/v1718148154932/f7c11f56-5834-41b6-9530-fd70909a5890.png?auto=compress,format&format=webp",
      "bytes": 174172,
      "reused": true,
      "local_path": "attachments/28da496e0fcb/images/003.png",
      "src": "https://cdn.hashnode.com/res/hashnode/image/upload/v1718148154932/f7c11f56-5834-41b6-9530-fd70909a5890.png?auto=compress,format&format=webp",
      "alt": "",
      "downloaded": true
    }
  ],
  "audio": [],
  "videos": [],
  "links": [
    {
      "href": "https://mfahadbashir.hashnode.dev/7-guide-to-the-final-stage-response-generation-in-simple-rag.md",
      "label": "View as Markdown",
      "type": "a"
    },
    {
      "href": "https://hashnode.com/@mfahadbashir",
      "label": "M",
      "type": "a"
    },
    {
      "href": "https://www.linkedin.com/in/muhammad-fahad-bashir-09568216a/",
      "label": "",
      "type": "a"
    },
    {
      "href": "https://mfahadbashir.hashnode.dev/series/rag-series",
      "label": "Part of series Implementing RAG systems from Scratch in-depth",
      "type": "a"
    },
    {
      "href": "https://mindsdb.com/blog/navigating-the-llm-landscape-a-comparative-analysis-of-leading-large-language-models",
      "label": "source",
      "type": "a"
    },
    {
      "href": "https://mfahadbashir.hashnode.dev/6-understanding-retrievers-in-detail-langchain-retriever-methods",
      "label": "6. Understanding Retrievers in detail -LangChain Retriever Methods",
      "type": "a"
    },
    {
      "href": "https://streamlit.io/",
      "label": "Streamlit",
      "type": "a"
    },
    {
      "href": "https://www.gradio.app/",
      "label": "Gradio.",
      "type": "a"
    },
    {
      "href": "https://mfahadbashir.hashnode.dev/tag/rag",
      "label": "# rag",
      "type": "a"
    },
    {
      "href": "https://mfahadbashir.hashnode.dev/tag/tutorial",
      "label": "# tutorial",
      "type": "a"
    },
    {
      "href": "https://mfahadbashir.hashnode.dev/tag/step-by-step",
      "label": "# step-by-step",
      "type": "a"
    },
    {
      "href": "https://mfahadbashir.hashnode.dev/tag/llm",
      "label": "# llm",
      "type": "a"
    },
    {
      "href": "https://mfahadbashir.hashnode.dev/tag/langchain",
      "label": "# langchain",
      "type": "a"
    },
    {
      "href": "https://mfahadbashir.hashnode.dev/tag/gemini",
      "label": "# gemini",
      "type": "a"
    },
    {
      "href": "https://mfahadbashir.hashnode.dev/tag/articles",
      "label": "# articles",
      "type": "a"
    }
  ],
  "retry_at": "2026-10-03T08:08:19",
  "repair_version": 2,
  "repaired_at": "2026-10-14T10:50:44Z",
  "source_url": "https://mfahadbashir.hashnode.dev/7-guide-to-the-final-stage-response-generation-in-simple-rag",
  "papers": [],
  "actual_video_files": [],
  "body_chars": 9076,
  "expected_body_images": 3,
  "discovery_status": "ok",
  "delivery_status": "ok",
  "missing_reason": "",
  "raw_html": "attachments/28da496e0fcb/pages/source.html",
  "local_html": "attachments/28da496e0fcb/pages/article.html",
  "body_base_path": "attachments/28da496e0fcb/pages/",
  "body_markdown_path": "attachments/28da496e0fcb/pages/article.md"
}