| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153 |
- import commonModel from '../model/commonModel.js';
- import fs from 'fs';
- import { stringUtils } from '../util/stringClass.js';
- import aiController from '../api/yjbdc/aiController.js';
- async function runScript(){
- try {
-
- //按照高频单词的使用频率排序,列出所有单词
- const sql="select * from Words where BookID=110 and (EnglishExplanation is null or EnglishExplanation='') order by ID;"
- let list = await commonModel.RunSql(null,sql);
- let count=list.length;
- const start=0;
- //count=1;
- // 添加延时函数,确保每分钟只发送9次请求(约每6.67秒一次请求)
- const delay = (ms) => new Promise(resolve => setTimeout(resolve, ms));
- const requestDelay = 7000; // 7秒,确保每分钟最多9次请求
-
- for(let i=start;i<count;i++){
- let item=list[i];
- //获得单词
- const word=item.Word;
- let content={
- "instruction": "Generate an English-English explanation using the word '" + word + "'",
- "requirements": [
- "For Chinese students learning English",
- "If the word has multiple meanings, generate one to three of the most common English-English explanations",
- "Each explanation should be preceded by the part of speech (multiple parts of speech are allowed)",
- "Try to use A1 or A2 level vocabulary for the English-English explanations",
- "The format must be JSON, strictly following the sample output_format, and avoid duplicate quotation marks ('\"')"
- ],
- "output_format": {
- "Word": "spring",
- "EnglishExplanation": [
- "n. the season of the year between winter and summer, lasting from March to June north of the equator, and from September to December south of the equator, when the weather becomes warmer, leaves and plants start to grow again and flowers appear",
- "n. a piece of curved or bent metal that can be pressed into a smaller space but then returns to its usual shape",
- "n. a place where water naturally flows out from the ground"
- ]
- }
- }
- content=JSON.stringify(content);
- //console.log(content);
- let aiProvider="llama-4-maverick-17b-128e-instruct";
- //aiProvider="ali-qwen-plus";
- //生成例句
- let result = await aiController.generateArticle(content, aiProvider);
-
- //console.log("result1:"+result);
- //console.log(result.indexOf("{"));
- if (result.indexOf("```json")>0){
- console.log("```json");
- console.log(result.indexOf("```json"));
- result=result.substring(result.indexOf("```json")+7);
- result=result.substring(0,result.lastIndexOf("```"));
- }
- else if (result.indexOf("```")>0){
- console.log("```");
- console.log(result.indexOf("```"));
-
- result=result.substring(result.indexOf("```")+3);
- result=result.substring(0,result.lastIndexOf("```"));
- }
- else if (result.indexOf("Here 's a breakdown of the response")>0){
- result=result.substring(0,result.lastIndexOf("Here 's a breakdown of the response"));
- }
- //console.log("result2:"+result);
- result=result.replace("\"\":","\":");
- result=result.replace("\"\" :","\" :");
- result=result.replace(":\"\"",":\"");
- result=result.replace(": \"\"",": \"");
- result=result.replace(",\"\"",",\"");
- result=result.replace("\"\",","\",");
- result=result.replace("\"\":","\":");
- result=result.replace("\"\" :","\" :");
- result=result.replace(":\"\"",":\"");
- result=result.replace(": \"\"",": \"");
- result=result.replace(",\"\"",",\"");
- result=result.replace("\"\",","\",");
-
- //console.log(result);
- let sql2="update Words set EnglishExplanation=? where ID="+item.ID+";";
- await commonModel.RunSql(result,sql2);
-
- console.log( i +"/"+ list.length+" "+word);
-
- // 在每次请求后添加延时,除非是最后一个请求
- if (i < count - 1) {
- console.log(`等待 ${requestDelay/1000} 秒后继续下一个请求...`);
- await delay(requestDelay);
- }
- }
-
- console.log("完成");
- process.exit(1);
- } catch (error) {
- console.error('Error executing script:', error);
- process.exit(1);
- }
- }
- // 处理Promise并添加错误捕获
- runScript().catch(error => {
- console.error('Error in runScript:', error);
- });
- ////批量处理加资料的数据
- // async function runScript(){
- // try {
- // const sql="select ID from MiaoguoLiteracy order by ID;"
- // let list = await commonModel.RunSql(null,sql);
- // for(let i=0;i<list.length;i++){
- // if (i % 1000 == 0)
- // console.log("i:"+i+" ID:"+list[i].ID
- // let item =await commonModel.RunSql(null,"select * from MiaoguoLiteracy where ID="+list[i].ID+";"
- // let json;
- // try {
- // json = JSON.parse(item[0].JSONString
- // } catch (parseError) {
- // console.error('JSON Parse Error at ID:', list[i].ID);
- // console.error('Error details:', parseError);
- // continue;
- // }
- // if (json && json.CHN && json.CHN.BiShunArr && json.CHN.BiShunArr.length>0){
- // console.log(i+" "+json.CHN.HanZi);
-
- // delete json.CHN.BiShunArr;
- // let obj={};
- // obj.ID=list[i].ID;
- // obj.JSONString=JSON.stringify(json);
- // await commonModel.RunSql(obj,"update MiaoguoLiteracy set ? where ID="+list[i].ID+";"
- // }
- // //console.log(json);
- // //console.log(item.length);
- // }
- // console.log("完成"
- // } catch (error) {
- // console.error('Error executing script:', error);
- // }
- // }
|