跳到内容

LLM 在数据理解方面表现出色,这带来了它们最重要的用例之一:将常规人类语言(我们称之为非结构化数据)转化为特定、规则、预期的格式,供计算机程序使用。我们将此过程的输出称为结构化数据。由于在转换过程中通常会忽略大量冗余数据,我们称之为提取

LlamaIndex 中结构化数据提取的核心是 Pydantic 类:您在 Pydantic 中定义一个数据结构,LlamaIndex 与 Pydantic 协同工作,以强制 LLM 的输出符合该结构。

什么是 Pydantic?#

Pydantic 是一个广泛使用的数据验证和转换库。它高度依赖 Python 类型声明。Pydantic 项目文档中有关于 Pydantic 的详尽指南,但我们将在此处介绍最基础的内容。

要创建 Pydantic 类,请继承 Pydantic 的 BaseModel

在此示例中,您创建了一个具有两个字段 idnameUser 类。您将 id 定义为整数,将 name 定义为默认为 Jane Doe 的字符串。

from pydantic import BaseModel


class User(BaseModel):
    id: int
    name: str = "Jane Doe"

您可以通过嵌套这些模型来创建更复杂的结构

现在 Spam 具有 foobarsFoo 具有 count 和可选的 sizebars 是一个对象列表,每个对象都有一个 applebanana 属性。

from typing import List, Optional
from pydantic import BaseModel


class Foo(BaseModel):
    count: int
    size: Optional[float] = None


class Bar(BaseModel):
    apple: str = "x"
    banana: str = "y"


class Spam(BaseModel):
    foo: Foo
    bars: List[Bar]

将 Pydantic 对象转换为 JSON 模式#

Pydantic 支持将 Pydantic 类转换为符合 流行标准 的 JSON 序列化模式对象。例如,上面的 User 类序列化为以下内容

此属性至关重要:这些 JSON 格式的模式通常传递给 LLM,LLM 反过来将其用作返回数据的指令。

{
  "properties": {
    "id": {
      "title": "Id",
      "type": "integer"
    },
    "name": {
      "default": "Jane Doe",
      "title": "Name",
      "type": "string"
    }
  },
  "required": [
    "id"
  ],
  "title": "User",
  "type": "object"
}

使用注解#

如前所述,LLM 使用 Pydantic 中的 JSON 模式作为返回数据的指令。为了帮助它们并提高返回数据的准确性,包含对象和字段及其用途的自然语言描述会很有帮助。Pydantic 支持使用 文档字符串 (docstrings)字段 (Fields) 来实现这一点。

在接下来所有的示例中,我们将使用以下 Pydantic 示例类

这会扩展为一个更复杂的 JSON 模式

from datetime import datetime


class LineItem(BaseModel):
    """A line item in an invoice."""

    item_name: str = Field(description="The name of this item")
    price: float = Field(description="The price of this item")


class Invoice(BaseModel):
    """A representation of information from an invoice."""

    invoice_id: str = Field(
        description="A unique identifier for this invoice, often a number"
    )
    date: datetime = Field(description="The date this invoice was created")
    line_items: list[LineItem] = Field(
        description="A list of all the items in this invoice"
    )

现在您对 Pydantic 及其生成的模式有了基本的了解,您可以继续使用 Pydantic 类在 LlamaIndex 中进行结构化数据提取,首先从 结构化 LLM 开始。

{
  "$defs": {
    "LineItem": {
      "description": "A line item in an invoice.",
      "properties": {
        "item_name": {
          "description": "The name of this item",
          "title": "Item Name",
          "type": "string"
        },
        "price": {
          "description": "The price of this item",
          "title": "Price",
          "type": "number"
        }
      },
      "required": [
        "item_name",
        "price"
      ],
      "title": "LineItem",
      "type": "object"
    }
  },
  "description": "A representation of information from an invoice.",
  "properties": {
    "invoice_id": {
      "description": "A unique identifier for this invoice, often a number",
      "title": "Invoice Id",
      "type": "string"
    },
    "date": {
      "description": "The date this invoice was created",
      "format": "date-time",
      "title": "Date",
      "type": "string"
    },
    "line_items": {
      "description": "A list of all the items in this invoice",
      "items": {
        "$ref": "#/$defs/LineItem"
      },
      "title": "Line Items",
      "type": "array"
    }
  },
  "required": [
    "invoice_id",
    "date",
    "line_items"
  ],
  "title": "Invoice",
  "type": "object"
}

回到顶部