Update rag_pipeline.py
Browse files- rag_pipeline.py +300 -111
rag_pipeline.py
CHANGED
|
@@ -1,37 +1,41 @@
|
|
| 1 |
# rag_pipeline.py
|
| 2 |
-
import os, json, re, shutil
|
| 3 |
from typing import List, Dict, Tuple
|
| 4 |
from functools import lru_cache
|
| 5 |
|
| 6 |
import faiss
|
| 7 |
import numpy as np
|
| 8 |
-
from huggingface_hub import hf_hub_download
|
| 9 |
|
| 10 |
from providers import embed, generate, rerank, qa_extract
|
| 11 |
|
| 12 |
# =========================
|
| 13 |
-
#
|
| 14 |
# =========================
|
| 15 |
|
| 16 |
-
VSTORE_DIR
|
| 17 |
-
FAISS_FILE
|
| 18 |
-
META_JSONL
|
|
|
|
| 19 |
|
| 20 |
-
|
| 21 |
-
|
| 22 |
-
|
| 23 |
-
HIGH_SCORE_THRES = 0.78
|
| 24 |
-
MARGIN_THRES = 0.06
|
| 25 |
|
| 26 |
-
|
| 27 |
-
|
| 28 |
-
|
|
|
|
| 29 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 30 |
W_TITLE_BOOST = 0.25
|
| 31 |
-
W_LEXICAL
|
| 32 |
|
| 33 |
# =========================
|
| 34 |
-
#
|
| 35 |
# =========================
|
| 36 |
|
| 37 |
DATE_RX = re.compile(
|
|
@@ -40,119 +44,192 @@ DATE_RX = re.compile(
|
|
| 40 |
r"|\d{4})\b",
|
| 41 |
flags=re.IGNORECASE,
|
| 42 |
)
|
| 43 |
-
|
| 44 |
-
|
| 45 |
-
FOUND_KEYS = ["kuruldu", "kuruluş", "kurulmuştur", "kuruluş tarihi"]
|
| 46 |
-
|
| 47 |
-
# =========================
|
| 48 |
-
# Yardımcı fonksiyonlar
|
| 49 |
-
# =========================
|
| 50 |
-
|
| 51 |
-
def _detect_repo_id() -> str:
|
| 52 |
-
"""Space repo kimliğini otomatik bulur."""
|
| 53 |
-
for key in ("SPACE_ID", "HF_SPACE_REPO_ID", "HF_REPO_ID"):
|
| 54 |
-
v = os.getenv(key)
|
| 55 |
-
if v:
|
| 56 |
-
return v
|
| 57 |
-
return ""
|
| 58 |
-
|
| 59 |
|
| 60 |
def _split_sentences(txt: str) -> List[str]:
|
| 61 |
parts = re.split(r"(?<=[.!?])\s+", (txt or "").strip())
|
| 62 |
return [p.strip() for p in parts if p.strip()]
|
| 63 |
|
| 64 |
-
|
| 65 |
def _extract_fact_sentence(query: str, hits: List[Dict]) -> Tuple[str, str]:
|
| 66 |
-
"""
|
|
|
|
|
|
|
|
|
|
|
|
|
| 67 |
q = (query or "").lower()
|
| 68 |
if "ne zaman" not in q:
|
| 69 |
return "", ""
|
| 70 |
|
| 71 |
-
if any(k in q for k in
|
| 72 |
keylist = DEATH_KEYS
|
| 73 |
-
elif any(k in q for k in
|
| 74 |
keylist = FOUND_KEYS
|
| 75 |
else:
|
| 76 |
keylist = DEATH_KEYS + FOUND_KEYS
|
| 77 |
|
| 78 |
for h in hits:
|
| 79 |
-
|
| 80 |
-
for s in sents:
|
| 81 |
if any(k in s.lower() for k in keylist) and DATE_RX.search(s):
|
| 82 |
return s, h.get("source", "")
|
| 83 |
return "", ""
|
| 84 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 85 |
|
| 86 |
# =========================
|
| 87 |
-
# Vectorstore
|
| 88 |
# =========================
|
| 89 |
|
| 90 |
-
def
|
| 91 |
-
|
| 92 |
-
meta_file = os.path.join(vstore_dir, "meta.jsonl")
|
| 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 |
-
os.makedirs(vstore_dir, exist_ok=True)
|
| 122 |
-
shutil.copy2(local_meta, meta_file)
|
| 123 |
-
|
| 124 |
-
except Exception as e:
|
| 125 |
raise FileNotFoundError(
|
| 126 |
-
"
|
| 127 |
-
"
|
| 128 |
-
"veya Settings > Variables kısmına 'HF_SPACE_REPO_ID' ekleyin.\n"
|
| 129 |
-
f"Hata ayrıntısı: {e}"
|
| 130 |
)
|
| 131 |
|
| 132 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 133 |
raise FileNotFoundError(
|
| 134 |
-
"Vektör deposu bulunamadı. 'vectorstore/index.faiss'
|
|
|
|
| 135 |
)
|
| 136 |
|
| 137 |
-
index = faiss.read_index(
|
|
|
|
|
|
|
| 138 |
try:
|
| 139 |
-
|
|
|
|
|
|
|
|
|
|
| 140 |
except Exception:
|
| 141 |
pass
|
| 142 |
|
|
|
|
| 143 |
records: List[Dict] = []
|
| 144 |
-
with
|
| 145 |
for line in f:
|
|
|
|
|
|
|
| 146 |
obj = json.loads(line)
|
| 147 |
-
records.append({
|
|
|
|
|
|
|
|
|
|
| 148 |
|
| 149 |
if not records:
|
| 150 |
-
raise RuntimeError("meta.jsonl boş görünüyor
|
|
|
|
| 151 |
return index, records
|
| 152 |
|
| 153 |
|
| 154 |
# =========================
|
| 155 |
-
#
|
| 156 |
# =========================
|
| 157 |
|
| 158 |
_CAP_WORD = re.compile(r"\b([A-ZÇĞİIÖŞÜ][a-zçğıiöşü]+(?:\s+[A-ZÇĞİIÖŞÜ][a-zçğıiöşü]+)*)\b")
|
|
@@ -163,69 +240,137 @@ def _keywords_from_query(q: str) -> List[str]:
|
|
| 163 |
nums = re.findall(r"\b\d{3,4}\b", q)
|
| 164 |
base = re.findall(r"[A-Za-zÇĞİIÖŞÜçğıiöşü]+", q)
|
| 165 |
base = [w.lower() for w in base if len(w) > 2]
|
|
|
|
| 166 |
return list(dict.fromkeys(caps + nums + base))
|
| 167 |
|
| 168 |
-
|
| 169 |
def _lexical_overlap(q_tokens: List[str], text: str) -> float:
|
| 170 |
toks = re.findall(r"[A-Za-zÇĞİIÖŞÜçğıiöşü]+", (text or "").lower())
|
| 171 |
if not toks:
|
| 172 |
return 0.0
|
| 173 |
qset = set([t for t in q_tokens if len(t) > 2])
|
| 174 |
tset = set([t for t in toks if len(t) > 2])
|
| 175 |
-
|
|
|
|
|
|
|
| 176 |
|
| 177 |
|
|
|
|
|
|
|
|
|
|
|
|
|
| 178 |
@lru_cache(maxsize=256)
|
| 179 |
def _cached_query_vec(e5_query: str) -> np.ndarray:
|
|
|
|
| 180 |
v = embed([e5_query]).astype("float32")
|
| 181 |
return v
|
| 182 |
|
| 183 |
-
|
| 184 |
-
|
| 185 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 186 |
q = (query or "").strip()
|
| 187 |
q_e5 = "query: " + q
|
| 188 |
q_vec = _cached_query_vec(q_e5)
|
| 189 |
faiss.normalize_L2(q_vec)
|
| 190 |
|
| 191 |
scores, idxs = index.search(q_vec, fetch_k)
|
| 192 |
-
|
|
|
|
| 193 |
for i, s in zip(idxs[0], scores[0]):
|
| 194 |
if 0 <= i < len(records):
|
| 195 |
-
md = records[i]["metadata"]
|
| 196 |
pool.append({
|
| 197 |
"text": records[i]["text"],
|
| 198 |
"title": md.get("title", ""),
|
| 199 |
"source": md.get("source", ""),
|
| 200 |
-
"score_vec": float(s)
|
| 201 |
})
|
| 202 |
if not pool:
|
| 203 |
return []
|
| 204 |
|
|
|
|
| 205 |
q_tokens = _keywords_from_query(q)
|
|
|
|
| 206 |
for p in pool:
|
| 207 |
-
|
| 208 |
-
|
| 209 |
-
|
| 210 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 211 |
|
| 212 |
-
|
| 213 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 214 |
|
| 215 |
|
| 216 |
# =========================
|
| 217 |
-
# Nihai cevap
|
| 218 |
# =========================
|
| 219 |
|
| 220 |
-
def generate_answer(
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 221 |
hits = search_chunks(query, index, records, top_k=top_k)
|
| 222 |
if not hits:
|
| 223 |
return "Bilgi bulunamadı."
|
| 224 |
|
|
|
|
| 225 |
rule_sent, rule_src = _extract_fact_sentence(query, hits)
|
| 226 |
if rule_sent:
|
| 227 |
-
return f"{rule_sent}\n\nKaynaklar:\n- {rule_src
|
| 228 |
|
|
|
|
| 229 |
best = {"answer": None, "score": 0.0, "src": None}
|
| 230 |
for h in hits[:QA_PER_PASSAGES]:
|
| 231 |
try:
|
|
@@ -233,21 +378,65 @@ def generate_answer(query: str, index: faiss.Index, records: List[Dict], top_k:
|
|
| 233 |
except Exception:
|
| 234 |
qa = None
|
| 235 |
if qa and qa.get("answer"):
|
| 236 |
-
score = float(qa.get("score", 0))
|
| 237 |
-
ans = qa
|
| 238 |
-
|
| 239 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 240 |
if score > best["score"]:
|
| 241 |
best = {"answer": ans, "score": score, "src": h.get("source")}
|
| 242 |
|
| 243 |
-
if best["answer"]:
|
| 244 |
-
|
| 245 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 246 |
|
| 247 |
|
| 248 |
# =========================
|
| 249 |
-
#
|
| 250 |
# =========================
|
|
|
|
| 251 |
if __name__ == "__main__":
|
| 252 |
-
idx, recs = load_vectorstore()
|
| 253 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
# rag_pipeline.py
|
| 2 |
+
import os, json, re, gzip, shutil
|
| 3 |
from typing import List, Dict, Tuple
|
| 4 |
from functools import lru_cache
|
| 5 |
|
| 6 |
import faiss
|
| 7 |
import numpy as np
|
|
|
|
| 8 |
|
| 9 |
from providers import embed, generate, rerank, qa_extract
|
| 10 |
|
| 11 |
# =========================
|
| 12 |
+
# Dosya yolları ve sabitler
|
| 13 |
# =========================
|
| 14 |
|
| 15 |
+
VSTORE_DIR = "vectorstore"
|
| 16 |
+
FAISS_FILE = "index.faiss"
|
| 17 |
+
META_JSONL = "meta.jsonl"
|
| 18 |
+
META_JSONL_GZ = "meta.jsonl.gz"
|
| 19 |
|
| 20 |
+
# =========================
|
| 21 |
+
# Hız / kalite ayarları
|
| 22 |
+
# =========================
|
|
|
|
|
|
|
| 23 |
|
| 24 |
+
TOP_K_DEFAULT = 4 # Kaç pasaj döndürelim?
|
| 25 |
+
FETCH_K_DEFAULT = 16 # FAISS'ten kaç aday çekelim?
|
| 26 |
+
HIGH_SCORE_THRES = 0.78 # erken karar eşiği (cosine)
|
| 27 |
+
MARGIN_THRES = 0.06 # top1 - top2 farkı
|
| 28 |
|
| 29 |
+
CTX_CHAR_LIMIT = 1400 # LLM'e verilecek maksimum bağlam karakteri
|
| 30 |
+
QA_SCORE_THRES = 0.25 # ekstraktif QA güven eşiği
|
| 31 |
+
QA_PER_PASSAGES = 4 # kaç hit üzerinde tek tek QA denensin
|
| 32 |
+
|
| 33 |
+
# Basit boost ağırlıkları
|
| 34 |
W_TITLE_BOOST = 0.25
|
| 35 |
+
W_LEXICAL = 0.15
|
| 36 |
|
| 37 |
# =========================
|
| 38 |
+
# Kural-tabanlı çıkarım yardımcıları (tarih/kuruluş)
|
| 39 |
# =========================
|
| 40 |
|
| 41 |
DATE_RX = re.compile(
|
|
|
|
| 44 |
r"|\d{4})\b",
|
| 45 |
flags=re.IGNORECASE,
|
| 46 |
)
|
| 47 |
+
DEATH_KEYS = ["öldü", "vefat", "hayatını kaybet", "ölümü", "ölüm"]
|
| 48 |
+
FOUND_KEYS = ["kuruldu", "kuruluş", "kurulmuştur", "kuruluşu", "kuruluş tarihi"]
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 49 |
|
| 50 |
def _split_sentences(txt: str) -> List[str]:
|
| 51 |
parts = re.split(r"(?<=[.!?])\s+", (txt or "").strip())
|
| 52 |
return [p.strip() for p in parts if p.strip()]
|
| 53 |
|
|
|
|
| 54 |
def _extract_fact_sentence(query: str, hits: List[Dict]) -> Tuple[str, str]:
|
| 55 |
+
"""
|
| 56 |
+
'ne zaman öldü / ne zaman kuruldu' tipindeki sorularda
|
| 57 |
+
tarih + anahtar kelime içeren ilk cümleyi yakala.
|
| 58 |
+
Dönen: (cümle, kaynak_url) | ("", "")
|
| 59 |
+
"""
|
| 60 |
q = (query or "").lower()
|
| 61 |
if "ne zaman" not in q:
|
| 62 |
return "", ""
|
| 63 |
|
| 64 |
+
if any(k in q for k in ["öldü", "vefat", "ölümü", "ölüm"]):
|
| 65 |
keylist = DEATH_KEYS
|
| 66 |
+
elif any(k in q for k in ["kuruldu", "kuruluş"]):
|
| 67 |
keylist = FOUND_KEYS
|
| 68 |
else:
|
| 69 |
keylist = DEATH_KEYS + FOUND_KEYS
|
| 70 |
|
| 71 |
for h in hits:
|
| 72 |
+
for s in _split_sentences(h.get("text", "")):
|
|
|
|
| 73 |
if any(k in s.lower() for k in keylist) and DATE_RX.search(s):
|
| 74 |
return s, h.get("source", "")
|
| 75 |
return "", ""
|
| 76 |
|
| 77 |
+
# =========================
|
| 78 |
+
# İsim normalizasyonu (kısa span → tam özel ad)
|
| 79 |
+
# =========================
|
| 80 |
+
|
| 81 |
+
NAME_RX = re.compile(r"\b([A-ZÇĞİIÖŞÜ][a-zçğıiöşü]+(?:\s+[A-ZÇĞİIÖŞÜ][a-zçğıiöşü]+){0,3})\b")
|
| 82 |
+
|
| 83 |
+
def _expand_named_span(answer: str, hits: List[Dict]) -> str:
|
| 84 |
+
"""
|
| 85 |
+
QA'dan gelen 'Kemal' gibi kısa/eksik özel adı,
|
| 86 |
+
bağlamdaki en uzun uygun özel adla genişletir.
|
| 87 |
+
"""
|
| 88 |
+
ans = (answer or "").strip()
|
| 89 |
+
if not ans or len(ans.split()) > 2:
|
| 90 |
+
return ans
|
| 91 |
+
|
| 92 |
+
ans_low = ans.lower()
|
| 93 |
+
|
| 94 |
+
preferred_aliases = [
|
| 95 |
+
"Mustafa Kemal Atatürk",
|
| 96 |
+
"Sabiha Gökçen",
|
| 97 |
+
"İsmet İnönü",
|
| 98 |
+
]
|
| 99 |
+
# Tercihli alias varsa onu döndür
|
| 100 |
+
for h in hits:
|
| 101 |
+
text = h.get("text", "")
|
| 102 |
+
for alias in preferred_aliases:
|
| 103 |
+
if alias.lower().find(ans_low) != -1 and alias in text:
|
| 104 |
+
return alias
|
| 105 |
+
|
| 106 |
+
# Ans'ı içeren en uzun özel adı ara
|
| 107 |
+
best = ans
|
| 108 |
+
for h in hits:
|
| 109 |
+
for sent in _split_sentences(h.get("text", "")):
|
| 110 |
+
if ans_low not in sent.lower():
|
| 111 |
+
continue
|
| 112 |
+
for m in NAME_RX.finditer(sent):
|
| 113 |
+
cand = m.group(1).strip()
|
| 114 |
+
if ans_low in cand.lower():
|
| 115 |
+
if len(cand) >= len(best) and any(ch.islower() for ch in cand):
|
| 116 |
+
best = cand if len(cand.split()) >= len(best.split()) else best
|
| 117 |
+
return best
|
| 118 |
|
| 119 |
# =========================
|
| 120 |
+
# Vectorstore: LFS/Xet için otomatik indirme
|
| 121 |
# =========================
|
| 122 |
|
| 123 |
+
def _open_meta(path: str):
|
| 124 |
+
return gzip.open(path, "rt", encoding="utf-8") if path.endswith(".gz") else open(path, "r", encoding="utf-8")
|
|
|
|
| 125 |
|
| 126 |
+
def _ensure_local_vectorstore(vstore_dir: str):
|
| 127 |
+
"""
|
| 128 |
+
vectorstore klasörü yoksa veya LFS/Xet pointer yüzünden gerçek içerik yoksa
|
| 129 |
+
Space deposundan indir ve vstore_dir içine kopyala.
|
| 130 |
+
"""
|
| 131 |
+
os.makedirs(vstore_dir, exist_ok=True)
|
| 132 |
|
| 133 |
+
faiss_path = os.path.join(vstore_dir, FAISS_FILE)
|
| 134 |
+
meta_path = os.path.join(vstore_dir, META_JSONL)
|
| 135 |
+
meta_gz = os.path.join(vstore_dir, META_JSONL_GZ)
|
| 136 |
+
|
| 137 |
+
have_faiss = os.path.exists(faiss_path)
|
| 138 |
+
have_meta = os.path.exists(meta_path) or os.path.exists(meta_gz)
|
| 139 |
+
if have_faiss and have_meta:
|
| 140 |
+
return # her şey hazır
|
| 141 |
+
|
| 142 |
+
# huggingface_hub ile repo'dan yalnız vectorstore/* indir
|
| 143 |
+
try:
|
| 144 |
+
from huggingface_hub import snapshot_download
|
| 145 |
+
except Exception as e:
|
| 146 |
+
raise FileNotFoundError(
|
| 147 |
+
f"'{faiss_path}' indirilemedi veya bulunamadı ve 'huggingface_hub' yok: {e}"
|
| 148 |
+
)
|
| 149 |
+
|
| 150 |
+
repo_id = os.environ.get("HF_SPACE_REPO_ID")
|
| 151 |
+
if not repo_id:
|
| 152 |
+
owner = os.environ.get("SPACE_AUTHOR_NAME")
|
| 153 |
+
space = os.environ.get("SPACE_REPO_NAME")
|
| 154 |
+
if owner and space:
|
| 155 |
+
repo_id = f"{owner}/{space}"
|
| 156 |
+
else:
|
|
|
|
|
|
|
|
|
|
|
|
|
| 157 |
raise FileNotFoundError(
|
| 158 |
+
"HF_SPACE_REPO_ID tanımlı değil. Settings ▸ Variables bölümüne "
|
| 159 |
+
"HF_SPACE_REPO_ID = <kullanıcı>/<space> olarak ekleyin."
|
|
|
|
|
|
|
| 160 |
)
|
| 161 |
|
| 162 |
+
cache_dir = snapshot_download(
|
| 163 |
+
repo_id=repo_id,
|
| 164 |
+
repo_type="space",
|
| 165 |
+
allow_patterns=["vectorstore/*"],
|
| 166 |
+
ignore_patterns=["*.ipynb", "*.png", "*.jpg", "*.jpeg", "*.gif"],
|
| 167 |
+
local_files_only=False,
|
| 168 |
+
)
|
| 169 |
+
|
| 170 |
+
src_faiss = os.path.join(cache_dir, "vectorstore", FAISS_FILE)
|
| 171 |
+
src_meta = os.path.join(cache_dir, "vectorstore", META_JSONL)
|
| 172 |
+
src_metagz = os.path.join(cache_dir, "vectorstore", META_JSONL_GZ)
|
| 173 |
+
|
| 174 |
+
if not os.path.exists(src_faiss):
|
| 175 |
+
raise FileNotFoundError(f"'{FAISS_FILE}' Space deposunda bulunamadı (repo: {repo_id}).")
|
| 176 |
+
|
| 177 |
+
shutil.copy2(src_faiss, faiss_path)
|
| 178 |
+
if os.path.exists(src_metagz):
|
| 179 |
+
shutil.copy2(src_metagz, meta_gz)
|
| 180 |
+
elif os.path.exists(src_meta):
|
| 181 |
+
shutil.copy2(src_meta, meta_path)
|
| 182 |
+
else:
|
| 183 |
+
raise FileNotFoundError(f"'meta.jsonl(.gz)' Space deposunda bulunamadı (repo: {repo_id}).")
|
| 184 |
+
|
| 185 |
+
def load_vectorstore(vstore_dir: str = VSTORE_DIR) -> Tuple[faiss.Index, List[Dict]]:
|
| 186 |
+
"""
|
| 187 |
+
HF Spaces'ta LFS/Xet pointer dosyaları yüzünden yerel kopya yoksa,
|
| 188 |
+
gerekli dosyaları repo'dan indirir ve okur.
|
| 189 |
+
"""
|
| 190 |
+
_ensure_local_vectorstore(vstore_dir)
|
| 191 |
+
|
| 192 |
+
index_path = os.path.join(vstore_dir, FAISS_FILE)
|
| 193 |
+
meta_path_gz = os.path.join(vstore_dir, META_JSONL_GZ)
|
| 194 |
+
meta_path = meta_path_gz if os.path.exists(meta_path_gz) else os.path.join(vstore_dir, META_JSONL)
|
| 195 |
+
|
| 196 |
+
if not (os.path.exists(index_path) and os.path.exists(meta_path)):
|
| 197 |
raise FileNotFoundError(
|
| 198 |
+
"Vektör deposu bulunamadı. Lütfen 'vectorstore/index.faiss' ile "
|
| 199 |
+
"'vectorstore/meta.jsonl' (veya meta.jsonl.gz) dosyalarının mevcut olduğundan emin olun."
|
| 200 |
)
|
| 201 |
|
| 202 |
+
index = faiss.read_index(index_path)
|
| 203 |
+
# IVF/HNSW için arama derinliği parametreleri
|
| 204 |
+
# IVF/HNSW için arama derinliği parametreleri (varsa ayarla)
|
| 205 |
try:
|
| 206 |
+
# Ortam değişkeniyle özelleştirilebilir; yoksa 32
|
| 207 |
+
ef = int(os.environ.get("FAISS_EFSEARCH", "32"))
|
| 208 |
+
if hasattr(index, "hnsw"):
|
| 209 |
+
index.hnsw.efSearch = ef
|
| 210 |
except Exception:
|
| 211 |
pass
|
| 212 |
|
| 213 |
+
# meta.jsonl(.gz) oku
|
| 214 |
records: List[Dict] = []
|
| 215 |
+
with _open_meta(meta_path) as f:
|
| 216 |
for line in f:
|
| 217 |
+
if not line.strip():
|
| 218 |
+
continue
|
| 219 |
obj = json.loads(line)
|
| 220 |
+
records.append({
|
| 221 |
+
"text": obj.get("text", ""),
|
| 222 |
+
"metadata": obj.get("metadata", {})
|
| 223 |
+
})
|
| 224 |
|
| 225 |
if not records:
|
| 226 |
+
raise RuntimeError("meta.jsonl(.gz) boş görünüyor veya okunamadı.")
|
| 227 |
+
|
| 228 |
return index, records
|
| 229 |
|
| 230 |
|
| 231 |
# =========================
|
| 232 |
+
# Anahtar kelime çıkarımı + lexical puan
|
| 233 |
# =========================
|
| 234 |
|
| 235 |
_CAP_WORD = re.compile(r"\b([A-ZÇĞİIÖŞÜ][a-zçğıiöşü]+(?:\s+[A-ZÇĞİIÖŞÜ][a-zçğıiöşü]+)*)\b")
|
|
|
|
| 240 |
nums = re.findall(r"\b\d{3,4}\b", q)
|
| 241 |
base = re.findall(r"[A-Za-zÇĞİIÖŞÜçğıiöşü]+", q)
|
| 242 |
base = [w.lower() for w in base if len(w) > 2]
|
| 243 |
+
# tekrarları at
|
| 244 |
return list(dict.fromkeys(caps + nums + base))
|
| 245 |
|
|
|
|
| 246 |
def _lexical_overlap(q_tokens: List[str], text: str) -> float:
|
| 247 |
toks = re.findall(r"[A-Za-zÇĞİIÖŞÜçğıiöşü]+", (text or "").lower())
|
| 248 |
if not toks:
|
| 249 |
return 0.0
|
| 250 |
qset = set([t for t in q_tokens if len(t) > 2])
|
| 251 |
tset = set([t for t in toks if len(t) > 2])
|
| 252 |
+
inter = len(qset & tset)
|
| 253 |
+
denom = len(qset) or 1
|
| 254 |
+
return inter / denom
|
| 255 |
|
| 256 |
|
| 257 |
+
# =========================
|
| 258 |
+
# Retrieval + (koşullu) Rerank + title/lexical boost
|
| 259 |
+
# =========================
|
| 260 |
+
|
| 261 |
@lru_cache(maxsize=256)
|
| 262 |
def _cached_query_vec(e5_query: str) -> np.ndarray:
|
| 263 |
+
"""E5 sorgu embedding'ini cache'ler."""
|
| 264 |
v = embed([e5_query]).astype("float32")
|
| 265 |
return v
|
| 266 |
|
| 267 |
+
def search_chunks(
|
| 268 |
+
query: str,
|
| 269 |
+
index: faiss.Index,
|
| 270 |
+
records: List[Dict],
|
| 271 |
+
top_k: int = TOP_K_DEFAULT,
|
| 272 |
+
fetch_k: int = FETCH_K_DEFAULT,
|
| 273 |
+
) -> List[Dict]:
|
| 274 |
q = (query or "").strip()
|
| 275 |
q_e5 = "query: " + q
|
| 276 |
q_vec = _cached_query_vec(q_e5)
|
| 277 |
faiss.normalize_L2(q_vec)
|
| 278 |
|
| 279 |
scores, idxs = index.search(q_vec, fetch_k)
|
| 280 |
+
|
| 281 |
+
pool: List[Dict] = []
|
| 282 |
for i, s in zip(idxs[0], scores[0]):
|
| 283 |
if 0 <= i < len(records):
|
| 284 |
+
md = records[i]["metadata"] or {}
|
| 285 |
pool.append({
|
| 286 |
"text": records[i]["text"],
|
| 287 |
"title": md.get("title", ""),
|
| 288 |
"source": md.get("source", ""),
|
| 289 |
+
"score_vec": float(s),
|
| 290 |
})
|
| 291 |
if not pool:
|
| 292 |
return []
|
| 293 |
|
| 294 |
+
# --- title & lexical boost ---
|
| 295 |
q_tokens = _keywords_from_query(q)
|
| 296 |
+
q_tokens_lower = [t.lower() for t in q_tokens]
|
| 297 |
for p in pool:
|
| 298 |
+
title = (p.get("title") or "").lower()
|
| 299 |
+
# Büyük harfle başlayan query token'ı başlıkta geçiyorsa boost
|
| 300 |
+
title_hit = any(tok.lower() in title for tok in q_tokens if tok and tok[0].isupper())
|
| 301 |
+
title_boost = W_TITLE_BOOST if title_hit else 0.0
|
| 302 |
+
lex = _lexical_overlap(q_tokens_lower, p["text"]) * W_LEXICAL
|
| 303 |
+
p["score_boosted"] = p["score_vec"] + title_boost + lex
|
| 304 |
+
|
| 305 |
+
pool_by_boost = sorted(pool, key=lambda x: x["score_boosted"], reverse=True)
|
| 306 |
+
|
| 307 |
+
# --- erken karar: top1 güçlü ve fark yüksekse rerank yapma ---
|
| 308 |
+
if len(pool_by_boost) >= 2:
|
| 309 |
+
top1, top2 = pool_by_boost[0]["score_boosted"], pool_by_boost[1]["score_boosted"]
|
| 310 |
+
else:
|
| 311 |
+
top1, top2 = pool_by_boost[0]["score_boosted"], 0.0
|
| 312 |
+
do_rerank = not (top1 >= HIGH_SCORE_THRES and (top1 - top2) >= MARGIN_THRES)
|
| 313 |
|
| 314 |
+
if do_rerank:
|
| 315 |
+
try:
|
| 316 |
+
rs = rerank(q, [p["text"] for p in pool_by_boost])
|
| 317 |
+
for p, r in zip(pool_by_boost, rs):
|
| 318 |
+
p["score_rerank"] = float(r)
|
| 319 |
+
pool_by_boost.sort(
|
| 320 |
+
key=lambda x: (x.get("score_rerank", 0.0), x["score_boosted"]),
|
| 321 |
+
reverse=True,
|
| 322 |
+
)
|
| 323 |
+
except Exception:
|
| 324 |
+
# Rerank başarısızsa boost'lu sırayı kullan
|
| 325 |
+
pass
|
| 326 |
+
|
| 327 |
+
return pool_by_boost[:top_k]
|
| 328 |
+
|
| 329 |
+
|
| 330 |
+
# =========================
|
| 331 |
+
# LLM bağlamı ve kaynak listesi
|
| 332 |
+
# =========================
|
| 333 |
+
|
| 334 |
+
def _format_sources(hits: List[Dict]) -> str:
|
| 335 |
+
seen, urls = set(), []
|
| 336 |
+
for h in hits:
|
| 337 |
+
u = (h.get("source") or "").strip()
|
| 338 |
+
if u and u not in seen:
|
| 339 |
+
urls.append(u)
|
| 340 |
+
seen.add(u)
|
| 341 |
+
return "\n".join(f"- {u}" for u in urls) if urls else "- (yok)"
|
| 342 |
+
|
| 343 |
+
def _llm_context(hits: List[Dict], limit: int = CTX_CHAR_LIMIT) -> str:
|
| 344 |
+
ctx, total = [], 0
|
| 345 |
+
for i, h in enumerate(hits, 1):
|
| 346 |
+
block = f"[{i}] {h.get('title','')} — {h.get('source','')}\n{h.get('text','')}"
|
| 347 |
+
if total + len(block) > limit:
|
| 348 |
+
break
|
| 349 |
+
ctx.append(block)
|
| 350 |
+
total += len(block)
|
| 351 |
+
return "\n\n---\n\n".join(ctx)
|
| 352 |
|
| 353 |
|
| 354 |
# =========================
|
| 355 |
+
# Nihai cevap (kural → QA → LLM → güvenli özet)
|
| 356 |
# =========================
|
| 357 |
|
| 358 |
+
def generate_answer(
|
| 359 |
+
query: str,
|
| 360 |
+
index: faiss.Index,
|
| 361 |
+
records: List[Dict],
|
| 362 |
+
top_k: int = TOP_K_DEFAULT,
|
| 363 |
+
) -> str:
|
| 364 |
hits = search_chunks(query, index, records, top_k=top_k)
|
| 365 |
if not hits:
|
| 366 |
return "Bilgi bulunamadı."
|
| 367 |
|
| 368 |
+
# 0) Kural-tabanlı hızlı çıkarım (tarih/kuruluş soruları)
|
| 369 |
rule_sent, rule_src = _extract_fact_sentence(query, hits)
|
| 370 |
if rule_sent:
|
| 371 |
+
return f"{rule_sent}\n\nKaynaklar:\n- {rule_src if rule_src else _format_sources(hits)}"
|
| 372 |
|
| 373 |
+
# 1) Pasaj bazlı ekstraktif QA
|
| 374 |
best = {"answer": None, "score": 0.0, "src": None}
|
| 375 |
for h in hits[:QA_PER_PASSAGES]:
|
| 376 |
try:
|
|
|
|
| 378 |
except Exception:
|
| 379 |
qa = None
|
| 380 |
if qa and qa.get("answer"):
|
| 381 |
+
score = float(qa.get("score", 0.0))
|
| 382 |
+
ans = (qa.get("answer") or "").strip()
|
| 383 |
+
|
| 384 |
+
# Cevap tarih/özel ad içeriyorsa ekstra güven
|
| 385 |
+
if re.search(r"\b(19\d{2}|20\d{2}|Atatürk|Gökçen|Kemal|Ankara|Fenerbahçe)\b",
|
| 386 |
+
ans, flags=re.IGNORECASE):
|
| 387 |
+
score += 0.30
|
| 388 |
+
|
| 389 |
+
# Çok kısa veya eksik isimse → bağlamdan tam özel ada genişlet
|
| 390 |
+
if len(ans.split()) <= 2:
|
| 391 |
+
ans = _expand_named_span(ans, hits)
|
| 392 |
+
|
| 393 |
if score > best["score"]:
|
| 394 |
best = {"answer": ans, "score": score, "src": h.get("source")}
|
| 395 |
|
| 396 |
+
if best["answer"] and best["score"] >= QA_SCORE_THRES:
|
| 397 |
+
final = best["answer"].strip()
|
| 398 |
+
# Soru "kimdir/kim" ise doğal cümleye dök
|
| 399 |
+
if any(k in (query or "").lower() for k in ["kimdir", "kim"]):
|
| 400 |
+
if not final.endswith("."):
|
| 401 |
+
final += "."
|
| 402 |
+
final = f"{final} {query.rstrip('?')} sorusunun yanıtıdır."
|
| 403 |
+
src_line = f"Kaynaklar:\n- {best['src']}" if best["src"] else "Kaynaklar:\n" + _format_sources(hits)
|
| 404 |
+
return f"{final}\n\n{src_line}"
|
| 405 |
+
|
| 406 |
+
# 2) QA düşük güven verdiyse → LLM (varsa)
|
| 407 |
+
context = _llm_context(hits)
|
| 408 |
+
prompt = (
|
| 409 |
+
"Aşağıdaki BAĞLAM Wikipedia parçalarından alınmıştır.\n"
|
| 410 |
+
"Sadece bu bağlamdan yararlanarak soruya kısa, net ve doğru bir Türkçe cevap ver.\n"
|
| 411 |
+
"Uydurma yapma, sadece metinlerde geçen bilgileri kullan.\n\n"
|
| 412 |
+
f"Soru:\n{query}\n\nBağlam:\n{context}\n\nYanıtı 1-2 cümlede ver."
|
| 413 |
+
)
|
| 414 |
+
llm_ans = (generate(prompt) or "").strip()
|
| 415 |
+
|
| 416 |
+
# 3) LLM yapılandırılmamışsa → güvenli özet fallback
|
| 417 |
+
if (not llm_ans) or ("yapılandırılmadı" in llm_ans.lower()):
|
| 418 |
+
text = hits[0].get("text", "")
|
| 419 |
+
first = re.split(r"(?<=[.!?])\s+", text.strip())[:2]
|
| 420 |
+
llm_ans = " ".join(first).strip() or "Verilen bağlamda bu sorunun cevabı bulunmamaktadır."
|
| 421 |
+
|
| 422 |
+
if "Kaynaklar:" not in llm_ans:
|
| 423 |
+
llm_ans += "\n\nKaynaklar:\n" + _format_sources(hits)
|
| 424 |
+
return llm_ans
|
| 425 |
|
| 426 |
|
| 427 |
# =========================
|
| 428 |
+
# Hızlı test
|
| 429 |
# =========================
|
| 430 |
+
|
| 431 |
if __name__ == "__main__":
|
| 432 |
+
idx, recs = load_vectorstore(VSTORE_DIR)
|
| 433 |
+
for q in [
|
| 434 |
+
"Atatürk ne zaman öldü?",
|
| 435 |
+
"Türkiye'nin ilk cumhurbaşkanı kimdir?",
|
| 436 |
+
"Fenerbahçe ne zaman kuruldu?",
|
| 437 |
+
"Türkiye'nin başkenti neresidir?",
|
| 438 |
+
"Türkiye'nin ilk kadın pilotu kimdir?",
|
| 439 |
+
]:
|
| 440 |
+
print("Soru:", q)
|
| 441 |
+
print(generate_answer(q, idx, recs, top_k=TOP_K_DEFAULT))
|
| 442 |
+
print("-" * 80)
|