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

ChatGPT风靡全球近一年后,LLM进入了新的发展阶段。 发布后的几个月的特点是急于构建和发布基础技术,而现在的开发重点是商业化。

组织不再急于将人工智能纳入其流程,而是退一步问自己,人工智能能为我们做什么,实际上是对我们现在所拥有的东西的改进?

1、LLM商业化的障碍

通常,这个问题很难回答。 这是因为,从商业角度评估时,LLM在普遍采用方面仍然存在许多障碍:

  • GPU 可用性:无论是像 AWS SageMaker 那样直接计算,还是像 OpenAI 那样打包为每个token成本,GPU 最终都是稀缺商品,而计算密集型 LLM 的运行成本可能相当昂贵。 例如,运行 GPT4 等超大型模型的成本可能会迅速增加,尤其是当你嵌入大型上下文时。
  • 可扩展性:与 GPU 成本相关的是缺乏可扩展性。 大多数企业用例需要重复一个过程数千甚至数百万次。 对于组织来说,部署可能花费数百万美元的新流程非常困难,特别是在已经有足够的现有解决方案的情况下。
  • 数据安全:由于大多数组织缺乏 MLOps 知识或运行自己的 GPU 服务器的资源,因此大多数 LLM 查询都会发送到异地进行处理。 这给许多公司带来了不可接受的安全问题。 像 OpenAI Enterprise 这样的服务缓解了其中一些担忧,但某种违规行为破坏对此类服务的信任只是时间问题。

2、AI小型化

在此背景下,新一波人工智能先驱开始探索小型化LLM的想法。 与 GPT-3.5 相比,GPT-4 的参数数量增加了 10 倍以提高性能,而小型化先驱则试图在保持性能不变的同时将参数数量减少 50 至 100 倍。

如果成功,小型化LLM有可能立即克服所有采用障碍。 由于规模较小,LLM 可以在没有 GPU 的情况下运行,因此变得更具可扩展性。 足够小的模型也可以在内部基础设施上运行,从而使公司能够更好地控制其数据。

在今天的文章中,我们将探讨 LLMWare 公司新发布的几项技术,该公司正在通过为 LLM 应用程序开发开源基础设施来彻底改变人工智能领域。 具体来说,我们将使用 LLMWare 的开源软件和微型模型来解决现实世界的业务问题,以展示该框架的功能。

此外,今天文章中的所有计算都是在 M1 Max 32 GB RAM MacBook Pro 上完成的,数据在任何时候都不会离开笔记本电脑进行处理。 这种设置对于笔记本电脑来说相当强大,但通过云基础设施相对容易复制,因此它应该作为人工智能小型化承诺的良好概念验证。

我们今天将探讨的用例将基于公共数据,但与我所有文章的情况一样,这些技术可以交叉应用于具有专有数据的许多领域,包括科学、医学和金融。

话不多说,让我们今天深入探讨用例。

3、业务问题

在今天的例子中,我们将建立一个立法分析LLM。 通常,在金融或法律研究过程中,人们必须搜索大量法律和立法文本来寻找具体的想法。

虽然许多分析师目前使用纯文本搜索(在复杂的圈子中也称为 control-f)来做到这一点,但这可能非常耗时,因为人们必须浏览许多相关术语并阅读找到的术语周围的大量文本才能确定这个想法是否相关。

一些立法文件也无法搜索,这又带来了另一个复杂性。

我们将使用AI驱动的法律分析器来解决这个问题。 该分析器将使我们能够使用称为语义搜索的技术快速找到文本的相关位,该技术不仅可以找到确切的短语,还可以找到与搜索词语义相似的部分。

然后,搜索结果将输入LLM进行进一步的总结和分析。

更重要的是,该应用程序将能够在一分钟内完成此操作,这比手动文本搜索有了显着改进。

为了测试我们的应用程序,我们将搜索并总结“合格机会区合伙权益”的定义,这是 2017 年《减税和就业法案》中定义的术语。

该应用程序将可以访问《减税和就业法案》的全文,但为了使本次练习的搜索部分更具挑战性,我们将通过向其提供其他几项冗长立法的文本来增加搜索空间。

应用程序不会知道《减税和就业法案》中的定义,因此必须搜索整个语料库才能完成查询。 对于今天的示例,除了《减税和就业法案》之外,我们还下载了《多德弗兰克法案》、《减少通货膨胀法案》和《国家环境政策法案》的 PDF 版本。

