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