Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

refactor: add abstract decorator and output log when query embedding fails #9264

Merged
merged 1 commit into from
Oct 12, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
chore: add abstract decorator and output log when query embedding fails
  • Loading branch information
hwzhuhao committed Oct 12, 2024
commit 22f53b9613bc642a1f90c88cd8cac53ba0a4d54d
7 changes: 6 additions & 1 deletion api/core/embedding/cached_embedding.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
import numpy as np
from sqlalchemy.exc import IntegrityError

from configs import dify_config
from core.embedding.embedding_constant import EmbeddingInputType
from core.model_manager import ModelInstance
from core.model_runtime.entities.model_entities import ModelPropertyKey
Expand Down Expand Up @@ -110,6 +111,8 @@ def embed_query(self, text: str) -> list[float]:
embedding_results = embedding_result.embeddings[0]
embedding_results = (embedding_results / np.linalg.norm(embedding_results)).tolist()
except Exception as ex:
if dify_config.DEBUG:
logging.exception(f"Failed to embed query text: {ex}")
raise ex

try:
Expand All @@ -122,6 +125,8 @@ def embed_query(self, text: str) -> list[float]:
encoded_str = encoded_vector.decode("utf-8")
redis_client.setex(embedding_cache_key, 600, encoded_str)
except Exception as ex:
logging.exception("Failed to add embedding to redis %s", ex)
if dify_config.DEBUG:
logging.exception("Failed to add embedding to redis %s", ex)
raise ex

return embedding_results
2 changes: 2 additions & 0 deletions api/core/rag/datasource/keyword/keyword_base.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,9 +27,11 @@ def text_exists(self, id: str) -> bool:
def delete_by_ids(self, ids: list[str]) -> None:
raise NotImplementedError

@abstractmethod
def delete(self) -> None:
raise NotImplementedError

@abstractmethod
def search(self, query: str, **kwargs: Any) -> list[Document]:
raise NotImplementedError

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,7 @@ def _check_version(self):
raise ValueError("Elasticsearch vector database version must be greater than 8.0.0")

def get_type(self) -> str:
return "elasticsearch"
return VectorType.ELASTICSEARCH

def add_texts(self, documents: list[Document], embeddings: list[list[float]], **kwargs):
uuids = self._get_uuids(documents)
Expand Down