patbat-codegen
v0.0.7
Published
Simple template for typescript packages
Downloads
466
Readme
PatBat - Constrained Code Generation
Tool for generating constrained code with TypeScript schema validation.
Installation
You can install the package using npm or bun:
# Using npm
npm install patbat-codegen
# Using bun
bun add patbat-codegen
CLI Usage
The CLI can be run directly using bun:
bun x patbat-codegen -i input.json -o output.json
CLI Options
-i, --input <path>
- Input JSON file path (required)-o, --output <path>
- Output JSON file path (optional, defaults to<input>_output.json
)-c, --config <path>
- Config JSON file path (optional)-m, --model <model>
- Override model specified in input file (optional)--no-validate
- Skip input validation (optional)--return-code
- Include the generated code in output (optional)
Package Usage
TypeScript/JavaScript
import { generateCode } from "patbat-codegen";
// Basic usage
const result = await generateCode({
question: "What is the average age?",
model: "claude-3-5-haiku-20241022",
sampling: {
maxTokens: 30,
minPerFile: 1
},
input: {
schema: "z.object({ name: z.string(), age: z.number(), city: z.string() })",
data: [
{ "name": "Alice", "age": 25, "city": "New York" },
{ "name": "Bob", "age": 30, "city": "San Francisco" }
]
},
output: {
schema: "z.object({ averageAge: z.string(), oldestPerson: z.string() })"
}
});
// With config options
const resultWithCode = await generateCode({
// ... same input as above
}, {
validateInput: true, // Validate input data against schema (default: true)
returnCode: true // Include generated code in output (default: false)
});
console.log(result);
Input Schema
type CodegenInput = {
question: string;
model: ModelTypeEnum;
apiKey?: string;
sampling?: {
maxTokens: number;
minPerFile: number;
};
input: {
schema: string; // Zod schema as string
data: any[]; // Array of data objects
};
output: {
schema: string; // Expected output Zod schema
};
}
// Available models
type ModelTypeEnum =
| "claude-3-haiku-20240307"
| "claude-3-sonnet-20240229"
| "claude-3-opus-20240229"
| "claude-3-5-sonnet-20240620"
| "claude-3-5-sonnet-20241022"
| "claude-3-5-haiku-20241022";
Output Schema
type CodegenOutput = {
requestId: string;
requestedAt: string;
output: any; // Validated output matching output schema
code?: string; // Generated code when returnCode=true
llmUsage: {
input_tokens: number;
output_tokens: number;
};
performanceTimeStamps: {
startLLMCallPrep: number;
endLLMCallPrep: number;
startLLMCall: number;
endLLMCall: number;
startCodeRun: number;
endCodeRun: number;
responseReady: number;
};
model: ModelTypeEnum;
}
Development
Clone the repo and install dependencies:
git clone <repository-url>
cd patbat-codegen
bun install
Available Scripts
bun run build
- Build the packagebun run cli
- Run the CLI directly from source
License
Private use license - see LICENSE.md for details.