feat: ai评论支持所有兼容OpenAI API的平台 (#463)

Fixed #463
This commit is contained in:
shanmite 2025-08-19 15:09:25 +08:00
parent 15c1d22469
commit 30bf989aa3
6 changed files with 81 additions and 40 deletions

View File

@ -33,7 +33,7 @@
- 监控用户转发
- 监控话题页面
- 监控专栏合集
- 自动点赞、评论、乱序转发、@好友、带话题、可选随机动态
- 自动点赞、AI评论、乱序转发、@好友、带话题、可选随机动态
- 直播预约抽奖
- 检测是否中奖
- 已读@

View File

@ -6,16 +6,19 @@ module.exports = Object.freeze({
* - `NUMBER` 表示是第几个账号
* - `CLEAR` 是否启用清理功能
* - `ACCOUNT_UA` 账号UA, 可在浏览器控制台输入 navigator.userAgent 查看
*
* ## 高级功能
* - `ENABLE_CHAT_CAPTCHA_OCR` 开启评论验证码识别 使用方法见README
* - `CHAT_CAPTCHA_OCR_URL` 验证码识别接口 POST `url`->`code`
* - `ENABLE_MULTIPLE_ACCOUNT` 是否启用多账号
* - `MULTIPLE_ACCOUNT_PARM` 多账号参数(JSON格式) <不推荐使用
* - `ENABLE_AI_COMMENTS` 是否启用AI评论
*
* ## 调试相关
* - `LOTTERY_LOG_LEVEL` 输出日志等级 Error<Warn<Notice<Info<Debug 0<1<2<3<4
* - `NOT_GO_LOTTERY` 关闭抽奖行为
*
* ## 多账号
* - `ENABLE_MULTIPLE_ACCOUNT` 是否启用多账号
* - `MULTIPLE_ACCOUNT_PARM` 多账号参数(JSON格式) <不推荐使用
* 1. ENABLE_MULTIPLE_ACCOUNT 的值改为true
* 2. 将账号信息依次填写于 multiple_account_parm , 参考例子类推
* - `WAIT` 表示下一个账号运行等待时间(毫秒)
@ -31,9 +34,11 @@ module.exports = Object.freeze({
ENABLE_CHAT_CAPTCHA_OCR: false,
CHAT_CAPTCHA_OCR_URL: 'http://127.0.0.1:9898/ocr/url/text',
ENABLE_MULTIPLE_ACCOUNT: false,
ENABLE_AI_COMMENTS: false,
ENABLE_MULTIPLE_ACCOUNT: false,
MULTIPLE_ACCOUNT_PARM: '',
LOTTERY_LOG_LEVEL: 3,
NOT_GO_LOTTERY: ''
},
@ -65,10 +70,10 @@ module.exports = Object.freeze({
CLEAR: true,
WAIT: 60 * 1000,
ACCOUNT_UA: 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/87.0.4280.88 Safari/537.36',
PROXY_HOST:'',//代理ip
PROXY_PORT:'',//代理ip端口
PROXY_USER:'',//代理ip账号
PROXY_PASS:'',//代理ip密码
PROXY_HOST: '',//代理ip
PROXY_PORT: '',//代理ip端口
PROXY_USER: '',//代理ip账号
PROXY_PASS: '',//代理ip密码
}
],
@ -107,12 +112,12 @@ module.exports = Object.freeze({
},
/**
* ai相关参数
* AI Authentication(OpenAI 兼容的 API 格式)
* Chat completions
* 此处填写Key, 在my_config中的ai_comments_parm中填写API地址等信息
*/
ai_parm: {
//[硅基流动](https://siliconflow.cn/) apikey
SILICON_FLOW_API_KEY:'',
//提示词
PROMPT:''
//apikey
AI_API_KEY: '',
}
});

View File

@ -281,7 +281,7 @@ class Monitor extends Searcher {
reserve_lottery_wait, sneaktower, key_words,
model, chatmodel, chat: chats, relay: relays,
block_dynamic_type, max_create_time, is_imitator,
only_followed, at_users, blockword, blacklist, use_ai_comments
only_followed, at_users, blockword, blacklist, ai_comments_parm
} = config,
now_ts = Date.now() / 1000;
@ -527,10 +527,14 @@ class Monitor extends Searcher {
/* 是否评论 */
if (isSendChat) {
onelotteryinfo.rid = rid;
if (use_ai_comments) {
if (hasEnv('ENABLE_AI_COMMENTS')) {
try {
log.info('开始获取Ai评论', `(https://t.bilibili.com/${dyid})`);
onelotteryinfo.chat = await getAiContent(lottery_info.des) || '!!!';
onelotteryinfo.chat = await getAiContent(
ai_comments_parm.url,
ai_comments_parm.body,
ai_comments_parm.prompt,
lottery_info.des) || '!!!';
log.info('获取到Ai评论内容', `${onelotteryinfo.chat}`);
} catch (e) {
log.error('获取AI评论失败使用随机评论', e);

View File

@ -332,6 +332,12 @@ const config = {
'坚持不懈,迎难而上,开拓创新!', '[OK][OK]', '我来抽个奖', '中中中中中中', '[doge][doge][doge]', '我我我',
],
ai_comments_parm: {
url: '',
body: {},
prompt: ''
},
/**
* 是否抄热评
*/

View File

@ -511,36 +511,36 @@ const utils = {
},
/**
* 获取ai评论
* @param {string} url
* @param {object} body
* @param {string} prompt
* @param {string} content
* @returns {Promise<string|null>}
*/
getAiContent(content) {
getAiContent(url, body, prompt, content) {
return new Promise((resolve) => {
send({
method: 'POST',
url: 'https://api.siliconflow.cn/v1/chat/completions',
url,
headers: {
'authorization': 'Bearer ' + process.env.SILICON_FLOW_API_KEY,
'authorization': 'Bearer ' + process.env.AI_API_KEY,
'content-type': 'application/json'
},
contents: {
model: 'Qwen/Qwen3-32B',
...body,
'stream': false,
'max_tokens': 512,
'enable_thinking': true,
'thinking_budget': 4096,
'min_p': 0.05,
'temperature': 0.7,
'top_p': 0.7,
'top_k': 50,
'frequency_penalty': 0.5,
'n': 1,
'stop': [],
'response_format': { 'type': 'text' },
'messages': [{
'role': 'system',
'content': process.env.PROMPT
}, { 'role': 'user', 'content': content }]
'messages': [
{
'role': 'system',
'content': prompt
},
{
'role': 'user',
'content': content
}
]
},
success: res => {
const data = utils.strToJson(res.body);

View File

@ -341,6 +341,19 @@ module.exports = Object.freeze({
'坚持不懈,迎难而上,开拓创新!', '[OK][OK]', '我来抽个奖', '中中中中中中', '[doge][doge][doge]', '我我我',
],
/**
* AI Chat completions参数
* https://learn.microsoft.com/en-us/azure/ai-foundry/openai/reference#chat-completions
*/
ai_comments_parm: {
/**
* /chat/completions
*/
url: '',
body: {},
prompt: ''
},
/**
* 是否抄热评
*/
@ -460,13 +473,6 @@ module.exports = Object.freeze({
* [1,2,4]
*/
clear_dynamic_type: [1],
/**
* 是否使用ai评论
* true:使用
* false:不使用
* 如需使用需要再env.js配置ai_parm
*/
use_ai_comments: false
},
/**
@ -493,6 +499,26 @@ module.exports = Object.freeze({
APIs: [],
/**
* 默认为硅基流动可以修改为其他AI服务
*/
ai_comments_parm: {
url: 'https://api.siliconflow.cn/v1/chat/completions',
body: {
'model': 'Qwen/Qwen3-32B',
'max_tokens': 512,
'enable_thinking': true,
'thinking_budget': 4096,
'min_p': 0.05,
'temperature': 0.7,
'top_p': 0.7,
'top_k': 50,
'frequency_penalty': 0.5,
'n': 1,
},
prompt: '请根据以下内容直接生成一条简短评论,无需说明信息,且不包含任何敏感词汇。'
},
save_lottery_info_to_file: true,
},
config_2: {},