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 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
/**

 * Exercise 24: Multi-Parser Content Pipeline

 *

 * Difficulty: ⭐⭐⭐⭐ (Expert)

 *

 * Goal: Build a robust content processing pipeline using multiple parsers with fallbacks

 *

 * Skills gained:

 * - Multi-parser orchestration

 * - Fallback parsing strategies

 * - Regex-based extraction

 * - Error handling and recovery

 * - Building robust production pipelines

 */

import {
    Runnable,
    PromptTemplate,
    StructuredOutputParser,
    ListOutputParser,
    RegexOutputParser
} from '../../../../src/index.js';
import {LlamaCppLLM} from '../../../../src/llm/llama-cpp-llm.js';
import {QwenChatWrapper} from "node-llama-cpp";

const CONTENT_SAMPLES = [
    {
        text: "Breaking: Stock market hits record high! NASDAQ up 2.5%, S&P 500 gains 1.8%. Tech sector leads with Apple +3.2%, Microsoft +2.9%. Analysts predict continued growth.",
        type: "news"
    },
    {
        text: "Recipe: Chocolate Chip Cookies. Ingredients: 2 cups flour, 1 cup butter, 1 cup sugar, 2 eggs, 1 tsp vanilla, 2 cups chocolate chips. Bake at 350°F for 12 minutes.",
        type: "recipe"
    },
    {
        text: "Product Review: The XPhone 15 Pro (Score: 8.5/10) - Great camera, long battery life, but expensive at $1,199. Pros: Display, Performance. Cons: Price, Weight.",
        type: "review"
    }
];

/**

 * Extract structured data from news articles

 */
async function createNewsParser() {
    const parser = new StructuredOutputParser({
        responseSchemas: [
            {
                name: "headline",
                description: "A brief, catchy headline summarizing the article",
                type: "string",
                required: true
            },
            {
                name: "category",
                description: "The primary category of the article",
                type: "string",
                enum: ["business", "technology", "politics", "sports", "other"],
                required: true
            },
            {
                name: "sentiment",
                description: "The overall sentiment or tone of the article",
                type: "string",
                enum: ["positive", "negative", "neutral"],
                required: true
            },
            {
                name: "entities",
                description: "List of notable entities mentioned in the article such as companies, people, and places",
                type: "array"
            },
            {
                name: "marketData",
                description: "Any numerical market data or statistics mentioned with their context (e.g., 'NASDAQ up 2.5%', 'unemployment at 3.8%')",
                type: "array"
            }
        ]
    });

    const prompt = new PromptTemplate({
        template: `Analyze this news article and extract structured information.



Article: {text}



{format_instructions}



Be precise and extract all relevant entities and market data mentioned.`,
        inputVariables: ["text"],
        partialVariables: {
            format_instructions: parser.getFormatInstructions()
        }
    });

    const llm = new LlamaCppLLM({
        modelPath: './models/Qwen3-1.7B-Q6_K.gguf',
        chatWrapper: new QwenChatWrapper({
            thoughts: 'discourage'
        }),
    });

    const chain = prompt.pipe(llm).pipe(parser);

    return chain;
}

/**

 * Extract recipe components using regex and list parsers

 */
async function createRecipeParser() {
    const nameParser = new RegexOutputParser({
        regex: /Recipe:\s*(.+?)\./
    });

    const cookingParser = new RegexOutputParser({
        regex: /(\d+)°F.*?(\d+)\s*minutes/
    });

    const ingredientsPrompt = new PromptTemplate({
        template: `Extract only the ingredients from this recipe as a comma-separated list. Only list the ingredients, nothing else.



Recipe: {text}



Return only the ingredients as a comma-separated list.`,
        inputVariables: ["text"]
    });

    const llm = new LlamaCppLLM({
        modelPath: './models/Qwen3-1.7B-Q6_K.gguf',
        chatWrapper: new QwenChatWrapper({
            thoughts: 'discourage'
        }),
        temperature: 0.1
    });

    const ingredientsParser = new ListOutputParser();

    class RecipeParserRunnable extends Runnable {
        async _call(input, config) {
            const text = input.text;

            try {
                const nameResult = await nameParser.parse(text);
                const name = Array.isArray(nameResult) ? nameResult[0] : nameResult;

                const ingredientsChain = ingredientsPrompt.pipe(llm).pipe(ingredientsParser);
                const ingredients = await ingredientsChain.invoke({text}, config);

                const cookingResult = await cookingParser.parse(text);
                const temperature = Array.isArray(cookingResult) && cookingResult.length >= 1 ? cookingResult[0] : null;
                const time = Array.isArray(cookingResult) && cookingResult.length >= 2 ? cookingResult[1] : null;

                return {
                    name,
                    ingredients,
                    temperature: temperature ? `${temperature}°F` : null,
                    time: time ? `${time} minutes` : null
                };
            } catch (error) {
                return {
                    name: text.match(/Recipe:\s*(.+?)\./)?.[1] || "Unknown",
                    ingredients: [],
                    temperature: text.match(/(\d+)°F/)?.[1] || null,
                    time: text.match(/(\d+)\s*minutes/)?.[1] || null,
                    error: error.message
                };
            }
        }
    }

    return new RecipeParserRunnable();
}

