NSDT工具推荐Three.js AI纹理开发包 - YOLO合成数据生成器 - GLTF/GLB在线编辑 - 3D模型格式在线转换 - 可编程3D场景编辑器 - REVIT导出3D模型插件 - 3D模型语义搜索引擎 - Three.js虚拟轴心开发包 - 3D模型在线减面 - STL模型在线切割

大型语言模型 (LLM) 通常被描述为生成人工智能 (GenAI),因为它们确实具有生成文本的能力。LLM 的第一个流行应用是聊天机器人,其中 ChatGPT 处于领先地位。然后我们将其视野扩展到其他任务,例如语义搜索和检索增强生成 (RAG)。今天,我想谈谈 LLM 的一个新兴应用,即结构化非结构化数据,为此我将向你展示一个示例,即将原始文本结构化为 JSON 数据。

使用 LLM 进行数据结构化和提取是一个非常有前途的应用,具有很大的潜力。原因如下:

  • 提高准确性:LLM 了解人类语言的细微差别。这使它们能够比传统的基于规则的系统更准确地识别杂乱、非结构化文本中的关键信息
  • 自动化潜力:从非结构化数据中提取信息可能是一项耗时且费力的任务。 LLM 可以自动化此过程,释放人力资源以执行其他任务,并允许更快地分析更大的数据集。
  • 适应性和学习能力:另一方面,LLM 可以不断微调和调整以处理新的数据源和信息类型。随着它们接触到更多的非结构化数据,它们可以学习并提高识别模式和提取相关信息的能力。
  • 业务成果:大量有价值的信息存在于非结构化文本数据源中,例如电子邮件、客户评论、社交媒体对话和内部文档。但是,这些数据通常很难分析。LLM 可以通过将非结构化数据转换为结构化格式来释放这种隐藏的潜力。这使企业能够利用强大的分析工具来识别趋势并获得见解。从本质上讲,通过使用 LLM 构造非结构化数据,企业可以将负债(不可用的数据)转化为资产(有价值的见解),从而推动更好的决策并改善整体业务成果。

1、一个例子

最近,我正在为一个个人项目搜索一个开源食谱数据集,但除了这个包含 publicdomainrecipes.com 上显示的食谱的 github 存储库之外,我找不到任何其他数据集。

不幸的是,我需要一个更易于利用的数据集,即更接近表格数据或 NoSQL 文档的数据集。因此,我想找到一种方法将原始数据转换为更适合我需求的数据,而无需花费数小时、数天和数周的时间手动完成。

让我向你展示我如何利用大型语言模型的强大功能来自动将原始文本转换为结构化文档。

2、数据集

原始数据集是 markdown 文件的集合。每个文件代表一个配方:

标题 日期 标签 作者
法式薄饼 2021-03-14 法式、甜点、早餐、奶酪美食 aeredren

像煎饼,但非常薄。

15 个薄饼。

配料:

  • 300 克白面粉
  • 3 个鸡蛋
  • 60 毫升牛奶
  • 20 毫升啤酒
  • 30 克黄油或 3 汤匙油

做法:

  • 将面粉、盐、鸡蛋和融化的黄油或油混合。(这样可以避免面粉变干)
  • 慢慢加入牛奶和啤酒,直到面团变得足够稀。(从勺子里倒出来时必须很稀)
    静置一小时。(可选)
  • 用平底锅烹制薄饼,一次一勺。

提示:

  • 经常用沾油的布给平底锅上油。
  • 等锅热了再烹制第一个薄饼。
  • 等薄饼干了再翻面。
  • 倒面团时,用勺子顺着锅的旋转均匀地铺上一层薄薄的面团。

如你所见,这并不是完全非结构化的,文件顶部有很好的表格元数据,然后有 4 个不同的部分:

  • 简介,
  • 配料列表
  • 说明
  • 一些提示。

基于这一观察,Sebastian Bahr 开发了一个解析器,将 markdown 文件转换为 JSON。

解析器的输出已经更具可利用性,此外 Sebastian 还用它来构建食谱推荐聊天机器人。然而,仍然存在一些缺点。配料和说明键包含可以更好地结构化的原始文本。
按原样,一些有用的信息被隐藏了。

例如,配料的数量、每个步骤的准备或烹饪时间。

3、代码

在本文的其余部分,我将展示获取如下所示的 JSON 文档所采取的步骤。

