项目作者: khs1994

项目描述 :
Tencent AI 腾讯 AI JavaScript SDK
高级语言: TypeScript
项目地址: git://github.com/khs1994/tencent-ai-js.git
创建时间: 2018-09-21T14:26:03Z
项目社区:https://github.com/khs1994/tencent-ai-js

开源协议:MIT License

下载


TencentAI JavaScript SDK

@khs1994/tencent-ai"">npm Build Status codecov @khs1994/tencent-ai"">install size code style: prettier Build Status Build Status

This repo fork from https://github.com/w89612b/qqai-api-sdk

微信订阅号



关注项目作者微信订阅号,接收项目最新动态

Installation

  1. $ npm i @khs1994/tencent-ai --save

Usage

  • 可直接运行于 Node.js 浏览器 微信小程序 Deno

  • 一切逻辑均在微信小程序客户端完成,无需第三方服务器,保证用户隐私

  • Node.js 端文件相关 API 可传入 base64 编码、本地文件路径、图片 url

  1. const { Translate, TencentAIError } = require('@khs1994/tencent-ai');
  2. const App = {
  3. // 设置请求数据(应用密钥、接口请求参数)
  4. appkey: 'your-appkey', // process.env.NODE_TENCENT_AI_APP_KEY
  5. appid: 'your-appid', // process.env.NODE_TENCENT_AI_APP_ID
  6. };
  7. const translate = new Translate(App.appkey, App.appid);
  8. // 文本翻译
  9. translate.text('你好').then(
  10. res => {
  11. console.log(res);
  12. },
  13. e => {
  14. console.log(e);
  15. },
  16. );
  17. // Or Using async / await ES7
  18. (async () => {
  19. try {
  20. let res = await translate.text('hello');
  21. console.log(res);
  22. // error demo
  23. res = await translate.text();
  24. } catch (e) {
  25. console.log(e);
  26. }
  27. })();

微信小程序

  1. $ npm i @khs1994/tencent-ai --save
  • 使用 npm 安装,之后在菜单栏选择构建 npm
  • 必须勾选 增强编译,设置方法:详情(IDE 右上角) -> 本地设置 -> 增强编译
  • 必须将 https://api.ai.qq.com 加入 request 合法域名(开发环境请忽略)
  1. // 解构赋值
  2. const { TencentAI } = require('@khs1994/tencent-ai');
  3. // 以下两项请到 ai.qq.com 控制台查看
  4. const app_key = '';
  5. const app_id = '';
  6. // 获取全部实例,也可以只获取特定实例,例如 NLP,具体参考上边例子
  7. const ai = new TencentAI(app_key, app_id);
  8. ai.nlp.textChat('hello', 'session_id').then(
  9. res => {
  10. // TODO
  11. console.log(res);
  12. },
  13. e => console.log(e),
  14. );
  • 在小程序端调用文件相关的 API 时(人脸识别、OCR、音频),可以直接传入文件路径,无需转码。也可直接传入 base64 编码

  • 由于小程序限制,请不要传入 url(配置了请求域名除外)

TypeScript

  1. import TencentAI from '@khs1994/tencent-ai';
  2. const app_key = '';
  3. const app_id = '';
  4. const ai = new TencentAI(app_key, app_id);
  5. ai.nlp.textChat('hello', '1').then(res => {
  6. console.log(res);
  7. });

浏览器

如何安全的保存 密钥(app_key) 请自行实现

  1. <script src="https://unpkg.com/@khs1994/tencent-ai@19.6.0-alpha.5/dist/tencent-ai.min.js"></script>
  2. <script>
  3. // 由于跨域限制,请事先搭建好代理服务器,并在第三个参数传入代理服务器地址
  4. let ai = new TencentAI.TencentAI(
  5. app_key,
  6. app_id,
  7. (proxy = 'https://domain.com/proxy_tencent_ai'),
  8. );
  9. // 浏览器端用法同上
  10. </script>

代理服务器 Nginx 简单设置

  1. location /proxy_tencent_ai {
  2. proxy_pass https://api.ai.qq.com/;
  3. }

Deno

暂时只支持 js, ts 暂不支持。

ai.js

  1. import TencentAI from 'https://unpkg.com/@khs1994/tencent-ai@19.6.0-alpha.6/dist/mod.js';
  2. const app_key = '';
  3. const app_id = '';
  4. const ai = new TencentAI(app_key, app_id);
  5. // Deno 端用法同上
  1. $ deno ai.js -A

CI/CD

PCIT Node.js 示例项目

Test

please set system env first

  1. NODE_TENCENT_AI_APP_KEY=your-appkey
  2. NODE_TENCENT_AI_APP_ID=your-appid
  1. $ npm test