Spaces:
Sleeping
Sleeping
github-actions commited on
Commit ·
128a507
1
Parent(s): 9c6d539
Auto deploy from GitHub Actions
Browse files- hf-space/hf-space/.github/workflows/deploy.yml +43 -0
- hf-space/hf-space/.gitignore +5 -0
- hf-space/hf-space/app.py +45 -0
- hf-space/hf-space/config.py +12 -0
- hf-space/hf-space/hf-space/.gitattributes +35 -0
- hf-space/hf-space/hf-space/README.md +14 -0
- hf-space/hf-space/ingest.py +34 -0
- hf-space/hf-space/prompts.py +60 -0
- hf-space/hf-space/rag_pipeline.py +75 -0
- hf-space/hf-space/requirements.txt +10 -0
- hf-space/hf-space/vectorstore/faiss_index/index.faiss +0 -0
- hf-space/hf-space/vectorstore/faiss_index/index.pkl +3 -0
- hf-space/rag_pipeline.py +28 -43
- rag_pipeline.py +28 -43
hf-space/hf-space/.github/workflows/deploy.yml
ADDED
|
@@ -0,0 +1,43 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
name: Deploy to Hugging Face Spaces
|
| 2 |
+
|
| 3 |
+
on:
|
| 4 |
+
push:
|
| 5 |
+
branches:
|
| 6 |
+
- main
|
| 7 |
+
|
| 8 |
+
jobs:
|
| 9 |
+
deploy:
|
| 10 |
+
runs-on: ubuntu-latest
|
| 11 |
+
|
| 12 |
+
env:
|
| 13 |
+
FORCE_JAVASCRIPT_ACTIONS_TO_NODE24: true
|
| 14 |
+
|
| 15 |
+
steps:
|
| 16 |
+
|
| 17 |
+
- name: Checkout Repository
|
| 18 |
+
uses: actions/checkout@v5
|
| 19 |
+
|
| 20 |
+
- name: Deploy to Hugging Face Space
|
| 21 |
+
|
| 22 |
+
env:
|
| 23 |
+
HF_TOKEN: ${{ secrets.HF_TOKEN }}
|
| 24 |
+
|
| 25 |
+
run: |
|
| 26 |
+
|
| 27 |
+
git config --global user.email "github-actions@github.com"
|
| 28 |
+
git config --global user.name "github-actions"
|
| 29 |
+
|
| 30 |
+
git clone https://mdsalmon159:$HF_TOKEN@huggingface.co/spaces/mdsalmon159/hrchatbot hf-space
|
| 31 |
+
|
| 32 |
+
rsync -av \
|
| 33 |
+
--exclude='.git' \
|
| 34 |
+
--exclude='data/manual.pdf' \
|
| 35 |
+
./ hf-space/
|
| 36 |
+
|
| 37 |
+
cd hf-space
|
| 38 |
+
|
| 39 |
+
git add .
|
| 40 |
+
|
| 41 |
+
git commit -m "Auto deploy from GitHub Actions" || echo "No changes to commit"
|
| 42 |
+
|
| 43 |
+
git push
|
hf-space/hf-space/.gitignore
ADDED
|
@@ -0,0 +1,5 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
|
| 2 |
+
__pycache__/
|
| 3 |
+
.ipynb_checkpoints/
|
| 4 |
+
*.pyc
|
| 5 |
+
data/manual.pdf
|
hf-space/hf-space/app.py
ADDED
|
@@ -0,0 +1,45 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
|
| 2 |
+
import gradio as gr
|
| 3 |
+
|
| 4 |
+
from rag_pipeline import ask_question
|
| 5 |
+
|
| 6 |
+
def respond(message, chat_history):
|
| 7 |
+
|
| 8 |
+
bot_message = ask_question(message)
|
| 9 |
+
|
| 10 |
+
chat_history.append(
|
| 11 |
+
(message, bot_message)
|
| 12 |
+
)
|
| 13 |
+
|
| 14 |
+
return "", chat_history
|
| 15 |
+
|
| 16 |
+
with gr.Blocks(theme=gr.themes.Soft()) as demo:
|
| 17 |
+
|
| 18 |
+
gr.Markdown(
|
| 19 |
+
"# Airlines HR Assistant"
|
| 20 |
+
)
|
| 21 |
+
|
| 22 |
+
chatbot = gr.Chatbot(
|
| 23 |
+
height=500
|
| 24 |
+
)
|
| 25 |
+
|
| 26 |
+
msg = gr.Textbox(
|
| 27 |
+
placeholder="Ask HR policy questions..."
|
| 28 |
+
)
|
| 29 |
+
|
| 30 |
+
clear = gr.Button("Clear Chat")
|
| 31 |
+
|
| 32 |
+
msg.submit(
|
| 33 |
+
respond,
|
| 34 |
+
[msg, chatbot],
|
| 35 |
+
[msg, chatbot]
|
| 36 |
+
)
|
| 37 |
+
|
| 38 |
+
clear.click(
|
| 39 |
+
lambda: None,
|
| 40 |
+
None,
|
| 41 |
+
chatbot,
|
| 42 |
+
queue=False
|
| 43 |
+
)
|
| 44 |
+
|
| 45 |
+
demo.launch()
|
hf-space/hf-space/config.py
ADDED
|
@@ -0,0 +1,12 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
|
| 2 |
+
MODEL_NAME = "sentence-transformers/all-MiniLM-L6-v2"
|
| 3 |
+
|
| 4 |
+
LLM_MODEL = "google/flan-t5-base"
|
| 5 |
+
|
| 6 |
+
PDF_PATH = "data/manual.pdf"
|
| 7 |
+
|
| 8 |
+
VECTOR_DB_PATH = "vectorstore/faiss_index"
|
| 9 |
+
|
| 10 |
+
CHUNK_SIZE = 500
|
| 11 |
+
|
| 12 |
+
CHUNK_OVERLAP = 50
|
hf-space/hf-space/hf-space/.gitattributes
ADDED
|
@@ -0,0 +1,35 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
*.7z filter=lfs diff=lfs merge=lfs -text
|
| 2 |
+
*.arrow filter=lfs diff=lfs merge=lfs -text
|
| 3 |
+
*.bin filter=lfs diff=lfs merge=lfs -text
|
| 4 |
+
*.bz2 filter=lfs diff=lfs merge=lfs -text
|
| 5 |
+
*.ckpt filter=lfs diff=lfs merge=lfs -text
|
| 6 |
+
*.ftz filter=lfs diff=lfs merge=lfs -text
|
| 7 |
+
*.gz filter=lfs diff=lfs merge=lfs -text
|
| 8 |
+
*.h5 filter=lfs diff=lfs merge=lfs -text
|
| 9 |
+
*.joblib filter=lfs diff=lfs merge=lfs -text
|
| 10 |
+
*.lfs.* filter=lfs diff=lfs merge=lfs -text
|
| 11 |
+
*.mlmodel filter=lfs diff=lfs merge=lfs -text
|
| 12 |
+
*.model filter=lfs diff=lfs merge=lfs -text
|
| 13 |
+
*.msgpack filter=lfs diff=lfs merge=lfs -text
|
| 14 |
+
*.npy filter=lfs diff=lfs merge=lfs -text
|
| 15 |
+
*.npz filter=lfs diff=lfs merge=lfs -text
|
| 16 |
+
*.onnx filter=lfs diff=lfs merge=lfs -text
|
| 17 |
+
*.ot filter=lfs diff=lfs merge=lfs -text
|
| 18 |
+
*.parquet filter=lfs diff=lfs merge=lfs -text
|
| 19 |
+
*.pb filter=lfs diff=lfs merge=lfs -text
|
| 20 |
+
*.pickle filter=lfs diff=lfs merge=lfs -text
|
| 21 |
+
*.pkl filter=lfs diff=lfs merge=lfs -text
|
| 22 |
+
*.pt filter=lfs diff=lfs merge=lfs -text
|
| 23 |
+
*.pth filter=lfs diff=lfs merge=lfs -text
|
| 24 |
+
*.rar filter=lfs diff=lfs merge=lfs -text
|
| 25 |
+
*.safetensors filter=lfs diff=lfs merge=lfs -text
|
| 26 |
+
saved_model/**/* filter=lfs diff=lfs merge=lfs -text
|
| 27 |
+
*.tar.* filter=lfs diff=lfs merge=lfs -text
|
| 28 |
+
*.tar filter=lfs diff=lfs merge=lfs -text
|
| 29 |
+
*.tflite filter=lfs diff=lfs merge=lfs -text
|
| 30 |
+
*.tgz filter=lfs diff=lfs merge=lfs -text
|
| 31 |
+
*.wasm filter=lfs diff=lfs merge=lfs -text
|
| 32 |
+
*.xz filter=lfs diff=lfs merge=lfs -text
|
| 33 |
+
*.zip filter=lfs diff=lfs merge=lfs -text
|
| 34 |
+
*.zst filter=lfs diff=lfs merge=lfs -text
|
| 35 |
+
*tfevents* filter=lfs diff=lfs merge=lfs -text
|
hf-space/hf-space/hf-space/README.md
ADDED
|
@@ -0,0 +1,14 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
---
|
| 2 |
+
title: Hrchatbot
|
| 3 |
+
emoji: 📈
|
| 4 |
+
colorFrom: blue
|
| 5 |
+
colorTo: purple
|
| 6 |
+
sdk: gradio
|
| 7 |
+
sdk_version: 6.14.0
|
| 8 |
+
python_version: '3.13'
|
| 9 |
+
app_file: app.py
|
| 10 |
+
pinned: false
|
| 11 |
+
license: apache-2.0
|
| 12 |
+
---
|
| 13 |
+
|
| 14 |
+
Check out the configuration reference at https://huggingface.co/docs/hub/spaces-config-reference
|
hf-space/hf-space/ingest.py
ADDED
|
@@ -0,0 +1,34 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
|
| 2 |
+
from langchain_community.document_loaders import PyPDFLoader
|
| 3 |
+
|
| 4 |
+
from langchain.text_splitter import RecursiveCharacterTextSplitter
|
| 5 |
+
|
| 6 |
+
from langchain_community.embeddings import HuggingFaceEmbeddings
|
| 7 |
+
|
| 8 |
+
from langchain_community.vectorstores import FAISS
|
| 9 |
+
|
| 10 |
+
from config import *
|
| 11 |
+
|
| 12 |
+
loader = PyPDFLoader(PDF_PATH)
|
| 13 |
+
|
| 14 |
+
documents = loader.load()
|
| 15 |
+
|
| 16 |
+
splitter = RecursiveCharacterTextSplitter(
|
| 17 |
+
chunk_size=500,
|
| 18 |
+
chunk_overlap=50
|
| 19 |
+
)
|
| 20 |
+
|
| 21 |
+
docs = splitter.split_documents(documents)
|
| 22 |
+
|
| 23 |
+
embeddings = HuggingFaceEmbeddings(
|
| 24 |
+
model_name="sentence-transformers/all-MiniLM-L6-v2"
|
| 25 |
+
)
|
| 26 |
+
|
| 27 |
+
vector_db = FAISS.from_documents(
|
| 28 |
+
docs,
|
| 29 |
+
embeddings
|
| 30 |
+
)
|
| 31 |
+
|
| 32 |
+
vector_db.save_local(VECTOR_DB_PATH)
|
| 33 |
+
|
| 34 |
+
print("Vector DB created successfully")
|
hf-space/hf-space/prompts.py
ADDED
|
@@ -0,0 +1,60 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
|
| 2 |
+
SYSTEM_PROMPT = """
|
| 3 |
+
You are an Airlines HR Assistant AI responsible for answering employee questions based strictly on the official HR handbook and company policy documents.
|
| 4 |
+
|
| 5 |
+
The user input will contain contextual information retrieved from the HR handbook.
|
| 6 |
+
|
| 7 |
+
The context section begins with:
|
| 8 |
+
###Context
|
| 9 |
+
|
| 10 |
+
The employee question begins with:
|
| 11 |
+
###Question
|
| 12 |
+
|
| 13 |
+
Your role is to provide accurate HR support using ONLY the provided handbook information.
|
| 14 |
+
|
| 15 |
+
Instructions:
|
| 16 |
+
|
| 17 |
+
- Answer ONLY using the information present in the provided context.
|
| 18 |
+
- Do NOT use external knowledge.
|
| 19 |
+
- Do NOT make assumptions or generate information not explicitly stated.
|
| 20 |
+
- Do NOT mention the context, document, handbook excerpts, or retrieval process in your response.
|
| 21 |
+
- Keep responses professional, concise, and employee-friendly.
|
| 22 |
+
- Use bullet points when appropriate for clarity.
|
| 23 |
+
- Maintain the tone of an HR representative.
|
| 24 |
+
- Focus strictly on HR and company policy topics including:
|
| 25 |
+
- Leave policies
|
| 26 |
+
- Attendance
|
| 27 |
+
- Payroll
|
| 28 |
+
- Employee conduct
|
| 29 |
+
- Benefits
|
| 30 |
+
- Working hours
|
| 31 |
+
- Travel policy
|
| 32 |
+
- Reimbursements
|
| 33 |
+
- Escalation procedures
|
| 34 |
+
- Employee responsibilities
|
| 35 |
+
- Company procedures
|
| 36 |
+
|
| 37 |
+
Rules for answering:
|
| 38 |
+
|
| 39 |
+
1. If the answer exists in the context:
|
| 40 |
+
- Provide a direct and accurate answer.
|
| 41 |
+
- Keep formatting clean and readable.
|
| 42 |
+
- Avoid unnecessary elaboration.
|
| 43 |
+
|
| 44 |
+
2. If the answer is partially available:
|
| 45 |
+
- Answer only the portion supported by the context.
|
| 46 |
+
|
| 47 |
+
3. If the answer is NOT found in the context:
|
| 48 |
+
Respond EXACTLY with:
|
| 49 |
+
"I couldn't find this information in the handbook."
|
| 50 |
+
|
| 51 |
+
4. Never fabricate policy details.
|
| 52 |
+
|
| 53 |
+
5. Never answer questions unrelated to the HR handbook.
|
| 54 |
+
|
| 55 |
+
6. If the user asks ethical, legal, medical, or safety-sensitive questions not covered in the handbook:
|
| 56 |
+
Respond EXACTLY with:
|
| 57 |
+
"I couldn't find this information in the handbook."
|
| 58 |
+
|
| 59 |
+
Answer carefully and ensure policy accuracy.
|
| 60 |
+
"""
|
hf-space/hf-space/rag_pipeline.py
ADDED
|
@@ -0,0 +1,75 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
|
| 2 |
+
from transformers import pipeline
|
| 3 |
+
|
| 4 |
+
from langchain_community.llms import HuggingFacePipeline
|
| 5 |
+
|
| 6 |
+
from langchain.chains import RetrievalQA
|
| 7 |
+
|
| 8 |
+
from langchain.prompts import PromptTemplate
|
| 9 |
+
|
| 10 |
+
from langchain_community.embeddings import HuggingFaceEmbeddings
|
| 11 |
+
|
| 12 |
+
from langchain_community.vectorstores import FAISS
|
| 13 |
+
|
| 14 |
+
from config import *
|
| 15 |
+
|
| 16 |
+
from prompts import SYSTEM_PROMPT
|
| 17 |
+
|
| 18 |
+
template = """
|
| 19 |
+
{system_prompt}
|
| 20 |
+
|
| 21 |
+
###Context:
|
| 22 |
+
{context}
|
| 23 |
+
|
| 24 |
+
###Question:
|
| 25 |
+
{question}
|
| 26 |
+
|
| 27 |
+
Answer:
|
| 28 |
+
"""
|
| 29 |
+
|
| 30 |
+
PROMPT = PromptTemplate(
|
| 31 |
+
template=template,
|
| 32 |
+
input_variables=["context", "question"],
|
| 33 |
+
partial_variables={
|
| 34 |
+
"system_prompt": SYSTEM_PROMPT
|
| 35 |
+
}
|
| 36 |
+
)
|
| 37 |
+
|
| 38 |
+
embeddings = HuggingFaceEmbeddings(
|
| 39 |
+
model_name="sentence-transformers/all-MiniLM-L6-v2"
|
| 40 |
+
)
|
| 41 |
+
|
| 42 |
+
vector_db = FAISS.load_local(
|
| 43 |
+
VECTOR_DB_PATH,
|
| 44 |
+
embeddings,
|
| 45 |
+
allow_dangerous_deserialization=True
|
| 46 |
+
)
|
| 47 |
+
|
| 48 |
+
retriever = vector_db.as_retriever(
|
| 49 |
+
search_kwargs={"k": 3}
|
| 50 |
+
)
|
| 51 |
+
|
| 52 |
+
pipe = pipeline(
|
| 53 |
+
"text2text-generation",
|
| 54 |
+
model="google/flan-t5-base",
|
| 55 |
+
max_new_tokens=256
|
| 56 |
+
)
|
| 57 |
+
|
| 58 |
+
llm = HuggingFacePipeline(
|
| 59 |
+
pipeline=pipe
|
| 60 |
+
)
|
| 61 |
+
|
| 62 |
+
qa_chain = RetrievalQA.from_chain_type(
|
| 63 |
+
llm=llm,
|
| 64 |
+
retriever=retriever,
|
| 65 |
+
chain_type="stuff",
|
| 66 |
+
chain_type_kwargs={
|
| 67 |
+
"prompt": PROMPT
|
| 68 |
+
}
|
| 69 |
+
)
|
| 70 |
+
|
| 71 |
+
def ask_question(query):
|
| 72 |
+
|
| 73 |
+
response = qa_chain.run(query)
|
| 74 |
+
|
| 75 |
+
return response
|
hf-space/hf-space/requirements.txt
ADDED
|
@@ -0,0 +1,10 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
|
| 2 |
+
gradio
|
| 3 |
+
langchain
|
| 4 |
+
langchain-community
|
| 5 |
+
sentence-transformers
|
| 6 |
+
transformers
|
| 7 |
+
accelerate
|
| 8 |
+
faiss-cpu
|
| 9 |
+
pypdf
|
| 10 |
+
torch
|
hf-space/hf-space/vectorstore/faiss_index/index.faiss
ADDED
|
Binary file (73.8 kB). View file
|
|
|
hf-space/hf-space/vectorstore/faiss_index/index.pkl
ADDED
|
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
version https://git-lfs.github.com/spec/v1
|
| 2 |
+
oid sha256:5873301a1f6aab0cae850445e83a16e71f895000af15ff65d6dd28ed8d9873f4
|
| 3 |
+
size 26589
|
hf-space/rag_pipeline.py
CHANGED
|
@@ -1,12 +1,5 @@
|
|
| 1 |
-
|
| 2 |
from transformers import pipeline
|
| 3 |
|
| 4 |
-
from langchain_community.llms import HuggingFacePipeline
|
| 5 |
-
|
| 6 |
-
from langchain.chains import RetrievalQA
|
| 7 |
-
|
| 8 |
-
from langchain.prompts import PromptTemplate
|
| 9 |
-
|
| 10 |
from langchain_community.embeddings import HuggingFaceEmbeddings
|
| 11 |
|
| 12 |
from langchain_community.vectorstores import FAISS
|
|
@@ -15,61 +8,53 @@ from config import *
|
|
| 15 |
|
| 16 |
from prompts import SYSTEM_PROMPT
|
| 17 |
|
| 18 |
-
|
| 19 |
-
{system_prompt}
|
| 20 |
-
|
| 21 |
-
###Context:
|
| 22 |
-
{context}
|
| 23 |
-
|
| 24 |
-
###Question:
|
| 25 |
-
{question}
|
| 26 |
-
|
| 27 |
-
Answer:
|
| 28 |
-
"""
|
| 29 |
-
|
| 30 |
-
PROMPT = PromptTemplate(
|
| 31 |
-
template=template,
|
| 32 |
-
input_variables=["context", "question"],
|
| 33 |
-
partial_variables={
|
| 34 |
-
"system_prompt": SYSTEM_PROMPT
|
| 35 |
-
}
|
| 36 |
-
)
|
| 37 |
-
|
| 38 |
embeddings = HuggingFaceEmbeddings(
|
| 39 |
-
model_name=
|
| 40 |
)
|
| 41 |
|
|
|
|
| 42 |
vector_db = FAISS.load_local(
|
| 43 |
VECTOR_DB_PATH,
|
| 44 |
embeddings,
|
| 45 |
allow_dangerous_deserialization=True
|
| 46 |
)
|
| 47 |
|
|
|
|
| 48 |
retriever = vector_db.as_retriever(
|
| 49 |
search_kwargs={"k": 3}
|
| 50 |
)
|
| 51 |
|
|
|
|
| 52 |
pipe = pipeline(
|
| 53 |
"text2text-generation",
|
| 54 |
-
model=
|
| 55 |
max_new_tokens=256
|
| 56 |
)
|
| 57 |
|
| 58 |
-
|
| 59 |
-
pipeline=pipe
|
| 60 |
-
)
|
| 61 |
|
| 62 |
-
|
| 63 |
-
|
| 64 |
-
retriever=retriever,
|
| 65 |
-
chain_type="stuff",
|
| 66 |
-
chain_type_kwargs={
|
| 67 |
-
"prompt": PROMPT
|
| 68 |
-
}
|
| 69 |
-
)
|
| 70 |
|
| 71 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 72 |
|
| 73 |
-
|
| 74 |
|
| 75 |
-
return
|
|
|
|
|
|
|
| 1 |
from transformers import pipeline
|
| 2 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 3 |
from langchain_community.embeddings import HuggingFaceEmbeddings
|
| 4 |
|
| 5 |
from langchain_community.vectorstores import FAISS
|
|
|
|
| 8 |
|
| 9 |
from prompts import SYSTEM_PROMPT
|
| 10 |
|
| 11 |
+
# Load embedding model
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 12 |
embeddings = HuggingFaceEmbeddings(
|
| 13 |
+
model_name=MODEL_NAME
|
| 14 |
)
|
| 15 |
|
| 16 |
+
# Load vector database
|
| 17 |
vector_db = FAISS.load_local(
|
| 18 |
VECTOR_DB_PATH,
|
| 19 |
embeddings,
|
| 20 |
allow_dangerous_deserialization=True
|
| 21 |
)
|
| 22 |
|
| 23 |
+
# Create retriever
|
| 24 |
retriever = vector_db.as_retriever(
|
| 25 |
search_kwargs={"k": 3}
|
| 26 |
)
|
| 27 |
|
| 28 |
+
# Load LLM
|
| 29 |
pipe = pipeline(
|
| 30 |
"text2text-generation",
|
| 31 |
+
model=LLM_MODEL,
|
| 32 |
max_new_tokens=256
|
| 33 |
)
|
| 34 |
|
| 35 |
+
def ask_question(query):
|
|
|
|
|
|
|
| 36 |
|
| 37 |
+
# Retrieve relevant docs
|
| 38 |
+
docs = retriever.get_relevant_documents(query)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 39 |
|
| 40 |
+
context = "\n\n".join(
|
| 41 |
+
[doc.page_content for doc in docs]
|
| 42 |
+
)
|
| 43 |
+
|
| 44 |
+
prompt = f"""
|
| 45 |
+
{SYSTEM_PROMPT}
|
| 46 |
+
|
| 47 |
+
###Context:
|
| 48 |
+
{context}
|
| 49 |
+
|
| 50 |
+
###Question:
|
| 51 |
+
{query}
|
| 52 |
+
|
| 53 |
+
Answer:
|
| 54 |
+
"""
|
| 55 |
+
|
| 56 |
+
result = pipe(prompt)
|
| 57 |
|
| 58 |
+
answer = result[0]["generated_text"]
|
| 59 |
|
| 60 |
+
return answer
|
rag_pipeline.py
CHANGED
|
@@ -1,12 +1,5 @@
|
|
| 1 |
-
|
| 2 |
from transformers import pipeline
|
| 3 |
|
| 4 |
-
from langchain_community.llms import HuggingFacePipeline
|
| 5 |
-
|
| 6 |
-
from langchain.chains import RetrievalQA
|
| 7 |
-
|
| 8 |
-
from langchain.prompts import PromptTemplate
|
| 9 |
-
|
| 10 |
from langchain_community.embeddings import HuggingFaceEmbeddings
|
| 11 |
|
| 12 |
from langchain_community.vectorstores import FAISS
|
|
@@ -15,61 +8,53 @@ from config import *
|
|
| 15 |
|
| 16 |
from prompts import SYSTEM_PROMPT
|
| 17 |
|
| 18 |
-
|
| 19 |
-
{system_prompt}
|
| 20 |
-
|
| 21 |
-
###Context:
|
| 22 |
-
{context}
|
| 23 |
-
|
| 24 |
-
###Question:
|
| 25 |
-
{question}
|
| 26 |
-
|
| 27 |
-
Answer:
|
| 28 |
-
"""
|
| 29 |
-
|
| 30 |
-
PROMPT = PromptTemplate(
|
| 31 |
-
template=template,
|
| 32 |
-
input_variables=["context", "question"],
|
| 33 |
-
partial_variables={
|
| 34 |
-
"system_prompt": SYSTEM_PROMPT
|
| 35 |
-
}
|
| 36 |
-
)
|
| 37 |
-
|
| 38 |
embeddings = HuggingFaceEmbeddings(
|
| 39 |
-
model_name=
|
| 40 |
)
|
| 41 |
|
|
|
|
| 42 |
vector_db = FAISS.load_local(
|
| 43 |
VECTOR_DB_PATH,
|
| 44 |
embeddings,
|
| 45 |
allow_dangerous_deserialization=True
|
| 46 |
)
|
| 47 |
|
|
|
|
| 48 |
retriever = vector_db.as_retriever(
|
| 49 |
search_kwargs={"k": 3}
|
| 50 |
)
|
| 51 |
|
|
|
|
| 52 |
pipe = pipeline(
|
| 53 |
"text2text-generation",
|
| 54 |
-
model=
|
| 55 |
max_new_tokens=256
|
| 56 |
)
|
| 57 |
|
| 58 |
-
|
| 59 |
-
pipeline=pipe
|
| 60 |
-
)
|
| 61 |
|
| 62 |
-
|
| 63 |
-
|
| 64 |
-
retriever=retriever,
|
| 65 |
-
chain_type="stuff",
|
| 66 |
-
chain_type_kwargs={
|
| 67 |
-
"prompt": PROMPT
|
| 68 |
-
}
|
| 69 |
-
)
|
| 70 |
|
| 71 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 72 |
|
| 73 |
-
|
| 74 |
|
| 75 |
-
return
|
|
|
|
|
|
|
| 1 |
from transformers import pipeline
|
| 2 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 3 |
from langchain_community.embeddings import HuggingFaceEmbeddings
|
| 4 |
|
| 5 |
from langchain_community.vectorstores import FAISS
|
|
|
|
| 8 |
|
| 9 |
from prompts import SYSTEM_PROMPT
|
| 10 |
|
| 11 |
+
# Load embedding model
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 12 |
embeddings = HuggingFaceEmbeddings(
|
| 13 |
+
model_name=MODEL_NAME
|
| 14 |
)
|
| 15 |
|
| 16 |
+
# Load vector database
|
| 17 |
vector_db = FAISS.load_local(
|
| 18 |
VECTOR_DB_PATH,
|
| 19 |
embeddings,
|
| 20 |
allow_dangerous_deserialization=True
|
| 21 |
)
|
| 22 |
|
| 23 |
+
# Create retriever
|
| 24 |
retriever = vector_db.as_retriever(
|
| 25 |
search_kwargs={"k": 3}
|
| 26 |
)
|
| 27 |
|
| 28 |
+
# Load LLM
|
| 29 |
pipe = pipeline(
|
| 30 |
"text2text-generation",
|
| 31 |
+
model=LLM_MODEL,
|
| 32 |
max_new_tokens=256
|
| 33 |
)
|
| 34 |
|
| 35 |
+
def ask_question(query):
|
|
|
|
|
|
|
| 36 |
|
| 37 |
+
# Retrieve relevant docs
|
| 38 |
+
docs = retriever.get_relevant_documents(query)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 39 |
|
| 40 |
+
context = "\n\n".join(
|
| 41 |
+
[doc.page_content for doc in docs]
|
| 42 |
+
)
|
| 43 |
+
|
| 44 |
+
prompt = f"""
|
| 45 |
+
{SYSTEM_PROMPT}
|
| 46 |
+
|
| 47 |
+
###Context:
|
| 48 |
+
{context}
|
| 49 |
+
|
| 50 |
+
###Question:
|
| 51 |
+
{query}
|
| 52 |
+
|
| 53 |
+
Answer:
|
| 54 |
+
"""
|
| 55 |
+
|
| 56 |
+
result = pipe(prompt)
|
| 57 |
|
| 58 |
+
answer = result[0]["generated_text"]
|
| 59 |
|
| 60 |
+
return answer
|