-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
dashboard ukpl data with hybrid search (#28)
* hybrid search works * fix text area * fix linter * fix nbdev
- Loading branch information
1 parent
c70a092
commit 1641858
Showing
15 changed files
with
418 additions
and
14 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,40 +1,60 @@ | ||
from typing import Any | ||
import streamlit as st | ||
|
||
from juddges.data.datasets import get_mongo_collection | ||
import streamlit as st | ||
from pymongo.collection import Collection | ||
from sentence_transformers import SentenceTransformer | ||
|
||
from juddges.data.database import get_mongo_collection | ||
from juddges.retrieval.mongo_hybrid_search import run_hybrid_search | ||
from juddges.settings import TEXT_EMBEDDING_MODEL | ||
|
||
TITLE = "Search for Judgements" | ||
|
||
st.set_page_config(page_title=TITLE, page_icon="⚖️", layout="wide") | ||
|
||
st.title(TITLE) | ||
st.header( | ||
"Search is based on hybrid search using text and vector search with the same priority for both." | ||
) | ||
|
||
judgement_country = st.sidebar.selectbox("Select judgement country", ["pl", "uk"]) | ||
judgement_collection_name = f"{judgement_country}-court" | ||
st.sidebar.info(f"Selected country: {judgement_collection_name}") | ||
|
||
|
||
@st.cache_resource | ||
def get_judgements_collection() -> Collection: | ||
return get_mongo_collection("judgements") | ||
def get_judgements_collection(collection_name: str = "pl-court") -> Collection: | ||
return get_mongo_collection(collection_name=collection_name) | ||
|
||
|
||
judgements_collection = get_judgements_collection() | ||
@st.cache_resource | ||
def get_embedding_model() -> Any: | ||
return SentenceTransformer(TEXT_EMBEDDING_MODEL) | ||
|
||
|
||
def search_data(query: str, max_judgements: int = 5) -> list[dict[str, Any]]: | ||
items = list(judgements_collection.find({"$text": {"$search": query}}).limit(max_judgements)) | ||
return items | ||
judgements_collection = get_judgements_collection(judgement_collection_name) | ||
|
||
model = get_embedding_model() | ||
|
||
with st.form(key="search_form"): | ||
text = st.text_area("What you are looking for in the judgements?") | ||
query = st.text_area("What you are looking for in the judgements?") | ||
max_judgements = st.slider("Max judgements to show", min_value=1, max_value=20, value=5) | ||
submit_button = st.form_submit_button(label="Search") | ||
|
||
if submit_button: | ||
with st.spinner("Searching..."): | ||
items = search_data(text, max_judgements) | ||
items = run_hybrid_search( | ||
collection=judgements_collection, | ||
collection_name=judgement_collection_name, | ||
embedding=model.encode(query).tolist(), | ||
query=query, | ||
limit=max_judgements, | ||
) | ||
|
||
st.header("Judgements - Results") | ||
for item in items: | ||
st.header(item["signature"]) | ||
st.subheader(item["publicationDate"]) | ||
st.write(item["text"]) | ||
st.info(f"Department: {item['department_name']}") | ||
st.info(f"Score: {item['score']}") | ||
st.subheader(item["excerpt"]) | ||
st.text_area(label="Judgement text", value=item["text"], height=200) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Empty file.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,110 @@ | ||
def run_hybrid_search( | ||
collection, | ||
collection_name: str, | ||
embedding, | ||
query: str, | ||
limit: int = 10, | ||
vector_priority: float = 1, | ||
text_priority: float = 1, | ||
): | ||
num_candidates = limit * 10 | ||
|
||
vector_search = { | ||
"$vectorSearch": { | ||
"index": "vector_index", | ||
"path": "embedding", | ||
"queryVector": embedding, | ||
"numCandidates": num_candidates, | ||
"limit": limit, | ||
} | ||
} | ||
|
||
make_array = {"$group": {"_id": None, "docs": {"$push": "$$ROOT"}}} | ||
|
||
add_rank = {"$unwind": {"path": "$docs", "includeArrayIndex": "rank"}} | ||
|
||
def make_compute_score_doc(priority, score_field_name): | ||
return { | ||
"$addFields": {score_field_name: {"$divide": [1.0, {"$add": ["$rank", priority, 1]}]}} | ||
} | ||
|
||
def make_projection_doc(score_field_name): | ||
return { | ||
"$project": { | ||
score_field_name: 1, | ||
"_id": "$docs._id", | ||
"excerpt": "$docs.excerpt", | ||
"text": "$docs.text", | ||
"department_name": "$docs.department_name", | ||
"signature": "$docs.signature", | ||
} | ||
} | ||
|
||
text_search = { | ||
"$search": { | ||
"index": "text_index", | ||
"text": {"query": query, "path": "text"}, | ||
} | ||
} | ||
|
||
limit_results = {"$limit": limit} | ||
|
||
combine_search_results = { | ||
"$group": { | ||
"_id": "$_id", | ||
"vs_score": {"$max": "$vs_score"}, | ||
"ts_score": {"$max": "$ts_score"}, | ||
"excerpt": {"$first": "$excerpt"}, | ||
"text": {"$first": "$text"}, | ||
"department_name": {"$first": "$department_name"}, | ||
"signature": {"$first": "$signature"}, | ||
} | ||
} | ||
|
||
project_combined_results = { | ||
"$project": { | ||
"_id": 1, | ||
"excerpt": 1, | ||
"text": 1, | ||
"department_name": 1, | ||
"signature": 1, | ||
"score": { | ||
"$let": { | ||
"vars": { | ||
"vs_score": {"$ifNull": ["$vs_score", 0]}, | ||
"ts_score": {"$ifNull": ["$ts_score", 0]}, | ||
}, | ||
"in": {"$add": ["$$vs_score", "$$ts_score"]}, | ||
} | ||
}, | ||
} | ||
} | ||
|
||
sort_results = {"$sort": {"score": -1}} | ||
|
||
pipeline = [ | ||
vector_search, | ||
make_array, | ||
add_rank, | ||
make_compute_score_doc(vector_priority, "vs_score"), | ||
make_projection_doc("vs_score"), | ||
{ | ||
"$unionWith": { | ||
"coll": collection_name, | ||
"pipeline": [ | ||
text_search, | ||
limit_results, | ||
make_array, | ||
add_rank, | ||
make_compute_score_doc(text_priority, "ts_score"), | ||
make_projection_doc("ts_score"), | ||
], | ||
} | ||
}, | ||
combine_search_results, | ||
project_combined_results, | ||
sort_results, | ||
limit_results, | ||
] | ||
|
||
return collection.aggregate(pipeline) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
/.quarto/ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.