ErnieRAG:用ErnieBot打造企业级RAG应用
项目背景
随着人工智能技术的深入发展,大语言模型(Large Language Model,LLM)已如狂潮般席卷了各行各业,其在自然语言处理、计算机视觉、推荐系统等诸多领域展现出了惊人的实力。然而,当面对特定行业或专业领域的深入应用时,通用大模型往往会遇到专业知识不足的挑战。针对这一问题,相对于成本高昂且效果不稳定的SFT或Lora微调方法,基于检索增强生成(Retrieval-Augmented Generation,RAG)的技术方案正成为越来越多企业的优选。
RAG架构是一种创新性的解决方案,它通过将大模型的生成能力与外部知识库的检索能力相结合,实现了在回答问题或执行任务时能够引入最新、最准确的专业知识。具体而言,当用户提出问题或需求时,RAG架构首先会在外部知识库中进行向量搜索,通过语义相似度匹配找到相关的专业知识,然后再将这些知识融入到大模型的生成过程中,从而得到更加专业、准确的回答或结果。
本文将基于文心大模型Erniebot和Milvus向量数据库,从零开始搭建一个通用的RAG企业级应用。
项目演示
RAG原理
如上图所示,我把一个RAG拆成了知识数据流
和用户数据流
。首先,先讲解知识数据流。
- 知识数据流:首先需要准备行业知识文档,可以是word、pdf、excel等容易被编程语言读取的格式文件,因为文档都是连续的长文档,为了匹配和计算方便,需要对长文本进行分块,分成若干个短文档,文本分块有自动分块(参考langchain的文本处理和截断),也有人工分块(工作量大,但因为人工对语义分块有优势,后期输出效果会更好),本项目采用人工分块的方式,提前将回答整理成了逐行的文档。分块后,将若干分块分别输入embedding模型中进行编码,将分块原文本存入关系型数据库,embedding编码向量存入向量数据库(本文为了方便演示,直接将原文本存入向量数据库内,但产业落地时建议将原文本存入关系型数据库,用id进行映射对应,获得更快的读取速度和方便知识管理)。
- 用户数据流:用户输入一个问题,对用户的问题进行embedding编码得到语义向量,将向量与向量数据库里存的数据向量进行计算比对,取相似度最大的值,通过id对应找到知识原文本,再将用户问题与知识原文本交给大语言模型进行理解、提取、修饰等二次加工,输出专业准确的回答。
环境安装
RAG应用环境主要包括两部分,一部分是大模型LLM,即文心Erniebot;另一部分是向量数据库,在这个案例中我们使用Milvus的轻量级版本Milvus Lite
,方便在Jupyter notebook中给大家运行展示。
In [1]
!pip install "milvus[client]" --user !pip install --upgrade erniebot --user Looking in indexes: https://mirror.baidu.com/pypi/simple/, https://mirrors.aliyun.com/pypi/simple/ Requirement already satisfied: milvus[client] in ./.local/lib/python3.10/site-packages (2.3.5) Requirement already satisfied: pymilvus<2.4.0,>=2.3.0b1 in ./.local/lib/python3.10/site-packages (from milvus[client]) (2.3.7) Requirement already satisfied: setuptools>=67 in /opt/conda/envs/python35-paddle120-env/lib/python3.10/site-packages (from pymilvus<2.4.0,>=2.3.0b1->milvus[client]) (68.0.0) Requirement already satisfied: grpcio<=1.60.0,>=1.49.1 in ./.local/lib/python3.10/site-packages (from pymilvus<2.4.0,>=2.3.0b1->milvus[client]) (1.60.0) Requirement already satisfied: protobuf>=3.20.0 in /opt/conda/envs/python35-paddle120-env/lib/python3.10/site-packages (from pymilvus<2.4.0,>=2.3.0b1->milvus[client]) (3.20.3) Requirement already satisfied: environs<=9.5.0 in ./.local/lib/python3.10/site-packages (from pymilvus<2.4.0,>=2.3.0b1->milvus[client]) (9.5.0) Requirement already satisfied: ujson>=2.0.0 in ./.local/lib/python3.10/site-packages (from pymilvus<2.4.0,>=2.3.0b1->milvus[client]) (5.9.0) Requirement already satisfied: pandas>=1.2.4 in /opt/conda/envs/python35-paddle120-env/lib/python3.10/site-packages (from pymilvus<2.4.0,>=2.3.0b1->milvus[client]) (2.1.4) Requirement already satisfied: requests in /opt/conda/envs/python35-paddle120-env/lib/python3.10/site-packages (from pymilvus<2.4.0,>=2.3.0b1->milvus[client]) (2.31.0) Requirement already satisfied: minio>=7.0.0 in ./.local/lib/python3.10/site-packages (from pymilvus<2.4.0,>=2.3.0b1->milvus[client]) (7.2.5) Requirement already satisfied: pyarrow>=12.0.0 in /opt/conda/envs/python35-paddle120-env/lib/python3.10/site-packages (from pymilvus<2.4.0,>=2.3.0b1->milvus[client]) (14.0.2) Requirement already satisfied: azure-storage-blob in ./.local/lib/python3.10/site-packages (from pymilvus<2.4.0,>=2.3.0b1->milvus[client]) (12.19.1) Requirement already satisfied: marshmallow>=3.0.0 in ./.local/lib/python3.10/site-packages (from environs<=9.5.0->pymilvus<2.4.0,>=2.3.0b1->milvus[client]) (3.21.1) Requirement already satisfied: python-dotenv in ./.local/lib/python3.10/site-packages (from environs<=9.5.0->pymilvus<2.4.0,>=2.3.0b1->milvus[client]) (1.0.1) Requirement already satisfied: certifi in /opt/conda/envs/python35-paddle120-env/lib/python3.10/site-packages (from minio>=7.0.0->pymilvus<2.4.0,>=2.3.0b1->milvus[client]) (2023.11.17) Requirement already satisfied: urllib3 in /opt/conda/envs/python35-paddle120-env/lib/python3.10/site-packages (from minio>=7.0.0->pymilvus<2.4.0,>=2.3.0b1->milvus[client]) (2.1.0) Requirement already satisfied: argon2-cffi in ./.local/lib/python3.10/site-packages (from minio>=7.0.0->pymilvus<2.4.0,>=2.3.0b1->milvus[client]) (23.1.0) Requirement already satisfied: pycryptodome in /opt/conda/envs/python35-paddle120-env/lib/python3.10/site-packages (from minio>=7.0.0->pymilvus<2.4.0,>=2.3.0b1->milvus[client]) (3.19.0) Requirement already satisfied: typing-extensions in /opt/conda/envs/python35-paddle120-env/lib/python3.10/site-packages (from minio>=7.0.0->pymilvus<2.4.0,>=2.3.0b1->milvus[client]) (4.9.0) Requirement already satisfied: numpy<2,>=1.22.4 in /opt/conda/envs/python35-paddle120-env/lib/python3.10/site-packages (from pandas>=1.2.4->pymilvus<2.4.0,>=2.3.0b1->milvus[client]) (1.26.2) Requirement already satisfied: python-dateutil>=2.8.2 in /opt/conda/envs/python35-paddle120-env/lib/python3.10/site-packages (from pandas>=1.2.4->pymilvus<2.4.0,>=2.3.0b1->milvus[client]) (2.8.2) Requirement already satisfied: pytz>=2020.1 in /opt/conda/envs/python35-paddle120-env/lib/python3.10/site-packages (from pandas>=1.2.4->pymilvus<2.4.0,>=2.3.0b1->milvus[client]) (2023.3.post1) Requirement already satisfied: tzdata>=2022.1 in /opt/conda/envs/python35-paddle120-env/lib/python3.10/site-packages (from pandas>=1.2.4->pymilvus<2.4.0,>=2.3.0b1->milvus[client]) (2023.3) Requirement already satisfied: azure-core<2.0.0,>=1.28.0 in ./.local/lib/python3.10/site-packages (from azure-storage-blob->pymilvus<2.4.0,>=2.3.0b1->milvus[client]) (1.30.1) Requirement already satisfied: cryptography>=2.1.4 in ./.local/lib/python3.10/site-packages (from azure-storage-blob->pymilvus<2.4.0,>=2.3.0b1->milvus[client]) (42.0.5) Requirement already satisfied: isodate>=0.6.1 in ./.local/lib/python3.10/site-packages (from azure-storage-blob->pymilvus<2.4.0,>=2.3.0b1->milvus[client]) (0.6.1) Requirement already satisfied: charset-normalizer<4,>=2 in /opt/conda/envs/python35-paddle120-env/lib/python3.10/site-packages (from requests->pymilvus<2.4.0,>=2.3.0b1->milvus[client]) (3.3.2) Requirement already satisfied: idna<4,>=2.5 in /opt/conda/envs/python35-paddle120-env/lib/python3.10/site-packages (from requests->pymilvus<2.4.0,>=2.3.0b1->milvus[client]) (3.6) Requirement already satisfied: six>=1.11.0 in /opt/conda/envs/python35-paddle120-env/lib/python3.10/site-packages (from azure-core<2.0.0,>=1.28.0->azure-storage-blob->pymilvus<2.4.0,>=2.3.0b1->milvus[client]) (1.16.0) Requirement already satisfied: cffi>=1.12 in ./.local/lib/python3.10/site-packages (from cryptography>=2.1.4->azure-storage-blob->pymilvus<2.4.0,>=2.3.0b1->milvus[client]) (1.16.0) Requirement already satisfied: packaging>=17.0 in /opt/conda/envs/python35-paddle120-env/lib/python3.10/site-packages (from marshmallow>=3.0.0->environs<=9.5.0->pymilvus<2.4.0,>=2.3.0b1->milvus[client]) (23.2) Requirement already satisfied: argon2-cffi-bindings in ./.local/lib/python3.10/site-packages (from argon2-cffi->minio>=7.0.0->pymilvus<2.4.0,>=2.3.0b1->milvus[client]) (21.2.0) Requirement already satisfied: pycparser in ./.local/lib/python3.10/site-packages (from cffi>=1.12->cryptography>=2.1.4->azure-storage-blob->pymilvus<2.4.0,>=2.3.0b1->milvus[client]) (2.21) Looking in indexes: https://mirror.baidu.com/pypi/simple/, https://mirrors.aliyun.com/pypi/simple/ Requirement already satisfied: erniebot in ./.local/lib/python3.10/site-packages (0.5.3) Requirement already satisfied: aiohttp in /opt/conda/envs/python35-paddle120-env/lib/python3.10/site-packages (from erniebot) (3.9.1) Requirement already satisfied: bce-python-sdk in /opt/conda/envs/python35-paddle120-env/lib/python3.10/site-packages (from erniebot) (0.8.98) Requirement already satisfied: colorlog in /opt/conda/envs/python35-paddle120-env/lib/python3.10/site-packages (from erniebot) (6.8.0) Requirement already satisfied: jsonschema>=4.19 in /opt/conda/envs/python35-paddle120-env/lib/python3.10/site-packages (from erniebot) (4.20.0) Requirement already satisfied: requests>=2.20 in /opt/conda/envs/python35-paddle120-env/lib/python3.10/site-packages (from erniebot) (2.31.0) Requirement already satisfied: tenacity in ./.local/lib/python3.10/site-packages (from erniebot) (8.2.3) Requirement already satisfied: typing-extensions>=4.8.0 in /opt/conda/envs/python35-paddle120-env/lib/python3.10/site-packages (from erniebot) (4.9.0) Requirement already satisfied: attrs>=22.2.0 in /opt/conda/envs/python35-paddle120-env/lib/python3.10/site-packages (from jsonschema>=4.19->erniebot) (23.1.0) Requirement already satisfied: jsonschema-specifications>=2023.03.6 in /opt/conda/envs/python35-paddle120-env/lib/python3.10/site-packages (from jsonschema>=4.19->erniebot) (2023.11.2) Requirement already satisfied: referencing>=0.28.4 in /opt/conda/envs/python35-paddle120-env/lib/python3.10/site-packages (from jsonschema>=4.19->erniebot) (0.32.0) Requirement already satisfied: rpds-py>=0.7.1 in /opt/conda/envs/python35-paddle120-env/lib/python3.10/site-packages (from jsonschema>=4.19->erniebot) (0.15.2) Requirement already satisfied: charset-normalizer<4,>=2 in /opt/conda/envs/python35-paddle120-env/lib/python3.10/site-packages (from requests>=2.20->erniebot) (3.3.2) Requirement already satisfied: idna<4,>=2.5 in /opt/conda/envs/python35-paddle120-env/lib/python3.10/site-packages (from requests>=2.20->erniebot) (3.6) Requirement already satisfied: urllib3<3,>=1.21.1 in /opt/conda/envs/python35-paddle120-env/lib/python3.10/site-packages (from requests>=2.20->erniebot) (2.1.0) Requirement already satisfied: certifi>=2017.4.17 in /opt/conda/envs/python35-paddle120-env/lib/python3.10/site-packages (from requests>=2.20->erniebot) (2023.11.17) Requirement already satisfied: multidict<7.0,>=4.5 in /opt/conda/envs/python35-paddle120-env/lib/python3.10/site-packages (from aiohttp->erniebot) (6.0.4) Requirement already satisfied: yarl<2.0,>=1.0 in /opt/conda/envs/python35-paddle120-env/lib/python3.10/site-packages (from aiohttp->erniebot) (1.9.4) Requirement already satisfied: frozenlist>=1.1.1 in /opt/conda/envs/python35-paddle120-env/lib/python3.10/site-packages (from aiohttp->erniebot) (1.4.1) Requirement already satisfied: aiosignal>=1.1.2 in /opt/conda/envs/python35-paddle120-env/lib/python3.10/site-packages (from aiohttp->erniebot) (1.3.1) Requirement already satisfied: async-timeout<5.0,>=4.0 in /opt/conda/envs/python35-paddle120-env/lib/python3.10/site-packages (from aiohttp->erniebot) (4.0.3) Requirement already satisfied: pycryptodome>=3.8.0 in /opt/conda/envs/python35-paddle120-env/lib/python3.10/site-packages (from bce-python-sdk->erniebot) (3.19.0) Requirement already satisfied: future>=0.6.0 in /opt/conda/envs/python35-paddle120-env/lib/python3.10/site-packages (from bce-python-sdk->erniebot) (0.18.3) Requirement already satisfied: six>=1.4.0 in /opt/conda/envs/python35-paddle120-env/lib/python3.10/site-packages (from bce-python-sdk->erniebot) (1.16.0)
Erniebot配置
请注意,这一步需要把erniebot.access_token
替换成自己的Token。
Token获取链接 -> 注册用户免费领取百万Tokens
In [2]
# 如遇到No module named 'erniebot'报错,请先重启内核。 import erniebot erniebot.api_type = "aistudio" erniebot.access_token = "85ce2be89300caf45768cd0f00b65bde56d9713a"
Embedding模型
Embedding是将自然语言转换为计算机能理解和处理的编码过程,这里采用在线的文心百中语义模型 - ernie-text-embedding
作为Embedding模型对文本进行编码。这时可能就有很多人会问,为什么不用word2vec
之类的小模型来处理?这里我给大家从以下几点解答:
word2vec
大部分的模型都是基于英文语料训练的,对中文支持不够。word2vec
的训练语料和模型参数规模都很小,语言理解能力不够。word2vec
极少能支持同义理解、近似表达等,如世界最高的山脉
和珠穆朗玛峰
,word2vec
的计算结果就认为这是两个名词。word2vec
不支持问答匹配,如世界第二高的山脉是哪一个?
和乔戈里峰
,word2vec
计算出来的相似度极低。
与之相对,这里选择文心百中
语义模型具有以下特点:
- 文心百中源自文心千亿参数模型的语义理解能力,单模型刷新60+中文NLP任务基准,并曾登顶SuperGLUE全球榜首。
- 大模型具备强大的小样本能力、优化门槛低,行业迁移所需的标注数据量少,部分场景无需优化即可上线。
In [3]
def embedding(text): response = erniebot.Embedding.create( model="ernie-text-embedding", input=[text]) return response.get_result() # 测试效果 embedding("你好,世界") [[-0.01785610243678093, -0.042828336358070374, -0.01752176135778427, 0.09543166309595108, -0.01862281933426857, -0.05556907504796982, 0.010166742838919163, -0.037863291800022125, 0.03760803863406181, 0.008617705665528774, 0.04345136508345604, -0.1222190335392952, 0.07381621748209, -0.01850126124918461, -0.07926321029663086, -0.02984457276761532, 0.010519803501665592, -0.0029366740491241217, 0.03355805203318596, 0.025333112105727196, -0.0005689557874575257, -0.0434546060860157, -0.04686760902404785, -0.015284668654203415, 0.04244505986571312, 0.027027184143662453, 0.07517201453447342, 0.046653274446725845, 0.015043158084154129, -0.005754225421696901, -0.006629697512835264, 0.024701019749045372, 0.028177708387374878, -0.05654280632734299, -0.013399244286119938, 0.046939898282289505, 0.048646196722984314, 0.01825331710278988, -0.08939190208911896, 0.03551991283893585, -0.011281047016382217, 0.04672731086611748, 0.006005595903843641, 0.08318183571100235, 0.023625217378139496, -0.06570187211036682, -0.04111391305923462, -0.08419842272996902, -0.07816535979509354, -0.043935153633356094, -0.00035373744321987033, 0.004239416681230068, -0.007446739356964827, 0.07145892083644867, -0.013144105672836304, -0.041798997670412064, -0.11306712031364441, -0.020347028970718384, 0.0704353079199791, -0.06216113269329071, 0.02456301636993885, 0.05432466045022011, 0.08695327490568161, -0.05152402073144913, 0.051052071154117584, 0.008570078760385513, -0.005971777252852917, -0.0022575012408196926, 0.07115739583969116, -0.002533943159505725, -0.011866597458720207, -0.03920729458332062, 0.0689861997961998, 0.08254721015691757, -0.04664626717567444, 0.042452622205019, -0.04421757161617279, 0.004480578470975161, 0.0011216930579394102, -0.024794364348053932, 0.05494285374879837, -0.08317882567644119, 0.09075280278921127, -0.0851575955748558, -0.07809795439243317, -0.007565879262983799, 0.011666921898722649, 0.04131567105650902, -0.010791061446070671, -0.005981830880045891, -0.0019869066309183836, -0.08432333171367645, 0.022921711206436157, -0.0805506482720375, -0.07643667608499527, -0.037204790860414505, 0.014994845725595951, -0.005704901646822691, -0.018062127754092216, -0.05812552198767662, 0.06531049311161041, 0.06062709167599678, 0.06087446212768555, 0.005163520108908415, 0.07488510757684708, -0.04966919124126434, -0.0846000611782074, -0.1027790904045105, 0.015971707180142403, 0.041128359735012054, -0.02075926959514618, 0.056347377598285675, 0.01636473834514618, -0.018552377820014954, -0.04921926185488701, 0.08147001266479492, 0.07541684061288834, -0.021367667242884636, 0.02736336551606655, -0.055668748915195465, -0.060512643307447433, 0.077652707695961, -0.028188958764076233, 0.09743193536996841, -0.0528443418443203, 0.04866989701986313, -0.10998515039682388, -0.043504707515239716, -0.04149447754025459, 0.05495055019855499, 0.004249947145581245, 0.014552794396877289, 0.07019367069005966, -0.0020548568572849035, -0.037553656846284866, -0.009290461428463459, 0.025151921436190605, -0.02855624631047249, -0.03279455006122589, 0.07650482654571533, -0.10623850673437119, -0.0260025542229414, -0.03130897879600525, 0.0554753839969635, 0.0521874725818634, 0.09777776896953583, 0.03568036109209061, -0.0003055407723877579, -0.032107964158058167, 0.027797551825642586, -0.044380079954862595, 0.00916537269949913, -0.015910256654024124, 0.009658414870500565, -0.06990692019462585, 0.00913875550031662, 0.003912254702299833, -0.05677613615989685, -0.010873374529182911, 0.06990883499383926, -0.004991276189684868, 0.05390562862157822, 0.122899129986763, -0.08609943091869354, 0.06711353361606598, -0.012983414344489574, -0.05796580761671066, 0.0348932147026062, -0.015217752195894718, 0.02781454101204872, -0.048797089606523514, 0.08602281659841537, -0.019149301573634148, -0.030097784474492073, -0.016177352517843246, 0.018601031973958015, 0.043769195675849915, 0.009057965129613876, -0.06631405651569366, -0.00245236256159842, -0.02977677993476391, -0.009740660898387432, 0.07287545502185822, 0.02424764074385166, 0.0496152825653553, -0.12354772537946701, -0.08172590285539627, 0.03898832947015762, -0.01105730701237917, -0.09063262492418289, 0.018524253740906715, 0.04945143312215805, -0.04995923489332199, 0.05600837990641594, 0.00888981856405735, -0.03751247748732567, -0.014067706651985645, -0.05430635064840317, -0.02911038137972355, 0.05775154381990433, -0.010475082322955132, -0.006295882165431976, 0.03287462890148163, -0.07751692831516266, -0.05661683902144432, -0.03687905892729759, -0.01023193821310997, 0.05870586633682251, -0.026653945446014404, -0.008421825245022774, -0.03681901469826698, 0.02946249581873417, 0.07128909975290298, -0.10110173374414444, 0.012930287979543209, 0.06238439306616783, -0.002304614521563053, -0.026508193463087082, 0.11493299901485443, 0.0335574597120285, -0.007101740222424269, -0.02188318781554699, 0.10004038363695145, -0.041006337851285934, 0.10643132030963898, 0.07339324802160263, 0.02000460959970951, 0.052644625306129456, 0.02533302642405033, 0.04929334297776222, 0.022557204589247704, 0.017720738425850868, 0.05399899557232857, 0.07935840636491776, 0.05509363114833832, -0.010119030252099037, 0.010045656003057957, -0.07895113527774811, 0.030403118580579758, 0.033564019948244095, -0.0717368945479393, -0.10425501316785812, 0.01599375158548355, -0.09382200241088867, 0.01762322708964348, -0.03417035937309265, 0.023074183613061905, 0.11572007834911346, -0.05602532997727394, 0.061245378106832504, -0.07590549439191818, -0.013885479420423508, -0.08077850192785263, -0.016714224591851234, 0.026227956637740135, -0.062054648995399475, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.37939706444740295, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, -0.023315366357564926, -0.39577001333236694, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0]]
文本生成模型
文本生成这里我们选择了ernie-4.0
作为演示,企业可根据自己的需求、效果要求以及预算,采用其他例如ernie-3.5
,ernie-turbo
或者ernie-longtext
模型。
In [4]
def chat(text, temperature=0.9, top_p=0.9): response = erniebot.ChatCompletion.create( model="ernie-turbo", messages=[{"role": "user", "content": text}], temperature=temperature, top_p=top_p) return response.get_result()
知识库数据准备
在本案例中使用了某保险公司的一个问答数据集,为了测试方便,我们这里仅取了10条进行批量插入和问答演示。
In [5]
import pandas as pd data_df = pd.read_csv("baoxianzhidao_filtertest.csv", encoding="gbk") reply = data_df.iloc[:, 0] for rly in reply: print(rly) 您好,这个是重疾险中给予投保者的一项权利,安*长青树保障责任规定,投保者可以享受多次赔付,豁免等权益。也就是说不同轻症累计5次赔付,理赔1次轻症豁免后期所交保费,人性化的设计,无需加保费。 您好,HUTS保险中的乐游全球(探亲版)-慧择旅游保险澳新计划是澳*新西兰探亲专属保障,承保年龄可达90周岁,含有50万高额医疗保障,完全满足境外医疗保障需求,需要注意的是这款产品仅承保出行目的为境外探亲的人群,理赔时需提供相关签证或亲属关系证明等 您好,水上运动比赛,尤其是带有奖金的比赛一般承保的公司比较少。不过,HUTS保险中的众行天下-水上运动保险赛事版B就是适合帆船等水上比赛的产品,含户外溺水保障,是水上运动专属定制的保障,意外住院有津贴,保障期限灵活可选,还可以投保有奖金的赛事,您可以根据情况看看。 您好,端午出行的人比较多,而且自驾存在一定风险,所以有保险意识还是很好的。考虑到价格以及保障内容等相关因素,您可以看看HUTS保险中的畅玩神州-慧择旅游保险计划三,适合驾驶私家车走南闯北国内旅游,自驾意外累计赔付,承保的范围也较为广泛,适合带家人出游,保障全面,三天仅需75元,性价比还是蛮高的。 不到10块钱……………… 您好,赛龙舟是一项比较传统的活动,很有意义。不过由于是在水上活动,建议安全保障还要做足,HUTS保险中有针对水上运动风险特别定制的众行天下-水上运动保险,可以针对这种赛事进行保障,含有意外住院有津贴以及一系列保障,性价比较高,关键是费用也比较实惠。 您好,这点是不用担心的。投保后保险公司会在约定的保险费交纳日从消费者购买时填写的银行账号中划扣当期应交的安*长青树重疾险的保险费,所以您老婆是不用亲自去保险公司缴费的。 您好,是可以的。该医疗垫付目的便是解决投保者的切实困难,可以说是很人性化的一项保障了。在以下几个情况下可以申请医疗垫付:①发生重大意外或突发急性病;②所投保保险涵盖该医疗费用补偿责任;③医疗费用高客户无法承担 您好,去北极探险本身就存在一定的风险,建议选择专业的装备以及在专业人士的陪同下进行。至于保险,市面上关于此类的保险并不多,不过HUTS保险中却有一款专门针对南北极旅游的定制产品,保障内容充足,户外伤害、医疗保障甚至的紧急救援都具备,详情可以多了解下。
启动向量数据库
请注意只能启动一次,重复启动会报错,如需重启数据库,请先重启环境。
In [6]
from milvus import default_server from pymilvus import connections try: default_server.start() except: default_server.cleanup() default_server.start() connections.connect(host='127.0.0.1', port=default_server.listen_port)
---------------------------------------------------------------------------TimeoutError Traceback (most recent call last)Cell In[6], line 5 4 try: ----> 5 default_server.start() 6 except: File ~/.local/lib/python3.10/site-packages/milvus/init.py:432, in MilvusServer.start(self) 431 if self.wait_for_started: --> 432 self.wait_started() 433 if not self._debug: File ~/.local/lib/python3.10/site-packages/milvus/init.py:395, in MilvusServer.wait_started(self, timeout) 394 if self.running: --> 395 raise TimeoutError(f’Milvus not startd in {timeout/1000} seconds’) 396 else: TimeoutError: Milvus not startd in 180.0 seconds During handling of the above exception, another exception occurred: RuntimeError Traceback (most recent call last)Cell In[6], line 7 5 default_server.start() 6 except: ----> 7 default_server.cleanup() 8 default_server.start() 9 connections.connect(host=‘127.0.0.1’, port=default_server.listen_port) File ~/.local/lib/python3.10/site-packages/milvus/init.py:364, in MilvusServer.cleanup(self) 362 def cleanup(self): 363 if self.running: --> 364 raise RuntimeError(‘Server is running’) 365 shutil.rmtree(self.config.base_data_dir, ignore_errors=True) RuntimeError: Server is running
创建数据表
创建一个数据集合(可以理解为Mysql里面的数据表),里面包含三个字段,分别是answer_id
(自增数据ID)、answer
(回答文本)以及最重要的answer_vector
(回答文本的对应向量),使用文心百中语义模型,其输出的数据维度为384,如果是其他模型,请遵循其他模型对应的输出维度。然后把这个集合记为"qadb"。
In [ ]
from pymilvus import utility, Collection from pymilvus import CollectionSchema, FieldSchema, DataType answer_id = FieldSchema( name="answer_id", dtype=DataType.INT64, is_primary=True, auto_id=True ) answer = FieldSchema( name="answer", dtype=DataType.VARCHAR, max_length=1024, ) answer_vector = FieldSchema( name="answer_vector", dtype=DataType.FLOAT_VECTOR, dim=384 ) schema = CollectionSchema( fields=[answer_id, answer, answer_vector], description="vector data" ) collection_name = "qadb" Collection( name=collection_name, schema=schema, using='default', shards_num=2 ) collection = Collection("qadb")
创建索引和加载数据库
创建集合后,还需要对该集合的answer_vector向量列添加索引,以进行后续的向量运算。这里使用欧氏距离和IVF_FLATIVF_FLAT
算法对向量建立索引。
In [ ]
index_params = { "metric_type":"L2", "index_type":"IVF_FLAT", "params":{"nlist":1024} } collection.create_index( field_name="answer_vector", index_params=index_params ) utility.index_building_progress("qadb") collection.load()
插入数据
数据库、数据集合和索引都建立之后,我们就可以往里面插入数据。
In [ ]
import time for rly in reply: rlyEmbedding = embedding(rly) data = [ [rly], rlyEmbedding ] collection.insert(data) time.sleep(0.5) # 加延时,防止embedding接口超QPS
检索数据
数据插入完成后,我们尝试从数据库里面检索数据,观察问题和检索结果是否有一定的关联度。
In [ ]
question = "假期出行买哪种保险好?" qEmbedding = embedding(question) search_params = { "metric_type": "L2", "offset": 0, "ignore_growing": False, "params": {"nprobe": 10} } results = collection.search( data=qEmbedding, anns_field="answer_vector", param=search_params, limit=1, expr=None, output_fields=['answer'], consistency_level="Strong" ) answer = results[0][0].entity.get('answer') answer
从上面的回答中,根据用户“假期出行”的信息,向量计算后从数据库匹配到了一条跟假期出行保险相关的回答,但回答是端午出行的,而用户的提问是假期出行,虽匹配但明显不合语义,那我们应该怎么将数据库的答案变成更贴合用户问题的回复呢,这时候我们就用文心4.0对用户问题进行分析和对答案进行二次加工处理输出。
结合Erniebot问答
将检索结果输入Erniebot处理,让文心大模型对文本进行提取、修饰和补充,得到更加专业、准确的回答或结果。
In [ ]
erniebotInput = "使用以下文段来回答最后的问题。仅根据给定的文段生成答案。如果你在给定的文段中没有找到任何与问题相关的信息,就说你不知道,不要试图编造答案。保持你的答案富有表现力。用户最后的问题是:" + question + "。给定的文段是:" + answer chatResult = chat(erniebotInput) chatResult
从上面的回复可以看出,经过文心4.0处理后,能够把原数据库答案里的产品信息进行提取,并结合用户的问题进行了二次加工生成了合适贴切的回答。
Gradio应用部署
本文开头所示的Gradio应用已经打包在本项目根目录下的app.gradio.py
文件内,大家可按照aistudio应用部署的方法进行在线部署,也可下载文件到本地进行本地运行。
具体步骤如下:
- 编辑器右上角找到
部署
按钮
- 选择
Gradio部署
- 填写应用信息,执行文件选择
app.gradio.py
,部署环境选择CPU
即可,最后点击部署,接下来耐心等待部署完成。
总结
本文介绍了如何使用ErnieBot和Milvus向量数据库打造企业级RAG(检索增强生成)应用。RAG架构通过将大模型的生成能力与外部知识库的检索能力相结合,实现了在回答问题或执行任务时能够引入最新、最准确的专业知识。文章详细阐述了RAG的原理和环境安装,包括ErnieBot的配置和Embedding模型的选择。同时,还介绍了文本生成模型的选择、知识库数据的准备、向量数据库的启动、数据表的创建、索引和加载数据库、插入数据以及检索数据等步骤。最终,通过检索数据,验证RAG应用的有效性。该方案为企业提供了一种大语言模型落地企业应用的解决方案,能够帮助大语言模型更好地应对特定行业或专业领域的深入应用挑战。