polyfact
Version:
<h1 align="center">PolyFact</h1>
163 lines (162 loc) • 8.33 kB
JavaScript
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
return new (P || (P = Promise))(function (resolve, reject) {
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
step((generator = generator.apply(thisArg, _arguments || [])).next());
});
};
var __generator = (this && this.__generator) || function (thisArg, body) {
var _ = { label: 0, sent: function() { if (t[0] & 1) throw t[1]; return t[1]; }, trys: [], ops: [] }, f, y, t, g;
return g = { next: verb(0), "throw": verb(1), "return": verb(2) }, typeof Symbol === "function" && (g[Symbol.iterator] = function() { return this; }), g;
function verb(n) { return function (v) { return step([n, v]); }; }
function step(op) {
if (f) throw new TypeError("Generator is already executing.");
while (g && (g = 0, op[0] && (_ = 0)), _) try {
if (f = 1, y && (t = op[0] & 2 ? y["return"] : op[0] ? y["throw"] || ((t = y["return"]) && t.call(y), 0) : y.next) && !(t = t.call(y, op[1])).done) return t;
if (y = 0, t) op = [op[0] & 2, t.value];
switch (op[0]) {
case 0: case 1: t = op; break;
case 4: _.label++; return { value: op[1], done: false };
case 5: _.label++; y = op[1]; op = [0]; continue;
case 7: op = _.ops.pop(); _.trys.pop(); continue;
default:
if (!(t = _.trys, t = t.length > 0 && t[t.length - 1]) && (op[0] === 6 || op[0] === 2)) { _ = 0; continue; }
if (op[0] === 3 && (!t || (op[1] > t[0] && op[1] < t[3]))) { _.label = op[1]; break; }
if (op[0] === 6 && _.label < t[1]) { _.label = t[1]; t = op; break; }
if (t && _.label < t[2]) { _.label = t[2]; _.ops.push(op); break; }
if (t[2]) _.ops.pop();
_.trys.pop(); continue;
}
op = body.call(thisArg, _);
} catch (e) { op = [6, e]; y = 0; } finally { f = t = 0; }
if (op[0] & 5) throw op[1]; return { value: op[0] ? op[1] : void 0, done: true };
}
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.generateWithType = void 0;
var generate_1 = require("../generate");
// The ts.io types are way too complex for me to write, I didn't want to spend 2 days fixing this so I
// decided to bypass the typechecker and throw an error at runtime if the type is not supported.
// eslint-disable-next-line @typescript-eslint/no-explicit-any
function typePartial2String(entries, indent, partial) {
var leftpad = Array(2 * (indent + 1))
.fill(" ")
.join("");
return entries
.map(function (_a) {
var key = _a[0], value = _a[1];
return [
key,
internalTsio2String(value, indent + 1),
value._desc ? " // ".concat(value._desc) : "",
];
})
.reduce(function (prev, curr) {
return "".concat(prev, "\n").concat(leftpad).concat(JSON.stringify(curr[0])).concat(partial ? "?" : "", ": ").concat(curr[1], ",").concat(curr[2]);
}, "");
}
// eslint-disable-next-line @typescript-eslint/no-explicit-any
function internalTsio2String(type, indent) {
var leftpad = Array(2 * indent)
.fill(" ")
.join("");
if (type._tag === "InterfaceType") {
return "{".concat(typePartial2String(Object.entries(type.props), indent + 1, false), "\n").concat(leftpad, "}");
}
if (type._tag === "IntersectionType") {
var res = "";
for (var t_1 in type.types) {
if (type.types[t_1]._tag === "InterfaceType" || type.types[t_1]._tag === "PartialType") {
res += typePartial2String(Object.entries(type.types[t_1].props), indent + 1, type.types[t_1]._tag === "PartialType");
}
}
return "{".concat(res, "\n").concat(leftpad, "}");
}
if (type._tag === "KeyofType") {
return type.name;
}
if (type._tag === "UnionType") {
// eslint-disable-next-line @typescript-eslint/no-explicit-any
return type.types.map(function (t) { return internalTsio2String(t, indent + 1); }).join(" | ");
}
if (type._tag === "LiteralType") {
return JSON.stringify(type.value);
}
if (type._tag === "ArrayType") {
return "[".concat(internalTsio2String(type.type, indent), "]");
}
if (type._tag === "NumberType") {
return "number";
}
if (type._tag === "StringType") {
return "string";
}
if (type._tag === "BooleanType") {
return "boolean";
}
if (type._tag === "NullType") {
return "null";
}
throw new Error("Unsupported type \"".concat(type._tag, "\".\nPlease use one of:\n\t- InterfaceType (t.type)\n\t- ArrayType (t.array)\n\t- NumberType (t.number)\n\t- StringType (t.string)\n\t- BooleanType (t.boolean)"));
}
function tsio2String(type) {
var res = JSON.parse(JSON.stringify(type));
return internalTsio2String(res, 0);
}
function generateTypedPrompt(typeFormat, task) {
return "Your goal is to write a JSON object that will accomplish a specific task.\nThe string inside the JSON must be plain text, and not contain any markdown or HTML unless explicitely mentionned in the task.\nThe JSON object should follow this type:\n```\n".concat(typeFormat, "\n``` The task you must accomplish:\n").concat(task, "\n\nPlease only provide the JSON in a single json markdown code block with the keys described above. Do not include any other text.\nPlease make sure the JSON is a single line and does not contain any newlines outside of the strings.");
}
function generateWithType(task, type, options, clientOptions) {
if (clientOptions === void 0) { clientOptions = {}; }
return __awaiter(this, void 0, void 0, function () {
var typeFormat, tokenUsage, tryCount, _a, resultJson, tu, result;
return __generator(this, function (_b) {
switch (_b.label) {
case 0:
typeFormat = tsio2String(type);
tokenUsage = { input: 0, output: 0 };
tryCount = 0;
_b.label = 1;
case 1:
if (!(tryCount < 5)) return [3 /*break*/, 4];
return [4 /*yield*/, (0, generate_1.generate)(generateTypedPrompt(typeFormat, task), options || {}, clientOptions).infos()];
case 2:
_a = _b.sent(), resultJson = _a.result, tu = _a.tokenUsage;
tokenUsage.output += tu.output;
tokenUsage.input += tu.input;
result = void 0;
try {
result = JSON.parse(resultJson
.replace("\n", "")
.replace(/^```((json)|(JSON))?/, "")
.replace(/```$/, ""));
}
catch (e) {
return [3 /*break*/, 3];
}
if (!type.is(result)) {
return [3 /*break*/, 3];
}
if (!(options === null || options === void 0 ? void 0 : options.infos)) {
return [2 /*return*/, result];
}
return [2 /*return*/, { result: result, tokenUsage: tokenUsage }];
case 3:
tryCount++;
return [3 /*break*/, 1];
case 4: throw new Error("Generation failed to match the given type after 5 retry");
}
});
});
}
exports.generateWithType = generateWithType;
function client(clientOptions) {
if (clientOptions === void 0) { clientOptions = {}; }
return {
generateWithType: function (task, type, options) { return generateWithType(task, type, options, clientOptions); },
};
}
exports.default = client;
;