/**
* WS Customer Service
* Zhipu AI Chat API
* WordPress AJAX
* PHP 7.0 compatible
*/
if (!function_exists(‘ws_cs_handle_chat’)) {
add_action(‘wp_ajax_ws_cs_chat’, ‘ws_cs_handle_chat’);
add_action(‘wp_ajax_nopriv_ws_cs_chat’, ‘ws_cs_handle_chat’);
function ws_cs_handle_chat()
{
// 验证请求
if (
!isset($_POST[‘nonce’]) ||
!wp_verify_nonce(
sanitize_text_field(wp_unslash($_POST[‘nonce’])),
‘ws_cs_chat_nonce’
)
) {
wp_send_json_error(array(
‘message’ => ‘请求验证失败,请刷新页面后重试。’
));
}
// 检查 API Key
if (
!defined(‘ZHIPU_API_KEY’) ||
ZHIPU_API_KEY === ”
) {
wp_send_json_error(array(
‘message’ => ‘智能客服尚未配置 API Key。’
));
}
// 模型
$model = defined(‘ZHIPU_MODEL’)
? ZHIPU_MODEL
: ‘glm-4-flash’;
// 获取聊天记录
$history_raw = isset($_POST[‘history’])
? wp_unslash($_POST[‘history’])
: ”;
$history = json_decode($history_raw, true);
if (!is_array($history)) {
wp_send_json_error(array(
‘message’ => ‘聊天内容格式错误。’
));
}
// 系统指令
$messages = array();
$messages[] = array(
‘role’ => ‘system’,
‘content’ =>
‘你是网站的智能客服,请使用中文与访客交流。’
. ‘网站主要提供人工智能技术研发、智能软件系统开发、’
. ‘多模态信息处理、智能互联网及智能设备系统研发等相关服务。’
. ‘请根据访客问题准确、清晰、礼貌地回答。’
. ‘不要编造企业信息、价格、技术能力或服务承诺。’
. ‘涉及具体报价、项目周期和合同条件时,’
. ‘请建议访客通过免费申请表单或人工联系方式进一步沟通。’
. ‘不确定的信息必须明确说明。’
);
// 整理有效聊天记录
$valid_count = 0;
foreach ($history as $item) {
if (!is_array($item)) {
continue;
}
if (
!isset($item[‘role’]) ||
!isset($item[‘content’])
) {
continue;
}
$role = $item[‘role’];
if (
$role !== ‘user’ &&
$role !== ‘assistant’
) {
continue;
}
$content = sanitize_textarea_field(
$item[‘content’]
);
if ($content === ”) {
continue;
}
if (function_exists(‘mb_strlen’)) {
if (mb_strlen($content, ‘UTF-8’) > 2000) {
continue;
}
} elseif (strlen($content) > 8000) {
continue;
}
$messages[] = array(
‘role’ => $role,
‘content’ => $content
);
$valid_count++;
}
if ($valid_count === 0) {
wp_send_json_error(array(
‘message’ => ‘请输入咨询内容。’
));
}
// 只保留最近12条对话消息,另保留系统指令
if (count($messages) > 13) {
$system_message = array_shift($messages);
$messages = array_slice($messages, -12);
array_unshift(
$messages,
$system_message
);
}
// 智谱 API 地址
$api_url =
‘https://open.bigmodel.cn/api/paas/v4/chat/completions’;
// 请求数据
$request_body = array(
‘model’ => $model,
‘messages’ => $messages,
‘temperature’ => 0.7,
‘max_tokens’ => 1024
);
// 请求智谱 API
$response = wp_remote_post(
$api_url,
array(
‘timeout’ => 45,
‘headers’ => array(
‘Authorization’ =>
‘Bearer ‘ . ZHIPU_API_KEY,
‘Content-Type’ =>
‘application/json’
),
‘body’ => wp_json_encode($request_body)
)
);
if (is_wp_error($response)) {
wp_send_json_error(array(
‘message’ => ‘暂时无法连接智能客服,请稍后再试。’
));
}
// 获取响应
$status_code = wp_remote_retrieve_response_code(
$response
);
$response_body = wp_remote_retrieve_body(
$response
);
$result = json_decode(
$response_body,
true
);
if (
$status_code < 200 ||
$status_code >= 300
) {
wp_send_json_error(array(
‘message’ =>
‘智谱 API 请求失败,请检查 API Key、模型权限或账户额度。’,
‘status’ => $status_code
));
}
// 检查回复
if (
!is_array($result) ||
!isset($result[‘choices’][0][‘message’][‘content’])
) {
wp_send_json_error(array(
‘message’ => ‘智能客服暂时没有返回有效内容。’
));
}
$reply = trim(
$result[‘choices’][0][‘message’][‘content’]
);
if ($reply === ”) {
wp_send_json_error(array(
‘message’ => ‘智能客服暂时没有返回有效内容。’
));
}
// 返回前端
wp_send_json_success(array(
‘reply’ => $reply
));
}
}