UNPKG

prompt-compose

Version:

A JS library for building and managing GPT prompts documents.

300 lines (245 loc) 11.8 kB
import {equal as $cdSf9$equal} from "assert"; const $a2e9f72124fe1d27$export$57ca7e07b341709d = ()=>()=>""; const $1f9b678c01543e76$export$c84356afd4299847 = (inputStr = (0, $a2e9f72124fe1d27$export$57ca7e07b341709d)())=>(nextInputStr = (0, $a2e9f72124fe1d27$export$57ca7e07b341709d)())=>{ let str1 = inputStr; let str2 = nextInputStr; while(typeof str1 === "function")str1 = str1(); while(typeof str2 === "function")str2 = str2(); return `${str1}${str2}`; }; const $86f26681ce46095b$var$CONSTANT = { CONFIG: { dividerType: " ", dividerRepeat: 5, dividerBefore: "\n", dividerAfter: "\n", tabType: " ", tabRepeat: 4, instructionSpacing: "\n" } }; function $86f26681ce46095b$export$44487a86467333c3() { return $86f26681ce46095b$var$CONSTANT.CONFIG; } function $86f26681ce46095b$export$940a7873bb071df8(newConfig) { $86f26681ce46095b$var$CONSTANT.CONFIG = { ...$86f26681ce46095b$var$CONSTANT.CONFIG, ...newConfig }; } const $626ee866b25b0ac5$export$2e0a83ec2e27ecbb = ()=>{ const config = (0, $86f26681ce46095b$export$44487a86467333c3)(); const separator = config.dividerType; const repeatCount = config.dividerRepeat; return (0, $1f9b678c01543e76$export$c84356afd4299847)(`${config.dividerBefore}${separator.repeat(repeatCount)}${config.dividerAfter}`); }; const $4620ca763270bcb1$export$b2539bed5023c21c = (prompt)=>(input = (0, $a2e9f72124fe1d27$export$57ca7e07b341709d)())=>{ const config = (0, $86f26681ce46095b$export$44487a86467333c3)(); const TAB_TEXT = config.tabType.repeat(config.tabRepeat); let text = (0, $1f9b678c01543e76$export$c84356afd4299847)(prompt)(input); text = TAB_TEXT + text; text = text.replaceAll("\n", `\n${TAB_TEXT}`); return text; }; const $b25eb9d79a1f0663$export$f672e0b6f7222cd7 = (...strFns)=>{ return (input)=>{ const safeInput = (0, $1f9b678c01543e76$export$c84356afd4299847)()(input); return strFns.reduceRight((result, next)=>{ return (0, $1f9b678c01543e76$export$c84356afd4299847)(next)(result); }, safeInput); }; }; const $0371ef996ab63e5e$export$f361d4ae234b90f4 = (instructionTitle, instructionText)=>{ return (0, $b25eb9d79a1f0663$export$f672e0b6f7222cd7)((0, $1f9b678c01543e76$export$c84356afd4299847)(instructionTitle), (0, $1f9b678c01543e76$export$c84356afd4299847)(`:${(0, $86f26681ce46095b$export$44487a86467333c3)().instructionSpacing}`), (0, $4620ca763270bcb1$export$b2539bed5023c21c)((0, $1f9b678c01543e76$export$c84356afd4299847)(instructionText))); }; function $08dcc038d0c4345d$export$93514d28bd18d75a(dataStructureSample) { return (0, $0371ef996ab63e5e$export$f361d4ae234b90f4)(`Answer should be in a valid JSON, use the following structure`, `ResponseJSON: ${JSON.stringify(dataStructureSample)}`); } const $ae32b9e80a187aa8$export$6e2c8f0811a474ce = (instructionTitle, ...content)=>{ let instructionText; switch(content.length){ case 0: instructionText = (0, $1f9b678c01543e76$export$c84356afd4299847)(""); break; case 1: instructionText = content[0]; break; default: { // (content.length > 1) const dividedContent = []; content.forEach((cont)=>{ dividedContent.push(cont); dividedContent.push((0, $626ee866b25b0ac5$export$2e0a83ec2e27ecbb)()); }); dividedContent.pop(); instructionText = (0, $b25eb9d79a1f0663$export$f672e0b6f7222cd7)(...dividedContent); break; } } return (0, $b25eb9d79a1f0663$export$f672e0b6f7222cd7)((0, $0371ef996ab63e5e$export$f361d4ae234b90f4)(instructionTitle, instructionText), (0, $626ee866b25b0ac5$export$2e0a83ec2e27ecbb)()); }; function $8353d51fe90e9238$export$e1dae5660003ffa7(instruction) { return (0, $ae32b9e80a187aa8$export$6e2c8f0811a474ce)((0, $1f9b678c01543e76$export$c84356afd4299847)("System"), instruction); // Instruction(Statement("System"), ); } /* -1 => pending, 0 => failed 1 => passed */ function $672ac76f8afa1c4e$export$95f6b3f100346fbb(declareTests) { const testList = []; const test = (name, fn)=>{ testList.push([ name, fn, -1 ]); }; const executeTest = ()=>{ testList.forEach((test)=>{ const [name, fn] = test; try { fn(); console.log(`[Success] ${name}`); test[2] = 1; } catch (ex) { console.error(`[Failed] ${name}\n`, ex); test[2] = 0; } }); }; function checkStatus() { const expectedSuccessRuns = testList.length; const actualSuccessRuns = testList.map((test)=>test[2]).reduce((result, next)=>next + result, 0); (0, $cdSf9$equal)(actualSuccessRuns, expectedSuccessRuns); console.log("Test pass: Success"); } declareTests(test); executeTest(); checkStatus(); } (0, $86f26681ce46095b$export$940a7873bb071df8)({ tabType: " ", tabRepeat: 4, dividerType: "-", dividerRepeat: 5, dividerBefore: "\n", dividerAfter: "\n" }); (0, $672ac76f8afa1c4e$export$95f6b3f100346fbb)((test)=>{ test("Statement", ()=>{ const question = (0, $1f9b678c01543e76$export$c84356afd4299847)("How to make a website?"); (0, $cdSf9$equal)(question(), "How to make a website?"); }); test("Instruction", ()=>{ const problemInstruction = (0, $0371ef996ab63e5e$export$f361d4ae234b90f4)((0, $1f9b678c01543e76$export$c84356afd4299847)("Problem Statement"), (0, $1f9b678c01543e76$export$c84356afd4299847)("How to make a website?")); (0, $cdSf9$equal)(problemInstruction(), `Problem Statement: How to make a website?`); }); test("Section", ()=>{ const problemSection = (0, $ae32b9e80a187aa8$export$6e2c8f0811a474ce)((0, $1f9b678c01543e76$export$c84356afd4299847)("Problem Statement"), (0, $1f9b678c01543e76$export$c84356afd4299847)("How to make a website?")); (0, $cdSf9$equal)(problemSection(), `Problem Statement: How to make a website? ----- `); }); test("Format", ()=>{ const formatTheReponse = (0, $08dcc038d0c4345d$export$93514d28bd18d75a)([ { stepIndex: "<number>", stepDetails: "<string>" } ]); (0, $cdSf9$equal)(formatTheReponse(), `Answer should be in a valid JSON, use the following structure: ResponseJSON: [{"stepIndex":"<number>","stepDetails":"<string>"}]`); }); test("System", ()=>{ const systemStatement = (0, $8353d51fe90e9238$export$e1dae5660003ffa7)((0, $1f9b678c01543e76$export$c84356afd4299847)(`You are an AI assistant, you help users solve the given problem. - Your answeres are brief and to the point. - You only answer in the format given by the user. - If you don't know the answer to a question, you truthfully say that I don't know it. - You can end your answer with "END_END_END" text.`)); (0, $cdSf9$equal)(systemStatement(), `System: You are an AI assistant, you help users solve the given problem. - Your answeres are brief and to the point. - You only answer in the format given by the user. - If you don't know the answer to a question, you truthfully say that I don't know it. - You can end your answer with "END_END_END" text. ----- `); }); test("compose", ()=>{ const systemStatement = (0, $8353d51fe90e9238$export$e1dae5660003ffa7)((0, $1f9b678c01543e76$export$c84356afd4299847)(`You are an AI assistant, you help users solve the given problem. - Your answeres are brief and to the point. - You only answer in the format given by the user. - If you don't know the answer to a question, you truthfully say that I don't know it. - You can end your answer with "END_END_END" text.`)); const problemSection = (0, $0371ef996ab63e5e$export$f361d4ae234b90f4)((0, $1f9b678c01543e76$export$c84356afd4299847)("Problem Statement"), (0, $1f9b678c01543e76$export$c84356afd4299847)("How to make a website?")); const makePrompt = (0, $b25eb9d79a1f0663$export$f672e0b6f7222cd7)(systemStatement, problemSection); (0, $cdSf9$equal)(makePrompt(), `System: You are an AI assistant, you help users solve the given problem. - Your answeres are brief and to the point. - You only answer in the format given by the user. - If you don't know the answer to a question, you truthfully say that I don't know it. - You can end your answer with "END_END_END" text. ----- Problem Statement: How to make a website?`); }); test("Divider", ()=>{ const promptWithDivider = (0, $b25eb9d79a1f0663$export$f672e0b6f7222cd7)((0, $1f9b678c01543e76$export$c84356afd4299847)("Do task 1."), (0, $626ee866b25b0ac5$export$2e0a83ec2e27ecbb)(), (0, $1f9b678c01543e76$export$c84356afd4299847)("Do task 2.")); (0, $cdSf9$equal)(promptWithDivider(), `Do task 1. ----- Do task 2.`); }); test("Prompt", ()=>{ (0, $86f26681ce46095b$export$940a7873bb071df8)({ tabType: " ", tabRepeat: 4, dividerType: "", dividerRepeat: 0, dividerBefore: "\n", dividerAfter: "\n", instructionSpacing: "\n" }); const systemStatement = ()=>(0, $8353d51fe90e9238$export$e1dae5660003ffa7)(`You are an AI assistant, you help users solve the given problem. - Your answeres are brief and to the point. - You only answer in the format given by the user. - If you don't know the answer to a question, you truthfully say that I don't know it. - You can end your answer with "END_END_END" text.`); const userQuery = (problemToSolve, responseFormat)=>{ return (0, $ae32b9e80a187aa8$export$6e2c8f0811a474ce)("User", (0, $0371ef996ab63e5e$export$f361d4ae234b90f4)("Format", (0, $08dcc038d0c4345d$export$93514d28bd18d75a)(responseFormat)), problemInstruction(problemToSolve)); }; const aiResponse = (response = (0, $a2e9f72124fe1d27$export$57ca7e07b341709d)())=>{ return (0, $ae32b9e80a187aa8$export$6e2c8f0811a474ce)("AI", response); }; const problemInstruction = (problemToSolve)=>(0, $0371ef996ab63e5e$export$f361d4ae234b90f4)("Problem", problemToSolve); const responseFormat = [ { stepIndex: "<number>", stepDetails: "<string>" } ]; const getProblemSolverPromptMaker = (problemToSolve)=>{ return (0, $b25eb9d79a1f0663$export$f672e0b6f7222cd7)(systemStatement(), userQuery(problemToSolve, responseFormat), aiResponse()); }; const problemSolverPromptMaker = getProblemSolverPromptMaker("How to build a website?"); const promptGenerated = problemSolverPromptMaker(); const expectedPrompt = `System: You are an AI assistant, you help users solve the given problem. - Your answeres are brief and to the point. - You only answer in the format given by the user. - If you don't know the answer to a question, you truthfully say that I don't know it. - You can end your answer with "END_END_END" text. User: Format: Answer should be in a valid JSON, use the following structure: ResponseJSON: [{"stepIndex":"<number>","stepDetails":"<string>"}] Problem: How to build a website? AI: `; (0, $cdSf9$equal)(promptGenerated, expectedPrompt, "Post construction response doesn't match"); }); }); //# sourceMappingURL=index.js.map