|
| 1 | +<?php |
| 2 | +// 这个文件用于对接腾讯混元大模型,目前免费5路并发。 |
| 3 | +// 需要在 db_info.inc.php 文件中配置 $OJ_AI_API_URL |
| 4 | +// 混元大模型目前提供后付费日结的计费模式,且为每个开通服务的腾讯云账号提供累计100万token的调用额度(以资源包形式发放); |
| 5 | +// 在账单结算时,系统将按照“免费资源包 > 后付费”的顺序进行结算,即免费资源包是优先扣除的,免费额度内不收取任何费用。后付费在开通服务时默认关闭,需手动开启。 详情请查看购买指南。 |
| 6 | +// 登录腾讯云,打开 https://console.cloud.tencent.com/hunyuan/api-key 创建新的API KEY |
| 7 | +// 注意这个功能滥用可能会导致付费账单,请关注混元的计费规则变化 |
| 8 | +// 访问类似 https://console.cloud.tencent.com/hunyuan/packages |
| 9 | +// 关注所用模型的剩余免费额度 |
| 10 | +require_once("../include/db_info.inc.php"); |
| 11 | + |
| 12 | +$apiKey ="你在腾讯云生成的API-KEY填在这里 "; //https://console.cloud.tencent.com/hunyuan/api-key |
| 13 | + |
| 14 | +$sid=intval($_GET['sid']); |
| 15 | +$solution=pdo_query("select user_id,problem_id from solution where solution_id=?",$sid)[0]; |
| 16 | +$user_id=$solution[0]; |
| 17 | +$problem_id=$solution[1]; |
| 18 | + |
| 19 | +if(!(isset($_SESSION[$OJ_NAME."_source_browser"])|| $user_id==$_SESSION[$OJ_NAME."_user_id"] )){ |
| 20 | + echo "非法参数"; |
| 21 | + exit(); |
| 22 | +} |
| 23 | +$sql="SELECT `source` FROM `source_code_user` WHERE `solution_id`=?"; |
| 24 | +$result=pdo_query($sql,$sid); |
| 25 | +if(!empty($result)){ |
| 26 | + $row=$result[0]; |
| 27 | + $source=$row[0]; |
| 28 | +}else{ |
| 29 | + echo "非法参数"; |
| 30 | + exit(); |
| 31 | +} |
| 32 | +$http_referer = $_SERVER['HTTP_REFERER']; |
| 33 | +if(str_starts_with( basename($http_referer),"reinfo")) |
| 34 | + $table="runtimeinfo"; |
| 35 | +else |
| 36 | + $table="compileinfo"; |
| 37 | + |
| 38 | +$sql="SELECT `error` FROM `$table` WHERE `solution_id`=?"; |
| 39 | +$result=pdo_query($sql,$sid); |
| 40 | +if(!empty($result)){ |
| 41 | + $row=$result[0]; |
| 42 | + $ceinfo=$row[0]; |
| 43 | +}else{ |
| 44 | + echo "非法参数"; |
| 45 | + exit(); |
| 46 | +} |
| 47 | +$sql="select answer from solution_ai_answer where solution_id=? "; |
| 48 | +$answer=pdo_query($sql,$sid); |
| 49 | +if(!empty($answer)){ |
| 50 | + $tail= "<br><a href='https://github.com/zhblue/hustoj/' target=_blank > 如果你觉得这个系统对你有帮助,请到Github来给我们加个Star⭐ 吧 </a>"; |
| 51 | + echo htmlentities($answer[0][0].$tail); |
| 52 | + exit(); |
| 53 | +} |
| 54 | +$problem=pdo_query("select concat(description,'输入:',input,'输出:',output,'样例输入:',sample_input,'样例输出:',sample_output,'提示:',hint) from problem where problem_id=?",$problem_id)[0]; |
| 55 | +// 设置请求的URL |
| 56 | +$url = "https://api.hunyuan.cloud.tencent.com/v1/chat/completions"; |
| 57 | +// 设置请求头 |
| 58 | +$headers = [ |
| 59 | + 'Authorization: Bearer '.$apiKey, |
| 60 | + 'Content-Type: application/json' |
| 61 | +]; |
| 62 | +if(isset($_SESSION[$OJ_NAME."_source_browser"])){ |
| 63 | + $code_suggestion="分析我可能薄弱的知识点,问我一个提示性的相关问题。"; |
| 64 | +}else{ |
| 65 | + $code_suggestion="不要直接给出完整代码,只给出问题原因,让我自己学习修改。分析我可能薄弱的知识点,问我一个提示性的相关问题。"; |
| 66 | +} |
| 67 | +$models=array("hunyuan-turbos-latest"); |
| 68 | +$model = $models[array_rand($models)]; |
| 69 | +// 设置请求体 |
| 70 | +$data = [ |
| 71 | + // 此处以qwen-plus为例,可按需更换模型名称。模型列表:https://help.aliyun.com/zh/model-studio/getting-started/models |
| 72 | + "model" => "$model", |
| 73 | + "messages" => [ |
| 74 | + [ |
| 75 | + "role" => "system", |
| 76 | + "content" => "你是一个编程高手,能帮我用简单清晰的中文,解释我看不懂的报错信息。如果对比中用户的输出为空,可能是没有考虑到多组输入的情况,应该使用循环处理。$code_suggestion 请尽量言简意赅,节省token消耗。" |
| 77 | + ], |
| 78 | + [ |
| 79 | + "role" => "user", |
| 80 | + "content" => "题目是: ".$problem[0]." \n源代码是:".$source."\n报错信息是:".$ceinfo |
| 81 | + ] |
| 82 | + ], |
| 83 | + "stream" => false, |
| 84 | + "max_tokens" => 4096 |
| 85 | +]; |
| 86 | +// 初始化cURL会话 |
| 87 | +$ch = curl_init(); |
| 88 | +// 设置cURL选项 |
| 89 | +curl_setopt($ch, CURLOPT_URL, $url); |
| 90 | +curl_setopt($ch, CURLOPT_POST, true); |
| 91 | +curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode($data)); |
| 92 | +curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); |
| 93 | +curl_setopt($ch, CURLOPT_HTTPHEADER, $headers); |
| 94 | +curl_setopt($ch, CURLOPT_TIMEOUT, 30); |
| 95 | +curl_setopt($ch, CURLOPT_PROXY, "127.0.0.1:1080"); |
| 96 | +// 执行cURL会话 |
| 97 | +$response = curl_exec($ch); |
| 98 | +// 检查是否有错误发生 |
| 99 | +if (curl_errno($ch)) { |
| 100 | + echo 'Curl error: ' . curl_error($ch); |
| 101 | + exit(); |
| 102 | +} |
| 103 | +// 关闭cURL资源 |
| 104 | +curl_close($ch); |
| 105 | +// 输出响应结果 |
| 106 | +$data=json_decode($response); |
| 107 | + |
| 108 | +$answer=$data->choices[0]->message->content; |
| 109 | + |
| 110 | +$tail= "<br> --- $model <br><a href='https://github.com/zhblue/hustoj/' target=_blank > 如果你觉得这个系统对你有帮助,请到Github来给我们加个Star⭐ 吧 </a>"; |
| 111 | + |
| 112 | +echo htmlentities($answer.$tail); |
| 113 | +$sql="insert into solution_ai_answer (solution_id,answer) values(?,?)"; |
| 114 | +pdo_query($sql,$sid,$answer); |
| 115 | + |
| 116 | +?> |
0 commit comments