LLMParser 开源项目教程
1、项目介绍
LLMParser 是一个简单且灵活的工具,用于通过大型语言模型(LLMs)从文本中分类和提取结构化数据。尽管大型语言模型非常强大,但生成可靠的 JSON 输出仍然具有挑战性。LLMParser 旨在通过强制执行一致的 JSON 输入和输出格式来解决这一问题,从而实现对文本的分类和提取。
2、项目快速启动
安装
首先,通过 npm 安装 LLMParser:
npm install llmparser
使用示例
以下是一个简单的使用示例,展示了如何使用 LLMParser 解析一个 PDF 文件并提取信息:
import { LLMParser } from 'llmparser';
const categories = [
{
name: "MSA",
description: "Master service agreement"
},
{
name: "NDA",
description: "Non disclosure agreement",
fields: [
{
name: "effective_date",
description: "effective date or start date",
type: "string"
},
{
name: "company",
description: "name of the company",
type: "string"
},
{
name: "counterparty",
description: "name of the counterparty",
type: "string"
}
]
}
];
const parser = new LLMParser({
categories,
apiKey: process.env.OPENAI_API_KEY
});
const ndaText = await loadPDFAsText("src/nda.pdf"); // 获取 PDF 文本
const extraction = await parser.parse(ndaText);
console.log(extraction);
3、应用案例和最佳实践
应用案例
- 提取简历信息:从简历中提取姓名、学校、当前工作职位等信息。
- 分类合同:将公司合同分类为 NDA、MSA 等,并提取重要字段如生效日期和对方公司名称。
- 提取地点名称:从 Apple Notes 中提取地点名称。
最佳实践
- 确保 API 密钥安全:不要在客户端代码中暴露 API 密钥,确保在服务器端使用。
- 定义清晰的分类和字段:在配置
categories
时,确保每个分类和字段都有清晰的描述和类型定义。
4、典型生态项目
LLMParser 可以与其他开源项目结合使用,以增强其功能和应用范围。以下是一些典型的生态项目:
- PDF 解析库:如
pdf-parse
或pdfjs
,用于从 PDF 文件中提取文本。 - 自然语言处理库:如
spaCy
或NLTK
,用于进一步处理和分析提取的文本数据。 - 数据存储和分析工具:如
Elasticsearch
或MongoDB
,用于存储和分析提取的结构化数据。
通过结合这些生态项目,LLMParser 可以实现更复杂和强大的文本处理和数据提取功能。