This topic describes how to create a collection by using the SDK for Python.
Prerequisites
A cluster is created. For more information, see Create a cluster.
An API key is obtained. For more information, see Manage API keys.
The SDK of the latest version is installed. For more information, see Install DashVector SDK.
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
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: If the value is |
timeout | Optional[int] | None |
|
For more information about the advantages of predefining fields during collection creation, see Schema-free.
Response parameters
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 |