|
@@ -7,8 +7,8 @@ _import_err_msg = (
|
|
|
"`alibabacloud_gpdb20160503` and `alibabacloud_tea_openapi` packages not found, "
|
|
|
"please run `pip install alibabacloud_gpdb20160503 alibabacloud_tea_openapi`"
|
|
|
)
|
|
|
-from flask import current_app
|
|
|
|
|
|
+from configs import dify_config
|
|
|
from core.rag.datasource.entity.embedding import Embeddings
|
|
|
from core.rag.datasource.vdb.vector_base import BaseVector
|
|
|
from core.rag.datasource.vdb.vector_factory import AbstractVectorFactory
|
|
@@ -36,7 +36,7 @@ class AnalyticdbConfig(BaseModel):
|
|
|
"region_id": self.region_id,
|
|
|
"read_timeout": self.read_timeout,
|
|
|
}
|
|
|
-
|
|
|
+
|
|
|
class AnalyticdbVector(BaseVector):
|
|
|
_instance = None
|
|
|
_init = False
|
|
@@ -45,7 +45,7 @@ class AnalyticdbVector(BaseVector):
|
|
|
if cls._instance is None:
|
|
|
cls._instance = super().__new__(cls)
|
|
|
return cls._instance
|
|
|
-
|
|
|
+
|
|
|
def __init__(self, collection_name: str, config: AnalyticdbConfig):
|
|
|
# collection_name must be updated every time
|
|
|
self._collection_name = collection_name.lower()
|
|
@@ -105,7 +105,7 @@ class AnalyticdbVector(BaseVector):
|
|
|
raise ValueError(
|
|
|
f"failed to create namespace {self.config.namespace}: {e}"
|
|
|
)
|
|
|
-
|
|
|
+
|
|
|
def _create_collection_if_not_exists(self, embedding_dimension: int):
|
|
|
from alibabacloud_gpdb20160503 import models as gpdb_20160503_models
|
|
|
from Tea.exceptions import TeaException
|
|
@@ -149,7 +149,7 @@ class AnalyticdbVector(BaseVector):
|
|
|
|
|
|
def get_type(self) -> str:
|
|
|
return VectorType.ANALYTICDB
|
|
|
-
|
|
|
+
|
|
|
def create(self, texts: list[Document], embeddings: list[list[float]], **kwargs):
|
|
|
dimension = len(embeddings[0])
|
|
|
self._create_collection_if_not_exists(dimension)
|
|
@@ -199,7 +199,7 @@ class AnalyticdbVector(BaseVector):
|
|
|
)
|
|
|
response = self._client.query_collection_data(request)
|
|
|
return len(response.body.matches.match) > 0
|
|
|
-
|
|
|
+
|
|
|
def delete_by_ids(self, ids: list[str]) -> None:
|
|
|
from alibabacloud_gpdb20160503 import models as gpdb_20160503_models
|
|
|
ids_str = ",".join(f"'{id}'" for id in ids)
|
|
@@ -260,7 +260,7 @@ class AnalyticdbVector(BaseVector):
|
|
|
)
|
|
|
documents.append(doc)
|
|
|
return documents
|
|
|
-
|
|
|
+
|
|
|
def search_by_full_text(self, query: str, **kwargs: Any) -> list[Document]:
|
|
|
from alibabacloud_gpdb20160503 import models as gpdb_20160503_models
|
|
|
score_threshold = (
|
|
@@ -291,7 +291,7 @@ class AnalyticdbVector(BaseVector):
|
|
|
)
|
|
|
documents.append(doc)
|
|
|
return documents
|
|
|
-
|
|
|
+
|
|
|
def delete(self) -> None:
|
|
|
from alibabacloud_gpdb20160503 import models as gpdb_20160503_models
|
|
|
request = gpdb_20160503_models.DeleteCollectionRequest(
|
|
@@ -316,17 +316,18 @@ class AnalyticdbVectorFactory(AbstractVectorFactory):
|
|
|
dataset.index_struct = json.dumps(
|
|
|
self.gen_index_struct_dict(VectorType.ANALYTICDB, collection_name)
|
|
|
)
|
|
|
- config = current_app.config
|
|
|
+
|
|
|
+ # TODO handle optional params
|
|
|
return AnalyticdbVector(
|
|
|
collection_name,
|
|
|
AnalyticdbConfig(
|
|
|
- access_key_id=config.get("ANALYTICDB_KEY_ID"),
|
|
|
- access_key_secret=config.get("ANALYTICDB_KEY_SECRET"),
|
|
|
- region_id=config.get("ANALYTICDB_REGION_ID"),
|
|
|
- instance_id=config.get("ANALYTICDB_INSTANCE_ID"),
|
|
|
- account=config.get("ANALYTICDB_ACCOUNT"),
|
|
|
- account_password=config.get("ANALYTICDB_PASSWORD"),
|
|
|
- namespace=config.get("ANALYTICDB_NAMESPACE"),
|
|
|
- namespace_password=config.get("ANALYTICDB_NAMESPACE_PASSWORD"),
|
|
|
+ access_key_id=dify_config.ANALYTICDB_KEY_ID,
|
|
|
+ access_key_secret=dify_config.ANALYTICDB_KEY_SECRET,
|
|
|
+ region_id=dify_config.ANALYTICDB_REGION_ID,
|
|
|
+ instance_id=dify_config.ANALYTICDB_INSTANCE_ID,
|
|
|
+ account=dify_config.ANALYTICDB_ACCOUNT,
|
|
|
+ account_password=dify_config.ANALYTICDB_PASSWORD,
|
|
|
+ namespace=dify_config.ANALYTICDB_NAMESPACE,
|
|
|
+ namespace_password=dify_config.ANALYTICDB_NAMESPACE_PASSWORD,
|
|
|
),
|
|
|
- )
|
|
|
+ )
|