changed index (removed description field & description vector). Small refactor in front and backend
LocationBasedServices/smartguide/pipeline/head This commit looks good
LocationBasedServices/smartguide/pipeline/head This commit looks good
This commit is contained in:
@@ -7,4 +7,4 @@ RUN mkdir /test && \
|
||||
pip install --root-user-action=ignore --no-cache-dir "sentence-transformers~=5.5.1"
|
||||
|
||||
ENV SENTENCE_TRANSFORMERS_HOME=/models
|
||||
RUN python -c "from sentence_transformers import SentenceTransformer; SentenceTransformer('paraphrase-multilingual-mpnet-base-v2')"
|
||||
RUN python -c "from sentence_transformers import SentenceTransformer; SentenceTransformer('intfloat/multilingual-e5-base')"
|
||||
|
||||
@@ -80,17 +80,6 @@ def update_mappings():
|
||||
"ef_construction": 100
|
||||
}
|
||||
},
|
||||
"description_vector": {
|
||||
"type": "dense_vector",
|
||||
"dims": 768,
|
||||
"index": True,
|
||||
"similarity": "cosine",
|
||||
"index_options": {
|
||||
"type": "int8_hnsw",
|
||||
"m": 16,
|
||||
"ef_construction": 100
|
||||
}
|
||||
},
|
||||
"keyword_vectors": {
|
||||
"type": "nested",
|
||||
"properties": {
|
||||
@@ -125,7 +114,7 @@ def update_mappings():
|
||||
resp.raise_for_status()
|
||||
print("Mappings update process completed.")
|
||||
|
||||
def bulk_index_topics(topics, display_name_vectors, description_vectors, keyword_vectors_list):
|
||||
def bulk_index_topics(topics, display_name_vectors, keyword_vectors_list):
|
||||
print("Bulk indexing vectorized topics into 'topicvectors'...")
|
||||
batch_size = 500
|
||||
|
||||
@@ -133,21 +122,18 @@ def bulk_index_topics(topics, display_name_vectors, description_vectors, keyword
|
||||
for i in range(0, len(topics), batch_size):
|
||||
batch_topics = topics[i:i+batch_size]
|
||||
batch_name_vecs = display_name_vectors[i:i+batch_size]
|
||||
batch_desc_vecs = description_vectors[i:i+batch_size]
|
||||
batch_kw_vecs_list = keyword_vectors_list[i:i+batch_size]
|
||||
|
||||
bulk_lines = []
|
||||
for t, name_vec, desc_vec, kw_vecs in zip(batch_topics, batch_name_vecs, batch_desc_vecs, batch_kw_vecs_list):
|
||||
for t, name_vec, kw_vecs in zip(batch_topics, batch_name_vecs, batch_kw_vecs_list):
|
||||
source = t["_source"]
|
||||
|
||||
source.pop("siblings", None) # drop siblings field when making the copy
|
||||
source.pop("description", None) # drop description field entirely
|
||||
|
||||
# Assign name vector as a standard float list
|
||||
source["display_name_vector"] = name_vec.tolist()
|
||||
|
||||
# Assign description vector
|
||||
source["description_vector"] = desc_vec.tolist()
|
||||
|
||||
# Assign nested keyword vectors
|
||||
source["keyword_vectors"] = [
|
||||
{"vector": vec.tolist()} for vec in kw_vecs
|
||||
@@ -196,41 +182,34 @@ def main():
|
||||
name = source.get("display_name") or ""
|
||||
all_texts_to_embed.append(name)
|
||||
|
||||
desc = source.get("description") or ""
|
||||
all_texts_to_embed.append(desc)
|
||||
|
||||
kws = source.get("keywords") or []
|
||||
if not isinstance(kws, list):
|
||||
kws = [kws] if kws else []
|
||||
keywords_by_topic.append(kws)
|
||||
all_texts_to_embed.extend(kws)
|
||||
|
||||
print(f"Embedding {len(all_texts_to_embed)} texts (display names, descriptions, and keywords)...")
|
||||
print(f"Embedding {len(all_texts_to_embed)} texts (display names and keywords)...")
|
||||
all_vectors = model.encode(all_texts_to_embed, show_progress_bar=True, batch_size=32)
|
||||
|
||||
# 2. Map embeddings back to topics
|
||||
display_name_vectors = []
|
||||
description_vectors = []
|
||||
keyword_vectors_list = []
|
||||
|
||||
vector_idx = 0
|
||||
for idx, t in enumerate(topics):
|
||||
|
||||
display_name_vectors.append(all_vectors[vector_idx])
|
||||
vector_idx += 1
|
||||
display_name_vectors.append(all_vectors[vector_idx])
|
||||
vector_idx += 1
|
||||
|
||||
description_vectors.append(all_vectors[vector_idx])
|
||||
vector_idx += 1
|
||||
|
||||
kws = keywords_by_topic[idx]
|
||||
topic_kw_vecs = []
|
||||
for _ in range(len(kws)):
|
||||
topic_kw_vecs.append(all_vectors[vector_idx])
|
||||
vector_idx += 1
|
||||
keyword_vectors_list.append(topic_kw_vecs)
|
||||
kws = keywords_by_topic[idx]
|
||||
topic_kw_vecs = []
|
||||
for _ in range(len(kws)):
|
||||
topic_kw_vecs.append(all_vectors[vector_idx])
|
||||
vector_idx += 1
|
||||
keyword_vectors_list.append(topic_kw_vecs)
|
||||
|
||||
# Bulk index
|
||||
bulk_index_topics(topics, display_name_vectors, description_vectors, keyword_vectors_list)
|
||||
bulk_index_topics(topics, display_name_vectors, keyword_vectors_list)
|
||||
print("Done!")
|
||||
|
||||
if __name__ == "__main__":
|
||||
|
||||
@@ -19,4 +19,4 @@ LLM_API_KEY=c2d5e7ccf7a4760dd3d19bed2caed9d85ff7437400e0ea82a6d6f912ebc2f212
|
||||
LLM_MODEL=gemma4-e4b
|
||||
|
||||
# Sentence Transformers Configuration
|
||||
SENTENCE_TRANSFORMERS_MODEL_NAME=paraphrase-multilingual-mpnet-base-v2
|
||||
SENTENCE_TRANSFORMERS_MODEL_NAME=intfloat/multilingual-e5-base
|
||||
|
||||
@@ -257,13 +257,16 @@ def _datetime_str_or_none(value: Any) -> str | None:
|
||||
return text
|
||||
|
||||
# Configuration for vector search similarity thresholds and boosts
|
||||
SIMILARITY_THRESHOLD = 0.62
|
||||
SIMILARITY_THRESHOLD = 0.8 # intfloat/multilingual-e5-base uses cosine similarity between 0.7 - 1
|
||||
BOOST_LEXICAL = 1.0
|
||||
# higher values for semantic weights, because cosine similarity is between 0 and 1
|
||||
BOOST_DISPLAY_NAME_VECTOR = 30.0
|
||||
BOOST_DESCRIPTION_VECTOR = 15.0
|
||||
BOOST_KEYWORD_VECTORS = 25.0
|
||||
|
||||
# Approximate Nearest Neighbor (ANN) => higher values = slower but more accurate (nested Keyword vectors require more)
|
||||
K = 500
|
||||
NUM_CANDIDATES = 500
|
||||
|
||||
async def vector_search_topics(
|
||||
*,
|
||||
query: str,
|
||||
@@ -305,8 +308,7 @@ async def vector_search_topics(
|
||||
"query": query,
|
||||
"fields": [
|
||||
"display_name^3",
|
||||
"keywords^2",
|
||||
"description^1"
|
||||
"keywords^2"
|
||||
],
|
||||
"boost": BOOST_LEXICAL
|
||||
}
|
||||
@@ -320,18 +322,8 @@ async def vector_search_topics(
|
||||
"query_vector": query_vector,
|
||||
"similarity": SIMILARITY_THRESHOLD,
|
||||
"boost": BOOST_DISPLAY_NAME_VECTOR,
|
||||
"k": 500,
|
||||
"num_candidates": 500
|
||||
}
|
||||
},
|
||||
{
|
||||
"knn": {
|
||||
"field": "description_vector",
|
||||
"query_vector": query_vector,
|
||||
"similarity": SIMILARITY_THRESHOLD,
|
||||
"boost": BOOST_DESCRIPTION_VECTOR,
|
||||
"k": 500,
|
||||
"num_candidates": 500
|
||||
"k": K,
|
||||
"num_candidates": NUM_CANDIDATES
|
||||
}
|
||||
},
|
||||
{
|
||||
@@ -342,8 +334,8 @@ async def vector_search_topics(
|
||||
"field": "keyword_vectors.vector",
|
||||
"query_vector": query_vector,
|
||||
"similarity": SIMILARITY_THRESHOLD,
|
||||
"k": 500,
|
||||
"num_candidates": 500
|
||||
"k": K,
|
||||
"num_candidates": NUM_CANDIDATES
|
||||
}
|
||||
},
|
||||
"score_mode": "max",
|
||||
@@ -359,7 +351,7 @@ async def vector_search_topics(
|
||||
}
|
||||
},
|
||||
"_source": {
|
||||
"excludes": ["display_name_vector", "description_vector", "keyword_vectors", "description"] # remove unrequired fields
|
||||
"excludes": ["display_name_vector", "keyword_vectors", "works_api_url", "updated_date", "created_date", "_index" ] # remove unrequired fields
|
||||
},
|
||||
"size": limit
|
||||
}
|
||||
|
||||
@@ -23,11 +23,23 @@
|
||||
[value]="searchType()"
|
||||
(change)="onSearchTypeChange($event.value)"
|
||||
style="display: flex; gap: 16px">
|
||||
<mat-radio-button value="hybrid">
|
||||
<span style="font-size: 12px; color: var(--sg-text)">Hybrid</span>
|
||||
<mat-radio-button
|
||||
#tooltipSem="matTooltip"
|
||||
value="semantic"
|
||||
matTooltip="Findet Themen mit ähnlicher Bedeutung, auch ohne exakte Übereinstimmung der Worte."
|
||||
matTooltipPosition="above"
|
||||
(focusin)="tooltipSem.show()"
|
||||
(focusout)="tooltipSem.hide()">
|
||||
<span style="font-size: 12px; color: var(--sg-text)">Ähnlich</span>
|
||||
</mat-radio-button>
|
||||
<mat-radio-button value="semantic">
|
||||
<span style="font-size: 12px; color: var(--sg-text)">Semantisch</span>
|
||||
<mat-radio-button
|
||||
#tooltipGen="matTooltip"
|
||||
value="hybrid"
|
||||
matTooltip="Sucht nach exakten Wortübereinstimmungen kombiniert mit Relevanzgewichtung."
|
||||
matTooltipPosition="above"
|
||||
(focusin)="tooltipGen.show()"
|
||||
(focusout)="tooltipGen.hide()">
|
||||
<span style="font-size: 12px; color: var(--sg-text)">Genau</span>
|
||||
</mat-radio-button>
|
||||
</mat-radio-group>
|
||||
</div>
|
||||
|
||||
Reference in New Issue
Block a user