volcengine / volc-sdk-nodejs

Apache License 2.0
38 stars 8 forks source link

需要文本翻译 API,以及 Node.js SDK 使用例子。 #3

Open 1c7 opened 1 year ago

1c7 commented 1 year ago

https://www.volcengine.com/docs/4640/65067

image
  1. 文档里没有把 http endpoint 地址写清楚。
Otto-J commented 1 year ago

刚跑通了, node 的例子,供参考,里面的魔法字符串挺多,如果可以,建议研发把这些常量改为枚举,比如 servicename region version 等

import { Service } from "@volcengine/openapi";

const accessKeyId = env.VOLC_ACCESSKEY;
const secretKey = env.VOLC_SECRETKEY;

const postBody = {
  SourceLanguage: "en",
  TargetLanguage: "zh",
  TextList: ["hello world"],
};

const service = new Service({
  host: "open.volcengineapi.com",
  serviceName: "translate",
  region: "cn-north-1",
  accessKeyId,
  secretKey,
});

const fetchApi = service.createAPI("TranslateText", {
  Version: "2020-06-01",
  method: "POST",
  contentType: "json",
});

const rr = await fetchApi(postBody, {});

// [ { Translation: '你好世界', DetectedSourceLanguage: '', Extra: null } ]
console.log(rr.TranslationList);
1c7 commented 1 year ago

@Otto-J 非常感谢!

1c7 commented 1 year ago

实测的确能用

image image image