Skip to content

Commit e0ea15e

Browse files
authored
Create demo.php
1 parent 5424f97 commit e0ea15e

File tree

1 file changed

+116
-0
lines changed

1 file changed

+116
-0
lines changed

trunk/web/aiapi/demo.php

Lines changed: 116 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,116 @@
1+
<?php
2+
// 这个文件用于对接阿里千问,解析编译报错和运行错误信息。
3+
// 需要在db_info.inc.php中配置 $OJ_AI_API_URL指向本文件;
4+
// 登录阿里云,打开 https://bailian.console.aliyun.com/?tab=model#/api-key 创建新的API KEY
5+
// 注意这个功能可能会导致阿里云付费账单,
6+
// 访问类似 https://bailian.console.aliyun.com/?tab=model#/model-market/detail/qwen3-coder-480b-a35b-instruct
7+
// 关注所用模型的剩余免费额度
8+
require_once("../include/db_info.inc.php");
9+
// 设置请求的URL
10+
$url = 'http://demo.hustoj.com/aiapi/proxy.php';
11+
$apiKey = "设置为阿里云的API-KEY"; //https://bailian.console.aliyun.com/?tab=model#/api-key 创建新的API KEY
12+
$models=array("qwen-turbo"); //,"qwen3-coder-480b-a35b-instruct","qwen3-max","qwen3-coder-30b-a3b-instruct"
13+
14+
15+
$sid=intval($_GET['sid']);
16+
$solution=pdo_query("select user_id,problem_id from solution where solution_id=?",$sid)[0];
17+
$user_id=$solution[0];
18+
$problem_id=$solution[1];
19+
20+
if(!(isset($_SESSION[$OJ_NAME."_source_browser"])|| $user_id==$_SESSION[$OJ_NAME."_user_id"] )){
21+
echo "非法参数";
22+
exit();
23+
}
24+
$sql="SELECT `source` FROM `source_code_user` WHERE `solution_id`=?";
25+
$result=pdo_query($sql,$sid);
26+
if(!empty($result)){
27+
$row=$result[0];
28+
$source=$row[0];
29+
}else{
30+
echo "非法参数";
31+
exit();
32+
}
33+
$http_referer = $_SERVER['HTTP_REFERER'];
34+
if(str_starts_with( basename($http_referer),"reinfo"))
35+
$table="runtimeinfo";
36+
else
37+
$table="compileinfo";
38+
39+
$sql="SELECT `error` FROM `$table` WHERE `solution_id`=?";
40+
$result=pdo_query($sql,$sid);
41+
if(!empty($result)){
42+
$row=$result[0];
43+
$ceinfo=$row[0];
44+
}else{
45+
echo "非法参数";
46+
exit();
47+
}
48+
$sql="select answer from solution_ai_answer where solution_id=? ";
49+
$answer=pdo_query($sql,$sid);
50+
if(!empty($answer)){
51+
echo htmlentities($answer[0][0]);
52+
echo "<!-- cached answer -->";
53+
exit();
54+
}
55+
$problem=pdo_query("select concat(description,'输入:',input,'输出:',output,'样例输入:',sample_input,'样例输出:',sample_output,'提示:',hint) from problem where problem_id=?",$problem_id)[0][0];
56+
57+
58+
59+
// 设置请求头
60+
$headers = [
61+
'Authorization: Bearer '.$apiKey,
62+
'Content-Type: application/json'
63+
];
64+
if(isset($_SESSION[$OJ_NAME."_source_browser"])){
65+
$code_suggestion="分析我可能薄弱的知识点,问我一个提示性的相关问题。";
66+
}else{
67+
$code_suggestion="不要直接给出完整代码,只给出问题原因,让我自己学习修改。分析我可能薄弱的知识点,问我一个提示性的相关问题,最后说一句鼓励或安慰的话,卖个萌。";
68+
}
69+
70+
$model = $models[array_rand($models)];
71+
// 设置请求体
72+
$data = [
73+
// 此处以qwen-plus为例,可按需更换模型名称。模型列表:https://help.aliyun.com/zh/model-studio/getting-started/models
74+
"model" => "$model",
75+
"messages" => [
76+
[
77+
"role" => "system",
78+
"content" => "你是一个编程高手,能帮我用简单清晰的中文,解释我看不懂的报错信息。如果对比中用户的输出为空,可能是没有考虑到多组输入的情况,应该使用循环处理。$code_suggestion 请尽量言简意赅,节省token消耗。"
79+
],
80+
[
81+
"role" => "user",
82+
"content" => "题目是:".$problem."\n 源代码是:".$source."\n报错信息是:".$ceinfo
83+
]
84+
]
85+
];
86+
// 初始化cURL会话
87+
$ch = curl_init();
88+
// 设置cURL选项
89+
if(empty($domain)) $domain=$http_referer;
90+
curl_setopt($ch, CURLOPT_URL, $url);
91+
curl_setopt($ch, CURLOPT_POST, true);
92+
curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode($data));
93+
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
94+
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
95+
curl_setopt($ch, CURLOPT_REFERER, $domain );
96+
curl_setopt($ch, CURLOPT_TIMEOUT, 60);
97+
// 执行cURL会话
98+
$response = curl_exec($ch);
99+
// 检查是否有错误发生
100+
if (curl_errno($ch)) {
101+
echo 'Curl error: ' . curl_error($ch);
102+
exit(); // 超时等错误发生时,不将结果入库,下次还能重试。
103+
}
104+
// 关闭cURL资源
105+
curl_close($ch);
106+
// 输出响应结果
107+
$data=json_decode($response);
108+
109+
$answer=$data->choices[0]->message->content."<br> --- $model <br><a href='https://github.com/zhblue/hustoj/' target=_blank > 如果你觉得这个系统对你有帮助,请到Github来给我们加个
110+
Star⭐吧 </a> ";
111+
112+
echo htmlentities($answer);
113+
$sql="insert into solution_ai_answer (solution_id,answer) values(?,?)";
114+
pdo_query($sql,$sid,$answer);
115+
116+
?>

0 commit comments

Comments
 (0)