|
|
@@ -583,60 +583,66 @@ class robot{
|
|
|
* ai 自由聊天
|
|
|
*/
|
|
|
if($operate == "ai_chat"){
|
|
|
- //前端生成一个唯一的serial_no
|
|
|
- $serial_no = common::check_input($_POST["serial_no"]);
|
|
|
- //系统提示词
|
|
|
- $systemPrompt = common::check_input($_POST["prompt"]);
|
|
|
- $question_type = common::check_input($_POST['question_type']);
|
|
|
- $question_type = $question_type == 'Free Question' ? "Free Text" : $question_type;
|
|
|
- $question_content = common::check_input($_POST['question_content']);
|
|
|
-
|
|
|
- //固定问题的原始问题
|
|
|
- $fixed_faq = common::check_input($_POST['fixed_faq']);
|
|
|
- //claude deepseek 根據賬號信息判断
|
|
|
- $model = common::getUserCountry();
|
|
|
- //$model = "deepseek";
|
|
|
+ try {
|
|
|
+ //前端生成一个唯一的serial_no
|
|
|
+ $serial_no = common::check_input($_POST["serial_no"]);
|
|
|
+ //系统提示词
|
|
|
+ $systemPrompt = common::check_input($_POST["prompt"]);
|
|
|
+ $question_type = common::check_input($_POST['question_type']);
|
|
|
+ $question_type = $question_type == 'Free Question' ? "Free Text" : $question_type;
|
|
|
+ $question_content = common::check_input($_POST['question_content']);
|
|
|
+
|
|
|
+ //固定问题的原始问题
|
|
|
+ $fixed_faq = common::check_input($_POST['fixed_faq']);
|
|
|
+ //claude deepseek 根據賬號信息判断
|
|
|
+ $model = common::getUserCountry();
|
|
|
+ //$model = "deepseek";
|
|
|
+
|
|
|
+ //获取自然序列
|
|
|
+ $sequence = common::getChatAiSequence();
|
|
|
+ $name = $model == 'deepseek' ? "DS" : "CD";
|
|
|
+ $request_id = "R".$name."".date("Ymd").$sequence;
|
|
|
+ $question_id = "Q"."".date("Ymd").$sequence;
|
|
|
+
|
|
|
+ //处理一下参数
|
|
|
+ $user_name = common::check_input(_getLoginName());
|
|
|
+ $user_type = _isApexLogin() ? "employee" : "customer";
|
|
|
+
|
|
|
+ //首先生成基本记录数据 以便以后根据情况更新状态
|
|
|
+ $sql = "INSERT INTO public.kln_robot_chat_log(serial_no, question_id, user_name, user_type, question_type, question_content,
|
|
|
+ question_date, question_time, request_id, request_time,ai_model)
|
|
|
+ VALUES ('$serial_no','$question_id', '$user_name', '$user_type', '$question_type', '$question_content',
|
|
|
+ now(),to_char(CURRENT_TIME::time, 'HH24:MI:SS'), '$request_id',now(),'$model');";
|
|
|
+ $rs = common::excuteUpdateSql($sql);
|
|
|
+ if (!$rs) {
|
|
|
+ $data = array("type"=>"markdown","data" => "AI Chat Save Error");
|
|
|
+ common::echo_json_encode(200,$data);
|
|
|
+ exit();
|
|
|
+ }
|
|
|
|
|
|
- //获取自然序列
|
|
|
- $sequence = common::getChatAiSequence();
|
|
|
- $name = $model == 'deepseek' ? "DS" : "CD";
|
|
|
- $request_id = "R".$name."".date("Ymd").$sequence;
|
|
|
- $question_id = "Q"."".date("Ymd").$sequence;
|
|
|
+ if ($question_type == 'Predefined Question'){
|
|
|
+ $answer = $this->doFixedAnswerAndLog($serial_no,$fixed_faq,$question_content);
|
|
|
+
|
|
|
+ //判断固定回答是否结束
|
|
|
+ $is_fixedAnswer_end = false;
|
|
|
+ $fixedChat = common::excuteObjectSql("select * from public.kln_robot_chat_fixed where fixed_faq = '$fixed_faq'");
|
|
|
+ if (empty($fixedChat['secondary_interaction_content']) || $question_content != $fixedChat['fixed_faq'] ){
|
|
|
+ $is_fixedAnswer_end = true;
|
|
|
+ }
|
|
|
+ } else {
|
|
|
+ $answer = $this->doFreeAnswerAndLog($serial_no,$model,$systemPrompt, $question_content, $history = []);
|
|
|
+ //自由问题固定为true
|
|
|
+ $is_fixedAnswer_end = true;
|
|
|
+ }
|
|
|
|
|
|
- //处理一下参数
|
|
|
- $user_name = common::check_input(_getLoginName());
|
|
|
- $user_type = _isApexLogin() ? "employee" : "customer";
|
|
|
-
|
|
|
- //首先生成基本记录数据 以便以后根据情况更新状态
|
|
|
- $sql = "INSERT INTO public.kln_robot_chat_log(serial_no, question_id, user_name, user_type, question_type, question_content,
|
|
|
- question_date, question_time, request_id, request_time,ai_model)
|
|
|
- VALUES ('$serial_no','$question_id', '$user_name', '$user_type', '$question_type', '$question_content',
|
|
|
- now(),to_char(CURRENT_TIME::time, 'HH24:MI:SS'), '$request_id',now(),'$model');";
|
|
|
- $rs = common::excuteUpdateSql($sql);
|
|
|
- if (!$rs) {
|
|
|
- $data = array("type"=>"markdown","data" => "AI Chat Save Error");
|
|
|
- common::echo_json_encode(200,$data);
|
|
|
+ $return = array("type"=>"markdown","is_fixedAnswer_end"=>$is_fixedAnswer_end,"data" =>$answer);
|
|
|
+ common::echo_json_encode(200,$return);
|
|
|
+ exit();
|
|
|
+ } catch (Exception $e) {
|
|
|
+ $return = array("type"=>"markdown","is_fixedAnswer_end"=>true,"data" =>$e->getMessage());
|
|
|
+ common::echo_json_encode(200,$return);
|
|
|
exit();
|
|
|
}
|
|
|
-
|
|
|
- if ($question_type == 'Predefined Question'){
|
|
|
- $answer = $this->doFixedAnswerAndLog($serial_no,$fixed_faq,$question_content);
|
|
|
-
|
|
|
- //判断固定回答是否结束
|
|
|
- $is_fixedAnswer_end = false;
|
|
|
- $fixedChat = common::excuteObjectSql("select * from public.kln_robot_chat_fixed where fixed_faq = '$fixed_faq'");
|
|
|
- if (empty($fixedChat['secondary_interaction_content']) || $question_content != $fixedChat['fixed_faq'] ){
|
|
|
- $is_fixedAnswer_end = true;
|
|
|
- }
|
|
|
- } else {
|
|
|
- $answer = $this->doFreeAnswerAndLog($serial_no,$model,$systemPrompt, $question_content, $history = []);
|
|
|
- //自由问题固定为true
|
|
|
- $is_fixedAnswer_end = true;
|
|
|
- }
|
|
|
-
|
|
|
- $return = array("type"=>"markdown","is_fixedAnswer_end"=>$is_fixedAnswer_end,"data" =>$answer);
|
|
|
- common::echo_json_encode(200,$return);
|
|
|
- exit();
|
|
|
}
|
|
|
|
|
|
if($operate == "ai_chat_stop"){
|