现在我们了解了我们要解决的问题,让我们看看我们将用于构建此应用程序的技术。

4、使用的技术

我们使用的主要技术包括LLMWare、RAG和BLING。

4.1 LLMWare

LLMWare 是一个开源框架,可以非常轻松地构建功能强大、可扩展的 RAG 应用程序。 市场上有几个旨在解决类似问题的框架,例如 LangChain 和 LlamaIndex,但有几个因素使 LLMWare 与众不同:

  • 对本地 LLM 应用程序的一流支持:大多数 LLM 框架都是围绕 OpenAI 的 GPT 模型等高性能模型构建的,因此最终成为编排 API 调用的专用框架。 另一方面,LLMWare 可以轻松地在本地运行整个 LLM 基础设施,这使得这些类型的实验变得更加容易。
  • 卓越的可扩展性:LLMWare 的抽象是可组合的,并利用 Python 的内置数据结构。 这使得你可以轻松选择想要使用的组件并添加额外的处理,而无需深入研究框架代码。
  • 大量具有直观抽象的有用实用程序:除了编排和提示工程实用程序之外,LLMWare 还附带自己的 PDF 解析器、OCR 引擎、法律元数据处理引擎和数据存储实用程序。 这些实用程序还以直观的方式进行管理,允许用户通过很少的代码行来利用它们。
  • 与 LLMWare 的嵌入和语言模型集成:LLMWare 使下载和运行其嵌入和语言模型变得非常容易,这些模型针对法律文本语料库进行了优化。

4.2 RAG

检索增强生成(RAAG)是该应用程序的核心,因为该模式允许我们实现几个关键的设计功能,帮助小型化LLM更好地执行。

尽管许多普通 RAG 实现只是将源数据切成大块并使用 OpenAI 的 ada 等通用嵌入模型,但我们应用了以下优化。

  • 专门的语义搜索:我们不使用通用的嵌入模型,而是使用专门的嵌入模型,在本例中为“industry-bert-contracts”模型。 该模型专门针对法律文本进行了训练,从而提高了搜索的准确性。
  • 提示压缩:我们将源数据切成更小的块,以便更好地控制提示的长度。 小语言模型最明显的局限性之一是无法理解长提示,因此重要的是我们只将源材料中最相关的部分作为上下文嵌入到提示中。
  • 上下文最大化:LLMWare 的解析库针对合法内容进行了优化,因此可以智能地对源数据进行分块。 这使我们能够最大限度地增加提示中相关内容的数量,从而大大提高输出的质量。

4.3 BLING

BLING 是 LLMWare 的开源、小型化、CPU 驱动的语言模型的集合。 这些基于开源LLM,参数范围在 10 亿到 30 亿之间。 尽管它们的尺寸非常小,但它们经过专门针对法律文本摘要和提取用例的训练,并且在构建适合该用例的提示时可以令人满意地执行。

尽管这些模型很棒,但它们并不神奇,如果我们过度推动模型,性能就会下降。 因此,在前面的步骤中需要小心,以保持最终输出的质量。

现在我们了解了应用程序背后的技术,让我们开始编码吧!

5、实施教程

LLMWare 框架非常容易设置。 我们首先安装 Python 库。 要运行LLMWare,您只需要安装两个包,依赖项将自动安装。 只需运行:

pip install llmware transformers

请注意,截至撰写本文时,LLMWare 不支持 Python 3.12,因此如果你遇到安装问题,请创建一个基于 Python 3.10 的虚拟环境。

现在 Python 库已安装,让我们安装其他基础设施部分,例如 Milvus 矢量数据库和 MongoDB 文档数据库。 LLMWare 使这变得非常简单,因此你唯一需要安装的软件是 docker。

在 MacOS 上,你需要下载并安装 Docker Desktop,而在其他操作系统上,你需要遵循流行的最佳实践。 安装后,你应该能够在命令行中使用 docker。

一旦确认安装了 docker,你就可以从 LLMWare 运行 docker-compose 文件,一切都会为你设置好。 只需运行以下命令即可。

curl -o docker-compose.yaml https://raw.githubusercontent.com/llmware-ai/llmware/main/docker-compose.yaml
docker compose up -d

