(20-4)基于《哈利·波特》系列图书内容的问答系统:系统测试

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-3-02)基于《哈利·波特》系列图书内容的问答系统:LangChain多文档检索器(02)Prompt Template(提示模板)+Retriever Chain(检索链)+格式化输出-CSDN博客

(20-4)基于《哈利·波特》系列图书内容的问答系统:系统测试-CSDN博客

  • 10
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
根据提供的引用内容,我们可以了解到《哈》是一部关于哈、赫敏、罗恩等人在大法师邓布多的帮助下,使用魔法抵抗伏地魔的故事。同时,根据引用和引用,我们可以使用Python对小说中的人物名字和出现频率进行统计和分析。 以下是Python代码示例: 1. 统计人物名字TOP20的词语 ```python import jieba import pandas as pd from collections import Counter from pyecharts import Bar # 读取小说文本 with open('harry_potter.txt', 'r', encoding='utf-8') as f: text = f.read() # 使用jieba分词 words = jieba.lcut(text) # 统计人物名字出现的次数 names = ['哈', '赫敏', '罗恩', '邓布多', '马尔福', '斯内普', '小天狼星'] names_count = Counter([word for word in words if word in names]) # 绘制柱状图 bar = Bar('主要人物Top20', background_color='white', title_pos='center', title_text_size=20) x = names_count.most_common(20) bar.add('', [i[0] for i in x], [i[1] for i in x], xaxis_interval=0, xaxis_rotate=30, is_label_show=True) bar.render() ``` 2. 统计整部小说出现最多的词语TOP15 ```python import jieba import pandas as pd from collections import Counter # 读取小说文本 with open('harry_potter.txt', 'r', encoding='utf-8') as f: text = f.read() # 使用jieba分词 words = jieba.lcut(text) # 统计词语出现的次数 words_count = Counter(words) # 去除停用词 stopwords = pd.read_csv('stopwords.txt', index_col=False, quoting=3, sep='\t', names=['stopword'], encoding='utf-8') words = [word for word in words if word not in stopwords] # 统计出现最多的词语TOP15 top15 = words_count.most_common(15) print(top15) ```

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

码农三叔

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值