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여러 문서에 대해 배치 처리 사용
  • 3API 호출을 줄이기 위해 추출 결과 캐시
  • 4적절한 오류 처리와 재시도 구현
  • 5API 키는 환경 변수 사용

시작할 준비가 되셨나요?

pip로 LangExtract를 설치하고 몇 분 안에 텍스트 데이터에서 구조화된 정보 추출을 시작하세요.

npm install langextract