refac
Co-Authored-By: Classic298 <27028174+Classic298@users.noreply.github.com>
This commit is contained in:
1 parent
067114c280
commit
1d6d4e6e66
12 files changed
+191
-35
No files matched your search
@@ -3,7 +3,7 @@ NOTE: This vector database integration is community-supported and maintained on
|
||||
"""
|
||||
|
||||
import ssl
|
||||
from typing import Optional
|
||||
from typing import Any, Optional
|
||||
|
||||
from elasticsearch import BadRequestError, Elasticsearch
|
||||
from elasticsearch.helpers import bulk, scan
|
||||
@@ -23,7 +23,13 @@ from open_webui.retrieval.vector.main import (
|
||||
VectorDBBase,
|
||||
VectorItem,
|
||||
)
|
||||
from open_webui.retrieval.vector.utils import process_metadata
|
||||
from open_webui.retrieval.vector.utils import iter_filter_conditions, process_metadata
|
||||
|
||||
|
||||
def _metadata_filter(key: str, op: str, value: Any) -> dict:
|
||||
if op == '$in':
|
||||
return {'terms': {f'metadata.{key}': value}}
|
||||
return {'term': {f'metadata.{key}': value}}
|
||||
|
||||
|
||||
class ElasticsearchClient(VectorDBBase):
|
||||
@@ -161,12 +167,16 @@ class ElasticsearchClient(VectorDBBase):
|
||||
filter: Optional[dict] = None,
|
||||
limit: int = 10,
|
||||
) -> Optional[SearchResult]:
|
||||
filters = [{'term': {'collection': collection_name}}]
|
||||
if filter:
|
||||
filters.extend(_metadata_filter(key, op, value) for key, op, value in iter_filter_conditions(filter))
|
||||
|
||||
query = {
|
||||
'size': limit,
|
||||
'_source': ['text', 'metadata'],
|
||||
'query': {
|
||||
'script_score': {
|
||||
'query': {'bool': {'filter': [{'term': {'collection': collection_name}}]}},
|
||||
'query': {'bool': {'filter': filters}},
|
||||
'script': {
|
||||
'source': "cosineSimilarity(params.vector, 'vector') + 1.0",
|
||||
'params': {'vector': vectors[0]}, # Assuming single query vector
|
||||
|
||||
Reference in new issue
Block a user