Home

Awesome

TencentAI JavaScript SDK

npm Build Status codecov install size code style: prettier Build Status Build Status

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

微信订阅号

<p align="center"> <img width="200" src="https://user-images.githubusercontent.com/16733187/46847944-84a96b80-ce19-11e8-9f0c-ec84b2ac463e.jpg"> </p> <p align="center"><strong>关注项目作者微信订阅号,接收项目最新动态</strong></p>

Installation

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

Usage

const { Translate, TencentAIError } = require('@khs1994/tencent-ai');

const App = {
  // 设置请求数据(应用密钥、接口请求参数)
  appkey: 'your-appkey', // process.env.NODE_TENCENT_AI_APP_KEY
  appid: 'your-appid', // process.env.NODE_TENCENT_AI_APP_ID
};

const translate = new Translate(App.appkey, App.appid);

// 文本翻译
translate.text('你好').then(
  res => {
    console.log(res);
  },
  e => {
    console.log(e);
  },
);

// Or Using async / await ES7

(async () => {
  try {
    let res = await translate.text('hello');
    console.log(res);
    // error demo
    res = await translate.text();
  } catch (e) {
    console.log(e);
  }
})();

微信小程序

$ npm i @khs1994/tencent-ai --save
// 解构赋值
const { TencentAI } = require('@khs1994/tencent-ai');

// 以下两项请到 ai.qq.com 控制台查看
const app_key = '';
const app_id = '';

// 获取全部实例,也可以只获取特定实例,例如 NLP,具体参考上边例子
const ai = new TencentAI(app_key, app_id);

ai.nlp.textChat('hello', 'session_id').then(
  res => {
    // TODO
    console.log(res);
  },
  e => console.log(e),
);

TypeScript

import TencentAI from '@khs1994/tencent-ai';

const app_key = '';
const app_id = '';

const ai = new TencentAI(app_key, app_id);

ai.nlp.textChat('hello', '1').then(res => {
  console.log(res);
});

浏览器

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

<script src="https://unpkg.com/@khs1994/tencent-ai@19.6.0-alpha.5/dist/tencent-ai.min.js"></script>

<script>
  // 由于跨域限制,请事先搭建好代理服务器,并在第三个参数传入代理服务器地址
  let ai = new TencentAI.TencentAI(
    app_key,
    app_id,
    (proxy = 'https://domain.com/proxy_tencent_ai'),
  );

  // 浏览器端用法同上
</script>

代理服务器 Nginx 简单设置

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

Deno

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

ai.js

import TencentAI from 'https://unpkg.com/@khs1994/tencent-ai@19.6.0-alpha.6/dist/mod.js';

const app_key = '';
const app_id = '';

const ai = new TencentAI(app_key, app_id);

// Deno 端用法同上
$ deno ai.js -A

CI/CD

PCIT Node.js 示例项目

Test

please set system env first

NODE_TENCENT_AI_APP_KEY=your-appkey

NODE_TENCENT_AI_APP_ID=your-appid
$ npm test