bge-large-zh是中文文本表示模型,可将任意文本映射为低维稠密向量,以用于检索、分类、聚类或语义匹配等任务,并可支持为大模型调用外部知识。
本文详细介绍了 bge-large-zh 的SDK及API使用方法。
Host:maas-api.ml-platform-cn-beijing.volces.com
Region: cn-beijing
提供统一 SDK 的接入形式(需要用 ak/sk 进行旁路鉴权,火山鉴权逻辑可以参考)
Golang SDK: https://github.com/volcengine/volc-sdk-golang
Python SDK: https://github.com/volcengine/volc-sdk-python
说明
调用前请修改:
设置环境变量(或在代码中修改):VOLC_ACCESSKEY
和VOLC_SECRETKEY
;
修改调用模型名占位符${YOUR_MODLE_NAME}
(或者设置model.endpoint_id
,具体请参考 API Specification)
// Usage: // // 1. go get -u github.com/volcengine/volc-sdk-golang // 2. VOLC_ACCESSKEY=XXXXX VOLC_SECRETKEY=YYYYY go run main.go package main import ( "encoding/json" "errors" "fmt" "os" "github.com/volcengine/volc-sdk-golang/service/maas" "github.com/volcengine/volc-sdk-golang/service/maas/models/api" ) func main() { r := maas.NewInstance("maas-api.ml-platform-cn-beijing.volces.com", "cn-beijing") // fetch ak&sk from environmental variables r.SetAccessKey(os.Getenv("VOLC_ACCESSKEY")) r.SetSecretKey(os.Getenv("VOLC_SECRETKEY")) reqE := &api.EmbeddingsReq{ Model: &api.Model{ Name: "bge-large-zh", }, Input: []string{"天很蓝", "海很深"}, } TestEmbeddings(r, reqE) } func TestEmbeddings(r *maas.MaaS, req *api.EmbeddingsReq) { got, status, err := r.Embeddings(req) if err != nil { errVal := &api.Error{} if errors.As(err, &errVal) { // the returned error always type of *api.Error fmt.Printf("meet maas error=%v, status=%d\n", errVal, status) } return } fmt.Println("embeddings answer", mustMarshalJson(got)) }
注意
目前仅支持 python>=3.5
。
''' Usage: 1. python3 -m pip install --user volcengine 2. VOLC_ACCESSKEY=XXXXX VOLC_SECRETKEY=YYYYY python main.py ''' import os from volcengine.maas import MaasService, MaasException def test_embeddings(maas, req): try: resp = maas.embeddings(req) print(resp) except MaasException as e: print(e) if __name__ == '__main__': maas = MaasService('maas-api.ml-platform-cn-beijing.volces.com', 'cn-beijing') maas.set_ak(os.getenv("VOLC_ACCESSKEY")) maas.set_sk(os.getenv("VOLC_SECRETKEY")) # document: "https://www.volcengine.com/docs/82379/1099475" # embeddings embeddingsReq = { "model": { "name": "bge-large-zh" }, "input": [ "天很蓝", "海很深" ] } test_embeddings(maas, embeddingsReq)
/* # pom.xml <dependency> <groupId>com.volcengine</groupId> <artifactId>volc-sdk-java</artifactId> <version>LATEST</version> </dependency> */ package com.volcengine.example.maas; import com.volcengine.helper.Const; import com.volcengine.model.maas.api.Api; import com.volcengine.service.maas.MaasException; import com.volcengine.service.maas.MaasService; import com.volcengine.service.maas.impl.MaasServiceImpl; import java.util.stream.Stream; public class EmbeddingsDemo { public static void main(String[] args) { MaasService maasService = new MaasServiceImpl("maas-api.ml-platform-cn-beijing.volces.com", "cn-beijing"); // fetch ak&sk from environmental variables maasService.setAccessKey(System.getenv("VOLC_ACCESSKEY")); maasService.setSecretKey(System.getenv("VOLC_SECRETKEY")); Api.EmbeddingsReq tReq = Api.EmbeddingsReq.newBuilder() .setModel(Api.Model.newBuilder().setName("bge-large-zh")) .addInput("天很蓝") .addInput("海很深") .build(); testEmbeddings(maasService, tReq); } private static void testEmbeddings(MaasService maasService, Api.EmbeddingsReq req) { try { Api.EmbeddingsResp resp = maasService.embeddings(req); System.out.println(resp.getObject()); System.out.println(resp.getDataList()); //object: object type 恒为embedding,embedding: embedding向量,index: 列表中序号 } catch (MaasException e) { System.out.println("req_id: " + e.getRequestId()); System.out.println("code: " + e.getCode()); System.out.println("code_n: " + e.getCodeN()); System.out.println("message: " + e.getMsg()); e.printStackTrace(); } } }
主要参考 OpenAI 和 HuggingFace
Parameters 记录可选控制参数,具体哪些参数可用依赖模型服务(模型详情页会描述哪些参数可用)
字段 | 类型 | 描述 | 默认值 |
---|---|---|---|
input (required) | list | 等待embedding的文本列表 |
字段 | 类型 | 描述 |
---|---|---|
req_id | string | 请求 id |
object | string | data字段类型,仅list |
data | list |
|
usage error(optioanl) | object object |
|