Spaces:
Sleeping
Sleeping
adds doc searcher v2
Browse files- app.py +8 -20
- suggestion_searcher.py → doc_searcher_v2.py +18 -46
- reranker.py +1 -1
app.py
CHANGED
|
@@ -1,6 +1,7 @@
|
|
| 1 |
import nh3
|
| 2 |
from fastapi import FastAPI, Request
|
| 3 |
from doc_searcher import DocSearcher
|
|
|
|
| 4 |
from suggestion_searcher import SuggestionSearcher
|
| 5 |
from huggingface_hub import login
|
| 6 |
from config import HUGGING_FACE_API_KEY, COLLECTION_NAME, API_KEY, COLLECTION_NAME_SUGGESTION
|
|
@@ -12,34 +13,21 @@ login(HUGGING_FACE_API_KEY)
|
|
| 12 |
app = FastAPI()
|
| 13 |
|
| 14 |
doc_searcher = DocSearcher(collection_name=COLLECTION_NAME)
|
|
|
|
| 15 |
suggestion_searcher = SuggestionSearcher(collection_name=COLLECTION_NAME_SUGGESTION)
|
| 16 |
|
| 17 |
ALLOWED_API_KEY = str(API_KEY)
|
| 18 |
|
| 19 |
@app.get("/api/search")
|
| 20 |
-
async def search(q: str):
|
| 21 |
-
# q: str, type: int, lt: str | None = None, offset: int = 0
|
| 22 |
query = q.lower()
|
| 23 |
xss = nh3.clean(query)
|
| 24 |
-
|
| 25 |
-
data = await doc_searcher.search_temp(text=xss)
|
| 26 |
return data
|
| 27 |
|
| 28 |
-
@app.get("/api/
|
| 29 |
-
async def
|
| 30 |
query = q.lower()
|
| 31 |
xss = nh3.clean(query)
|
| 32 |
-
data = await
|
| 33 |
-
return data
|
| 34 |
-
|
| 35 |
-
|
| 36 |
-
# @app.middleware("http")
|
| 37 |
-
# async def api_key_authentication(request: Request, call_next):
|
| 38 |
-
# api_key = request.headers.get("X-API-KEY")
|
| 39 |
-
# if api_key != ALLOWED_API_KEY:
|
| 40 |
-
# return JSONResponse(
|
| 41 |
-
# status_code=403,
|
| 42 |
-
# content={"message": "Forbidden."}
|
| 43 |
-
# )
|
| 44 |
-
|
| 45 |
-
# return await call_next(request)
|
|
|
|
| 1 |
import nh3
|
| 2 |
from fastapi import FastAPI, Request
|
| 3 |
from doc_searcher import DocSearcher
|
| 4 |
+
from doc_searcher_v2 import DocSearcherV2
|
| 5 |
from suggestion_searcher import SuggestionSearcher
|
| 6 |
from huggingface_hub import login
|
| 7 |
from config import HUGGING_FACE_API_KEY, COLLECTION_NAME, API_KEY, COLLECTION_NAME_SUGGESTION
|
|
|
|
| 13 |
app = FastAPI()
|
| 14 |
|
| 15 |
doc_searcher = DocSearcher(collection_name=COLLECTION_NAME)
|
| 16 |
+
doc_searcher_v2 = DocSearcherV2(collection_name=COLLECTION_NAME)
|
| 17 |
suggestion_searcher = SuggestionSearcher(collection_name=COLLECTION_NAME_SUGGESTION)
|
| 18 |
|
| 19 |
ALLOWED_API_KEY = str(API_KEY)
|
| 20 |
|
| 21 |
@app.get("/api/search")
|
| 22 |
+
async def search(q: str, type: int, lt: str | None = None, offset: int = 0):
|
|
|
|
| 23 |
query = q.lower()
|
| 24 |
xss = nh3.clean(query)
|
| 25 |
+
data = await doc_searcher.search(text=xss,type=type,law_type=lt,offset=offset)
|
|
|
|
| 26 |
return data
|
| 27 |
|
| 28 |
+
@app.get("/api/v2/search")
|
| 29 |
+
async def v2_search(q: str):
|
| 30 |
query = q.lower()
|
| 31 |
xss = nh3.clean(query)
|
| 32 |
+
data = await doc_searcher_v2.search_temp(text=xss)
|
| 33 |
+
return data
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
suggestion_searcher.py → doc_searcher_v2.py
RENAMED
|
@@ -1,76 +1,48 @@
|
|
| 1 |
from qdrant_client import QdrantClient
|
| 2 |
-
from
|
| 3 |
-
from fastembed import SparseTextEmbedding, LateInteractionTextEmbedding
|
| 4 |
from qdrant_client import QdrantClient, models
|
|
|
|
| 5 |
from sentence_transformers import SentenceTransformer
|
| 6 |
-
from config import DENSE_MODEL, SPARSE_MODEL,
|
| 7 |
|
| 8 |
-
class
|
| 9 |
|
| 10 |
def __init__(self, collection_name):
|
| 11 |
self.collection_name = collection_name
|
| 12 |
-
self.
|
|
|
|
| 13 |
self.sparse_model = SparseTextEmbedding(SPARSE_MODEL)
|
| 14 |
-
self.late_interaction_model = LateInteractionTextEmbedding(LATE_INTERACTION_MODEL)
|
| 15 |
self.qdrant_client = QdrantClient(QDRANT_URL,api_key=QDRANT_API_KEY,timeout=30)
|
| 16 |
|
| 17 |
-
async def
|
| 18 |
|
| 19 |
-
|
|
|
|
| 20 |
sparse_query = next(self.sparse_model.query_embed(text))
|
| 21 |
|
| 22 |
prefetch = [
|
| 23 |
models.Prefetch(
|
| 24 |
query=dense_query,
|
| 25 |
-
|
| 26 |
-
|
| 27 |
-
rescore=False,
|
| 28 |
-
),
|
| 29 |
-
),
|
| 30 |
-
using=DENSE_MODEL,
|
| 31 |
-
limit=10000
|
| 32 |
),
|
| 33 |
-
models.Prefetch(
|
| 34 |
-
query=models.SparseVector(**sparse_query.as_object()),
|
| 35 |
-
params=models.SearchParams(
|
| 36 |
-
quantization=models.QuantizationSearchParams(
|
| 37 |
-
rescore=False,
|
| 38 |
-
),
|
| 39 |
-
),
|
| 40 |
-
using=SPARSE_MODEL,
|
| 41 |
-
limit=10000
|
| 42 |
-
)
|
| 43 |
]
|
| 44 |
-
|
| 45 |
-
self.qdrant_client.scroll
|
| 46 |
search_result = self.qdrant_client.query_points(
|
| 47 |
-
collection_name=
|
| 48 |
-
query_filter=Filter(
|
| 49 |
-
must=[
|
| 50 |
-
FieldCondition(
|
| 51 |
-
key="tip",
|
| 52 |
-
match=MatchValue(value=type)
|
| 53 |
-
)
|
| 54 |
-
]
|
| 55 |
-
),
|
| 56 |
-
search_params=models.SearchParams(
|
| 57 |
-
hnsw_ef=64,
|
| 58 |
-
exact=False,
|
| 59 |
-
quantization=models.QuantizationSearchParams(
|
| 60 |
-
rescore=True,
|
| 61 |
-
),
|
| 62 |
-
),
|
| 63 |
prefetch=prefetch,
|
| 64 |
query=models.FusionQuery(
|
| 65 |
fusion=models.Fusion.RRF,
|
| 66 |
),
|
| 67 |
with_payload=True,
|
| 68 |
-
limit =
|
| 69 |
).points
|
| 70 |
|
| 71 |
data = []
|
| 72 |
|
| 73 |
for hit in search_result:
|
| 74 |
-
data.append(hit.payload)
|
|
|
|
|
|
|
| 75 |
|
| 76 |
-
return
|
|
|
|
| 1 |
from qdrant_client import QdrantClient
|
| 2 |
+
from fastembed import SparseTextEmbedding
|
|
|
|
| 3 |
from qdrant_client import QdrantClient, models
|
| 4 |
+
from reranker import Reranker
|
| 5 |
from sentence_transformers import SentenceTransformer
|
| 6 |
+
from config import DENSE_MODEL, SPARSE_MODEL, QDRANT_URL, QDRANT_API_KEY
|
| 7 |
|
| 8 |
+
class DocSearcherV2:
|
| 9 |
|
| 10 |
def __init__(self, collection_name):
|
| 11 |
self.collection_name = collection_name
|
| 12 |
+
self.reranker = Reranker()
|
| 13 |
+
self.model = SentenceTransformer("Qwen/Qwen3-Embedding-0.6B",device="cpu")
|
| 14 |
self.sparse_model = SparseTextEmbedding(SPARSE_MODEL)
|
|
|
|
| 15 |
self.qdrant_client = QdrantClient(QDRANT_URL,api_key=QDRANT_API_KEY,timeout=30)
|
| 16 |
|
| 17 |
+
async def search_temp(self, text: str):
|
| 18 |
|
| 19 |
+
queries = [text]
|
| 20 |
+
dense_query = self.model.encode(text).tolist()
|
| 21 |
sparse_query = next(self.sparse_model.query_embed(text))
|
| 22 |
|
| 23 |
prefetch = [
|
| 24 |
models.Prefetch(
|
| 25 |
query=dense_query,
|
| 26 |
+
using="Qwen/Qwen3-Embedding-0.6B",
|
| 27 |
+
limit=100
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 28 |
),
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 29 |
]
|
| 30 |
+
|
|
|
|
| 31 |
search_result = self.qdrant_client.query_points(
|
| 32 |
+
collection_name= "sl-list",
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 33 |
prefetch=prefetch,
|
| 34 |
query=models.FusionQuery(
|
| 35 |
fusion=models.Fusion.RRF,
|
| 36 |
),
|
| 37 |
with_payload=True,
|
| 38 |
+
limit = 100,
|
| 39 |
).points
|
| 40 |
|
| 41 |
data = []
|
| 42 |
|
| 43 |
for hit in search_result:
|
| 44 |
+
data.append(hit.payload["tekst"])
|
| 45 |
+
|
| 46 |
+
scores = self.reranker.compute_logits(queries,data)
|
| 47 |
|
| 48 |
+
return scores
|
reranker.py
CHANGED
|
@@ -16,7 +16,7 @@ class Reranker:
|
|
| 16 |
def process_inputs(self,pairs):
|
| 17 |
prefix = (
|
| 18 |
"<|im_start|>system\n"
|
| 19 |
-
"
|
| 20 |
"Dokument treba da bude relevantan, tačan i u skladu sa važećim pravnim propisima i standardima. "
|
| 21 |
"Odgovor mora biti striktno \"da\" ako ispunjava uslove, ili \"ne\" ako ne ispunjava.\n"
|
| 22 |
"<|im_end|>\n"
|
|
|
|
| 16 |
def process_inputs(self,pairs):
|
| 17 |
prefix = (
|
| 18 |
"<|im_start|>system\n"
|
| 19 |
+
"Procijeni da li dati Dokument adekvatno odgovara na Upit na osnovu pravne instrukcije. "
|
| 20 |
"Dokument treba da bude relevantan, tačan i u skladu sa važećim pravnim propisima i standardima. "
|
| 21 |
"Odgovor mora biti striktno \"da\" ako ispunjava uslove, ili \"ne\" ako ne ispunjava.\n"
|
| 22 |
"<|im_end|>\n"
|