以下为输出截图:
需要在同路径中添加一个名为conversations的文件夹
原本输出为英文,对接翻译接口
沐晨API翻译:https://mcapi.muwl.xyz/api/fanyi2.php,可以自己改成别的,不过改了输出的格式不一样,代码也需要变动
沐晨API:沐晨免费稳定API,沐晨收录站,欢迎前来申请
代码
// 定义保存对话历史的文件夹
$folder = 'conversations'; // 文件夹名称
// 如果文件夹不存在,则创建文件夹
if (!is_dir($folder)) {
mkdir($folder, 0755, true); // 创建文件夹,权限设置为 0755,递归创建父文件夹
}
// 从文件中读取对话历史
function loadConversation($filePath) {
if (file_exists($filePath)) {
$data = file_get_contents($filePath);
return json_decode($data, true); // 解析 JSON 数据并返回为数组
}
return []; // 如果文件不存在,返回一个空数组
}
// 将对话历史保存到文件
function saveConversation($filePath, $conversation) {
file_put_contents($filePath, json_encode($conversation, JSON_PRETTY_PRINT)); // 保存对话历史到 JSON 文件
}
// 从 GPT 接口获取响应
function getGptResponse($messages) {
$url = 'https://api.alcex.cn/API/gpt-4/'; // GPT 接口的 URL
$query = http_build_query(['messages' => json_encode($messages)]); // 构建 GET 请求的查询字符串
$ch = curl_init(); // 初始化 CURL 会话
curl_setopt($ch, CURLOPT_URL, $url . '?' . $query); // 设置请求的 URL
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); // 设置返回结果为字符串
curl_setopt($ch, CURLOPT_HTTPGET, true); // 设置请求方法为 GET
$response = curl_exec($ch); // 执行 CURL 请求
if ($response === false) {
die('CURL 请求失败: ' . curl_error($ch)); // CURL 请求失败时显示错误信息
}
curl_close($ch); // 关闭 CURL 会话
$decodedResponse = json_decode($response, true);
if (json_last_error() !== JSON_ERROR_NONE) {
die('无法解析 JSON 响应: ' . json_last_error_msg()); // JSON 解析失败时显示错误信息
}
return $decodedResponse; // 解析 JSON 响应并返回为数组
}
// 检查并限制对话记录数量
function limitConversationSize(&$conversation, $maxSize = 50) {
if (count($conversation) > $maxSize) {
$conversation = array_slice($conversation, -$maxSize); // 保留最后 $maxSize 条记录
}
}
// 主处理逻辑
if ($_SERVER['REQUEST_METHOD'] === 'GET' && isset($_GET['msg']) && isset($_GET['id'])) {
$msg = $_GET['msg']; // 获取 GET 请求中的 msg 参数
$userId = $_GET['id']; // 获取 GET 请求中的 id 参数
$conversationFile = $folder . '/' . $userId . '.json'; // 保存对话历史的文件名
// 读取现有对话历史
$conversation = loadConversation($conversationFile);
// 添加用户消息到对话历史
$conversation[] = ['role' => 'user', 'content' => $msg];
// 获取 GPT 响应
$gptResponse = getGptResponse($conversation);
// 提取 GPT 的响应内容
$content = $gptResponse['choices'][0]['message']['content'] ?? '未找到内容'; // 提取内容,如果没有则返回默认消息
// 将 GPT 的响应添加到对话历史
$conversation[] = ['role' => 'assistant', 'content' => $content];
// 限制对话历史记录数量
limitConversationSize($conversation);
// 保存更新后的对话历史到文件
saveConversation($conversationFile, $conversation);
// 将输出对接沐晨API翻译接口进行翻译中文
$output = $content;
$url = 'https://mcapi.muwl.xyz/api/fanyi2.php?msg=' . urlencode($output);
$ch = curl_init($url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
$output = curl_exec($ch); // 将返回的结果赋给 $output
curl_close($ch);
//echo $output;
$marker = "结果:";//取结果的文本
$position = strpos($output, $marker);
if ($position === FALSE) {
die("Marker not found in the TXT content");
}
// 计算标记的长度
$markerLength = strlen($marker);
// 获取标记后的文本
$result = substr($output, $position + $markerLength);
// 输出结果
echo $result;
} else {
echo '无效的请求。请提供 "msg" 和 "id" 参数。'; // 如果没有提供 msg 或 id 参数,输出提示信息
}