The AnalyticDB for PostgreSQL API encapsulates the vector-related DDL and DML operations of AnalyticDB for PostgreSQL. You can use API operations to manage vector data. This topic describes how to use API operations to import and query vector data by calling Alibaba Cloud SDK for Go.
Prerequisites
An AnalyticDB for PostgreSQL V6.0 instance in elastic storage mode is created. For more information, see Create an instance.
Vector search engine optimization is enabled. For more information, see Enable or disable vector search engine optimization.
A privileged account is created. For more information, see Create a database account.
Procedure
Initialize the vector database
Before you use vector search, you must initialize the knowledgebase database and the full-text search feature.
The following sample code provides an example on how to call an API operation:
package main
import (
"fmt"
"os"
"github.com/aliyun/alibaba-cloud-sdk-go/sdk"
"github.com/aliyun/alibaba-cloud-sdk-go/sdk/auth/credentials"
gpdb "github.com/aliyun/alibaba-cloud-sdk-go/services/gpdb"
)
func main() {
config := sdk.NewConfig()
// Please ensure that the environment variables ALIBABA_CLOUD_ACCESS_KEY_ID and ALIBABA_CLOUD_ACCESS_KEY_SECRET are set.
credential := credentials.NewAccessKeyCredential(os.Getenv("ALIBABA_CLOUD_ACCESS_KEY_ID"), os.Getenv("ALIBABA_CLOUD_ACCESS_KEY_SECRET"))
/* use STS Token
credential := credentials.NewStsTokenCredential(os.Getenv("ALIBABA_CLOUD_ACCESS_KEY_ID"), os.Getenv("ALIBABA_CLOUD_ACCESS_KEY_SECRET"), os.Getenv("ALIBABA_CLOUD_SECURITY_TOKEN"))
*/
client, err := gpdb.NewClientWithOptions("cn-qingdao", config, credential)
if err != nil {
panic(err)
}
request := gpdb.CreateInitVectorDatabaseRequest()
request.Scheme = "https"
request.DBInstanceId = "gp-bp1c62r3l489****"
request.RegionId = "cn-qingdao"
request.ManagerAccount = "myaccount"
request.ManagerAccountPassword = "myaccount_password"
response, err := client.InitVectorDatabase(request)
if err != nil {
fmt.Print(err.Error())
}
fmt.Printf("response is %#v\n", response)
}
For information about the relevant parameters, see InitVectorDatabase.
Create a namespace
Namespaces are used to separate schemas. Before you use vectors, you must create at least one namespace or use the public namespace.
The following sample code provides an example on how to call an API operation:
package main
import (
"fmt"
"os"
"github.com/aliyun/alibaba-cloud-sdk-go/sdk"
"github.com/aliyun/alibaba-cloud-sdk-go/sdk/auth/credentials"
gpdb "github.com/aliyun/alibaba-cloud-sdk-go/services/gpdb"
)
func main() {
config := sdk.NewConfig()
// Please ensure that the environment variables ALIBABA_CLOUD_ACCESS_KEY_ID and ALIBABA_CLOUD_ACCESS_KEY_SECRET are set.
credential := credentials.NewAccessKeyCredential(os.Getenv("ALIBABA_CLOUD_ACCESS_KEY_ID"), os.Getenv("ALIBABA_CLOUD_ACCESS_KEY_SECRET"))
/* use STS Token
credential := credentials.NewStsTokenCredential(os.Getenv("ALIBABA_CLOUD_ACCESS_KEY_ID"), os.Getenv("ALIBABA_CLOUD_ACCESS_KEY_SECRET"), os.Getenv("ALIBABA_CLOUD_SECURITY_TOKEN"))
*/
client, err := gpdb.NewClientWithOptions("cn-qingdao", config, credential)
if err != nil {
panic(err)
}
request := gpdb.CreateCreateNamespaceRequest()
request.Scheme = "https"
request.DBInstanceId = "gp-bp1c62r3l489****"
request.RegionId = "cn-qingdao"
request.ManagerAccount = "myaccount"
request.ManagerAccountPassword = "myaccount_password"
request.Namespace = "vector_test"
request.NamespacePassword = "vector_test_password"
response, err := client.CreateNamespace(request)
if err != nil {
fmt.Print(err.Error())
}
fmt.Printf("response is %#v\n", response)
}
For information about the relevant parameters, see CreateNamespace.
After you create a namespace, you can query the corresponding schema in the knowledgebase database of the instance.
SELECT schema_name FROM information_schema.schemata;
Create a collection
Collections are used to store vector data and are separated by namespaces.
The following sample code provides an example on how to call an API operation:
package main
import (
"fmt"
"os"
"github.com/aliyun/alibaba-cloud-sdk-go/sdk"
"github.com/aliyun/alibaba-cloud-sdk-go/sdk/auth/credentials"
gpdb "github.com/aliyun/alibaba-cloud-sdk-go/services/gpdb"
"github.com/aliyun/alibaba-cloud-sdk-go/sdk/requests"
)
func main() {
config := sdk.NewConfig()
// Please ensure that the environment variables ALIBABA_CLOUD_ACCESS_KEY_ID and ALIBABA_CLOUD_ACCESS_KEY_SECRET are set.
credential := credentials.NewAccessKeyCredential(os.Getenv("ALIBABA_CLOUD_ACCESS_KEY_ID"), os.Getenv("ALIBABA_CLOUD_ACCESS_KEY_SECRET"))
/* use STS Token
credential := credentials.NewStsTokenCredential(os.Getenv("ALIBABA_CLOUD_ACCESS_KEY_ID"), os.Getenv("ALIBABA_CLOUD_ACCESS_KEY_SECRET"), os.Getenv("ALIBABA_CLOUD_SECURITY_TOKEN"))
*/
client, err := gpdb.NewClientWithOptions("cn-qingdao", config, credential)
if err != nil {
panic(err)
}
request := gpdb.CreateCreateCollectionRequest()
request.Scheme = "https"
request.DBInstanceId = "gp-bp1c62r3l489****"
request.RegionId = "cn-qingdao"
request.ManagerAccount = "myaccount"
request.ManagerAccountPassword = "myaccount_password"
request.Namespace = "vector_test"
request.Collection = "document"
request.Dimension = requests.NewInteger(10)
request.FullTextRetrievalFields = ","
request.Parser = "zh_ch"
request.Metadata = "{\"pv\": \"text\",\"link\": \"text\",\"content\": \"text\",\"title\": \"text\"}"
response, err := client.CreateCollection(request)
if err != nil {
fmt.Print(err.Error())
}
fmt.Printf("response is %#v\n", response)
}
After you create a collection, you can query the corresponding table in the knowledgebase database of the instance.
SELECT tablename FROM pg_tables WHERE schemaname='vector_test';
Upload vector data
Upload the prepared embedding vector data to the corresponding collection.
The following sample code provides an example on how to call an API operation:
package main
import (
"fmt"
"os"
"github.com/aliyun/alibaba-cloud-sdk-go/sdk"
"github.com/aliyun/alibaba-cloud-sdk-go/sdk/auth/credentials"
gpdb "github.com/aliyun/alibaba-cloud-sdk-go/services/gpdb"
"github.com/aliyun/alibaba-cloud-sdk-go/sdk/requests"
)
func main() {
config := sdk.NewConfig()
// Please ensure that the environment variables ALIBABA_CLOUD_ACCESS_KEY_ID and ALIBABA_CLOUD_ACCESS_KEY_SECRET are set.
credential := credentials.NewAccessKeyCredential(os.Getenv("ALIBABA_CLOUD_ACCESS_KEY_ID"), os.Getenv("ALIBABA_CLOUD_ACCESS_KEY_SECRET"))
/* use STS Token
credential := credentials.NewStsTokenCredential(os.Getenv("ALIBABA_CLOUD_ACCESS_KEY_ID"), os.Getenv("ALIBABA_CLOUD_ACCESS_KEY_SECRET"), os.Getenv("ALIBABA_CLOUD_SECURITY_TOKEN"))
*/
client, err := gpdb.NewClientWithOptions("cn-qingdao", config, credential)
if err != nil {
panic(err)
}
request := gpdb.CreateUpsertCollectionDataRequest()
request.Scheme = "https"
request.DBInstanceId = "gp-bp1c62r3l489****"
request.RegionId = "cn-qingdao"
request.Collection = "document"
request.Namespace = "vector_test"
request.NamespacePassword = "vector_test_password"
request.Rows = &[]gpdb.UpsertCollectionDataRows{
{
Id: "0CB55798-ECF5-4064-B81E-FE35B19E01A6",
Metadata: gpdb.UpsertCollectionDataRowsMetadata{
Pv: "1000",
Link: "http://127.X.X.1/document1",
Content: "Test content",
Title: "Test document",
},
Vector: &[]number{requests.NewInteger(0.2894745251078251),requests.NewInteger(0.5364747050266715),requests.NewInteger(0.1276845661831275)},
},
}
response, err := client.UpsertCollectionData(request)
if err != nil {
fmt.Print(err.Error())
}
fmt.Printf("response is %#v\n", response)
}
For information about the relevant parameters, see UpsertCollectionData.
After you upload vector data, you can query the data in the knowledgebase database of the instance.
SELECT * FROM vector_test.document;
Retrieve vector data
Use the prepared vectors or full-text search fields to retrieve vector data.
The following sample code provides an example on how to call an API operation:
package main
import (
"fmt"
"os"
"github.com/aliyun/alibaba-cloud-sdk-go/sdk"
"github.com/aliyun/alibaba-cloud-sdk-go/sdk/auth/credentials"
gpdb "github.com/aliyun/alibaba-cloud-sdk-go/services/gpdb"
"github.com/aliyun/alibaba-cloud-sdk-go/sdk/requests"
)
func main() {
config := sdk.NewConfig()
// Please ensure that the environment variables ALIBABA_CLOUD_ACCESS_KEY_ID and ALIBABA_CLOUD_ACCESS_KEY_SECRET are set.
credential := credentials.NewAccessKeyCredential(os.Getenv("ALIBABA_CLOUD_ACCESS_KEY_ID"), os.Getenv("ALIBABA_CLOUD_ACCESS_KEY_SECRET"))
/* use STS Token
credential := credentials.NewStsTokenCredential(os.Getenv("ALIBABA_CLOUD_ACCESS_KEY_ID"), os.Getenv("ALIBABA_CLOUD_ACCESS_KEY_SECRET"), os.Getenv("ALIBABA_CLOUD_SECURITY_TOKEN"))
*/
client, err := gpdb.NewClientWithOptions("cn-qingdao", config, credential)
if err != nil {
panic(err)
}
request := gpdb.CreateQueryCollectionDataRequest()
request.Scheme = "https"
request.DBInstanceId = "gp-bp1c62r3l489****"
request.RegionId = "cn-qingdao"
request.Collection = "document"
request.Namespace = "vector_test"
request.NamespacePassword = "vector_test_password"
request.Content = "Test"
request.Filter = "pv > 10"
request.TopK = requests.NewInteger(10)
request.Vector = &[]number{requests.NewInteger(0.7152607422256894),requests.NewInteger(0.5524872066437732),requests.NewInteger(0.1168505269851303)}
response, err := client.QueryCollectionData(request)
if err != nil {
fmt.Print(err.Error())
}
fmt.Printf("response is %#v\n", response)
}
Sample result:
{
"Matches": {
"match": [{
"Id": "0CB55798-ECF5-4064-B81E-FE35B19E01A6",
"Metadata": {
"title": "Test document",
"content": "Test content",
"link": "http://127.X.X.1/document1",
"pv": "1000"
},
"Values": [0.2894745251078251, 0.5364747050266715, 0.1276845661831275, 0.22528871956822372, 0.7009319238651552, 0.40267406135256123, 0.8873626696379067, 0.1248525955774931, 0.9115507046412368, 0.2450859133174706]
}]
},
"RequestId": "ABB39CC3-4488-4857-905D-2E4A051D0521",
"Status": "success"
}