如果 docker 安装正确,你应该会看到 docker 容器正在旋转。 现在,一切都应该设置完毕。 要测试你的设置,可以从 LLMWare 的 GitHub 页面运行“快速启动”脚本的截断版本。

import os
from llmware.library import Library
from llmware.retrieval import Query
from llmware.prompts import Prompt
from llmware.setup import Setup

library = Library().create_new_library("Agreements")
sample_files_path = Setup().load_sample_files()
library.add_files(os.path.join(sample_files_path,"Agreements"))

library.install_new_embedding(embedding_model_name="industry-bert-contracts", vector_db="milvus")

os.environ["TOKENIZERS_PARALLELISM"] = "false" # Avoid a HuggingFace tokenizer warning
query_results = Query(library).semantic_query("Termination", result_count=20)

print(query_results)

如果看到从 LLMWare 的语义查询返回的一堆查询结果,则你的计算机已完全设置好构建应用程序!

5.1 索引并创建立法文档向量数据库

我们要做的第一件事是根据我们今天正在分析的立法文本创建一个矢量数据库。 这将帮助我们快速执行语义查询。 如果我们在没有 LLMWare 的情况下执行此操作,则必须对 PDF 文档执行 OCR、提取元数据、对语料库进行分块,并对生成的块运行嵌入模型。

值得庆幸的是,有了 LLMWare,这一切都变得非常简单。 只需将立法的 PDF 转储到文件夹中并运行:

import os
import time
from llmware.library import Library
from llmware.retrieval import Query
from llmware.prompts import Prompt
from llmware.setup import Setup


library = Library().create_new_library("Tax_Cuts_Jobs_Act")
library.add_files([Your folder])
library.install_new_embedding(embedding_model_name="industry-bert-contracts", vector_db="milvus")

如果此操作顺利完成,那么你已经创建了一个嵌入向量数据库! 如果你在 Jupyter Notebook 中运行此命令,那么还可能会看到一个输出,表明已创建数千个嵌入。

现在,我们可以对向量数据库进行语义查询。 出于此应用程序的目的,我们可以简单地使用问题“What is a qualified opportunity zone partnership interest?”来执行查询。 为此,我们只需运行:

query_results = Query(library).semantic_query("What is a qualified opportunity zone partnership interest?", result_count=2)

查询最多需要几秒钟。 打印结果,你应该看到语义搜索检索到的 2 个最相关的部分。 检查元数据,应该看到搜索找到了《减税和就业法案》中的相关部分。

现在我们已经检索了源数据,让我们将其输入 LLMWare 的 BLING 模型,看看它们的表现如何!

5.2 下载并运行 LLMWare BLING 模型

由于 LLMWare BLING 模型托管在 Hugging Face 上,因此下载和运行模型实际上非常容易。 然而,由于 BLING 模型的提示需要特定的结构,因此通过 LLMWare 的 Prompt 对象来管理 BLING 模型的下载和运行会更容易。

要运行模型,理论上我们只需要像这样加载和运行模型:

prompter = Prompt().load_model(model_name, from_hf=True, api_key="")
output = prompter.prompt_main(query, context=embedded_text
                                 , prompt_name="default_with_context",temperature=0.0)

然而,为了获得最佳结果,我们需要对语义查询结果进行一些预处理。 此外,由于我们想要评估 LLMWare 的所有各种 BLING 模型,因此我们需要枚举模型并输出结果。

为此,我们将向提示调用添加一些包装器,如下所示:

embedded_text = ''
for qr in query_results:
   embedded_text += '\n'.join(qr['text'].split("\'\'"))


# check all of the models for performance

model_list = ["llmware/bling-1b-0.1",
             "llmware/bling-1.4b-0.1",
             "llmware/bling-falcon-1b-0.1",
             "llmware/bling-cerebras-1.3b-0.1",
             "llmware/bling-sheared-llama-1.3b-0.1",
             "llmware/bling-sheared-llama-2.7b-0.1",
             "llmware/bling-red-pajamas-3b-0.1",
             ]


