Skip to content

Commit bdd4e71

Browse files
authored
Create slm.php
1 parent 1e2d115 commit bdd4e71

File tree

1 file changed

+110
-0
lines changed

1 file changed

+110
-0
lines changed

trunk/web/aiapi/slm.php

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

0 commit comments

Comments
 (0)