Upload 5 files
Browse files- app.py +53 -0
- data_preparation.py +155 -0
- providers.py +121 -0
- rag_pipeline.py +352 -0
- requirements.txt +13 -0
app.py
ADDED
|
@@ -0,0 +1,53 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
# app.py
|
| 2 |
+
import os, textwrap
|
| 3 |
+
import streamlit as st
|
| 4 |
+
from rag_pipeline import load_vectorstore, search_chunks, generate_answer
|
| 5 |
+
|
| 6 |
+
st.set_page_config(page_title="Turkish Wikipedia Q&A (Gemini RAG)", page_icon="🧠")
|
| 7 |
+
|
| 8 |
+
@st.cache_resource(show_spinner=False)
|
| 9 |
+
def _load_vdb():
|
| 10 |
+
return load_vectorstore()
|
| 11 |
+
|
| 12 |
+
st.title("🧠 Turkish Wikipedia Q&A")
|
| 13 |
+
|
| 14 |
+
with st.expander("⚙️ Ayarlar", expanded=False):
|
| 15 |
+
top_k = st.slider("Top K (kaç pasaj getirilsin?)", 2, 8, 5)
|
| 16 |
+
show_passages = st.checkbox("Getirilen pasaj özetini göster", value=True)
|
| 17 |
+
st.info("LLM: " + ("Gemini ✅" if os.getenv("GOOGLE_API_KEY") else "Yapılandırılmadı ❌"))
|
| 18 |
+
|
| 19 |
+
query = st.text_input("📝 Sorunuzu yazın (ör. “Türkiye'nin ilk kadın pilotu kimdir?”)")
|
| 20 |
+
|
| 21 |
+
if st.button("Cevabı Getir", type="primary", use_container_width=True) and query.strip():
|
| 22 |
+
try:
|
| 23 |
+
with st.spinner("Aranıyor ve cevap oluşturuluyor..."):
|
| 24 |
+
index, records = _load_vdb()
|
| 25 |
+
answer = generate_answer(query.strip(), index, records, top_k=top_k)
|
| 26 |
+
hits = search_chunks(query.strip(), index, records, top_k=top_k)
|
| 27 |
+
|
| 28 |
+
st.subheader("✅ Yanıt")
|
| 29 |
+
st.write(answer)
|
| 30 |
+
|
| 31 |
+
st.subheader("🔎 Kaynaklar")
|
| 32 |
+
if hits:
|
| 33 |
+
for i, h in enumerate(hits, 1):
|
| 34 |
+
title = h.get("title") or "(başlık yok)"
|
| 35 |
+
url = h.get("source") or ""
|
| 36 |
+
score = h.get("score_rerank", 0.0)
|
| 37 |
+
lead = textwrap.shorten((h.get("text") or "").replace("\n", " "), width=220, placeholder="…")
|
| 38 |
+
if url:
|
| 39 |
+
st.markdown(f"**{i}.** [{title}]({url}) \nRerank skoru: `{score:.3f}`")
|
| 40 |
+
else:
|
| 41 |
+
st.markdown(f"**{i}.** {title} \nRerank skoru: `{score:.3f}`")
|
| 42 |
+
if show_passages and lead:
|
| 43 |
+
st.caption(lead)
|
| 44 |
+
st.markdown("---")
|
| 45 |
+
else:
|
| 46 |
+
st.write("_Kaynak bulunamadı._")
|
| 47 |
+
|
| 48 |
+
except FileNotFoundError as e:
|
| 49 |
+
st.error("Vektör deposu bulunamadı. Önce `python data_preparation.py` çalıştırın.")
|
| 50 |
+
st.code(str(e))
|
| 51 |
+
except Exception as e:
|
| 52 |
+
st.error("Bir hata oluştu.")
|
| 53 |
+
st.exception(e)
|
data_preparation.py
ADDED
|
@@ -0,0 +1,155 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
# data_preparation.py
|
| 2 |
+
import os, re, time, json
|
| 3 |
+
from typing import List, Dict, Tuple
|
| 4 |
+
|
| 5 |
+
import numpy as np
|
| 6 |
+
import faiss
|
| 7 |
+
from datasets import load_dataset, DownloadConfig
|
| 8 |
+
from langchain_text_splitters import RecursiveCharacterTextSplitter
|
| 9 |
+
|
| 10 |
+
# Embedding sağlayıcısı (E5) -> providers.embed
|
| 11 |
+
from providers import embed
|
| 12 |
+
|
| 13 |
+
# =========================
|
| 14 |
+
# AYARLAR
|
| 15 |
+
# =========================
|
| 16 |
+
|
| 17 |
+
DATASET_NAME = "wikimedia/wikipedia" # HF dataset adı
|
| 18 |
+
WIKI_CONFIG = "20231101.tr" # Türkçe dump sürümü
|
| 19 |
+
MAX_PAGES = 500 # hızlı demo için 500
|
| 20 |
+
CHUNK_SIZE = 1000 # büyük parça = daha az chunk
|
| 21 |
+
CHUNK_OVERLAP= 100
|
| 22 |
+
MAX_CHUNKS = 8000 # güvenli üst limit (None yapıldığında sınırsız)
|
| 23 |
+
|
| 24 |
+
VSTORE_DIR = "vectorstore"
|
| 25 |
+
META_JSONL = "meta.jsonl"
|
| 26 |
+
FAISS_FILE = "index.faiss"
|
| 27 |
+
SIGN_FILE = "signature.json"
|
| 28 |
+
|
| 29 |
+
# =========================
|
| 30 |
+
# Yardımcılar
|
| 31 |
+
# =========================
|
| 32 |
+
|
| 33 |
+
def ensure_dir(p: str):
|
| 34 |
+
os.makedirs(p, exist_ok=True)
|
| 35 |
+
|
| 36 |
+
def slugify_title(title: str) -> str:
|
| 37 |
+
t = (title or "").strip().replace(" ", "_")
|
| 38 |
+
t = re.sub(r"[^\w\-ÇçĞğİıÖöŞşÜü]+", "", t, flags=re.UNICODE)
|
| 39 |
+
return t or "Sayfa"
|
| 40 |
+
|
| 41 |
+
# Gereksiz template token temizliği
|
| 42 |
+
TOKEN_PAT = re.compile(r"<\|/?(system|user|assistant|start|end)\|>|<\/?s>|<\/?unk>", re.IGNORECASE)
|
| 43 |
+
def clean_text(s: str) -> str:
|
| 44 |
+
if not s:
|
| 45 |
+
return ""
|
| 46 |
+
s = TOKEN_PAT.sub(" ", s)
|
| 47 |
+
return re.sub(r"\s+", " ", s).strip()
|
| 48 |
+
|
| 49 |
+
# =========================
|
| 50 |
+
# Chunking
|
| 51 |
+
# =========================
|
| 52 |
+
|
| 53 |
+
def chunk_documents(rows: List[Dict]) -> Tuple[List[str], List[Dict]]:
|
| 54 |
+
splitter = RecursiveCharacterTextSplitter(
|
| 55 |
+
chunk_size=CHUNK_SIZE,
|
| 56 |
+
chunk_overlap=CHUNK_OVERLAP,
|
| 57 |
+
)
|
| 58 |
+
texts_raw: List[str] = []
|
| 59 |
+
metas: List[Dict] = []
|
| 60 |
+
|
| 61 |
+
for r in rows:
|
| 62 |
+
title = clean_text(r.get("title", ""))
|
| 63 |
+
text = clean_text(r.get("text", ""))
|
| 64 |
+
if not text:
|
| 65 |
+
continue
|
| 66 |
+
|
| 67 |
+
url = f"https://tr.wikipedia.org/wiki/{slugify_title(title)}"
|
| 68 |
+
chunks = splitter.split_text(text)
|
| 69 |
+
|
| 70 |
+
for i, ch in enumerate(chunks):
|
| 71 |
+
ch = ch.strip()
|
| 72 |
+
if not ch:
|
| 73 |
+
continue
|
| 74 |
+
texts_raw.append(ch) # meta'ya RAW metin yazılıyor
|
| 75 |
+
metas.append({"title": title or "(başlık yok)", "chunk_id": i, "source": url})
|
| 76 |
+
|
| 77 |
+
if MAX_CHUNKS and len(texts_raw) >= MAX_CHUNKS:
|
| 78 |
+
return texts_raw, metas
|
| 79 |
+
|
| 80 |
+
return texts_raw, metas
|
| 81 |
+
|
| 82 |
+
# =========================
|
| 83 |
+
# FAISS (HNSW) — hızlı ANN arama
|
| 84 |
+
# =========================
|
| 85 |
+
|
| 86 |
+
def build_faiss_index(vecs: np.ndarray) -> faiss.Index:
|
| 87 |
+
"""
|
| 88 |
+
Cosine benzerliği için L2 normalize edip Inner-Product ile HNSW kullanılır.
|
| 89 |
+
HNSW, IndexFlat'e yakın doğrulukta olup sorgu süresini ciddi düşürür.
|
| 90 |
+
"""
|
| 91 |
+
faiss.normalize_L2(vecs)
|
| 92 |
+
dim = vecs.shape[1]
|
| 93 |
+
M = 32 # graph degree
|
| 94 |
+
index = faiss.IndexHNSWFlat(dim, M, faiss.METRIC_INNER_PRODUCT)
|
| 95 |
+
index.hnsw.efConstruction = 80 # inşa kalitesi (yüksek = daha iyi/az fark)
|
| 96 |
+
index.add(vecs)
|
| 97 |
+
return index
|
| 98 |
+
|
| 99 |
+
# =========================
|
| 100 |
+
# Ana akış
|
| 101 |
+
# =========================
|
| 102 |
+
|
| 103 |
+
def main():
|
| 104 |
+
t0 = time.time()
|
| 105 |
+
|
| 106 |
+
print("👉 Wikipedia(TR) yükleniyor...")
|
| 107 |
+
split_expr = f"train[:{MAX_PAGES}]" if MAX_PAGES else "train"
|
| 108 |
+
ds = load_dataset(
|
| 109 |
+
DATASET_NAME, WIKI_CONFIG,
|
| 110 |
+
split=split_expr,
|
| 111 |
+
download_config=DownloadConfig(max_retries=5),
|
| 112 |
+
)
|
| 113 |
+
print(f"Toplam sayfa (seçim sonrası): {len(ds)}")
|
| 114 |
+
|
| 115 |
+
print("👉 Chunk'lanıyor...")
|
| 116 |
+
texts_raw, metas = chunk_documents([dict(x) for x in ds])
|
| 117 |
+
print(f"Toplam chunk: {len(texts_raw)}")
|
| 118 |
+
if not texts_raw:
|
| 119 |
+
raise SystemExit("⚠️ Metin bulunamadı.")
|
| 120 |
+
|
| 121 |
+
print("👉 Embedding hesaplanıyor (E5)...")
|
| 122 |
+
# E5 kuralı: embed edilirken PASSAGE prefix kullan, meta'da görünmesin
|
| 123 |
+
texts_for_emb = [f"passage: {t}" for t in texts_raw]
|
| 124 |
+
vecs = np.asarray(embed(texts_for_emb), dtype="float32")
|
| 125 |
+
if vecs.ndim != 2:
|
| 126 |
+
raise ValueError(f"Beklenen (N,D) vektör, gelen {vecs.shape}")
|
| 127 |
+
|
| 128 |
+
print("👉 FAISS (HNSW) indeks oluşturuluyor...")
|
| 129 |
+
index = build_faiss_index(vecs)
|
| 130 |
+
|
| 131 |
+
print("👉 Kaydediliyor...")
|
| 132 |
+
ensure_dir(VSTORE_DIR)
|
| 133 |
+
faiss.write_index(index, os.path.join(VSTORE_DIR, FAISS_FILE))
|
| 134 |
+
|
| 135 |
+
meta_path = os.path.join(VSTORE_DIR, META_JSONL)
|
| 136 |
+
with open(meta_path, "w", encoding="utf-8") as f:
|
| 137 |
+
for t, m in zip(texts_raw, metas):
|
| 138 |
+
f.write(json.dumps({"text": t, "metadata": m}, ensure_ascii=False) + "\n")
|
| 139 |
+
|
| 140 |
+
sign_path = os.path.join(VSTORE_DIR, SIGN_FILE)
|
| 141 |
+
with open(sign_path, "w", encoding="utf-8") as f:
|
| 142 |
+
json.dump({
|
| 143 |
+
"dataset": f"{DATASET_NAME}:{WIKI_CONFIG}",
|
| 144 |
+
"max_pages": MAX_PAGES,
|
| 145 |
+
"chunk_size": CHUNK_SIZE,
|
| 146 |
+
"chunk_overlap": CHUNK_OVERLAP,
|
| 147 |
+
"max_chunks": MAX_CHUNKS,
|
| 148 |
+
"faiss": {"type": "HNSWFlat", "metric": "IP", "M": 32, "efConstruction": 80},
|
| 149 |
+
"emb_model": os.getenv("EMB_MODEL", "intfloat/multilingual-e5-small"),
|
| 150 |
+
}, f, ensure_ascii=False, indent=2)
|
| 151 |
+
|
| 152 |
+
print(f"✅ Tamamlandı. Süre: {(time.time()-t0):.1f} sn | Çıktı klasörü: {VSTORE_DIR}")
|
| 153 |
+
|
| 154 |
+
if __name__ == "__main__":
|
| 155 |
+
main()
|
providers.py
ADDED
|
@@ -0,0 +1,121 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
# providers.py
|
| 2 |
+
from typing import List
|
| 3 |
+
import os
|
| 4 |
+
import numpy as np
|
| 5 |
+
import torch
|
| 6 |
+
from functools import lru_cache
|
| 7 |
+
from sentence_transformers import SentenceTransformer, CrossEncoder
|
| 8 |
+
|
| 9 |
+
from dotenv import load_dotenv
|
| 10 |
+
|
| 11 |
+
# .env dosyasını oku
|
| 12 |
+
load_dotenv()
|
| 13 |
+
|
| 14 |
+
# API anahtarını al
|
| 15 |
+
GOOGLE_API_KEY = os.getenv("GOOGLE_API_KEY")
|
| 16 |
+
if not GOOGLE_API_KEY:
|
| 17 |
+
print("⚠️ Uyarı: GOOGLE_API_KEY .env dosyasında bulunamadı!")
|
| 18 |
+
|
| 19 |
+
# =========================
|
| 20 |
+
# CONFIG (env ile override)
|
| 21 |
+
# =========================
|
| 22 |
+
EMB_MODEL_NAME = os.getenv("EMB_MODEL", "intfloat/multilingual-e5-small")
|
| 23 |
+
# Hız için default MiniLM; Jina kullanmak istersen RERANKER_MODEL=jinaai/jina-reranker-v2-base-multilingual
|
| 24 |
+
RERANKER_NAME = os.getenv("RERANKER_MODEL", "cross-encoder/ms-marco-MiniLM-L-6-v2")
|
| 25 |
+
GEMINI_MODEL = os.getenv("GEMINI_MODEL", "gemini-1.5-flash")
|
| 26 |
+
|
| 27 |
+
# =========================
|
| 28 |
+
# Embedding (E5)
|
| 29 |
+
# =========================
|
| 30 |
+
|
| 31 |
+
_emb_model: SentenceTransformer | None = None
|
| 32 |
+
|
| 33 |
+
def _get_emb_model() -> SentenceTransformer:
|
| 34 |
+
global _emb_model
|
| 35 |
+
if _emb_model is None:
|
| 36 |
+
# CPU'da stabil ve hızlı çalışması için
|
| 37 |
+
torch.set_num_threads(max(1, (os.cpu_count() or 4) // 2))
|
| 38 |
+
_emb_model = SentenceTransformer(EMB_MODEL_NAME)
|
| 39 |
+
return _emb_model
|
| 40 |
+
|
| 41 |
+
def embed(texts: List[str]) -> np.ndarray:
|
| 42 |
+
"""E5 embedding üretir (normalize etmez)."""
|
| 43 |
+
model = _get_emb_model()
|
| 44 |
+
vecs = model.encode(
|
| 45 |
+
texts,
|
| 46 |
+
batch_size=32,
|
| 47 |
+
show_progress_bar=False,
|
| 48 |
+
convert_to_numpy=True,
|
| 49 |
+
normalize_embeddings=False,
|
| 50 |
+
)
|
| 51 |
+
return vecs
|
| 52 |
+
|
| 53 |
+
# =========================
|
| 54 |
+
# Reranker (Cross-Encoder)
|
| 55 |
+
# =========================
|
| 56 |
+
|
| 57 |
+
_reranker: CrossEncoder | None = None
|
| 58 |
+
|
| 59 |
+
def _get_reranker() -> CrossEncoder:
|
| 60 |
+
global _reranker
|
| 61 |
+
if _reranker is None:
|
| 62 |
+
|
| 63 |
+
trust = "jina" in RERANKER_NAME.lower()
|
| 64 |
+
_reranker = CrossEncoder(
|
| 65 |
+
RERANKER_NAME,
|
| 66 |
+
max_length=384,
|
| 67 |
+
trust_remote_code=trust,
|
| 68 |
+
)
|
| 69 |
+
return _reranker
|
| 70 |
+
|
| 71 |
+
def rerank(query: str, candidates: List[str]) -> List[float]:
|
| 72 |
+
"""Sorgu + aday pasajlar için alaka skorları döndürür (yüksek skor = daha alakalı)."""
|
| 73 |
+
model = _get_reranker()
|
| 74 |
+
pairs = [[query, c] for c in candidates]
|
| 75 |
+
scores = model.predict(pairs, convert_to_numpy=True, show_progress_bar=False)
|
| 76 |
+
return scores.tolist()
|
| 77 |
+
|
| 78 |
+
# =========================
|
| 79 |
+
# (Opsiyonel) Ekstraktif QA – TR SQuAD
|
| 80 |
+
# =========================
|
| 81 |
+
|
| 82 |
+
_QA_MODEL = os.getenv("QA_MODEL", "savasy/bert-base-turkish-squad")
|
| 83 |
+
_qa_pipe = None # lazy load
|
| 84 |
+
|
| 85 |
+
def qa_extract(question: str, context: str) -> dict:
|
| 86 |
+
"""
|
| 87 |
+
Pasajdan doğrudan cevap span'ı çıkarır.
|
| 88 |
+
Dönen örnek: {'answer': '1907', 'score': 0.93, 'start': 123, 'end': 127}
|
| 89 |
+
Kullanmazsan çağırma; yüklenmez ve hız etkisi olmaz.
|
| 90 |
+
"""
|
| 91 |
+
global _qa_pipe
|
| 92 |
+
if _qa_pipe is None:
|
| 93 |
+
from transformers import pipeline # import burada ki ihtiyaca göre yüklensin
|
| 94 |
+
_qa_pipe = pipeline("question-answering", model=_QA_MODEL, tokenizer=_QA_MODEL)
|
| 95 |
+
res = _qa_pipe(question=question, context=context)
|
| 96 |
+
return dict(res)
|
| 97 |
+
|
| 98 |
+
# =========================
|
| 99 |
+
# LLM: Google Gemini
|
| 100 |
+
# =========================
|
| 101 |
+
|
| 102 |
+
def generate(prompt: str) -> str:
|
| 103 |
+
"""
|
| 104 |
+
Gemini ile üretken cevap. GOOGLE_API_KEY yoksa 'LLM yapılandırılmadı.' döner.
|
| 105 |
+
"""
|
| 106 |
+
api_key = os.getenv("GOOGLE_API_KEY")
|
| 107 |
+
if not api_key:
|
| 108 |
+
return "LLM yapılandırılmadı."
|
| 109 |
+
try:
|
| 110 |
+
import google.generativeai as genai
|
| 111 |
+
genai.configure(api_key=api_key)
|
| 112 |
+
model = genai.GenerativeModel(GEMINI_MODEL)
|
| 113 |
+
response = model.generate_content(
|
| 114 |
+
prompt,
|
| 115 |
+
generation_config=genai.types.GenerationConfig(
|
| 116 |
+
temperature=0.1, max_output_tokens=300, top_p=0.8
|
| 117 |
+
),
|
| 118 |
+
)
|
| 119 |
+
return response.text.strip() if hasattr(response, "text") else "Cevap oluşturulamadı."
|
| 120 |
+
except Exception as e:
|
| 121 |
+
return f"LLM hata: {e}"
|
rag_pipeline.py
ADDED
|
@@ -0,0 +1,352 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
# rag_pipeline.py
|
| 2 |
+
import os, json, re
|
| 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ı
|
| 13 |
+
# =========================
|
| 14 |
+
|
| 15 |
+
VSTORE_DIR = "vectorstore"
|
| 16 |
+
FAISS_FILE = "index.faiss"
|
| 17 |
+
META_JSONL = "meta.jsonl"
|
| 18 |
+
|
| 19 |
+
# =========================
|
| 20 |
+
# Hız / kalite ayarları
|
| 21 |
+
# =========================
|
| 22 |
+
|
| 23 |
+
TOP_K_DEFAULT = 4 # Kaç pasaj döndürelim?
|
| 24 |
+
FETCH_K_DEFAULT = 16 # FAISS'ten kaç aday çekelim?
|
| 25 |
+
HNSW_EFSEARCH = 32 # HNSW arama derinliği
|
| 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 (biraz düşük)
|
| 31 |
+
QA_PER_PASSAGES = 4 # kaç hit üzerinde tek tek QA denensin
|
| 32 |
+
|
| 33 |
+
# Basit "title" ve "lexical" 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(
|
| 42 |
+
r"\b(\d{1,2}\s+(Ocak|Şubat|Mart|Nisan|Mayıs|Haziran|Temmuz|Ağustos|Eylül|Ekim|Kasım|Aralık)\s+\d{3,4}"
|
| 43 |
+
r"|\d{1,2}\.\d{1,2}\.\d{2,4}"
|
| 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 |
+
sents = _split_sentences(h.get("text", ""))
|
| 73 |
+
for s in sents:
|
| 74 |
+
if any(k in s.lower() for k in keylist) and DATE_RX.search(s):
|
| 75 |
+
return s, h.get("source", "")
|
| 76 |
+
return "", ""
|
| 77 |
+
|
| 78 |
+
# =========================
|
| 79 |
+
# İsim normalizasyonu (kısa span → tam özel ad)
|
| 80 |
+
# =========================
|
| 81 |
+
|
| 82 |
+
NAME_RX = re.compile(
|
| 83 |
+
r"\b([A-ZÇĞİIÖŞÜ][a-zçğıiöşü]+(?:\s+[A-ZÇĞİIÖŞÜ][a-zçğıiöşü]+){0,3})\b"
|
| 84 |
+
)
|
| 85 |
+
|
| 86 |
+
def _expand_named_span(answer: str, hits: List[Dict]) -> str:
|
| 87 |
+
"""
|
| 88 |
+
QA'dan gelen 'Kemal' gibi kısa/eksik özel adı,
|
| 89 |
+
bağlamdaki en uzun uygun özel adla genişletir.
|
| 90 |
+
"""
|
| 91 |
+
ans = (answer or "").strip()
|
| 92 |
+
if not ans or len(ans.split()) > 2:
|
| 93 |
+
return ans
|
| 94 |
+
|
| 95 |
+
ans_low = ans.lower()
|
| 96 |
+
|
| 97 |
+
# Özel eşleştirme: 'Atatürk' veya 'Kemal' görülürse 'Mustafa Kemal Atatürk' aransın
|
| 98 |
+
preferred_aliases = [
|
| 99 |
+
"Mustafa Kemal Atatürk",
|
| 100 |
+
"Sabiha Gökçen",
|
| 101 |
+
"İsmet İnönü",
|
| 102 |
+
]
|
| 103 |
+
|
| 104 |
+
# 1) Önce tercihli alias'lar bağlamda geçiyorsa onu döndür
|
| 105 |
+
for h in hits:
|
| 106 |
+
text = h.get("text", "")
|
| 107 |
+
for alias in preferred_aliases:
|
| 108 |
+
if alias.lower().find(ans_low) != -1 and alias in text:
|
| 109 |
+
return alias
|
| 110 |
+
|
| 111 |
+
# 2) Aksi halde: ans'ı içeren en uzun özel adı bulma
|
| 112 |
+
best = ans
|
| 113 |
+
for h in hits:
|
| 114 |
+
for sent in _split_sentences(h.get("text", "")):
|
| 115 |
+
if ans_low not in sent.lower():
|
| 116 |
+
continue
|
| 117 |
+
for m in NAME_RX.finditer(sent):
|
| 118 |
+
cand = m.group(1).strip()
|
| 119 |
+
if ans_low in cand.lower():
|
| 120 |
+
# tek harfli/çok kısa kurum adlarını eleme
|
| 121 |
+
if len(cand) >= len(best) and any(ch.islower() for ch in cand):
|
| 122 |
+
best = cand if len(cand.split()) >= len(best.split()) else best
|
| 123 |
+
return best
|
| 124 |
+
|
| 125 |
+
# =========================
|
| 126 |
+
# Vektör deposunu yükle
|
| 127 |
+
# =========================
|
| 128 |
+
|
| 129 |
+
def load_vectorstore() -> Tuple[faiss.Index, List[Dict]]:
|
| 130 |
+
index_path = os.path.join(VSTORE_DIR, FAISS_FILE)
|
| 131 |
+
meta_path = os.path.join(VSTORE_DIR, META_JSONL)
|
| 132 |
+
if not (os.path.exists(index_path) and os.path.exists(meta_path)):
|
| 133 |
+
raise FileNotFoundError(
|
| 134 |
+
"Vektör deposu bulunamadı. Önce `python data_preparation.py` çalıştırın:\n"
|
| 135 |
+
f"- {index_path}\n- {meta_path}"
|
| 136 |
+
)
|
| 137 |
+
|
| 138 |
+
index = faiss.read_index(index_path)
|
| 139 |
+
try:
|
| 140 |
+
index.hnsw.efSearch = HNSW_EFSEARCH
|
| 141 |
+
except Exception:
|
| 142 |
+
pass
|
| 143 |
+
|
| 144 |
+
records: List[Dict] = []
|
| 145 |
+
with open(meta_path, "r", encoding="utf-8") as f:
|
| 146 |
+
for line in f:
|
| 147 |
+
obj = json.loads(line)
|
| 148 |
+
records.append({"text": obj.get("text", ""), "metadata": obj.get("metadata", {})})
|
| 149 |
+
|
| 150 |
+
if not records:
|
| 151 |
+
raise RuntimeError("meta.jsonl boş görünüyor.")
|
| 152 |
+
return index, records
|
| 153 |
+
|
| 154 |
+
# =========================
|
| 155 |
+
# Anahtar kelime çıkarımı + lexical puan
|
| 156 |
+
# =========================
|
| 157 |
+
|
| 158 |
+
_CAP_WORD = re.compile(r"\b([A-ZÇĞİIÖŞÜ][a-zçğıiöşü]+(?:\s+[A-ZÇĞİIÖŞÜ][a-zçğıiöşü]+)*)\b")
|
| 159 |
+
|
| 160 |
+
def _keywords_from_query(q: str) -> List[str]:
|
| 161 |
+
q = (q or "").strip()
|
| 162 |
+
caps = [m.group(1) for m in _CAP_WORD.finditer(q)]
|
| 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 |
+
def _lexical_overlap(q_tokens: List[str], text: str) -> float:
|
| 169 |
+
toks = re.findall(r"[A-Za-zÇĞİIÖŞÜçğıiöşü]+", (text or "").lower())
|
| 170 |
+
if not toks:
|
| 171 |
+
return 0.0
|
| 172 |
+
qset = set([t for t in q_tokens if len(t) > 2])
|
| 173 |
+
tset = set([t for t in toks if len(t) > 2])
|
| 174 |
+
inter = len(qset & tset)
|
| 175 |
+
denom = len(qset) or 1
|
| 176 |
+
return inter / denom
|
| 177 |
+
|
| 178 |
+
# =========================
|
| 179 |
+
# Retrieval + (koşullu) Rerank + title/lexical boost
|
| 180 |
+
# =========================
|
| 181 |
+
|
| 182 |
+
@lru_cache(maxsize=256)
|
| 183 |
+
def _cached_query_vec(e5_query: str) -> np.ndarray:
|
| 184 |
+
"""E5 sorgu embedding'ini cache'ler."""
|
| 185 |
+
v = embed([e5_query]).astype("float32")
|
| 186 |
+
return v
|
| 187 |
+
|
| 188 |
+
def search_chunks(
|
| 189 |
+
query: str,
|
| 190 |
+
index: faiss.Index,
|
| 191 |
+
records: List[Dict],
|
| 192 |
+
top_k: int = TOP_K_DEFAULT,
|
| 193 |
+
fetch_k: int = FETCH_K_DEFAULT,
|
| 194 |
+
) -> List[Dict]:
|
| 195 |
+
q = (query or "").strip()
|
| 196 |
+
q_e5 = "query: " + q
|
| 197 |
+
q_vec = _cached_query_vec(q_e5)
|
| 198 |
+
faiss.normalize_L2(q_vec)
|
| 199 |
+
|
| 200 |
+
scores, idxs = index.search(q_vec, fetch_k)
|
| 201 |
+
|
| 202 |
+
pool: List[Dict] = []
|
| 203 |
+
for i, s in zip(idxs[0], scores[0]):
|
| 204 |
+
if 0 <= i < len(records):
|
| 205 |
+
md = records[i]["metadata"]
|
| 206 |
+
pool.append({
|
| 207 |
+
"text": records[i]["text"],
|
| 208 |
+
"title": md.get("title", ""),
|
| 209 |
+
"source": md.get("source", ""),
|
| 210 |
+
"score_vec": float(s),
|
| 211 |
+
})
|
| 212 |
+
if not pool:
|
| 213 |
+
return []
|
| 214 |
+
|
| 215 |
+
# --- title & lexical boost ---
|
| 216 |
+
q_tokens = _keywords_from_query(q)
|
| 217 |
+
q_tokens_lower = [t.lower() for t in q_tokens]
|
| 218 |
+
for p in pool:
|
| 219 |
+
title = (p.get("title") or "").lower()
|
| 220 |
+
title_hit = any(tok.lower() in title for tok in q_tokens if tok and tok[0].isupper())
|
| 221 |
+
title_boost = W_TITLE_BOOST if title_hit else 0.0
|
| 222 |
+
lex = _lexical_overlap(q_tokens_lower, p["text"]) * W_LEXICAL
|
| 223 |
+
p["score_boosted"] = p["score_vec"] + title_boost + lex
|
| 224 |
+
|
| 225 |
+
pool_by_boost = sorted(pool, key=lambda x: x["score_boosted"], reverse=True)
|
| 226 |
+
|
| 227 |
+
# --- erken karar: top1 güçlü ve fark yüksekse rerank yapma ---
|
| 228 |
+
if len(pool_by_boost) >= 2:
|
| 229 |
+
top1, top2 = pool_by_boost[0]["score_boosted"], pool_by_boost[1]["score_boosted"]
|
| 230 |
+
else:
|
| 231 |
+
top1, top2 = pool_by_boost[0]["score_boosted"], 0.0
|
| 232 |
+
do_rerank = not (top1 >= HIGH_SCORE_THRES and (top1 - top2) >= MARGIN_THRES)
|
| 233 |
+
|
| 234 |
+
if do_rerank:
|
| 235 |
+
rs = rerank(q, [p["text"] for p in pool_by_boost])
|
| 236 |
+
for p, r in zip(pool_by_boost, rs):
|
| 237 |
+
p["score_rerank"] = float(r)
|
| 238 |
+
pool_by_boost.sort(key=lambda x: (x.get("score_rerank", 0.0), x["score_boosted"]), reverse=True)
|
| 239 |
+
|
| 240 |
+
return pool_by_boost[:top_k]
|
| 241 |
+
|
| 242 |
+
# =========================
|
| 243 |
+
# LLM bağlamı ve kaynak listesi
|
| 244 |
+
# =========================
|
| 245 |
+
|
| 246 |
+
def _format_sources(hits: List[Dict]) -> str:
|
| 247 |
+
seen, urls = set(), []
|
| 248 |
+
for h in hits:
|
| 249 |
+
u = (h.get("source") or "").strip()
|
| 250 |
+
if u and u not in seen:
|
| 251 |
+
urls.append(u)
|
| 252 |
+
seen.add(u)
|
| 253 |
+
return "\n".join(f"- {u}" for u in urls) if urls else "- (yok)"
|
| 254 |
+
|
| 255 |
+
def _llm_context(hits: List[Dict], limit: int = CTX_CHAR_LIMIT) -> str:
|
| 256 |
+
ctx, total = [], 0
|
| 257 |
+
for i, h in enumerate(hits, 1):
|
| 258 |
+
block = f"[{i}] {h.get('title','')} — {h.get('source','')}\n{h.get('text','')}"
|
| 259 |
+
if total + len(block) > limit:
|
| 260 |
+
break
|
| 261 |
+
ctx.append(block)
|
| 262 |
+
total += len(block)
|
| 263 |
+
return "\n\n---\n\n".join(ctx)
|
| 264 |
+
|
| 265 |
+
# =========================
|
| 266 |
+
# Nihai cevap (kural → QA → LLM → güvenli özet)
|
| 267 |
+
# =========================
|
| 268 |
+
|
| 269 |
+
def generate_answer(
|
| 270 |
+
query: str,
|
| 271 |
+
index: faiss.Index,
|
| 272 |
+
records: List[Dict],
|
| 273 |
+
top_k: int = TOP_K_DEFAULT,
|
| 274 |
+
) -> str:
|
| 275 |
+
hits = search_chunks(query, index, records, top_k=top_k)
|
| 276 |
+
if not hits:
|
| 277 |
+
return "Bilgi bulunamadı."
|
| 278 |
+
|
| 279 |
+
# 0) Kural-tabanlı hızlı çıkarım (tarih/kuruluş soruları)
|
| 280 |
+
rule_sent, rule_src = _extract_fact_sentence(query, hits)
|
| 281 |
+
if rule_sent:
|
| 282 |
+
return f"{rule_sent}\n\nKaynaklar:\n- {rule_src if rule_src else _format_sources(hits)}"
|
| 283 |
+
|
| 284 |
+
# 1) Pasaj bazlı ekstraktif QA
|
| 285 |
+
best = {"answer": None, "score": 0.0, "src": None}
|
| 286 |
+
for h in hits[:QA_PER_PASSAGES]:
|
| 287 |
+
try:
|
| 288 |
+
qa = qa_extract(query, h["text"])
|
| 289 |
+
except Exception:
|
| 290 |
+
qa = None
|
| 291 |
+
if qa and qa.get("answer"):
|
| 292 |
+
score = float(qa.get("score", 0.0))
|
| 293 |
+
ans = qa["answer"].strip()
|
| 294 |
+
|
| 295 |
+
# Cevap tarih/özel ad içeriyorsa ekstra güven
|
| 296 |
+
if re.search(r"\b(19\d{2}|20\d{2}|Atatürk|Gökçen|Kemal|Ankara|Fenerbahçe)\b",
|
| 297 |
+
ans, flags=re.IGNORECASE):
|
| 298 |
+
score += 0.30
|
| 299 |
+
|
| 300 |
+
# Çok kısa veya eksik isimse → bağlamdan tam özel ada genişlet
|
| 301 |
+
if len(ans.split()) <= 2:
|
| 302 |
+
ans = _expand_named_span(ans, hits)
|
| 303 |
+
|
| 304 |
+
if score > best["score"]:
|
| 305 |
+
best = {"answer": ans, "score": score, "src": h.get("source")}
|
| 306 |
+
|
| 307 |
+
if best["answer"] and best["score"] >= QA_SCORE_THRES:
|
| 308 |
+
final = best["answer"].strip()
|
| 309 |
+
# Soru "kimdir/kim" ise doğal cümleye dök
|
| 310 |
+
if any(k in (query or "").lower() for k in ["kimdir", "kim"]):
|
| 311 |
+
if not final.endswith("."):
|
| 312 |
+
final += "."
|
| 313 |
+
final = f"{final} {query.rstrip('?')} sorusunun yanıtıdır."
|
| 314 |
+
src_line = f"Kaynaklar:\n- {best['src']}" if best["src"] else "Kaynaklar:\n" + _format_sources(hits)
|
| 315 |
+
return f"{final}\n\n{src_line}"
|
| 316 |
+
|
| 317 |
+
# 2) QA düşük güven verdiyse → LLM (varsa)
|
| 318 |
+
context = _llm_context(hits)
|
| 319 |
+
prompt = (
|
| 320 |
+
"Aşağıdaki BAĞLAM Wikipedia parçalarından alınmıştır.\n"
|
| 321 |
+
"Sadece bu bağlamdan yararlanarak soruya kısa, net ve doğru bir Türkçe cevap ver.\n"
|
| 322 |
+
"Uydurma yapma, sadece metinlerde geçen bilgileri kullan.\n\n"
|
| 323 |
+
f"Soru:\n{query}\n\nBağlam:\n{context}\n\nYanıtı 1-2 cümlede ver."
|
| 324 |
+
)
|
| 325 |
+
llm_ans = (generate(prompt) or "").strip()
|
| 326 |
+
|
| 327 |
+
# 3) LLM yapılandırılmamışsa → güvenli özet fallback
|
| 328 |
+
if (not llm_ans) or ("yapılandırılmadı" in llm_ans.lower()):
|
| 329 |
+
text = hits[0].get("text", "")
|
| 330 |
+
first = re.split(r"(?<=[.!?])\s+", text.strip())[:2]
|
| 331 |
+
llm_ans = " ".join(first).strip() or "Verilen bağlamda bu sorunun cevabı bulunmamaktadır."
|
| 332 |
+
|
| 333 |
+
if "Kaynaklar:" not in llm_ans:
|
| 334 |
+
llm_ans += "\n\nKaynaklar:\n" + _format_sources(hits)
|
| 335 |
+
return llm_ans
|
| 336 |
+
|
| 337 |
+
# =========================
|
| 338 |
+
# Hızlı test
|
| 339 |
+
# =========================
|
| 340 |
+
|
| 341 |
+
if __name__ == "__main__":
|
| 342 |
+
idx, recs = load_vectorstore()
|
| 343 |
+
for q in [
|
| 344 |
+
"Atatürk ne zaman öldü?",
|
| 345 |
+
"Türkiye'nin ilk cumhurbaşkanı kimdir?",
|
| 346 |
+
"Fenerbahçe ne zaman kuruldu?",
|
| 347 |
+
"Türkiye'nin başkenti neresidir?",
|
| 348 |
+
"Türkiye'nin ilk kadın pilotu kimdir?",
|
| 349 |
+
]:
|
| 350 |
+
print("Soru:", q)
|
| 351 |
+
print(generate_answer(q, idx, recs, top_k=TOP_K_DEFAULT))
|
| 352 |
+
print("-" * 80)
|
requirements.txt
ADDED
|
@@ -0,0 +1,13 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
|
| 2 |
+
streamlit==1.36
|
| 3 |
+
numpy
|
| 4 |
+
faiss-cpu
|
| 5 |
+
datasets
|
| 6 |
+
transformers
|
| 7 |
+
sentence-transformers
|
| 8 |
+
einops
|
| 9 |
+
accelerate
|
| 10 |
+
scikit-learn
|
| 11 |
+
google-generativeai
|
| 12 |
+
python-dotenv
|
| 13 |
+
huggingface_hub
|