-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.py
210 lines (182 loc) · 7.28 KB
/
app.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
import os
import time
import io
import asyncio
import logging
from concurrent.futures import ThreadPoolExecutor
from fastapi import FastAPI, File, UploadFile, HTTPException
from fastapi.middleware.cors import CORSMiddleware
from pydantic import BaseModel
from typing import List
from langchain.embeddings.base import Embeddings
from langchain.text_splitter import RecursiveCharacterTextSplitter
from sentence_transformers import SentenceTransformer
from langchain.vectorstores import FAISS
from langchain.prompts import PromptTemplate
from langchain.chains import LLMChain
from langchain_google_genai import ChatGoogleGenerativeAI
from PyPDF2 import PdfReader
from rank_bm25 import BM25Okapi
import numpy as np
# Configure logging
logging.basicConfig(level=logging.INFO)
logger = logging.getLogger(__name__)
# Define custom embedding class
class STEmbeddings(Embeddings):
def __init__(self, model_name="thenlper/gte-large"):
self.model = SentenceTransformer(model_name)
def embed_documents(self, texts):
embeddings = self.model.encode(texts)
return embeddings.tolist()
def embed_query(self, text):
embedding = self.model.encode(text)
return embedding.tolist()
# Initialize embeddings
embeddings = STEmbeddings()
def split_paragraphs(raw_text):
"""
Splits text into semantically meaningful chunks using RecursiveCharacterTextSplitter.
"""
text_splitter = RecursiveCharacterTextSplitter(
chunk_size=1000,
chunk_overlap=200,
separators=["\n\n", "\n", ".", "!", "?"],
length_function=len
)
return text_splitter.split_text(raw_text)
def load_pdfs_with_metadata(uploaded_files, doc_type="generic"):
"""
Process uploaded PDFs and split their text into chunks.
"""
text_chunks = []
metadata = []
lock = ThreadPoolExecutor()
def process_pdf(uploaded_file):
try:
reader = PdfReader(uploaded_file)
for page_num, page in enumerate(reader.pages, start=1):
raw_text = page.extract_text()
if not raw_text.strip():
continue
chunks = split_paragraphs(raw_text)
with lock:
text_chunks.extend(chunks)
metadata.extend([{"doc_type": doc_type, "page_number": page_num}] * len(chunks))
except Exception as e:
logger.error(f"Error reading file: {e}")
with ThreadPoolExecutor() as executor:
executor.map(process_pdf, uploaded_files)
return text_chunks, metadata
def retrieval_result(query, path, top_k=5):
"""
Retrieve relevant documents from the FAISS vector store.
"""
try:
store = FAISS.load_local(path, embeddings=embeddings, allow_dangerous_deserialization=True)
retriever = store.as_retriever()
retrieved_docs = retriever.get_relevant_documents(query)[:top_k]
if not retrieved_docs:
return "No relevant information found.", ""
context = "\n\n".join(doc.page_content for doc in retrieved_docs)
return context
except Exception as e:
logger.error(f"Error during retrieval: {e}")
return "", ""
def answer_query(query, context):
"""
Answer a query using a language model with the given context.
"""
try:
api_key = os.getenv("GOOGLE_API_KEY") # Replace "default-key" with actual default or raise error
llm = ChatGoogleGenerativeAI(
model="gemini-1.5-pro",
temperature=0,
google_api_key=api_key
)
prompt_template = PromptTemplate(
input_variables=["context", "query"],
template=(
"Bạn là một giảng viên các môn chính trị, triết học tại Việt Nam. Dựa vào thông tin sau để trả lời câu hỏi của sinh viên:\n\n"
"Chỉ cần đưa ra nguyên văn câu trả lời đúng, không trả lời gì thêm.Trả lời bằng tiếng Việt.\n\n"
"Nếu học viên hỏi những câu không liên quan đến các môn chính trị, hãy trả lời 'Tôi chỉ trả lời câu hỏi lien quan đến các môn chính trị được dạy tại UIT.'\n\n"
"Context:\n{context}\n\n"
"Question:\n{query}\n\n"
"Answer:"
)
)
chain = LLMChain(llm=llm, prompt=prompt_template)
response = chain.invoke({"context": context, "query": query})
return response["text"]
except Exception as e:
logger.error(f"Error during query answering: {e}")
return "Error generating answer."
def rerank_with_bm25(query, contexts, top_k=3):
"""
Rerank contexts using BM25 scoring
"""
# Split contexts into sentences/passages
contexts = [c.strip() for c in contexts.split('\n\n') if c.strip()]
# Tokenize contexts
tokenized_contexts = [doc.lower().split() for doc in contexts]
# Create BM25 model
bm25 = BM25Okapi(tokenized_contexts)
# Get BM25 scores
tokenized_query = query.lower().split()
doc_scores = bm25.get_scores(tokenized_query)
# Get top-k indices
top_indices = np.argsort(doc_scores)[-top_k:][::-1]
# Return reranked contexts
reranked_contexts = [contexts[i] for i in top_indices]
return '\n\n'.join(reranked_contexts)
# Initialize FastAPI
app = FastAPI()
# Add CORS middleware
app.add_middleware(
CORSMiddleware,
allow_origins=["*"],
allow_credentials=True,
allow_methods=["*"],
allow_headers=["*"],
)
# Request/Response models
class QuestionRequest(BaseModel):
query: str
use_external_knowledge: bool = False
class AnswerResponse(BaseModel):
answer: str
inference_time: float
# API Endpoints
@app.post("/upload")
async def upload_file(file: UploadFile = File(...)):
try:
os.makedirs('./external_knowledge', exist_ok=True)
contents = await file.read()
pdf_file = io.BytesIO(contents)
text_chunks, metadata = load_pdfs_with_metadata([pdf_file])
store = FAISS.from_texts(text_chunks, embeddings, metadatas=metadata)
store.save_local('./external_knowledge')
pdf_file.close()
return {"message": "File uploaded and processed successfully"}
except Exception as e:
logger.error(f"Error during file upload: {e}")
raise HTTPException(status_code=400, detail=str(e))
@app.post("/answer", response_model=AnswerResponse)
async def get_answer(request: QuestionRequest):
start_time = time.time()
try:
context = retrieval_result(request.query, './vectorstore')
if request.use_external_knowledge:
external_context = retrieval_result(request.query, './external_knowledge')
all_context = context + "\n\n" + external_context
reranked_context = rerank_with_bm25(request.query, all_context)
answer = answer_query(request.query, reranked_context)
else :
answer = answer_query(request.query, context)
inference_time = time.time() - start_time
return AnswerResponse(answer=answer, inference_time=inference_time)
except Exception as e:
logger.error(f"Error during query answering: {e}")
raise HTTPException(status_code=500, detail=str(e))
if __name__ == "__main__":
import uvicorn
uvicorn.run(app, host="0.0.0.0", port=8000)