diff options
| author | bobzel <zzzman@gmail.com> | 2025-06-19 15:30:54 -0400 |
|---|---|---|
| committer | bobzel <zzzman@gmail.com> | 2025-06-19 15:30:54 -0400 |
| commit | abdfe5b109f42cd5da4aceef1bd667c8e1f6cbbd (patch) | |
| tree | 26c0d1b4d4034a98003f97ecdc771066a0e04804 /src/client/views/nodes/scrapbook/AIPresetGenerator.ts | |
| parent | 562d6591a5e4feb7d44135445fd16056e0f8943f (diff) | |
| parent | c0daf8b6aff92b71662d9791aeaa4bb63076dd02 (diff) | |
Merge branch 'lanyi-branch'
Diffstat (limited to 'src/client/views/nodes/scrapbook/AIPresetGenerator.ts')
| -rw-r--r-- | src/client/views/nodes/scrapbook/AIPresetGenerator.ts | 31 |
1 files changed, 31 insertions, 0 deletions
diff --git a/src/client/views/nodes/scrapbook/AIPresetGenerator.ts b/src/client/views/nodes/scrapbook/AIPresetGenerator.ts new file mode 100644 index 000000000..1f159222b --- /dev/null +++ b/src/client/views/nodes/scrapbook/AIPresetGenerator.ts @@ -0,0 +1,31 @@ +import { ScrapbookItemConfig } from './ScrapbookPreset'; +import { GPTCallType, gptAPICall } from '../../../apis/gpt/GPT'; + +// Represents the descriptor for each document +export interface DocumentDescriptor { + type: string; + tags: string[]; +} + +// Main function to request AI-generated presets +export async function requestAiGeneratedPreset(descriptors: DocumentDescriptor[]): Promise<ScrapbookItemConfig[]> { + const prompt = createPrompt(descriptors); + let aiResponse = await gptAPICall(prompt, GPTCallType.GENERATESCRAPBOOK); + // Strip out ```json and ``` if the model wrapped its answer in fences + aiResponse = aiResponse + .trim() + .replace(/^```(?:json)?\s*/, "") // remove leading ``` or ```json + .replace(/\s*```$/, ""); // remove trailing ``` + const parsedPreset = JSON.parse(aiResponse) as ScrapbookItemConfig[]; + return parsedPreset; +} + +// Helper to generate prompt text for AI +function createPrompt(descriptors: DocumentDescriptor[]): string { + let prompt = ""; + descriptors.forEach((desc, index) => { + prompt += `${index + 1}. Type: ${desc.type}, Tags: ${desc.tags.join(', ')}\n`; + }); + + return prompt; +} |
