@mzhub/promptc 0.1.0 → 0.1.1
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/README.md +0 -0
- package/dist/examples/load-compiled.d.ts +7 -0
- package/dist/examples/load-compiled.d.ts.map +1 -0
- package/dist/examples/load-compiled.js +44 -0
- package/dist/examples/load-compiled.js.map +1 -0
- package/dist/examples/multi-provider.d.ts +7 -0
- package/dist/examples/multi-provider.d.ts.map +1 -0
- package/dist/examples/multi-provider.js +63 -0
- package/dist/examples/multi-provider.js.map +1 -0
- package/dist/examples/name-extractor.d.ts +11 -0
- package/dist/examples/name-extractor.d.ts.map +1 -0
- package/dist/examples/name-extractor.js +85 -0
- package/dist/examples/name-extractor.js.map +1 -0
- package/dist/examples/qa-system.d.ts +8 -0
- package/dist/examples/qa-system.d.ts.map +1 -0
- package/dist/examples/qa-system.js +73 -0
- package/dist/examples/qa-system.js.map +1 -0
- package/dist/src/cli.d.ts +3 -0
- package/dist/src/cli.d.ts.map +1 -0
- package/dist/src/cli.js +111 -0
- package/dist/src/cli.js.map +1 -0
- package/dist/src/compiler/BootstrapFewShot.d.ts +13 -0
- package/dist/src/compiler/BootstrapFewShot.d.ts.map +1 -0
- package/dist/src/compiler/BootstrapFewShot.js +93 -0
- package/dist/src/compiler/BootstrapFewShot.js.map +1 -0
- package/dist/src/compiler/CandidatePool.d.ts +10 -0
- package/dist/src/compiler/CandidatePool.d.ts.map +1 -0
- package/dist/src/compiler/CandidatePool.js +29 -0
- package/dist/src/compiler/CandidatePool.js.map +1 -0
- package/dist/src/compiler/CompiledProgram.d.ts +43 -0
- package/dist/src/compiler/CompiledProgram.d.ts.map +1 -0
- package/dist/src/compiler/CompiledProgram.js +41 -0
- package/dist/src/compiler/CompiledProgram.js.map +1 -0
- package/dist/src/compiler/InstructionRewrite.d.ts +19 -0
- package/dist/src/compiler/InstructionRewrite.d.ts.map +1 -0
- package/dist/src/compiler/InstructionRewrite.js +117 -0
- package/dist/src/compiler/InstructionRewrite.js.map +1 -0
- package/dist/src/compiler/index.d.ts +8 -0
- package/dist/src/compiler/index.d.ts.map +1 -0
- package/dist/src/compiler/index.js +5 -0
- package/dist/src/compiler/index.js.map +1 -0
- package/dist/src/compiler/types.d.ts +41 -0
- package/dist/src/compiler/types.d.ts.map +1 -0
- package/dist/src/compiler/types.js +2 -0
- package/dist/src/compiler/types.js.map +1 -0
- package/dist/src/eval/exactMatch.d.ts +5 -0
- package/dist/src/eval/exactMatch.d.ts.map +1 -0
- package/dist/src/eval/exactMatch.js +58 -0
- package/dist/src/eval/exactMatch.js.map +1 -0
- package/dist/src/eval/index.d.ts +5 -0
- package/dist/src/eval/index.d.ts.map +1 -0
- package/dist/src/eval/index.js +3 -0
- package/dist/src/eval/index.js.map +1 -0
- package/dist/src/eval/llmJudge.d.ts +9 -0
- package/dist/src/eval/llmJudge.d.ts.map +1 -0
- package/dist/src/eval/llmJudge.js +33 -0
- package/dist/src/eval/llmJudge.js.map +1 -0
- package/dist/src/eval/types.d.ts +2 -0
- package/dist/src/eval/types.d.ts.map +1 -0
- package/dist/src/eval/types.js +2 -0
- package/dist/src/eval/types.js.map +1 -0
- package/dist/src/index.d.ts +14 -0
- package/dist/src/index.d.ts.map +1 -0
- package/dist/src/index.js +8 -0
- package/dist/src/index.js.map +1 -0
- package/dist/src/program/ChainOfThought.d.ts +6 -0
- package/dist/src/program/ChainOfThought.d.ts.map +1 -0
- package/dist/src/program/ChainOfThought.js +44 -0
- package/dist/src/program/ChainOfThought.js.map +1 -0
- package/dist/src/program/Predict.d.ts +6 -0
- package/dist/src/program/Predict.d.ts.map +1 -0
- package/dist/src/program/Predict.js +33 -0
- package/dist/src/program/Predict.js.map +1 -0
- package/dist/src/program/Program.d.ts +33 -0
- package/dist/src/program/Program.d.ts.map +1 -0
- package/dist/src/program/Program.js +28 -0
- package/dist/src/program/Program.js.map +1 -0
- package/dist/src/program/index.d.ts +5 -0
- package/dist/src/program/index.d.ts.map +1 -0
- package/dist/src/program/index.js +4 -0
- package/dist/src/program/index.js.map +1 -0
- package/dist/src/providers/anthropic.d.ts +10 -0
- package/dist/src/providers/anthropic.d.ts.map +1 -0
- package/dist/src/providers/anthropic.js +40 -0
- package/dist/src/providers/anthropic.js.map +1 -0
- package/dist/src/providers/cerebras.d.ts +10 -0
- package/dist/src/providers/cerebras.d.ts.map +1 -0
- package/dist/src/providers/cerebras.js +39 -0
- package/dist/src/providers/cerebras.js.map +1 -0
- package/dist/src/providers/google.d.ts +10 -0
- package/dist/src/providers/google.d.ts.map +1 -0
- package/dist/src/providers/google.js +42 -0
- package/dist/src/providers/google.js.map +1 -0
- package/dist/src/providers/groq.d.ts +10 -0
- package/dist/src/providers/groq.d.ts.map +1 -0
- package/dist/src/providers/groq.js +42 -0
- package/dist/src/providers/groq.js.map +1 -0
- package/dist/src/providers/index.d.ts +11 -0
- package/dist/src/providers/index.d.ts.map +1 -0
- package/dist/src/providers/index.js +31 -0
- package/dist/src/providers/index.js.map +1 -0
- package/dist/src/providers/ollama.d.ts +9 -0
- package/dist/src/providers/ollama.d.ts.map +1 -0
- package/dist/src/providers/ollama.js +39 -0
- package/dist/src/providers/ollama.js.map +1 -0
- package/dist/src/providers/openai.d.ts +10 -0
- package/dist/src/providers/openai.d.ts.map +1 -0
- package/dist/src/providers/openai.js +42 -0
- package/dist/src/providers/openai.js.map +1 -0
- package/dist/src/providers/types.d.ts +25 -0
- package/dist/src/providers/types.d.ts.map +1 -0
- package/dist/src/providers/types.js +2 -0
- package/dist/src/providers/types.js.map +1 -0
- package/dist/src/runtime/cache.d.ts +18 -0
- package/dist/src/runtime/cache.d.ts.map +1 -0
- package/dist/src/runtime/cache.js +45 -0
- package/dist/src/runtime/cache.js.map +1 -0
- package/dist/src/runtime/concurrency.d.ts +7 -0
- package/dist/src/runtime/concurrency.d.ts.map +1 -0
- package/dist/src/runtime/concurrency.js +14 -0
- package/dist/src/runtime/concurrency.js.map +1 -0
- package/dist/src/runtime/costTracker.d.ts +24 -0
- package/dist/src/runtime/costTracker.d.ts.map +1 -0
- package/dist/src/runtime/costTracker.js +37 -0
- package/dist/src/runtime/costTracker.js.map +1 -0
- package/dist/src/runtime/index.d.ts +9 -0
- package/dist/src/runtime/index.d.ts.map +1 -0
- package/dist/src/runtime/index.js +5 -0
- package/dist/src/runtime/index.js.map +1 -0
- package/dist/src/runtime/retry.d.ts +10 -0
- package/dist/src/runtime/retry.d.ts.map +1 -0
- package/dist/src/runtime/retry.js +39 -0
- package/dist/src/runtime/retry.js.map +1 -0
- package/dist/src/schema/defineSchema.d.ts +18 -0
- package/dist/src/schema/defineSchema.d.ts.map +1 -0
- package/dist/src/schema/defineSchema.js +27 -0
- package/dist/src/schema/defineSchema.js.map +1 -0
- package/dist/src/schema/index.d.ts +3 -0
- package/dist/src/schema/index.d.ts.map +1 -0
- package/dist/src/schema/index.js +2 -0
- package/dist/src/schema/index.js.map +1 -0
- package/examples/README.md +1 -1
- package/package.json +3 -3
- package/src/cli.ts +1 -1
package/README.md
CHANGED
|
Binary file
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"load-compiled.d.ts","sourceRoot":"","sources":["../../examples/load-compiled.ts"],"names":[],"mappings":"AAAA;;;;GAIG"}
|
|
@@ -0,0 +1,44 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Load Compiled Program Example
|
|
3
|
+
*
|
|
4
|
+
* Shows how to load a previously compiled program from JSON
|
|
5
|
+
*/
|
|
6
|
+
import { readFileSync } from "fs";
|
|
7
|
+
import { defineSchema, ChainOfThought, loadCompiledProgram, createProvider, z, } from "@mzhub/promptc";
|
|
8
|
+
// Must match the schema used during compilation
|
|
9
|
+
const NameExtractor = defineSchema({
|
|
10
|
+
description: "Extract proper names of people from text",
|
|
11
|
+
inputs: { text: z.string() },
|
|
12
|
+
outputs: { names: z.array(z.string()) },
|
|
13
|
+
});
|
|
14
|
+
async function main() {
|
|
15
|
+
// 1. Load the saved config
|
|
16
|
+
const savedJson = readFileSync("name-extractor.json", "utf-8");
|
|
17
|
+
// 2. Create the same program (schema + provider)
|
|
18
|
+
const provider = createProvider("openai", {
|
|
19
|
+
apiKey: process.env.OPENAI_API_KEY,
|
|
20
|
+
});
|
|
21
|
+
const program = new ChainOfThought(NameExtractor, provider);
|
|
22
|
+
// 3. Load the compiled program
|
|
23
|
+
const compiled = loadCompiledProgram(savedJson, program);
|
|
24
|
+
console.log("Loaded compiled program:");
|
|
25
|
+
console.log(` Strategy: ${compiled.meta.strategy}`);
|
|
26
|
+
console.log(` Score: ${(compiled.meta.score * 100).toFixed(1)}%`);
|
|
27
|
+
console.log(` Examples: ${compiled.config.fewShotExamples.length}`);
|
|
28
|
+
// 4. Use it in production
|
|
29
|
+
const testCases = [
|
|
30
|
+
"Warren Buffett is a legendary investor.",
|
|
31
|
+
"The meeting with Sarah and John went well.",
|
|
32
|
+
"No specific people were mentioned.",
|
|
33
|
+
];
|
|
34
|
+
console.log("\nRunning inference:");
|
|
35
|
+
for (const text of testCases) {
|
|
36
|
+
const result = await compiled.run({ text });
|
|
37
|
+
console.log(`\n Input: "${text}"`);
|
|
38
|
+
console.log(` Names: ${result.result.names.length > 0
|
|
39
|
+
? result.result.names.join(", ")
|
|
40
|
+
: "(none)"}`);
|
|
41
|
+
}
|
|
42
|
+
}
|
|
43
|
+
main().catch(console.error);
|
|
44
|
+
//# sourceMappingURL=load-compiled.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"load-compiled.js","sourceRoot":"","sources":["../../examples/load-compiled.ts"],"names":[],"mappings":"AAAA;;;;GAIG;AAEH,OAAO,EAAE,YAAY,EAAE,MAAM,IAAI,CAAC;AAClC,OAAO,EACL,YAAY,EACZ,cAAc,EACd,mBAAmB,EACnB,cAAc,EACd,CAAC,GACF,MAAM,gBAAgB,CAAC;AAExB,gDAAgD;AAChD,MAAM,aAAa,GAAG,YAAY,CAAC;IACjC,WAAW,EAAE,0CAA0C;IACvD,MAAM,EAAE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O,CAAC,GAAG,CAAC,eAAe,IAAI,GAAG,CAAC,CAAC;QACpC,OAAO,CAAC,GAAG,CACT,YACE,MAAM,CAAC,MAAM,CAAC,KAAK,CAAC,MAAM,GAAG,CAAC;YAC5B,CAAC,CAAC,MAAM,CAAC,MAAM,CAAC,KAAK,CAAC,IAAI,CAAC,IAAI,CAAC;YAChC,CAAC,CAAC,QACN,EAAE,CACH,CAAC;IACJ,CAAC;AACH,CAAC;AAED,IAAI,EAAE,CAAC,KAAK,CAAC,OAAO,CAAC,KAAK,CAAC,CAAC"}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"multi-provider.d.ts","sourceRoot":"","sources":["../../examples/multi-provider.ts"],"names":[],"mappings":"AAAA;;;;GAIG"}
|
|
@@ -0,0 +1,63 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Multi-Provider Example
|
|
3
|
+
*
|
|
4
|
+
* Shows how to use different LLM providers with the same program
|
|
5
|
+
*/
|
|
6
|
+
import { defineSchema, Predict, createProvider, z } from "@mzhub/promptc";
|
|
7
|
+
const SentimentAnalyzer = defineSchema({
|
|
8
|
+
description: "Analyze the sentiment of text as positive, negative, or neutral",
|
|
9
|
+
inputs: { text: z.string() },
|
|
10
|
+
outputs: {
|
|
11
|
+
sentiment: z.enum(["positive", "negative", "neutral"]),
|
|
12
|
+
confidence: z.number(),
|
|
13
|
+
},
|
|
14
|
+
});
|
|
15
|
+
async function testProvider(providerName, apiKeyEnv) {
|
|
16
|
+
const apiKey = process.env[apiKeyEnv];
|
|
17
|
+
if (!apiKey) {
|
|
18
|
+
console.log(`⏭️ Skipping ${providerName} (no ${apiKeyEnv})`);
|
|
19
|
+
return;
|
|
20
|
+
}
|
|
21
|
+
try {
|
|
22
|
+
const provider = createProvider(providerName, { apiKey });
|
|
23
|
+
const program = new Predict(SentimentAnalyzer, provider);
|
|
24
|
+
const start = Date.now();
|
|
25
|
+
const result = await program.run({
|
|
26
|
+
text: "I absolutely love this product!",
|
|
27
|
+
});
|
|
28
|
+
const latency = Date.now() - start;
|
|
29
|
+
console.log(`\n✅ ${providerName}`);
|
|
30
|
+
console.log(` Sentiment: ${result.result.sentiment}`);
|
|
31
|
+
console.log(` Confidence: ${(result.result.confidence * 100).toFixed(0)}%`);
|
|
32
|
+
console.log(` Latency: ${latency}ms`);
|
|
33
|
+
console.log(` Tokens: ${result.trace.usage.inputTokens + result.trace.usage.outputTokens}`);
|
|
34
|
+
}
|
|
35
|
+
catch (error) {
|
|
36
|
+
console.log(`\n❌ ${providerName}: ${error.message}`);
|
|
37
|
+
}
|
|
38
|
+
}
|
|
39
|
+
async function main() {
|
|
40
|
+
console.log("Testing sentiment analysis across providers...");
|
|
41
|
+
await testProvider("openai", "OPENAI_API_KEY");
|
|
42
|
+
await testProvider("anthropic", "ANTHROPIC_API_KEY");
|
|
43
|
+
await testProvider("google", "GOOGLE_API_KEY");
|
|
44
|
+
await testProvider("groq", "GROQ_API_KEY");
|
|
45
|
+
await testProvider("cerebras", "CEREBRAS_API_KEY");
|
|
46
|
+
// Ollama (local, no API key)
|
|
47
|
+
try {
|
|
48
|
+
const provider = createProvider("ollama");
|
|
49
|
+
const program = new Predict(SentimentAnalyzer, provider);
|
|
50
|
+
const start = Date.now();
|
|
51
|
+
const result = await program.run({
|
|
52
|
+
text: "I absolutely love this product!",
|
|
53
|
+
});
|
|
54
|
+
console.log(`\n✅ ollama (local)`);
|
|
55
|
+
console.log(` Sentiment: ${result.result.sentiment}`);
|
|
56
|
+
console.log(` Latency: ${Date.now() - start}ms`);
|
|
57
|
+
}
|
|
58
|
+
catch {
|
|
59
|
+
console.log(`\n⏭️ Skipping ollama (not running locally)`);
|
|
60
|
+
}
|
|
61
|
+
}
|
|
62
|
+
main().catch(console.error);
|
|
63
|
+
//# sourceMappingURL=multi-provider.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"multi-provider.js","sourceRoot":"","sources":["../../examples/multi-provider.ts"],"names":[],"mappings":"A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C,MAAM,CAAC,SAAS,EAAE,CAAC,CAAC;QACxD,OAAO,CAAC,GAAG,CAAC,eAAe,IAAI,CAAC,GAAG,EAAE,GAAG,KAAK,IAAI,CAAC,CAAC;IACrD,CAAC;IAAC,MAAM,CAAC;QACP,OAAO,CAAC,GAAG,CAAC,4CAA4C,CAAC,CAAC;IAC5D,CAAC;AACH,CAAC;AAED,IAAI,EAAE,CAAC,KAAK,CAAC,OAAO,CAAC,KAAK,CAAC,CAAC"}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"name-extractor.d.ts","sourceRoot":"","sources":["../../examples/name-extractor.ts"],"names":[],"mappings":"AAAA;;;;;;;;GAQG"}
|
|
@@ -0,0 +1,85 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Name Extractor Example
|
|
3
|
+
*
|
|
4
|
+
* This example shows how to:
|
|
5
|
+
* 1. Define a schema
|
|
6
|
+
* 2. Create a program
|
|
7
|
+
* 3. Compile with BootstrapFewShot
|
|
8
|
+
* 4. Save and use the compiled program
|
|
9
|
+
*/
|
|
10
|
+
import { writeFileSync } from "fs";
|
|
11
|
+
import { defineSchema, ChainOfThought, BootstrapFewShot, exactMatch, createProvider, createCompiledProgram, z, } from "@mzhub/promptc";
|
|
12
|
+
// 1. Define the schema
|
|
13
|
+
const NameExtractor = defineSchema({
|
|
14
|
+
description: "Extract proper names of people from text",
|
|
15
|
+
inputs: { text: z.string() },
|
|
16
|
+
outputs: { names: z.array(z.string()) },
|
|
17
|
+
});
|
|
18
|
+
// 2. Create a provider (uses OPENAI_API_KEY env var)
|
|
19
|
+
const provider = createProvider("openai", {
|
|
20
|
+
apiKey: process.env.OPENAI_API_KEY,
|
|
21
|
+
});
|
|
22
|
+
// 3. Create the program
|
|
23
|
+
const program = new ChainOfThought(NameExtractor, provider);
|
|
24
|
+
// 4. Training data
|
|
25
|
+
const trainset = [
|
|
26
|
+
{
|
|
27
|
+
input: { text: "Bill Gates founded Microsoft with Paul Allen." },
|
|
28
|
+
output: { names: ["Bill Gates", "Paul Allen"] },
|
|
29
|
+
},
|
|
30
|
+
{
|
|
31
|
+
input: { text: "Elon Musk runs Tesla and SpaceX." },
|
|
32
|
+
output: { names: ["Elon Musk"] },
|
|
33
|
+
},
|
|
34
|
+
{
|
|
35
|
+
input: { text: "Jeff Bezos started Amazon in his garage." },
|
|
36
|
+
output: { names: ["Jeff Bezos"] },
|
|
37
|
+
},
|
|
38
|
+
{
|
|
39
|
+
input: { text: "Satya Nadella is the CEO of Microsoft." },
|
|
40
|
+
output: { names: ["Satya Nadella"] },
|
|
41
|
+
},
|
|
42
|
+
{
|
|
43
|
+
input: { text: "Tim Cook leads Apple after Steve Jobs." },
|
|
44
|
+
output: { names: ["Tim Cook", "Steve Jobs"] },
|
|
45
|
+
},
|
|
46
|
+
{
|
|
47
|
+
input: { text: "Mark Zuckerberg created Facebook." },
|
|
48
|
+
output: { names: ["Mark Zuckerberg"] },
|
|
49
|
+
},
|
|
50
|
+
{
|
|
51
|
+
input: { text: "Sundar Pichai runs Google and Alphabet." },
|
|
52
|
+
output: { names: ["Sundar Pichai"] },
|
|
53
|
+
},
|
|
54
|
+
];
|
|
55
|
+
async function main() {
|
|
56
|
+
console.log("Starting compilation...\n");
|
|
57
|
+
// 5. Create compiler with evaluator
|
|
58
|
+
const compiler = new BootstrapFewShot(exactMatch());
|
|
59
|
+
// 6. Compile with progress tracking
|
|
60
|
+
const result = await compiler.compile(program, trainset, {
|
|
61
|
+
candidates: 10,
|
|
62
|
+
concurrency: 3,
|
|
63
|
+
onProgress: ({ candidatesEvaluated, totalCandidates, currentBestScore, }) => {
|
|
64
|
+
console.log(`Progress: ${candidatesEvaluated}/${totalCandidates} | Best: ${(currentBestScore * 100).toFixed(1)}%`);
|
|
65
|
+
},
|
|
66
|
+
});
|
|
67
|
+
console.log("\nCompilation complete!");
|
|
68
|
+
console.log(`Best score: ${(result.meta.score * 100).toFixed(1)}%`);
|
|
69
|
+
console.log(`Tokens used: ${result.meta.tokenUsage.totalTokens}`);
|
|
70
|
+
console.log(`API calls: ${result.meta.tokenUsage.calls}`);
|
|
71
|
+
// 7. Save the compiled config
|
|
72
|
+
writeFileSync("name-extractor.json", JSON.stringify(result, null, 2));
|
|
73
|
+
console.log("\nSaved to name-extractor.json");
|
|
74
|
+
// 8. Create a production-ready compiled program
|
|
75
|
+
const compiled = createCompiledProgram(program, result);
|
|
76
|
+
// 9. Test it
|
|
77
|
+
console.log("\nTesting compiled program:");
|
|
78
|
+
const testResult = await compiled.run({
|
|
79
|
+
text: "Jensen Huang founded NVIDIA.",
|
|
80
|
+
});
|
|
81
|
+
console.log("Input: 'Jensen Huang founded NVIDIA.'");
|
|
82
|
+
console.log("Output:", testResult.result.names);
|
|
83
|
+
}
|
|
84
|
+
main().catch(console.error);
|
|
85
|
+
//# sourceMappingURL=name-extractor.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"name-extractor.js","sourceRoot":"","sources":["../../examples/name-extractor.ts"],"names":[],"mappings":"AAAA;;;;;;;;GAQG;AAEH,OAAO,EAAE,aAAa,EAAE,MAAM,IAAI,CAAC;AACnC,OAAO,EACL,YAAY,EACZ,cAAc,EACd,gBAAgB,EAChB,UAAU,EACV,cAAc,EACd,qBAAqB,EACrB,CAAC,GACF,MAAM,gBAAgB,CAAC;AAExB,uBAAuB;AACvB,MAAM,aAAa,GAAG,YAAY,CAAC;IACjC,WAAW,EAAE,0CAA0C;IACvD,MAAM,EAAE,EAAE,IAAI,EAAE,CAAC,CAAC,MAAM,EAAE,EAAE;IAC5B,OAAO,EAAE,EAAE,KAAK,EAAE,CAAC,CAAC,KAAK,CAAC,CAAC,CAAC,MAAM,EAAE,CAAC,EAAE;CACxC,CAAC,CAAC;AAEH,qDAAqD;AACrD,MAAM,QAAQ,GAAG,cAAc,CAAC,QAAQ,EAAE;IACxC,MAAM,EAAE,OAAO,CAAC,GAAG,CAAC,cAAc;CACnC,CAAC,CAAC;AAEH,wBAAwB;AAC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AAC,KAAK,CAAC,CAAC;AAClD,CAAC;AAED,IAAI,EAAE,CAAC,KAAK,CAAC,OAAO,CAAC,KAAK,CAAC,CAAC"}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"qa-system.d.ts","sourceRoot":"","sources":["../../examples/qa-system.ts"],"names":[],"mappings":"AAAA;;;;;GAKG"}
|
|
@@ -0,0 +1,73 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* QA System with Chain of Thought
|
|
3
|
+
*
|
|
4
|
+
* Shows how to build a question-answering system
|
|
5
|
+
* that shows its reasoning before answering
|
|
6
|
+
*/
|
|
7
|
+
import { defineSchema, ChainOfThought, BootstrapFewShot, partialMatch, createProvider, z, } from "@mzhub/promptc";
|
|
8
|
+
const QASchema = defineSchema({
|
|
9
|
+
description: "Answer questions based on provided context. Think step by step.",
|
|
10
|
+
inputs: {
|
|
11
|
+
context: z.string().describe("The text containing information"),
|
|
12
|
+
question: z.string().describe("The question to answer"),
|
|
13
|
+
},
|
|
14
|
+
outputs: {
|
|
15
|
+
answer: z.string().describe("The answer extracted from context"),
|
|
16
|
+
confidence: z.number().min(0).max(1).describe("Confidence in the answer"),
|
|
17
|
+
},
|
|
18
|
+
});
|
|
19
|
+
const provider = createProvider("openai", {
|
|
20
|
+
apiKey: process.env.OPENAI_API_KEY,
|
|
21
|
+
});
|
|
22
|
+
const qaProgram = new ChainOfThought(QASchema, provider);
|
|
23
|
+
const trainset = [
|
|
24
|
+
{
|
|
25
|
+
input: {
|
|
26
|
+
context: "The Eiffel Tower was completed in 1889. It stands 330 meters tall and is located in Paris, France.",
|
|
27
|
+
question: "When was the Eiffel Tower completed?",
|
|
28
|
+
},
|
|
29
|
+
output: { answer: "1889", confidence: 0.95 },
|
|
30
|
+
},
|
|
31
|
+
{
|
|
32
|
+
input: {
|
|
33
|
+
context: "Apple Inc. was founded by Steve Jobs, Steve Wozniak, and Ronald Wayne in 1976.",
|
|
34
|
+
question: "Who founded Apple?",
|
|
35
|
+
},
|
|
36
|
+
output: {
|
|
37
|
+
answer: "Steve Jobs, Steve Wozniak, and Ronald Wayne",
|
|
38
|
+
confidence: 0.95,
|
|
39
|
+
},
|
|
40
|
+
},
|
|
41
|
+
{
|
|
42
|
+
input: {
|
|
43
|
+
context: "The speed of light is approximately 299,792 kilometers per second in a vacuum.",
|
|
44
|
+
question: "What is the speed of light?",
|
|
45
|
+
},
|
|
46
|
+
output: {
|
|
47
|
+
answer: "approximately 299,792 kilometers per second",
|
|
48
|
+
confidence: 0.9,
|
|
49
|
+
},
|
|
50
|
+
},
|
|
51
|
+
];
|
|
52
|
+
async function main() {
|
|
53
|
+
console.log("Building QA System with Chain of Thought...\n");
|
|
54
|
+
// Single inference to see the reasoning
|
|
55
|
+
const result = await qaProgram.run({
|
|
56
|
+
context: "Python was created by Guido van Rossum and first released in 1991. It emphasizes code readability.",
|
|
57
|
+
question: "Who created Python?",
|
|
58
|
+
});
|
|
59
|
+
console.log("Question: Who created Python?");
|
|
60
|
+
console.log("\nReasoning:", result.trace.reasoning);
|
|
61
|
+
console.log("Answer:", result.result.answer);
|
|
62
|
+
console.log("Confidence:", `${(result.result.confidence * 100).toFixed(0)}%`);
|
|
63
|
+
// Optional: Compile for better accuracy
|
|
64
|
+
console.log("\n--- Compiling for better accuracy ---\n");
|
|
65
|
+
const compiler = new BootstrapFewShot(partialMatch());
|
|
66
|
+
const compiled = await compiler.compile(qaProgram, trainset, {
|
|
67
|
+
candidates: 5,
|
|
68
|
+
concurrency: 2,
|
|
69
|
+
});
|
|
70
|
+
console.log(`Compilation score: ${(compiled.meta.score * 100).toFixed(1)}%`);
|
|
71
|
+
}
|
|
72
|
+
main().catch(console.error);
|
|
73
|
+
//# sourceMappingURL=qa-system.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"qa-system.js","sourceRoot":"","sources":["../../examples/qa-system.ts"],"names":[],"mappings":"AAAA;;;;;GAKG;AAEH,OAAO,EACL,YAAY,EACZ,cA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
|
package/dist/src/cli.js
ADDED
|
@@ -0,0 +1,111 @@
|
|
|
1
|
+
#!/usr/bin/env node
|
|
2
|
+
import { parseArgs } from "node:util";
|
|
3
|
+
import { readFileSync, existsSync } from "node:fs";
|
|
4
|
+
import { resolve } from "node:path";
|
|
5
|
+
const { values, positionals } = parseArgs({
|
|
6
|
+
args: process.argv.slice(2),
|
|
7
|
+
options: {
|
|
8
|
+
config: { type: "string", short: "c" },
|
|
9
|
+
output: { type: "string", short: "o", default: "compiled-prompt.json" },
|
|
10
|
+
candidates: { type: "string", default: "10" },
|
|
11
|
+
concurrency: { type: "string", default: "5" },
|
|
12
|
+
provider: { type: "string", short: "p", default: "openai" },
|
|
13
|
+
model: { type: "string", short: "m" },
|
|
14
|
+
help: { type: "boolean", short: "h" },
|
|
15
|
+
},
|
|
16
|
+
allowPositionals: true,
|
|
17
|
+
});
|
|
18
|
+
function printHelp() {
|
|
19
|
+
console.log(`
|
|
20
|
+
promptc - Type-safe LLM program compiler
|
|
21
|
+
|
|
22
|
+
Usage:
|
|
23
|
+
promptc compile <trainset.json> [options]
|
|
24
|
+
promptc validate <compiled.json>
|
|
25
|
+
|
|
26
|
+
Commands:
|
|
27
|
+
compile Compile a program using a training set
|
|
28
|
+
validate Validate a compiled prompt JSON file
|
|
29
|
+
|
|
30
|
+
Options:
|
|
31
|
+
-c, --config <file> Path to config file
|
|
32
|
+
-o, --output <file> Output file (default: compiled-prompt.json)
|
|
33
|
+
-p, --provider <name> LLM provider: openai, anthropic, google, groq, cerebras, ollama
|
|
34
|
+
-m, --model <name> Model to use (overrides provider default)
|
|
35
|
+
--candidates <n> Number of candidates to try (default: 10)
|
|
36
|
+
--concurrency <n> Parallel evaluations (default: 5)
|
|
37
|
+
-h, --help Show this help
|
|
38
|
+
|
|
39
|
+
Examples:
|
|
40
|
+
promptc compile trainset.json -p openai -o prompt.json
|
|
41
|
+
promptc validate prompt.json
|
|
42
|
+
`);
|
|
43
|
+
}
|
|
44
|
+
async function validateCommand(filePath) {
|
|
45
|
+
if (!existsSync(filePath)) {
|
|
46
|
+
console.error(`Error: File not found: ${filePath}`);
|
|
47
|
+
process.exit(1);
|
|
48
|
+
}
|
|
49
|
+
try {
|
|
50
|
+
const content = readFileSync(filePath, "utf-8");
|
|
51
|
+
const data = JSON.parse(content);
|
|
52
|
+
if (!data.meta || !data.config) {
|
|
53
|
+
console.error("Invalid format: missing 'meta' or 'config' fields");
|
|
54
|
+
process.exit(1);
|
|
55
|
+
}
|
|
56
|
+
console.log("✓ Valid compiled prompt file");
|
|
57
|
+
console.log(` Score: ${data.meta.score}`);
|
|
58
|
+
console.log(` Strategy: ${data.meta.strategy}`);
|
|
59
|
+
console.log(` Compiled: ${data.meta.compiledAt}`);
|
|
60
|
+
console.log(` Examples: ${data.config.fewShotExamples?.length || 0}`);
|
|
61
|
+
console.log(` Tokens used: ${data.meta.tokenUsage?.totalTokens || "N/A"}`);
|
|
62
|
+
}
|
|
63
|
+
catch (error) {
|
|
64
|
+
console.error("Invalid JSON file");
|
|
65
|
+
process.exit(1);
|
|
66
|
+
}
|
|
67
|
+
}
|
|
68
|
+
async function main() {
|
|
69
|
+
if (values.help || positionals.length === 0) {
|
|
70
|
+
printHelp();
|
|
71
|
+
process.exit(0);
|
|
72
|
+
}
|
|
73
|
+
const command = positionals[0];
|
|
74
|
+
switch (command) {
|
|
75
|
+
case "validate":
|
|
76
|
+
if (!positionals[1]) {
|
|
77
|
+
console.error("Error: Please provide a file to validate");
|
|
78
|
+
process.exit(1);
|
|
79
|
+
}
|
|
80
|
+
await validateCommand(resolve(positionals[1]));
|
|
81
|
+
break;
|
|
82
|
+
case "compile":
|
|
83
|
+
console.log(`
|
|
84
|
+
Note: The 'compile' command requires a custom config file that defines:
|
|
85
|
+
- Your schema (using defineSchema)
|
|
86
|
+
- Your program (Predict or ChainOfThought)
|
|
87
|
+
- Your evaluator
|
|
88
|
+
|
|
89
|
+
Example config file (promptc.config.ts):
|
|
90
|
+
|
|
91
|
+
import { defineSchema, ChainOfThought, createProvider, exactMatch, z } from '@mzhub/promptc';
|
|
92
|
+
|
|
93
|
+
export const schema = defineSchema({...});
|
|
94
|
+
export const provider = createProvider('openai', { apiKey: process.env.OPENAI_API_KEY });
|
|
95
|
+
export const program = new ChainOfThought(schema, provider);
|
|
96
|
+
export const evaluator = exactMatch();
|
|
97
|
+
|
|
98
|
+
Run with: promptc compile trainset.json -c promptc.config.ts
|
|
99
|
+
`);
|
|
100
|
+
break;
|
|
101
|
+
default:
|
|
102
|
+
console.error(`Unknown command: ${command}`);
|
|
103
|
+
printHelp();
|
|
104
|
+
process.exit(1);
|
|
105
|
+
}
|
|
106
|
+
}
|
|
107
|
+
main().catch((error) => {
|
|
108
|
+
console.error("Error:", error.message);
|
|
109
|
+
process.exit(1);
|
|
110
|
+
});
|
|
111
|
+
//# sourceMappingURL=cli.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"cli.js","sourceRoot":"","sources":["../../src/cli.ts"],"names":[],"mappings":";AAEA,OAAO,EAAE,SAAS,EAAE,MAAM,WAAW,CAAC;AACtC,OAAO,EAAE,YAAY,EAAiB,UAAU,EAAE,MAAM,SAAS,CAAC;AAClE,OAAO,EAAE,OAAO,EAAE,MAAM,WAAW,CAAC;AAEpC,MAAM,EAAE,MAAM,EAAE,WAAW,EAAE,GAAG,SAAS,CAAC;IACxC,IAAI,EAAE,OAAO,CAAC,IAAI,CAAC,KAAK,CAAC,CAAC,CAAC;IAC3B,OAAO,EAAE;QACP,MAAM,EAAE,EAAE,IAAI,EAAE,QAAQ,EAAE,KAAK,EAAE,GAAG,EAAE;QACtC,MAAM,EAAE,EAAE,IAAI,EAAE,QAAQ,EAAE,KAAK,EAAE,GAAG,EAAE,OAAO,EAAE,sBAAsB,EAAE;QACvE,UAAU,EAAE,EAAE,IAAI,EAAE,QAAQ,EAAE,OAAO,EAAE,IAAI,EAAE;QAC7C,WAAW,EAAE,EAAE,IAAI,EAAE,QAAQ,EAAE,O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C;AACH,CAAC;AAED,IAAI,EAAE,CAAC,KAAK,CAAC,CAAC,KAAK,EAAE,EAAE;IACrB,OAAO,CAAC,KAAK,CAAC,QAAQ,EAAE,KAAK,CAAC,OAAO,CAAC,CAAC;IACvC,OAAO,CAAC,IAAI,CAAC,CAAC,CAAC,CAAC;AAClB,CAAC,CAAC,CAAC"}
|
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
import { ZodRawShape } from "zod";
|
|
2
|
+
import { Program } from "../program/Program.js";
|
|
3
|
+
import type { Evaluator, Example, CompilationResult, CompileOptions } from "./types.js";
|
|
4
|
+
export declare class BootstrapFewShot<O> {
|
|
5
|
+
private evaluator;
|
|
6
|
+
constructor(evaluator: Evaluator<O>);
|
|
7
|
+
compile<I extends ZodRawShape, OShape extends ZodRawShape>(program: Program<I, OShape>, trainset: Example<unknown, O>[], options?: CompileOptions): Promise<CompilationResult<unknown, O>>;
|
|
8
|
+
estimateCost(trainsetSize: number, options?: CompileOptions): {
|
|
9
|
+
estimatedCalls: number;
|
|
10
|
+
estimatedTokens: number;
|
|
11
|
+
};
|
|
12
|
+
}
|
|
13
|
+
//# sourceMappingURL=BootstrapFewShot.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"BootstrapFewShot.d.ts","sourceRoot":"","sources":["../../../src/compiler/BootstrapFewShot.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,WAAW,EAAE,MAAM,KAAK,CAAC;AAClC,OAAO,EAAE,OAAO,EAAE,MAAM,uBAAuB,CAAC;AAIhD,OAAO,KAAK,EACV,SAAS,EACT,OAAO,EACP,iBAAiB,EACjB,cAAc,EACf,MAAM,YAAY,CAAC;AAEpB,qBAAa,gBAAgB,CAAC,CAAC;IACjB,OAAO,CAAC,SAAS;gBAAT,SAAS,EAAE,SAAS,CAAC,CAAC,CAAC;IAErC,OAAO,CAAC,CAAC,SAAS,WAAW,EAAE,MAAM,SAAS,WAAW,EAC7D,OAAO,EAAE,OAAO,CAAC,CAAC,EAAE,MAAM,CAAC,EAC3B,QAAQ,EAAE,OAAO,CAAC,OAAO,EAAE,CAAC,CAAC,EAAE,EAC/B,OAAO,GAAE,cAAmB,GAC3B,OAAO,CAAC,iBAAiB,CAAC,OAAO,EAAE,CAAC,CAAC,CAAC;IA6GzC,YAAY,CACV,YAAY,EAAE,MAAM,EACpB,OAAO,GAAE,cAAmB,GAC3B;QACD,cAAc,EAAE,MAAM,CAAC;QACvB,eAAe,EAAE,MAAM,CAAC;KACzB;CAcF"}
|
|
@@ -0,0 +1,93 @@
|
|
|
1
|
+
import { createConcurrencyManager } from "../runtime/concurrency.js";
|
|
2
|
+
import { CostTracker } from "../runtime/costTracker.js";
|
|
3
|
+
import { CandidatePool } from "./CandidatePool.js";
|
|
4
|
+
export class BootstrapFewShot {
|
|
5
|
+
evaluator;
|
|
6
|
+
constructor(evaluator) {
|
|
7
|
+
this.evaluator = evaluator;
|
|
8
|
+
}
|
|
9
|
+
async compile(program, trainset, options = {}) {
|
|
10
|
+
const { candidates: candidateCount = 10, concurrency = 5, examplesPerCandidate = 3, validationSplit = 0.3, seed, earlyStopThreshold = 0, budget, onProgress, } = options;
|
|
11
|
+
const pool = new CandidatePool(trainset, seed);
|
|
12
|
+
const candidates = pool.generateFewShotCandidates(candidateCount, examplesPerCandidate);
|
|
13
|
+
const validationSet = pool.getValidationSet(validationSplit);
|
|
14
|
+
const limiter = createConcurrencyManager(concurrency);
|
|
15
|
+
const costTracker = new CostTracker();
|
|
16
|
+
const results = [];
|
|
17
|
+
const evaluationPromises = candidates.map((fewShotSet) => limiter.run(async () => {
|
|
18
|
+
if (budget?.maxTokens && costTracker.exceedsBudget(budget.maxTokens)) {
|
|
19
|
+
budget.onBudgetWarning?.(costTracker.total.totalTokens, budget.maxTokens);
|
|
20
|
+
return { score: -1, examples: fewShotSet };
|
|
21
|
+
}
|
|
22
|
+
let totalScore = 0;
|
|
23
|
+
let validCount = 0;
|
|
24
|
+
for (const testCase of validationSet) {
|
|
25
|
+
try {
|
|
26
|
+
const prediction = await program.run(testCase.input, {
|
|
27
|
+
fewShotExamples: fewShotSet.map((ex) => ({
|
|
28
|
+
input: ex.input,
|
|
29
|
+
output: ex.output,
|
|
30
|
+
})),
|
|
31
|
+
});
|
|
32
|
+
costTracker.record(prediction.trace.usage);
|
|
33
|
+
const score = await this.evaluator(prediction.result, testCase.output);
|
|
34
|
+
totalScore += score;
|
|
35
|
+
validCount++;
|
|
36
|
+
if (earlyStopThreshold > 0 && validCount >= 2) {
|
|
37
|
+
const avgSoFar = totalScore / validCount;
|
|
38
|
+
if (avgSoFar < earlyStopThreshold) {
|
|
39
|
+
break;
|
|
40
|
+
}
|
|
41
|
+
}
|
|
42
|
+
}
|
|
43
|
+
catch {
|
|
44
|
+
continue;
|
|
45
|
+
}
|
|
46
|
+
}
|
|
47
|
+
return {
|
|
48
|
+
score: validCount > 0 ? totalScore / validCount : 0,
|
|
49
|
+
examples: fewShotSet,
|
|
50
|
+
};
|
|
51
|
+
}));
|
|
52
|
+
const candidateResults = await Promise.all(evaluationPromises);
|
|
53
|
+
let currentBestScore = -1;
|
|
54
|
+
for (const result of candidateResults) {
|
|
55
|
+
if (result.score >= 0) {
|
|
56
|
+
results.push(result);
|
|
57
|
+
if (result.score > currentBestScore) {
|
|
58
|
+
currentBestScore = result.score;
|
|
59
|
+
}
|
|
60
|
+
onProgress?.({
|
|
61
|
+
candidatesEvaluated: results.length,
|
|
62
|
+
totalCandidates: candidateCount,
|
|
63
|
+
currentBestScore,
|
|
64
|
+
tokensUsed: costTracker.total.totalTokens,
|
|
65
|
+
});
|
|
66
|
+
}
|
|
67
|
+
}
|
|
68
|
+
const winner = results.reduce((best, current) => (current.score > best.score ? current : best), { score: -1, examples: [] });
|
|
69
|
+
return {
|
|
70
|
+
meta: {
|
|
71
|
+
score: winner.score,
|
|
72
|
+
compiledAt: new Date().toISOString(),
|
|
73
|
+
strategy: "BootstrapFewShot",
|
|
74
|
+
tokenUsage: costTracker.total,
|
|
75
|
+
},
|
|
76
|
+
config: {
|
|
77
|
+
instructions: program["schema"].description,
|
|
78
|
+
fewShotExamples: winner.examples,
|
|
79
|
+
},
|
|
80
|
+
};
|
|
81
|
+
}
|
|
82
|
+
estimateCost(trainsetSize, options = {}) {
|
|
83
|
+
const { candidates = 10, validationSplit = 0.3 } = options;
|
|
84
|
+
const validationSize = Math.max(1, Math.floor(trainsetSize * validationSplit));
|
|
85
|
+
const estimatedCalls = candidates * validationSize;
|
|
86
|
+
const avgTokensPerCall = 500;
|
|
87
|
+
return {
|
|
88
|
+
estimatedCalls,
|
|
89
|
+
estimatedTokens: estimatedCalls * avgTokensPerCall,
|
|
90
|
+
};
|
|
91
|
+
}
|
|
92
|
+
}
|
|
93
|
+
//# sourceMappingURL=BootstrapFewShot.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"BootstrapFewShot.js","sourceRoot":"","sources":["../../../src/compiler/BootstrapFewShot.ts"],"names":[],"mappings":"AAEA,OAAO,EAAE,wBAAwB,EAAE,MAAM,2BAA2B,CAAC;AACrE,OAAO,EAAE,WAAW,EAAE,MAAM,2BAA2B,CAAC;AACxD,OAAO,EAAE,aAAa,EAAE,MAAM,oBAAoB,CAAC;AAQnD,MAAM,OAAO,gBAAgB;IACP;IAApB,YAAoB,SAAuB;QAAvB,cAAS,GAAT,SAAS,CAAc;IAAG,CAAC;IAE/C,KAAK,CAAC,OAAO,CACX,OAA2B,EAC3B,QAA+B,EAC/B,UAA0B,EAAE;QAE5B,MAAM,EACJ,UAAU,EAAE,cAAc,GAAG,EAAE,EAC/B,WAAW,GAAG,CAAC,EACf,oBAAoB,GAAG,CAAC,EACxB,eAAe,GAAG,GAAG,EACrB,IAAI,EACJ,kBAAkB,GAAG,CAAC,EACtB,MAAM,EACN,UAAU,GACX,GAAG,OAAO,CAAC;QAEZ,MAAM,IAAI,GAAG,IAAI,aAAa,CAAC,QAAQ,EAAE,IAAI,CAAC,CAAC;QAC/C,MA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AAO,EAAE,EAAE,CAAC,CAAC,OAAO,CAAC,KAAK,GAAG,IAAI,CAAC,KAAK,CAAC,CAAC,CAAC,OAAO,CAAC,CAAC,CAAC,IAAI,CAAC,EAChE,EAAE,KAAK,EAAE,CAAC,CAAC,EAAE,QAAQ,EAAE,EAA2B,EAAE,CACrD,CAAC;QAEF,OAAO;YACL,IAAI,EAAE;gBACJ,KAAK,EAAE,MAAM,CAAC,KAAK;gBACnB,UAAU,EAAE,IAAI,IAAI,EAAE,CAAC,WAAW,EAAE;gBACpC,QAAQ,EAAE,kBAAkB;gBAC5B,UAAU,EAAE,WAAW,CAAC,KAAK;aAC9B;YACD,MAAM,EAAE;gBACN,YAAY,EAAE,OAAO,CAAC,QAAQ,CAAC,CAAC,WAAW;gBAC3C,eAAe,EAAE,MAAM,CAAC,QAAQ;aACjC;SACF,CAAC;IACJ,CAAC;IAED,YAAY,CACV,YAAoB,EACpB,UAA0B,EAAE;QAK5B,MAAM,EAAE,UAAU,GAAG,EAAE,EAAE,eAAe,GAAG,GAAG,EAAE,GAAG,OAAO,CAAC;QAC3D,MAAM,cAAc,GAAG,IAAI,CAAC,GAAG,CAC7B,CAAC,EACD,IAAI,CAAC,KAAK,CAAC,YAAY,GAAG,eAAe,CAAC,CAC3C,CAAC;QACF,MAAM,cAAc,GAAG,UAAU,GAAG,cAAc,CAAC;QACnD,MAAM,gBAAgB,GAAG,GAAG,CAAC;QAE7B,OAAO;YACL,cAAc;YACd,eAAe,EAAE,cAAc,GAAG,gBAAgB;SACnD,CAAC;IACJ,CAAC;CACF"}
|
|
@@ -0,0 +1,10 @@
|
|
|
1
|
+
import type { Example } from "./types.js";
|
|
2
|
+
export declare class CandidatePool<I, O> {
|
|
3
|
+
private trainset;
|
|
4
|
+
private rng;
|
|
5
|
+
constructor(trainset: Example<I, O>[], seed?: number);
|
|
6
|
+
generateFewShotCandidates(count: number, examplesPerCandidate: number): Array<Example<I, O>[]>;
|
|
7
|
+
getValidationSet(splitRatio?: number): Example<I, O>[];
|
|
8
|
+
private seededRandom;
|
|
9
|
+
}
|
|
10
|
+
//# sourceMappingURL=CandidatePool.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"CandidatePool.d.ts","sourceRoot":"","sources":["../../../src/compiler/CandidatePool.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,OAAO,EAAE,MAAM,YAAY,CAAC;AAE1C,qBAAa,aAAa,CAAC,CAAC,EAAE,CAAC;IAGjB,OAAO,CAAC,QAAQ;IAF5B,OAAO,CAAC,GAAG,CAAe;gBAEN,QAAQ,EAAE,OAAO,CAAC,CAAC,EAAE,CAAC,CAAC,EAAE,EAAE,IAAI,CAAC,EAAE,MAAM;IAI5D,yBAAyB,CACvB,KAAK,EAAE,MAAM,EACb,oBAAoB,EAAE,MAAM,GAC3B,KAAK,CAAC,OAAO,CAAC,CAAC,EAAE,CAAC,CAAC,EAAE,CAAC;IAazB,gBAAgB,CAAC,UAAU,GAAE,MAAY,GAAG,OAAO,CAAC,CAAC,EAAE,CAAC,CAAC,EAAE;IAM3D,OAAO,CAAC,YAAY;CAOrB"}
|
|
@@ -0,0 +1,29 @@
|
|
|
1
|
+
export class CandidatePool {
|
|
2
|
+
trainset;
|
|
3
|
+
rng;
|
|
4
|
+
constructor(trainset, seed) {
|
|
5
|
+
this.trainset = trainset;
|
|
6
|
+
this.rng = seed !== undefined ? this.seededRandom(seed) : Math.random;
|
|
7
|
+
}
|
|
8
|
+
generateFewShotCandidates(count, examplesPerCandidate) {
|
|
9
|
+
const candidates = [];
|
|
10
|
+
for (let i = 0; i < count; i++) {
|
|
11
|
+
const shuffled = [...this.trainset].sort(() => this.rng() - 0.5);
|
|
12
|
+
candidates.push(shuffled.slice(0, Math.min(examplesPerCandidate, shuffled.length)));
|
|
13
|
+
}
|
|
14
|
+
return candidates;
|
|
15
|
+
}
|
|
16
|
+
getValidationSet(splitRatio = 0.3) {
|
|
17
|
+
const count = Math.max(1, Math.floor(this.trainset.length * splitRatio));
|
|
18
|
+
const shuffled = [...this.trainset].sort(() => this.rng() - 0.5);
|
|
19
|
+
return shuffled.slice(0, count);
|
|
20
|
+
}
|
|
21
|
+
seededRandom(seed) {
|
|
22
|
+
let s = seed;
|
|
23
|
+
return () => {
|
|
24
|
+
s = Math.sin(s) * 10000;
|
|
25
|
+
return s - Math.floor(s);
|
|
26
|
+
};
|
|
27
|
+
}
|
|
28
|
+
}
|
|
29
|
+
//# sourceMappingURL=CandidatePool.js.map
|