|
| 1 | +import { PromptTemplate } from '@langchain/prompts' |
| 2 | + |
| 3 | +/** |
| 4 | + * Configuration interface for PromptTemplate |
| 5 | + * @interface PromptTemplateConfig |
| 6 | + */ |
| 7 | +export interface PromptTemplateConfig { |
| 8 | + template: string |
| 9 | + inputVariables: string[] |
| 10 | + partialVariables: Instruction |
| 11 | +} |
| 12 | + |
| 13 | +/** |
| 14 | + * Interface for formatting instructions |
| 15 | + * @interface Instruction |
| 16 | + */ |
| 17 | +export interface Instruction { |
| 18 | + formatInstructions: string |
| 19 | +} |
| 20 | + |
| 21 | +/** |
| 22 | + * Enum defining types of prompts that can be generated |
| 23 | + * @enum {string} |
| 24 | + */ |
| 25 | +export enum PromptType { |
| 26 | + Folder = 'folder', |
| 27 | + File = 'file', |
| 28 | +} |
| 29 | + |
| 30 | +/** |
| 31 | + * Class responsible for generating prompts based on different PromptTypes |
| 32 | + * @class PromptGenerator |
| 33 | + */ |
| 34 | +export class PromptGenerator { |
| 35 | + protected template: PromptTemplate |
| 36 | + protected promptType: PromptType |
| 37 | + |
| 38 | + /** |
| 39 | + * Creates an instance of PromptGenerator |
| 40 | + * @example |
| 41 | + * const generator = new PromptGenerator({ |
| 42 | + * template: "The following instruction is given:\n{formatInstructions}\nBelow is the code for your task: {code}", |
| 43 | + * inputVariables: ["code"], |
| 44 | + * partialVariables: { |
| 45 | + * formatInstructions: CodePrompt // Import CodePrompt from prompt.ts |
| 46 | + * }}, PromptType.File); |
| 47 | + * @example |
| 48 | + * const generator = new PromptGenerator({ |
| 49 | + * template: "The following instruction is given:\n{formatInstructions}\nBelow are the AI summaries for the codebase:\n{ai_summaries}", |
| 50 | + * inputVariables: ["ai_summaries"], |
| 51 | + * partialVariables: { |
| 52 | + * formatInstructions: FolderPrompt // Import FolderPrompt from prompt.ts |
| 53 | + * }}, PromptType.Folder); |
| 54 | + * @param {PromptTemplateConfig} config - Configuration for the prompt template |
| 55 | + * @param {PromptType} promptType - Type of prompt to generate |
| 56 | + */ |
| 57 | + constructor(config: PromptTemplateConfig, promptType: PromptType) { |
| 58 | + this.template = new PromptTemplate({ |
| 59 | + ...config, |
| 60 | + }) |
| 61 | + this.promptType = promptType |
| 62 | + } |
| 63 | + |
| 64 | + /** |
| 65 | + * Generates a formatted prompt based on the prompt type and inputs |
| 66 | + * @param {string} [code] - Optional code string for file prompts |
| 67 | + * @param {string[]} [ai_summaries] - Optional array of AI summaries for folder prompts |
| 68 | + * @returns {Promise<string>} Formatted prompt string |
| 69 | + * @throws {Error} If prompt type is invalid or required input is missing |
| 70 | + */ |
| 71 | + async generate(code?: string, ai_summaries?: string[]): Promise<string> { |
| 72 | + const promptMap: Record<PromptType, string> = { |
| 73 | + [PromptType.Folder]: ai_summaries?.join('\n') ?? '', |
| 74 | + [PromptType.File]: code ?? '', |
| 75 | + } |
| 76 | + |
| 77 | + const userPrompt: string = promptMap[this.promptType] |
| 78 | + if (!userPrompt.length) { |
| 79 | + throw new Error( |
| 80 | + 'Invalid prompt type or missing input for prompt generation' |
| 81 | + ) |
| 82 | + } |
| 83 | + |
| 84 | + return this.template.format( this.promptType === PromptType.File ? { code: userPrompt } : { ai_summaries: userPrompt }) |
| 85 | + } |
| 86 | +} |
0 commit comments