fix: change milvus init args from (host, port) to (url, token) (#8019)

Signed-off-by: ChengZi <chen.zhang@zilliz.com>
This commit is contained in:
ChengZi 2024-09-06 17:32:48 +08:00 committed by GitHub
parent 9ded063417
commit 2060db8e11
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
9 changed files with 42 additions and 111 deletions

View File

@ -107,11 +107,10 @@ QDRANT_GRPC_ENABLED=false
QDRANT_GRPC_PORT=6334 QDRANT_GRPC_PORT=6334
# Milvus configuration # Milvus configuration
MILVUS_HOST=127.0.0.1 MILVUS_URI=http://127.0.0.1:19530
MILVUS_PORT=19530 MILVUS_TOKEN=
MILVUS_USER=root MILVUS_USER=root
MILVUS_PASSWORD=Milvus MILVUS_PASSWORD=Milvus
MILVUS_SECURE=false
# MyScale configuration # MyScale configuration
MYSCALE_HOST=127.0.0.1 MYSCALE_HOST=127.0.0.1

View File

@ -1,6 +1,6 @@
from typing import Optional from typing import Optional
from pydantic import Field, PositiveInt from pydantic import Field
from pydantic_settings import BaseSettings from pydantic_settings import BaseSettings
@ -9,14 +9,14 @@ class MilvusConfig(BaseSettings):
Milvus configs Milvus configs
""" """
MILVUS_HOST: Optional[str] = Field( MILVUS_URI: Optional[str] = Field(
description="Milvus host", description="Milvus uri",
default=None, default="http://127.0.0.1:19530",
) )
MILVUS_PORT: PositiveInt = Field( MILVUS_TOKEN: Optional[str] = Field(
description="Milvus RestFul API port", description="Milvus token",
default=9091, default=None,
) )
MILVUS_USER: Optional[str] = Field( MILVUS_USER: Optional[str] = Field(
@ -29,11 +29,6 @@ class MilvusConfig(BaseSettings):
default=None, default=None,
) )
MILVUS_SECURE: bool = Field(
description="whether to use SSL connection for Milvus",
default=False,
)
MILVUS_DATABASE: str = Field( MILVUS_DATABASE: str = Field(
description="Milvus database, default to `default`", description="Milvus database, default to `default`",
default="default", default="default",

View File

@ -1,10 +1,9 @@
import json import json
import logging import logging
from typing import Any, Optional from typing import Any, Optional
from uuid import uuid4
from pydantic import BaseModel, model_validator from pydantic import BaseModel, model_validator
from pymilvus import MilvusClient, MilvusException, connections from pymilvus import MilvusClient, MilvusException
from pymilvus.milvus_client import IndexParams from pymilvus.milvus_client import IndexParams
from configs import dify_config from configs import dify_config
@ -21,20 +20,17 @@ logger = logging.getLogger(__name__)
class MilvusConfig(BaseModel): class MilvusConfig(BaseModel):
host: str uri: str
port: int token: Optional[str] = None
user: str user: str
password: str password: str
secure: bool = False
batch_size: int = 100 batch_size: int = 100
database: str = "default" database: str = "default"
@model_validator(mode='before') @model_validator(mode='before')
def validate_config(cls, values: dict) -> dict: def validate_config(cls, values: dict) -> dict:
if not values.get('host'): if not values.get('uri'):
raise ValueError("config MILVUS_HOST is required") raise ValueError("config MILVUS_URI is required")
if not values.get('port'):
raise ValueError("config MILVUS_PORT is required")
if not values.get('user'): if not values.get('user'):
raise ValueError("config MILVUS_USER is required") raise ValueError("config MILVUS_USER is required")
if not values.get('password'): if not values.get('password'):
@ -43,11 +39,10 @@ class MilvusConfig(BaseModel):
def to_milvus_params(self): def to_milvus_params(self):
return { return {
'host': self.host, 'uri': self.uri,
'port': self.port, 'token': self.token,
'user': self.user, 'user': self.user,
'password': self.password, 'password': self.password,
'secure': self.secure,
'db_name': self.database, 'db_name': self.database,
} }
@ -111,32 +106,14 @@ class MilvusVector(BaseVector):
return None return None
def delete_by_metadata_field(self, key: str, value: str): def delete_by_metadata_field(self, key: str, value: str):
alias = uuid4().hex if self._client.has_collection(self._collection_name):
if self._client_config.secure:
uri = "https://" + str(self._client_config.host) + ":" + str(self._client_config.port)
else:
uri = "http://" + str(self._client_config.host) + ":" + str(self._client_config.port)
connections.connect(alias=alias, uri=uri, user=self._client_config.user, password=self._client_config.password,
db_name=self._client_config.database)
from pymilvus import utility
if utility.has_collection(self._collection_name, using=alias):
ids = self.get_ids_by_metadata_field(key, value) ids = self.get_ids_by_metadata_field(key, value)
if ids: if ids:
self._client.delete(collection_name=self._collection_name, pks=ids) self._client.delete(collection_name=self._collection_name, pks=ids)
def delete_by_ids(self, ids: list[str]) -> None: def delete_by_ids(self, ids: list[str]) -> None:
alias = uuid4().hex if self._client.has_collection(self._collection_name):
if self._client_config.secure:
uri = "https://" + str(self._client_config.host) + ":" + str(self._client_config.port)
else:
uri = "http://" + str(self._client_config.host) + ":" + str(self._client_config.port)
connections.connect(alias=alias, uri=uri, user=self._client_config.user, password=self._client_config.password,
db_name=self._client_config.database)
from pymilvus import utility
if utility.has_collection(self._collection_name, using=alias):
result = self._client.query(collection_name=self._collection_name, result = self._client.query(collection_name=self._collection_name,
filter=f'metadata["doc_id"] in {ids}', filter=f'metadata["doc_id"] in {ids}',
@ -146,29 +123,11 @@ class MilvusVector(BaseVector):
self._client.delete(collection_name=self._collection_name, pks=ids) self._client.delete(collection_name=self._collection_name, pks=ids)
def delete(self) -> None: def delete(self) -> None:
alias = uuid4().hex if self._client.has_collection(self._collection_name):
if self._client_config.secure: self._client.drop_collection(self._collection_name, None)
uri = "https://" + str(self._client_config.host) + ":" + str(self._client_config.port)
else:
uri = "http://" + str(self._client_config.host) + ":" + str(self._client_config.port)
connections.connect(alias=alias, uri=uri, user=self._client_config.user, password=self._client_config.password,
db_name=self._client_config.database)
from pymilvus import utility
if utility.has_collection(self._collection_name, using=alias):
utility.drop_collection(self._collection_name, None, using=alias)
def text_exists(self, id: str) -> bool: def text_exists(self, id: str) -> bool:
alias = uuid4().hex if not self._client.has_collection(self._collection_name):
if self._client_config.secure:
uri = "https://" + str(self._client_config.host) + ":" + str(self._client_config.port)
else:
uri = "http://" + str(self._client_config.host) + ":" + str(self._client_config.port)
connections.connect(alias=alias, uri=uri, user=self._client_config.user, password=self._client_config.password,
db_name=self._client_config.database)
from pymilvus import utility
if not utility.has_collection(self._collection_name, using=alias):
return False return False
result = self._client.query(collection_name=self._collection_name, result = self._client.query(collection_name=self._collection_name,
@ -210,15 +169,7 @@ class MilvusVector(BaseVector):
if redis_client.get(collection_exist_cache_key): if redis_client.get(collection_exist_cache_key):
return return
# Grab the existing collection if it exists # Grab the existing collection if it exists
from pymilvus import utility if not self._client.has_collection(self._collection_name):
alias = uuid4().hex
if self._client_config.secure:
uri = "https://" + str(self._client_config.host) + ":" + str(self._client_config.port)
else:
uri = "http://" + str(self._client_config.host) + ":" + str(self._client_config.port)
connections.connect(alias=alias, uri=uri, user=self._client_config.user,
password=self._client_config.password, db_name=self._client_config.database)
if not utility.has_collection(self._collection_name, using=alias):
from pymilvus import CollectionSchema, DataType, FieldSchema from pymilvus import CollectionSchema, DataType, FieldSchema
from pymilvus.orm.types import infer_dtype_bydata from pymilvus.orm.types import infer_dtype_bydata
@ -263,11 +214,7 @@ class MilvusVector(BaseVector):
redis_client.set(collection_exist_cache_key, 1, ex=3600) redis_client.set(collection_exist_cache_key, 1, ex=3600)
def _init_client(self, config) -> MilvusClient: def _init_client(self, config) -> MilvusClient:
if config.secure: client = MilvusClient(uri=config.uri, user=config.user, password=config.password, db_name=config.database)
uri = "https://" + str(config.host) + ":" + str(config.port)
else:
uri = "http://" + str(config.host) + ":" + str(config.port)
client = MilvusClient(uri=uri, user=config.user, password=config.password, db_name=config.database)
return client return client
@ -285,11 +232,10 @@ class MilvusVectorFactory(AbstractVectorFactory):
return MilvusVector( return MilvusVector(
collection_name=collection_name, collection_name=collection_name,
config=MilvusConfig( config=MilvusConfig(
host=dify_config.MILVUS_HOST, uri=dify_config.MILVUS_URI,
port=dify_config.MILVUS_PORT, token=dify_config.MILVUS_TOKEN,
user=dify_config.MILVUS_USER, user=dify_config.MILVUS_USER,
password=dify_config.MILVUS_PASSWORD, password=dify_config.MILVUS_PASSWORD,
secure=dify_config.MILVUS_SECURE,
database=dify_config.MILVUS_DATABASE, database=dify_config.MILVUS_DATABASE,
) )
) )

View File

@ -12,8 +12,7 @@ class MilvusVectorTest(AbstractVectorTest):
self.vector = MilvusVector( self.vector = MilvusVector(
collection_name=self.collection_name, collection_name=self.collection_name,
config=MilvusConfig( config=MilvusConfig(
host="localhost", uri="http://localhost:19530",
port=19530,
user="root", user="root",
password="Milvus", password="Milvus",
), ),

View File

@ -5,7 +5,7 @@ from core.rag.datasource.vdb.milvus.milvus_vector import MilvusConfig
def test_default_value(): def test_default_value():
valid_config = {"host": "localhost", "port": 19530, "user": "root", "password": "Milvus"} valid_config = {"uri": "http://localhost:19530", "user": "root", "password": "Milvus"}
for key in valid_config: for key in valid_config:
config = valid_config.copy() config = valid_config.copy()
@ -15,5 +15,4 @@ def test_default_value():
assert e.value.errors()[0]["msg"] == f"Value error, config MILVUS_{key.upper()} is required" assert e.value.errors()[0]["msg"] == f"Value error, config MILVUS_{key.upper()} is required"
config = MilvusConfig(**valid_config) config = MilvusConfig(**valid_config)
assert config.secure is False
assert config.database == "default" assert config.database == "default"

View File

@ -128,16 +128,14 @@ services:
# The Qdrant server gRPC mode PORT. # The Qdrant server gRPC mode PORT.
QDRANT_GRPC_PORT: 6334 QDRANT_GRPC_PORT: 6334
# Milvus configuration Only available when VECTOR_STORE is `milvus`. # Milvus configuration Only available when VECTOR_STORE is `milvus`.
# The milvus host. # The milvus uri.
MILVUS_HOST: 127.0.0.1 MILVUS_URI: http://127.0.0.1:19530
# The milvus host. # The milvus token.
MILVUS_PORT: 19530 MILVUS_TOKEN: ''
# The milvus username. # The milvus username.
MILVUS_USER: root MILVUS_USER: root
# The milvus password. # The milvus password.
MILVUS_PASSWORD: Milvus MILVUS_PASSWORD: Milvus
# The milvus tls switch.
MILVUS_SECURE: 'false'
# relyt configurations # relyt configurations
RELYT_HOST: db RELYT_HOST: db
RELYT_PORT: 5432 RELYT_PORT: 5432
@ -308,16 +306,14 @@ services:
# The Qdrant server gRPC mode PORT. # The Qdrant server gRPC mode PORT.
QDRANT_GRPC_PORT: 6334 QDRANT_GRPC_PORT: 6334
# Milvus configuration Only available when VECTOR_STORE is `milvus`. # Milvus configuration Only available when VECTOR_STORE is `milvus`.
# The milvus host. # The milvus uri.
MILVUS_HOST: 127.0.0.1 MILVUS_URI: http://127.0.0.1:19530
# The milvus host. # The milvus token.
MILVUS_PORT: 19530 MILVUS_PORT: ''
# The milvus username. # The milvus username.
MILVUS_USER: root MILVUS_USER: root
# The milvus password. # The milvus password.
MILVUS_PASSWORD: Milvus MILVUS_PASSWORD: Milvus
# The milvus tls switch.
MILVUS_SECURE: 'false'
# Mail configuration, support: resend # Mail configuration, support: resend
MAIL_TYPE: '' MAIL_TYPE: ''
# default send from email address, if not specified # default send from email address, if not specified

View File

@ -335,16 +335,14 @@ QDRANT_GRPC_ENABLED=false
QDRANT_GRPC_PORT=6334 QDRANT_GRPC_PORT=6334
# Milvus configuration Only available when VECTOR_STORE is `milvus`. # Milvus configuration Only available when VECTOR_STORE is `milvus`.
# The milvus host. # The milvus uri.
MILVUS_HOST=127.0.0.1 MILVUS_URI=http://127.0.0.1:19530
# The milvus host. # The milvus token.
MILVUS_PORT=19530 MILVUS_TOKEN=
# The milvus username. # The milvus username.
MILVUS_USER=root MILVUS_USER=root
# The milvus password. # The milvus password.
MILVUS_PASSWORD=Milvus MILVUS_PASSWORD=Milvus
# The milvus tls switch.
MILVUS_SECURE=false
# MyScale configuration, only available when VECTOR_STORE is `myscale` # MyScale configuration, only available when VECTOR_STORE is `myscale`
# For multi-language support, please set MYSCALE_FTS_PARAMS with referring to: # For multi-language support, please set MYSCALE_FTS_PARAMS with referring to:

View File

@ -83,7 +83,7 @@ The `.env.example` file provided in the Docker setup is extensive and covers a w
7. **Vector Database Configuration**: 7. **Vector Database Configuration**:
- `VECTOR_STORE`: Type of vector database (e.g., `weaviate`, `milvus`). - `VECTOR_STORE`: Type of vector database (e.g., `weaviate`, `milvus`).
- Specific settings for each vector store like `WEAVIATE_ENDPOINT`, `MILVUS_HOST`. - Specific settings for each vector store like `WEAVIATE_ENDPOINT`, `MILVUS_URI`.
8. **CORS Configuration**: 8. **CORS Configuration**:
- `WEB_API_CORS_ALLOW_ORIGINS`, `CONSOLE_CORS_ALLOW_ORIGINS`: Settings for cross-origin resource sharing. - `WEB_API_CORS_ALLOW_ORIGINS`, `CONSOLE_CORS_ALLOW_ORIGINS`: Settings for cross-origin resource sharing.

View File

@ -89,11 +89,10 @@ x-shared-env: &shared-api-worker-env
QDRANT_CLIENT_TIMEOUT: ${QDRANT_CLIENT_TIMEOUT:-20} QDRANT_CLIENT_TIMEOUT: ${QDRANT_CLIENT_TIMEOUT:-20}
QDRANT_GRPC_ENABLED: ${QDRANT_GRPC_ENABLED:-false} QDRANT_GRPC_ENABLED: ${QDRANT_GRPC_ENABLED:-false}
QDRANT_GRPC_PORT: ${QDRANT_GRPC_PORT:-6334} QDRANT_GRPC_PORT: ${QDRANT_GRPC_PORT:-6334}
MILVUS_HOST: ${MILVUS_HOST:-127.0.0.1} MILVUS_URI: ${MILVUS_URI:-http://127.0.0.1:19530}
MILVUS_PORT: ${MILVUS_PORT:-19530} MILVUS_TOKEN: ${MILVUS_TOKEN:-}
MILVUS_USER: ${MILVUS_USER:-root} MILVUS_USER: ${MILVUS_USER:-root}
MILVUS_PASSWORD: ${MILVUS_PASSWORD:-Milvus} MILVUS_PASSWORD: ${MILVUS_PASSWORD:-Milvus}
MILVUS_SECURE: ${MILVUS_SECURE:-false}
MYSCALE_HOST: ${MYSCALE_HOST:-myscale} MYSCALE_HOST: ${MYSCALE_HOST:-myscale}
MYSCALE_PORT: ${MYSCALE_PORT:-8123} MYSCALE_PORT: ${MYSCALE_PORT:-8123}
MYSCALE_USER: ${MYSCALE_USER:-default} MYSCALE_USER: ${MYSCALE_USER:-default}