| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361 |
- <?php
- if (!defined('IN_ONLINE')) {
- exit('Access Denied');
- }
- /**
- * Description of robot
- *
- * @author Administrator
- */
- class robot{
- private static $_robot;
- function __construct() {
-
- }
- public static function getInstance() {
- global $memory_limit;
- $memory_limit = ini_get("memory_limit");
- ini_set("memory_limit", '2048M');
- if (!self::$_robot) {
- $c = __CLASS__;
- self::$_robot = new $c;
- }
- return self::$_robot;
- }
- public function robot_prompt_configuration() {
- $operate = utils::_get('operate');
- $operate = strtolower($operate);
- if (empty($operate)) {
- //查询所有配置信息 目前只有一个表一个数据
- $configuration = common::excuteObjectSql("select id, character_name,professional_field,main_tasks,table_name,table_description,
- table_structure_configuration,response_rule_configuration,output_format_configuration,fixed_problem_configuration
- from public.kln_robot_prompt_configuration where id = 1");
- $PromptAndVue = $this->dealPromptFormatAndVue($configuration);
- //tableDat-VUE
- $tableData = $PromptAndVue["vuedata"]["tableData"];
- //stepData -VUE
- $stepData = $PromptAndVue["vuedata"]["stepData"];
- //formatList -VUE
- $formatListType = $PromptAndVue["vuedata"]["formatListType"];
- $formatList = $PromptAndVue["vuedata"]["formatList"];
- //promptFormat
- $promptFormat =$PromptAndVue["promptFormat"];
- //获取历史变更记录
- $old_configuration = $this->getChangedLog($configuration["id"]);
- $id = $configuration['id'];
- $character_name = $configuration['character_name'];
- $professional_field = $configuration['professional_field'];
- $main_tasks = $configuration['main_tasks'];
- $table_name = $configuration['table_name'];
- $table_description = $configuration['table_description'];
- $prompt_summary = array("您是专門从事".$professional_field."的".$character_name."...",
- "表名:{".$table_name."}...",
- "响应规则,请按以下步骤处理...");
- $data = array(
- "id"=>$id,
- "character_name"=>$character_name,
- "professional_field"=>$professional_field,
- "main_tasks"=>$main_tasks,
- "table_name"=>$table_name,
- "table_description"=>$table_description,
- "tableData"=>$tableData,
- "stepData"=>$stepData,
- "formatList"=>$formatList,
- "formatListType"=>$formatListType,
- "complete_prompt"=>$promptFormat,
- "prompt_summary"=>$prompt_summary,
- "prompt_log_record"=>$old_configuration
- );
- $data = utils::arrayRemoveNull($data);
- common::echo_json_encode(200,$data);
- exit();
- }
- if ($operate == "save") {
- $id =common::check_input($_POST['id']);
- $character_name = common::check_input($_POST['role_name']);
- $professional_field = common::check_input($_POST['professional_field']);
- $main_tasks = common::check_input($_POST['main_tasks']);
- $table_name = common::check_input($_POST['table_name']);
- $table_description = common::check_input($_POST['table_description']);
- $outputvalue = common::check_input($_POST['outputvalue']);
- $tableDataList = $_POST['tableDataList'];
- $stepData = $_POST['stepData'];
- $formatList = $_POST['formatList'];
- $table_structure_configuration = common::check_input(json_encode($tableDataList));
- $response_rule_configuration = common::check_input(json_encode($stepData));
- $output_data = array("output_type"=>$outputvalue,"data"=>$formatList);
- $output_format_configuration = common::check_input(json_encode($output_data));
- if(empty($id)){
- $sql = "INSERT INTO public.kln_robot_prompt_configuration(
- character_name, professional_field, main_tasks, table_name,
- table_description, table_structure_configuration, response_rule_configuration,
- output_format_configuration, created_by, created_time)
- VALUES ('$character_name', '$professional_field', '$main_tasks', '$table_name',
- '$table_description', '$table_structure_configuration', '$response_rule_configuration',
- '$output_format_configuration', '"._getLoginName()."', now());";
- }else{
- $sql = "update public.kln_robot_prompt_configuration
- set character_name = '$character_name',professional_field ='$professional_field', main_tasks = '$main_tasks',
- table_name = '$table_name',table_description = '$table_description',
- table_structure_configuration = '$table_structure_configuration',
- response_rule_configuration = '$response_rule_configuration',
- output_format_configuration = '$output_format_configuration'
- where id = '$id';";
- //插入变更日志记录
- $sql .= "INSERT INTO public.kln_robot_prompt_configuration_log(ref_id, character_name, professional_field, main_tasks, table_name,
- table_description, table_structure_configuration, response_rule_configuration,
- output_format_configuration, created_by, created_time)
- VALUES ('$id','$character_name', '$professional_field', '$main_tasks', '$table_name',
- '$table_description', '$table_structure_configuration', '$response_rule_configuration',
- '$output_format_configuration', '"._getLoginName()."', now());";
- }
- error_log($sql);
- $rs = common::excuteUpdateSql($sql);
- if (!$rs) {
- $data = array("msg" => "save Error");
- } else {
- $data = array("msg" => "save Successful");
- }
- $data = array("msg" => "save Successful");
- common::echo_json_encode(200,$data);
- exit();
- }
- if($operate == "preview_propmpt_witout_save"){
- $character_name = common::check_input($_POST['role_name']);
- $professional_field = common::check_input($_POST['professional_field']);
- $main_tasks = common::check_input($_POST['main_tasks']);
- $table_name = common::check_input($_POST['table_name']);
- $table_description = common::check_input($_POST['table_description']);
- $outputvalue = common::check_input($_POST['outputvalue']);
- $tableDataList = $_POST['tableDataList'];
- $stepData = $_POST['stepData'];
- $formatList = $_POST['formatList'];
- $table_structure_configuration = common::check_input(json_encode($tableDataList));
- $response_rule_configuration = common::check_input(json_encode($stepData));
- $output_data = array("output_type"=>$outputvalue,"data"=>$formatList);
- $output_format_configuration = common::check_input(json_encode($output_data));
- //组合拼接需要的数据
- $configuration = array("character_name"=>$character_name,
- "professional_field"=>$professional_field,
- "main_tasks"=>$main_tasks,
- "table_name"=>$table_name,
- "table_description"=>$table_description,
- "professional_field"=>$professional_field,
- "table_structure_configuration"=>$table_structure_configuration,
- "response_rule_configuration" =>$response_rule_configuration,
- "output_format_configuration" =>$output_format_configuration
- );
- $PromptAndVue = $this->dealPromptFormatAndVue($configuration);
- //promptFormat
- $promptFormat =$PromptAndVue["promptFormat"];
- $character_name = empty($configuration['character_name']) ? "" :$configuration['character_name'];
- $professional_field = empty($configuration['professional_field']) ? "" :$configuration['professional_field'];
- $main_tasks = empty($configuration['main_tasks']) ? "" :$configuration['main_tasks'];
- $table_name = empty($configuration['table_name']) ? "" :$configuration['table_name'];
- $table_description = empty($configuration['table_description']) ? "" :$configuration['table_description'];
- $prompt_summary = array("您是专門从事".$professional_field."的".$character_name."...",
- "表名:{".$table_name."}...",
- "响应规则,请按以下步骤处理...");
- $data = array(
- "complete_prompt"=>$promptFormat,
- "prompt_summary"=>$prompt_summary
- );
- common::echo_json_encode(200,$data);
- exit();
- }
- if($operate == "test_with_ds_claude"){
- $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");
- $input_token = "";
- $output_token = "";
- $date1 = new DateTime($request_question_time);
- $date2 = new DateTime($response_time);
- $response_duration = $date2->getTimestamp() - $date1->getTimestamp();
- //回答问题类型,在没有超时的情况下,固定: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();
- }
- }
- public function dealPromptFormatAndVue($configuration){
- //tableDat-VUE
- $tableData = array();
- $table_structure_text="[";
- $table_structure_configuration = json_decode($configuration['table_structure_configuration'],true);
- foreach($table_structure_configuration as $tk =>$tv){
- $tableData[] = $tv;
- $table_structure_text .='{
- "name": "'.$tv["name"].'",
- "type": "'.$tv["type"].'",
- "description": "'.$tv["description"].'",
- "exampledata": "'.$tv["exampledata"].'",
- }';
- if(count($table_structure_configuration)-1 > $tk){
- $table_structure_text .=",";
- }
- $table_structure_text .=common::splicedLlineBreaks();
- }
- $table_structure_text.="]";
- //stepData -VUE
- $stepData = array();
- $response_rule_text = "";
- $response_rule_configuration = json_decode($configuration['response_rule_configuration'],true);
- foreach($response_rule_configuration as $rk =>$rv){
- $tempTable = array();
- $tempTable["description"] = $rv["description"];
- $stepData[] = $tempTable;
- $response_rule_text .=($rk+1).'.'.$rv["description"];
- $response_rule_text .=common::splicedLlineBreaks();
- }
- //formatList -VUE
- $output_format_text = "{";
- $output_format_configuration = json_decode($configuration['output_format_configuration'],true);
- $formatListType = $output_format_configuration['output_type'];
- $formatList = empty($output_format_configuration['data']) ? array(): $output_format_configuration['data'];
- foreach($formatList as $ok =>$ov){
- $output_format_text .="\"".$ov['name']."\"".": "."\"".$ov['describe']."\"";
- $output_format_text .=common::splicedLlineBreaks();
- }
- $output_format_text .= "}";
- $promptFormat = $this->getCompletePromptFormat();
- $promptFormat = str_replace('<{professional_field}>', $configuration['character_name'], $promptFormat);
- $promptFormat = str_replace('<{character_name}>', $configuration['character_name'], $promptFormat);
- $promptFormat = str_replace('<{main_tasks}>', $configuration['main_tasks'], $promptFormat);
- $promptFormat = str_replace('<{table}>', $configuration['table_name'], $promptFormat);
- $promptFormat = str_replace('<{table_structure_configuration}>', $table_structure_text, $promptFormat);
- $promptFormat = str_replace('<{response_rule_configuration}>', $response_rule_text, $promptFormat);
- $promptFormat = str_replace('<{number}>', count($response_rule_configuration)+1, $promptFormat);
- $promptFormat = str_replace('<{output_type}>', $formatListType, $promptFormat);
- $promptFormat = str_replace('<{output_format_configuration}>', $output_format_text, $promptFormat);
- return array("vuedata"=>array("tableData"=>$tableData,"stepData"=>$stepData,"formatList"=>$formatList,"formatListType"=>$formatListType),
- "promptFormat" =>$promptFormat);
- }
- public function getChangedLog($id){
- $changeLogList = array();
- $configurationArr = common::excuteListSql("select id, character_name,professional_field,main_tasks,table_name,table_description,
- created_by,TO_CHAR(created_time, 'YYYY-mm-dd HH24:MI:SS') as created_time,
- table_structure_configuration,response_rule_configuration,output_format_configuration,fixed_problem_configuration
- from public.kln_robot_prompt_configuration_log where ref_id = '$id' order by id desc");
- foreach($configurationArr as $ca){
- $data = $this->dealPromptFormatAndVue($ca);
- $temp =array();
- $temp["time"] = $ca['created_time']." Prompt";
- $temp["person"] ="提交人:".$ca['created_by'];
- $temp["text"] = $data['promptFormat'];
- $changeLogList[] = $temp;
- }
- return $changeLogList;
- }
- public function getCompletePromptFormat() {
- $formatTpl = "
- 您是专门从<{professional_field}>的 <{character_name}>,请始终以<{output_type}>格式响应。您的主要任务是<{main_tasks}>
- 表名:<{table}>
- 表结构: \"columns\": <{table_structure_configuration}>
-
- 响应规则,请按以下步骤处理:
- <{response_rule_configuration}>
- <{number}>. 请用以下<{output_type}>格式回复:
- <{output_format_configuration}>";
- return $formatTpl;
- }
- }
- ?>
|