mirror of
https://github.com/langgenius/dify.git
synced 2026-01-14 06:07:33 +08:00
Signed-off-by: -LAN- <laipz8200@outlook.com> Signed-off-by: kenwoodjw <blackxin55+@gmail.com> Signed-off-by: Yongtao Huang <yongtaoh2022@gmail.com> Signed-off-by: yihong0618 <zouzou0208@gmail.com> Signed-off-by: zhanluxianshen <zhanluxianshen@163.com> Co-authored-by: -LAN- <laipz8200@outlook.com> Co-authored-by: GuanMu <ballmanjq@gmail.com> Co-authored-by: Davide Delbianco <davide.delbianco@outlook.com> Co-authored-by: NeatGuyCoding <15627489+NeatGuyCoding@users.noreply.github.com> Co-authored-by: kenwoodjw <blackxin55+@gmail.com> Co-authored-by: Yongtao Huang <yongtaoh2022@gmail.com> Co-authored-by: Yongtao Huang <99629139+hyongtao-db@users.noreply.github.com> Co-authored-by: Qiang Lee <18018968632@163.com> Co-authored-by: 李强04 <liqiang04@gaotu.cn> Co-authored-by: autofix-ci[bot] <114827586+autofix-ci[bot]@users.noreply.github.com> Co-authored-by: Asuka Minato <i@asukaminato.eu.org> Co-authored-by: Matri Qi <matrixdom@126.com> Co-authored-by: huayaoyue6 <huayaoyue@163.com> Co-authored-by: Bowen Liang <liangbowen@gf.com.cn> Co-authored-by: znn <jubinkumarsoni@gmail.com> Co-authored-by: crazywoola <427733928@qq.com> Co-authored-by: crazywoola <100913391+crazywoola@users.noreply.github.com> Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com> Co-authored-by: yihong <zouzou0208@gmail.com> Co-authored-by: Muke Wang <shaodwaaron@gmail.com> Co-authored-by: wangmuke <wangmuke@kingsware.cn> Co-authored-by: Wu Tianwei <30284043+WTW0313@users.noreply.github.com> Co-authored-by: quicksand <quicksandzn@gmail.com> Co-authored-by: 非法操作 <hjlarry@163.com> Co-authored-by: zxhlyh <jasonapring2015@outlook.com> Co-authored-by: Eric Guo <eric.guocz@gmail.com> Co-authored-by: Zhedong Cen <cenzhedong2@126.com> Co-authored-by: jiangbo721 <jiangbo721@163.com> Co-authored-by: github-actions[bot] <41898282+github-actions[bot]@users.noreply.github.com> Co-authored-by: hjlarry <25834719+hjlarry@users.noreply.github.com> Co-authored-by: lxsummer <35754229+lxjustdoit@users.noreply.github.com> Co-authored-by: 湛露先生 <zhanluxianshen@163.com> Co-authored-by: Guangdong Liu <liugddx@gmail.com> Co-authored-by: QuantumGhost <obelisk.reg+git@gmail.com> Co-authored-by: Claude <noreply@anthropic.com> Co-authored-by: Yessenia-d <yessenia.contact@gmail.com> Co-authored-by: huangzhuo1949 <167434202+huangzhuo1949@users.noreply.github.com> Co-authored-by: huangzhuo <huangzhuo1@xiaomi.com> Co-authored-by: 17hz <0x149527@gmail.com> Co-authored-by: Amy <1530140574@qq.com> Co-authored-by: Joel <iamjoel007@gmail.com> Co-authored-by: Nite Knite <nkCoding@gmail.com> Co-authored-by: Yeuoly <45712896+Yeuoly@users.noreply.github.com> Co-authored-by: Petrus Han <petrus.hanks@gmail.com> Co-authored-by: iamjoel <2120155+iamjoel@users.noreply.github.com> Co-authored-by: Kalo Chin <frog.beepers.0n@icloud.com> Co-authored-by: Ujjwal Maurya <ujjwalsbx@gmail.com> Co-authored-by: Maries <xh001x@hotmail.com>
145 lines
6.7 KiB
Python
145 lines
6.7 KiB
Python
import base64
|
|
import logging
|
|
from typing import Any, Optional, cast
|
|
|
|
import numpy as np
|
|
from sqlalchemy.exc import IntegrityError
|
|
|
|
from configs import dify_config
|
|
from core.entities.embedding_type import EmbeddingInputType
|
|
from core.model_manager import ModelInstance
|
|
from core.model_runtime.entities.model_entities import ModelPropertyKey
|
|
from core.model_runtime.model_providers.__base.text_embedding_model import TextEmbeddingModel
|
|
from core.rag.embedding.embedding_base import Embeddings
|
|
from extensions.ext_database import db
|
|
from extensions.ext_redis import redis_client
|
|
from libs import helper
|
|
from models.dataset import Embedding
|
|
|
|
logger = logging.getLogger(__name__)
|
|
|
|
|
|
class CacheEmbedding(Embeddings):
|
|
def __init__(self, model_instance: ModelInstance, user: Optional[str] = None) -> None:
|
|
self._model_instance = model_instance
|
|
self._user = user
|
|
|
|
def embed_documents(self, texts: list[str]) -> list[list[float]]:
|
|
"""Embed search docs in batches of 10."""
|
|
# use doc embedding cache or store if not exists
|
|
text_embeddings: list[Any] = [None for _ in range(len(texts))]
|
|
embedding_queue_indices = []
|
|
for i, text in enumerate(texts):
|
|
hash = helper.generate_text_hash(text)
|
|
embedding = (
|
|
db.session.query(Embedding)
|
|
.filter_by(
|
|
model_name=self._model_instance.model, hash=hash, provider_name=self._model_instance.provider
|
|
)
|
|
.first()
|
|
)
|
|
if embedding:
|
|
text_embeddings[i] = embedding.get_embedding()
|
|
else:
|
|
embedding_queue_indices.append(i)
|
|
if embedding_queue_indices:
|
|
embedding_queue_texts = [texts[i] for i in embedding_queue_indices]
|
|
embedding_queue_embeddings = []
|
|
try:
|
|
model_type_instance = cast(TextEmbeddingModel, self._model_instance.model_type_instance)
|
|
model_schema = model_type_instance.get_model_schema(
|
|
self._model_instance.model, self._model_instance.credentials
|
|
)
|
|
max_chunks = (
|
|
model_schema.model_properties[ModelPropertyKey.MAX_CHUNKS]
|
|
if model_schema and ModelPropertyKey.MAX_CHUNKS in model_schema.model_properties
|
|
else 1
|
|
)
|
|
for i in range(0, len(embedding_queue_texts), max_chunks):
|
|
batch_texts = embedding_queue_texts[i : i + max_chunks]
|
|
|
|
embedding_result = self._model_instance.invoke_text_embedding(
|
|
texts=batch_texts, user=self._user, input_type=EmbeddingInputType.DOCUMENT
|
|
)
|
|
|
|
for vector in embedding_result.embeddings:
|
|
try:
|
|
# FIXME: type ignore for numpy here
|
|
normalized_embedding = (vector / np.linalg.norm(vector)).tolist() # type: ignore
|
|
# stackoverflow best way: https://stackoverflow.com/questions/20319813/how-to-check-list-containing-nan
|
|
if np.isnan(normalized_embedding).any():
|
|
# for issue #11827 float values are not json compliant
|
|
logger.warning("Normalized embedding is nan: %s", normalized_embedding)
|
|
continue
|
|
embedding_queue_embeddings.append(normalized_embedding)
|
|
except IntegrityError:
|
|
db.session.rollback()
|
|
except Exception:
|
|
logger.exception("Failed transform embedding")
|
|
cache_embeddings = []
|
|
try:
|
|
for i, n_embedding in zip(embedding_queue_indices, embedding_queue_embeddings):
|
|
text_embeddings[i] = n_embedding
|
|
hash = helper.generate_text_hash(texts[i])
|
|
if hash not in cache_embeddings:
|
|
embedding_cache = Embedding(
|
|
model_name=self._model_instance.model,
|
|
hash=hash,
|
|
provider_name=self._model_instance.provider,
|
|
)
|
|
embedding_cache.set_embedding(n_embedding)
|
|
db.session.add(embedding_cache)
|
|
cache_embeddings.append(hash)
|
|
db.session.commit()
|
|
except IntegrityError:
|
|
db.session.rollback()
|
|
except Exception as ex:
|
|
db.session.rollback()
|
|
logger.exception("Failed to embed documents")
|
|
raise ex
|
|
|
|
return text_embeddings
|
|
|
|
def embed_query(self, text: str) -> list[float]:
|
|
"""Embed query text."""
|
|
# use doc embedding cache or store if not exists
|
|
hash = helper.generate_text_hash(text)
|
|
embedding_cache_key = f"{self._model_instance.provider}_{self._model_instance.model}_{hash}"
|
|
embedding = redis_client.get(embedding_cache_key)
|
|
if embedding:
|
|
redis_client.expire(embedding_cache_key, 600)
|
|
decoded_embedding = np.frombuffer(base64.b64decode(embedding), dtype="float")
|
|
return [float(x) for x in decoded_embedding]
|
|
try:
|
|
embedding_result = self._model_instance.invoke_text_embedding(
|
|
texts=[text], user=self._user, input_type=EmbeddingInputType.QUERY
|
|
)
|
|
|
|
embedding_results = embedding_result.embeddings[0]
|
|
# FIXME: type ignore for numpy here
|
|
embedding_results = (embedding_results / np.linalg.norm(embedding_results)).tolist() # type: ignore
|
|
if np.isnan(embedding_results).any():
|
|
raise ValueError("Normalized embedding is nan please try again")
|
|
except Exception as ex:
|
|
if dify_config.DEBUG:
|
|
logger.exception("Failed to embed query text '%s...(%s chars)'", text[:10], len(text))
|
|
raise ex
|
|
|
|
try:
|
|
# encode embedding to base64
|
|
embedding_vector = np.array(embedding_results)
|
|
vector_bytes = embedding_vector.tobytes()
|
|
# Transform to Base64
|
|
encoded_vector = base64.b64encode(vector_bytes)
|
|
# Transform to string
|
|
encoded_str = encoded_vector.decode("utf-8")
|
|
redis_client.setex(embedding_cache_key, 600, encoded_str)
|
|
except Exception as ex:
|
|
if dify_config.DEBUG:
|
|
logger.exception(
|
|
"Failed to add embedding to redis for the text '%s...(%s chars)'", text[:10], len(text)
|
|
)
|
|
raise ex
|
|
|
|
return embedding_results # type: ignore
|