Este tema describe cómo crear una colección mediante el SDK para Python.
Requisitos previos
A cluster is created. For more information, see Create a cluster.
An API key is obtained. For more information, see API key management.
Instale la versión más reciente del SDK. Para obtener más información, consulte Install DashVector SDK.
Definición de la API
Client.create(
name: str,
dimension: int,
dtype: Union[Type[int], Type[float]] = float,
fields_schema: Optional[Dict[str, Union[Type[str], Type[int], Type[float], Type[bool], Type[long]]]] = None,
metric: str = 'cosine',
timeout: Optional[int] = None
) -> DashVectorResponseEjemplo
En el código de ejemplo, reemplace YOUR_API_KEY por su clave de API y YOUR_CLUSTER_ENDPOINT por el endpoint de su clúster para que el código se ejecute correctamente.
import dashvector
client = dashvector.Client(
api_key='YOUR_API_KEY',
endpoint='YOUR_CLUSTER_ENDPOINT'
)
# Create a collection and set its name to quickstart, the number of vector dimensions to 4,
# vector data type to the default value of float,
# and distance metric to dotproduct.
# Predefine the name, weight, age and id fields, and set their data types to str, float, int and dashvector.long respectively.
# Set timeout to -1 to enable the asynchronous mode for the create method.
ret = client.create(
name='quickstart',
dimension=4,
metric='dotproduct',
dtype=float,
# To preserve the semantic clarity of type annotations,
# DashVector defines the Long type through the typing module,
# which provides type annotation support for scenarios where large integers need to be explicitly annotated
fields_schema={'name': str, 'weight': float, 'age': int, 'id': dashvector.long},
timeout=-1
)
# Check whether the collection is successfully created.
if ret:
print('create collection success!')
# You can also use the following code to check whether the collection is successfully created.
# from dashvector import DashVectorCode
# if ret.code == DashVectorCode.Success:
# print('create collection success!')Parámetros de solicitud
Parámetro | Tipo | Valor predeterminado | Descripción |
name | str | - | Nombre de la colección que se va a crear. |
dimension | int | - | Número de dimensiones del vector. |
dtype | Union[Type[int], Type[float]] | float | Opcional. Tipo de datos del vector. |
fields_schema
| Optional[Dict[str,Union[Type[str], Type[int], Type[float], Type[bool], Type[long]]]] | Ninguna | Opcional. Campos predefinidos. |
metric | str | cosine | Opcional. Métrica de distancia. Valores válidos: Si el valor es |
timeout | Optional[int] | Ninguna |
|
Para obtener más información sobre las ventajas de predefinir campos al crear una colección, consulte Schema-free fields.
Parámetros de respuesta
Se devuelve un objeto DashVectorResponse que contiene el resultado de la operación, tal como se describe a continuación.
|
Parámetro |
Tipo |
Descripción |
Ejemplo |
|
code |
int |
Código de estado devuelto. Para obtener más información, consulte Status codes. |
0 |
|
message |
str |
Mensaje devuelto. |
success |
|
request_id |
str |
Identificador único de la solicitud. |
19215409-ea66-4db9-8764-26ce2eb5bb99 |