{
    "name": "Crêpes",
    "serving_size": 4,
    "ingredients": [
        {
            "id": 1,
            "name": "white flour",
            "quantity": 300.0,
            "unit": "g"
        },
        {
            "id": 2,
            "name": "eggs",
            "quantity": 3.0,
            "unit": "unit"
        },
        {
            "id": 3,
            "name": "milk",
            "quantity": 60.0,
            "unit": "cl"
        },
        {
            "id": 4,
            "name": "beer",
            "quantity": 20.0,
            "unit": "cl"
        },
        {
            "id": 5,
            "name": "butter",
            "quantity": 30.0,
            "unit": "g"
        }
    ],
    "steps": [
        {
            "number": 1,
            "description": "Mix flour, eggs, and melted butter in a bowl.",
            "preparation_time": null,
            "cooking_time": null,
            "used_ingredients": [1,2,5]
        },
        {
            "number": 2,
            "description": "Slowly add milk and beer until the dough becomes fluid enough.",
            "preparation_time": 5,
            "cooking_time": null,
            "used_ingredients": [3,4]
        },
        {
            "number": 3,
            "description": "Let the dough rest for one hour.",
            "preparation_time": 60,
            "cooking_time": null,
            "used_ingredients": []
        },
        {
            "number": 4,
            "description": "Cook the crêpe in a flat pan, one ladle at a time.",
            "preparation_time": 10,
            "cooking_time": null,
            "used_ingredients": []
        }
    ]
}

重现本教程的代码位于 GitHub 上。

我依靠两个强大的库 langchain 与 LLM 提供商进行通信,并依靠 pydantic 格式化 LLM 的输出。

首先,我使用 Ingredient 和 Step 类定义了配方的两个主要组成部分。

在每个类中,我定义了相关属性并提供了字段和示例的描述。然后,langchain 将它们输入到 LLM 中,从而获得更好的结果。

"""`schemas.py`"""

from pydantic import BaseModel, Field, field_validator

class Ingredient(BaseModel):
    """Ingredient schema"""

    id: int = Field(
        description="Randomly generated unique identifier of the ingredient",
        examples=[1, 2, 3, 4, 5, 6],
    )
    name: str = Field(
        description="The name of the ingredient", 
        examples=["flour", "sugar", "salt"]
    )
    quantity: float | None = Field(
        None,
        description="The quantity of the ingredient",
        examples=[200, 4, 0.5, 1, 1, 1],
    )
    unit: str | None = Field(
        None,
        description="The unit in which the quantity is specified",
        examples=["ml", "unit", "l", "unit", "teaspoon", "tablespoon"],
    )

    @field_validator("quantity", mode="before")
    def parse_quantity(cls, value: float | int | str | None):
        """Converts the quantity to a float if it is not already one"""

        if isinstance(value, str):
            try:
                value = float(value)
            except ValueError:
                try:
                    value = eval(value)
                except Exception as e:
                    print(e)
                    pass

        return value


class Step(BaseModel):
    number: int | None = Field(
        None,
        description="The position of the step in the recipe",
        examples=[1, 2, 3, 4, 5, 6],
    )
    description: str = Field(
        description="The action that needs to be performed during that step",
        examples=[
            "Preheat the oven to 180°C",
            "Mix the flour and sugar in a bowl",
            "Add the eggs and mix well",
            "Pour the batter into a greased cake tin",
            "Bake for 30 minutes",
            "Let the cake cool down before serving",
        ],
    )
    preparation_time: int | None = Field(
        None,
        description="The preparation time mentioned in the step description if any.",
        examples=[5, 10, 15, 20, 25, 30],
    )
    cooking_time: int | None = Field(
        None,
        description="The cooking time mentioned in the step description if any.",
        examples=[5, 10, 15, 20, 25, 30],
    )
    used_ingredients: list[int] = Field(
        [],
        description="The list of ingredient ids used in the step",
        examples=[[1, 2], [3, 4], [5, 6], [7, 8], [9, 10], [11, 12]],
    )


class Recipe(BaseModel):
    """Recipe schema"""

    name: str = Field(
        description="The name of the recipe",
        examples=[
            "Chocolate Cake",
            "Apple Pie",
            "Pasta Carbonara",
            "Pumpkin Soup",
            "Chili con Carne",
        ],
    )
    serving_size: int | None = Field(
        None,
        description="The number of servings the recipe makes",
        examples=[1, 2, 4, 6, 8, 10],
    )
    ingredients: list[Ingredient] = []
    steps: list[Step] = []
    total_preparation_time: int | None = Field(
        None,
        description="The total preparation time for the recipe",
        examples=[5, 10, 15, 20, 25, 30],
    )
    total_cooking_time: int | None = Field(
        None,
        description="The total cooking time for the recipe",
        examples=[5, 10, 15, 20, 25, 30],
    )
    comments: list[str] = []

