Node.js SDK(非官方)

LangExtract 信息提取的 JavaScript/TypeScript 客户端

重要提示

这是 LangExtract 的非官方 Node.js SDK。它与 Google 没有关联或认可。有关官方 Python 库,请访问 官方 GitHub 仓库.

安装

使用 npm 或 yarn 安装 LangExtract Node.js SDK:

# npm
npm install langextract

# yarn
yarn add langextract

# pnpm
pnpm add langextract

快速开始

通过这个简单的示例开始在 Node.js 中使用 LangExtract:

基本用法

使用 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: process.env.LANGEXTRACT_API_KEY
  });
  
  console.log(result.extractions);
}

配置

API 密钥设置

为 Gemini 等云端模型设置您的 API 密钥:

# .env 文件
LANGEXTRACT_API_KEY=your-api-key-here

支持的模型

SDK 支持多个 LLM 提供商:

  • Google Gemini 模型(推荐)
  • OpenAI GPT 模型
  • 本地 Ollama 模型

API 参考

extract()

从文本中提取信息的主要方法

async function extract(
  text: string,
  options: {
    promptDescription: string;
    examples: ExampleData[];
    modelType: "gemini" | "openai" | "ollama";
    apiKey?: string;
    modelId?: string;
  }
): Promise<ExtractionResult>

visualize()

生成提取结果的交互式 HTML 可视化

function visualize(
  results: ExtractionResult[],
  options?: {
    theme?: "light" | "dark";
    highlightColors?: string[];
  }
): string

batchExtract()

并行处理多个文档

async function batchExtract(
  documents: string[],
  options: ExtractOptions & {
    concurrency?: number;
    onProgress?: (completed: number, total: number) => void;
  }
): Promise<ExtractionResult[]>

代码示例

基本提取

从简单文本中提取实体

const result = await extract("The product costs $99.99", {
  promptDescription: "Extract product price",
  examples: [{
    text: "Price: $50",
    extractions: [{
      extractionClass: "price",
      extractionText: "$50"
    }]
  }],
  modelType: "gemini"
});

高级功能

使用属性和自定义提取类

const examples = [{
  text: "Dr. Smith prescribed 500mg aspirin",
  extractions: [{
    extractionClass: "medication",
    extractionText: "aspirin",
    attributes: {
      dosage: "500mg",
      prescriber: "Dr. Smith"
    }
  }]
}];

批处理

高效处理多个文档

const documents = [
  "Document 1 content...",
  "Document 2 content...",
  "Document 3 content..."
];

const results = await batchExtract(documents, {
  promptDescription: "Extract entities",
  examples: examples,
  modelType: "gemini",
  concurrency: 3,
  onProgress: (done, total) => {
    console.log(`Progress: ${done}/${total}`);
  }
});

可视化

生成并保存交互式可视化

const html = visualize(results, {
  theme: "light",
  highlightColors: ["#3B82F6", "#10B981", "#F59E0B"]
});

// Save to file
import { writeFileSync } from "fs";
writeFileSync("results.html", html);

TypeScript 支持

包含类型定义的完整 TypeScript 支持:

  • 所有 API 方法的完整类型定义
  • VS Code 和其他 IDE 中的 IntelliSense 支持
  • 类型安全的提取结果
  • 自定义提取类的泛型类型

错误处理

正确的错误处理模式:

错误处理示例

处理 API 错误和验证失败

try {
  const result = await extract(text, options);
  console.log("Extraction successful:", result);
} catch (error) {
  if (error.code === "INVALID_API_KEY") {
    console.error("Invalid API key provided");
  } else if (error.code === "RATE_LIMIT_EXCEEDED") {
    console.error("Rate limit exceeded, retry after:", error.retryAfter);
  } else if (error.code === "VALIDATION_ERROR") {
    console.error("Validation error:", error.details);
  } else {
    console.error("Unexpected error:", error.message);
  }
}

最佳实践

  • 1提供高质量的示例以获得更好的提取准确性
  • 2为多个文档使用批处理
  • 3缓存提取结果以减少 API 调用
  • 4实现适当的错误处理和重试
  • 5使用环境变量存储 API 密钥

准备开始了吗?

使用 pip 安装 LangExtract,几分钟内即可开始从文本数据中提取结构化信息。

npm install langextract