本帖最后由 Pwaerm 于 2024-2-22 16:51 编辑
需求:最近辅导参加一个科技比赛,题库中有近3000道题。先使用了chart接口,但基本上都有次数限制。
就想到了用讯飞星火认识大模型,但是使用它的接口要申请,认证,太麻烦了。
直接用网页版,灌入题干等待答案虽然慢一点,但是使用起来方便快捷无门槛。
于是就直接写了一段js来不断的向它提问,然后获取答案。纯页面元素操作。
运行效果:
上代码:
[Asm] 纯文本查看 复制代码 function setInputValue(element, value) {
let lastValue = element.value;
element.value = value;
let event = new Event('input', { bubbles: true });
event.simulated = true;
let tracker = element._valueTracker;
if (tracker) {
tracker.setValue(lastValue);
}
element.dispatchEvent(event);
}
function readQuestion() {
$.ajax({
url: server + "questionHandler.php"
, type: "POST"
, data: { type: "readQuestion" }
, success: function (_data) {
_data = JSON.parse(_data);
console.log(_data);
//"请回答以下选择题:飞属于植物给我们提供的资源的是( )。\n选项为:鸡蛋|豆浆|牛奶"
setInputValue($("textarea[placeholder*='在此输入您想了解的内容']")[0], "请从指定选项中选择答案:" + _data.content + "\n选项为:" + _data.options);
$("div[class*='ask-window_send']").click();
setTimeout(getAnswerByChat, 1500, _data.id);
}
})
}
function getAnswerByChat(_id) {
//通过判断 "重新回答" 这个按钮的出现来确定是否回答完毕
var _rebtn = $("div[class^='chat-window_re_answer']:visible");
if (_rebtn.length) {
//提取答案(只提取首行)
var _answer = _rebtn.parent().prev().find(".ant-spin-container").find("p:first");
if (_answer.find("strong").length) {
//只提取首行中加粗的关键字
_answer = _answer.find("strong").text();
} else {
_answer = _answer.text();
}
setInputValue($("textarea[placeholder*='在此输入您想了解的内容']")[0], "获得了答案:" + _answer);
saveAnswerById(_id, _answer);
return;
}
setTimeout(getAnswerByChat, 500, _id);
}
function saveAnswerById(_id, _answer) {
$.ajax({
url: server + "questionHandler.php"
, type: "POST"
, data: { type: "saveAnswerById", id: _id, answer: _answer }
, success: function (_data) {
_data = JSON.parse(_data);
console.log("刷新题库中的答案返回:", _data);
setTimeout(readQuestion, 2000);
}
})
}
function init() {
//全新对话按钮
//$("div[class^='ask-window_quit_botmode']").click();
readQuestion();
return;
}
var server = "http://127.0.0.1/kxsy/";
if (typeof $ == 'undefined' || typeof $.ajax == 'undefined') {
var s = document.createElement("script");
s.onload = init;
s.src = "https://cdn.bootcss.com/jquery/3.1.1/jquery.min.js";
document.getElementsByTagName("HEAD")[0].appendChild(s);
} else {
init();
}
console.log("KXSY2024_SCRIPT_INIT");
后台PHP
[Asm] 纯文本查看 复制代码 <?php
include "conn.php";
$type = $_POST["type"];
$out = array();
if ($type == "readQuestion") {
$sql = "SELECT * FROM `{$dbprefix}_data` WHERE `answer`='' ORDER BY RAND() LIMIT 1";
$result = mysqli_query($conn, $sql);
$total = mysqli_num_rows($result);
$out['t'] = $total;
//$out['sql'] = $sql;
if ($total > 0) {
$row = mysqli_fetch_object($result);
$out['id'] = $row->id;
$out['content'] = $row->content;
$out['options'] = $row->options;
}
}
if ($type == "saveAnswerById") {
$id = $_POST["id"];
$answer = $_POST['answer'];
mysqli_query($conn, "UPDATE `{$dbprefix}_data` SET `answer`='$answer' WHERE `id`='$id'");
$out['msg'] = "saveAnswerById";
}
if ($type == "addQuestion") {
$content = $_POST['content'];
$options = $_POST['options'];
if ($tid && $content && $option) {
$rs = mysqli_query($conn, "SELECT * FROM `{$dbprefix}_data` WHERE `content`='$content' LIMIT 1");
if (!mysqli_num_rows($rs)) {
mysqli_query($conn, "INSERT INTO `{$dbprefix}_data` (`content`,`options`) VALUES ('$content','$options','$answer')");
$out['msg'] = "试题添加成功。";
} else {
$out['msg'] = "库中已有此题。";
}
}
}
echo (json_encode($out));
mysqli_close($conn);
数据库结构:
这样就只需要把题导入到题库中,等待星火大模型回答就行了。 |