8.6 系统测试
现在可以开始使用我们的问答系统,输入问题并获取模型生成的答案,展示系统如何从多个文档中检索信息并使用问答链来生成回答,这包括“Ask questions”、“Invoke QA Chain”等交互操作。
8.6.1 基于llama2-13b-chat模型的问答
(1)看下面的代码,CFG.model_name 是一个类属性,用于指定在项目或应用程序中使用的预训练语言模型的名称。这个属性被用于初始化或加载特定的语言模型,以执行文本生成、问答、文本分类或其他自然语言处理(NLP)任务。
CFG.model_name
执行后会输出:
'llama2-13b-chat'
这表明接下来的问答系统是基于llama2-13b-chat预训练模型实现的。
(2)使用前面定义的函数 llm_ans来处理一个特定的用户查询,并将结果打印出来。
query = "Which challenges does Harry face during the Triwizard Tournament?"
print(llm_ans(query))
执行这段代码后将得到一个格式化的回答,其中包含了哈利在三强争霸赛中所面临的挑战的描述,以及生成这个答案所花费的时间。
During the Triwizard Tournament, Harry faces several challenges, including:
* The first task, which involves retrieving a golden egg from a dragon nest.
* The second task, which requires him to navigate a maze while being pursued by a giant spider.
* The third task, which takes place that evening and involves facing his fears and overcoming them.
Sources:
Harry Potter - Book 4 - The Goblet of Fire - page: 271
Harry Potter - Book 4 - The Goblet of Fire - page: 237
Harry Potter - Book 4 - The Goblet of Fire - page: 252
Harry Potter - Book 2 - The Chamber of Secrets - page: 116
Harry Potter - Book 4 - The Goblet of Fire - page: 628
Harry Potter - Book 4 - The Goblet of Fire - page: 279
Time elapsed: 13 s
这个过程展示了将自然语言处理模型集成到一个问答系统中,以提供有用的信息并增强用户体验的用法。
(3)使用前面定义的函数llm_ans,处理用户提出的关于《哈利·波特》系列中的角色德拉科·马尔福(Draco Malfoy)是否是伏地魔(Voldemort)的盟友的问题。
query = "Is Malfoy an ally of Voldemort?"
print(llm_ans(query))
执行后会输出:
Yes, according to the passage, Malfoy is an ally of Voldemort.
Sources:
Harry Potter - Book 6 - The Half-Blood Prince - page: 716
Harry Potter - Book 4 - The Goblet of Fire - page: 665
Harry Potter - Book 7 - The Deathly Hallows - page: 16
Harry Potter - Book 1 - The Sorcerers Stone - page: 156
Harry Potter - Book 7 - The Deathly Hallows - page: 648
Harry Potter - Book 4 - The Goblet of Fire - page: 184
Time elapsed: 4 s
(4)处理用户关于《哈利·波特》系列中“魂器”(Horcrux)的信息查询,并打印输出答案。
query = "What are horcrux?"
print(llm_ans(query))
执行后会输出:
Based on the text, a Horcrux is an object in which a person has concealed part of their soul. It is created by committing murder, which rips the soul apart, and then encasing the torn portion in the object.
Sources:
Harry Potter - Book 6 - The Half-Blood Prince - page: 557
Harry Potter - Book 6 - The Half-Blood Prince - page: 419
Harry Potter - Book 6 - The Half-Blood Prince - page: 715
Harry Potter - Book 7 - The Deathly Hallows - page: 618
Harry Potter - Book 6 - The Half-Blood Prince - page: 558
Harry Potter - Book 7 - The Deathly Hallows - page: 108
Time elapsed: 8 s
(5)处理用户提出的关于“给出五个酷炫药剂的例子并解释它们的用途”的查询,并打印输出答案。
query = "Give me 5 examples of cool potions and explain what they do"
print(llm_ans(query))
执行后会输出:
Sure, here are five examples of cool potions from the Harry Potter series and a brief explanation of what they do:
1. Polyjuice Potion - This potion allows the drinker to take on the appearance of another person. It's used by the characters in the series to disguise themselves and carry out missions.
2. Draught of Living Death - This potion puts the drinker into a deep sleep that lasts for days. It's used by the characters in the series to escape danger or to get a good night's rest.
3. Felix Felicis - Also known as Liquid Luck, this potion makes the drinker lucky for a short period of time. It's used by the characters in the series to improve their chances of success in various endeavors.
4. Ambrosia - This potion is a delicious drink that grants the drinker temporary immortality. It's used by the characters in the series to prolong their lives and avoid death.
5. Gillywater - This potion is a refreshing drink that cures hangovers and other forms of alcohol poisoning. It's used by the characters in the series to recover from excessive drinking.
Sources:
Harry Potter - Book 6 - The Half-Blood Prince - page: 204
Harry Potter - Book 6 - The Half-Blood Prince - page: 204
Harry Potter - Book 7 - The Deathly Hallows - page: 76
Harry Potter - Book 2 - The Chamber of Secrets - page: 183
Harry Potter - Book 6 - The Half-Blood Prince - page: 93
Harry Potter - Book 6 - The Half-Blood Prince - page: 209
Time elapsed: 40 s
8.6.2 创建聊天界面UI
为了提高本系统的用户体验,接下来使用库Gradio创建聊天用户界面(Chat UI)。Gradio 是一个用于快速创建机器学习模型前端界面的 Python 库,它可以创建聊天界面,允许用户与模型进行交互。请看下面的代码,使用 Gradio创建并启动了一个聊天界面,该界面与一个预定义的预测函数 predict 进行交互。
import locale
locale.getpreferredencoding = lambda: "UTF-8"
clear_output()
import gradio as gr
print(gr.__version__)
def predict(message, history):
output = message
output = str(llm_ans(message)).replace("\n", "<br/>")
return output
demo = gr.ChatInterface(
predict,
title = f' Open-Source LLM ({CFG.model_name}) for Harry Potter Question Answering'
)
demo.queue()
demo.launch()
注意,由于在 Kaggle 或某些 Jupyter Notebook 环境中可能存在兼容性问题,上述代码可能不会在所有平台上正常工作。建议在 Google Colab 中运行 Gradio 创建的聊天界面,在 Google Colab 中的执行效果如图8-8所示。
图8-8 了解界面UI
本项目暂时完结:
(20-1)基于《哈利·波特》系列图书内容的问答系统:项目介绍+ 大型语言模型(LLMs)+准备环境-CSDN博客
(20-2-01)基于《哈利·波特》系列图书内容的问答系统:语言模型的集成与优化(1)-CSDN博客
(20-2-02)基于《哈利·波特》系列图书内容的问答系统:语言模型的集成与优化(2)-CSDN博客
(20-3-01)基于《哈利·波特》系列图书内容的问答系统:LangChain多文档检索器(01)加载文档+文本分割+创建嵌入+加载向量数据库-CSDN博客
(20-4)基于《哈利·波特》系列图书内容的问答系统:系统测试-CSDN博客