robot.class.php 50 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975
  1. <?php
  2. if (!defined('IN_ONLINE')) {
  3. exit('Access Denied');
  4. }
  5. /**
  6. * Description of robot
  7. *
  8. * @author Administrator
  9. */
  10. class robot{
  11. private static $_robot;
  12. function __construct() {
  13. }
  14. public static function getInstance() {
  15. global $memory_limit;
  16. $memory_limit = ini_get("memory_limit");
  17. ini_set("memory_limit", '2048M');
  18. if (!self::$_robot) {
  19. $c = __CLASS__;
  20. self::$_robot = new $c;
  21. }
  22. return self::$_robot;
  23. }
  24. public function robot_chat_log() {
  25. $operate = utils::_get('operate');
  26. $operate = strtolower($operate);
  27. if (empty($operate)) {
  28. $column = column::getInstance()->getDisplayColumn('Robot_Chat_Search');
  29. $OperationTableColumns = column::getInstance()->tableColumns('Robot_Chat_Search',$column);
  30. $data['OperationTableColumns'] = $OperationTableColumns;
  31. common::echo_json_encode(200,$data);
  32. exit();
  33. }
  34. if ($operate == "search") {
  35. $cp = common::check_input($_POST ['cp']); //current_page
  36. $ps = common::check_input($_POST ['ps']); //ps
  37. if (empty($ps))
  38. $ps = 10;
  39. $sqlWhere = ' where 1=1';
  40. $text_search = common::check_input($_POST ['text_search']);
  41. $user_type = common::check_input($_POST ['user_type']);
  42. $question_type = common::check_input($_POST ['question_type']);
  43. $answer_type = common::check_input($_POST ['answer_type']);
  44. $answer_satisfication = common::check_input($_POST ['answer_satisfication']);
  45. $response_duration_type = common::check_input($_POST ['response_duration_type']);
  46. $response_duration_num = common::check_input($_POST ['response_duration_num']);
  47. if (!empty($text_search)){
  48. $sqlWhere .= " and (question_id ilike '%" . $text_search . "%' or user_name ilike '%" . $text_search . "%')";
  49. }
  50. if (isset($_POST['question_date_start']) && !empty($_POST['question_date_start'])){
  51. $sqlWhere .= " and question_date >= '" . common::usDate2sqlDate($_POST['question_date_start']) . " 00:00:00'";
  52. }
  53. if (isset($_POST['question_date_end']) && !empty($_POST['question_date_end'])){
  54. $sqlWhere .= " and question_date <= '" . common::usDate2sqlDate($_POST['question_date_end']) . " 23:59:59'";
  55. }
  56. if (!empty($user_type)){
  57. $sqlWhere .= " and user_type = '".strtolower($user_type)."'";
  58. }
  59. if (!empty($question_type)){
  60. //$question_type = $question_type == 'Free Text' ? "Free Question" : $question_type;
  61. $sqlWhere .= " and question_type = '$question_type'";
  62. }
  63. if (!empty($answer_type)){
  64. $sqlWhere .= " and answer_type = '$answer_type'";
  65. }
  66. if (!empty($answer_satisfication)){
  67. $answer_satisfication = $answer_satisfication == 'Null' ? '' : $answer_satisfication;
  68. $sqlWhere .= " and COALESCE(answer_satisfication,'') = '$answer_satisfication'";
  69. }
  70. if (!empty($response_duration_type) && !empty($response_duration_num)){
  71. if($response_duration_type == "thanOrEqual"){
  72. $sqlWhere .= " and response_duration >= '$response_duration_num'";
  73. } elseif ($response_duration_type == "equal"){
  74. $sqlWhere .= " and response_duration = '$response_duration_num'";
  75. } else {
  76. $sqlWhere .= " and response_duration <= '$response_duration_num'";
  77. }
  78. }
  79. $rc = $_POST ['rc'];
  80. if ($rc == - 1) {
  81. $sql = "SELECT count(1) from public.kln_robot_chat_log" . $sqlWhere;
  82. error_log($sql);
  83. $rc = common::excuteOneSql($sql);
  84. }
  85. $tp = ceil($rc / $ps);
  86. $order_by = " id desc";
  87. if ($rc > 0) {
  88. $sql = "SELECT ".column::getInstance()->getSearchSqlForDisplay('Robot_Chat_Search').",
  89. to_char(response_time, 'YYYY/MM/DD HH24:MI:SS') as _response_time,
  90. to_char(request_time, 'YYYY/MM/DD HH24:MI:SS') as _request_time,
  91. request_id
  92. from public.kln_robot_chat_log " . $sqlWhere .
  93. " order by $order_by limit " . $ps . " offset " . ($cp - 1) * $ps;
  94. $rs = common::excuteListSql($sql);
  95. foreach($rs as $key => $val){
  96. $rs[$key]["response_time"] = $val["_response_time"];
  97. $rs[$key]["request_time"] = $val["_request_time"];
  98. }
  99. $arrTmp = array('searchData' => $rs,
  100. 'tmp_search' => common::deCode($sql, 'E'),
  101. 'rc' => intval($rc),
  102. 'ps' => intval($ps),
  103. 'cp' => intval($cp),
  104. 'tp' => intval($tp));
  105. } else {
  106. $arrTmp = array('searchData' => array(),
  107. 'tmp_search' => common::deCode($sql, 'E'),
  108. 'rc' => intval($rc),
  109. 'ps' => intval($ps),
  110. 'cp' => intval($cp),
  111. 'tp' => intval($tp));
  112. }
  113. common::echo_json_encode(200,$arrTmp);
  114. exit();
  115. }
  116. if ($operate == "excel") {
  117. $sql = common::deCode($_POST ['tmp_search'], 'D');
  118. $sql = substr($sql, 0, strripos($sql, " limit"));
  119. if(!empty($sql)){
  120. $rss = common::excuteListSql($sql);
  121. }
  122. foreach($rss as $key =>$val){
  123. $rs[$key]["response_time"] = $val["_response_time"];
  124. $rs[$key]["request_time"] = $val["_request_time"];
  125. }
  126. common::echo_json_encode(200,array("msg"=>"success","Data" => $rss));
  127. exit;
  128. }
  129. if ($operate == "api_log") {
  130. $request_id = common::check_input($_POST['request_id']);
  131. $sql = "select question_content,ai_response_content from public.kln_robot_chat_log where request_id = '$request_id'";
  132. $data = common::excuteObjectSql($sql);
  133. $question_content = $data['question_content'];
  134. $ai_response_content = json_decode($data['ai_response_content']);
  135. common::echo_json_encode(200,array("msg"=>"success","Data" => array("request_content" =>$question_content, "ai_response_content"=>$ai_response_content)));
  136. //common::echo_json_encode(200,array("msg"=>"success","Data" => $data));
  137. exit;
  138. }
  139. }
  140. public function robot_api_log() {
  141. $operate = utils::_get('operate');
  142. $operate = strtolower($operate);
  143. if (empty($operate)) {
  144. $column = column::getInstance()->getDisplayColumn('Robot_API_Search');
  145. $OperationTableColumns = column::getInstance()->tableColumns('Robot_API_Search',$column);
  146. $data['OperationTableColumns'] = $OperationTableColumns;
  147. common::echo_json_encode(200,$data);
  148. exit();
  149. }
  150. if ($operate == "search") {
  151. $cp = common::check_input($_POST ['cp']); //current_page
  152. $ps = common::check_input($_POST ['ps']); //ps
  153. if (empty($ps))
  154. $ps = 10;
  155. //只查询自由问题
  156. $sqlWhere = " where 1=1 and question_type = 'Free Text' ";
  157. $text_search = common::check_input($_POST ['text_search']);
  158. $ai_model = common::check_input($_POST ['ai_model']);
  159. $response_duration_type = common::check_input($_POST ['response_duration_type']);
  160. $response_duration_num = common::check_input($_POST ['response_duration_num']);
  161. if (!empty($text_search)){
  162. $sqlWhere .= " and (question_id ilike '%" . $text_search . "%' or request_id ilike '%" . $text_search . "%')";
  163. }
  164. if (!empty($ai_model)){
  165. $sqlWhere .= " and lower(ai_model) = '".strtolower($ai_model)."'";
  166. }
  167. if (isset($_POST['request_date_start']) && !empty($_POST['request_date_start']))
  168. $sqlWhere .= " and request_time >= '" . common::usDate2sqlDate($_POST['request_date_start']) . " 00:00:00'";
  169. if (isset($_POST['request_date_end']) && !empty($_POST['request_date_end']))
  170. $sqlWhere .= " and request_time <= '" . common::usDate2sqlDate($_POST['request_date_end']) . " 23:59:59'";
  171. if (!empty($response_duration_type) && !empty($response_duration_num)){
  172. if($response_duration_type == "thanOrEqual"){
  173. $sqlWhere .= " and response_duration >= '$response_duration_num'";
  174. } elseif ($response_duration_type == "equal"){
  175. $sqlWhere .= " and response_duration = '$response_duration_num'";
  176. } else {
  177. $sqlWhere .= " and response_duration <= '$response_duration_num'";
  178. }
  179. }
  180. $rc = $_POST ['rc'];
  181. if ($rc == - 1) {
  182. $sql = "SELECT count(1) from public.kln_robot_chat_log" . $sqlWhere;
  183. $rc = common::excuteOneSql($sql);
  184. }
  185. $tp = ceil($rc / $ps);
  186. $order_by = " id desc";
  187. if ($rc > 0) {
  188. $sql = "SELECT ".column::getInstance()->getSearchSqlForDisplay('Robot_API_Search').",
  189. to_char(response_time, 'YYYY/MM/DD HH24:MI:SS') as _response_time,
  190. to_char(request_time, 'YYYY/MM/DD HH24:MI:SS') as _request_time
  191. from public.kln_robot_chat_log " . $sqlWhere .
  192. " order by $order_by limit " . $ps . " offset " . ($cp - 1) * $ps;
  193. $rs = common::excuteListSql($sql);
  194. foreach($rs as $key => $val){
  195. $rs[$key]["response_time"] = $val["_response_time"];
  196. $rs[$key]["request_time"] = $val["_request_time"];
  197. }
  198. $arrTmp = array('searchData' => $rs,
  199. 'tmp_search' => common::deCode($sql, 'E'),
  200. 'rc' => $rc,
  201. 'ps' => $ps,
  202. 'cp' => $cp,
  203. 'tp' => $tp);
  204. } else {
  205. $arrTmp = array('searchData' => array(),
  206. 'tmp_search' => common::deCode($sql, 'E'),
  207. 'rc' => $rc,
  208. 'ps' => $ps,
  209. 'cp' => $cp,
  210. 'tp' => $tp);
  211. }
  212. common::echo_json_encode(200,$arrTmp);
  213. exit();
  214. }
  215. if ($operate == "excel") {
  216. $sql = common::deCode($_POST ['tmp_search'], 'D');
  217. $sql = substr($sql, 0, strripos($sql, " limit"));
  218. if(!empty($sql)){
  219. $rss = common::excuteListSql($sql);
  220. }
  221. foreach($rss as $key =>$val){
  222. $rs[$key]["response_time"] = $val["_response_time"];
  223. $rs[$key]["request_time"] = $val["_request_time"];
  224. }
  225. common::echo_json_encode(200,array("msg"=>"success","Data" => $rss));
  226. exit;
  227. }
  228. if ($operate == "api_log") {
  229. $request_id = common::check_input($_POST['request_id']);
  230. $sql = "select request_content,ai_response_content from public.kln_robot_chat_log where request_id = '$request_id'";
  231. $data = common::excuteObjectSql($sql);
  232. common::echo_json_encode(200,array("msg"=>"success","Data" => $data));
  233. exit;
  234. }
  235. }
  236. public function robot_prompt_configuration() {
  237. $operate = utils::_get('operate');
  238. $operate = strtolower($operate);
  239. if (empty($operate)) {
  240. //查询所有配置信息 目前只有一个表一个数据
  241. $configuration = common::excuteObjectSql("select id, character_name,professional_field,main_tasks,table_name,table_description,
  242. table_structure_configuration,response_rule_configuration,output_format_configuration,fixed_problem_configuration
  243. from public.kln_robot_prompt_configuration where id = 1");
  244. $PromptAndVue = $this->dealPromptFormatAndVue($configuration);
  245. //tableDat-VUE
  246. $tableData = $PromptAndVue["vuedata"]["tableData"];
  247. //stepData -VUE
  248. $stepData = $PromptAndVue["vuedata"]["stepData"];
  249. //formatList -VUE
  250. $formatListType = $PromptAndVue["vuedata"]["formatListType"];
  251. $formatList = $PromptAndVue["vuedata"]["formatList"];
  252. //promptFormat
  253. $promptFormat =$PromptAndVue["promptFormat"];
  254. //获取历史变更记录
  255. $old_configuration = $this->getChangedLog($configuration["id"]);
  256. $id = $configuration['id'];
  257. $character_name = $configuration['character_name'];
  258. $professional_field = $configuration['professional_field'];
  259. $main_tasks = $configuration['main_tasks'];
  260. $table_name = $configuration['table_name'];
  261. $table_description = $configuration['table_description'];
  262. $prompt_summary = array("您是专門从事".$professional_field."的".$character_name."...",
  263. "表名:{".$table_name."}...",
  264. "响应规则,请按以下步骤处理...");
  265. $data = array(
  266. "id"=>$id,
  267. "character_name"=>$character_name,
  268. "professional_field"=>$professional_field,
  269. "main_tasks"=>$main_tasks,
  270. "table_name"=>$table_name,
  271. "table_description"=>$table_description,
  272. "tableData"=>$tableData,
  273. "stepData"=>$stepData,
  274. "formatList"=>$formatList,
  275. "formatListType"=>$formatListType,
  276. "complete_prompt"=>$promptFormat,
  277. "prompt_summary"=>$prompt_summary,
  278. "prompt_log_record"=>$old_configuration
  279. );
  280. $data = utils::arrayRemoveNull($data);
  281. common::echo_json_encode(200,$data);
  282. exit();
  283. }
  284. if ($operate == "save") {
  285. $id =common::check_input($_POST['id']);
  286. $character_name = common::check_input($_POST['role_name']);
  287. $professional_field = common::check_input($_POST['professional_field']);
  288. $main_tasks = common::check_input($_POST['main_tasks']);
  289. $table_name = common::check_input($_POST['table_name']);
  290. $table_description = common::check_input($_POST['table_description']);
  291. $outputvalue = common::check_input($_POST['outputvalue']);
  292. $tableDataList = $_POST['tableDataList'];
  293. $stepData = $_POST['stepData'];
  294. $formatList = $_POST['formatList'];
  295. $table_structure_configuration = common::check_input(json_encode($tableDataList));
  296. $response_rule_configuration = common::check_input(json_encode($stepData));
  297. $output_data = array("output_type"=>$outputvalue,"data"=>$formatList);
  298. $output_format_configuration = common::check_input(json_encode($output_data));
  299. if(empty($id)){
  300. $sql = "INSERT INTO public.kln_robot_prompt_configuration(
  301. character_name, professional_field, main_tasks, table_name,
  302. table_description, table_structure_configuration, response_rule_configuration,
  303. output_format_configuration, created_by, created_time)
  304. VALUES ('$character_name', '$professional_field', '$main_tasks', '$table_name',
  305. '$table_description', '$table_structure_configuration', '$response_rule_configuration',
  306. '$output_format_configuration', '"._getLoginName()."', now());";
  307. }else{
  308. $sql = "update public.kln_robot_prompt_configuration
  309. set character_name = '$character_name',professional_field ='$professional_field', main_tasks = '$main_tasks',
  310. table_name = '$table_name',table_description = '$table_description',
  311. table_structure_configuration = '$table_structure_configuration',
  312. response_rule_configuration = '$response_rule_configuration',
  313. output_format_configuration = '$output_format_configuration'
  314. where id = '$id';";
  315. //插入变更日志记录
  316. $sql .= "INSERT INTO public.kln_robot_prompt_configuration_log(ref_id, character_name, professional_field, main_tasks, table_name,
  317. table_description, table_structure_configuration, response_rule_configuration,
  318. output_format_configuration, created_by, created_time)
  319. VALUES ('$id','$character_name', '$professional_field', '$main_tasks', '$table_name',
  320. '$table_description', '$table_structure_configuration', '$response_rule_configuration',
  321. '$output_format_configuration', '"._getLoginName()."', now());";
  322. }
  323. error_log($sql);
  324. $rs = common::excuteUpdateSql($sql);
  325. if (!$rs) {
  326. $data = array("msg" => "save Error");
  327. } else {
  328. $data = array("msg" => "save Successful");
  329. }
  330. $data = array("msg" => "save Successful");
  331. common::echo_json_encode(200,$data);
  332. exit();
  333. }
  334. if($operate == "preview_propmpt_witout_save"){
  335. $character_name = common::check_input($_POST['role_name']);
  336. $professional_field = common::check_input($_POST['professional_field']);
  337. $main_tasks = common::check_input($_POST['main_tasks']);
  338. $table_name = common::check_input($_POST['table_name']);
  339. $table_description = common::check_input($_POST['table_description']);
  340. $outputvalue = common::check_input($_POST['outputvalue']);
  341. $tableDataList = $_POST['tableDataList'];
  342. $stepData = $_POST['stepData'];
  343. $formatList = $_POST['formatList'];
  344. $table_structure_configuration = common::check_input(json_encode($tableDataList));
  345. $response_rule_configuration = common::check_input(json_encode($stepData));
  346. $output_data = array("output_type"=>$outputvalue,"data"=>$formatList);
  347. $output_format_configuration = common::check_input(json_encode($output_data));
  348. //组合拼接需要的数据
  349. $configuration = array("character_name"=>$character_name,
  350. "professional_field"=>$professional_field,
  351. "main_tasks"=>$main_tasks,
  352. "table_name"=>$table_name,
  353. "table_description"=>$table_description,
  354. "professional_field"=>$professional_field,
  355. "table_structure_configuration"=>$table_structure_configuration,
  356. "response_rule_configuration" =>$response_rule_configuration,
  357. "output_format_configuration" =>$output_format_configuration
  358. );
  359. $PromptAndVue = $this->dealPromptFormatAndVue($configuration);
  360. //promptFormat
  361. $promptFormat =$PromptAndVue["promptFormat"];
  362. $character_name = empty($configuration['character_name']) ? "" :$configuration['character_name'];
  363. $professional_field = empty($configuration['professional_field']) ? "" :$configuration['professional_field'];
  364. $main_tasks = empty($configuration['main_tasks']) ? "" :$configuration['main_tasks'];
  365. $table_name = empty($configuration['table_name']) ? "" :$configuration['table_name'];
  366. $table_description = empty($configuration['table_description']) ? "" :$configuration['table_description'];
  367. $prompt_summary = array("您是专門从事".$professional_field."的".$character_name."...",
  368. "表名:{".$table_name."}...",
  369. "响应规则,请按以下步骤处理...");
  370. $data = array(
  371. "complete_prompt"=>$promptFormat,
  372. "prompt_summary"=>$prompt_summary
  373. );
  374. common::echo_json_encode(200,$data);
  375. exit();
  376. }
  377. if($operate == "test_with_ds_claude"){
  378. //$data = "### 查询结果\n\n- **总hbol数量**: 3581667\n\n- **前10票hbol信息**:\n\n | HBOL | MBOL | 运输方式 | 服务类型 | 发货人 | 收货人 | 数量 | 总重量(KG) | 预计离港时间 | 预计到港时间 |\n |------|------|----------|----------|--------|--------|------|------------|--------------|--------------|\n | E1205546127 | UASUCNNBO432262 | sea | CY/CY | HANGZHOU ZHONGSHEN BAGS & LUGGAGES CO.LTD. | C.Y IMPORT CORPORATION | 455 | 8689.000 | 2012-05-31 | 2012-07-09 |\n| E120537061 | HJSCFOC202818100 | sea | CY/DOOR | NANLIN(FUJIAN) INDUSTRIES CO.,LTD | CHICAGO STOOL & CHAIR INC | 1200 | 14400.000 | 2012-05-31 | 2012-06-18 |\n| E120537095 | MAEU602116826 | sea | CY/CY | LONG SHAN IMP & EXP CO LTD | COMPOSITE TECHNOLOGY INTERNATIONAL INC | 12 | 19900.000 | 2012-05-27 | 2012-07-02 |\n| E1205570012 | COSU6025917580 | sea | CY/DOOR | SHAANXI FUHUA CHEMICAL CO., LTD | CIMBAR PERFORMANCE MINERALS | 38 | 34396.000 | 2012-05-29 | 2012-07-06 |\n| E1205391561A | YMLUW230775660 | sea | CFS/CY | WISTRON NEWEB (KUNSHAN) CORPORATION | ARUBA NETWORKS | 30 | 391.000 | 2012-05-29 | 2012-06-16 |\n| A1208395110 | ZIMUSNH5455049 | sea | CFS/CFS | SHANGHAI EAST BEST INT'L BUSINESS DEV.CO | LUMINA GLOBAL, INC. | 2 | 2600.000 | 2012-08-25 | 2012-09-06 |\n| E1205391561 | YMLUW230775660 | sea | CY/CY | SERCOMM CORP. C/O SERNET | ARUBA NETWORKS | 354 | 2833.940 | 2012-05-29 | 2012-06-16 |\n| E1205395237 | YMLUW230777029 | sea | CY/CY | UNIPAK RESOURCES CO.,LTD. | PAN PACIFIC PLASTIC | 404 | 2424.000 | 2012-05-29 | 2012-06-16 |\n| X1205480323 | YMLUW241023500 | sea | CY/CY | SHANGHAI AUTOMOBILE IMPORT&EXPORT CO.,LTD. | AMERICAN CONDENSER | 1082 | 5178.000 | 2012-05-25 | 2012-06-12 |\n| E1205359270 | MAEU602108615 | sea | CY/CY | LZ CASKET MANUFACTURING CO.,LTD | BRICK ALLEY,LLC | 64 | 5901.700 | 2012-05-29 | 2012-07-06 |\n ";
  379. //$data = "### 查询结果\n\n- **总hbol数量**: 3581667\n\n- **前10票hbol信息**:\n\n| HBOL | MBOL | 运输方式 | 服务类型 | 发货人 | 收货人 | 数量 | 总重量(KG) | 预计离港时间 | 预计到港时间 |\n|------|------|----------|----------|--------|--------|------|------------|--------------|--------------|\n| E1205546127 | UASUCNNBO432262 | sea | CY/CY | HANGZHOU ZHONGSHEN BAGS & LUGGAGES CO.LTD. | C.Y IMPORT CORPORATION | 455 | 8689.000 | 2012-05-31 | 2012-07-09 |\n| E120537061 | HJSCFOC202818100 | sea | CY/DOOR | NANLIN(FUJIAN) INDUSTRIES CO.,LTD | CHICAGO STOOL & CHAIR INC | 1200 | 14400.000 | 2012-05-31 | 2012-06-18 |\n| E120537095 | MAEU602116826 | sea | CY/CY | LONG SHAN IMP & EXP CO LTD | COMPOSITE TECHNOLOGY INTERNATIONAL INC | 12 | 19900.000 | 2012-05-27 | 2012-07-02 |\n| E1205570012 | COSU6025917580 | sea | CY/DOOR | SHAANXI FUHUA CHEMICAL CO., LTD | CIMBAR PERFORMANCE MINERALS | 38 | 34396.000 | 2012-05-29 | 2012-07-06 |\n| E1205391561A | YMLUW230775660 | sea | CFS/CY | WISTRON NEWEB (KUNSHAN) CORPORATION | ARUBA NETWORKS | 30 | 391.000 | 2012-05-29 | 2012-06-16 |\n| A1208395110 | ZIMUSNH5455049 | sea | CFS/CFS | SHANGHAI EAST BEST INT'L BUSINESS DEV.CO | LUMINA GLOBAL, INC. | 2 | 2600.000 | 2012-08-25 | 2012-09-06 |\n| E1205391561 | YMLUW230775660 | sea | CY/CY | SERCOMM CORP. C/O SERNET | ARUBA NETWORKS | 354 | 2833.940 | 2012-05-29 | 2012-06-16 |\n| E1205395237 | YMLUW230777029 | sea | CY/CY | UNIPAK RESOURCES CO.,LTD. | PAN PACIFIC PLASTIC | 404 | 2424.000 | 2012-05-29 | 2012-06-16 |\n| X1205480323 | YMLUW241023500 | sea | CY/CY | SHANGHAI AUTOMOBILE IMPORT&EXPORT CO.,LTD. | AMERICAN CONDENSER | 1082 | 5178.000 | 2012-05-25 | 2012-06-12 |\n| E1205359270 | MAEU602108615 | sea | CY/CY | LZ CASKET MANUFACTURING CO.,LTD | BRICK ALLEY,LLC | 64 | 5901.700 | 2012-05-29 | 2012-07-06 |\n";
  380. //$return = array("type"=>"markdown","data" =>$data);
  381. //common::echo_json_encode(200,$return);
  382. //exit();
  383. //分担查询
  384. include ONLINE_ROOT . 'libs' . DS . 'map_config.ini.php';
  385. $ai_method = $_POST["test_ai_method"];
  386. $systemPrompt = $_POST["prompt"];
  387. $message = $_POST["test_question"];
  388. $model = $ai_method == 'DS' ? "deepseek" : "claude";
  389. //question_date request_time
  390. $request_question_time = date("Y-m-d H:i:s");
  391. $question_time = date("H:i:s");
  392. $item_value = common::excuteOneSql("select item_value from public.config where item = 'AIAPISetting'");
  393. $config = json_decode($item_value,true);
  394. $response = AIClientFactory::create($model, $config[$model], $systemPrompt, $message, $history = []);
  395. $response_time = date("Y-m-d H:i:s");
  396. $input_token = "";
  397. $output_token = "";
  398. $date1 = new DateTime($request_question_time);
  399. $date2 = new DateTime($response_time);
  400. $response_duration = $date2->getTimestamp() - $date1->getTimestamp();
  401. //回答问题类型,在没有超时的情况下,固定:AI回答
  402. $answer_type = "AI Answer";
  403. if ($response_duration > 120){
  404. $answer_type = "Timeout";
  405. }
  406. //获取自然序列
  407. $sequence = common::getChatAiSequence();
  408. $name = $ai_method == 'DS' ? "DS" : "CD";
  409. $request_id = "R".$name."".date("Ymd").$sequence;
  410. $question_id = "Q"."".date("Ymd").$sequence;
  411. //处理一下参数
  412. $user_name = common::check_input(_getLoginName());
  413. $user_type = _isApexLogin() ? "employee" : "customer";
  414. //这里的测试,固定写自由文本
  415. $question_type = 'Free Text';
  416. $question_id = common::check_input($question_id);
  417. $question_content = common::check_input($message);
  418. $answer_type = common::check_input($answer_type);
  419. //记录回答展示给客户的具体内容
  420. $answer = common::check_input(common::getChatAimessage($response['message']));
  421. $ai_model = common::check_input($model);
  422. $input_token = common::check_input($input_token);
  423. $request_content = common::check_input(json_encode($response['data']));
  424. $ai_response_content = common::check_input(json_encode($response['full_response']));
  425. $output_token = common::check_input($output_token);
  426. $answer_template = "";
  427. $message = json_decode(common::getChatAimessage($response['message']),true);
  428. if($message["can_query"] == "true" && !empty($message["sql"])){
  429. $reference = $message["reference"];
  430. $answer_template = common::check_input($message["reference"]);
  431. $sql = $message["sql"];
  432. //拆分sql 存在多条的情况
  433. $sqlArr = explode(";", $sql);
  434. //给所有sql 拼接用户权限
  435. $sqlWhere = ' ' . common::searchExtendHand_KLN("ocean", $_SESSION["ONLINE_USER"]);
  436. //先不考虑总数,处理有难度
  437. foreach($sqlArr as $_sql){
  438. error_log($_sql);
  439. if(empty($_sql)){
  440. continue;
  441. }
  442. //根据public.kln_ocean 和 WHERE 的位置关系,带入权限
  443. $new_sql = utils::modifyString($_sql,$sqlWhere);
  444. //处理limit 超过10 先限制10
  445. $new_sql = utils::processLimitClause($new_sql);
  446. //$rs = common::excuteListSql($new_sql);
  447. $rs = $mapdb->GetAll($new_sql) or ( (!$mapdb->ErrorMsg()) or error_log(common::dbLog($mapdb, $new_sql), 0));
  448. $reference = utils::replacementsMultiline($rs,$reference,$new_sql);
  449. }
  450. //有就去掉{{#each hbol_list}}\n {{/each}}
  451. $reference = preg_replace([
  452. '/\{\{[^}]+\}\}\n/', // 匹配开始标签及换行
  453. '/\{\{\/[^}]+\}\}/' // 匹配结束标签
  454. ], '', $reference);
  455. //$reference = $reference.$message["reference"];
  456. $answer = $reference;
  457. //$response['message'] = json_decode(common::getChatAimessage($response['message']),true);
  458. //$answer = $response;
  459. }else{
  460. $answer = $message["response"];
  461. }
  462. //最后插入数据
  463. $sql = "INSERT INTO public.kln_robot_chat_log( question_id, user_name, user_type, question_type, question_content,
  464. answer_type, answer, answer_satisfication, answer_duration,answer_template, question_date,
  465. question_time, request_id, ai_model, request_content, input_token,
  466. ai_response_content, output_token, request_time, response_time,
  467. response_duration)
  468. VALUES ('$question_id', '$user_name', '$user_type', '$question_type', '$question_content',
  469. '$answer_type', '".common::check_input($answer)."', '', '$response_duration','$answer_template','$request_question_time',
  470. '$question_time', '$request_id', '$ai_model','$request_content', '$input_token',
  471. '$ai_response_content', '$output_token', '$request_question_time', '$response_time',
  472. '$response_duration');";
  473. $rs = common::excuteUpdateSql($sql);
  474. if (!$rs) {
  475. $data = array("msg" => "AI Chat Save Error");
  476. common::echo_json_encode(200,$data);
  477. exit();
  478. }
  479. $return = array("type"=>"markdown","data" =>$answer);
  480. common::echo_json_encode(200,$return);
  481. exit();
  482. }
  483. if ($operate == "download"){
  484. $sql = common::deCode($_REQUEST['tmp_search'], 'D');
  485. if(!empty($sql)){
  486. $rs = common::excuteListSql($sql);
  487. }
  488. common::echo_json_encode(200,array("msg"=>"success","Data" => $rs));
  489. exit;
  490. }
  491. }
  492. public function ai_chat(){
  493. $operate = utils::_get('operate');
  494. $operate = strtolower($operate);
  495. if($operate == "ai_chat_prompt"){
  496. //查询所有配置信息 目前只有一个表一个数据
  497. $configuration = common::excuteObjectSql("select id, character_name,professional_field,main_tasks,table_name,table_description,
  498. table_structure_configuration,response_rule_configuration,output_format_configuration,fixed_problem_configuration
  499. from public.kln_robot_prompt_configuration where id = 1");
  500. $PromptAndVue = $this->dealPromptFormatAndVue($configuration);
  501. //promptFormat
  502. $promptFormat =$PromptAndVue["promptFormat"];
  503. $data = array("msg" => "succssful","prompt"=>$promptFormat);
  504. common::echo_json_encode(200,$data);
  505. exit();
  506. }
  507. if ($operate == "ai_chat_fixed_init"){
  508. $rs = common::excuteListSql("select fixed_faq,secondary_interaction_content from public.kln_robot_chat_fixed where active = true order by id");
  509. $data = array();
  510. foreach($rs as $v){
  511. $data[] = array("label" => $v['fixed_faq'],"value" =>$v['fixed_faq'],"isLong" => strlen($v['fixed_faq']) >80);
  512. }
  513. $data = array("msg" => "succssful","fixed_question"=>$data);
  514. common::echo_json_encode(200,$data);
  515. exit();
  516. }
  517. /**
  518. * ai 自由聊天
  519. */
  520. if($operate == "ai_chat"){
  521. //前端生成一个唯一的serial_no
  522. $serial_no = common::check_input($_POST["serial_no"]);
  523. //系统提示词
  524. $systemPrompt = common::check_input($_POST["prompt"]);
  525. $question_type = common::check_input($_POST['question_type']);
  526. $question_type = $question_type == 'Free Question' ? "Free Text" : $question_type;
  527. $question_content = common::check_input($_POST['question_content']);
  528. //固定问题的原始问题
  529. $fixed_faq = common::check_input($_POST['fixed_faq']);
  530. //claude deepseek 根據賬號信息判断
  531. $model = common::getUserCountry();
  532. //$model = "deepseek";
  533. //获取自然序列
  534. $sequence = common::getChatAiSequence();
  535. $name = $model == 'deepseek' ? "DS" : "CD";
  536. $request_id = "R".$name."".date("Ymd").$sequence;
  537. $question_id = "Q"."".date("Ymd").$sequence;
  538. //处理一下参数
  539. $user_name = common::check_input(_getLoginName());
  540. $user_type = _isApexLogin() ? "employee" : "customer";
  541. //首先生成基本记录数据 以便以后根据情况更新状态
  542. $sql = "INSERT INTO public.kln_robot_chat_log(serial_no, question_id, user_name, user_type, question_type, question_content,
  543. question_date, question_time, request_id, request_time,ai_model)
  544. VALUES ('$serial_no','$question_id', '$user_name', '$user_type', '$question_type', '$question_content',
  545. now(),to_char(CURRENT_TIME::time, 'HH24:MI:SS'), '$request_id',now(),'$model');";
  546. $rs = common::excuteUpdateSql($sql);
  547. if (!$rs) {
  548. $data = array("type"=>"markdown","data" => "AI Chat Save Error");
  549. common::echo_json_encode(200,$data);
  550. exit();
  551. }
  552. if ($question_type == 'Predefined Question'){
  553. $answer = $this->doFixedAnswerAndLog($serial_no,$fixed_faq,$question_content);
  554. //判断固定回答是否结束
  555. $is_fixedAnswer_end = false;
  556. $fixedChat = common::excuteObjectSql("select * from public.kln_robot_chat_fixed where fixed_faq = '$fixed_faq'");
  557. if (empty($fixedChat['secondary_interaction_content']) || $question_content != $fixedChat['fixed_faq'] ){
  558. $is_fixedAnswer_end = true;
  559. }
  560. } else {
  561. $answer = $this->doFreeAnswerAndLog($serial_no,$model,$systemPrompt, $question_content, $history = []);
  562. //自由问题固定为true
  563. $is_fixedAnswer_end = true;
  564. }
  565. $return = array("type"=>"markdown","is_fixedAnswer_end"=>$is_fixedAnswer_end,"data" =>$answer);
  566. common::echo_json_encode(200,$return);
  567. exit();
  568. }
  569. if($operate == "ai_chat_stop"){
  570. $serial_no = common::check_input($_POST["serial_no"]);
  571. $updateSql = "update public.kln_robot_chat_log set answer_type = 'Suspend' where serial_no = '$serial_no'";
  572. $updateRs = common::excuteUpdateSql($updateSql);
  573. if (!$updateRs) {
  574. $data = array("msg" => "AI Chat Response Stop Error");
  575. }else{
  576. $data = array("msg" => "AI Chat Response Stop succssful");
  577. }
  578. common::echo_json_encode(200,$data);
  579. exit();
  580. }
  581. if($operate == "ai_chat_answer_mark"){
  582. $serial_no = common::check_input($_POST["serial_no"]);
  583. $answer_satisfication = common::check_input($_POST["feedback"]);
  584. $updateSql = "update public.kln_robot_chat_log set answer_satisfication = '$answer_satisfication' where serial_no = '$serial_no'";
  585. $updateRs = common::excuteUpdateSql($updateSql);
  586. if (!$updateRs) {
  587. $data = array("msg" => "AI Chat Response Mark Error");
  588. }else{
  589. $data = array("msg" => "AI Chat Response Mark succssful");
  590. }
  591. common::echo_json_encode(200,$data);
  592. exit();
  593. }
  594. if($operate == "download"){
  595. $faq = common::deCode($_GET['faq'], 'D');
  596. $sql = common::getFaqSql($faq);
  597. $columns = common::getFaqColumnSql($faq);
  598. //拼接用户权限
  599. $sqlWhere = ' ' . common::searchExtendHand_KLN("ocean", $_SESSION["ONLINE_USER"]);
  600. $sql = str_replace('<{ExtendHand_KLN}>', $sqlWhere, $sql);
  601. $rss = common::excuteListSql($sql);
  602. $rss = common::dealDataWithFaq($faq,$rss);
  603. //XLSXWriter输出
  604. $writer = new XLSXWriter();
  605. $sheetName = date('mdy');
  606. foreach ($rss as $k => $v) {
  607. $column = array();
  608. $value_temp = array();
  609. foreach($columns as $col){
  610. $value_temp[] = $v[$col['database_column_name']];
  611. if($k == 0){
  612. $column[] = $col['display_name'];
  613. }
  614. }
  615. if($k == 0){
  616. //输出column
  617. $writer->writeSheetRow($sheetName, $column);
  618. }
  619. $writer->writeSheetRow($sheetName, $value_temp, []);
  620. }
  621. $outfile = "Complete Data_" .date("Y-m-d H:i:s") . ".xlsx";
  622. if (file_exists($outfile)) {
  623. $outfile = "Complete Data_" .date("Y-m-d H:i:s") . "_". md5(uniqid(rand(), true)) . ".xlsx";
  624. }
  625. ob_end_clean();
  626. header('Content-Type: application/vnd.ms-excel');
  627. header("Expires: 0");
  628. header("Cache-Control: must-revalidate, post-check=0, pre-check=0");
  629. header("Pragma: can-cache");
  630. header('Content-Disposition:attachment;filename="' . $outfile . '"');
  631. $writer->writetostdout();
  632. }
  633. }
  634. public function dealPromptFormatAndVue($configuration){
  635. //tableDat-VUE
  636. $tableData = array();
  637. $table_structure_text="[";
  638. $table_structure_configuration = json_decode($configuration['table_structure_configuration'],true);
  639. foreach($table_structure_configuration as $tk =>$tv){
  640. $tableData[] = $tv;
  641. $table_structure_text .='{
  642. "name": "'.$tv["name"].'",
  643. "type": "'.$tv["type"].'",
  644. "description": "'.$tv["description"].'",
  645. "exampledata": "'.$tv["exampledata"].'",
  646. }';
  647. if(count($table_structure_configuration)-1 > $tk){
  648. $table_structure_text .=",";
  649. }
  650. $table_structure_text .=common::splicedLlineBreaks();
  651. }
  652. $table_structure_text.="]";
  653. //stepData -VUE
  654. $stepData = array();
  655. $response_rule_text = "";
  656. $response_rule_configuration = json_decode($configuration['response_rule_configuration'],true);
  657. foreach($response_rule_configuration as $rk =>$rv){
  658. $tempTable = array();
  659. $tempTable["description"] = $rv["description"];
  660. $stepData[] = $tempTable;
  661. $response_rule_text .=($rk+1).'.'.$rv["description"];
  662. $response_rule_text .=common::splicedLlineBreaks();
  663. }
  664. //formatList -VUE
  665. $output_format_text = "{";
  666. $output_format_configuration = json_decode($configuration['output_format_configuration'],true);
  667. $formatListType = $output_format_configuration['output_type'];
  668. $formatList = empty($output_format_configuration['data']) ? array(): $output_format_configuration['data'];
  669. foreach($formatList as $ok =>$ov){
  670. $describe = $ov['describe'];
  671. if ($ov['selecttype'] == '必填字段') {
  672. $describe .= " . 必填要求:所有情况必填";
  673. } elseif ($ov['selecttype'] == '有条件必填'){
  674. $requirements = $ov['requirements'];
  675. $describe .= " . 必填要求:".$ov['selecttype']."(".$requirements.")";
  676. }
  677. $output_format_text .="\"".$ov['name']."\"".": "."\"".$describe."\"";
  678. $output_format_text .=common::splicedLlineBreaks();
  679. }
  680. $output_format_text .= "}";
  681. $output_format_text_two = "";
  682. foreach($formatList as $ok =>$ov){
  683. $describe = $ov['describe'];
  684. if ($ov['selecttype'] == '必填字段') {
  685. $describe .= " . 必填要求:所有情况必填";
  686. } elseif ($ov['selecttype'] == '有条件必填'){
  687. $requirements = $ov['requirements'];
  688. $describe .= " . 必填要求:".$ov['selecttype']."(".$requirements.")";
  689. }
  690. $output_format_text_two .="-".$ov['name'].": ".$describe;
  691. $output_format_text_two .=common::splicedLlineBreaks();
  692. }
  693. $promptFormat = $this->getCompletePromptFormat();
  694. $promptFormat = str_replace('<{professional_field}>', $configuration['character_name'], $promptFormat);
  695. $promptFormat = str_replace('<{character_name}>', $configuration['character_name'], $promptFormat);
  696. $promptFormat = str_replace('<{main_tasks}>', $configuration['main_tasks'], $promptFormat);
  697. $promptFormat = str_replace('<{table}>', $configuration['table_name'], $promptFormat);
  698. $promptFormat = str_replace('<{table_structure_configuration}>', $table_structure_text, $promptFormat);
  699. $promptFormat = str_replace('<{response_rule_configuration}>', $response_rule_text, $promptFormat);
  700. $promptFormat = str_replace('<{number}>', count($response_rule_configuration)+1, $promptFormat);
  701. $promptFormat = str_replace('<{output_type}>', $formatListType, $promptFormat);
  702. $promptFormat = str_replace('<{output_format_configuration}>', $output_format_text_two, $promptFormat);
  703. return array("vuedata"=>array("tableData"=>$tableData,"stepData"=>$stepData,"formatList"=>$formatList,"formatListType"=>$formatListType),
  704. "promptFormat" =>$promptFormat);
  705. }
  706. public function getChangedLog($id){
  707. $changeLogList = array();
  708. $configurationArr = common::excuteListSql("select id, character_name,professional_field,main_tasks,table_name,table_description,
  709. created_by,TO_CHAR(created_time, 'YYYY-mm-dd HH24:MI:SS') as created_time,
  710. table_structure_configuration,response_rule_configuration,output_format_configuration,fixed_problem_configuration
  711. from public.kln_robot_prompt_configuration_log where ref_id = '$id' order by id desc");
  712. foreach($configurationArr as $ca){
  713. $data = $this->dealPromptFormatAndVue($ca);
  714. $temp =array();
  715. $temp["time"] = $ca['created_time']." Prompt";
  716. $temp["person"] ="提交人:".$ca['created_by'];
  717. $temp["text"] = $data['promptFormat'];
  718. $changeLogList[] = $temp;
  719. }
  720. return $changeLogList;
  721. }
  722. public function getSystemPrompt(){
  723. //查询所有配置信息 目前只有一个表一个数据
  724. $configuration = common::excuteObjectSql("select id, character_name,professional_field,main_tasks,table_name,table_description,
  725. table_structure_configuration,response_rule_configuration,output_format_configuration,fixed_problem_configuration
  726. from public.kln_robot_prompt_configuration where id = 1");
  727. $PromptAndVue = $this->dealPromptFormatAndVue($configuration);
  728. //promptFormat
  729. $promptFormat =$PromptAndVue["promptFormat"];
  730. return $promptFormat;
  731. }
  732. public function getCompletePromptFormat() {
  733. $formatTpl = "
  734. 您是专门从<{professional_field}>的 <{character_name}>,请始终以<{output_type}>格式响应。您的主要任务是<{main_tasks}>
  735. 表名:<{table}>
  736. 表结构: \"columns\": <{table_structure_configuration}>
  737. 响应规则,请按以下步骤处理:
  738. <{response_rule_configuration}>
  739. <{number}>.请严格按照以下<{output_type}>格式返回,不要包含任何其他文字或说明:
  740. {
  741. \"can_query\": true,
  742. \"query_type\": \"货物跟踪\",
  743. \"reason\": \"分析原因\",
  744. \"sql\": \"生成的SQL語句\",
  745. \"reference\": \"Markdown格式的回复模板\",
  746. \"response\": \"直接回答內容或空字符串\"
  747. }
  748. 字段說明:
  749. <{output_format_configuration}>
  750. 重要:请只返回JSON对象,不要包含任何解释文字或代码块标记";
  751. return $formatTpl;
  752. }
  753. public function doFreeAnswerAndLog($serial_no,$model,$systemPrompt, $question_content, $history){
  754. //分担查询
  755. include ONLINE_ROOT . 'libs' . DS . 'map_config.ini.php';
  756. //Ai response
  757. $item_value = common::excuteOneSql("select item_value from public.config where item = 'AIAPISetting'");
  758. $config = json_decode($item_value,true);
  759. $response = AIClientFactory::create($model, $config[$model], $systemPrompt, $question_content, $history = []);
  760. $input_token = "";
  761. $output_token = "";
  762. $answer_template = "";
  763. $answer ="";
  764. $response_pram = common::excuteObjectSql("select to_char(now(), 'YYYY-mm-dd HH24:MI:SS') as response_time,
  765. CEIL(EXTRACT(EPOCH FROM (now() - question_date))) AS response_duration from public.kln_robot_chat_log where serial_no = '$serial_no'");
  766. $response_duration = $response_pram['response_duration'];
  767. //回答问题类型,在没有超时的情况下,固定:AI回答
  768. $answer_type = "AI Answer";
  769. if ($response_duration > 120){
  770. $answer_type = "Timeout";
  771. }
  772. $request_content = common::check_input(json_encode($response['data']));
  773. $ai_response_content = common::check_input(json_encode($response['full_response']));
  774. $message = json_decode(common::getChatAimessage($response['message']),true);
  775. if($message["can_query"] == "true" && !empty($message["sql"])){
  776. $reference = $message["reference"];
  777. $answer_template = common::check_input($message["reference"]);
  778. $sql = $message["sql"];
  779. //拆分sql 存在多条的情况
  780. $sqlArr = explode(";", $sql);
  781. //给所有sql 拼接用户权限
  782. $sqlWhere = ' ' . common::searchExtendHand_KLN("ocean", $_SESSION["ONLINE_USER"]);
  783. //先不考虑总数,处理有难度
  784. foreach($sqlArr as $_sql){
  785. if(empty($_sql)){
  786. continue;
  787. }
  788. //根据public.kln_ocean 和 WHERE 的位置关系,带入权限
  789. $new_sql = utils::modifyString($_sql,$sqlWhere);
  790. //处理limit 超过10 先限制10
  791. //$new_sql = utils::processLimitClause($new_sql);
  792. //$rs = common::excuteListSql($new_sql);
  793. $rs = $mapdb->GetAll($new_sql) or ( (!$mapdb->ErrorMsg()) or error_log(common::dbLog($mapdb, $new_sql), 0));
  794. $reference = utils::replacementsMultiline($rs,$reference,$new_sql);
  795. }
  796. //有就去掉{{#each hbol_list}}\n {{/each}}
  797. $reference = preg_replace([
  798. '/\{\{[^}]+\}\}\n/', // 匹配开始标签及换行
  799. '/\{\{\/[^}]+\}\}/' // 匹配结束标签
  800. ], '', $reference);
  801. $answer = $reference;
  802. }else{
  803. $answer = $message["response"];
  804. }
  805. //update 更新
  806. $updateSql = "update public.kln_robot_chat_log set answer_duration = '$response_duration',answer_template='$answer_template',answer = '".common::check_input($answer)."',
  807. answer_type = case when COALESCE(answer_type,'') = 'Suspend' then 'Suspend' else '$answer_type' end,
  808. request_content='$request_content',
  809. input_token='$input_token',ai_response_content='$ai_response_content',
  810. output_token='$output_token',response_time=now(),response_duration='$response_duration'
  811. where serial_no = '$serial_no'";
  812. $updateRs = common::excuteUpdateSql($updateSql);
  813. if (!$updateRs) {
  814. $data = array("type"=>"markdown","data" => "AI Chat Response Save Error");
  815. common::echo_json_encode(200,$data);
  816. exit();
  817. }
  818. return $answer;
  819. }
  820. public function doFixedAnswerAndLog($serial_no,$fixed_faq,$question_content){
  821. $input_token = "";
  822. $output_token = "";
  823. $answer_template = "";
  824. $answer ="";
  825. $fixedChat = common::excuteObjectSql("select * from public.kln_robot_chat_fixed where fixed_faq = '$fixed_faq'");
  826. if (empty($fixedChat['secondary_interaction_content']) || $question_content != $fixedChat['fixed_faq'] ){
  827. $answer_template = common::check_input($fixedChat["answer_style"]);
  828. $answer = common::FixedAnswerAndLogData($fixedChat,$question_content);
  829. } else {
  830. $answer = $fixedChat['secondary_interaction_content'];
  831. }
  832. //回答完毕计时
  833. $response_pram = common::excuteObjectSql("select to_char(now(), 'YYYY-mm-dd HH24:MI:SS') as response_time,
  834. CEIL(EXTRACT(EPOCH FROM (now() - question_date))) AS response_duration from public.kln_robot_chat_log where serial_no = '$serial_no'");
  835. $response_duration = $response_pram['response_duration'];
  836. //回答问题类型,在没有超时的情况下,固定:AI回答
  837. $answer_type = "Predefined Template";
  838. if ($response_duration > 120){
  839. $answer_type = "Timeout";
  840. }
  841. $request_content = common::check_input($question_content);
  842. $ai_response_content = common::check_input($answer);
  843. //update 更新
  844. $updateSql = "update public.kln_robot_chat_log set answer_duration = '$response_duration',answer_template='$answer_template',answer = '".common::check_input($answer)."',
  845. answer_type = case when COALESCE(answer_type,'') = 'Suspend' then 'Suspend' else '$answer_type' end,
  846. request_content='$request_content',
  847. input_token='$input_token',ai_response_content='$ai_response_content',
  848. output_token='$output_token',response_time=now(),response_duration='$response_duration'
  849. where serial_no = '$serial_no'";
  850. $updateRs = common::excuteUpdateSql($updateSql);
  851. if (!$updateRs) {
  852. $data = array("type"=>"markdown","data" => "AI Chat Response Save Error");
  853. common::echo_json_encode(200,$data);
  854. exit();
  855. }
  856. return $answer;
  857. }
  858. }
  859. ?>