链载Ai

标题: 企业级 RAG 应用程序的终极工具包 [打印本页]

作者: 链载Ai    时间: 昨天 11:55
标题: 企业级 RAG 应用程序的终极工具包

ingFang SC", "Hiragino Sans GB", "Microsoft YaHei UI", "Microsoft YaHei", Arial, sans-serif;font-size: 15px;margin: 1em 8px;letter-spacing: 0.1em;color: rgb(33, 37, 41);padding: 8px 12px;background: rgba(237, 242, 255, 0.8);border-radius: 8px;">llmware 是一个集成框架,具有 50+ 多个小型、专业、开源模型, 用于快速开发基于 LLM 的应用程序,包括 Retrieval Augmented Generation (RAG) 和代理工作流的多步骤编排。

ingFang SC", "Hiragino Sans GB", "Microsoft YaHei UI", "Microsoft YaHei", Arial, sans-serif;font-size: 20px;font-weight: bold;display: table;margin: 2em auto 1.5em;padding-top: 6px;padding-bottom: 6px;padding-left: 16.7448px;background-image: linear-gradient(135deg, rgb(113, 23, 234), rgba(113, 23, 234, 0.667), rgba(234, 96, 96, 0.533), rgba(217, 57, 205, 0.267), rgba(217, 57, 205, 0));background-position: initial;background-size: initial;background-repeat: initial;background-attachment: initial;background-origin: initial;background-clip: initial;color: rgb(255, 255, 255);border-radius: 8px;width: 318.247px;">适用谁

ingFang SC", "Hiragino Sans GB", "Microsoft YaHei UI", "Microsoft YaHei", Arial, sans-serif;font-size: 15px;margin: 1em 8px;letter-spacing: 0.1em;color: rgb(33, 37, 41);padding: 8px 12px;background: rgba(237, 242, 255, 0.8);border-radius: 8px;">该项目提供了一套全面的工具,任何人都可以使用,从初学者到最老练的 AI 开发人员,都可以快速构建工业级、基于知识的企业 LLM 应用程序。

ingFang SC", "Hiragino Sans GB", "Microsoft YaHei UI", "Microsoft YaHei", Arial, sans-serif;font-size: 20px;font-weight: bold;display: table;margin: 2em auto 1.5em;padding-top: 6px;padding-bottom: 6px;padding-left: 16.7448px;background-image: linear-gradient(135deg, rgb(113, 23, 234), rgba(113, 23, 234, 0.667), rgba(234, 96, 96, 0.533), rgba(217, 57, 205, 0.267), rgba(217, 57, 205, 0));background-position: initial;background-size: initial;background-repeat: initial;background-attachment: initial;background-origin: initial;background-clip: initial;color: rgb(255, 255, 255);border-radius: 8px;width: 318.247px;">概述

ingFang SC", "Hiragino Sans GB", "Microsoft YaHei UI", "Microsoft YaHei", Arial, sans-serif;font-size: 15px;margin: 1em 8px;letter-spacing: 0.1em;color: rgb(33, 37, 41);padding: 8px 12px;background: rgba(237, 242, 255, 0.8);border-radius: 8px;">llmware 提供一个统一的框架,用于构建基于 LLM 的应用程序(例如 RAG、代理), 使用小型专用模型,这些模型可以私密部署,安全可靠地与企业知识库集成,并且经济高效地调整和适应任何业务流程。

ingFang SC", "Hiragino Sans GB", "Microsoft YaHei UI", "Microsoft YaHei", Arial, sans-serif;font-size: 15px;margin: 1em 8px;letter-spacing: 0.1em;color: rgb(33, 37, 41);padding: 8px 12px;background: rgba(237, 242, 255, 0.8);border-radius: 8px;">llmware有两个主要组件:

ingFang SC", "Hiragino Sans GB", "Microsoft YaHei UI", "Microsoft YaHei", Arial, sans-serif;font-size: 15px;margin: 1em 8px;letter-spacing: 0.1em;color: rgb(33, 37, 41);padding: 8px 12px;background: rgba(237, 242, 255, 0.8);border-radius: 8px;">通过将这两个组件结合在一起,并集成领先的开源模型和底层技术,提供了一套全面的工具来快速构建基于知识的企业 LLM 应用程序。llmware

