linux版本中间件

kg.h 6.5KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206
  1. /**
  2. * Copyright (c) 2017 Baidu.com, Inc. All Rights Reserved
  3. *
  4. * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with
  5. * the License. You may obtain a copy of the License at
  6. *
  7. * http://www.apache.org/licenses/LICENSE-2.0
  8. *
  9. * Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on
  10. * an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the
  11. * specific language governing permissions and limitations under the License.
  12. *
  13. * @author baidu aip
  14. */
  15. #ifndef __AIP_KG_H__
  16. #define __AIP_KG_H__
  17. #include "base/base.h"
  18. namespace aip {
  19. class Kg: public AipBase
  20. {
  21. public:
  22. std::string _create_task =
  23. "https://aip.baidubce.com/rest/2.0/kg/v1/pie/task_create";
  24. std::string _update_task =
  25. "https://aip.baidubce.com/rest/2.0/kg/v1/pie/task_update";
  26. std::string _task_info =
  27. "https://aip.baidubce.com/rest/2.0/kg/v1/pie/task_info";
  28. std::string _task_query =
  29. "https://aip.baidubce.com/rest/2.0/kg/v1/pie/task_query";
  30. std::string _task_start =
  31. "https://aip.baidubce.com/rest/2.0/kg/v1/pie/task_start";
  32. std::string _task_status =
  33. "https://aip.baidubce.com/rest/2.0/kg/v1/pie/task_status";
  34. Kg(const std::string & app_id, const std::string & ak, const std::string & sk): AipBase(app_id, ak, sk)
  35. {
  36. }
  37. /**
  38. * create_task
  39. * 创建一个新的信息抽取任务
  40. * @param name 任务名字
  41. * @param template_content json string 解析模板内容
  42. * @param input_mapping_file 抓取结果映射文件的路径
  43. * @param url_pattern url pattern
  44. * @param output_file 输出文件名字
  45. * options 可选参数:
  46. * limit_count 限制解析数量limit_count为0时进行全量任务,limit_count>0时只解析limit_count数量的页面
  47. */
  48. Json::Value create_task(
  49. std::string const & name,
  50. std::string const & template_content,
  51. std::string const & input_mapping_file,
  52. std::string const & url_pattern,
  53. std::string const & output_file,
  54. const std::map<std::string, std::string> & options)
  55. {
  56. std::map<std::string, std::string> data;
  57. data["name"] = name;
  58. data["template_content"] = template_content;
  59. data["input_mapping_file"] = input_mapping_file;
  60. data["url_pattern"] = url_pattern;
  61. data["output_file"] = output_file;
  62. std::copy(options.begin(), options.end(), std::inserter(data, data.end()));
  63. Json::Value result =
  64. this->request(_create_task, null, data, null);
  65. return result;
  66. }
  67. /**
  68. * update_task
  69. * 更新任务配置,在任务重新启动后生效
  70. * @param id 任务ID
  71. * options 可选参数:
  72. * name 任务名字
  73. * template_content json string 解析模板内容
  74. * input_mapping_file 抓取结果映射文件的路径
  75. * url_pattern url pattern
  76. * output_file 输出文件名字
  77. */
  78. Json::Value update_task(
  79. const int & id,
  80. const std::map<std::string, std::string> & options)
  81. {
  82. std::map<std::string, std::string> data;
  83. data["id"] = std::to_string(id);
  84. std::copy(options.begin(), options.end(), std::inserter(data, data.end()));
  85. Json::Value result =
  86. this->request(_update_task, null, data, null);
  87. return result;
  88. }
  89. /**
  90. * task_info
  91. * 根据任务id获取单个任务的详细信息
  92. * @param id 任务ID
  93. * options 可选参数:
  94. */
  95. Json::Value task_info(
  96. const int & id,
  97. const std::map<std::string, std::string> & options)
  98. {
  99. std::map<std::string, std::string> data;
  100. data["id"] = std::to_string(id);
  101. std::copy(options.begin(), options.end(), std::inserter(data, data.end()));
  102. Json::Value result =
  103. this->request(_task_info, null, data, null);
  104. return result;
  105. }
  106. /**
  107. * task_query
  108. * 该请求用于菜品识别。即对于输入的一张图片(可正常解码,且长宽比适宜),输出图片的菜品名称、卡路里信息、置信度。
  109. * options 可选参数:
  110. * id 任务ID,精确匹配
  111. * name 中缀模糊匹配,abc可以匹配abc,aaabc,abcde等
  112. * status 要筛选的任务状态
  113. * page 页码
  114. * per_page 页码
  115. */
  116. Json::Value task_query(
  117. const std::map<std::string, std::string> & options)
  118. {
  119. std::map<std::string, std::string> data;
  120. std::copy(options.begin(), options.end(), std::inserter(data, data.end()));
  121. Json::Value result =
  122. this->request(_task_query, null, data, null);
  123. return result;
  124. }
  125. /**
  126. * task_start
  127. * 启动一个已经创建的信息抽取任务
  128. * @param id 任务ID
  129. * options 可选参数:
  130. */
  131. Json::Value task_start(
  132. const int & id,
  133. const std::map<std::string, std::string> & options)
  134. {
  135. std::map<std::string, std::string> data;
  136. data["id"] = std::to_string(id);
  137. std::copy(options.begin(), options.end(), std::inserter(data, data.end()));
  138. Json::Value result =
  139. this->request(_task_start, null, data, null);
  140. return result;
  141. }
  142. /**
  143. * task_status
  144. * 查询指定的任务的最新执行状态
  145. * @param id 任务ID
  146. * options 可选参数:
  147. */
  148. Json::Value task_status(
  149. const int & id,
  150. const std::map<std::string, std::string> & options)
  151. {
  152. std::map<std::string, std::string> data;
  153. data["id"] = std::to_string(id);
  154. std::copy(options.begin(), options.end(), std::inserter(data, data.end()));
  155. Json::Value result =
  156. this->request(_task_status, null, data, null);
  157. return result;
  158. }
  159. };
  160. }
  161. #endif