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
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
# -*- coding: utf-8 -*-
|
| 2 |
+
"""ByteCode RAG System with LangChain + Chroma + Gemma 2B (Quantized).ipynb
|
| 3 |
+
|
| 4 |
+
Automatically generated by Colab.
|
| 5 |
+
|
| 6 |
+
Original file is located at
|
| 7 |
+
https://colab.research.google.com/drive/1oI4ou4NLuiP4KFc2UZJak8VXzKAXt62_
|
| 8 |
+
"""
|
| 9 |
+
|
| 10 |
+
# Install Required Libraries
|
| 11 |
+
!pip install langchain transformers bitsandbytes accelerate chromadb sentence-transformers
|
| 12 |
+
|
| 13 |
+
# Import Libraries
|
| 14 |
+
from langchain.vectorstores import Chroma
|
| 15 |
+
from langchain.embeddings import HuggingFaceEmbeddings
|
| 16 |
+
from langchain.llms import HuggingFacePipeline
|
| 17 |
+
from transformers import AutoTokenizer, AutoModelForCausalLM, pipeline
|
| 18 |
+
from langchain.chains import RetrievalQA
|
| 19 |
+
from langchain.prompts import PromptTemplate
|
| 20 |
+
from langchain.text_splitter import CharacterTextSplitter
|
| 21 |
+
from langchain.schema import Document
|
| 22 |
+
from transformers import BitsAndBytesConfig
|
| 23 |
+
|
| 24 |
+
# ByteCode Data (as context)
|
| 25 |
+
bytecode_info = """
|
| 26 |
+
You are a ByteCode helpful GenZ AI assistant. Be concise, friendly, and practical.
|
| 27 |
+
|
| 28 |
+
Company Name: ByteCode Limited
|
| 29 |
+
Website: https://bytecodeltd.com/
|
| 30 |
+
|
| 31 |
+
Overview:
|
| 32 |
+
ByteCode 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.
|
| 33 |
+
|
| 34 |
+
Mission:
|
| 35 |
+
Listening to you, and answering with cutting-edge software engineering solutions.
|
| 36 |
+
|
| 37 |
+
Core Values:
|
| 38 |
+
- Quality over everything: We never compromise on quality.
|
| 39 |
+
- Innovation in every Byte: We develop with unique and modern perspectives.
|
| 40 |
+
- Timely and accurate delivery: Sharp execution with proactive communication.
|
| 41 |
+
- Long-term client relationships: We always provide after-sale support and technical help.
|
| 42 |
+
- Friendly, efficient team environment: Collaborative, skilled, and highly motivated.
|
| 43 |
+
|
| 44 |
+
What Makes ByteCode Different:
|
| 45 |
+
- Flawless and proactive communication with clients.
|
| 46 |
+
- Cost-effective, world-class technology.
|
| 47 |
+
- Professional after-sales support.
|
| 48 |
+
- Friendly and productive work culture.
|
| 49 |
+
- Dedicated QA and testing for every product.
|
| 50 |
+
- Top-tier technical talent for high-performing solutions.
|
| 51 |
+
|
| 52 |
+
Services We Provide:
|
| 53 |
+
1. Software Development
|
| 54 |
+
2. Web Application Development
|
| 55 |
+
3. Mobile Application Development
|
| 56 |
+
4. Quality Assurance
|
| 57 |
+
|
| 58 |
+
Technologies We Use:
|
| 59 |
+
- Backend: ASP.NET, C#.NET, Node.js, Python
|
| 60 |
+
- Frontend: React.JS, Angular
|
| 61 |
+
- Mobile: Android (Native), iOS (Native), React Native
|
| 62 |
+
|
| 63 |
+
Development Process:
|
| 64 |
+
1. Requirement Analysis
|
| 65 |
+
2. Prototype Design
|
| 66 |
+
3. Client Feedback & Revisions
|
| 67 |
+
4. Final Development
|
| 68 |
+
5. QA Testing
|
| 69 |
+
6. Deployment & Support
|
| 70 |
+
|
| 71 |
+
Team ByteCode:
|
| 72 |
+
- A friendly, skilled, and experienced team
|
| 73 |
+
- Works collaboratively with clients
|
| 74 |
+
- Prioritizes your satisfaction β βWe work until you're happy.β
|
| 75 |
+
|
| 76 |
+
Why Choose ByteCode:
|
| 77 |
+
- Innovation: Unique ideas for the best user experiences
|
| 78 |
+
- Standard: Eliminating imperfections for top-quality output
|
| 79 |
+
- Teamwork: Clients and developers work hand-in-hand
|
| 80 |
+
- Service: Strong, ongoing client relationships
|
| 81 |
+
|
| 82 |
+
Employee/Developer Information:
|
| 83 |
+
1. Rahat Morshed Nabil | +8801909993446 | imrmnabil@gmail.com | Khulna University
|
| 84 |
+
2. Md. Masrafi Bin Seraj Sakib | +8801886420246 | masrafi190116@gmail.com | Jashore University of Science and Technology
|
| 85 |
+
3. Asif Mehedi Haris | 01753584194 | asifmehedi11@gmail.com | Khulna University
|
| 86 |
+
4. Rabiul Islam Rabi | 01608077170 | rabiulrabi.cse@gmail.com | Khulna University
|
| 87 |
+
5. Nishat Jahan Tandra | 01613915286 | nishattandra2001@gmail.com | Jashore University of Science and Technology
|
| 88 |
+
6. Masum Billa | 01971636762 | masumbilla190101@gmail.com | Jashore University of Science and Technology
|
| 89 |
+
7. Safkat Mahmud Sakib | 01629313026 | safkatmahmudsakib@gmail.com | American International University-Bangladesh
|
| 90 |
+
8. Habibur Rahman Shihab | 01316944878 | hrshihab10@gmail.com | Khulna University
|
| 91 |
+
|
| 92 |
+
Contact Information:
|
| 93 |
+
Phone: +88 0222 447 0613, +88 01936 444 555
|
| 94 |
+
Email: info@bytecodeltd.com
|
| 95 |
+
Address: House # 19 (1st Floor), Road # 20, Sector # 13, Uttara, Dhaka 1230
|
| 96 |
+
|
| 97 |
+
Company Pages:
|
| 98 |
+
- Home
|
| 99 |
+
- About
|
| 100 |
+
- Services
|
| 101 |
+
- Contact
|
| 102 |
+
|
| 103 |
+
Newsletter:
|
| 104 |
+
Stay updated with the latest tech tips and company news by subscribing via email.
|
| 105 |
+
|
| 106 |
+
Slogan:
|
| 107 |
+
"Innovation in every Byte."
|
| 108 |
+
"""
|
| 109 |
+
|
| 110 |
+
# Split into chunks
|
| 111 |
+
splitter = CharacterTextSplitter(chunk_size=500, chunk_overlap=50)
|
| 112 |
+
docs = splitter.split_text(bytecode_info)
|
| 113 |
+
documents = [Document(page_content=text) for text in docs]
|
| 114 |
+
|
| 115 |
+
# Create Embedding Model
|
| 116 |
+
embedding_model = HuggingFaceEmbeddings(model_name="sentence-transformers/all-MiniLM-L6-v2")
|
| 117 |
+
|
| 118 |
+
# Create Chroma Vector DB
|
| 119 |
+
db = Chroma.from_documents(documents, embedding_model, persist_directory="./bytecode_db")
|
| 120 |
+
|
| 121 |
+
# Config for 4-bit quantization
|
| 122 |
+
quant_config = BitsAndBytesConfig(
|
| 123 |
+
load_in_4bit=True,
|
| 124 |
+
bnb_4bit_compute_dtype="float16",
|
| 125 |
+
bnb_4bit_use_double_quant=True,
|
| 126 |
+
bnb_4bit_quant_type="nf4"
|
| 127 |
+
)
|
| 128 |
+
|
| 129 |
+
# Load Gemma 2B (Quantized) with config
|
| 130 |
+
model = AutoModelForCausalLM.from_pretrained(
|
| 131 |
+
model_name,
|
| 132 |
+
device_map="auto",
|
| 133 |
+
quantization_config=quant_config
|
| 134 |
+
)
|
| 135 |
+
|
| 136 |
+
# Create LLM Pipeline
|
| 137 |
+
text_gen_pipeline = pipeline(
|
| 138 |
+
"text-generation",
|
| 139 |
+
model=model,
|
| 140 |
+
tokenizer=tokenizer,
|
| 141 |
+
max_new_tokens=300,
|
| 142 |
+
temperature=0.2,
|
| 143 |
+
repetition_penalty=1.1
|
| 144 |
+
)
|
| 145 |
+
|
| 146 |
+
llm = HuggingFacePipeline(pipeline=text_gen_pipeline)
|
| 147 |
+
|
| 148 |
+
# Create Prompt Template
|
| 149 |
+
prompt_template = PromptTemplate(
|
| 150 |
+
input_variables=["context", "question"],
|
| 151 |
+
template="""
|
| 152 |
+
Answer the question based only on the following company information.
|
| 153 |
+
If not available, reply 'Sorry, not found.'
|
| 154 |
+
|
| 155 |
+
Company Info:
|
| 156 |
+
{context}
|
| 157 |
+
|
| 158 |
+
Question: {question}
|
| 159 |
+
|
| 160 |
+
Answer:
|
| 161 |
+
"""
|
| 162 |
+
)
|
| 163 |
+
|
| 164 |
+
# Create Retrieval QA Chain
|
| 165 |
+
qa_chain = RetrievalQA.from_chain_type(
|
| 166 |
+
llm=llm,
|
| 167 |
+
retriever=db.as_retriever(),
|
| 168 |
+
chain_type="stuff",
|
| 169 |
+
chain_type_kwargs={"prompt": prompt_template},
|
| 170 |
+
return_source_documents=True
|
| 171 |
+
)
|
| 172 |
+
|
| 173 |
+
# import re
|
| 174 |
+
|
| 175 |
+
# # Test Query
|
| 176 |
+
# user_question = "all employee name"
|
| 177 |
+
|
| 178 |
+
# # Query invoke
|
| 179 |
+
# result = qa_chain.invoke({"query": user_question})
|
| 180 |
+
# raw_answer = result["result"]
|
| 181 |
+
|
| 182 |
+
# # Final answer
|
| 183 |
+
# match = re.search(r"Answer:\s*(.*)", raw_answer, re.DOTALL)
|
| 184 |
+
# if match:
|
| 185 |
+
# final_answer = match.group(1).strip()
|
| 186 |
+
# else:
|
| 187 |
+
# final_answer = raw_answer.strip()
|
| 188 |
+
|
| 189 |
+
# # Show
|
| 190 |
+
# print("π User Question:", user_question)
|
| 191 |
+
# print("β
Answer:", final_answer)
|
| 192 |
+
|
| 193 |
+
import re
|
| 194 |
+
import gradio as gr
|
| 195 |
+
|
| 196 |
+
# Function to process query and return clean answer
|
| 197 |
+
def get_answer(user_question):
|
| 198 |
+
result = qa_chain.invoke({"query": user_question})
|
| 199 |
+
raw_answer = result["result"]
|
| 200 |
+
|
| 201 |
+
# Clean only the final answer part
|
| 202 |
+
match = re.search(r"Answer:\s*(.*)", raw_answer, re.DOTALL)
|
| 203 |
+
if match:
|
| 204 |
+
final_answer = match.group(1).strip()
|
| 205 |
+
else:
|
| 206 |
+
final_answer = raw_answer.strip()
|
| 207 |
+
|
| 208 |
+
return final_answer
|
| 209 |
+
|
| 210 |
+
# Gradio Interface
|
| 211 |
+
iface = gr.Interface(
|
| 212 |
+
fn=get_answer,
|
| 213 |
+
inputs=gr.Textbox(label="Ask your question to ByteCode Assistant π"),
|
| 214 |
+
outputs=gr.Textbox(label="π’ Answer"),
|
| 215 |
+
title="π± ByteCode AI Assistant",
|
| 216 |
+
description="Ask anything about ByteCode Limited β employee info, services, or company details."
|
| 217 |
+
)
|
| 218 |
+
|
| 219 |
+
# Launch UI
|
| 220 |
+
iface.launch()
|
requirements.txt
ADDED
|
File without changes
|