ingFang SC", "Hiragino Sans GB", "Microsoft YaHei UI", "Microsoft YaHei", Arial, sans-serif;font-size: 20px;font-weight: bold;display: table;margin: 2em auto 1.5em;padding-top: 6px;padding-bottom: 6px;padding-left: 16.7448px;background-image: linear-gradient(135deg, rgb(113, 23, 234), rgba(113, 23, 234, 0.667), rgba(234, 96, 96, 0.533), rgba(217, 57, 205, 0.267), rgba(217, 57, 205, 0));background-position: initial;background-size: initial;background-repeat: initial;background-attachment: initial;background-origin: initial;background-clip: initial;color: rgb(255, 255, 255);border-radius: 8px;width: 318.247px;">主要模块

ingFang SC", "Hiragino Sans GB", "Microsoft YaHei UI", "Microsoft YaHei", Arial, sans-serif;font-size: 15px;margin: 1em 8px;letter-spacing: 0.1em;color: rgb(33, 37, 41);padding: 8px 12px;background: rgba(237, 242, 255, 0.8);border-radius: 8px;">llmware 的主要组件包括:

通过这些组件可以实现我们 RAG 应用中的所有功能。下面简单介绍一下。

模型目录

通过轻松查找以相同的方式访问所有模型,而不管底层实现如何。

fromllmware.modelsimportModelCatalog
fromllmware.promptsimportPrompt

#allmodelsaccessedthroughtheModelCatalog
models=ModelCatalog().list_all_models()

#touseanymodelintheModelCatalog-"load_model"methodandpassthemodel_nameparameter
my_model=ModelCatalog().load_model("llmware/bling-phi-3-gguf")
output=my_model.inference("whatisthefutureofAI?",add_context="Hereisthearticletoread")

#tointegratemodelintoaPrompt
prompter=Prompt().load_model("llmware/bling-tiny-llama-v0")
response=prompter.prompt_main("whatisthefutureofAI?",context="InsertSourcesofinformation")

RAG 优化模型

1-7B 参数的 RAG 优化模型,专为 RAG 工作流集成而设计,可在本地流畅运行。

代理 Agents

llmware 旨在使用专为本地和私有部署而设计的小型语言模型实现基于 Agent 和 LLM 的函数调用, 并能够利用开源模型来执行复杂的 RAG 和基于知识的工作流程自动化。

llmware 中的关键元素:

嵌入模型

llmware 在默认 ModelCatalog 中支持 30+ 开箱即用的嵌入模型, 并且可以轻松扩展以从 HuggingFace 或 Sentence Transformers 添加其他流行的开源嵌入模型。

要获取当前支持的嵌入模型的列表,请执行以下操作:

fromllmware.modelsimportModelCatalog
embedding_models=ModelCatalog().list_embedding_models()
fori,modelsinenumerate(embedding_models):
print(f"embeddingmodels:{i}-{models}")

支持的热门模型包括:

llmware 中的嵌入模型可以直接由 安装,但在大多数情况下, 在创建新嵌入时,嵌入模型的名称将传递给 Library 类中的处理程序。 一旦完成,嵌入模型将作为该库的嵌入记录的一部分捕获到 LibraryCard 上的 Library 元数据中, 因此,通常不需要再次显式使用,例如,

fromllmware.libraryimportLibrary

library=Library().create_new_library("my_library")

#parsesthecontentfromthedocumentsinthefilepath,textchunksandindexesinatextcollectiondatabase
library.add_files(input_folder_path="/local/path/to/my_files",chunk_size=400,max_chunk_size=600,smart_chunking=1)

#createsembeddings-andkeepssynchronizedrecordsofwhichtextchunkshavebeenembeddedtoenableincrementaluse
library.install_new_embedding(embedding_model_name="jinaai/jina-embeddings-v2-small-en",
vector_db="milvus",
batch_size=100)

库 Library

Library 是 LLMWare 中非结构化信息的主要组织结构。 用户可以创建一个包含所有类型不同内容的大型库, 也可以创建多个库,每个库都包含有关特定主题、项目/案例/交易,甚至不同帐户/用户/部门的特定逻辑信息集合。

每个库都由以下组件组成:

  1. 1.数据库上的集合- 这是库的核心,通过解析文档创建,然后在文本集合数据库中自动分块和索引。这是检索的基础,也是将用作跟踪可附加到库集合的任意数量的向量嵌入的基础的集合。

  2. 2.文件存档- 位于llmware_data路径中,在 帐户 中,每个库都有一个文件夹结构。库的所有基于文件的工件都组织在这些文件夹中,包括库中添加的所有文件的副本(对于基于检索的应用程序非常有用)、从源文档中提取和索引的图像,以及派生的工件,例如 nlp 和知识图谱和数据集。

  3. 3.Library Catalog- 每个 Library 都在LibraryCatalog表中注册,并具有唯一的library_card,其中包含 Library 的关键属性和统计信息。

