Use o Go SDK V2 para chamar a operação PutVectorIndex e criar um índice vetorial em um bucket vetorial.
Permissions
By default, Alibaba Cloud accounts have all permissions, whereas Resource Access Management (RAM) users and RAM roles have no permissions. The Alibaba Cloud account or an administrator must grant permissions through a RAM policy or a bucket policy.
|
API |
Action |
Description |
|
PutVectorIndex |
|
Creates a vector index. |
Definição do método
func (c *VectorsClient) PutVectorIndex(ctx context.Context, request *PutVectorIndexRequest, optFns ...func(*oss.Options)) (*PutVectorIndexResult, error)
Parâmetros de solicitação
|
Parâmetro |
Tipo |
Descrição |
|
ctx |
context.Context |
O contexto da solicitação. |
|
request |
*PutVectorIndexRequest |
Os parâmetros de solicitação. Para mais informações, consulte PutVectorIndexRequest. |
|
optFns |
...func(*Options) |
(Opcional) Os parâmetros de configuração em nível de operação. Para mais informações, consulte Options. |
Valores de retorno
|
Parâmetro |
Tipo |
Descrição |
|
result |
*PutVectorIndexResult |
O valor de retorno da operação. Este parâmetro é válido somente quando err é nil. Para mais informações, consulte PutVectorIndexResult. |
|
err |
error |
O status da solicitação. Se a solicitação falhar, err não será nil. |
Código de exemplo
package main
import (
"context"
"flag"
"log"
"github.com/aliyun/alibabacloud-oss-go-sdk-v2/oss"
"github.com/aliyun/alibabacloud-oss-go-sdk-v2/oss/credentials"
"github.com/aliyun/alibabacloud-oss-go-sdk-v2/oss/vectors"
)
var (
region string
bucketName string
accountId string
indexName string
)
func init() {
flag.StringVar(®ion, "region", "", "The region in which the vector bucket is located.")
flag.StringVar(&bucketName, "bucket", "", "The name of the vector bucket.")
flag.StringVar(&accountId, "account-id", "", "The ID of the Alibaba Cloud account.")
flag.StringVar(&indexName, "index", "", "The name of the vector index.")
}
func main() {
flag.Parse()
if len(bucketName) == 0 {
flag.PrintDefaults()
log.Fatalf("invalid parameters, bucket name required")
}
if len(region) == 0 {
flag.PrintDefaults()
log.Fatalf("invalid parameters, region required")
}
if len(accountId) == 0 {
flag.PrintDefaults()
log.Fatalf("invalid parameters, accountId required")
}
if len(indexName) == 0 {
flag.PrintDefaults()
log.Fatalf("invalid parameters, index required")
}
cfg := oss.LoadDefaultConfig().
WithCredentialsProvider(credentials.NewEnvironmentVariableCredentialsProvider()).
WithRegion(region).WithAccountId(accountId).
// To access the service over the public internet, set this parameter to false or remove this line.
WithUseInternalEndpoint(true)
client := vectors.NewVectorsClient(cfg)
request := &vectors.PutVectorIndexRequest{
Bucket: oss.Ptr(bucketName),
DataType: oss.Ptr("float32"),
Dimension: oss.Ptr(128),
DistanceMetric: oss.Ptr("cosine"),
IndexName: oss.Ptr(indexName),
Metadata: map[string]any{
"nonFilterableMetadataKeys": []string{"foo", "bar"},
},
}
result, err := client.PutVectorIndex(context.TODO(), request)
if err != nil {
log.Fatalf("failed to put vector index: %v", err)
}
log.Printf("put vector index result: %#v\n", result)
}
Referências
Para o código de exemplo completo para criar um índice vetorial, consulte put_vector_index.go.