### Clone and Setup LangExtract Repository Source: https://github.com/kmbro/langextract-typescript/blob/main/README.md Steps to clone the LangExtract repository, navigate to the TypeScript directory, and install dependencies. ```bash git clone https://github.com/kmbro/langextract.git cd langextract/typescript npm install ``` -------------------------------- ### Install Dependencies and Build Parent Library Source: https://github.com/kmbro/langextract-typescript/blob/main/integration/README.md Navigate to the integration directory, install local dependencies, and then install and build the parent library before returning to the integration directory. ```bash cd typescript/integration npm install cd .. npm install npm run build cd integration ``` -------------------------------- ### Basic Information Extraction with Examples Source: https://github.com/kmbro/langextract-typescript/blob/main/integration/README.md Demonstrates how to extract structured information from text using provided examples to guide the extraction process. Ensure the LANGEXTRACT_API_KEY environment variable is set. ```typescript import { extract, ExampleData, FormatType } from "../src/index"; // Define examples to guide extraction const examples: ExampleData[] = [ { text: "John Smith is 30 years old and works at Google as a software engineer.", extractions: [ { extractionClass: "person", extractionText: "John Smith", attributes: { age: "30", employer: "Google", job_title: "software engineer", }, }, ], }, ]; // Extract information from text const result = await extract("Alice Brown is 25 years old and works at Apple as a product manager.", { promptDescription: "Extract person information including name, age, employer, and job title", examples: examples, apiKey: process.env.LANGEXTRACT_API_KEY, modelId: "gemini-2.5-flash", formatType: FormatType.JSON, temperature: 0.3, }); console.log("Extraction Result:", result); ``` -------------------------------- ### Install LangExtract from Source Source: https://github.com/kmbro/langextract-typescript/blob/main/README.md Clone the repository and install dependencies from source. This is useful for development or if you need the latest unreleased features. ```bash git clone https://github.com/kmbro/langextract.git cd langextract/typescript npm install npm run build ``` -------------------------------- ### Install LangExtract via npm Source: https://github.com/kmbro/langextract-typescript/blob/main/README.md Install the LangExtract library using npm. This is the recommended method for most users. ```bash npm install langextract ``` -------------------------------- ### Model Configuration Source: https://github.com/kmbro/langextract-typescript/blob/main/README.md Configuration examples for different language models. ```APIDOC ## Google Gemini Configuration ### Description Configure the Google Gemini language model. ### Code Example ```typescript import { GeminiLanguageModel } from "langextract"; const model = new GeminiLanguageModel({ modelId: "gemini-2.5-flash", apiKey: "your-api-key", temperature: 0.3, }); ``` ``` ```APIDOC ## OpenAI Configuration ### Description Configure the OpenAI language model. ### Code Example ```typescript import { OpenAILanguageModel } from "langextract"; const model = new OpenAILanguageModel({ model: "gpt-4o-mini", // or "gpt-4", "gpt-3.5-turbo", etc. apiKey: "your-openai-api-key", temperature: 0.3, baseURL: "https://api.openai.com/v1", // Optional: for custom endpoints }); ``` ``` ```APIDOC ## Ollama (Local Models) Configuration ### Description Configure the Ollama language model for local deployments. ### Code Example ```typescript import { OllamaLanguageModel } from "langextract"; const model = new OllamaLanguageModel({ model: "llama2:latest", modelUrl: "http://localhost:11434", temperature: 0.7, }); ``` ``` -------------------------------- ### Generate JSON Schema from Examples with GeminiSchemaImpl Source: https://context7.com/kmbro/langextract-typescript/llms.txt Use GeminiSchemaImpl.fromExamples to automatically generate a JSON schema based on provided text examples and their extractions. Supports array attributes. ```typescript import { GeminiSchemaImpl, ExampleData } from "langextract"; // Generate schema from examples const examples: ExampleData[] = [ { text: "John works at Google as an engineer.", extractions: [ { extractionClass: "person", extractionText: "John", attributes: { employer: "Google", job_title: "engineer", skills: ["coding", "design"], // Array attributes supported }, }, ], }, ]; const schema = GeminiSchemaImpl.fromExamples(examples, "_attributes"); console.log(JSON.stringify(schema.schemaDict, null, 2)); // { // "type": "object", // "properties": { // "extractions": { // "type": "array", // "items": { // "type": "object", // "properties": { // "person": { "type": "string" }, // "person_attributes": { // "type": "object", // "properties": { // "employer": { "type": "string" }, // "job_title": { "type": "string" }, // "skills": { "type": "array", "items": { "type": "string" } } // } // } // } // } // } // } // } ``` -------------------------------- ### Basic Extraction with Gemini Source: https://github.com/kmbro/langextract-typescript/blob/main/README.md Perform basic information extraction using the Gemini model. Ensure you provide a valid API key and model ID. The `examples` parameter helps guide the LLM. ```typescript import { extract, ExampleData } from "langextract"; // Define examples to guide the extraction const examples: ExampleData[] = [ { text: "John Smith is 30 years old and works at Google.", extractions: [ { extractionClass: "person", extractionText: "John Smith", attributes: { age: "30", employer: "Google", }, }, ], }, ]; // Extract information from text using Gemini async function extractPersonInfo() { const result = await extract("Alice Johnson is 25 and works at Microsoft.", { promptDescription: "Extract person information including name, age, and employer", examples: examples, modelType: "gemini", apiKey: "your-gemini-api-key", modelId: "gemini-2.5-flash", }); console.log(result.extractions); // Output: [ // { // extractionClass: "person", // extractionText: "Alice Johnson", // attributes: { // age: "25", // employer: "Microsoft" // }, // charInterval: { startPos: 0, endPos: 13 }, // alignmentStatus: "match_exact" // } // ] } ``` -------------------------------- ### ExampleData Interface Definition Source: https://github.com/kmbro/langextract-typescript/blob/main/README.md Defines the structure for providing example data to the `extract` function. This helps in guiding the LLM for more accurate extractions. ```typescript interface ExampleData { text: string; extractions: Extraction[]; } ``` -------------------------------- ### Generate Q&A Prompts with QAPromptGeneratorImpl Source: https://context7.com/kmbro/langextract-typescript/llms.txt Configure QAPromptGeneratorImpl with a template and examples to render structured prompts for LLM interactions. Supports JSON output format. ```typescript import { QAPromptGeneratorImpl, PromptTemplateStructured, FormatType } from "langextract"; const template: PromptTemplateStructured = { description: "Extract medical conditions and their attributes from clinical text", examples: [ { text: "Patient has diabetes mellitus type 2.", extractions: [ { extractionClass: "condition", extractionText: "diabetes mellitus type 2", attributes: { type: "chronic", severity: "moderate" }, }, ], }, ], }; const generator = new QAPromptGeneratorImpl(template); generator.formatType = FormatType.JSON; generator.fenceOutput = false; generator.attributeSuffix = "_attributes"; const prompt = generator.render( "Patient diagnosed with hypertension and asthma.", "Focus on chronic conditions" // Additional context ); console.log(prompt); // Extract medical conditions and their attributes from clinical text // // Focus on chronic conditions // // Examples // Q: Patient has diabetes mellitus type 2. // A: {"extractions": [{"condition": "diabetes mellitus type 2", "condition_attributes": {"type": "chronic", "severity": "moderate"}}]} // // Please respond with a JSON object. // Q: Patient diagnosed with hypertension and asthma. // A: ``` -------------------------------- ### Define Custom Prompt Template - TypeScript Source: https://github.com/kmbro/langextract-typescript/blob/main/README.md Illustrates creating a custom prompt template for extraction tasks, including examples and a description. ```typescript import { PromptTemplateStructured, QAPromptGeneratorImpl } from "langextract"; const template: PromptTemplateStructured = { description: "Extract medical entities from clinical text", examples: [ { text: "Patient has diabetes and hypertension", extractions: [ { extractionClass: "condition", extractionText: "diabetes", }, { extractionClass: "condition", extractionText: "hypertension", }, ], }, ], }; const generator = new QAPromptGeneratorImpl(template); const prompt = generator.render("Patient shows signs of asthma"); ``` -------------------------------- ### Extract Structured Information with TypeScript Source: https://context7.com/kmbro/langextract-typescript/llms.txt Use the `extract` function to get structured data from text. Provide examples to guide the LLM and configure extraction parameters like model, API key, and output format. Ensure API keys are securely managed. ```typescript import { extract, ExampleData, FormatType } from "langextract"; // Define examples to guide the extraction const examples: ExampleData[] = [ { text: "John Smith is 30 years old and works at Google.", extractions: [ { extractionClass: "person", extractionText: "John Smith", attributes: { age: "30", employer: "Google", }, }, ], }, ]; // Extract with Gemini (default) const result = await extract("Alice Johnson is 25 and works at Microsoft.", { promptDescription: "Extract person information including name, age, and employer", examples: examples, modelType: "gemini", apiKey: process.env.GEMINI_API_KEY, modelId: "gemini-2.5-flash", formatType: FormatType.JSON, temperature: 0.3, maxCharBuffer: 1000, maxTokens: 2048, }); console.log(result.extractions); // Output: // [ // { // extractionClass: "person", // extractionText: "Alice Johnson", // attributes: { age: "25", employer: "Microsoft" }, // charInterval: { startPos: 0, endPos: 13 }, // alignmentStatus: "match_exact" // } // ] ``` -------------------------------- ### Initialize and Infer with OllamaLanguageModel Source: https://context7.com/kmbro/langextract-typescript/llms.txt Instantiate OllamaLanguageModel for local LLM inference with specified parameters. Use the infer method to get results from a prompt. ```typescript import { OllamaLanguageModel } from "langextract"; const model = new OllamaLanguageModel({ model: "llama2:latest", modelUrl: "http://localhost:11434", structuredOutputFormat: "json", temperature: 0.7, maxTokens: 2048, }); const results = await model.infer([ "Extract the person name from: Dr. Jane Wilson works at the hospital.", ]); console.log(results[0][0].output); // {"person": "Dr. Jane Wilson", "workplace": "hospital"} ``` ```typescript // Use with main extract function const extraction = await extract("Dr. Jane Wilson is a cardiologist.", { examples: [{ text: "Dr. Smith is a doctor.", extractions: [{ extractionClass: "person", extractionText: "Dr. Smith" }] }], modelType: "ollama", modelUrl: "http://localhost:11434", modelId: "llama2:latest", apiKey: "not-required-for-ollama", }); ``` -------------------------------- ### Named Entity Recognition Example Source: https://github.com/kmbro/langextract-typescript/blob/main/README.md Perform Named Entity Recognition (NER) to identify and categorize entities like organizations, persons, and locations. This example shows how to structure NER examples with attributes. ```typescript const nerExamples: ExampleData[] = [ { text: "Apple Inc. was founded by Steve Jobs in Cupertino, California.", extractions: [ { extractionClass: "organization", extractionText: "Apple Inc.", attributes: { type: "company", }, }, { extractionClass: "person", extractionText: "Steve Jobs", attributes: { role: "founder", }, }, { extractionClass: "location", extractionText: "Cupertino, California", attributes: { type: "city", }, }, ], }, ]; ``` -------------------------------- ### Run LangExtract Visualization CLI (Development) Source: https://github.com/kmbro/langextract-typescript/blob/main/README.md Execute the visualization CLI using npm run visualize, providing sample input and output file names. ```bash npm run visualize -- sample-extractions.jsonl output.html ``` -------------------------------- ### Run LangExtract Visualization CLI Source: https://github.com/kmbro/langextract-typescript/blob/main/README.md Use the npm script to visualize extractions. Specify input JSONL and output HTML files. Adjust animation speed with the --speed option. ```bash npm run visualize -- input.jsonl output.html --speed 0.8 ``` -------------------------------- ### Medical Entity Extraction Example Source: https://github.com/kmbro/langextract-typescript/blob/main/README.md Extract specific medical conditions and their attributes from text. This example demonstrates how to define custom examples for medical entity recognition. ```typescript const medicalExamples: ExampleData[] = [ { text: "The patient has diabetes mellitus type 2 and hypertension.", extractions: [ { extractionClass: "condition", extractionText: "diabetes mellitus type 2", attributes: { severity: "moderate", type: "type 2", }, }, { extractionClass: "condition", extractionText: "hypertension", attributes: { severity: "mild", }, }, ], }, ]; const result = await extract("Patient diagnosed with asthma and obesity.", { promptDescription: "Extract medical conditions and their attributes", examples: medicalExamples, apiKey: "your-api-key", }); ``` -------------------------------- ### Configure API Key via .env File Source: https://github.com/kmbro/langextract-typescript/blob/main/integration/README.md Alternatively, create a .env file in the integration directory and add your Gemini API key. ```dotenv LANGEXTRACT_API_KEY=your-gemini-api-key-here ``` -------------------------------- ### CLI - Command Line Visualization Source: https://context7.com/kmbro/langextract-typescript/llms.txt Utilize the command-line interface to generate visualizations from JSONL input files, with options for customization. ```APIDOC ## CLI - Command Line Visualization ### Description Generate visualizations from the command line using JSONL input files. ### Method Command Line Interface (CLI) ### Endpoint `npx ts-node bin/visualize.ts [options]` ### Parameters #### Path Parameters None #### Query Parameters None #### Request Body None ### Request Example ```bash # Basic usage npx ts-node bin/visualize.ts input.jsonl output.html # With custom animation speed npx ts-node bin/visualize.ts input.jsonl output.html --speed 1.5 # GIF-optimized output (larger fonts, better contrast) npx ts-node bin/visualize.ts input.jsonl output.html --gif-optimized # Hide legend npx ts-node bin/visualize.ts input.jsonl output.html --no-legend # Custom context window npx ts-node bin/visualize.ts input.jsonl output.html --context 200 # Using npm script npm run visualize -- input.jsonl output.html --speed 0.8 # Process multiple files for file in *.jsonl; do npx ts-node bin/visualize.ts "$file" "${file%.jsonl}.html" done ``` ### Response #### Success Response (200) An HTML file containing the generated visualization. #### Response Example N/A (CLI output is a file, not a direct response body) ``` -------------------------------- ### Generate Visualizations from Command Line Source: https://context7.com/kmbro/langextract-typescript/llms.txt Use the CLI tool to generate HTML visualizations from JSONL input files. Customize animation speed, GIF optimization, legend visibility, and context window size. ```bash # Basic usage npx ts-node bin/visualize.ts input.jsonl output.html # With custom animation speed npx ts-node bin/visualize.ts input.jsonl output.html --speed 1.5 # GIF-optimized output (larger fonts, better contrast) npx ts-node bin/visualize.ts input.jsonl output.html --gif-optimized # Hide legend npx ts-node bin/visualize.ts input.jsonl output.html --no-legend # Custom context window npx ts-node bin/visualize.ts input.jsonl output.html --context 200 # Using npm script npm run visualize -- input.jsonl output.html --speed 0.8 # Process multiple files for file in *.jsonl; do npx ts-node bin/visualize.ts "$file" "${file%.jsonl}.html" done ``` -------------------------------- ### CLI for Visualization Generation Source: https://github.com/kmbro/langextract-typescript/blob/main/README.md Use the LangExtract CLI tool to generate visualization HTML files from the command line. Supports basic usage and custom options for speed, GIF optimization, and legend display. ```bash # Basic usage npx ts-node bin/visualize.ts input.jsonl output.html # With custom options npx ts-node bin/visualize.ts input.jsonl output.html --speed 1.5 --gif-optimized # Hide legend npx ts-node bin/visualize.ts input.jsonl output.html --no-legend ``` -------------------------------- ### Define Character Interval - TypeScript Interface Source: https://github.com/kmbro/langextract-typescript/blob/main/README.md Defines the structure for a character interval, including optional start and end positions. ```typescript interface CharInterval { startPos?: number; endPos?: number; } ``` -------------------------------- ### Build and Test LangExtract Project Source: https://github.com/kmbro/langextract-typescript/blob/main/README.md Commands to build the TypeScript project and run tests. Integration tests require an OpenAI API key. ```bash # Build the project npm run build # Run tests npm test # Run specific integration tests (requires API key) OPENAI_API_KEY=your-api-key npm test -- medical-extraction.test.ts ``` -------------------------------- ### Tokenize Text with Character Position Tracking Source: https://context7.com/kmbro/langextract-typescript/llms.txt Use the tokenize function to split text into words and track their character start and end positions. Normalize tokens using normalizeToken or tokenizeWithLowercase for case-insensitive comparisons. ```typescript import { tokenize, normalizeToken, tokenizeWithLowercase } from "langextract"; const text = "John Smith works at Google Inc."; const result = tokenize(text); console.log(result.tokens); // ["John", "Smith", "works", "at", "Google", "Inc", "."] console.log(result.charIntervals); // [ // { startPos: 0, endPos: 4 }, // "John" // { startPos: 5, endPos: 10 }, // "Smith" // { startPos: 11, endPos: 16 }, // "works" // { startPos: 17, endPos: 19 }, // "at" // { startPos: 20, endPos: 26 }, // "Google" // { startPos: 27, endPos: 30 }, // "Inc" // { startPos: 30, endPos: 31 }, // "." // ] // Normalize tokens for comparison console.log(normalizeToken("Google")); // "google" // Tokenize with automatic lowercase normalization const lowercaseTokens = tokenizeWithLowercase(text); console.log(lowercaseTokens); // ["john", "smith", "works", "at", "google", "inc", "."] ``` -------------------------------- ### Set LangExtract API Key Environment Variable Source: https://github.com/kmbro/langextract-typescript/blob/main/README.md Configure your API key by exporting it as an environment variable. This is required for authentication. ```bash export LANGEXTRACT_API_KEY="your-api-key" ``` -------------------------------- ### Configure API Key via Environment Variable Source: https://github.com/kmbro/langextract-typescript/blob/main/integration/README.md Set your Gemini API key as an environment variable named LANGEXTRACT_API_KEY. ```bash export LANGEXTRACT_API_KEY="your-gemini-api-key-here" ``` -------------------------------- ### Run All LangExtract TypeScript Tests Source: https://github.com/kmbro/langextract-typescript/blob/main/TEST_COVERAGE.md Execute all tests in the TypeScript suite using npm. This is the standard command for verifying the entire test suite. ```bash npm test ``` -------------------------------- ### Create Presentation-Friendly Visualization Source: https://github.com/kmbro/langextract-typescript/blob/main/README.md Generate a visualization with slower animation speed and hidden legend for presentations. ```bash npx ts-node bin/visualize.ts extractions.jsonl presentation.html --speed 2.0 --no-legend ``` -------------------------------- ### Process Multiple JSONL Files Source: https://github.com/kmbro/langextract-typescript/blob/main/README.md Use a shell loop to process multiple JSONL files, generating a corresponding HTML visualization for each. ```bash for file in *.jsonl; do npx ts-node bin/visualize.ts "$file" "${file%.jsonl}.html" done ``` -------------------------------- ### Configure OpenAI Model - TypeScript Source: https://github.com/kmbro/langextract-typescript/blob/main/README.md Initializes the OpenAI language model with a model name, API key, temperature, and optional base URL. ```typescript import { OpenAILanguageModel } from "langextract"; const model = new OpenAILanguageModel({ model: "gpt-4o-mini", // or "gpt-4", "gpt-3.5-turbo", etc. apiKey: "your-openai-api-key", temperature: 0.3, baseURL: "https://api.openai.com/v1", // Optional: for custom endpoints }); ``` -------------------------------- ### Create Fast Animation for GIF Capture Source: https://github.com/kmbro/langextract-typescript/blob/main/README.md Utilize ts-node to run the visualization script with optimized settings for GIF or video capture, including a faster animation speed. ```bash npx ts-node bin/visualize.ts extractions.jsonl demo.html --speed 0.5 --gif-optimized ``` -------------------------------- ### LangExtract TypeScript - Basic Extraction Source: https://github.com/kmbro/langextract-typescript/blob/main/README.md Demonstrates how to perform basic information extraction using the `extract` function with Gemini and OpenAI models. ```APIDOC ## Basic Extraction Example ### Description This example shows how to use the `extract` function to get structured information from text, providing examples to guide the LLM. It includes examples for both Gemini and OpenAI. ### Method `extract(textOrDocuments, options)` ### Parameters #### Request Body (Implicit in `extract` function call) - **textOrDocuments** (string | Document | Document[]) - Required - The text or document(s) to process. - **options** (object) - Required - Configuration for the extraction process. - **promptDescription** (string) - Required - Instructions for what to extract. - **examples** (ExampleData[]) - Required - Training examples to guide extraction. - **modelType** (string) - Required - LLM provider type (e.g., "gemini", "openai"). - **apiKey** (string) - Required - API key for the LLM service. - **modelId** (string) - Optional - LLM model ID (default: "gemini-2.5-flash"). - **temperature** (number) - Optional - Sampling temperature (default: 0.5). ### Request Example (Gemini) ```typescript import { extract, ExampleData } from "langextract"; const examples: ExampleData[] = [ { text: "John Smith is 30 years old and works at Google.", extractions: [ { extractionClass: "person", extractionText: "John Smith", attributes: { age: "30", employer: "Google", }, }, ], }, ]; async function extractPersonInfo() { const result = await extract("Alice Johnson is 25 and works at Microsoft.", { promptDescription: "Extract person information including name, age, and employer", examples: examples, modelType: "gemini", apiKey: "your-gemini-api-key", modelId: "gemini-2.5-flash", }); console.log(result.extractions); } ``` ### Request Example (OpenAI) ```typescript import { extract, ExampleData } from "langextract"; const examples: ExampleData[] = [ { text: "John Smith is 30 years old and works at Google.", extractions: [ { extractionClass: "person", extractionText: "John Smith", attributes: { age: "30", employer: "Google", }, }, ], }, ]; async function extractPersonInfoWithOpenAI() { const result = await extract("Alice Johnson is 25 and works at Microsoft.", { promptDescription: "Extract person information including name, age, and employer", examples: examples, modelType: "openai", apiKey: "your-openai-api-key", modelId: "gpt-4o-mini", temperature: 0.1, }); console.log(result.extractions); } ``` ### Response Example ```json [ { "extractionClass": "person", "extractionText": "Alice Johnson", "attributes": { "age": "25", "employer": "Microsoft" }, "charInterval": { "startPos": 0, "endPos": 13 }, "alignmentStatus": "match_exact" } ] ``` ``` -------------------------------- ### Configure Google Gemini Model - TypeScript Source: https://github.com/kmbro/langextract-typescript/blob/main/README.md Initializes the Gemini language model with a specific model ID, API key, and temperature. ```typescript import { GeminiLanguageModel } from "langextract"; const model = new GeminiLanguageModel({ modelId: "gemini-2.5-flash", apiKey: "your-api-key", temperature: 0.3, }); ``` -------------------------------- ### Basic Extraction with OpenAI Source: https://github.com/kmbro/langextract-typescript/blob/main/README.md Perform basic information extraction using an OpenAI model. Requires an API key and model ID. Adjust the `temperature` for creativity vs. determinism. ```typescript import { extract, ExampleData } from "langextract"; // Define examples to guide the extraction const examples: ExampleData[] = [ { text: "John Smith is 30 years old and works at Google.", extractions: [ { extractionClass: "person", extractionText: "John Smith", attributes: { age: "30", employer: "Google", }, }, ], }, ]; // Extract information from text using OpenAI async function extractPersonInfoWithOpenAI() { const result = await extract("Alice Johnson is 25 and works at Microsoft.", { promptDescription: "Extract person information including name, age, and employer", examples: examples, modelType: "openai", apiKey: "your-openai-api-key", modelId: "gpt-4o-mini", temperature: 0.1, }); console.log(result.extractions); } ``` -------------------------------- ### Use Custom Ollama Endpoint - TypeScript Source: https://github.com/kmbro/langextract-typescript/blob/main/README.md Shows how to connect to a custom Ollama server by providing the modelUrl. ```typescript // Use custom Ollama endpoint const result = await extract("Extract person information from this text.", { examples: examples, modelType: "ollama", modelUrl: "http://your-custom-ollama-server:11434", // Custom Ollama server maxTokens: 200, }); ``` -------------------------------- ### Configure Ollama Model (Local) - TypeScript Source: https://github.com/kmbro/langextract-typescript/blob/main/README.md Initializes the Ollama language model for local use with a model name, URL, and temperature. ```typescript import { OllamaLanguageModel } from "langextract"; const model = new OllamaLanguageModel({ model: "llama2:latest", modelUrl: "http://localhost:11434", temperature: 0.7, }); ``` -------------------------------- ### Generate Interactive Visualizations with LangExtract Source: https://context7.com/kmbro/langextract-typescript/llms.txt Use `visualize()` and `saveVisualizationPage()` to create interactive HTML visualizations of extracted entities. Customize animation speed, legend display, GIF optimization, and context characters. Load visualizations from JSONL files. ```typescript import { visualize, saveVisualizationPage, AnnotatedDocument } from "langextract"; const annotatedDoc: AnnotatedDocument = { text: "John Smith is a software engineer at Google. He lives in San Francisco.", extractions: [ { extractionClass: "PERSON", extractionText: "John Smith", charInterval: { startPos: 0, endPos: 10 }, attributes: { role: "software engineer", company: "Google" }, }, { extractionClass: "ORGANIZATION", extractionText: "Google", charInterval: { startPos: 37, endPos: 43 }, attributes: { type: "technology company" }, }, { extractionClass: "LOCATION", extractionText: "San Francisco", charInterval: { startPos: 57, endPos: 70 }, attributes: { type: "city" }, }, ], }; // Generate HTML string const html = visualize(annotatedDoc, { animationSpeed: 1.5, // Seconds between entity highlights showLegend: true, // Show color legend gifOptimized: true, // Optimize for GIF/video capture contextChars: 150, // Characters of context around extractions }); // Save as complete HTML page saveVisualizationPage(annotatedDoc, "./extraction-viz.html", { animationSpeed: 1.0, showLegend: true, gifOptimized: false, }); // Load and visualize from JSONL file const htmlFromFile = visualize("./extractions.jsonl", { animationSpeed: 0.8, showLegend: true, }); ``` -------------------------------- ### Initialize and Control Entity Extractions Source: https://github.com/kmbro/langextract-typescript/blob/main/visualization-output.html This code initializes entity extraction data and sets up functions for playing, pausing, and navigating through extracted entities. It updates the display with entity attributes, position information, and highlights the current entity in the text. Use this to enable interactive exploration of extracted entities. ```typescript (function() { const extractions = [{"index":0,"class":"PERSON","text":"John Smith","color":"#F9DEDC","startPos":0,"endPos":10,"beforeText":"","extractionText":"John Smith","afterText":" is a software engineer at Google. He lives in San Francisco, California. His email is john.smith@google.com and his phone number is (555) 123-4567.","attributesHtml":"
class: PERSON
attributes: {role: software engineer, company: Google}
"},{"index":1,"class":"ORGANIZATION","text":"Google","color":"#FEF0C3","startPos":37,"endPos":43,"beforeText":"John Smith is a software engineer at ","extractionText":"Google","afterText":". He lives in San Francisco, California. His email is john.smith@google.com and his phone number is (555) 123-4567.","attributesHtml":"
class: ORGANIZATION
attributes: {type: technology company}
"},{"index":2,"class":"LOCATION","text":"San Francisco, California","color":"#C8E6C9","startPos":57,"endPos":82,"beforeText":"John Smith is a software engineer at Google. He lives in ","extractionText":"San Francisco, California","afterText":". His email is john.smith@google.com and his phone number is (555) 123-4567.","attributesHtml":"
class: LOCATION
attributes: {city: San Francisco, state: California}
"},{"index":3,"class":"EMAIL","text":"john.smith@google.com","color":"#D2E3FC","startPos":97,"endPos":118,"beforeText":"John Smith is a software engineer at Google. He lives in San Francisco, California. His email is ","extractionText":"john.smith@google.com","afterText":" and his phone number is (555) 123-4567.","attributesHtml":"
class: EMAIL
attributes: {domain: google.com}
"},{"index":4,"class":"PHONE","text":"(555) 123-4567","color":"#FFDDBE","startPos":143,"endPos":158,"beforeText":"John Smith is a software engineer at Google. He lives in San Francisco, California. His email is john.smith@google.com and his phone number is ","extractionText":"(555) 123-4567.","afterText":"","attributesHtml":"
class: PHONE
attributes: {area_code: 555}
"}]; let currentIndex = 0; let isPlaying = false; let animationInterval = null; let animationSpeed = 1; function updateDisplay() { const extraction = extractions[currentIndex]; if (!extraction) return; document.getElementById('attributesContainer').innerHTML = extraction.attributesHtml; document.getElementById('entityInfo').textContent = (currentIndex + 1) + '/' + extractions.length; document.getElementById('posInfo').textContent = '[' + extraction.startPos + '-' + extraction.endPos + ']'; document.getElementById('progressSlider').value = currentIndex; const playBtn = document.querySelector('.lx-control-btn'); if (playBtn) playBtn.textContent = isPlaying ? '⏸ Pause' : '▶️ Play'; const prevHighlight = document.querySelector('.lx-text-window .lx-current-highlight'); if (prevHighlight) prevHighlight.classList.remove('lx-current-highlight'); const currentSpan = document.querySelector('.lx-text-window span[data-idx="' + currentIndex + '"]'); if (currentSpan) { currentSpan.classList.add('lx-current-highlight'); currentSpan.scrollIntoView({block: 'center', behavior: 'smooth'}); } } function nextExtraction() { currentIndex = (currentIndex + 1) % extractions.length; updateDisplay(); } function prevExtraction() { currentIndex = (currentIndex - 1 + extractions.length) % extractions.length; updateDisplay(); } function jumpToExtraction(index) { currentIndex = parseInt(index); updateDisplay(); } function playPause() { if (isPlaying) { clearInterval(animationInterval); isPlaying = false; } else { animationInterval = setInterval(nextExtraction, animationSpeed * 1000); isPlaying = true; } updateDisplay(); } window.playPause = playPause; window.nextExtraction = nextExtraction; window.prevExtraction = prevExtraction; window.jumpToExtraction = jumpToExtraction; updateDisplay(); })(); ``` -------------------------------- ### Run Specific LangExtract TypeScript Test File Source: https://github.com/kmbro/langextract-typescript/blob/main/TEST_COVERAGE.md Execute a single test file within the TypeScript suite by specifying its path with npm. Useful for focused testing during development. ```bash npm test -- src/__tests__/init.test.ts ``` -------------------------------- ### Visualize Extractions from File Source: https://github.com/kmbro/langextract-typescript/blob/main/README.md Generate an interactive HTML visualization directly from a JSONL file containing extraction data. Adjust animation speed and legend visibility. ```typescript // Visualize extractions from a JSONL file const html = visualize("./extractions.jsonl", { animationSpeed: 0.8, showLegend: true, }); ``` -------------------------------- ### Configure Custom Resolvers Source: https://github.com/kmbro/langextract-typescript/blob/main/README.md Instantiate a custom Resolver with specific output formatting and attribute handling. Use this to control how extracted data is processed and presented. ```typescript import { Resolver, FormatType } from "langextract"; const resolver = new Resolver({ fenceOutput: true, formatType: FormatType.YAML, extractionAttributesSuffix: "_attrs", }); ``` -------------------------------- ### Generate and Save Visualization Page Source: https://github.com/kmbro/langextract-typescript/blob/main/README.md Create an interactive HTML visualization of extraction results and save it as a complete HTML page. Customize animation speed, legend display, and GIF optimization. ```typescript import { visualize, saveVisualizationPage } from "langextract"; // Create a visualization from an AnnotatedDocument const html = visualize(result, { animationSpeed: 1.0, // Seconds between extractions showLegend: true, // Show color legend gifOptimized: true, // Optimize for video capture }); // Save as a complete HTML page saveVisualizationPage(result, "./extraction-visualization.html", { animationSpeed: 1.5, showLegend: true, gifOptimized: false, }); ``` -------------------------------- ### LangExtract TypeScript - Quick Visualization Source: https://github.com/kmbro/langextract-typescript/blob/main/README.md Shows how to generate and save an interactive HTML visualization of the extraction results. ```APIDOC ## Quick Visualization ### Description This example demonstrates how to use the `saveVisualizationPage` function to create an interactive HTML file from the extraction results. ### Method `saveVisualizationPage(result, filePath, options)` ### Parameters #### Request Body (Implicit in `saveVisualizationPage` function call) - **result**: `AnnotatedDocument | AnnotatedDocument[]` - The extraction results to visualize. - **filePath**: `string` - The path where the HTML visualization file will be saved. - **options** (object) - Optional - Configuration for the visualization. - **animationSpeed** (number) - Optional - Speed of animations (default: 1.0). - **showLegend** (boolean) - Optional - Whether to display the legend (default: true). - **gifOptimized** (boolean) - Optional - Whether to optimize for GIF output (default: false). ### Request Example ```typescript import { visualize, saveVisualizationPage } from "langextract"; // Assuming 'result' is the output from the 'extract' function // const result = await extract(...); saveVisualizationPage(result, "./extraction-viz.html", { animationSpeed: 1.0, showLegend: true, gifOptimized: true }); ``` ``` -------------------------------- ### Run LangExtract TypeScript Tests with Coverage Source: https://github.com/kmbro/langextract-typescript/blob/main/TEST_COVERAGE.md Generate a code coverage report for the TypeScript tests using npm. This command helps identify areas of the codebase not covered by tests. ```bash npm run test:coverage ``` -------------------------------- ### Text Tokenization - tokenize() Source: https://context7.com/kmbro/langextract-typescript/llms.txt Tokenizes input text into individual words and provides character position tracking for each token, enabling precise alignment with the original text. ```APIDOC ## tokenize() - Text Tokenization ### Description Tokenizes text into words with character position tracking for alignment purposes. ### Method `tokenize(text: string): { tokens: string[]; charIntervals: { startPos: number; endPos: number }[] }` ### Endpoint N/A (Library function) ### Parameters #### Path Parameters None #### Query Parameters None #### Request Body None ### Request Example ```typescript import { tokenize } from "langextract"; const text = "John Smith works at Google Inc."; const result = tokenize(text); console.log(result.tokens); // ["John", "Smith", "works", "at", "Google", "Inc", "."] console.log(result.charIntervals); // [ // { startPos: 0, endPos: 4 }, // "John" // { startPos: 5, endPos: 10 }, // "Smith" // { startPos: 11, endPos: 16 }, // "works" // { startPos: 17, endPos: 19 }, // "at" // { startPos: 20, endPos: 26 }, // "Google" // { startPos: 27, endPos: 30 }, // "Inc" // { startPos: 30, endPos: 31 }, // "." // ] ``` ### Response #### Success Response (200) An object containing two arrays: `tokens` (an array of strings) and `charIntervals` (an array of objects, each with `startPos` and `endPos` numbers). #### Response Example ```json { "tokens": ["John", "Smith", "works", "at", "Google", "Inc", "."], "charIntervals": [ { "startPos": 0, "endPos": 4 }, { "startPos": 5, "endPos": 10 }, { "startPos": 11, "endPos": 16 }, { "startPos": 17, "endPos": 19 }, { "startPos": 20, "endPos": 26 }, { "startPos": 27, "endPos": 30 }, { "startPos": 30, "endPos": 31 } ] } ``` ## normalizeToken() ### Description Normalizes a token for comparison purposes, typically by converting it to lowercase. ### Method `normalizeToken(token: string): string` ### Endpoint N/A (Library function) ### Parameters #### Path Parameters None #### Query Parameters None #### Request Body None ### Request Example ```typescript import { normalizeToken } from "langextract"; console.log(normalizeToken("Google")); // "google" ``` ### Response #### Success Response (200) The normalized string. #### Response Example ```json "google" ``` ## tokenizeWithLowercase() ### Description Tokenizes text into words and automatically normalizes them to lowercase. ### Method `tokenizeWithLowercase(text: string): string[]` ### Endpoint N/A (Library function) ### Parameters #### Path Parameters None #### Query Parameters None #### Request Body None ### Request Example ```typescript import { tokenizeWithLowercase } from "langextract"; const text = "John Smith works at Google Inc."; const lowercaseTokens = tokenizeWithLowercase(text); console.log(lowercaseTokens); // ["john", "smith", "works", "at", "google", "inc", "."] ``` ### Response #### Success Response (200) An array of lowercase token strings. #### Response Example ```json ["john", "smith", "works", "at", "google", "inc", "."] ``` ``` -------------------------------- ### Generate and Save Visualization Source: https://github.com/kmbro/langextract-typescript/blob/main/README.md Generate an HTML visualization of extraction results and save it to a file. Customize visualization options like animation speed and legend visibility. ```typescript import { visualize, saveVisualizationPage } from "langextract"; // Generate and save visualization saveVisualizationPage(result, "./extraction-viz.html", { animationSpeed: 1.0, showLegend: true, gifOptimized: true }); ``` -------------------------------- ### Prompt Engineering Source: https://github.com/kmbro/langextract-typescript/blob/main/README.md Customizing prompts for specific extraction tasks. ```APIDOC ## Custom Prompt Templates ### Description Define custom prompt templates for structured extraction. ### Code Example ```typescript import { PromptTemplateStructured, QAPromptGeneratorImpl } from "langextract"; const template: PromptTemplateStructured = { description: "Extract medical entities from clinical text", examples: [ { text: "Patient has diabetes and hypertension", extractions: [ { extractionClass: "condition", extractionText: "diabetes", }, { extractionClass: "condition", extractionText: "hypertension", }, ], }, ], }; const generator = new QAPromptGeneratorImpl(template); const prompt = generator.render("Patient shows signs of asthma"); ``` ``` -------------------------------- ### Run LangExtract TypeScript Tests in Watch Mode Source: https://github.com/kmbro/langextract-typescript/blob/main/TEST_COVERAGE.md Continuously run the TypeScript tests as file changes are detected using npm. This is ideal for TDD workflows and rapid feedback. ```bash npm run test:watch ``` -------------------------------- ### LangExtract TypeScript - Core Types Source: https://github.com/kmbro/langextract-typescript/blob/main/README.md Defines the core data structures used by the LangExtract library, such as `ExampleData`. ```APIDOC ## Core Types ### `ExampleData` Interface #### Description Represents a single example used to guide the LLM during the extraction process. It includes the source text and the expected extractions. #### Fields - **text** (string) - Required - The input text for this example. - **extractions** (Extraction[]) - Required - An array of extraction objects representing the expected output for the given text. #### TypeScript Definition ```typescript interface ExampleData { text: string; extractions: Extraction[]; } ``` ### `Extraction` Interface (Implicitly used in `ExampleData`) #### Description Represents a single extracted piece of information. #### Fields (Commonly observed, may vary based on usage) - **extractionClass** (string) - The category or class of the extracted entity. - **extractionText** (string) - The actual text that was extracted. - **attributes** (object) - Key-value pairs representing attributes of the extracted entity. - **charInterval** (object) - Specifies the start and end character positions of the extracted text within the original document. - **startPos** (number) - The starting character index. - **endPos** (number) - The ending character index. - **alignmentStatus** (string) - Indicates how well the extraction aligns with the original text (e.g., "match_exact"). #### TypeScript Definition (Illustrative) ```typescript interface Extraction { extractionClass: string; extractionText: string; attributes?: { [key: string]: any }; charInterval?: { startPos: number; endPos: number; }; alignmentStatus?: string; } ``` ```