@tstdl/base 0.93.193 → 0.93.194
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/ai/prompts/prompt-builder.d.ts +2 -0
- package/ai/prompts/prompt-builder.js +20 -6
- package/document-management/server/services/document-management-ai.prompts.d.ts +1 -1
- package/document-management/server/services/document-management-ai.prompts.js +6 -7
- package/document-management/server/services/document-management-ai.service.js +1 -1
- package/document-management/server/validators/ai-validation-executor.js +1 -1
- package/package.json +1 -1
|
@@ -16,6 +16,8 @@ export declare class PromptBuilder {
|
|
|
16
16
|
setOutputSchema<Input = ObjectLiteral, Output = ObjectLiteral>(schema: SchemaTestable<Output>, examples?: FewShotExample<Input, Output>[]): this;
|
|
17
17
|
setSystemOutputExamples<Input = ObjectLiteral, Output = ObjectLiteral>(examples: FewShotExample<Input, Output>[]): this;
|
|
18
18
|
setOutputExamples<Input = ObjectLiteral, Output = ObjectLiteral>(examples: FewShotExample<Input, Output>[]): this;
|
|
19
|
+
enableSystemOutputRules(enabled?: boolean): this;
|
|
20
|
+
enableOutputRules(enabled?: boolean): this;
|
|
19
21
|
setSystemInstructionsOverride(override: ((instructions: Instructions) => Instructions | string) | undefined): this;
|
|
20
22
|
setInstructionsOverride(override: ((instructions: Instructions) => Instructions | string) | undefined): this;
|
|
21
23
|
setLanguage(language: string): this;
|
|
@@ -14,8 +14,10 @@ export class PromptBuilder {
|
|
|
14
14
|
#task;
|
|
15
15
|
#systemOutputSchema;
|
|
16
16
|
#systemOutputExamples;
|
|
17
|
+
#systemOutputRules = false;
|
|
17
18
|
#outputSchema;
|
|
18
19
|
#outputExamples;
|
|
20
|
+
#outputRules = false;
|
|
19
21
|
#systemInstructions = {};
|
|
20
22
|
#instructions = {};
|
|
21
23
|
#systemContextParts = {};
|
|
@@ -57,6 +59,14 @@ export class PromptBuilder {
|
|
|
57
59
|
this.#outputExamples = examples;
|
|
58
60
|
return this;
|
|
59
61
|
}
|
|
62
|
+
enableSystemOutputRules(enabled = true) {
|
|
63
|
+
this.#systemOutputRules = enabled;
|
|
64
|
+
return this;
|
|
65
|
+
}
|
|
66
|
+
enableOutputRules(enabled = true) {
|
|
67
|
+
this.#outputRules = enabled;
|
|
68
|
+
return this;
|
|
69
|
+
}
|
|
60
70
|
setSystemInstructionsOverride(override) {
|
|
61
71
|
this.#systemInstructionsOverride = override;
|
|
62
72
|
return this;
|
|
@@ -108,6 +118,7 @@ export class PromptBuilder {
|
|
|
108
118
|
instructions: this.#systemInstructions,
|
|
109
119
|
outputSchema: this.#systemOutputSchema,
|
|
110
120
|
outputExamples: this.#systemOutputExamples,
|
|
121
|
+
outputRules: this.#systemOutputRules,
|
|
111
122
|
task: this.#systemTask,
|
|
112
123
|
media: this.#systemMedia,
|
|
113
124
|
language: this.#language,
|
|
@@ -121,6 +132,7 @@ export class PromptBuilder {
|
|
|
121
132
|
instructions: this.#instructions,
|
|
122
133
|
outputSchema: this.#outputSchema,
|
|
123
134
|
outputExamples: this.#outputExamples,
|
|
135
|
+
outputRules: this.#outputRules,
|
|
124
136
|
task: this.#task,
|
|
125
137
|
media: this.#media,
|
|
126
138
|
language: this.#language,
|
|
@@ -152,17 +164,19 @@ function buildPrompt(data) {
|
|
|
152
164
|
instructions['**Instructions**'] = data.instructions;
|
|
153
165
|
}
|
|
154
166
|
const hasOutputSchema = isDefined(data.outputSchema);
|
|
155
|
-
if (hasOutputSchema || isDefined(data.outputExamples)) {
|
|
167
|
+
if (hasOutputSchema || data.outputRules || isDefined(data.outputExamples)) {
|
|
156
168
|
if (hasOutputSchema) {
|
|
157
169
|
const schema = convertToOpenApiSchema(data.outputSchema);
|
|
158
170
|
const schemaJson = JSON.stringify(schema, null, 2);
|
|
159
171
|
instructions['**Output Schema**'] = `\`\`\`json\n${schemaJson}\n\`\`\``;
|
|
160
172
|
}
|
|
161
|
-
|
|
162
|
-
'Schema
|
|
163
|
-
|
|
164
|
-
|
|
165
|
-
|
|
173
|
+
if (data.outputRules) {
|
|
174
|
+
instructions['**Output Schema Instructions**'] = unorderedList({
|
|
175
|
+
'Schema Compliance': 'Generate valid JSON that strictly matches the provided schema.',
|
|
176
|
+
'Nullable fields with missing data': 'Must be set to literal `null`, not the string "null".',
|
|
177
|
+
'Optional fields with missing data': 'Omit the key entirely (sparse JSON).',
|
|
178
|
+
});
|
|
179
|
+
}
|
|
166
180
|
if (isDefined(data.outputExamples) && (data.outputExamples.length > 0)) {
|
|
167
181
|
instructions['**Output Examples**'] = fewShotPrompt(data.outputExamples);
|
|
168
182
|
}
|
|
@@ -16,7 +16,7 @@ export declare const dataExtractionFields: {
|
|
|
16
16
|
Tags: string;
|
|
17
17
|
Date: string;
|
|
18
18
|
};
|
|
19
|
-
export declare function createDataExtractionPrompt(
|
|
19
|
+
export declare function createDataExtractionPrompt(): PromptBuilder;
|
|
20
20
|
export declare const assignCollectionSchema: import("../../../schema/index.js").ObjectSchema<{
|
|
21
21
|
collectionIds: string[];
|
|
22
22
|
}>;
|
|
@@ -8,7 +8,7 @@ export function createContentExtractionPrompt() {
|
|
|
8
8
|
return promptBuilder()
|
|
9
9
|
.setSystemRole(DOCUMENT_MANAGEMENT_SYSTEM_ROLE)
|
|
10
10
|
.setTask('Transcribe the attached document into Markdown following the instructions.')
|
|
11
|
-
.
|
|
11
|
+
.enableOutputRules()
|
|
12
12
|
.addInstructions({
|
|
13
13
|
'Objective': 'Convert the provided document into semantically structured, clean Markdown.',
|
|
14
14
|
'Critical Constraints': orderedList([
|
|
@@ -56,12 +56,11 @@ export function createClassifySchema(validTypes) {
|
|
|
56
56
|
return object({ documentType: enumeration(validTypes) });
|
|
57
57
|
}
|
|
58
58
|
export function createClassifyPrompt(validTypes) {
|
|
59
|
-
const schema = createClassifySchema(validTypes);
|
|
60
59
|
return promptBuilder()
|
|
61
60
|
.setSystemRole(DOCUMENT_MANAGEMENT_SYSTEM_ROLE)
|
|
62
61
|
.setRole('Document Taxonomy Specialist')
|
|
63
62
|
.setTask('Determine the single most accurate document type from the provided list based on the document.')
|
|
64
|
-
.
|
|
63
|
+
.setOutputExamples(CLASSIFY_FEW_SHOT)
|
|
65
64
|
.addInstructions({
|
|
66
65
|
'Analysis Strategy': orderedList([
|
|
67
66
|
'Scan the header and title for explicit document type names (e.g., "Invoice", "Contract", "Bill of Lading").',
|
|
@@ -85,12 +84,12 @@ export const dataExtractionFields = {
|
|
|
85
84
|
Tags: 'Generate 3-5 keywords for categorization. Only use important information missing in title, subtitle and properties. Prioritize reusing of existing tags where possible.',
|
|
86
85
|
Date: 'Identify the *creation* date of the document. If multiple dates exist, prioritize the primary date (like invoice or letter Date). Return as object with year, month and day.',
|
|
87
86
|
};
|
|
88
|
-
export function createDataExtractionPrompt(
|
|
87
|
+
export function createDataExtractionPrompt() {
|
|
89
88
|
return promptBuilder()
|
|
90
89
|
.setSystemRole(DOCUMENT_MANAGEMENT_SYSTEM_ROLE)
|
|
91
90
|
.setRole('Structured Data Extraction Analyst')
|
|
92
91
|
.setTask('Analyze the document and extract metadata and specific properties defined in the output schema following the instructions.')
|
|
93
|
-
.
|
|
92
|
+
.enableOutputRules()
|
|
94
93
|
.addInstructions({
|
|
95
94
|
'Field Specific Instructions': dataExtractionFields,
|
|
96
95
|
'Property Extraction': orderedList([
|
|
@@ -119,7 +118,7 @@ export function createAssignCollectionPrompt() {
|
|
|
119
118
|
.setSystemRole(DOCUMENT_MANAGEMENT_SYSTEM_ROLE)
|
|
120
119
|
.setRole('Digital Filing Assistant')
|
|
121
120
|
.setTask('Select the most appropriate collections for this document from the provided list following the instructions.')
|
|
122
|
-
.
|
|
121
|
+
.setOutputExamples(ASSIGN_COLLECTION_FEW_SHOT)
|
|
123
122
|
.addInstructions({
|
|
124
123
|
'Matching Logic': orderedList([
|
|
125
124
|
'Direct Key-Match: Look for exact keyword matches between the collection name and the document metadata.',
|
|
@@ -146,7 +145,7 @@ export function createAssignRequestPrompt() {
|
|
|
146
145
|
.setSystemRole(DOCUMENT_MANAGEMENT_SYSTEM_ROLE)
|
|
147
146
|
.setRole('Workflow Routing Agent')
|
|
148
147
|
.setTask('Evaluate the document against the list of open requests and find the best match following the instructions.')
|
|
149
|
-
.
|
|
148
|
+
.setOutputExamples(ASSIGN_REQUEST_FEW_SHOT)
|
|
150
149
|
.addInstructions({
|
|
151
150
|
'Matching Rules': orderedList({
|
|
152
151
|
'Hard Constraints': 'If a Request has a "Comment" or specific property requirement, the document MUST fulfill it strictly (e.g., "Need bill from July" must match date).',
|
|
@@ -134,7 +134,7 @@ let DocumentManagementAiService = DocumentManagementAiService_1 = class Document
|
|
|
134
134
|
const override = (isNotNull(property.key) ? aiConfig.extraction?.properties?.[property.key] : undefined) ?? aiConfig.extraction?.properties?.[property.label];
|
|
135
135
|
return isDefined(override) ? mergeInstructions(`Extract value for property "${property.label}".`, [override]) : undefined;
|
|
136
136
|
}).filter(isDefined);
|
|
137
|
-
const promptBuilder = createDataExtractionPrompt(
|
|
137
|
+
const promptBuilder = createDataExtractionPrompt();
|
|
138
138
|
promptBuilder.addInstructions({
|
|
139
139
|
'Field Specific Instructions': mergedFieldInstructions,
|
|
140
140
|
'Additional Property Extraction': isDefined(mergedPropertyInstructions) && (mergedPropertyInstructions.length > 0)
|
|
@@ -26,7 +26,7 @@ export class AiValidationExecutor extends DocumentValidationExecutor {
|
|
|
26
26
|
.setSystemTask('Validate a document based on the provided validation instructions and document content.')
|
|
27
27
|
.setTask('Validate the document based on the provided system and validation instructions and the document content.')
|
|
28
28
|
.addInstructions({ 'Validation Instructions': validationInstructions })
|
|
29
|
-
.
|
|
29
|
+
.enableOutputRules()
|
|
30
30
|
.addMedia(documentContent, context.document.mimeType);
|
|
31
31
|
if (isDefined(language)) {
|
|
32
32
|
builder.setLanguage(language);
|