이것은 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);
}
Gemini와 같은 클라우드 모델을 위한 API 키 설정:
# .env 파일
LANGEXTRACT_API_KEY=your-api-key-here
SDK는 여러 LLM 제공업체를 지원합니다:
텍스트에서 정보를 추출하는 주요 메서드
async function extract(
text: string,
options: {
promptDescription: string;
examples: ExampleData[];
modelType: "gemini" | "openai" | "ollama";
apiKey?: string;
modelId?: string;
}
): Promise<ExtractionResult>
추출 결과의 대화형 HTML 시각화 생성
function visualize(
results: ExtractionResult[],
options?: {
theme?: "light" | "dark";
highlightColors?: string[];
}
): string
여러 문서를 병렬 처리
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 지원:
적절한 오류 처리 패턴:
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);
}
}
pip로 LangExtract를 설치하고 몇 분 안에 텍스트 데이터에서 구조화된 정보 추출을 시작하세요.
npm install langextract