release: v0.0.7
Volc-VikingDB Golang SDK 为与 Volc-VikingDB 服务交互提供了一套全面的工具。它旨在实现直观、灵活和高效,使开发人员能够轻松地将其应用程序与 Volc-VikingDB 集成。
CollectionClient
IndexClient
EmbeddingClient
要安装 Volc-VikingDB Golang SDK,您可以使用 go get 命令:
go get
go get github.com/volcengine/vikingdb-go-sdk
最直接的方式是像 examples/vector/README.md 中的示例一样,通过环境变量加载配置。这样可以把密钥与代码分离,并且可以随时切换不同的凭据。
examples/vector/README.md
VIKINGDB_AK
VIKINGDB_SK
VIKINGDB_HOST
VIKINGDB_REGION
VIKINGDB_COLLECTION
VIKINGDB_INDEX
建议在项目根目录准备一个 .env 文件,便于本地加载。examples/vector/.env 提供了一个可直接复制的模板。
.env
examples/vector/.env
cp examples/vector/.env .env # 根据实际情况修改 .env 中的配置 export $(grep -v '^#' .env | xargs) # 加载到当前 shell # 或者仅在单条命令中注入变量 env $(grep -v '^#' .env | xargs) go test ./examples/vector -run TestScenario
环境变量准备好后,可以在代码中从进程环境读取配置并初始化 SDK:
package main import ( "context" "log" "os" "time" "github.com/volcengine/vikingdb-go-sdk/vector" "github.com/volcengine/vikingdb-go-sdk/vector/model" ) func main() { client, err := vector.New( vector.AuthIAM(os.Getenv("VIKINGDB_AK"), os.Getenv("VIKINGDB_SK")), vector.WithEndpoint("https://" + os.Getenv("VIKINGDB_HOST")), vector.WithRegion(os.Getenv("VIKINGDB_REGION")), vector.WithTimeout(30*time.Second), ) if err != nil { log.Fatal(err) } collection := client.Collection(model.CollectionLocator{ CollectionName: os.Getenv("VIKINGDB_COLLECTION"), }) index := client.Index(model.IndexLocator{ CollectionLocator: model.CollectionLocator{ CollectionName: os.Getenv("VIKINGDB_COLLECTION"), }, IndexName: os.Getenv("VIKINGDB_INDEX"), }) limit := 5 resp, err := index.SearchByRandom(context.Background(), model.SearchByRandomRequest{ SearchBase: model.SearchBase{Limit: &limit}, }) if err != nil { log.Fatal(err) } log.Printf("request_id=%s hits=%d", resp.RequestID, len(resp.Result.Data)) }
如果需要使用 API Key,可以导出 VIKINGDB_API_KEY 并将认证选项替换为 vector.AuthAPIKey(os.Getenv("VIKINGDB_API_KEY"))。
VIKINGDB_API_KEY
vector.AuthAPIKey(os.Getenv("VIKINGDB_API_KEY"))
完成初始化后,就可以使用对应的客户端(collection、index、embedding)调用 VikingDB 的各类接口,例如 Upsert、Update、Delete、Fetch、SearchByVector、SearchByMultiModal 和 SearchByKeywords。
collection
index
embedding
Upsert
Update
Delete
Fetch
SearchByVector
SearchByMultiModal
SearchByKeywords
Upsert 操作用于插入新数据或更新现有数据。
import ( "context" "github.com/volcengine/vikingdb-go-sdk/vector/model" ) req := model.UpsertDataRequest{ WriteDataBase: model.WriteDataBase{ Data: []model.MapStr{ { "ID": 1, "vector": []float64{1.1,2.2,3.4,4.2}, }, }, }, } resp, err := collection.Upsert(context.Background(), req) if err != nil { // 处理错误 } // 处理响应
使用 SearchByVector 可以将查询向量与索引中的向量进行对比,实现向量检索。
import ( "context" "github.com/volcengine/vikingdb-go-sdk/vector/model" ) limit := 5 req := model.SearchByVectorRequest{ SearchBase: model.SearchBase{ Limit: &limit, OutputFields: []string{"title", "score"}, }, DenseVector: []float64{0.1, 0.5, 0.2, 0.8}, } resp, err := index.SearchByVector(context.Background(), req) if err != nil { // 处理错误 } // 处理响应
Embedding 操作用于将文本转换为向量。以下示例假设已经按前文构建了 client:
Embedding
client
import ( "context" "github.com/volcengine/vikingdb-go-sdk/vector/model" ) text := "hello world" model_name := "doubao-embedding" model_version := "240715" req := model.EmbeddingRequest{ DenseModel: &model.EmbeddingModelOpt{ ModelName: &model_name, ModelVersion: &model_version, }, Data: []*model.EmbeddingData{ { Text: &text, }, }, } embeddingClient := client.Embedding() resp, err := embeddingClient.Embedding(context.Background(), req) if err != nil { // 处理错误 } // 处理响应
有关详细的 API 参考,请访问 Go Reference。
版权所有:中国计算机学会技术支持:开源发展技术委员会 京ICP备13000930号-9 京公网安备 11010802032778号
Volc-VikingDB Golang SDK
Volc-VikingDB Golang SDK 为与 Volc-VikingDB 服务交互提供了一套全面的工具。它旨在实现直观、灵活和高效,使开发人员能够轻松地将其应用程序与 Volc-VikingDB 集成。
特性
CollectionClient、IndexClient和EmbeddingClient)提供专用客户端,以提供更专注、更直观的开发体验。安装
要安装 Volc-VikingDB Golang SDK,您可以使用
go get命令:快速入门
最直接的方式是像
examples/vector/README.md中的示例一样,通过环境变量加载配置。这样可以把密钥与代码分离,并且可以随时切换不同的凭据。配置环境变量
VIKINGDB_AK/VIKINGDB_SKVIKINGDB_HOSTVIKINGDB_REGIONVIKINGDB_COLLECTIONVIKINGDB_INDEX建议在项目根目录准备一个
.env文件,便于本地加载。examples/vector/.env提供了一个可直接复制的模板。初始化客户端
环境变量准备好后,可以在代码中从进程环境读取配置并初始化 SDK:
如果需要使用 API Key,可以导出
VIKINGDB_API_KEY并将认证选项替换为vector.AuthAPIKey(os.Getenv("VIKINGDB_API_KEY"))。数据操作
完成初始化后,就可以使用对应的客户端(
collection、index、embedding)调用 VikingDB 的各类接口,例如Upsert、Update、Delete、Fetch、SearchByVector、SearchByMultiModal和SearchByKeywords。Upsert 数据
Upsert操作用于插入新数据或更新现有数据。向量检索
使用
SearchByVector可以将查询向量与索引中的向量进行对比,实现向量检索。文本嵌入
Embedding操作用于将文本转换为向量。以下示例假设已经按前文构建了client:API 参考
有关详细的 API 参考,请访问 Go Reference。