文档

使用Hologres+PAI+LLaMA2搭建企业专属问答知识库

更新时间:

本文为您介绍如何使用实时数仓Hologres、人工智能平台 PAI以及大模型LLaMA2部署企业专属问答机器人。

背景信息

企业专属问答机器人部署需要如下组件或服务:

  • LangChain:是一个开源框架,可以将大模型、向量数据库、定制语料结合,高效完成专属问答知识库的搭建。Hologres现已被LangChain作为向量数据库集成,详情请参见LangChain-Hologres。通过开源框架LangChain完成企业专属问答知识库的搭建。

  • PAI-EAS:是阿里云的模型在线服务平台,支持用户将模型一键部署为在线推理服务或AI-Web应用,可以一键部署LLM推理、AIGC等热门服务应用。PAI-EAS 适用于实时推理、近实时异步推理等多种AI推理场景,具备Serverless自动扩缩容和完整运维监控体系能力。

  • Hologres:Hologres是一站式实时数仓引擎,与达摩院自研高性能向量计算软件库Proxima深度整合,支持高效、易用的向量计算能力,详情请参见Proxima向量计算。通过Hologres向量计算能力进行专属语料数据的检索并对大模型进行微调。

  • LLaMA-2:是新一代开源大模型,有7B、13B、70B多种参数版本,现已支持基于PAI进行部署。

Hologres自研工具帮助您进行专属问答知识库的部署,仅通过该工具即可将Hologres作为向量实时存储和检索引擎,将Hologres、PAI、大模型(LLaMA-2)、定制语料、LangChain串联,快速完成企业专属问答机器人的搭建,自研工具详情请参见GitHubimage.png

前提条件

  • 已开通Hologres实例:您可以使用Hologres免费试用实例,0元搭建向量数据库,详情请参见新用户免费试用

    说明

    建议使用Hologres 8CU规格,免费试用实例处理的向量数据规模为百万级。如有更大规模向量数据,请使用更大规格实例。

  • 已开通PAI并创建工作空间:详情请参见开通PAI并创建默认工作空间

  • 已准备好基础环境:准备一台ECS机器(您也可以在本地进行),进行如下环境准备工作:

    • 安装Anaconda,详情请参见Anaconda

    • 安装3.8版本及以上版本的Python:

      conda create --name chatbot python=3.8
      conda activate chatbot
    • 安装搭建问答知识库相关依赖:

      pip install langchain modelscope psycopg2-binary sentence_transformers bottle requests
    • 克隆本文所需代码与样例数据:本文的工具位于其中的holo-llm目录下。

      git clone https://github.com/aliyun/alibabacloud-hologres-connectors.git