/**

 * Parse product reviews with fallback strategy

 * Try structured parser first, fall back to regex if it fails

 */
async function createReviewParser() {
    const llm = new LlamaCppLLM({
        modelPath: './models/Qwen3-1.7B-Q6_K.gguf',
        chatWrapper: new QwenChatWrapper({
            thoughts: 'discourage'
        }),
        temperature: 0.1
    });

    const structuredParser = new StructuredOutputParser({
        responseSchemas: [
            {
                name: "productName",
                description: "The name or model of the product being reviewed",
                type: "string",
                required: true
            },
            {
                name: "score",
                description: "The numerical rating or score given to the product (e.g., 8.5, 7/10)",
                type: "number",
                required: true
            },
            {
                name: "pros",
                description: "List of positive aspects or advantages of the product",
                type: "array",
                required: false
            },
            {
                name: "cons",
                description: "List of negative aspects or disadvantages of the product",
                type: "array",
                required: false
            },
            {
                name: "price",
                description: "The price of the product if mentioned (as a string with currency symbol, e.g., '$1,199')",
                type: "string",
                required: false
            }
        ]
    });

    const regexParser = new RegexOutputParser({
        regex: /(?:Product Review:|The)?\s*(.+?)\s*\(Score:\s*([\d.]+)(?:\/10)?\).*?\$([0-9,]+)/
    });

    class ReviewParserWithFallback extends Runnable {
        async _call(input, config) {
            const text = input.text;

            try {
                const prompt = new PromptTemplate({
                    template: `Extract review data: {text}\n\n{format_instructions}`,
                    inputVariables: ["text"],
                    partialVariables: {
                        format_instructions: structuredParser.getFormatInstructions()
                    }
                });

                const chain = prompt.pipe(llm).pipe(structuredParser);
                const result = await chain.invoke({text});

                return {
                    method: 'structured',
                    data: result
                };
            } catch (error) {
                console.warn('Structured parsing failed, using regex fallback');

                try {
                    const result = await regexParser.parse(text);

                    return {
                        method: 'regex',
                        data: {
                            productName: Array.isArray(result) && result.length > 0 ? result[0] : null,
                            score: Array.isArray(result) && result.length > 1 ? parseFloat(result[1]) : null,
                            price: Array.isArray(result) && result.length > 2 ? `$${result[2]}` : null
                        }
                    };
                } catch (regexError) {
                    console.warn('Regex parsing failed, using basic extraction');

                    const productMatch = text.match(/(?:Product Review:|The)?\s*(.+?)\s*\(/);
                    const scoreMatch = text.match(/Score:\s*([\d.]+)/);
                    const priceMatch = text.match(/\$([0-9,]+)/);

                    return {
                        method: 'basic',
                        data: {
                            productName: productMatch ? productMatch[1].trim() : null,
                            score: scoreMatch ? parseFloat(scoreMatch[1]) : null,
                            price: priceMatch ? `$${priceMatch[1]}` : null,
                            text: text
                        }
                    };
                }
            }
        }
    }

    return new ReviewParserWithFallback();
}

/**

 * Route content to appropriate parser based on content type

 */
class ContentRouter extends Runnable {
    constructor(parsers) {
        super();
        this.parsers = parsers;
    }

    async _call(input, config) {
        const {text, type} = input;

        const parser = this.parsers[type];

        if (!parser) {
            throw new Error(`No parser for content type: ${type}`);
        }

        const result = await parser.invoke({text}, config);

        return {
            type: type,
            parsed: result,
            originalText: text
        };
    }
}

async function buildContentPipeline() {
    console.log('=== Exercise 24: Multi-Parser Content Pipeline ===\n');

    const newsParser = await createNewsParser();
    const recipeParser = await createRecipeParser();
    const reviewParser = await createReviewParser();

    const router = new ContentRouter({
        news: newsParser,
        recipe: recipeParser,
        review: reviewParser
    });

    console.log('Processing content samples...\n');

    const results = [];

    for (let i = 0; i < CONTENT_SAMPLES.length; i++) {
        const sample = CONTENT_SAMPLES[i];

        console.log('='.repeat(70));
        console.log(`SAMPLE ${i + 1}: ${sample.type.toUpperCase()}`);
        console.log('='.repeat(70));
        console.log(`Text: ${sample.text}\n`);

        try {
            const result = await router.invoke({text: sample.text, type: sample.type});

            console.log('Parsing Result:');
            console.log(JSON.stringify(result, null, 2));

            results.push({
                success: true,
                data: result
            });
        } catch (error) {
            console.error(`Error: ${error.message}`);

            results.push({
                success: false,
                error: error.message,
                sample: sample
            });
        }

        console.log();
    }

    console.log('='.repeat(70));
    console.log('PROCESSING SUMMARY');
    console.log('='.repeat(70));

    const successful = results.filter(r => r.success).length;
    const failed = results.filter(r => !r.success).length;

    console.log(`Total Samples: ${results.length}`);
    console.log(`Successful: ${successful}`);
    console.log(`Failed: ${failed}`);
    console.log(`Success Rate: ${((successful / results.length) * 100).toFixed(1)}%`);

    console.log('\n✓ Exercise 24 Complete!');

    return {newsParser, recipeParser, reviewParser, router, results};
}

buildContentPipeline()
    .then(runTests)
    .catch(console.error);

async function runTests(context) {
    const {newsParser, recipeParser, reviewParser, router, results} = context;

    console.log('\n' + '='.repeat(60));
    console.log('RUNNING AUTOMATED TESTS');
    console.log('='.repeat(60) + '\n');

    const assert = (await import('assert')).default;
    let passed = 0;
    let failed = 0;

    function test(name, fn) {
        try {
            fn();
            passed++;
            console.log(`✅ ${name}`);
        } catch (error) {
            failed++;
            console.error(`❌ ${name}`);
            console.error(`   ${error.message}\n`);
        }
    }

    test('News parser created', () => {
        assert(newsParser !== null, 'Create newsParser');
        assert(newsParser instanceof Runnable, 'Should be Runnable');
    });

    test('Recipe parser created', () => {
        assert(recipeParser !== null, 'Create recipeParser');
    });

    test('Review parser created', () => {
        assert(reviewParser !== null, 'Create reviewParser');
    });

    test('Router created', () => {
        assert(router !== null, 'Create ContentRouter');
        assert(router instanceof ContentRouter, 'Should be ContentRouter instance');
    });

    test('News parser extracts structured data', async () => {
        const result = await newsParser.invoke({
            text: "Tech stocks surge: Apple up 5%, Google gains 3%"
        });
        assert(typeof result === 'object', 'Should return object');
        assert('headline' in result || 'category' in result, 'Should have headline or category');
    });

    test('Recipe parser extracts components', async () => {
        const result = await recipeParser.invoke({
            text: "Recipe: Pasta. Ingredients: noodles, sauce. Bake at 400°F for 20 minutes."
        });
        assert(typeof result === 'object', 'Should return object');
        assert('name' in result || 'ingredients' in result, 'Should have recipe components');
    });

    test('Review parser handles well-formed input', async () => {
        const result = await reviewParser.invoke({
            text: "Product XYZ (Score: 8/10) costs $99. Pros: Good. Cons: Expensive."
        });
        assert(result.method, 'Should indicate parsing method used');
        assert(result.data, 'Should have data');
    });

    test('Review parser falls back gracefully', async () => {
        const result = await reviewParser.invoke({
            text: "This is malformed data that won't parse well"
        });
        assert(result !== null, 'Should return something even on bad input');
        assert(result.method, 'Should indicate which method was used');
    });

    test('Router routes to correct parser', async () => {
        const newsResult = await router.invoke({
            text: "Breaking news story",
            type: "news"
        });
        assert(newsResult.type === 'news', 'Should preserve content type');
        assert(newsResult.parsed, 'Should have parsed data');
    });

    test('Pipeline processed all samples', () => {
        assert(results.length === CONTENT_SAMPLES.length, 'Should process all samples');
    });

    test('Pipeline has reasonable success rate', () => {
        const successRate = results.filter(r => r.success).length / results.length;
        assert(successRate >= 0.5, 'Should successfully parse at least 50% of samples');
    });

    test('Pipeline handles invalid content type', async () => {
        try {
            await router.invoke({
                text: "Some text",
                type: "invalid_type"
            });
            assert(false, 'Should throw error for invalid type');
        } catch (error) {
            assert(true, 'Correctly throws error for invalid type');
        }
    });

    console.log('\n' + '='.repeat(60));
    console.log('TEST SUMMARY');
    console.log('='.repeat(60));
    console.log(`Total: ${passed + failed}`);
    console.log(`✅ Passed: ${passed}`);
    console.log(`❌ Failed: ${failed}`);
    console.log('='.repeat(60));

    if (failed === 0) {
        console.log('\n🎉 All tests passed! You are a parser master!\n');
        console.log('📚 What you mastered:');
        console.log('  • Orchestrating multiple parser types');
        console.log('  • Implementing fallback strategies');
        console.log('  • Using RegexOutputParser for custom patterns');
        console.log('  • Building robust error handling');
        console.log('  • Creating production-ready pipelines');
        console.log('  • Routing content to appropriate parsers');
        console.log('  • Combining structured and pattern-based extraction\n');
        console.log('🚀 You are ready for production parser systems!');
    } else {
        console.log('\n⚠️  Some tests failed. Review the advanced patterns.\n');
    }
}