Skip to content

Commit aa0ad82

Browse files
committed
Implement prompt generator and schema parser for prompt engineering (#23)
Signed-off-by: Dae❤️ <74119677+daeisbae@users.noreply.github.com>
1 parent 2db4a7f commit aa0ad82

File tree

2 files changed

+111
-0
lines changed

2 files changed

+111
-0
lines changed
Lines changed: 86 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,86 @@
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+
}
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
import { StructuredOutputParser } from '@/langchain/output_parsers'
2+
import { ZodSchema } from 'zod'
3+
4+
5+
/**
6+
* Parser class that converts LLM outputs into structured objects
7+
* @class SchemaParser
8+
*/
9+
export class SchemaParser {
10+
private outputParser: StructuredOutputParser
11+
private formatInstructions: string
12+
13+
constructor(schema: ZodSchema) {
14+
this.outputParser = new StructuredOutputParser(schema)
15+
this.formatInstructions = this.outputParser.getFormalInstructions()
16+
}
17+
18+
get formalInstructions() {
19+
return this.formatInstructions;
20+
}
21+
22+
async parse(output: string): Promise<Object> {
23+
return await this.outputParser.parse(output)
24+
}
25+
}

0 commit comments

Comments
 (0)