UNPKG

llama-flow

Version:

The Typescript-first prompt engineering toolkit for working with chat based LLMs.

65 lines (64 loc) 2.95 kB
"use strict"; var __importDefault = (this && this.__importDefault) || function (mod) { return (mod && mod.__esModule) ? mod : { "default": mod }; }; Object.defineProperty(exports, "__esModule", { value: true }); const jsonic_1 = __importDefault(require("jsonic")); const lodash_1 = require("lodash"); const utils_1 = require("../utils"); const extracter_1 = require("./extracter"); function buildRawPrompt(prompt) { return { message: prompt.message, parse: async (response) => { try { let json; let extracted; if (!prompt.parseResponse) { const potientialArray = (0, extracter_1.extractJSONArrayResponse)(response.content); const potientialObject = (0, extracter_1.extractJSONObjectResponse)(response.content); extracted = (potientialArray?.length ?? 0) > (potientialObject?.length ?? 0) ? potientialArray : potientialObject; if (!extracted) { return { success: false, retryPrompt: prompt.retryMessage ?? 'No valid JSON was found, rewrite as valid JSON.', }; } json = (0, jsonic_1.default)(extracted); } else { json = await prompt.parseResponse(response.content); } const parsed = prompt.schema.safeParse(json); if (parsed.success) { return { success: true, data: parsed.data }; } else { utils_1.debug.error('Error parsing json:', parsed.error, extracted, json); const issuesMessage = parsed.error.issues.reduce((prev, issue) => issue.path && issue.path.length > 0 ? `${prev}\nThere is an issue with the the value "${JSON.stringify((0, lodash_1.get)(json, issue.path))}", at path ${issue.path.join('.')}. The issue is: ${issue.message}` : `\nThe issue is: ${issue.message}`, 'There is an issue with that response, please rewrite.'); return { success: false, retryPrompt: ((prompt.retryMessage ?? '') + '\n' + issuesMessage).trim(), }; } } catch (e) { utils_1.debug.error('Error building json prompt:', e); return { success: false, retryPrompt: prompt.retryMessage ?? 'No valid JSON was found, rewrite as valid JSON.', }; } }, }; } exports.default = buildRawPrompt;