实践步骤

  1. 部署LLaMA2大模型。

    使用PAI-EAS模型在线服务即可快速部署LLaMA-2,详情请参见Llama2-WebUI基于EAS的一键部署

    说明

    您也可以按照本文步骤,使用其他大模型进行专属问答知识库的搭建。其他大模型部署方式请参见LLM

    完成大模型部署后,您需要在EAS控制台的服务详情页中查看并记录实例的调用信息(调用地址和Token),详情请参见调用方式概览

  2. 配置config文件。

    使用本文工具搭建问答知识库,需要在config文件中设置各个资源的连接信息、embedding模型信息等配置项。运行如下命令进行配置:

    cd alibabacloud-hologres-connectors/holo-llm
    vim config/config.json

    配置项如下,其中embeddingquery_topkprompt_template均会对大模型微调效果产生影响,请谨慎修改配置值。

    配置项

    说明

    eas_config

    大模型服务的调用信息(步骤1记录的调用信息),包括:

    • url:LLaMA2大模型的调用地址。

    • token:LLaMA2大模型调用地址对应的Token。

    holo_config

    Hologres的连接信息,包括:

    • HOLO_ENDPOINT:Hologres实例的网络地址。

      进入Hologres管理控制台实例详情页获取网络地址。

    • HOLO_PORT:Hologres实例的端口。

      进入Hologres管理控制台实例详情页获取端口。

    • HOLO_DATABASE:Hologres实例的数据库名称。

    • HOLO_USER:当前阿里云账号的AccessKey ID。

      您可以单击AccessKey 管理,获取AccessKey ID。

    • HOLO_PASSWORD:当前阿里云账号的AccessKey Secret。

      您可以单击AccessKey 管理,获取AccessKey Secret。

    embedding

    将语料数据进行向量化处理的embedding模型信息,包括:

    • model_id:embedding模型路径。

      本文使用达摩院在魔搭平台的CoRom开源中文embedding模型,路径为:damo/nlp_corom_sentence-embedding_english-base

    • model_dimension:模型的向量维度。

      本文使用的embedding模型对应生成768维的向量数据,即model_dimension值设置为768,详情请参见coROM

    query_topk

    Hologres向量检索返回的数据条数。

    本文定义为4,您可以结合大模型允许的字符数上限、大模型微调效果等方面进行设置。

    prompt_template

    用于大模型微调的提词模板。

    config文件中已有默认模板,您无需调整。

  3. 处理专属语料数据。

    针对提前准备的语料数据,需要进行向量化处理并导入Hologres。使用本文的开源工具,通过如下命令进行。

    # 语料向量数据导入前,先清理数据库中的历史数据
    python main.py -l --clear
    
    # 将holo-llm/data/example.csv中的语料数据向量化处理后导入Hologres
    python main.py -l
    重要

    首次使用会自动下载Embedding模型(约400 MB),需要较长时间,第二次调用则不需要此过程。

    本文提供了若干针对Hologres的专业语料,示例如下:

    title

    content

    What is Hologres

    Hologres is a one-stop real-time data Warehouse independently developed by Alibaba. It supports real-time writing, real-time updating, real-time processing and real-time analysis of massive data...

    What is proxima

    Proxima is a high-performance software library for vector nearest neighbor search from the Ali Dharma Institute. Compared with similar open-source products such as Faiss, the Proxima is better in stability and performance...

    What is the principle of JSONB column storage in Hologres

    Hologres supports columnar storage optimization for JSONB type starting from V1.3, it can reduce the storage size of JSONB data and accelerate query. This article will introduce you to the use of columnar JSONB in the Hologres...

    您可以在Hologres实例对应数据库下的langchain_embedding表中查询embedding后的向量数据:

    SELECT * FROM langchain_embedding limit 1;
    
    --返回结果示例
    id|embedding|metadata|document
    2419815e-1407-11ee-bee5-acde48001122|{0.395261,0.123794,0.761932,0.413286,...}|{"source": "data/example_data.csv", "row": 1}|title: ... content: ...
  4. 验证大模型微调效果。

    • 调用原生LLaMA-2大模型进行问答:

      python main.py -n
      
      # 具体对话
      Please enter a Question: 
      What is hologres?
      PAI-LLM answer:
      Hologres is a term used in the context of digital holography, which refers to the holographic image produced by a digital holographic camera. The term "hologres" is derived from the Greek words "holos" meaning "whole" and "graphein" meaning "to record". It refers to the complete or entire holographic image that is recorded by the camera, rather than just a portion of it. Hologres can be used to create three-dimensional images that appear life-like and can be viewed from different angles, providing a more immersive and realistic viewing experience.Please let me know if you need anything else
    • 调用经过Hologres提示词微调的LLaMA-2大模型进行问答:

      python main.py
      
      # 具体对话
      Please enter a Question: 
      What is hologres?
      PAI-LLM + Hologres answer:
      Hologres is a one-stop real-time data warehouse independently developed by Alibaba, which supports real-time writing, real-time updating, real-time processing, and real-time analysis of massive data

    即已完成使用Hologres+PAI+LLaMA2部署专属问答知识库的基本操作。

    您可以继续将问答知识库接入实际业务场景进行使用,如接入钉钉聊天群聊(参见使用Hologres和大模型免费定制专属聊天机器人)等。

  • 本页导读 (1)
文档反馈