什么情况下网站需要备案,三都网站建设,WordPress自动修改标签别名,wordpress 插件角色基础作业#xff1a;
复现课程知识库助手搭建过程 (截图)
进阶作业#xff1a;
选择一个垂直领域#xff0c;收集该领域的专业资料构建专业知识库#xff0c;并搭建专业问答助手#xff0c;并在 OpenXLab 上成功部署#xff08;截图#xff0c;并提供应用地址#x…
基础作业
复现课程知识库助手搭建过程 (截图)
进阶作业
选择一个垂直领域收集该领域的专业资料构建专业知识库并搭建专业问答助手并在 OpenXLab 上成功部署截图并提供应用地址 基础作业
1 LangChain 相关环境配置
在已完成 InternLM 的部署基础上还需要安装以下依赖包
pip install langchain0.0.292
pip install gradio4.4.0
pip install chromadb0.4.15
pip install sentence-transformers2.2.2
pip install unstructured0.10.30
pip install markdown3.3.7
同时我们需要使用到开源词向量模型 Sentence Transformer:我们也可以选用别的开源词向量模型来进行 Embedding目前选用这个模型是相对轻量、支持中文且效果较好的同学们可以自由尝试别的开源词向量模型
首先需要使用 huggingface 官方提供的 huggingface-cli 命令行工具。安装依赖:
pip install -U huggingface_hub然后在和 /root/data 目录下新建python文件 download_hf.py填入以下代码
vim /root/data/download_hf.py
import os# 下载模型
os.system(huggingface-cli download --resume-download sentence-transformers/paraphrase-multilingual-MiniLM-L12-v2 --local-dir /root/data/model/sentence-transformer)
但是使用 huggingface 下载可能速度较慢我们可以使用 huggingface 镜像下载。与使用hugginge face下载相同只需要填入镜像地址即可。
将 download_hf.py 中的代码修改为以下代码
import os# 设置环境变量
os.environ[HF_ENDPOINT] https://hf-mirror.com# 下载模型
os.system(huggingface-cli download --resume-download sentence-transformers/paraphrase-multilingual-MiniLM-L12-v2 --local-dir /root/data/model/sentence-transformer)然后在 /root/data 目录下执行该脚本即可自动开始下载
python /root/data/download_hf.py 1.4 下载 NLTK 相关资源
我们在使用开源词向量模型构建开源词向量的时候需要用到第三方库 nltk 的一些资源。正常情况下其会自动从互联网上下载但可能由于网络原因会导致下载中断此处我们可以从国内仓库镜像地址下载相关资源保存到服务器上。
我们用以下命令下载 nltk 资源并解压到服务器上
cd /root
git clone https://gitee.com/yzy0612/nltk_data.git --branch gh-pages
cd nltk_data
mv packages/* ./
cd tokenizers
unzip punkt.zip
cd ../taggers
unzip averaged_perceptron_tagger.zip
之后使用时服务器即会自动使用已有资源无需再次下载。 1.5 下载本项目代码
我们在仓库中同步提供了所有脚本可以查看该教程文件的同级目录的 demo 文件夹。
建议通过以下目录将仓库 clone 到本地可以直接在本地运行相关代码
cd /root/data
git clone https://github.com/InternLM/tutorial 通过上述命令可以将本仓库 clone 到本地 root/data/tutorial 目录下在之后的过程中可以对照仓库中的脚本来完成自己的代码也可以直接使用仓库中的脚本。
2.1 数据收集
我们选择由上海人工智能实验室开源的一系列大模型工具开源仓库作为语料库来源包括
OpenCompass面向大模型评测的一站式平台IMDeploy涵盖了 LLM 任务的全套轻量化、部署和服务解决方案的高效推理工具箱XTuner轻量级微调大语言模型的工具库InternLM-XComposer浦语·灵笔基于书生·浦语大语言模型研发的视觉-语言大模型Lagent一个轻量级、开源的基于大语言模型的智能体agent框架InternLM一个开源的轻量级训练框架旨在支持大模型训练而无需大量的依赖
首先我们需要将上述远程开源仓库 Clone 到本地可以使用以下命令
# 进入到数据库盘
cd /root/data
# clone 上述开源仓库
git clone https://gitee.com/open-compass/opencompass.git
git clone https://gitee.com/InternLM/lmdeploy.git
git clone https://gitee.com/InternLM/xtuner.git
git clone https://gitee.com/InternLM/InternLM-XComposer.git
git clone https://gitee.com/InternLM/lagent.git
git clone https://gitee.com/InternLM/InternLM.git 接着为语料处理方便我们将选用上述仓库中所有的 markdown、txt 文件作为示例语料库。注意也可以选用其中的代码文件加入到知识库中但需要针对代码文件格式进行额外处理因为代码文件对逻辑联系要求较高且规范性较强在分割时最好基于代码模块进行分割再加入向量数据库。
我们首先将上述仓库中所有满足条件的文件路径找出来我们定义一个函数该函数将递归指定文件夹路径返回其中所有满足条件即后缀名为 .md 或者 .txt 的文件的文件路径
接着为语料处理方便我们将选用上述仓库中所有的 markdown、txt 文件作为示例语料库。注意也可以选用其中的代码文件加入到知识库中但需要针对代码文件格式进行额外处理因为代码文件对逻辑联系要求较高且规范性较强在分割时最好基于代码模块进行分割再加入向量数据库。
我们首先将上述仓库中所有满足条件的文件路径找出来我们定义一个函数该函数将递归指定文件夹路径返回其中所有满足条件即后缀名为 .md 或者 .txt 的文件的文件路径
import os
def get_files(dir_path):# argsdir_path目标文件夹路径file_list []for filepath, dirnames, filenames in os.walk(dir_path):# os.walk 函数将递归遍历指定文件夹for filename in filenames:# 通过后缀名判断文件类型是否满足要求if filename.endswith(.md):# 如果满足要求将其绝对路径加入到结果列表file_list.append(os.path.join(filepath, filename))elif filename.endswith(.txt):file_list.append(os.path.join(filepath, filename))return file_list
2.2 加载数据
得到所有目标文件路径之后我们可以使用 LangChain 提供的 FileLoader 对象来加载目标文件得到由目标文件解析出的纯文本内容。由于不同类型的文件需要对应不同的 FileLoader我们判断目标文件类型并针对性调用对应类型的 FileLoader同时调用 FileLoader 对象的 load 方法来得到加载之后的纯文本对象
from tqdm import tqdm
from langchain.document_loaders import UnstructuredFileLoader
from langchain.document_loaders import UnstructuredMarkdownLoaderdef get_text(dir_path):# argsdir_path目标文件夹路径# 首先调用上文定义的函数得到目标文件路径列表file_lst get_files(dir_path)# docs 存放加载之后的纯文本对象docs []# 遍历所有目标文件for one_file in tqdm(file_lst):file_type one_file.split(.)[-1]if file_type md:loader UnstructuredMarkdownLoader(one_file)elif file_type txt:loader UnstructuredFileLoader(one_file)else:# 如果是不符合条件的文件直接跳过continuedocs.extend(loader.load())return docs
使用上文函数我们得到的 docs 为一个纯文本对象对应的列表。
2.3 构建向量数据库
得到该列表之后我们就可以将它引入到 LangChain 框架中构建向量数据库。由纯文本对象构建向量数据库我们需要先对文本进行分块接着对文本块进行向量化。
LangChain 提供了多种文本分块工具此处我们使用字符串递归分割器并选择分块大小为 500块重叠长度为 150由于篇幅限制此处没有展示切割效果学习者可以自行尝试一下想要深入学习 LangChain 文本分块可以参考教程 《LangChain - Chat With Your Data》
from langchain.text_splitter import RecursiveCharacterTextSplittertext_splitter RecursiveCharacterTextSplitter(chunk_size500, chunk_overlap150)
split_docs text_splitter.split_documents(docs)
接着我们选用开源词向量模型 Sentence Transformer 来进行文本向量化。LangChain 提供了直接引入 HuggingFace 开源社区中的模型进行向量化的接口
from langchain.embeddings.huggingface import HuggingFaceEmbeddingsembeddings HuggingFaceEmbeddings(model_name/root/data/model/sentence-transformer)
同时考虑到 Chroma 是目前最常用的入门数据库我们选择 Chroma 作为向量数据库基于上文分块后的文档以及加载的开源向量化模型将语料加载到指定路径下的向量数据库
from langchain.vectorstores import Chroma# 定义持久化路径
persist_directory data_base/vector_db/chroma
# 加载数据库
vectordb Chroma.from_documents(documentssplit_docs,embeddingembeddings,persist_directorypersist_directory # 允许我们将persist_directory目录保存到磁盘上
)
# 将加载的向量数据库持久化到磁盘上
vectordb.persist()
2.4 整体脚本
将上述代码整合在一起为知识库搭建的脚本
# 首先导入所需第三方库
from langchain.document_loaders import UnstructuredFileLoader
from langchain.document_loaders import UnstructuredMarkdownLoader
from langchain.text_splitter import RecursiveCharacterTextSplitter
from langchain.vectorstores import Chroma
from langchain.embeddings.huggingface import HuggingFaceEmbeddings
from tqdm import tqdm
import os# 获取文件路径函数
def get_files(dir_path):# argsdir_path目标文件夹路径file_list []for filepath, dirnames, filenames in os.walk(dir_path):# os.walk 函数将递归遍历指定文件夹for filename in filenames:# 通过后缀名判断文件类型是否满足要求if filename.endswith(.md):# 如果满足要求将其绝对路径加入到结果列表file_list.append(os.path.join(filepath, filename))elif filename.endswith(.txt):file_list.append(os.path.join(filepath, filename))return file_list# 加载文件函数
def get_text(dir_path):# argsdir_path目标文件夹路径# 首先调用上文定义的函数得到目标文件路径列表file_lst get_files(dir_path)# docs 存放加载之后的纯文本对象docs []# 遍历所有目标文件for one_file in tqdm(file_lst):file_type one_file.split(.)[-1]if file_type md:loader UnstructuredMarkdownLoader(one_file)elif file_type txt:loader UnstructuredFileLoader(one_file)else:# 如果是不符合条件的文件直接跳过continuedocs.extend(loader.load())return docs# 目标文件夹
tar_dir [/root/data/InternLM,/root/data/InternLM-XComposer,/root/data/lagent,/root/data/lmdeploy,/root/data/opencompass,/root/data/xtuner
]# 加载目标文件
docs []
for dir_path in tar_dir:docs.extend(get_text(dir_path))# 对文本进行分块
text_splitter RecursiveCharacterTextSplitter(chunk_size500, chunk_overlap150)
split_docs text_splitter.split_documents(docs)# 加载开源词向量模型
embeddings HuggingFaceEmbeddings(model_name/root/data/model/sentence-transformer)# 构建向量数据库
# 定义持久化路径
persist_directory data_base/vector_db/chroma
# 加载数据库
vectordb Chroma.from_documents(documentssplit_docs,embeddingembeddings,persist_directorypersist_directory # 允许我们将persist_directory目录保存到磁盘上
)
# 将加载的向量数据库持久化到磁盘上
vectordb.persist()
可以在 /root/data 下新建一个 demo目录将该脚本和后续脚本均放在该目录下运行。运行上述脚本即可在本地构建已持久化的向量数据库后续直接导入该数据库即可无需重复构建。 3 InternLM 接入 LangChain
为便捷构建 LLM 应用我们需要基于本地部署的 InternLM继承 LangChain 的 LLM 类自定义一个 InternLM LLM 子类从而实现将 InternLM 接入到 LangChain 框架中。完成 LangChain 的自定义 LLM 子类之后可以以完全一致的方式调用 LangChain 的接口而无需考虑底层模型调用的不一致。
基于本地部署的 InternLM 自定义 LLM 类并不复杂我们只需从 LangChain.llms.base.LLM 类继承一个子类并重写构造函数与 _call 函数即可
from langchain.llms.base import LLM
from typing import Any, List, Optional
from langchain.callbacks.manager import CallbackManagerForLLMRun
from transformers import AutoTokenizer, AutoModelForCausalLM
import torchclass InternLM_LLM(LLM):# 基于本地 InternLM 自定义 LLM 类tokenizer : AutoTokenizer Nonemodel: AutoModelForCausalLM Nonedef __init__(self, model_path :str):# model_path: InternLM 模型路径# 从本地初始化模型super().__init__()print(正在从本地加载模型...)self.tokenizer AutoTokenizer.from_pretrained(model_path, trust_remote_codeTrue)self.model AutoModelForCausalLM.from_pretrained(model_path, trust_remote_codeTrue).to(torch.bfloat16).cuda()self.model self.model.eval()print(完成本地模型的加载)def _call(self, prompt : str, stop: Optional[List[str]] None,run_manager: Optional[CallbackManagerForLLMRun] None,**kwargs: Any):# 重写调用函数system_prompt You are an AI assistant whose name is InternLM (书生·浦语).- InternLM (书生·浦语) is a conversational language model that is developed by Shanghai AI Laboratory (上海人工智能实验室). It is designed to be helpful, honest, and harmless.- InternLM (书生·浦语) can understand and communicate fluently in the language chosen by the user such as English and 中文.messages [(system_prompt, )]response, history self.model.chat(self.tokenizer, prompt , historymessages)return responsepropertydef _llm_type(self) - str:return InternLM 在上述类定义中我们分别重写了构造函数和 _call 函数对于构造函数我们在对象实例化的一开始加载本地部署的 InternLM 模型从而避免每一次调用都需要重新加载模型带来的时间过长_call 函数是 LLM 类的核心函数LangChain 会调用该函数来调用 LLM在该函数中我们调用已实例化模型的 chat 方法从而实现对模型的调用并返回调用结果。
在整体项目中我们将上述代码封装为 LLM.py后续将直接从该文件中引入自定义的 LLM 类。
4 构建检索问答链
LangChain 通过提供检索问答链对象来实现对于 RAG 全流程的封装。所谓检索问答链即通过一个对象完成检索增强问答即RAG的全流程针对 RAG 的更多概念我们会在视频内容中讲解也欢迎读者查阅该教程来进一步了解《LLM Universe》。我们可以调用一个 LangChain 提供的 RetrievalQA 对象通过初始化时填入已构建的数据库和自定义 LLM 作为参数来简便地完成检索增强问答的全流程LangChain 会自动完成基于用户提问进行检索、获取相关文档、拼接为合适的 Prompt 并交给 LLM 问答的全部流程。
5 部署 Web Demo
在完成上述核心功能后我们可以基于 Gradio 框架将其部署到 Web 网页从而搭建一个小型 Demo便于测试与使用。
我们首先将上文的代码内容封装为一个返回构建的检索问答链对象的函数并在启动 Gradio 的第一时间调用该函数得到检索问答链对象后续直接使用该对象进行问答对话从而避免重复加载模型
vim /data/demo/web_demo.py
from langchain.vectorstores import Chroma
from langchain.embeddings.huggingface import HuggingFaceEmbeddings
import os
from LLM import InternLM_LLM
from langchain.prompts import PromptTemplate
from langchain.chains import RetrievalQA
import gradio as grdef load_chain():# 加载问答链# 定义 Embeddingsembeddings HuggingFaceEmbeddings(model_name/root/data/model/sentence-transformer)# 向量数据库持久化路径persist_directory data_base/vector_db/chroma# 加载数据库vectordb Chroma(persist_directorypersist_directory, # 允许我们将persist_directory目录保存到磁盘上embedding_functionembeddings)# 加载自定义 LLMllm InternLM_LLM(model_path /root/data/model/Shanghai_AI_Laboratory/internlm-chat-7b)# 定义一个 Prompt Templatetemplate 使用以下上下文来回答最后的问题。如果你不知道答案就说你不知道不要试图编造答案。尽量使答案简明扼要。总是在回答的最后说“谢谢你的提问”。{context}问题: {question}有用的回答:QA_CHAIN_PROMPT PromptTemplate(input_variables[context,question],templatetemplate)# 运行 chainqa_chain RetrievalQA.from_chain_type(llm,retrievervectordb.as_retriever(),return_source_documentsTrue,chain_type_kwargs{prompt:QA_CHAIN_PROMPT})return qa_chainclass Model_center():存储检索问答链的对象 def __init__(self):# 构造函数加载检索问答链self.chain load_chain()def qa_chain_self_answer(self, question: str, chat_history: list []):调用问答链进行回答if question None or len(question) 1:return , chat_historytry:chat_history.append((question, self.chain({query: question})[result]))# 将问答结果直接附加到问答历史中Gradio 会将其展示出来return , chat_historyexcept Exception as e:return e, chat_history# 实例化核心功能对象
model_center Model_center()
# 创建一个 Web 界面
block gr.Blocks()
with block as demo:with gr.Row(equal_heightTrue): with gr.Column(scale15):# 展示的页面标题gr.Markdown(h1centerInternLM/center/h1center书生浦语/center)with gr.Row():with gr.Column(scale4):# 创建一个聊天机器人对象chatbot gr.Chatbot(height450, show_copy_buttonTrue)# 创建一个文本框组件用于输入 prompt。msg gr.Textbox(labelPrompt/问题)with gr.Row():# 创建提交按钮。db_wo_his_btn gr.Button(Chat)with gr.Row():# 创建一个清除按钮用于清除聊天机器人组件的内容。clear gr.ClearButton(components[chatbot], valueClear console)# 设置按钮的点击事件。当点击时调用上面定义的 qa_chain_self_answer 函数并传入用户的消息和聊天历史记录然后更新文本框和聊天机器人组件。db_wo_his_btn.click(model_center.qa_chain_self_answer, inputs[msg, chatbot], outputs[msg, chatbot])gr.Markdown(提醒br1. 初始化数据库时间可能较长请耐心等待。2. 使用中如果出现异常将会在文本输入框进行展示请不要惊慌。 br)
gr.close_all()
# 直接启动
demo.launch() 通过将上述代码封装为 run_gradio.py 脚本直接通过 python 命令运行即可在本地启动知识库助手的 Web Demo默认会在 7860 端口运行接下来将服务器端口映射到本地端口即可访问:
此处我们简要介绍如何将服务器端口映射到本地端口
首先我们需要配置一下本地的 SSH Key 我们这里以Windows为例。
在本地机器上打开Power Shell终端。在终端中运行以下命令来生成SSH密钥对如下图所示 ssh-keygen -t rsa 您将被提示选择密钥文件的保存位置默认情况下是在 ~/.ssh/ 目录中。按Enter键接受默认值或输入自定义路径。 公钥默认存储在 ~/.ssh/id_rsa.pub可以通过系统自带的 cat 工具查看文件内容如下图所示 cat ~\.ssh\id_rsa.pub将公钥复制到剪贴板中然后回到 InternStudio 控制台点击配置SSH Key。如下图所示在本地终端输入以下指令.7860是在服务器中打开的端口而33090是根据开发机的端口进行更改。如下图所示 ssh -CNg -L 7860:127.0.0.1:7860 rootssh.intern-ai.org.cn -p 36478 通过将上述代码封装为 run_gradio.py 脚本直接通过 python 命令运行即可在本地启动知识库助手的 Web Demo默认会在 7860 端口运行接下来将服务器端口映射到本地端口即可访问: 进阶作业
选择领域
选择一个垂直领域收集该领域的专业资料构建专业知识库并搭建专业问答助手并在 OpenXLab 上成功部署截图选择了一个不常见的领域联邦学习在自动驾驶领域的应用 安装pdf加载依赖构建向量库
pip install pypdf
python create_db.py
# 首先导入所需第三方库
from langchain.document_loaders import UnstructuredFileLoader
from langchain.document_loaders import PyPDFLoader
from langchain.document_loaders import UnstructuredMarkdownLoader
from langchain.text_splitter import RecursiveCharacterTextSplitter
from langchain.vectorstores import Chroma
from langchain.embeddings.huggingface import HuggingFaceEmbeddings
from tqdm import tqdm
import os# 获取文件路径函数
def get_files(dir_path):# argsdir_path目标文件夹路径file_list []for filepath, dirnames, filenames in os.walk(dir_path):# os.walk 函数将递归遍历指定文件夹for filename in filenames:# 通过后缀名判断文件类型是否满足要求if filename.endswith(.md):# 如果满足要求将其绝对路径加入到结果列表file_list.append(os.path.join(filepath, filename))elif filename.endswith(.txt):file_list.append(os.path.join(filepath, filename))elif filename.endswith(.pdf):file_list.append(os.path.join(filepath, filename))return file_list# 加载文件函数
def get_text(dir_path):# argsdir_path目标文件夹路径# 首先调用上文定义的函数得到目标文件路径列表file_lst get_files(dir_path)# docs 存放加载之后的纯文本对象docs []# 遍历所有目标文件for one_file in tqdm(file_lst):file_type one_file.split(.)[-1]if file_type md:loader UnstructuredMarkdownLoader(one_file)elif file_type txt:loader UnstructuredFileLoader(one_file)elif file_type pdf:loader PyPDFLoader(one_file)else:# 如果是不符合条件的文件直接跳过continuedocs.extend(loader.load())return docs# 目标文件夹
tar_dir [/root/data/InternLM,/root/data/InternLM-XComposer,/root/data/lagent,/root/data/lmdeploy,/root/data/opencompass,/root/data/xtuner
]# 加载目标文件
docs []
for dir_path in tar_dir:docs.extend(get_text(dir_path))# 对文本进行分块
text_splitter RecursiveCharacterTextSplitter(chunk_size500, chunk_overlap150)
split_docs text_splitter.split_documents(docs)# 加载开源词向量模型
embeddings HuggingFaceEmbeddings(model_name/root/data/model/sentence-transformer)# 构建向量数据库
# 定义持久化路径
persist_directory data_base/vector_db/chroma
# 加载数据库
vectordb Chroma.from_documents(documentssplit_docs,embeddingembeddings,persist_directorypersist_directory # 允许我们将persist_directory目录保存到磁盘上
)
# 将加载的向量数据库持久化到磁盘上
vectordb.persist()
通过脚本测试有无RAG的效果区别 创建一个app.py用于发布到浦源平台
__import__(pysqlite3)
import sys
sys.modules[sqlite3] sys.modules.pop(pysqlite3)import gradio as gr
from langchain.vectorstores import Chroma
from langchain.embeddings.huggingface import HuggingFaceEmbeddings
import os
from LLM import InternLM_LLM
from langchain.prompts import PromptTemplate
from langchain.chains import RetrievalQA
from modelscope import snapshot_download, AutoModel, AutoTokenizerdef init():model_dir snapshot_download(Shanghai_AI_Laboratory/internlm-chat-7b, cache_dir./, revisionv1.0.3)os.environ[HF_ENDPOINT] https://hf-mirror.com# 下载模型os.system(huggingface-cli download --resume-download sentence-transformers/paraphrase-multilingual-MiniLM-L12-v2 --local-dir sentence-transformer)def load_chain():# 加载问答链# 定义 Embeddingsembeddings HuggingFaceEmbeddings(model_namesentence-transformer)# 向量数据库持久化路径persist_directory data_base/vector_db/chroma# 加载数据库vectordb Chroma(persist_directorypersist_directory, # 允许我们将persist_directory目录保存到磁盘上embedding_functionembeddings)# 加载自定义 LLMllm InternLM_LLM(model_path Shanghai_AI_Laboratory/internlm-chat-7b)# 定义一个 Prompt Templatetemplate 使用以下上下文来回答最后的问题。如果你不知道答案就说你不知道不要试图编造答案。尽量使回答具有条理。总是在回答的最后说“谢谢你的提问”。{context}问题: {question}有用的回答:QA_CHAIN_PROMPT PromptTemplate(input_variables[context,question],templatetemplate)# 运行 chainqa_chain RetrievalQA.from_chain_type(llm, retrievervectordb.as_retriever(), return_source_documentsTrue, chain_type_kwargs{prompt:QA_CHAIN_PROMPT})return qa_chainclass Model_center():存储检索问答链的对象 init()def __init__(self):# 构造函数加载检索问答链self.chain load_chain()def qa_chain_self_answer(self, question: str, chat_history: list []):调用问答链进行回答if question None or len(question) 1:return , chat_historytry:chat_history.append((question, self.chain({query: question})[result]))# 将问答结果直接附加到问答历史中Gradio 会将其展示出来return , chat_historyexcept Exception as e:return e, chat_history# 实例化核心功能对象
model_center Model_center()
# 创建一个 Web 界面
block gr.Blocks()
with block as demo:with gr.Row(equal_heightTrue): with gr.Column(scale15):# 展示的页面标题gr.Markdown(h1centerInternLM/center/h1center书生浦语RAG应用/center) with gr.Row():with gr.Column(scale4):# 创建一个聊天机器人对象chatbot gr.Chatbot(height450, show_copy_buttonTrue)# 创建一个文本框组件用于输入 prompt。msg gr.Textbox(labelPrompt/问题)with gr.Row():# 创建提交按钮。db_wo_his_btn gr.Button(Chat)with gr.Row():# 创建一个清除按钮用于清除聊天机器人组件的内容。clear gr.ClearButton(components[chatbot], valueClear console)# 设置按钮的点击事件。当点击时调用上面定义的 qa_chain_self_answer 函数并传入用户的消息和聊天历史记录然后更新文本框和聊天机器人组件。db_wo_his_btn.click(model_center.qa_chain_self_answer, inputs[msg, chatbot], outputs[msg, chatbot])gr.Markdown(提醒br1. 初始化数据库时间可能较长请耐心等待。2. 使用中如果出现异常将会在文本输入框进行展示请不要惊慌。 br)
gr.close_all()
# 直接启动
demo.launch()
上传代码仓库到github: 申请openxlab GPU
https://openxlab.org.cn/apps/apply-hardware
创建应用
https://openxlab.org.cn/openplatform?langzh-CN