跳到内容

Textractor

pipeline pipeline

Textractor 流水线从文档中提取和分割文本。此流水线扩展了 Segmentation 流水线。

每个文档都经过以下过程。

  • 如果内容不是本地的,则进行检索
  • 如果文档的 mime-type 不是纯文本或 HTML,则通过 FiletoHTML 流水线将其转换为 HTML
  • 通过 HTMLToMarkdown 流水线将 HTML 转换为 Markdown
  • 内容根据 分段参数 进行分割/分块并返回

backend 参数设置 FileToHTML 流水线的后端。如果后端不可用,此流水线假定输入是 HTML 内容,并且仅将其转换为 Markdown。

请参阅 FiletoHTMLHTMLToMarkdown 流水线,了解每个流水线所需的依赖项。

示例

以下显示了使用此流水线的一个简单示例。

from txtai.pipeline import Textractor

# Create and run pipeline
textract = Textractor()
textract("https://github.com/neuml/txtai")

有关更详细的示例,请参阅以下链接。

笔记本 描述
从文档中提取文本 从 PDF、Office、HTML 等提取文本 Open In Colab
为 RAG 分块数据 提取、分块和索引内容以进行有效检索 Open In Colab

配置驱动示例

流水线可以使用 Python 或配置运行。流水线可以在 配置 中使用流水线的小写名称进行实例化。配置驱动的流水线可以使用 工作流API 运行。

config.yml

# Create pipeline using lower case class name
textractor:

# Run pipeline with workflow
workflow:
  textract:
    tasks:
      - action: textractor

使用工作流运行

from txtai import Application

# Create and run pipeline with workflow
app = Application("config.yml")
list(app.workflow("textract", ["https://github.com/neuml/txtai"]))

使用 API 运行

CONFIG=config.yml uvicorn "txtai.api:app" &

curl \
  -X POST "http://localhost:8000/workflow" \
  -H "Content-Type: application/json" \
  -d '{"name":"textract", "elements":["https://github.com/neuml/txtai"]}'

方法

流水线的 Python 文档。

__init__(sentences=False, lines=False, paragraphs=False, minlength=None, join=False, sections=False, cleantext=True, chunker=None, headers=None, backend='available', **kwargs)

源代码位于 txtai/pipeline/data/textractor.py
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
def __init__(
    self,
    sentences=False,
    lines=False,
    paragraphs=False,
    minlength=None,
    join=False,
    sections=False,
    cleantext=True,
    chunker=None,
    headers=None,
    backend="available",
    **kwargs
):
    super().__init__(sentences, lines, paragraphs, minlength, join, sections, cleantext, chunker, **kwargs)

    # Get backend parameter - handle legacy tika flag
    backend = "tika" if "tika" in kwargs and kwargs["tika"] else None if "tika" in kwargs else backend

    # File to HTML pipeline
    self.html = FileToHTML(backend) if backend else None

    # HTML to Markdown pipeline
    self.markdown = HTMLToMarkdown(self.paragraphs, self.sections)

    # HTTP headers
    self.headers = headers if headers else {}

__call__(text)

将文本分段成语义单元。

此方法支持字符串或列表形式的文本输入。如果输入是字符串,返回类型为 text|list。如果输入是列表,返回的也是一个列表,这取决于分词策略,返回列表可能是文本列表或列表的列表。

参数

名称 类型 描述 默认值
text

文本|列表

必填

返回值

类型 描述

分段后的文本

源代码位于 txtai/pipeline/data/segmentation.py
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
def __call__(self, text):
    """
    Segments text into semantic units.

    This method supports text as a string or a list. If the input is a string, the return
    type is text|list. If text is a list, a list of returned, this could be a
    list of text or a list of lists depending on the tokenization strategy.

    Args:
        text: text|list

    Returns:
        segmented text
    """

    # Get inputs
    texts = [text] if not isinstance(text, list) else text

    # Extract text for each input file
    results = []
    for value in texts:
        # Get text
        value = self.text(value)

        # Parse and add extracted results
        results.append(self.parse(value))

    return results[0] if isinstance(text, str) else results