Skip to content

Commit 2196bf6

Browse files
committed
feat: ai评论支持所有兼容OpenAI API的平台 (#463)
Fixed #463
1 parent 15c1d22 commit 2196bf6

6 files changed

Lines changed: 81 additions & 40 deletions

File tree

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@
3333
- 监控用户转发
3434
- 监控话题页面
3535
- 监控专栏合集
36-
- 自动点赞、评论、乱序转发、@好友、带话题、可选随机动态
36+
- 自动点赞、AI评论、乱序转发、@好友、带话题、可选随机动态
3737
- 直播预约抽奖
3838
- 检测是否中奖
3939
- 已读@

env.example.js

Lines changed: 17 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -6,16 +6,19 @@ module.exports = Object.freeze({
66
* - `NUMBER` 表示是第几个账号
77
* - `CLEAR` 是否启用清理功能
88
* - `ACCOUNT_UA` 账号UA, 可在浏览器控制台输入 navigator.userAgent 查看
9+
*
910
* ## 高级功能
1011
* - `ENABLE_CHAT_CAPTCHA_OCR` 开启评论验证码识别 使用方法见README
1112
* - `CHAT_CAPTCHA_OCR_URL` 验证码识别接口 POST `url`->`code`
12-
* - `ENABLE_MULTIPLE_ACCOUNT` 是否启用多账号
13-
* - `MULTIPLE_ACCOUNT_PARM` 多账号参数(JSON格式) <不推荐使用
13+
* - `ENABLE_AI_COMMENTS` 是否启用AI评论
14+
*
1415
* ## 调试相关
1516
* - `LOTTERY_LOG_LEVEL` 输出日志等级 Error<Warn<Notice<Info<Debug 0<1<2<3<4
1617
* - `NOT_GO_LOTTERY` 关闭抽奖行为
1718
*
1819
* ## 多账号
20+
* - `ENABLE_MULTIPLE_ACCOUNT` 是否启用多账号
21+
* - `MULTIPLE_ACCOUNT_PARM` 多账号参数(JSON格式) <不推荐使用
1922
* 1. 将 ENABLE_MULTIPLE_ACCOUNT 的值改为true
2023
* 2. 将账号信息依次填写于 multiple_account_parm 中, 参考例子类推
2124
* - `WAIT` 表示下一个账号运行等待时间(毫秒)
@@ -31,9 +34,11 @@ module.exports = Object.freeze({
3134

3235
ENABLE_CHAT_CAPTCHA_OCR: false,
3336
CHAT_CAPTCHA_OCR_URL: 'http://127.0.0.1:9898/ocr/url/text',
34-
ENABLE_MULTIPLE_ACCOUNT: false,
37+
ENABLE_AI_COMMENTS: false,
3538

39+
ENABLE_MULTIPLE_ACCOUNT: false,
3640
MULTIPLE_ACCOUNT_PARM: '',
41+
3742
LOTTERY_LOG_LEVEL: 3,
3843
NOT_GO_LOTTERY: ''
3944
},
@@ -65,10 +70,10 @@ module.exports = Object.freeze({
6570
CLEAR: true,
6671
WAIT: 60 * 1000,
6772
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',
68-
PROXY_HOST:'',//代理ip
69-
PROXY_PORT:'',//代理ip端口
70-
PROXY_USER:'',//代理ip账号
71-
PROXY_PASS:'',//代理ip密码
73+
PROXY_HOST: '',//代理ip
74+
PROXY_PORT: '',//代理ip端口
75+
PROXY_USER: '',//代理ip账号
76+
PROXY_PASS: '',//代理ip密码
7277
}
7378
],
7479

@@ -107,12 +112,12 @@ module.exports = Object.freeze({
107112
},
108113

109114
/**
110-
* ai相关参数
115+
* AI Authentication(OpenAI 兼容的 API 格式)
116+
* Chat completions
117+
* 此处填写Key, 在my_config中的ai_comments_parm中填写API地址等信息
111118
*/
112119
ai_parm: {
113-
//[硅基流动](https://siliconflow.cn/) apikey
114-
SILICON_FLOW_API_KEY:'',
115-
//提示词
116-
PROMPT:''
120+
//apikey
121+
AI_API_KEY: '',
117122
}
118123
});

lib/core/monitor.js

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -281,7 +281,7 @@ class Monitor extends Searcher {
281281
reserve_lottery_wait, sneaktower, key_words,
282282
model, chatmodel, chat: chats, relay: relays,
283283
block_dynamic_type, max_create_time, is_imitator,
284-
only_followed, at_users, blockword, blacklist, use_ai_comments
284+
only_followed, at_users, blockword, blacklist, ai_comments_parm
285285
} = config,
286286
now_ts = Date.now() / 1000;
287287

@@ -527,10 +527,14 @@ class Monitor extends Searcher {
527527
/* 是否评论 */
528528
if (isSendChat) {
529529
onelotteryinfo.rid = rid;
530-
if (use_ai_comments) {
530+
if (hasEnv('ENABLE_AI_COMMENTS')) {
531531
try {
532532
log.info('开始获取Ai评论', `(https://t.bilibili.com/${dyid})`);
533-
onelotteryinfo.chat = await getAiContent(lottery_info.des) || '!!!';
533+
onelotteryinfo.chat = await getAiContent(
534+
ai_comments_parm.url,
535+
ai_comments_parm.body,
536+
ai_comments_parm.prompt,
537+
lottery_info.des) || '!!!';
534538
log.info('获取到Ai评论内容', `${onelotteryinfo.chat}`);
535539
} catch (e) {
536540
log.error('获取AI评论失败,使用随机评论', e);

lib/data/config.js

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -332,6 +332,12 @@ const config = {
332332
'坚持不懈,迎难而上,开拓创新!', '[OK][OK]', '我来抽个奖', '中中中中中中', '[doge][doge][doge]', '我我我',
333333
],
334334

335+
ai_comments_parm: {
336+
url: '',
337+
body: {},
338+
prompt: ''
339+
},
340+
335341
/**
336342
* 是否抄热评
337343
*/

lib/utils.js

Lines changed: 17 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -511,36 +511,36 @@ const utils = {
511511
},
512512
/**
513513
* 获取ai评论
514+
* @param {string} url
515+
* @param {object} body
516+
* @param {string} prompt
514517
* @param {string} content
515518
* @returns {Promise<string|null>}
516519
*/
517-
getAiContent(content) {
520+
getAiContent(url, body, prompt, content) {
518521
return new Promise((resolve) => {
519522
send({
520523
method: 'POST',
521-
url: 'https://api.siliconflow.cn/v1/chat/completions',
524+
url,
522525
headers: {
523-
'authorization': 'Bearer ' + process.env.SILICON_FLOW_API_KEY,
526+
'authorization': 'Bearer ' + process.env.AI_API_KEY,
524527
'content-type': 'application/json'
525528
},
526529
contents: {
527-
model: 'Qwen/Qwen3-32B',
530+
...body,
528531
'stream': false,
529-
'max_tokens': 512,
530532
'enable_thinking': true,
531-
'thinking_budget': 4096,
532-
'min_p': 0.05,
533-
'temperature': 0.7,
534-
'top_p': 0.7,
535-
'top_k': 50,
536-
'frequency_penalty': 0.5,
537-
'n': 1,
538-
'stop': [],
539533
'response_format': { 'type': 'text' },
540-
'messages': [{
541-
'role': 'system',
542-
'content': process.env.PROMPT
543-
}, { 'role': 'user', 'content': content }]
534+
'messages': [
535+
{
536+
'role': 'system',
537+
'content': prompt
538+
},
539+
{
540+
'role': 'user',
541+
'content': content
542+
}
543+
]
544544
},
545545
success: res => {
546546
const data = utils.strToJson(res.body);

my_config.example.js

Lines changed: 33 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -341,6 +341,19 @@ module.exports = Object.freeze({
341341
'坚持不懈,迎难而上,开拓创新!', '[OK][OK]', '我来抽个奖', '中中中中中中', '[doge][doge][doge]', '我我我',
342342
],
343343

344+
/**
345+
* AI Chat completions参数
346+
* https://learn.microsoft.com/en-us/azure/ai-foundry/openai/reference#chat-completions
347+
*/
348+
ai_comments_parm: {
349+
/**
350+
* /chat/completions
351+
*/
352+
url: '',
353+
body: {},
354+
prompt: ''
355+
},
356+
344357
/**
345358
* 是否抄热评
346359
*/
@@ -460,13 +473,6 @@ module.exports = Object.freeze({
460473
* [1,2,4]
461474
*/
462475
clear_dynamic_type: [1],
463-
/**
464-
* 是否使用ai评论。
465-
* true:使用
466-
* false:不使用
467-
* 如需使用需要再env.js配置ai_parm
468-
*/
469-
use_ai_comments: false
470476
},
471477

472478
/**
@@ -493,6 +499,26 @@ module.exports = Object.freeze({
493499

494500
APIs: [],
495501

502+
/**
503+
* 默认为硅基流动,可以修改为其他AI服务
504+
*/
505+
ai_comments_parm: {
506+
url: 'https://api.siliconflow.cn/v1/chat/completions',
507+
body: {
508+
'model': 'Qwen/Qwen3-32B',
509+
'max_tokens': 512,
510+
'enable_thinking': true,
511+
'thinking_budget': 4096,
512+
'min_p': 0.05,
513+
'temperature': 0.7,
514+
'top_p': 0.7,
515+
'top_k': 50,
516+
'frequency_penalty': 0.5,
517+
'n': 1,
518+
},
519+
prompt: '请根据以下内容生成一条简短评论,且不包含任何敏感词汇。'
520+
},
521+
496522
save_lottery_info_to_file: true,
497523
},
498524
config_2: {},

0 commit comments

Comments
 (0)