> ## Content Index
> Fetch the complete content index at: https://www.hubwiz.com/blog/llms.txt
> Use this file to discover other available public pages before exploring further.

# NuExtract简明教程
- URL: https://www.hubwiz.com/blog/nuextract-concise-tutorial/
- Published: 2025-01-04T02:40:32.000Z
- Updated: 2025-01-04T02:40:32.000Z
- Description: NuExtract 是专为结构化提取任务而设计的专用 LLM，本文介绍如何使用NuExtract提取文本中的结构化数据。
- Author: admin
- Tags: MODEL-ZOO, NUEXTRACT, DATA EXTRACTION, PYLLMCORE, #Import 2026-10-14 14:41

NuExtract 是专为结构化提取任务而设计的专用 LLM，本文介绍如何使用NuExtract提取文本中的结构化数据。

要开始使用 NuExtract 模型解析和提取文本中的结构化数据，首先需要设置环境。这涉及安装必要的库并确保具有所需的依赖项。

## 1、安装 PyLLMCore

PyLLMCore 是一个开源 Python 库 (MIT)，可与各种大型语言模型 (LLM) 交互，并提供用于解析和提取数据的简单 API。要安装 PyLLMCore，请在终端中运行以下命令：

```
# You need Python 3.10
python3 -m venv venv
source venv/bin/activate

# Important: Some versions need to be pinned
pip3 install py-llm-core==2.8.15
pip3 install mistralai==0.4.2
```

## 2、下载 NuExtract 模型

NuExtract 是专为结构化提取任务而设计的专用 LLM。你可以从 Hugging Face 下载 NuExtract 模型。对于此示例，我们将使用 NuExtract-tiny 模型。运行以下命令下载并存储模型：

```
mkdir -p ~/.cache/py-llm-core/models
cd ~/.cache/py-llm-core/models
wget -O nuextract-tiny https://huggingface.co/advanced-stack/NuExtract-tiny-GGUF/resolve/main/nuextract-tiny-f16.gguf?download=true
```

## 3、导入所需的库

下载模型后，你现在可以在 Python 脚本中导入必要的库：

```
from dataclasses import dataclass, field
from llm_core.parsers import NuExtractParser
```

> 要使用 NuExtract 模型从文本中解析和提取结构化数据，需要定义一个数据类来表示要提取的数据的结构。 PyLLMCore 将使用此数据类为提取过程生成 JSON 模式。

## 4、定义数据类

对于此示例，让我们创建一个产品数据类，其中包含产品名称、价格和说明字段：

```
from dataclasses import dataclass

@dataclass
class Product:
    name: str = ""
    price: str = ""
    description: str = ""
```

## 5、加载和解析文本

接下来，你需要加载要解析和提取数据的文本。对于此示例，我们将使用示例产品描述：

```
text = """
Introducing the new SuperWidget! This innovative gadget is priced at .99 and offers a range of features to make your life easier. With its sleek design and user-friendly interface, the SuperWidget is perfect for tech enthusiasts and casual users alike.
"""
```

## 6、使用 NuExtract 解析文本

最后，你可以使用 NuExtractParser 类解析文本并提取结构化数据：

```
from llm_core.parsers import NuExtractParser

with NuExtractParser(Product) as parser:
    product = parser.parse(text)
    print(product)
```

输出将是包含提取数据的 Product 数据类的实例：

```
Product(
    name='SuperWidget',
    price='.99',
    description='This innovative gadget offers a range of features to make your life easier. With its sleek design and user-friendly interface, the SuperWidget is perfect for tech enthusiasts and casual users alike.'
)
```

> 为了展示 NuExtract 的强大功能，让我们解析包含多个产品的文本。我们将扩展我们的产品数据类以包含产品列表并解析更复杂的文本。

## 7、定义扩展数据类

我们将创建一个包含产品实例列表的 Catalog 数据类：

```
@dataclass
class Catalog:
    products: list[Product] = field(default_factory=lambda: [Product()])
```

## 8、加载和解析扩展文本

现在，让我们加载包含多个产品描述的文本：

```
text = """
Introducing the new SuperWidget! This innovative gadget is priced at .99 and offers a range of features to make your life easier. With its sleek design and user-friendly interface, the SuperWidget is perfect for tech enthusiasts and casual users alike.

Check out the MegaGizmo! At just 2.59, this device packs a punch with its powerful performance and compact size. Ideal for on-the-go professionals and students, the MegaGizmo is a must-have.

Don't miss the UltraTool! Priced at 3.49, this versatile tool is perfect for DIY enthusiasts and professionals. With its robust build and multiple functionalities, the UltraTool is your go-to solution for any task.
"""
```

## 9、使用 NuExtract 解析扩展文本

最后，使用 NuExtractParser 类解析文本并提取结构化数据：

```
with NuExtractParser(Catalog) as parser:
    catalog = parser.parse(text)
    for product in catalog.products:
        print(product)
```

输出将是包含提取数据的 Product 实例列表：

```
Product(
    name='SuperWidget',
    price='.99',
    description='This innovative gadget offers a range of features to make your life easier. With its sleek design and user-friendly interface, the SuperWidget is perfect for tech enthusiasts and casual users alike.'
)
Product(
    name='MegaGizmo',
    price='2.59',
    description='This device packs a punch with its powerful performance and compact size. Ideal for on-the-go professionals and students, the MegaGizmo is a must-have.'
)
Product(
    name='UltraTool',
    price='3.49',
    description='This versatile tool is perfect for DIY enthusiasts and professionals. With its robust build and multiple functionalities, the UltraTool is your go-to solution for any task.'
)
```

## 10、结束语

使用 NuExtract 模型，你可以轻松地在本地解析和提取非结构化文本中的结构化数据，因为该模型非常小。特别感谢 NuMind 发布 NuExtract 模型系列，使结构化提取更易于访问和高效。

---

原文链接：[Use NuExtract to parse unstructured text locally in less than 5 min](https://advanced-stack.com/resources/how-to-parse-unstructured-text-with-nuextract.html?ref=hubwiz.com)

汇智网翻译整理，转载请标明出处