langextract
Version:
A TypeScript library for extracting structured and grounded information from text using LLMs
166 lines • 6.19 kB
JavaScript
;
/**
* 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.
*/
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
if (k2 === undefined) k2 = k;
var desc = Object.getOwnPropertyDescriptor(m, k);
if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
desc = { enumerable: true, get: function() { return m[k]; } };
}
Object.defineProperty(o, k2, desc);
}) : (function(o, m, k, k2) {
if (k2 === undefined) k2 = k;
o[k2] = m[k];
}));
var __exportStar = (this && this.__exportStar) || function(m, exports) {
for (var p in m) if (p !== "default" && !Object.prototype.hasOwnProperty.call(exports, p)) __createBinding(exports, m, p);
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.extract = extract;
exports.lx = extract;
/**
* LangExtract TypeScript: A TypeScript library for extracting structured and grounded information from text using LLMs.
* Translated from the original Python implementation by Google LLC.
*/
// Core types and enums
__exportStar(require("./types"), exports);
// Schema definitions
__exportStar(require("./schema"), exports);
// Tokenization utilities
__exportStar(require("./tokenizer"), exports);
// Prompt generation
__exportStar(require("./prompting"), exports);
// Language model inference
__exportStar(require("./inference"), exports);
// Output resolution
__exportStar(require("./resolver"), exports);
// Annotation pipeline
__exportStar(require("./annotation"), exports);
// Visualization utilities
__exportStar(require("./visualization"), exports);
// Main extraction function
const types_1 = require("./types");
const inference_1 = require("./inference");
const resolver_1 = require("./resolver");
const annotation_1 = require("./annotation");
const schema_1 = require("./schema");
/**
* Main extraction function that provides a high-level API for extracting structured information from text.
*/
async function extract(textOrDocuments, options = {}) {
const { promptDescription = "Extract structured information from the text", examples = [], modelId = "gemini-2.5-flash", modelType = "gemini", apiKey, formatType = types_1.FormatType.JSON, maxCharBuffer = 1000, temperature = 0.5, fenceOutput = false, useSchemaConstraints = true, batchLength = 10, maxWorkers = 10, additionalContext, debug = true, modelUrl, baseURL, extractionPasses = 1, maxTokens, } = options;
if (!examples || examples.length === 0) {
throw new Error("Examples are required for reliable extraction. Please provide at least one ExampleData object with sample extractions.");
}
if (!apiKey) {
throw new Error("API key must be provided for cloud-hosted models via the apiKey parameter or the LANGEXTRACT_API_KEY environment variable");
}
// Create prompt template
const promptTemplate = {
description: promptDescription,
examples,
};
// Generate schema constraints if enabled
let geminiSchema;
if (useSchemaConstraints) {
geminiSchema = schema_1.GeminiSchemaImpl.fromExamples(examples);
}
// Create language model based on modelType
let languageModel;
switch (modelType) {
case "openai":
languageModel = new inference_1.OpenAILanguageModel({
model: modelId,
apiKey,
openAISchema: geminiSchema,
formatType,
temperature,
maxWorkers,
baseURL,
maxTokens,
});
break;
case "ollama":
languageModel = new inference_1.OllamaLanguageModel({
model: modelId,
modelUrl: modelUrl || "http://localhost:11434",
structuredOutputFormat: formatType === types_1.FormatType.JSON ? "json" : "yaml",
temperature,
maxTokens,
});
break;
case "gemini":
default:
languageModel = new inference_1.GeminiLanguageModel({
modelId,
apiKey,
geminiSchema,
formatType,
temperature,
maxWorkers,
modelUrl,
maxTokens,
});
break;
}
// Create resolver
const resolver = new resolver_1.Resolver({
fenceOutput,
formatType,
extractionAttributesSuffix: "_attributes",
});
// Create annotator
const annotator = new annotation_1.Annotator(languageModel, promptTemplate, {
formatType,
fenceOutput,
maxTokens,
});
// Process input
if (typeof textOrDocuments === "string") {
return await annotator.annotateText(textOrDocuments, resolver, {
maxCharBuffer,
batchLength,
additionalContext,
debug,
extractionPasses,
});
}
else if (Array.isArray(textOrDocuments)) {
return await annotator.annotateDocuments(textOrDocuments, resolver, {
maxCharBuffer,
batchLength,
debug,
extractionPasses,
});
}
else {
return await annotator.annotateDocuments([textOrDocuments], resolver, {
maxCharBuffer,
batchLength,
debug,
extractionPasses,
});
}
}
// Export default
exports.default = {
extract,
lx: extract,
};
//# sourceMappingURL=index.js.map