|
|
@@ -187,38 +187,75 @@ class robot{
|
|
|
}
|
|
|
|
|
|
if($operate == "test_with_ds_claude"){
|
|
|
-
|
|
|
- $request_time = date("Y-m-d H:i:s");
|
|
|
$ai_method = $_POST["test_ai_method"];
|
|
|
$systemPrompt = $_POST["prompt"];
|
|
|
$message = $_POST["test_question"];
|
|
|
$model = $ai_method == 'DS' ? "deepseek" : "claude";
|
|
|
+ //question_date request_time
|
|
|
+ $request_question_time = date("Y-m-d H:i:s");
|
|
|
+ $question_time = date("H:i:s");
|
|
|
|
|
|
$item_value = common::excuteOneSql("select item_value from public.config where item = 'AIAPISetting'");
|
|
|
$config = json_decode($item_value,true);
|
|
|
$response = AIClientFactory::create($model, $config[$model], $systemPrompt, $message, $history = []);
|
|
|
$response_time = date("Y-m-d H:i:s");
|
|
|
|
|
|
- $date1 = new DateTime($request_time);
|
|
|
+ $input_token = "";
|
|
|
+ $output_token = "";
|
|
|
+ $date1 = new DateTime($request_question_time);
|
|
|
$date2 = new DateTime($response_time);
|
|
|
$response_duration = $date2->getTimestamp() - $date1->getTimestamp();
|
|
|
- $input_token = "";
|
|
|
- $output_token ="";
|
|
|
|
|
|
- //记录AI API log
|
|
|
- $sequence = common::excuteOneSql("select count(*) from kln_robot_ai_api_log where request_time >= CURRENT_DATE");
|
|
|
- $sequence = empty($sequence)? "1" : $sequence;
|
|
|
- $sequence = sprintf("%05d", $sequence);
|
|
|
-
|
|
|
- $question_id = "Q"."".date("YYYYMMDD")."0001";
|
|
|
- common::aiApiLogRecord($sequence,$question_id,$model,$message,$input_token,json_encode($response),
|
|
|
- $output_token,$request_time,$response_time,$response_duration);
|
|
|
+ //回答问题类型,在没有超时的情况下,固定:AI回答
|
|
|
+ $answer_type = "AI回答";
|
|
|
+ if ($response_duration > 120){
|
|
|
+ $answer_type = "回答超时";
|
|
|
+ }
|
|
|
+ //获取自然序列
|
|
|
+ $sequence = common::getChatAiSequence();
|
|
|
+ $name = $ai_method == 'DS' ? "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";
|
|
|
+ //这里的测试,固定写自由文本
|
|
|
+ $question_type = 'free text';
|
|
|
+ $question_id = common::check_input($question_id);
|
|
|
+ $question_content = common::check_input($message);
|
|
|
+ $answer_type = common::check_input($answer_type);
|
|
|
+ //记录回答展示给客户的具体内容
|
|
|
+ $answer = common::check_input(common::getChatAimessage($response['message']));
|
|
|
+
|
|
|
+ $ai_model = common::check_input($model);
|
|
|
+ $input_token = common::check_input($input_token);
|
|
|
+ $request_content = common::check_input($response['data']);
|
|
|
+ $ai_response_content = common::check_input($response['full_response']);
|
|
|
+ $output_token = common::check_input($output_token);
|
|
|
+
|
|
|
+ $sql = "INSERT INTO public.kln_robot_chat_log( question_id, user_name, user_type, question_type, question_content,
|
|
|
+ answer_type, answer, answer_satisfication, answer_duration, question_date,
|
|
|
+ question_time, request_id, ai_model, request_content, input_token,
|
|
|
+ ai_response_content, output_token, request_time, response_time,
|
|
|
+ response_duration)
|
|
|
+ VALUES ('$question_id', '$user_name', '$user_type', '$question_type', '$question_content',
|
|
|
+ '$answer_type', '$answer', '', '$response_duration', '$request_question_time',
|
|
|
+ '$question_time', '$request_id', '$ai_model', '$input_token', '$request_content',
|
|
|
+ '$ai_response_content', '$output_token', '$request_question_time', '$response_time',
|
|
|
+ '$response_duration');";
|
|
|
+ $rs = common::excuteUpdateSql($sql);
|
|
|
+ if (!$rs) {
|
|
|
+ $data = array("msg" => "AI Chat Save Error");
|
|
|
+ common::echo_json_encode(200,$data);
|
|
|
+ exit();
|
|
|
+ }
|
|
|
+ error_log($response['message']);
|
|
|
// $sql ="";
|
|
|
// //用户权限
|
|
|
// $sqlWhere = ' and ' . common::searchExtendHand_KLN("ocean", $_SESSION["ONLINE_USER"]);
|
|
|
// $sql .= $sqlWhere;
|
|
|
// $rs = common::excuteListSql($sql);
|
|
|
-
|
|
|
$rs = $response;
|
|
|
common::echo_json_encode(200,$rs);
|
|
|
exit();
|