OpenAPI封裝了云原生數據倉庫AnalyticDB PostgreSQL版向量操作的DDL和DML,使您可以通過OpenAPI來管理向量數據。本文以SDK Go調用方式介紹如何通過API導入并查詢向量數據。
前提條件
已創建存儲彈性模式6.0版的AnalyticDB PostgreSQL版實例。具體操作,請參見創建實例。
已開啟向量引擎優化。具體操作,請參見開啟或關閉向量檢索引擎優化。
已創建初始賬號。具體操作,請參見創建數據庫賬號。
若您使用RAM用戶,則需要對RAM用戶進行授權,更多方式請參見使用OpenAPI示例。
操作流程
初始化向量庫
在使用向量檢索前,需初始化knowledgebase庫以及全文檢索相關功能。
調用示例如下:
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)
}
參數說明,請參見InitVectorDatabase - 初始化向量數據庫。
創建Namespace
Namespace用于Schema隔離,在使用向量前,需至少創建一個Namespace或者使用public的Namespace。
調用示例如下:
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)
}
參數說明,請參見CreateNamespace - 創建命名空間。
創建完后,可以在實例的knowledgebase庫查看對應的Schema。
SELECT schema_name FROM information_schema.schemata;
創建Collection
Collection用于存儲向量數據,并使用Namespace隔離。
調用示例如下:
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)
}
參數說明,請參見CreateCollection - 創建向量數據集。
創建完后,可以在實例的knowledgebase庫查看對應的Table。
SELECT tablename FROM pg_tables WHERE schemaname='vector_test';
上傳向量數據
將準備好的Embedding向量數據上傳到對應的Collection中。
調用示例如下:
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: "測試內容",
Title: "測試文檔",
},
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)
}
參數說明,請參見UpsertCollectionData - 上傳向量數據。
上傳完成,可以在實例的knowledgebase庫查看數據。
SELECT * FROM vector_test.document;
召回向量數據
準備需要召回的查詢向量或全文檢索字段,執行查詢接口。
調用示例如下:
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 = "測試"
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)
}
返回結果如下:
{
"Matches": {
"match": [{
"Id": "0CB55798-ECF5-4064-B81E-FE35B19E01A6",
"Metadata": {
"title": "測試文檔",
"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"
}