update 添加保存

This commit is contained in:
2026-01-07 17:34:10 +08:00
commit 7cca41b457
15 changed files with 6411 additions and 0 deletions

14
prompts/src/index.ts Normal file
View File

@@ -0,0 +1,14 @@
import { PromptGenerator, type PromptGeneratorOptions } from "./prompt-geneator.ts";
import { writeFile } from "node:fs/promises";
import { Prompt } from "./prompt-perfect.ts";
import { customAlphabet } from "nanoid";
const letter = 'abcdefghijklmnopqrstuvwxyz'
const randomString = customAlphabet(letter, 16);
async function saveToFile(data: Map<string, string>, outputPath: string): Promise<void> {
const arrayData = Array.from(data.entries()).map(([key, value]) => ({ key, value, id: randomString() }));
await writeFile(outputPath, JSON.stringify(arrayData, null, 2), "utf-8");
console.log(`Generated ${arrayData.length} prompts and saved to ${outputPath}`);
}
export { PromptGenerator, PromptGeneratorOptions, saveToFile, Prompt };

View File

@@ -0,0 +1,91 @@
import { randomInt as random } from "es-toolkit";
import { max } from "es-toolkit/compat";
export interface PromptGeneratorOptions {
count?: number;
outputPath?: string;
sourceKeys?: string[];
additionalValues?: string[];
source?: { [key: string]: string[] };
endString?: string;
}
export class PromptGenerator {
sourceKeys: string[];
additionalValues: string[];
count: number;
source: { [key: string]: string[] } = {};
endString: string = "";
map: Map<string, string> = new Map();
constructor(options: PromptGeneratorOptions = {}) {
this.additionalValues = options.additionalValues || [];
this.count = options.count ?? 1000;
this.source = options.source || {};
if (options?.sourceKeys) {
this.sourceKeys = options.sourceKeys || [];
} else {
this.sourceKeys = Object.keys(this.source);
}
this.endString = options.endString || "";
}
generateMap(opts?: { max?: number }): Map<string, string> {
const map = new Map<string, string>();
const source = this.source
let _max = max([opts?.max || 1200, this.count + 1000])!;
let k = 0;
while (map.size < this.count) {
const selectedValues: string[] = [];
this.sourceKeys.forEach((key: string) => {
const values = source[key];
if (!values) {
return;
}
const randomIndex = random(0, values.length - 1);
const randomValue = values[randomIndex];
selectedValues.push(randomValue);
});
const randomSlect = random(1, 3);
const additionalRandomValue = this.pickRandomValue(randomSlect, this.additionalValues);
selectedValues.push(...additionalRandomValue);
if (this.endString) {
selectedValues.push(this.endString);
}
const key = selectedValues.join(",");
k++;
if (k > _max) {
console.warn(`Reached maximum attempts (${_max}) to generate unique prompts. Generated ${map.size} unique prompts.`);
break;
}
if (!map.has(key)) {
map.set(key, selectedValues.join(" "));
}
}
this.map = map;
return map;
}
/**
* 获取随机附加值
* @param num
* @param arr
* @returns
*/
pickRandomValue(num: number, arr: string[]): string[] {
const randomAdditionalValues: string[] = [];
const usedIndices: Set<number> = new Set();
if (num > arr.length) {
num = Math.min(num, arr.length - 1);
}
while (randomAdditionalValues.length < num && usedIndices.size < arr.length) {
const randomIndex = random(0, arr.length - 1);
if (!usedIndices.has(randomIndex)) {
usedIndices.add(randomIndex);
randomAdditionalValues.push(arr[randomIndex]);
}
}
return randomAdditionalValues;
}
}

View File

@@ -0,0 +1,18 @@
export interface PromptOptions {
perfectPrompt?: string;
}
const DefaultPerfectPrompt = "我需要你帮我完善提示词,使其更加详细和具体。\n";
export class Prompt {
perfectPrompt: string = "";
constructor(options: PromptOptions = {}) {
this.perfectPrompt = options.perfectPrompt || DefaultPerfectPrompt;
}
perfect(info: string): string {
return `${info}\n当前的提示词是在<perfect/>标签中:\n<perfect>${this.perfectPrompt}</perfect>`;
}
clearPerfectTags(text: string): string {
return text.replace('<perfect>', '').replace('</perfect>', '').trim();
}
}