当 Library 对象传递给解析器时,解析器会自动将所有信息路由到 Library 结构中。

该库还提供了方便的方法,可以轻松地在库上安装嵌入,包括跟踪增量进度。

要解析为 Library,有非常有用的便捷方法add_files,它将调用 Parser, 整理和路由所选文件夹路径中的文件,检查重复文件,执行解析,文本分块和插入数据库,并自动更新所有 Library 状态。

库是执行 Query 时使用的主要索引结构。在构造 Query 对象时传递 library 对象, 然后将仅针对该 Library 中的内容执行所有检索(文本、语义和混合)。

检索和查询

检索和查询是混合了文本、语义、混合、元数据和自定义筛选器的查询库。

retrieval.py模块实现该类,这是执行搜索和检索的主要方式。 每个对象在构造时都需要在构造函数中将Library作为必需参数传递。Query对象将针对该 Library 进行操作,并可以访问 Library 的所有特定属性、元数据和方法。

llmware 中的检索利用库抽象作为执行特定查询或检索的主要单元。这提供了拥有多个不同知识库的能力, 这些知识库可能与不同的用例和/或用户、账户和权限保持一致。

#step1-loadapreviouslycreatedlibrary
lib=Library().load_library("my_library")

#step2-createaqueryobject
q=Query(lib)

#step3-runlotsofdifferentqueries(manyotheroptionsintheexamples)

#basictextquery
results1=q.text_query("textquery",result_count=20,exact_mode=False)

#semanticquery
results2=q.semantic_query("semanticquery",result_count=10)

数据存储

数据存储易于扩展的数据库选项 - 可以实现从笔记本电脑到并行集群的集成数据存储。

支持了市面上常见的向量库:

fromllmware.configsimportLLMWareConfig

#tosetthecollectiondatabase-mongo,sqlite,postgres
LLMWareConfig().set_active_db("mongo")

#tosetthevectordatabase(ordeclarewheninstalling)
#--options:milvus,pg_vector(postgres),redis,qdrant,faiss,pinecone,mongoatlas
LLMWareConfig().set_vector_db("milvus")

#forfaststart-noinstallationsrequired
LLMWareConfig().set_active_db("sqlite")
LLMWareConfig().set_vector_db("chromadb")#tryalsofaissandlancedb

#forsinglepostgresdeployment
LLMWareConfig().set_active_db("postgres")
LLMWareConfig().set_vector_db("postgres")

Prompt with Sources

Prompt with Sources:将知识检索与 LLM 推理相结合的最简单方法,并提供了几种高级有用的方法, 可以轻松地将检索/查询/解析步骤集成到 Prompt 中,以用作在模型上运行推理的来源。

fromllmware.promptsimportPrompt

#buildapromptandattachamodel
prompter=Prompt().load_model("llmware/bling-tiny-llama-v0")

#add_source_documentmethod:acceptsanysupporteddocumenttype,parsesthefile,andcreatestextchunks
#ifaqueryispassed,thenitwillrunaquickin-memoryfilteringsearchagainstthetextchunks
#thetextchunksarepackagedintosourceswithalloftheaccompanyingmetadatafromthefile,andmade
#availableautomaticallyinbatchestobeusedinprompting-

source=prompter.add_source_document("/folder/to/one/doc/","filename",query="fastquery")

#toruninferencewith'promptwithsources'->sourcewillbeautomaticallyaddedtotheprompt
responses=prompter.prompt_with_source("myquery")

#dependinguponthesizeofthesource(andbatchingrelativetothemodelcontextwindow,theremaybemorethan
#asingleinferencerun,sounpackpotentiallymultipleresponses

fori,responseinenumerate(responses):
print("response:",i,response)

总结

在本文中,我们简单介绍了 llmware 的基本概念和功能, 这些功能模块涉及到了我们编写 AI 应用程序的核心模块,后面我们一起看下怎么使用 llmware 来实现人工智能应用。







欢迎光临 链载Ai (https://www.lianzai.com/) Powered by Discuz! X3.5