hrshihab/ByteCode_RAG_System
0
1# -*- coding: utf-8 -*-2"""ByteCode RAG System with LangChain + Chroma + Gemma 2B (Quantized).ipynb3 4Automatically generated by Colab.5 6Original file is located at7 https://colab.research.google.com/drive/1oI4ou4NLuiP4KFc2UZJak8VXzKAXt62_8"""9 10 11 12# Import Libraries13import os14from huggingface_hub import login15from langchain_community.vectorstores import Chroma16from langchain_community.embeddings import HuggingFaceEmbeddings17from langchain.llms import HuggingFacePipeline18from transformers import AutoTokenizer, AutoModelForCausalLM, pipeline19from langchain.chains import RetrievalQA20from langchain.prompts import PromptTemplate21from langchain.text_splitter import CharacterTextSplitter22from langchain.schema import Document23from transformers import BitsAndBytesConfig24 25 26 27# ByteCode Data (as context)28bytecode_info = """29You are a ByteCode helpful GenZ AI assistant. Be concise, friendly, and practical.30 31Company Name: ByteCode Limited32Website: https://bytecodeltd.com/33 34Overview:35ByteCode Limited is a dynamic software development company that delivers cutting-edge custom software solutions. With over a decade of experience, ByteCode builds powerful web and mobile applications that help organizations gain a competitive edge in the digital world.36 37Mission:38Listening to you, and answering with cutting-edge software engineering solutions.39 40Core Values:41- Quality over everything: We never compromise on quality.42- Innovation in every Byte: We develop with unique and modern perspectives.43- Timely and accurate delivery: Sharp execution with proactive communication.44- Long-term client relationships: We always provide after-sale support and technical help.45- Friendly, efficient team environment: Collaborative, skilled, and highly motivated.46 47What Makes ByteCode Different:48- Flawless and proactive communication with clients.49- Cost-effective, world-class technology.50- Professional after-sales support.51- Friendly and productive work culture.52- Dedicated QA and testing for every product.53- Top-tier technical talent for high-performing solutions.54 55Services We Provide:561. Software Development572. Web Application Development583. Mobile Application Development594. Quality Assurance60 61Technologies We Use:62- Backend: ASP.NET, C#.NET, Node.js, Python63- Frontend: React.JS, Angular64- Mobile: Android (Native), iOS (Native), React Native65 66Development Process:671. Requirement Analysis682. Prototype Design693. Client Feedback & Revisions704. Final Development715. QA Testing726. Deployment & Support73 74Team ByteCode:75- A friendly, skilled, and experienced team76- Works collaboratively with clients77- Prioritizes your satisfaction — “We work until you're happy.”78 79Why Choose ByteCode:80- Innovation: Unique ideas for the best user experiences81- Standard: Eliminating imperfections for top-quality output82- Teamwork: Clients and developers work hand-in-hand83- Service: Strong, ongoing client relationships84 85Employee/Developer Information:861. Rahat Morshed Nabil | +8801909993446 | imrmnabil@gmail.com | Khulna University872. Md. Masrafi Bin Seraj Sakib | +8801886420246 | masrafi190116@gmail.com | Jashore University of Science and Technology883. Asif Mehedi Haris | 01753584194 | asifmehedi11@gmail.com | Khulna University894. Rabiul Islam Rabi | 01608077170 | rabiulrabi.cse@gmail.com | Khulna University905. Nishat Jahan Tandra | 01613915286 | nishattandra2001@gmail.com | Jashore University of Science and Technology916. Masum Billa | 01971636762 | masumbilla190101@gmail.com | Jashore University of Science and Technology927. Safkat Mahmud Sakib | 01629313026 | safkatmahmudsakib@gmail.com | American International University-Bangladesh938. Habibur Rahman Shihab | 01316944878 | hrshihab10@gmail.com | Khulna University94 95Contact Information:96Phone: +88 0222 447 0613, +88 01936 444 55597Email: info@bytecodeltd.com98Address: House # 19 (1st Floor), Road # 20, Sector # 13, Uttara, Dhaka 123099 100Company Pages:101- Home102- About103- Services104- Contact105 106Newsletter:107Stay updated with the latest tech tips and company news by subscribing via email.108 109Slogan:110"Innovation in every Byte."111"""112 113# Split into chunks114splitter = CharacterTextSplitter(chunk_size=500, chunk_overlap=50)115docs = splitter.split_text(bytecode_info)116documents = [Document(page_content=text) for text in docs]117 118# Create Embedding Model119embedding_model = HuggingFaceEmbeddings(model_name="sentence-transformers/all-MiniLM-L6-v2")120 121# Create Chroma Vector DB122db = Chroma.from_documents(documents, embedding_model, persist_directory="./bytecode_db")123 124# Config for 4-bit quantization125quant_config = BitsAndBytesConfig(126 load_in_4bit=True,127 bnb_4bit_compute_dtype="float16",128 bnb_4bit_use_double_quant=True,129 bnb_4bit_quant_type="nf4"130)131 132# Load Gemma 2B (Quantized) with config133model_name = "google/gemma-2b-it"134model = AutoModelForCausalLM.from_pretrained(135 model_name,136 device_map="auto",137 quantization_config=quant_config,138 token=os.getenv("HF_TOKEN")139 140)141 142tokenizer = AutoTokenizer.from_pretrained(model_name, token=os.getenv("HF_TOKEN")143)144 145 146# Create LLM Pipeline147text_gen_pipeline = pipeline(148 "text-generation",149 model=model,150 tokenizer=tokenizer,151 max_new_tokens=300,152 temperature=0.2,153 repetition_penalty=1.1154)155 156llm = HuggingFacePipeline(pipeline=text_gen_pipeline)157 158# Create Prompt Template159prompt_template = PromptTemplate(160 input_variables=["context", "question"],161 template="""162Answer the question based only on the following company information.163If not available, reply 'Sorry, not found.'164 165Company Info:166{context}167 168Question: {question}169 170Answer:171"""172)173 174# Create Retrieval QA Chain175qa_chain = RetrievalQA.from_chain_type(176 llm=llm,177 retriever=db.as_retriever(),178 chain_type="stuff",179 chain_type_kwargs={"prompt": prompt_template},180 return_source_documents=True181)182 183# import re184 185# # Test Query186# user_question = "all employee name"187 188# # Query invoke189# result = qa_chain.invoke({"query": user_question})190# raw_answer = result["result"]191 192# # Final answer193# match = re.search(r"Answer:\s*(.*)", raw_answer, re.DOTALL)194# if match:195# final_answer = match.group(1).strip()196# else:197# final_answer = raw_answer.strip()198 199# # Show200# print("📝 User Question:", user_question)201# print("✅ Answer:", final_answer)202 203import re204import gradio as gr205 206# Function to process query and return clean answer207def get_answer(user_question):208 result = qa_chain.invoke({"query": user_question})209 raw_answer = result["result"]210 211 # Clean only the final answer part212 match = re.search(r"Answer:\s*(.*)", raw_answer, re.DOTALL)213 if match:214 final_answer = match.group(1).strip()215 else:216 final_answer = raw_answer.strip()217 218 return final_answer219 220# Gradio Interface221iface = gr.Interface(222 fn=get_answer,223 inputs=gr.Textbox(label="Ask your question to ByteCode Assistant 👇"),224 outputs=gr.Textbox(label="📢 Answer"),225 title="📱 ByteCode AI Assistant",226 description="Ask anything about ByteCode Limited — employee info, services, or company details."227)228 229# Launch UI230iface.launch()