功能描述
本文主要介绍如何使用SDK将文本数据快速转换为高质量的向量数据。
说明
请参考文档安装SDK, 安装对应编程语言的SDK。
调用说明
调用示例中的ACCESS_KEY_ID、ACCESS_KEY_SECRET、AGENT_KEY和APP_ID从系统环境变量中获取。执行示例前,请确保已正确设置相关的变量,或者在代码中直接修改变量。比如:
String accessKeyId = "abc******123";
String accessKeySecret = "abd******124";
String agentKey = "abe******125";
access_key_id = "abc******123"
access_key_secret = "abd******124"
agent_key = "abe******125"
accessKeyId = "abc******123"
accessKeySecret = "abd******124"
agentKey = "abe******125"
调用示例(文本向量)
import com.aliyun.bailian20230601.Client;
import com.aliyun.bailian20230601.models.CreateTextEmbeddingsRequest;
import com.aliyun.bailian20230601.models.CreateTextEmbeddingsResponse;
import com.aliyun.bailian20230601.models.CreateTextEmbeddingsResponseBody;
import com.aliyun.broadscope.bailian.sdk.consts.ConfigConsts;
import com.aliyun.broadscope.bailian.sdk.consts.EmbeddingTextTypeEnum;
import com.aliyun.teaopenapi.models.Config;
import java.util.ArrayList;
import java.util.List;
public class TextEmbeddingTest {
public void testCreateTextEmbeddings() {
String accessKeyId = System.getenv("ACCESS_KEY_ID");
String accessKeySecret = System.getenv("ACCESS_KEY_SECRET");
String agentKey = System.getenv("AGENT_KEY");
Config config = new Config()
.setAccessKeyId(accessKeyId)
.setAccessKeySecret(accessKeySecret)
.setEndpoint(ConfigConsts.POP_ENDPOINT);
try {
Client client = new Client(config);
List<String> input = new ArrayList<>();
input.add("今天天气怎么样");
CreateTextEmbeddingsRequest request = new CreateTextEmbeddingsRequest()
.setAgentKey(agentKey)
.setInput(input)
.setTextType(EmbeddingTextTypeEnum.DOCUMENT.getType());
CreateTextEmbeddingsResponse response = client.createTextEmbeddings(request);
CreateTextEmbeddingsResponseBody body = response.getBody();
if (body == null || !body.success) {
String error = body == null ? "create token error" : body.message;
throw new BaiLianSdkException(error);
}
CreateTextEmbeddingsResponseBody.CreateTextEmbeddingsResponseBodyData data = body.getData();
List<CreateTextEmbeddingsResponseBody.CreateTextEmbeddingsResponseBodyDataEmbeddings> embeddings = data.getEmbeddings();
for (CreateTextEmbeddingsResponseBody.CreateTextEmbeddingsResponseBodyDataEmbeddings embedding : embeddings) {
Integer textIndex = embedding.getTextIndex();
List<Double> embeddingsArray = embedding.getEmbedding();
System.out.printf("textIndex: %d, embedding: %s\n", textIndex, embeddingsArray);
}
} catch (BaiLianSdkException e) {
throw e;
} catch (Exception e) {
throw new BaiLianSdkException(e);
}
}
}
import os
from alibabacloud_bailian20230601.client import Client
from alibabacloud_bailian20230601.models import CreateTextEmbeddingsRequest
from alibabacloud_tea_openapi.models import Config
import broadscope_bailian
class TextEmbeddingTest():
def test_create_embeddings(self):
access_key_id = os.environ.get("ACCESS_KEY_ID")
access_key_secret = os.environ.get("ACCESS_KEY_SECRET")
agent_key = os.environ.get("AGENT_KEY")
config = Config(access_key_id=access_key_id,
access_key_secret=access_key_secret,
endpoint=broadscope_bailian.pop_endpoint)
client = Client(config=config)
request = CreateTextEmbeddingsRequest(agent_key=agent_key,
input=["今天天气怎么样", "我想去北京"],
text_type="query")
response = client.create_text_embeddings(request=request)
if response.status_code != 200 or response.body is None:
raise RuntimeError("create token error, code=%d" % response.status_code)
body = response.body
if not body.success:
raise RuntimeError("create token error, code=%s, message=%s" % (body.code, body.message))
for embedding in body.data.embeddings:
print("index: %s, embeddings: %s\n" % (embedding.text_index, embedding.embedding))
import (
"fmt"
apiClient "github.com/alibabacloud-go/bailian-20230601/client"
openapi "github.com/alibabacloud-go/darabonba-openapi/v2/client"
client "github.com/aliyun/alibabacloud-bailian-go-sdk/client"
"os"
)
func TestCreateTextEmbeddings() {
accessKeyId := os.Getenv("ACCESS_KEY_ID")
accessKeySecret := os.Getenv("ACCESS_KEY_SECRET")
agentKey := os.Getenv("AGENT_KEY")
endpoint := client.BroadscopeBailianPopEndpoint
config := &openapi.Config{AccessKeyId: &accessKeyId,
AccessKeySecret: &accessKeySecret,
Endpoint: &endpoint}
openapiClient, err := apiClient.NewClient(config)
if err != nil {
fmt.Printf("failed to new client, err: %v\n", err)
return
}
request := &apiClient.CreateTextEmbeddingsRequest{AgentKey: &agentKey}
text := "今天天气怎么样"
request.SetInput([]*string{&text})
result, err := openapiClient.CreateTextEmbeddings(request)
if err != nil {
fmt.Printf("failed to create token, err: %v\n", err)
return
}
resultBody := result.Body
if !(*resultBody.Success) {
requestId := resultBody.RequestId
if requestId == nil {
requestId = result.Headers["x-acs-request-id"]
}
errMessage := fmt.Sprintf("Failed to create embeddings, reason: %s RequestId: %s", *resultBody.Message, *requestId)
fmt.Printf("%v\n", errMessage)
return
}
embeddings := resultBody.Data.Embeddings
if embeddings != nil {
for _, embedding := range embeddings {
fmt.Printf("index: %d, embeding : [", embedding.TextIndex)
for _, num := range embedding.Embedding {
fmt.Printf("%.16f, ", *num)
}
fmt.Printf("]")
}
}
}
文档内容是否对您有帮助?