require __DIR__ . "/vendor/autoload.php";
use Volcengine\LLMShield\Api\LLMShieldApi;
use Volcengine\LLMShield\Model;
function load_file_content($ftype, $filePath) {
if (!file_exists($filePath) || !is_readable($filePath)) {
trigger_error("文件不存在或无法读取:{$filePath}", E_USER_ERROR);
$fileHandle = fopen($filePath, 'rb');
trigger_error("无法打开文件:{$filePath}", E_USER_ERROR);
$binaryContent = fread($fileHandle, filesize($filePath));
$base64Content = base64_encode($binaryContent);
$base64Content = "data:text/plain;base64," . $base64Content;
$base64Content = "data:application/msword;base64," . $base64Content;
$base64Content = "data:application/vnd.openxmlformats-officedocument.wordprocessingml.document;base64," . $base64Content;
$base64Content = "data:application/vnd.ms-excel;base64," . $base64Content;
$base64Content = "data:application/vnd.openxmlformats-officedocument.spreadsheetml.sheet;base64," . $base64Content;
$base64Content = "data:application/pdf;base64," . $base64Content;
$myUrl = "https://cn-beijing.sdk.access.llm-shield.omini-shield.com";息
$appid = "app-d2pgblcjslsilv******";取
$region = "cn-beijing";域
$service = "llmshield";称
$client = new LLMShieldApi($myUrl, $ak, $sk, $appid, $region, $timeout, $service, "http://10.37.**.**:8888");值
$request = new Model\ModerateV2Request();
$request->Scene = $appid;
$file_content = load_file_content("docx", "/Users/example/demo.docx");容
echo $file_content . "\n";
$request->Message = new Model\MessageV2();
$request->Message->Role = "user";容
$request->Message->Content = $file_content;情
$request->Message->ContentType = Model\ContentTypeV2::FILE;档
$request->History[] = new Model\MessageV2();
$request->History[0]->Role = "system";词
$request->History[0]->Content = "你是一个专业的问答助手,你的任务是回答用户的问题,注意保持友善和专业。";
$request->History[0]->ContentType = Model\ContentTypeV2::TEXT;
$request->History[] = new Model\MessageV2();
$request->History[1]->Role = "user";词
$request->History[1]->Content = "请给我一份十一出行计划";
$request->History[1]->ContentType = Model\ContentTypeV2::TEXT;
$client->Moderate($request)->then(function ($resp) {
echo json_encode($resp[0], JSON_UNESCAPED_UNICODE) . "\n";
echo json_encode($resp[2], JSON_UNESCAPED_UNICODE) . "\n";