zon-format
Version:
ZON: The most token-efficient serialization format for LLMs - beats CSV, TOON, JSON, and all competitors
61 lines (60 loc) • 2.14 kB
JavaScript
;
var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.ZOpenAI = void 0;
exports.createZOpenAI = createZOpenAI;
const openai_1 = __importDefault(require("openai"));
const index_1 = require("../index");
/**
* Wrapper for OpenAI SDK to automatically handle ZON format.
*/
class ZOpenAI {
constructor(client) {
this.client = client;
}
/**
* Sends a chat completion request and parses the response as ZON.
* Automatically appends ZON format instructions to the system prompt.
*/
async chat(params) {
var _a, _b;
const messages = [...params.messages];
const systemMsg = messages.find(m => m.role === 'system');
const instructions = `\n\nRESPONSE FORMAT: You must respond in ZON (Zero Overhead Notation).
Rules:
1. Use 'key:value' for properties.
2. Use 'key{...}' for nested objects.
3. Use 'key[...]' for arrays.
4. Use '@(N):col1,col2' for tables.
5. Use 'T'/'F' for booleans, 'null' for null.
6. Do NOT wrap in markdown code blocks.`;
if (systemMsg) {
if (typeof systemMsg.content === 'string') {
systemMsg.content += instructions;
}
else {
messages.push({ role: 'system', content: instructions });
}
}
else {
messages.unshift({ role: 'system', content: instructions });
}
const response = await this.client.chat.completions.create({
...params,
messages,
stream: false
});
const content = ((_b = (_a = response.choices[0]) === null || _a === void 0 ? void 0 : _a.message) === null || _b === void 0 ? void 0 : _b.content) || '';
const cleaned = content.replace(/```(zon|zonf)?/g, "").trim();
return (0, index_1.decode)(cleaned);
}
}
exports.ZOpenAI = ZOpenAI;
/**
* Helper to create a ZOpenAI instance.
*/
function createZOpenAI(apiKey) {
return new ZOpenAI(new openai_1.default({ apiKey }));
}