4、技术细节

重要的是不要使用过于严格的模型,否则 LLM 输出的 JSON 的 pydantic 验证将失败。提供一些灵活性的一个好方法是根据目标输出类型提供默认值,如 None 或空列表 []。

请注意,Ingredient 的 quantity 属性上的 field_validator 可以帮助引擎解析数量。它最初并不存在,但通过进行一些试验,我发现 LLM 通常以字符串形式提供数量,例如 1/3 或 1/2。

used_ingredients 允许将成分正式链接到食谱的相关步骤。

输出的模型被定义,其余过程非常顺利。

在 prompt.py 文件中,我定义了一个 create_prompt 函数来轻松生成提示。每个食谱都会生成一个“新”提示。所有提示都有相同的基础,但食谱本身作为变量传递给基本提示以创建一个新的提示。

""" `prompt.py`

The import statements and the create_prompt function have not been included 
in this snippet.
"""
# Note : Extra spaces have been included here for readability.

DEFAULT_BASE_PROMPT = """
What are the ingredients and their associated quantities 
as well as the steps to make the recipe described 
by the following {ingredients} and {steps} provided as raw text ?

In particular, please provide the following information:
- The name of the recipe
- The serving size
- The ingredients and their associated quantities
- The steps to make the recipe and in particular, the duration of each step
- The total duration of the recipe broken 
down into preparation, cooking and waiting time. 
The totals must be consistent with the sum of the durations of the steps. 
- Any additional comments

{format_instructions}
Make sure to provide a valid and well-formatted JSON.

"""

与 LLM 逻辑的通信是在 core.py 文件的运行函数中定义的,为了简洁起见,这里就不展示了。

最后,我将所有这些组件组合在 mydemo.ipynbnotebook 中,其内容如下所示。

# demo.ipynb
import os
from pathlib import Path

import pandas as pd
from langchain.output_parsers import PydanticOutputParser
from langchain_mistralai.chat_models import ChatMistralAI
from dotenv import load_dotenv

from core import run
from prompt import DEFAULT_BASE_PROMPT, create_prompt
from schemas import Recipe
 # End of first cell

# Setup environment
load_dotenv()
MISTRAL_API_KEY = os.getenv("MISTRAL_API_KEY") #1
 # End of second cell
 
# Load the data
path_to_data = Path(os.getcwd()) / "data" / "input" #2
df = pd.read_json("data/input/recipes_v1.json")
df.head()
 # End of third cell

# Preparing the components of the system
llm = ChatMistralAI(api_key=MISTRAL_API_KEY, model_name="open-mixtral-8x7b")
parser = PydanticOutputParser(pydantic_object=Recipe)
prompt = create_prompt(
    DEFAULT_BASE_PROMPT, 
    parser, 
    df["ingredients"][0], 
    df["direction"][0]
)
#prompt  
  # End of fourth cell

# Combining the components  
example = await run(llm, prompt, parser)
#example
 # End of fifth cell

我使用 MistralAI 作为 LLM 提供商,其 open-mixtral-8x7b 模型是 OpenAI 的一个非常好的开源替代方案。langchain 允许你轻松切换提供商,前提是你已在提供商平台上创建帐户。

如果你尝试重现结果:

  • 确保你在 .env 文件或操作系统环境中有一个 MISTRAL_API_KEY。
  • 注意数据路径。如果您克隆我的存储库,这将不是问题。

在整个数据集上运行代码的成本不到 2 欧元。此代码生成的结构化数据集可以在我的存储库中找到。

我对结果很满意,但我仍然可以尝试迭代提示、我的字段描述或用于改进它们的模型。我可能会尝试 MistralAI 较新的模型 open-mixtral-8x22b,或者尝试另一个 LLM 提供商,只需在 langchain 上更改 2 或 3 行代码即可。

5、结束语

大型语言模型 (LLM) 为结构化非结构化数据提供了强大的工具。它们能够理解和解释人类语言的细微差别、自动执行繁琐的任务并适应不断变化的数据,这使它们成为数据分析中无价的资源。通过释放非结构化文本数据中隐藏的潜力,企业可以将这些数据转化为有价值的见解,从而推动更好的决策和业务成果。所提供的将原始食谱数据转换为结构化格式的示例只是 LLM 提供的无数可能性之一。

随着我们继续探索和开发这些模型,我们可以期待在未来看到更多创新应用。充分利用 LLM 潜力的旅程才刚刚开始,未来的道路将是令人兴奋的。


原文链接:The (lesser known) rising application of LLMs

BimAnt翻译整理,转载请标明出处