{"id":25868412,"url":"https://github.com/md-emon-hasan/retrieval-augmented-generation-rag","last_synced_at":"2025-03-02T04:38:06.580Z","repository":{"id":277559130,"uuid":"932806333","full_name":"Md-Emon-Hasan/Retrieval-Augmented-Generation-RAG","owner":"Md-Emon-Hasan","description":"RAG enhances LLMs by retrieving relevant external knowledge before generating responses, improving accuracy and reducing hallucinations.","archived":false,"fork":false,"pushed_at":"2025-02-14T15:13:20.000Z","size":583,"stargazers_count":1,"open_issues_count":0,"forks_count":0,"subscribers_count":1,"default_branch":"master","last_synced_at":"2025-02-14T16:22:36.500Z","etag":null,"topics":["ai-chatbot","chromadb","custom-llm","document-retrieval","embedding-models","faiss","huggingface-rag","knowledge-augmented-llm","knowledge-graph","langchain-rag","llm-applications","llm-retrieval","multi-modal-rag","prompt-engineering","rag-pipeline","retrieval-augmented-generation","retrieval-qa","semantic-search","text-embedding","vector-search"],"latest_commit_sha":null,"homepage":"","language":"Jupyter Notebook","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/Md-Emon-Hasan.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null}},"created_at":"2025-02-14T14:58:40.000Z","updated_at":"2025-02-14T15:20:50.000Z","dependencies_parsed_at":"2025-02-14T16:32:43.586Z","dependency_job_id":null,"html_url":"https://github.com/Md-Emon-Hasan/Retrieval-Augmented-Generation-RAG","commit_stats":null,"previous_names":["md-emon-hasan/retrieval-augmented-generation-rag-"],"tags_count":0,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/Md-Emon-Hasan%2FRetrieval-Augmented-Generation-RAG","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/Md-Emon-Hasan%2FRetrieval-Augmented-Generation-RAG/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/Md-Emon-Hasan%2FRetrieval-Augmented-Generation-RAG/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/Md-Emon-Hasan%2FRetrieval-Augmented-Generation-RAG/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/Md-Emon-Hasan","download_url":"https://codeload.github.com/Md-Emon-Hasan/Retrieval-Augmented-Generation-RAG/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":241460064,"owners_count":19966511,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["ai-chatbot","chromadb","custom-llm","document-retrieval","embedding-models","faiss","huggingface-rag","knowledge-augmented-llm","knowledge-graph","langchain-rag","llm-applications","llm-retrieval","multi-modal-rag","prompt-engineering","rag-pipeline","retrieval-augmented-generation","retrieval-qa","semantic-search","text-embedding","vector-search"],"created_at":"2025-03-02T04:38:06.041Z","updated_at":"2025-03-02T04:38:06.575Z","avatar_url":"https://github.com/Md-Emon-Hasan.png","language":"Jupyter Notebook","funding_links":[],"categories":[],"sub_categories":[],"readme":"# Retrieval-Augmented Generation (RAG)\n![Image](https://github.com/user-attachments/assets/c689213c-b822-44bd-bfbb-2cd2a8dffb6a)\n\n## Overview\nRetrieval-Augmented Generation (RAG) is an advanced approach that enhances large language models (LLMs) by retrieving relevant information from external knowledge sources before generating responses. This technique improves factual accuracy, reduces hallucinations, and enables dynamic knowledge updates without retraining the model.\n\n## Key Components\n1. **Retriever**: Searches and fetches relevant documents from a knowledge base.\n2. **Embedder**: Converts text into dense vector representations for efficient retrieval.\n3. **Vector Store**: Stores and indexes embeddings for fast similarity search.\n4. **Generator (LLM)**: Generates responses based on retrieved documents.\n5. **Pipeline**: Integrates all components to process user queries efficiently.\n\n## Implementation Steps\n### 1. Environment Setup\n```bash\npip install langchain transformers faiss-cpu sentence-transformers chromadb\n```\n\n### 2. Load and Preprocess Documents\n```python\nfrom langchain.document_loaders import TextLoader\nfrom langchain.text_splitter import RecursiveCharacterTextSplitter\n\n# Load documents\ndoc_loader = TextLoader(\"data/documents.txt\")\ndocuments = doc_loader.load()\n\n# Split text into smaller chunks\ntext_splitter = RecursiveCharacterTextSplitter(chunk_size=500, chunk_overlap=100)\ndocument_chunks = text_splitter.split_documents(documents)\n```\n\n### 3. Create Embeddings and Store in Vector Database\n```python\nfrom langchain.embeddings import HuggingFaceEmbeddings\nfrom langchain.vectorstores import FAISS\n\n# Initialize embedding model\nembedding_model = HuggingFaceEmbeddings(model_name=\"sentence-transformers/all-MiniLM-L6-v2\")\n\n# Store embeddings in FAISS vector database\nvector_store = FAISS.from_documents(document_chunks, embedding_model)\n```\n\n### 4. Build the RAG Pipeline\n```python\nfrom langchain.chains import RetrievalQAWithSourcesChain\nfrom langchain.llms import HuggingFacePipeline\nfrom transformers import pipeline, AutoModelForCausalLM, AutoTokenizer\n\n# Load LLM\nmodel_name = \"distilgpt2\"\ntokenizer = AutoTokenizer.from_pretrained(model_name)\nmodel = AutoModelForCausalLM.from_pretrained(model_name)\n\n# Create pipeline for text generation\nhf_pipeline = pipeline(\"text-generation\", model=model, tokenizer=tokenizer)\nllm = HuggingFacePipeline(pipeline=hf_pipeline, max_new_tokens=150)\n\n# Initialize retrieval-based QA system\nqa_chain = RetrievalQAWithSourcesChain.from_llm(llm=llm, retriever=vector_store.as_retriever())\n```\n\n### 5. Query the RAG System\n```python\nquery = \"What is Retrieval-Augmented Generation?\"\nresponse = qa_chain({\"question\": query}, return_only_outputs=True)\nprint(\"Answer:\", response['answer'])\n```\n\n## Deployment Considerations\n- **Scalability**: Use **Weaviate, Pinecone, or ChromaDB** for large-scale vector storage.\n- **Latency Optimization**: Use optimized embedding models like **BGE-M3** or **FAISS-HNSW**.\n- **Fine-Tuning**: Adapt the LLM to domain-specific knowledge.\n- **API Integration**: Deploy using **FastAPI** or **Flask** for production use.\n\n## Conclusion\nRetrieval-Augmented Generation significantly improves LLM performance by incorporating external knowledge retrieval. Implementing RAG with LangChain and Hugging Face provides a powerful framework for knowledge-grounded AI applications.\n\n---\n### Contact Information\n- **Email:** [iconicemon01@gmail.com](mailto:iconicemon01@gmail.com)\n- **WhatsApp:** [+8801834363533](https://wa.me/8801834363533)\n- **GitHub:** [Md-Emon-Hasan](https://github.com/Md-Emon-Hasan)\n- **LinkedIn:** [Md Emon Hasan](https://www.linkedin.com/in/md-emon-hasan)\n- **Facebook:** [Md Emon Hasan](https://www.facebook.com/mdemon.hasan2001/)\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fmd-emon-hasan%2Fretrieval-augmented-generation-rag","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fmd-emon-hasan%2Fretrieval-augmented-generation-rag","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fmd-emon-hasan%2Fretrieval-augmented-generation-rag/lists"}