...
Smartbi AIChat二次开发,提供的是标准Restful API。只需要登录后,执行查询就可以了。所以,只需要2个API就可以完成需要的操作。
示例代码请参考:DemoCodeDemoCode
4.接口说明
4.1 登录AIChat
4.1.1 接口说明
值 | |
请求地址 | |
请求方式 | POST |
请求参数 | userName password smartbiServer casLoginUrl loginMethod |
...
代码块 | ||||||||||
---|---|---|---|---|---|---|---|---|---|---|
| ||||||||||
public class GradleTest { public static void main(String[] args) throws Exception { String server = "http://域名:端口/smartbi"; String user = "user"; String password = "password"; ObjectMapper mapper = new ObjectMapper(); Map<String, String> cookies = new HashMap<>(); boolean useRmiLogin = true; HttpRequest request = null; if (useRmiLogin) { String params = mapper.writeValueAsString(Arrays.asList(user, password)); request = HttpRequest.post(server + "/vision/RMIServlet") .form("className", "UserService") .form("methodName", "login") .form("params", params); try (HttpResponse response = request.execute()) { for (HttpCookie cookie : response.getCookies()) { cookies.put(cookie.getName(), cookie.getValue()); } } } else { // 以下的登录方式为使用openresoruce.jsp 等的URL访问方式 // 适用于修改了 Smartbi 的登录方式,不能直接通过 RMIServlet 用户密码登录 // I402882c701552f492f49736e01552f4df2c50013 是产品的一个内置报表,可根据需要修改 request = HttpRequest.get(server + "/vision/openresource.jsp?" + "resid=I402882c701552f492f49736e01552f4df2c50013&user=" + user + "&password=" + URLEncoder.encode(password, "UTF-8")); try (HttpResponse response = request.execute()) { for (HttpCookie cookie : response.getCookies()) { cookies.put(cookie.getName(), cookie.getValue()); } } } System.out.println(toCookie(cookies)); request = HttpRequest.post(server + "/vision/aichat/proxy/api/v1/login") .cookie(toCookie(cookies)) .form("userName", user) .form("smartbiServer", server) .form("loginMethod", "login") .form("cookie", toCookie(cookies)); String token = null; try (HttpResponse response = request.execute()) { for (HttpCookie cookie : response.getCookies()) { cookies.put(cookie.getName(), cookie.getValue()); } JsonNode tree = mapper.readTree(response.body()); token = tree.get("token").asText(); } // Thread.sleep(5000); // 旧版本由于存在问题,需要 sleep。 新版本后不需要 request = HttpRequest.post(server + "/vision/aichat/proxy/api/v3/conv/query_sync") .cookie(toCookie(cookies)) .header("token", token) // 数据模型ID .form("datasetId", "Iff808081018354c854c8f0fa018354cbde880001") // 对话ID .form("convId", "70c69b66-7af7-45fc-b51c-b486ceba1a43"UUID.randomUUID().toString()) // 问句 .form("question", "车均价 ") // 分析模式 .form("queryType", "analysis") // 问句的ID .form("id", "285fce7d-14e2-4a63-ab33-cf9abf09c41f"UUID.randomUUID().toString()) .form("pageSize", "10000") .form("showRows", "1000") .form("need_inquiry", "false") .form("onlyQueryChart", "false") .form("autoRecommendedChart", "false"); try (HttpResponse response = request.execute()) { System.out.println(response.body((new String(response.bodyBytes(), "UTF-8")); } } private static String toCookie(Map<String, String> cookies) throws IOException { StringBuilder buff = new StringBuilder(); for (Entry<String, String> entry : cookies.entrySet()) { buff.append(URLEncoder.encode(entry.getKey(), "UTF-8")); buff.append('='); buff.append(URLEncoder.encode(entry.getValue(), "UTF-8")); buff.append(';'); } return buff.toString(); } } |
4.3 查询接口(同步)
名称 | 描述 | 默认值 | |
接口路径 | http://host:port/aiweb/api/v3/conv/query_sync | ||
接口说明 | V3版本查询方法 -- 同步方法 | ||
header | token | 登录key,使用login方法获取;login方法请参考:登录AIChat | |
接口参数 | convId | 会话Id | |
datasetId | 数据模型Id | ||
question | 用户问题 | ||
queryType | 查询类型:
| 建议使用:analysis | |
id | 本次对话id 从外部传入才能做点赞点踩 | ||
need_inquiry | 本次查询是否需要使用反问功能 | default(true) | |
返回内容 | 返回查询结果(JSON),前端处理 { "code": 0, "result": "[\"[{\\\"年月\\\":\\\"2019-01\\\",\\\"销量\\\":132912,\\\"销量环比增长率\\\":\\\"-6.06%\\\",\\\"_销量占比\\\":\\\"8.8272%\\\"},...]", "message": null } code=0,表示返回正确(result是返回结果);否则message就是错误提示。 |
...