update
This commit is contained in:
@@ -4,9 +4,17 @@ export {
|
||||
app
|
||||
}
|
||||
|
||||
const res = await app.run({
|
||||
path: 'image-creator',
|
||||
key: 'create-task',
|
||||
})
|
||||
// const res = await app.run({
|
||||
// path: 'image-creator',
|
||||
// key: 'create-task',
|
||||
// })
|
||||
|
||||
console.log('Route run result:', res)
|
||||
// console.log('Route run result:', res)
|
||||
|
||||
|
||||
// const res = await app.run({
|
||||
// path: 'image-creator',
|
||||
// key: 'batch-update-tags',
|
||||
// })
|
||||
|
||||
// console.log('Route run result:', res)
|
||||
57
prompts/test/generate-sentence.ts
Normal file
57
prompts/test/generate-sentence.ts
Normal file
@@ -0,0 +1,57 @@
|
||||
import { SentenceGenerator } from '../src/module/sentence-generator.ts';
|
||||
import { writeFileSync } from 'node:fs';
|
||||
import { dirname, join } from 'node:path';
|
||||
import { fileURLToPath } from 'node:url';
|
||||
|
||||
const __dirname = dirname(fileURLToPath(import.meta.url));
|
||||
const outputPath = join(__dirname, '../data/sentence-01.json');
|
||||
|
||||
console.log('🚀 开始生成1000条哲理句...\n');
|
||||
|
||||
const generator = new SentenceGenerator({
|
||||
count: 1000,
|
||||
outputFormat: 'json',
|
||||
withTags: true,
|
||||
templateTypes: ['对比', '因果', '隐喻', '判断']
|
||||
});
|
||||
|
||||
const results = generator.generateAndOutput() as any[];
|
||||
|
||||
// 添加元信息
|
||||
const outputData = {
|
||||
meta: {
|
||||
total: results.length,
|
||||
generatedAt: new Date().toISOString(),
|
||||
generator: 'sentence-generator.ts',
|
||||
version: '1.0'
|
||||
},
|
||||
sentences: results
|
||||
};
|
||||
|
||||
// 写入文件
|
||||
writeFileSync(outputPath, JSON.stringify(outputData, null, 2), 'utf-8');
|
||||
|
||||
console.log(`✅ 成功生成 ${results.length} 条句子`);
|
||||
console.log(`📁 输出到: ${outputPath}\n`);
|
||||
|
||||
// 打印统计信息
|
||||
console.log('📊 主题分布统计:');
|
||||
const stats = generator.getStats();
|
||||
const sortedStats = Object.entries(stats)
|
||||
.sort(([, a], [, b]) => b - a)
|
||||
.slice(0, 10);
|
||||
|
||||
sortedStats.forEach(([theme, count]) => {
|
||||
const bar = '█'.repeat(Math.floor(count / 10));
|
||||
console.log(` ${theme.padEnd(6)}: ${count.toString().padStart(4)} ${bar}`);
|
||||
});
|
||||
|
||||
console.log(`\n📋 模板类型分布:`);
|
||||
const templateCount: Record<string, number> = {};
|
||||
results.forEach((s: any) => {
|
||||
const type = s.template.split(/[\d]/)[0] || s.template;
|
||||
templateCount[type] = (templateCount[type] || 0) + 1;
|
||||
});
|
||||
Object.entries(templateCount).forEach(([type, count]) => {
|
||||
console.log(` ${type}: ${count} 条`);
|
||||
});
|
||||
24
prompts/test/import-sentence.ts
Normal file
24
prompts/test/import-sentence.ts
Normal file
@@ -0,0 +1,24 @@
|
||||
import { app } from './common.ts';
|
||||
|
||||
import fs from 'node:fs';
|
||||
import path from 'node:path';
|
||||
|
||||
const sentence = path.join(process.cwd(), 'data', 'sentence-01-optimized.json');
|
||||
|
||||
const data = JSON.parse(fs.readFileSync(sentence, 'utf-8'));
|
||||
|
||||
async function run() {
|
||||
const sentences = data?.sentences || [];
|
||||
console.log(`Importing ${sentences.length} sentences...`);
|
||||
const res = await app.run({
|
||||
path: 'image-creator',
|
||||
// key: 'create-sentence-list',
|
||||
key: 'fix-sentences',
|
||||
payload: {
|
||||
data: sentences
|
||||
}
|
||||
});
|
||||
console.log('Import sentence result:', res);
|
||||
}
|
||||
|
||||
await run();
|
||||
@@ -34,10 +34,33 @@ async function main() {
|
||||
// await sleep(100); // To avoid hitting rate limits
|
||||
// }
|
||||
|
||||
const list = await pbService.collection.getFullList({
|
||||
sort: '-created',
|
||||
fields: 'id,title,summary,description,tags,status',
|
||||
})
|
||||
console.log('PocketBase Records:', list.length);
|
||||
// const list = await pbService.collection.getFullList({
|
||||
// sort: '-created',
|
||||
// fields: 'id,title,summary,description,tags,status',
|
||||
// })
|
||||
// console.log('PocketBase Records:', list.length);
|
||||
|
||||
// const b = await pbService.client.collections.create({
|
||||
// name: 'exampleBase',
|
||||
// type: 'base',
|
||||
// fields: [
|
||||
// {
|
||||
// name: 'title',
|
||||
// type: 'text',
|
||||
// required: true,
|
||||
// min: 10,
|
||||
// },
|
||||
// {
|
||||
// name: 'status',
|
||||
// type: 'bool',
|
||||
// },
|
||||
// ],
|
||||
// });
|
||||
// console.log('Created collection:', b);
|
||||
|
||||
const c = await pbService.createCollection({
|
||||
name: 'exampleBase',
|
||||
});
|
||||
console.log('Created collection via PBService:', c);
|
||||
}
|
||||
main();
|
||||
48
prompts/test/perfect-sentence-image.ts
Normal file
48
prompts/test/perfect-sentence-image.ts
Normal file
@@ -0,0 +1,48 @@
|
||||
import { ai } from '../src/index.ts'
|
||||
import { SentenceImage } from '../src/module/sentence-image.ts';
|
||||
|
||||
|
||||
export async function generatePerfectImage() {
|
||||
const sentenceImage = new SentenceImage();
|
||||
// const content = JSON.stringify({
|
||||
// "text": "选择像锁,终将奔跑。",
|
||||
// "theme": "选择",
|
||||
// "template": "{主}像{意象},终将{动}。",
|
||||
// "templateType": "隐喻",
|
||||
// "tags": [
|
||||
// "选择",
|
||||
// "隐喻",
|
||||
// "后悔",
|
||||
// "锁",
|
||||
// "奔跑"
|
||||
// ],
|
||||
// "index": 7,
|
||||
// "optimized": "选择如秤,终需掂量。"
|
||||
// });
|
||||
const content = JSON.stringify({
|
||||
"text": "人生如逆旅,我亦是行人。",
|
||||
"theme": "人生",
|
||||
"template": "人生如{意象},我亦是{身份}。",
|
||||
"templateType": "隐喻",
|
||||
"tags": [
|
||||
"人生",
|
||||
"隐喻",
|
||||
"旅途",
|
||||
"行人"
|
||||
],
|
||||
"index": 1,
|
||||
"optimized": "人生似长河,我自是过客。"
|
||||
});
|
||||
const prompt = sentenceImage.perfect(content);
|
||||
const response = await ai.chat([], {
|
||||
messages: [{ role: "user", content: prompt }],
|
||||
// model: 'qwen-turbo',
|
||||
// model: 'doubao-seed-1-6-251015',
|
||||
model: 'qwen-plus',
|
||||
enable_thinking: true
|
||||
})
|
||||
console.log('生成的海报设计方案:\n', response.choices[0].message.content);
|
||||
return response.choices[0].message.content;
|
||||
}
|
||||
|
||||
generatePerfectImage()
|
||||
90
prompts/test/perfect-sentence.ts
Normal file
90
prompts/test/perfect-sentence.ts
Normal file
@@ -0,0 +1,90 @@
|
||||
import { SentencePerfect } from '../src/module/sentence-perfect.ts';
|
||||
import { readFileSync, writeFileSync } from 'node:fs';
|
||||
import { join, dirname } from 'node:path';
|
||||
import { fileURLToPath } from 'node:url';
|
||||
import { Kevisual } from '@kevisual/ai';
|
||||
import { config } from '../src/module/config.ts';
|
||||
|
||||
const __dirname = dirname(fileURLToPath(import.meta.url));
|
||||
|
||||
// 配置
|
||||
const INPUT_PATH = join(__dirname, '../data/sentence-01.json');
|
||||
const OUTPUT_PATH = join(__dirname, '../data/sentence-01-optimized.json');
|
||||
|
||||
// 读取原始句子
|
||||
const data = JSON.parse(readFileSync(INPUT_PATH, 'utf-8'));
|
||||
const sentences = data.sentences || data;
|
||||
|
||||
console.log(`📖 加载了 ${sentences.length} 条句子\n`);
|
||||
|
||||
// 创建 AI 实例
|
||||
const ai = new Kevisual({
|
||||
apiKey: config.KEVISUAL_NEW_API_KEY || '',
|
||||
model: 'qwen-turbo'
|
||||
});
|
||||
|
||||
// 创建优化器
|
||||
const perfect = new SentencePerfect({
|
||||
ai,
|
||||
concurrency: 3, // 并发数
|
||||
retryTimes: 2, // 重试次数
|
||||
|
||||
// 进度回调
|
||||
onProgress: (progress) => {
|
||||
const { current, total, percentage, successCount, errorCount } = progress;
|
||||
process.stdout.write(`\r🔄 进度: ${current}/${total} (${percentage}%) | 成功: ${successCount} | 失败: ${errorCount}`);
|
||||
},
|
||||
|
||||
// 成功回调
|
||||
onSuccess: (result) => {
|
||||
if (result.index % 10 === 0) {
|
||||
console.log(`\n✅ ${result.index}: "${result.text}"`);
|
||||
console.log(` → "${result.optimized}"`);
|
||||
}
|
||||
},
|
||||
|
||||
// 失败回调
|
||||
onError: (error) => {
|
||||
console.log(`\n❌ ${error.index}: ${error.error}`);
|
||||
}
|
||||
});
|
||||
|
||||
// 准备数据 - 只处理前20条作为测试
|
||||
// const items = sentences.slice(0, 2).map((s: any, i: number) => ({
|
||||
// ...s,
|
||||
// index: s.index || i + 1
|
||||
// }));
|
||||
const items = sentences;
|
||||
console.log('🚀 开始优化句子...\n');
|
||||
|
||||
// 执行优化
|
||||
const startTime = Date.now();
|
||||
const results = await perfect.perfectBatch(items);
|
||||
const elapsed = Date.now() - startTime;
|
||||
|
||||
console.log(`\n\n✅ 完成!耗时: ${(elapsed / 1000).toFixed(2)}秒`);
|
||||
|
||||
// 统计
|
||||
const successResults = results.filter(r => r.optimized !== r.text);
|
||||
console.log(` 成功: ${successResults.length}/${results.length}`);
|
||||
|
||||
// 保存结果
|
||||
const output = {
|
||||
meta: {
|
||||
total: results.length,
|
||||
success: successResults.length,
|
||||
optimizedAt: new Date().toISOString()
|
||||
},
|
||||
sentences: results
|
||||
};
|
||||
|
||||
writeFileSync(OUTPUT_PATH, JSON.stringify(output, null, 2), 'utf-8');
|
||||
console.log(`📁 结果已保存到: ${OUTPUT_PATH}`);
|
||||
|
||||
// 显示示例
|
||||
console.log('\n📝 优化示例:');
|
||||
successResults.slice(0, 5).forEach((r, i) => {
|
||||
console.log(`\n${i + 1}. [${r.theme}]`);
|
||||
console.log(` 原: ${r.text}`);
|
||||
console.log(` 优: ${r.optimized}`);
|
||||
});
|
||||
Reference in New Issue
Block a user