mirror of
https://github.com/langgenius/dify.git
synced 2026-08-28 19:06:51 +08:00
[autofix.ci] apply automated fixes
This commit is contained in:
parent
18f9648a19
commit
928b17d0f1
@ -9,17 +9,17 @@ class PineconeConfig(BaseSettings):
|
||||
Configuration settings for Pinecone vector database
|
||||
"""
|
||||
|
||||
PINECONE_API_KEY: Optional[str] = Field(
|
||||
PINECONE_API_KEY: str | None = Field(
|
||||
description="API key for authenticating with Pinecone service",
|
||||
default=None,
|
||||
)
|
||||
|
||||
PINECONE_ENVIRONMENT: Optional[str] = Field(
|
||||
PINECONE_ENVIRONMENT: str | None = Field(
|
||||
description="Pinecone environment (e.g., 'us-west1-gcp', 'us-east-1-aws')",
|
||||
default=None,
|
||||
)
|
||||
|
||||
PINECONE_INDEX_NAME: Optional[str] = Field(
|
||||
PINECONE_INDEX_NAME: str | None = Field(
|
||||
description="Default Pinecone index name",
|
||||
default=None,
|
||||
)
|
||||
|
||||
@ -18,9 +18,10 @@ from models.dataset import Dataset, DatasetCollectionBinding
|
||||
|
||||
class PineconeConfig(BaseModel):
|
||||
"""Pinecone configuration class"""
|
||||
|
||||
api_key: str
|
||||
environment: str
|
||||
index_name: Optional[str] = None
|
||||
index_name: str | None = None
|
||||
timeout: float = 30
|
||||
batch_size: int = 100
|
||||
metric: str = "cosine"
|
||||
@ -48,14 +49,15 @@ class PineconeVector(BaseVector):
|
||||
# Normalize index name: lowercase, only a-z0-9- and <=45 chars
|
||||
import hashlib
|
||||
import re
|
||||
|
||||
base_name = collection_name.lower()
|
||||
base_name = re.sub(r'[^a-z0-9-]+', '-', base_name) # replace invalid chars with '-'
|
||||
base_name = re.sub(r'-+', '-', base_name).strip('-')
|
||||
base_name = re.sub(r"[^a-z0-9-]+", "-", base_name) # replace invalid chars with '-'
|
||||
base_name = re.sub(r"-+", "-", base_name).strip("-")
|
||||
# Use longer secure suffix to reduce collision risk
|
||||
suffix_len = 24 # 24 hex digits (96-bit entropy)
|
||||
if len(base_name) > 45:
|
||||
hash_suffix = hashlib.sha256(base_name.encode()).hexdigest()[:suffix_len]
|
||||
truncated_name = base_name[:45 - (suffix_len + 1)].rstrip('-')
|
||||
truncated_name = base_name[: 45 - (suffix_len + 1)].rstrip("-")
|
||||
self._index_name = f"{truncated_name}-{hash_suffix}"
|
||||
else:
|
||||
self._index_name = base_name
|
||||
@ -83,10 +85,7 @@ class PineconeVector(BaseVector):
|
||||
|
||||
def to_index_struct(self) -> dict:
|
||||
"""Generate index structure dictionary"""
|
||||
return {
|
||||
"type": self.get_type(),
|
||||
"vector_store": {"class_prefix": self._collection_name}
|
||||
}
|
||||
return {"type": self.get_type(), "vector_store": {"class_prefix": self._collection_name}}
|
||||
|
||||
def create(self, texts: list[Document], embeddings: list[list[float]], **kwargs):
|
||||
"""Create vector index"""
|
||||
@ -120,14 +119,11 @@ class PineconeVector(BaseVector):
|
||||
name=self._index_name,
|
||||
dimension=dimension,
|
||||
metric=self._client_config.metric,
|
||||
spec=ServerlessSpec(
|
||||
cloud='aws',
|
||||
region=self._client_config.environment
|
||||
)
|
||||
spec=ServerlessSpec(cloud="aws", region=self._client_config.environment),
|
||||
)
|
||||
|
||||
# Wait for index creation to complete
|
||||
while not self._pc.describe_index(self._index_name).status['ready']:
|
||||
while not self._pc.describe_index(self._index_name).status["ready"]:
|
||||
time.sleep(1)
|
||||
else:
|
||||
# Get index instance
|
||||
@ -162,12 +158,8 @@ class PineconeVector(BaseVector):
|
||||
safe_meta: dict[str, Any] = {}
|
||||
# lift common identifiers to top-level fields for filtering
|
||||
for k, v in raw_meta.items():
|
||||
if (
|
||||
isinstance(v, (str, int, float, bool))
|
||||
or (
|
||||
isinstance(v, list)
|
||||
and all(isinstance(x, str) for x in v)
|
||||
)
|
||||
if isinstance(v, (str, int, float, bool)) or (
|
||||
isinstance(v, list) and all(isinstance(x, str) for x in v)
|
||||
):
|
||||
safe_meta[k] = v
|
||||
else:
|
||||
@ -178,11 +170,7 @@ class PineconeVector(BaseVector):
|
||||
# group id as string
|
||||
safe_meta[Field.GROUP_KEY.value] = str(self._group_id)
|
||||
|
||||
vectors_to_upsert.append({
|
||||
"id": doc_id,
|
||||
"values": embedding,
|
||||
"metadata": safe_meta
|
||||
})
|
||||
vectors_to_upsert.append({"id": doc_id, "values": embedding, "metadata": safe_meta})
|
||||
|
||||
# Batch insert to Pinecone
|
||||
try:
|
||||
@ -211,12 +199,7 @@ class PineconeVector(BaseVector):
|
||||
|
||||
# Execute search
|
||||
try:
|
||||
response = self._index.query(
|
||||
vector=query_vector,
|
||||
top_k=top_k,
|
||||
include_metadata=True,
|
||||
filter=filter_dict
|
||||
)
|
||||
response = self._index.query(vector=query_vector, top_k=top_k, include_metadata=True, filter=filter_dict)
|
||||
except Exception as e:
|
||||
raise
|
||||
|
||||
@ -253,7 +236,7 @@ class PineconeVector(BaseVector):
|
||||
# Build filter conditions
|
||||
filter_dict = {
|
||||
Field.GROUP_KEY.value: {"$eq": self._group_id},
|
||||
f"{Field.METADATA_KEY.value}.{key}": {"$eq": value}
|
||||
f"{Field.METADATA_KEY.value}.{key}": {"$eq": value},
|
||||
}
|
||||
|
||||
# Pinecone delete operation
|
||||
@ -326,9 +309,7 @@ class PineconeVectorFactory(AbstractVectorFactory):
|
||||
|
||||
# Set index structure
|
||||
if not dataset.index_struct_dict:
|
||||
dataset.index_struct = json.dumps(
|
||||
self.gen_index_struct_dict("pinecone", collection_name)
|
||||
)
|
||||
dataset.index_struct = json.dumps(self.gen_index_struct_dict("pinecone", collection_name))
|
||||
|
||||
# Create PineconeVector instance
|
||||
return PineconeVector(
|
||||
|
||||
@ -24,5 +24,4 @@ class PineconeVectorTest(AbstractVectorTest):
|
||||
|
||||
|
||||
def test_pinecone_vector():
|
||||
|
||||
PineconeVectorTest().run_all_tests()
|
||||
Loading…
Reference in New Issue
Block a user