add missing fiel
Browse files- rag_core/business.py +35 -0
rag_core/business.py
ADDED
|
@@ -0,0 +1,35 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import os
|
| 2 |
+
import logging
|
| 3 |
+
from rag_core.chunker import chunk_legal_text
|
| 4 |
+
from rag_core.embedder import get_embedding
|
| 5 |
+
from rag_core.retriever import Retriever
|
| 6 |
+
from rag_core.llm import generate_answer
|
| 7 |
+
|
| 8 |
+
retriever = Retriever()
|
| 9 |
+
ready = retriever.index is not None
|
| 10 |
+
|
| 11 |
+
def build_index():
|
| 12 |
+
global ready
|
| 13 |
+
logging.info("🔄 Bắt đầu xây FAISS index từ đầu...")
|
| 14 |
+
with open("data/raw_law.txt", "r", encoding="utf-8") as f:
|
| 15 |
+
text = f.read()
|
| 16 |
+
chunks = chunk_legal_text(text)
|
| 17 |
+
retriever.build(chunks, get_embedding)
|
| 18 |
+
ready = True
|
| 19 |
+
|
| 20 |
+
def answer_query(query):
|
| 21 |
+
if not ready:
|
| 22 |
+
return {"error": "Index chưa sẵn sàng. Vui lòng bấm 'Xây Index' hoặc gọi API rescan."}
|
| 23 |
+
docs = retriever.query(query, get_embedding)
|
| 24 |
+
prompt = "\n\n".join(docs) + f"\n\nCâu hỏi: {query}\nTrả lời:"
|
| 25 |
+
answer = generate_answer(prompt)
|
| 26 |
+
return {"answer": answer}
|
| 27 |
+
|
| 28 |
+
def rescan_index():
|
| 29 |
+
if not ready:
|
| 30 |
+
return {"error": "Index chưa sẵn sàng."}
|
| 31 |
+
with open("data/raw_law.txt", "r", encoding="utf-8") as f:
|
| 32 |
+
text = f.read()
|
| 33 |
+
chunks = chunk_legal_text(text)
|
| 34 |
+
retriever.rescan_and_append(chunks, get_embedding)
|
| 35 |
+
return {"status": "Rescan & update thành công."}
|