baml-sap-ts 0.1.0
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/CHANGELOG.md +36 -0
- package/LICENSE +21 -0
- package/README.md +355 -0
- package/dist/index.d.ts +180 -0
- package/dist/index.d.ts.map +1 -0
- package/dist/index.js +274 -0
- package/dist/index.js.map +1 -0
- package/dist/json-extractor.d.ts +57 -0
- package/dist/json-extractor.d.ts.map +1 -0
- package/dist/json-extractor.js +382 -0
- package/dist/json-extractor.js.map +1 -0
- package/dist/schema-renderer.d.ts +34 -0
- package/dist/schema-renderer.d.ts.map +1 -0
- package/dist/schema-renderer.js +329 -0
- package/dist/schema-renderer.js.map +1 -0
- package/dist/type-coercer.d.ts +59 -0
- package/dist/type-coercer.d.ts.map +1 -0
- package/dist/type-coercer.js +589 -0
- package/dist/type-coercer.js.map +1 -0
- package/package.json +79 -0
package/dist/index.js
ADDED
|
@@ -0,0 +1,274 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
/**
|
|
3
|
+
* BAML SAP TypeScript Migration
|
|
4
|
+
*
|
|
5
|
+
* Schema-Aligned Parsing (SAP) for TypeScript using TypeBox
|
|
6
|
+
*
|
|
7
|
+
* This is a TypeScript port of BAML's core algorithm for structured
|
|
8
|
+
* data extraction from LLM responses.
|
|
9
|
+
*
|
|
10
|
+
* Usage:
|
|
11
|
+
* ```typescript
|
|
12
|
+
* import { Type } from '@sinclair/typebox';
|
|
13
|
+
* import { createPromptWithSchema, parseResponse } from 'baml-sap-ts';
|
|
14
|
+
*
|
|
15
|
+
* const schema = Type.Object({
|
|
16
|
+
* name: Type.String(),
|
|
17
|
+
* age: Type.Number(),
|
|
18
|
+
* });
|
|
19
|
+
*
|
|
20
|
+
* // Create a prompt with schema instructions
|
|
21
|
+
* const prompt = createPromptWithSchema(
|
|
22
|
+
* 'Extract user information from: John is 25 years old',
|
|
23
|
+
* schema
|
|
24
|
+
* );
|
|
25
|
+
*
|
|
26
|
+
* // After getting LLM response, parse it
|
|
27
|
+
* const response = `{ "name": "John", "age": 25 }`;
|
|
28
|
+
* const result = parseResponse(response, schema);
|
|
29
|
+
*
|
|
30
|
+
* if (result.success) {
|
|
31
|
+
* console.log(result.value); // { name: "John", age: 25 }
|
|
32
|
+
* }
|
|
33
|
+
* ```
|
|
34
|
+
*/
|
|
35
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
36
|
+
exports.validateValue = exports.coerceValue = exports.renderSchema = exports.createJsonSchemaPrompt = exports.hasChainOfThought = exports.filterChainOfThought = exports.extractJson = exports.extractAllCandidates = void 0;
|
|
37
|
+
exports.parseResponse = parseResponse;
|
|
38
|
+
exports.parsePartialResponse = parsePartialResponse;
|
|
39
|
+
exports.createPromptWithSchema = createPromptWithSchema;
|
|
40
|
+
exports.parseAllCandidates = parseAllCandidates;
|
|
41
|
+
exports.parseBestCandidate = parseBestCandidate;
|
|
42
|
+
exports.isValidResponse = isValidResponse;
|
|
43
|
+
exports.debugParse = debugParse;
|
|
44
|
+
const json_extractor_js_1 = require("./json-extractor.js");
|
|
45
|
+
const schema_renderer_js_1 = require("./schema-renderer.js");
|
|
46
|
+
const type_coercer_js_1 = require("./type-coercer.js");
|
|
47
|
+
var json_extractor_js_2 = require("./json-extractor.js");
|
|
48
|
+
Object.defineProperty(exports, "extractAllCandidates", { enumerable: true, get: function () { return json_extractor_js_2.extractAllCandidates; } });
|
|
49
|
+
Object.defineProperty(exports, "extractJson", { enumerable: true, get: function () { return json_extractor_js_2.extractJson; } });
|
|
50
|
+
Object.defineProperty(exports, "filterChainOfThought", { enumerable: true, get: function () { return json_extractor_js_2.filterChainOfThought; } });
|
|
51
|
+
Object.defineProperty(exports, "hasChainOfThought", { enumerable: true, get: function () { return json_extractor_js_2.hasChainOfThought; } });
|
|
52
|
+
// Re-export types and functions
|
|
53
|
+
var schema_renderer_js_2 = require("./schema-renderer.js");
|
|
54
|
+
Object.defineProperty(exports, "createJsonSchemaPrompt", { enumerable: true, get: function () { return schema_renderer_js_2.createJsonSchemaPrompt; } });
|
|
55
|
+
Object.defineProperty(exports, "renderSchema", { enumerable: true, get: function () { return schema_renderer_js_2.renderSchema; } });
|
|
56
|
+
var type_coercer_js_2 = require("./type-coercer.js");
|
|
57
|
+
Object.defineProperty(exports, "coerceValue", { enumerable: true, get: function () { return type_coercer_js_2.coerceValue; } });
|
|
58
|
+
Object.defineProperty(exports, "validateValue", { enumerable: true, get: function () { return type_coercer_js_2.validateValue; } });
|
|
59
|
+
const defaultParseOptions = {
|
|
60
|
+
// Extraction defaults
|
|
61
|
+
allowMarkdownJson: true,
|
|
62
|
+
allowFixes: true,
|
|
63
|
+
allowAsString: true,
|
|
64
|
+
findAllJsonObjects: true,
|
|
65
|
+
maxDepth: 100,
|
|
66
|
+
// Coercion defaults
|
|
67
|
+
allowPartials: false,
|
|
68
|
+
useDefaults: true,
|
|
69
|
+
strict: false,
|
|
70
|
+
trackCoercions: false,
|
|
71
|
+
// Parse defaults
|
|
72
|
+
filterChainOfThought: true,
|
|
73
|
+
returnAllCandidates: false,
|
|
74
|
+
};
|
|
75
|
+
/**
|
|
76
|
+
* Parse an LLM response into a typed value
|
|
77
|
+
*
|
|
78
|
+
* This is the main entry point for the SAP algorithm.
|
|
79
|
+
* It handles:
|
|
80
|
+
* 1. Chain-of-thought filtering
|
|
81
|
+
* 2. JSON extraction from markdown/text
|
|
82
|
+
* 3. Type coercion and validation
|
|
83
|
+
*
|
|
84
|
+
* @param response - The raw LLM response text
|
|
85
|
+
* @param schema - TypeBox schema to validate against
|
|
86
|
+
* @param options - Parsing options
|
|
87
|
+
* @returns ParseResult with the typed value or errors
|
|
88
|
+
*/
|
|
89
|
+
function parseResponse(response, schema, options = {}) {
|
|
90
|
+
const opts = { ...defaultParseOptions, ...options };
|
|
91
|
+
// Step 1: Filter chain-of-thought if needed
|
|
92
|
+
let text = response;
|
|
93
|
+
let chainOfThoughtFiltered = false;
|
|
94
|
+
if (opts.filterChainOfThought && (0, json_extractor_js_1.hasChainOfThought)(response)) {
|
|
95
|
+
text = (0, json_extractor_js_1.filterChainOfThought)(response);
|
|
96
|
+
chainOfThoughtFiltered = text !== response;
|
|
97
|
+
}
|
|
98
|
+
// Step 2: Extract JSON
|
|
99
|
+
let extraction;
|
|
100
|
+
try {
|
|
101
|
+
extraction = (0, json_extractor_js_1.extractJson)(text, opts, true, 0);
|
|
102
|
+
}
|
|
103
|
+
catch (error) {
|
|
104
|
+
return {
|
|
105
|
+
success: false,
|
|
106
|
+
value: undefined,
|
|
107
|
+
errors: [
|
|
108
|
+
{
|
|
109
|
+
path: "",
|
|
110
|
+
message: `JSON extraction failed: ${error instanceof Error ? error.message : String(error)}`,
|
|
111
|
+
},
|
|
112
|
+
],
|
|
113
|
+
isPartial: false,
|
|
114
|
+
meta: {
|
|
115
|
+
raw: response,
|
|
116
|
+
chainOfThoughtFiltered,
|
|
117
|
+
},
|
|
118
|
+
};
|
|
119
|
+
}
|
|
120
|
+
// Step 3: Coerce to schema
|
|
121
|
+
const coercion = (0, type_coercer_js_1.coerceValue)(extraction.value, schema, opts);
|
|
122
|
+
return {
|
|
123
|
+
success: coercion.success,
|
|
124
|
+
value: coercion.value,
|
|
125
|
+
errors: coercion.errors.map((e) => ({ path: e.path, message: e.message })),
|
|
126
|
+
isPartial: coercion.isPartial || false,
|
|
127
|
+
meta: {
|
|
128
|
+
raw: response,
|
|
129
|
+
fromMarkdown: extraction.fromMarkdown,
|
|
130
|
+
chainOfThoughtFiltered,
|
|
131
|
+
fixes: extraction.fixes,
|
|
132
|
+
coercions: coercion.coercions,
|
|
133
|
+
},
|
|
134
|
+
};
|
|
135
|
+
}
|
|
136
|
+
/**
|
|
137
|
+
* Stream parser for handling partial LLM responses
|
|
138
|
+
*
|
|
139
|
+
* This is useful for streaming scenarios where you want to
|
|
140
|
+
* show partial results as they arrive.
|
|
141
|
+
*
|
|
142
|
+
* @param partialResponse - The partial response text so far
|
|
143
|
+
* @param schema - TypeBox schema to validate against
|
|
144
|
+
* @param options - Parsing options
|
|
145
|
+
* @returns ParseResult with the best-effort typed value
|
|
146
|
+
*/
|
|
147
|
+
function parsePartialResponse(partialResponse, schema, options = {}) {
|
|
148
|
+
const opts = {
|
|
149
|
+
...defaultParseOptions,
|
|
150
|
+
...options,
|
|
151
|
+
allowPartials: true,
|
|
152
|
+
allowAsString: true,
|
|
153
|
+
};
|
|
154
|
+
return parseResponse(partialResponse, schema, opts);
|
|
155
|
+
}
|
|
156
|
+
/**
|
|
157
|
+
* Create a prompt with schema instructions
|
|
158
|
+
*
|
|
159
|
+
* Appends schema instructions to a base prompt to guide
|
|
160
|
+
* the LLM toward producing correctly structured output.
|
|
161
|
+
*
|
|
162
|
+
* @param basePrompt - The base prompt text
|
|
163
|
+
* @param schema - TypeBox schema describing expected output
|
|
164
|
+
* @param options - Schema rendering options
|
|
165
|
+
* @returns Complete prompt with schema instructions
|
|
166
|
+
*/
|
|
167
|
+
function createPromptWithSchema(basePrompt, schema, options) {
|
|
168
|
+
return (0, schema_renderer_js_1.createPromptWithSchema)(basePrompt, schema, options);
|
|
169
|
+
}
|
|
170
|
+
/**
|
|
171
|
+
* Parse multiple candidates from a response
|
|
172
|
+
*
|
|
173
|
+
* Useful when the LLM might have provided multiple JSON objects
|
|
174
|
+
* and you want to try them all.
|
|
175
|
+
*
|
|
176
|
+
* @param response - The raw LLM response text
|
|
177
|
+
* @param schema - TypeBox schema to validate against
|
|
178
|
+
* @param options - Parsing options
|
|
179
|
+
* @returns Array of ParseResults for each candidate
|
|
180
|
+
*/
|
|
181
|
+
function parseAllCandidates(response, schema, options = {}) {
|
|
182
|
+
const { extractAllCandidates } = require("./json-extractor.js");
|
|
183
|
+
const candidates = extractAllCandidates(response);
|
|
184
|
+
const results = [];
|
|
185
|
+
for (const candidate of candidates) {
|
|
186
|
+
try {
|
|
187
|
+
const result = parseResponse(candidate, schema, options);
|
|
188
|
+
results.push(result);
|
|
189
|
+
}
|
|
190
|
+
catch {
|
|
191
|
+
// Skip invalid candidates
|
|
192
|
+
}
|
|
193
|
+
}
|
|
194
|
+
return results;
|
|
195
|
+
}
|
|
196
|
+
/**
|
|
197
|
+
* Get the best candidate from multiple options
|
|
198
|
+
*
|
|
199
|
+
* Returns the candidate with the fewest errors.
|
|
200
|
+
*
|
|
201
|
+
* @param response - The raw LLM response text
|
|
202
|
+
* @param schema - TypeBox schema to validate against
|
|
203
|
+
* @param options - Parsing options
|
|
204
|
+
* @returns The best ParseResult or null if none valid
|
|
205
|
+
*/
|
|
206
|
+
function parseBestCandidate(response, schema, options = {}) {
|
|
207
|
+
const candidates = parseAllCandidates(response, schema, options);
|
|
208
|
+
if (candidates.length === 0) {
|
|
209
|
+
return null;
|
|
210
|
+
}
|
|
211
|
+
// Sort by number of errors (ascending)
|
|
212
|
+
candidates.sort((a, b) => a.errors.length - b.errors.length);
|
|
213
|
+
return candidates[0];
|
|
214
|
+
}
|
|
215
|
+
/**
|
|
216
|
+
* Check if a response is valid against a schema without full parsing
|
|
217
|
+
*
|
|
218
|
+
* @param response - The raw LLM response text
|
|
219
|
+
* @param schema - TypeBox schema to validate against
|
|
220
|
+
* @returns Whether the response is valid
|
|
221
|
+
*/
|
|
222
|
+
function isValidResponse(response, schema) {
|
|
223
|
+
try {
|
|
224
|
+
const result = parseResponse(response, schema);
|
|
225
|
+
return result.success;
|
|
226
|
+
}
|
|
227
|
+
catch {
|
|
228
|
+
return false;
|
|
229
|
+
}
|
|
230
|
+
}
|
|
231
|
+
/**
|
|
232
|
+
* Debug helper to see what the parser is doing
|
|
233
|
+
*
|
|
234
|
+
* @param response - The raw LLM response text
|
|
235
|
+
* @param schema - TypeBox schema to validate against
|
|
236
|
+
* @returns Debug information
|
|
237
|
+
*/
|
|
238
|
+
function debugParse(response, schema) {
|
|
239
|
+
const filteredText = (0, json_extractor_js_1.hasChainOfThought)(response) ? (0, json_extractor_js_1.filterChainOfThought)(response) : response;
|
|
240
|
+
let extractedValue = null;
|
|
241
|
+
const extractionErrors = [];
|
|
242
|
+
try {
|
|
243
|
+
const extraction = (0, json_extractor_js_1.extractJson)(filteredText, {}, true, 0);
|
|
244
|
+
extractedValue = extraction.value;
|
|
245
|
+
}
|
|
246
|
+
catch (error) {
|
|
247
|
+
extractionErrors.push(String(error));
|
|
248
|
+
}
|
|
249
|
+
const coercionResult = (0, type_coercer_js_1.coerceValue)(extractedValue ?? filteredText, schema, { trackCoercions: true });
|
|
250
|
+
return {
|
|
251
|
+
filteredText,
|
|
252
|
+
extractedValue,
|
|
253
|
+
extractionErrors,
|
|
254
|
+
coercionResult,
|
|
255
|
+
};
|
|
256
|
+
}
|
|
257
|
+
// Default export
|
|
258
|
+
exports.default = {
|
|
259
|
+
parseResponse,
|
|
260
|
+
parsePartialResponse,
|
|
261
|
+
createPromptWithSchema,
|
|
262
|
+
parseAllCandidates,
|
|
263
|
+
parseBestCandidate,
|
|
264
|
+
isValidResponse,
|
|
265
|
+
debugParse,
|
|
266
|
+
renderSchema: schema_renderer_js_1.renderSchema,
|
|
267
|
+
createJsonSchemaPrompt: schema_renderer_js_1.createJsonSchemaPrompt,
|
|
268
|
+
extractJson: json_extractor_js_1.extractJson,
|
|
269
|
+
filterChainOfThought: json_extractor_js_1.filterChainOfThought,
|
|
270
|
+
hasChainOfThought: json_extractor_js_1.hasChainOfThought,
|
|
271
|
+
coerceValue: type_coercer_js_1.coerceValue,
|
|
272
|
+
validateValue: type_coercer_js_1.validateValue,
|
|
273
|
+
};
|
|
274
|
+
//# sourceMappingURL=index.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.js","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":";AAAA;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;GAgCG;;;AAuGH,sCAsDC;AAaD,oDAaC;AAaD,wDAEC;AAaD,gDAoBC;AAYD,gDAeC;AASD,0CAOC;AASD,gCA6BC;AArTD,2DAM6B;AAC7B,6DAK8B;AAC9B,uDAA0G;AAE1G,yDAK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tB,WAAW,EAAX,+BAAW;IACX,oBAAoB,EAApB,wCAAoB;IACpB,iBAAiB,EAAjB,qCAAiB;IACjB,WAAW,EAAX,6BAAW;IACX,aAAa,EAAb,+BAAa;CACb,CAAC"}
|
|
@@ -0,0 +1,57 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* JSON Extractor - Extracts JSON from LLM responses
|
|
3
|
+
*
|
|
4
|
+
* Handles:
|
|
5
|
+
* - Markdown code block extraction
|
|
6
|
+
* - Multiple JSON objects
|
|
7
|
+
* - JSON fixing (trailing commas, missing quotes)
|
|
8
|
+
* - Chain-of-thought text filtering
|
|
9
|
+
*
|
|
10
|
+
* Based on BAML's jsonish parser
|
|
11
|
+
*/
|
|
12
|
+
export interface ExtractionOptions {
|
|
13
|
+
/** Allow extracting JSON from markdown code blocks */
|
|
14
|
+
allowMarkdownJson?: boolean;
|
|
15
|
+
/** Attempt to fix malformed JSON */
|
|
16
|
+
allowFixes?: boolean;
|
|
17
|
+
/** Return raw string if all else fails */
|
|
18
|
+
allowAsString?: boolean;
|
|
19
|
+
/** Find all JSON objects in the text */
|
|
20
|
+
findAllJsonObjects?: boolean;
|
|
21
|
+
/** Maximum recursion depth for nested parsing */
|
|
22
|
+
maxDepth?: number;
|
|
23
|
+
}
|
|
24
|
+
/**
|
|
25
|
+
* Result of JSON extraction
|
|
26
|
+
*/
|
|
27
|
+
export interface ExtractionResult {
|
|
28
|
+
/** The extracted value(s) */
|
|
29
|
+
value: unknown;
|
|
30
|
+
/** The raw text that was parsed */
|
|
31
|
+
raw: string;
|
|
32
|
+
/** Whether this was from a markdown code block */
|
|
33
|
+
fromMarkdown?: boolean;
|
|
34
|
+
/** Fixes that were applied */
|
|
35
|
+
fixes?: string[];
|
|
36
|
+
/** Whether this is a partial/incomplete result (for streaming) */
|
|
37
|
+
isPartial?: boolean;
|
|
38
|
+
}
|
|
39
|
+
/**
|
|
40
|
+
* Extract JSON from an LLM response string
|
|
41
|
+
*/
|
|
42
|
+
export declare function extractJson(text: string, options?: ExtractionOptions, isDone?: boolean, depth?: number): ExtractionResult;
|
|
43
|
+
/**
|
|
44
|
+
* Filter out chain-of-thought reasoning text
|
|
45
|
+
* Returns the text that appears after reasoning markers
|
|
46
|
+
*/
|
|
47
|
+
export declare function filterChainOfThought(text: string): string;
|
|
48
|
+
/**
|
|
49
|
+
* Extract all candidate JSON strings from text
|
|
50
|
+
* Useful for debugging and multiple-choice scenarios
|
|
51
|
+
*/
|
|
52
|
+
export declare function extractAllCandidates(text: string): string[];
|
|
53
|
+
/**
|
|
54
|
+
* Check if a response looks like it contains chain-of-thought reasoning
|
|
55
|
+
*/
|
|
56
|
+
export declare function hasChainOfThought(text: string): boolean;
|
|
57
|
+
//# sourceMappingURL=json-extractor.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"json-extractor.d.ts","sourceRoot":"","sources":["../src/json-extractor.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;GAUG;AAEH,MAAM,WAAW,iBAAiB;IACjC,sDAAsD;IACtD,iBAAiB,CAAC,EAAE,OAAO,CAAC;IAC5B,oCAAoC;IACpC,UAAU,CAAC,EAAE,OAAO,CAAC;IACrB,0CAA0C;IAC1C,aAAa,CAAC,EAAE,OAAO,CAAC;IACxB,wCAAwC;IACxC,kBAAkB,CAAC,EAAE,OAAO,CAAC;IAC7B,iDAAiD;IACjD,QAAQ,CAAC,EAAE,MAAM,CAAC;CAClB;AAUD;;GAEG;AACH,MAAM,WAAW,gBAAgB;IAChC,6BAA6B;IAC7B,KAAK,EAAE,OAAO,CAAC;IACf,mCAAmC;IACnC,GAAG,EAAE,MAAM,CAAC;IACZ,kDAAkD;IAClD,YAAY,CAAC,EAAE,OAAO,CAAC;IACvB,8BAA8B;IAC9B,KAAK,CAAC,EAAE,MAAM,EAAE,CAAC;IACjB,kEAAkE;IAClE,SAAS,CAAC,EAAE,OAAO,CAAC;CACpB;AAED;;GAEG;AACH,wBAAgB,WAAW,CAC1B,IAAI,EAAE,MAAM,EACZ,OAAO,GAAE,iBAAsB,EAC/B,MAAM,GAAE,OAAc,EACtB,KAAK,GAAE,MAAU,GACf,gBAAgB,CAgDlB;AA2SD;;;GAGG;AACH,wBAAgB,oBAAoB,CAAC,IAAI,EAAE,MAAM,GAAG,MAAM,CAqBzD;AAED;;;GAGG;AACH,wBAAgB,oBAAoB,CAAC,IAAI,EAAE,MAAM,GAAG,MAAM,EAAE,CAqB3D;AAED;;GAEG;AACH,wBAAgB,iBAAiB,CAAC,IAAI,EAAE,MAAM,GAAG,OAAO,CAavD"}
|