# adapted from the BLING demo
query = "What is the definition of qualified opportunity zone partnership interest?"
for model_name in model_list:
    t0 = time.time()
    print(f"\n > Loading Model: {model_name}...")
    prompter = Prompt().load_model(model_name, from_hf=True, api_key="")
    
    t1 = time.time()
    print(f"\n > Model {model_name} load time: {t1-t0} seconds")
    
    print(f"Query: {query}")
    output = prompter.prompt_main(query, context=embedded_text
                                 , prompt_name="default_with_context",temperature=0.0)
    
    llm_response = output["llm_response"].strip("\n")
    print(f"LLM Response: {llm_response}")
    print(f"LLM Usage: {output['usage']}")
    
    t2 = time.time()
    print(f"\nTotal processing time: {t2-t1} seconds")

运行此代码,并观察结果打印到控制台。 当我运行这段代码时,我得到以下结果:

> Loading Model: llmware/bling-1b-0.1...

 > Model llmware/bling-1b-0.1 load time: 10.456622123718262 seconds
Query: What is the definition of qualified opportunity zone partnership interest?
LLM Response:     (i) In general, qualified opportunity zone partnership interest means any capital or profits interest in a domestic partnership if-  owned by the qualified opportunity  fund solely in exchange for cash, and is acquired by the qualified opportunity  fund after December 31, 2017.
LLM Usage: {'input': 611, 'output': 54, 'total': 665, 'metric': 'tokens', 'processing_time': 9.11120319366455}

Total processing time: 9.115610837936401 seconds

 > Loading Model: llmware/bling-1.4b-0.1...

 > Model llmware/bling-1.4b-0.1 load time: 6.793190956115723 seconds
Query: What is the definition of qualified opportunity zone partnership interest?
LLM Response:   Qualified opportunity zone partnership interest is any capital or profits interest in a domestic partnership if the partnership is acquired by the qualified opportunity fund after December 31, 2017, from the partner-ship solely in exchange for cash.
LLM Usage: {'input': 611, 'output': 48, 'total': 659, 'metric': 'tokens', 'processing_time': 12.356989860534668}

Total processing time: 12.36338210105896 seconds

 > Loading Model: llmware/bling-falcon-1b-0.1...

 > Model llmware/bling-falcon-1b-0.1 load time: 6.4988627433776855 seconds
Query: What is the definition of qualified opportunity zone partnership interest?
LLM Response: •Section 1202(c)(3) definition.
LLM Usage: {'input': 645, 'output': 13, 'total': 658, 'metric': 'tokens', 'processing_time': 5.218444108963013}

Total processing time: 5.2250282764434814 seconds

 > Loading Model: llmware/bling-cerebras-1.3b-0.1...

 > Model llmware/bling-cerebras-1.3b-0.1 load time: 15.7601318359375 seconds
Query: What is the definition of qualified opportunity zone partnership interest?
LLM Response: (B) QUALIFIED OPPORTUNITY ZONE STOCK
LLM Usage: {'input': 645, 'output': 17, 'total': 662, 'metric': 'tokens', 'processing_time': 5.469969987869263}

Total processing time: 5.475223064422607 seconds

 > Loading Model: llmware/bling-sheared-llama-1.3b-0.1...

 > Model llmware/bling-sheared-llama-1.3b-0.1 load time: 5.345098972320557 seconds
Query: What is the definition of qualified opportunity zone partnership interest?
LLM Response: (I) such partnership was a qualified opportunity zone busi-  ness (or, in the case of a new partnership, such partner-  ship was being organized for purposes of being a qualified opportunity zone busi-  ness) and
during substantially all of the qualified opportunity fund's holding period for such interest, such partnership qualified as a qualified opportunity zone busi-  ness.
LLM Usage: {'input': 681, 'output': 95, 'total': 776, 'metric': 'tokens', 'processing_time': 23.642255067825317}

Total processing time: 23.648069858551025 seconds

 > Loading Model: llmware/bling-sheared-llama-2.7b-0.1...

 > Model llmware/bling-sheared-llama-2.7b-0.1 load time: 15.780867099761963 seconds
Query: What is the definition of qualified opportunity zone partnership interest?
LLM Response: • Any capital or profits interest in a domestic partnership if:
•Such interest is acquired by the qualified opportunity fund after December 31, 2017, from the partnership solely in exchange for cash
•As of the time such interest was acquired, such partnership was a qualified opportunity zone business (or, in the case of a new partnership, such partnership was being organized for purposes of being a qualified opportunity zone business), and
•During substantially all of the qualified opportunity fund's holding period for such interest, such partnership qualified as a qualified opportunity zone business.
LLM Usage: {'input': 681, 'output': 136, 'total': 817, 'metric': 'tokens', 'processing_time': 63.21877098083496}

