All Products
Search
Document Center

Vector Retrieval Service:Create a collection

Last Updated:Apr 11, 2024

This topic describes how to create a collection by using the SDK for Python.

Prerequisites

API definition

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]]]] = None,
    metric: str = 'cosine',
    timeout: Optional[int] = None
) -> DashVectorResponse

Example

Note

You need to replace YOUR_API_KEY with your API key and YOUR_CLUSTER_ENDPOINT with the endpoint of your cluster in the sample code for the code to run properly.

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, and age fields, and set their data types to str, float, and int, 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,
    fields_schema={'name': str, 'weight': float, 'age': int},
    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!')

Request parameters

Parameter

Type

Default value

Description

name

str

-

The name of the collection to be created.

dimension

int

-

The number of vector dimensions.

dtype

Union[Type[int], Type[float]]

float

Optional. The vector data type.

fields_schema

Optional[Dict[str,Union[Type[str],

Type[int], Type[float],

Type[bool]]]]

None

Optional. The predefined fields.

metric

str

cosine

Optional. The distance metric. Valid values: euclidean, dotproduct, and cosine.

If the value is cosine, dtype must be set to float.

timeout

Optional[int]

None

  • Optional. The timeout period. If you set timeout to None, the method works in synchronous mode and returns a response when the collection is successfully created.

  • If you set timeout to -1, the method works in asynchronous mode.

  • If you set timeout to a value greater than or equal to 0, the create works in synchronous mode and waits for the operation result within the specified timeout period. The method returns a timeout error if the collection fails to be created within the specified timeout period.

Note

For more information about the advantages of predefining fields during collection creation, see Schema-free.

Response parameters

Note

A DashVectorResponse object is returned, which contains the operation result, as described in the following table.

Parameter

Type

Description

Example

code

int

The returned status code. For more information, see Status codes.

0

message

str

The returned message.

success

request_id

str

The unique ID of the request.

19215409-ea66-4db9-8764-26ce2eb5bb99