ts-ai-lite
v1.0.1
Published
Lightweight, type-safe array utility library for web developers
Maintainers
Readme
ts-ai-lite
Lightweight utilities for AI/LLM response handling, validation, and prompt management.
Features
- JSON schema validation helpers for LLM responses
- Safe parsing utilities for AI outputs
- Guardrails for structured AI responses
- Prompt template helpers
- Token length estimation utilities
Installation
npm install ts-ai-liteUsage
import { AiUtils } from 'ts-ai-lite';
const ai = new AiUtils();
// JSON schema validation
const schema = { type: 'object', properties: { name: { type: 'string' } } };
const isValid = ai.validateJsonResponse('{ "name": "John" }', schema);
// Safe parsing
const parsed = ai.safeParseJson('{ "key": "value" }');
// Prompt templates
const prompt = ai.fillTemplate('Hello {{name}}!', { name: 'World' });
// Token estimation
const tokens = ai.estimateTokens('Hello world');API
validateJsonResponse(jsonString: string, schema: object): boolean
Validates a JSON string against a schema.
safeParseJson(jsonString: string): object | null
Safely parses JSON without throwing errors.
fillTemplate(template: string, variables: object): string
Fills template variables in a string.
estimateTokens(text: string): number
Estimates token count for text (rough approximation).
License
MIT