Total processing time: 63.23019576072693 seconds

 > Loading Model: llmware/bling-red-pajamas-3b-0.1...

 > Model llmware/bling-red-pajamas-3b-0.1 load time: 17.25023102760315 seconds
Query: What is the definition of qualified opportunity zone partnership interest?
LLM Response: 1.   Qualified opportunity zone business property is tangible property used in a trade or business of the qualified opportunity fund if:
1.1.  The original use of the property started with the qualified opportunity fund;
1.2.  The property was substantially improved by the qualified opportunity fund; and
1.3.  The property is qualified opportunity zone stock.
LLM Usage: {'input': 611, 'output': 77, 'total': 688, 'metric': 'tokens', 'processing_time': 38.12269997596741}

Total processing time: 38.1325581073761 seconds

 > Loading Model: llmware/bling-stable-lm-3b-4e1t-v0...

 > Model llmware/bling-stable-lm-3b-4e1t-v0 load time: 23.456523895263672 seconds
Query: What is the definition of qualified opportunity zone partnership interest?
LLM Response: "Qualified opportunity zone partnership interest" means any capital or profits interest in a domestic partnership if-
LLM Usage: {'input': 611, 'output': 21, 'total': 632, 'metric': 'tokens', 'processing_time': 14.21246600151062}

Total processing time: 14.222395181655884 seconds

该过程只需几分钟。 现在我们有了模型的一些输出,让我们评估它们!

5.3 评估输出 vs. GPT 3.5

如果仔细阅读输出,你会发现并非每个模型都能产生良好的结果,这是可以预料的。 一些模型,尤其是小型模型,将与输入的长度作斗争,并产生不准确或无意义的输出。

其他模型对源文本的格式感到困惑,并产生相对合理但格式奇怪的输出。

然而,有几个模型,尤其是 Sheared LLaMa 模型,在总结上下文和回答问题方面表现得相当好。 例如,将其与 GPT3.5 的答案进行比较,您会发现答案相当令人满意。 对我来说,GPT3.5 响应(通过 API,而不是聊天)是:

A qualified opportunity zone partnership interest refers to an investment in a partnership that operates within a designated opportunity zone. An opportunity zone is a specific geographic area in the United States that has been identified as economically distressed and in need of investment. The partnership interest must meet certain criteria and be held for a specific period of time in order to qualify for certain tax benefits, such as deferral or reduction of capital gains taxes.

与我们从 Sheared LLaMa 得到的答案相比,这个答案的信息量要少得多,但 Sheared LLaMa 具有嵌入上下文的优势。 如果我们将找到的文本嵌入到 GPT3.5 查询中,我们将得到如下内容:

The definition of qualified opportunity zone partnership interest is any capital or profits interest in a domestic partnership that meets the following criteria:
(i) The interest is acquired by the qualified opportunity fund after December 31, 2017, from the partnership solely in exchange for cash.
(ii) At the time of acquisition, the partnership was a qualified opportunity zone business or was being organized for the purpose of being a qualified opportunity zone business.
(iii) During substantially all of the qualified opportunity fund's holding period for the interest, the partnership qualified as a qualified opportunity zone business.

正如您所看到的,这与 27 亿参数 Sheared LLaMa 给出的答案相当。 当然,这对于 Sheared LLaMa 来说是一个巨大的胜利,因为它的大小只有 GPT3.5 的 1/50,并且可以在笔记本电脑上运行!

至此,我们使用 LLMWare 和 BLING 构建了立法分析器!

6、结束语

在今天的示例中,我们使用 LLMWare 的开源框架和开源模型实现了 RAG 架构的特殊实现。 通过智能解析数据并应用一些预处理,我们仅使用笔记本电脑即可实现非常有竞争力的性能。

小型化代表了LLM技术商业化竞赛中的革命性巨变。 借助 LLMWare 的本地优先方法,公司能够构建大规模可扩展的专业 LLM 应用程序,这些应用程序可以解决现实世界的问题,而无需应对 GPU 可用性和计算成本的限制。

立法分析只是LLM可以解决的众多商业问题之一 - 立法搜索和科学书目搜索、财务分析或病历搜索之间有很多相似之处。


原文链接:GPT-Like LLM With No GPU? Yes! Legislation Analysis With LLMWare

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