2023-06-25 16:49:14 +08:00
|
|
|
import logging
|
2024-01-02 23:42:00 +08:00
|
|
|
from typing import List, Optional
|
2023-06-25 16:49:14 +08:00
|
|
|
|
2023-08-23 19:10:11 +08:00
|
|
|
import numpy as np
|
2023-06-25 16:49:14 +08:00
|
|
|
from langchain.embeddings.base import Embeddings
|
|
|
|
from sqlalchemy.exc import IntegrityError
|
|
|
|
|
2024-01-02 23:42:00 +08:00
|
|
|
from core.model_manager import ModelInstance
|
2023-06-25 16:49:14 +08:00
|
|
|
from extensions.ext_database import db
|
|
|
|
from libs import helper
|
|
|
|
from models.dataset import Embedding
|
|
|
|
|
2024-01-02 23:42:00 +08:00
|
|
|
logger = logging.getLogger(__name__)
|
|
|
|
|
2023-06-25 16:49:14 +08:00
|
|
|
|
|
|
|
class CacheEmbedding(Embeddings):
|
2024-01-02 23:42:00 +08:00
|
|
|
def __init__(self, model_instance: ModelInstance, user: Optional[str] = None) -> None:
|
|
|
|
self._model_instance = model_instance
|
|
|
|
self._user = user
|
2023-06-25 16:49:14 +08:00
|
|
|
|
|
|
|
def embed_documents(self, texts: List[str]) -> List[List[float]]:
|
|
|
|
"""Embed search docs."""
|
|
|
|
# use doc embedding cache or store if not exists
|
2023-12-03 19:07:00 +08:00
|
|
|
text_embeddings = [None for _ in range(len(texts))]
|
|
|
|
embedding_queue_indices = []
|
|
|
|
for i, text in enumerate(texts):
|
2023-06-25 16:49:14 +08:00
|
|
|
hash = helper.generate_text_hash(text)
|
2024-01-02 23:42:00 +08:00
|
|
|
embedding = db.session.query(Embedding).filter_by(model_name=self._model_instance.model, hash=hash).first()
|
2023-06-25 16:49:14 +08:00
|
|
|
if embedding:
|
2023-12-03 19:07:00 +08:00
|
|
|
text_embeddings[i] = embedding.get_embedding()
|
2023-06-25 16:49:14 +08:00
|
|
|
else:
|
2023-12-03 19:07:00 +08:00
|
|
|
embedding_queue_indices.append(i)
|
2023-06-25 16:49:14 +08:00
|
|
|
|
2023-12-03 19:07:00 +08:00
|
|
|
if embedding_queue_indices:
|
2023-08-12 00:57:00 +08:00
|
|
|
try:
|
2024-01-02 23:42:00 +08:00
|
|
|
embedding_result = self._model_instance.invoke_text_embedding(
|
|
|
|
texts=[texts[i] for i in embedding_queue_indices],
|
|
|
|
user=self._user
|
|
|
|
)
|
|
|
|
|
|
|
|
embedding_results = embedding_result.embeddings
|
2023-08-12 00:57:00 +08:00
|
|
|
except Exception as ex:
|
2024-01-02 23:42:00 +08:00
|
|
|
logger.error('Failed to embed documents: ', ex)
|
|
|
|
raise ex
|
2023-12-03 19:07:00 +08:00
|
|
|
|
|
|
|
for i, indice in enumerate(embedding_queue_indices):
|
|
|
|
hash = helper.generate_text_hash(texts[indice])
|
2023-06-25 16:49:14 +08:00
|
|
|
|
2023-08-12 00:57:00 +08:00
|
|
|
try:
|
2024-01-02 23:42:00 +08:00
|
|
|
embedding = Embedding(model_name=self._model_instance.model, hash=hash)
|
2023-08-23 19:10:11 +08:00
|
|
|
vector = embedding_results[i]
|
|
|
|
normalized_embedding = (vector / np.linalg.norm(vector)).tolist()
|
2023-12-03 19:07:00 +08:00
|
|
|
text_embeddings[indice] = normalized_embedding
|
2023-08-23 19:10:11 +08:00
|
|
|
embedding.set_embedding(normalized_embedding)
|
2023-08-12 00:57:00 +08:00
|
|
|
db.session.add(embedding)
|
|
|
|
db.session.commit()
|
|
|
|
except IntegrityError:
|
|
|
|
db.session.rollback()
|
|
|
|
continue
|
|
|
|
except:
|
|
|
|
logging.exception('Failed to add embedding to db')
|
|
|
|
continue
|
2023-06-25 16:49:14 +08:00
|
|
|
|
|
|
|
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)
|
2024-01-02 23:42:00 +08:00
|
|
|
embedding = db.session.query(Embedding).filter_by(model_name=self._model_instance.model, hash=hash).first()
|
2023-06-25 16:49:14 +08:00
|
|
|
if embedding:
|
|
|
|
return embedding.get_embedding()
|
|
|
|
|
2023-08-12 00:57:00 +08:00
|
|
|
try:
|
2024-01-02 23:42:00 +08:00
|
|
|
embedding_result = self._model_instance.invoke_text_embedding(
|
|
|
|
texts=[text],
|
|
|
|
user=self._user
|
|
|
|
)
|
|
|
|
|
|
|
|
embedding_results = embedding_result.embeddings[0]
|
2023-08-23 19:10:11 +08:00
|
|
|
embedding_results = (embedding_results / np.linalg.norm(embedding_results)).tolist()
|
2023-08-12 00:57:00 +08:00
|
|
|
except Exception as ex:
|
2024-01-02 23:42:00 +08:00
|
|
|
raise ex
|
2023-06-25 16:49:14 +08:00
|
|
|
|
|
|
|
try:
|
2024-01-02 23:42:00 +08:00
|
|
|
embedding = Embedding(model_name=self._model_instance.model, hash=hash)
|
2023-06-25 16:49:14 +08:00
|
|
|
embedding.set_embedding(embedding_results)
|
|
|
|
db.session.add(embedding)
|
|
|
|
db.session.commit()
|
|
|
|
except IntegrityError:
|
|
|
|
db.session.rollback()
|
|
|
|
except:
|
|
|
|
logging.exception('Failed to add embedding to db')
|
|
|
|
|
|
|
|
return embedding_results
|