UNPKG

langextract

Version:

A TypeScript library for extracting structured and grounded information from text using LLMs

106 lines 4.26 kB
"use strict"; /** * Copyright 2025 kmbro. * * This is a TypeScript translation of the original Python LangExtract library * by Google LLC (https://github.com/google/langextract). * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ Object.defineProperty(exports, "__esModule", { value: true }); exports.GeminiSchemaImpl = exports.EXTRACTIONS_KEY = exports.ConstraintType = void 0; var ConstraintType; (function (ConstraintType) { ConstraintType["NONE"] = "none"; })(ConstraintType || (exports.ConstraintType = ConstraintType = {})); exports.EXTRACTIONS_KEY = "extractions"; class GeminiSchemaImpl { constructor(schemaDict) { this._schemaDict = schemaDict; } get schemaDict() { return this._schemaDict; } set schemaDict(schemaDict) { this._schemaDict = schemaDict; } static fromExamples(examplesData, attributeSuffix = "_attributes") { // Track attribute types for each category const extractionCategories = {}; for (const example of examplesData) { for (const extraction of example.extractions) { const category = extraction.extractionClass; if (!extractionCategories[category]) { extractionCategories[category] = {}; } if (extraction.attributes) { for (const [attrName, attrValue] of Object.entries(extraction.attributes)) { if (!extractionCategories[category][attrName]) { extractionCategories[category][attrName] = new Set(); } extractionCategories[category][attrName].add(Array.isArray(attrValue) ? "array" : "string"); } } } } const extractionProperties = {}; for (const [category, attrs] of Object.entries(extractionCategories)) { extractionProperties[category] = { type: "string" }; const attributesField = `${category}${attributeSuffix}`; const attrProperties = {}; // If no attributes were found for this category, add a default property if (Object.keys(attrs).length === 0) { attrProperties["_unused"] = { type: "string" }; } else { for (const [attrName, attrTypes] of Object.entries(attrs)) { // If we see array type, use array of strings if (attrTypes.has("array")) { attrProperties[attrName] = { type: "array", items: { type: "string" }, }; } else { attrProperties[attrName] = { type: "string" }; } } } extractionProperties[attributesField] = { type: "object", properties: attrProperties, nullable: true, }; } const extractionSchema = { type: "object", properties: extractionProperties, }; const schemaDict = { type: "object", properties: { [exports.EXTRACTIONS_KEY]: { type: "array", items: extractionSchema, }, }, required: [exports.EXTRACTIONS_KEY], }; return new GeminiSchemaImpl(schemaDict); } fromExamples(examplesData, attributeSuffix = "_attributes") { return GeminiSchemaImpl.fromExamples(examplesData, attributeSuffix); } } exports.GeminiSchemaImpl = GeminiSchemaImpl; //# sourceMappingURL=schema.js.map