Add files via upload

This commit is contained in:
Logan Zou
2023-06-02 01:06:23 +08:00
committed by GitHub
parent 4e3cd27ca2
commit d24fbc681c
2 changed files with 1203 additions and 0 deletions

File diff suppressed because one or more lines are too long

View File

@ -0,0 +1,688 @@
{
"cells": [
{
"attachments": {},
"cell_type": "markdown",
"metadata": {},
"source": [
"# 第九章 评估(下)——当不存在一个简单的正确答案时"
]
},
{
"attachments": {},
"cell_type": "markdown",
"metadata": {},
"source": [
"环境配置"
]
},
{
"cell_type": "code",
"execution_count": 1,
"metadata": {},
"outputs": [],
"source": [
"# 导入 OpenAI API\n",
"import os\n",
"import openai\n",
"import sys\n",
"sys.path.append('../..')\n",
"import utils_en\n",
"import utils_ch\n",
"from dotenv import load_dotenv, find_dotenv\n",
"_ = load_dotenv(find_dotenv()) # read local .env file\n",
"\n",
"openai.api_key = os.environ['OPENAI_API_KEY']"
]
},
{
"cell_type": "code",
"execution_count": 2,
"metadata": {},
"outputs": [],
"source": [
"# 封装一个访问 OpenAI GPT3.5 的函数\n",
"def get_completion_from_messages(messages, model=\"gpt-3.5-turbo\", temperature=0, max_tokens=500):\n",
" response = openai.ChatCompletion.create(\n",
" model=model,\n",
" messages=messages,\n",
" temperature=temperature, \n",
" max_tokens=max_tokens, \n",
" )\n",
" return response.choices[0].message[\"content\"]"
]
},
{
"attachments": {},
"cell_type": "markdown",
"metadata": {},
"source": [
"运行问答系统获得回答"
]
},
{
"cell_type": "code",
"execution_count": 8,
"metadata": {},
"outputs": [],
"source": [
"# 用户消息\n",
"customer_msg = f\"\"\"\n",
"tell me about the smartx pro phone and the fotosnap camera, the dslr one.\n",
"Also, what TVs or TV related products do you have?\"\"\"\n",
"\n",
"# 从问题中抽取商品名\n",
"products_by_category = utils_en.get_products_from_query(customer_msg)\n",
"# 将商品名转化为列表\n",
"category_and_product_list = utils_en.read_string_to_list(products_by_category)\n",
"# 查找商品对应的信息\n",
"product_info = utils_en.get_mentioned_product_info(category_and_product_list)\n",
"# 由信息生成回答\n",
"assistant_answer = utils_en.answer_user_msg(user_msg=customer_msg, product_info=product_info)"
]
},
{
"cell_type": "code",
"execution_count": 9,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"Sure, I'd be happy to help! The SmartX ProPhone is a powerful smartphone with a 6.1-inch display, 128GB storage, 12MP dual camera, and 5G capabilities. The FotoSnap DSLR Camera is a versatile camera with a 24.2MP sensor, 1080p video, 3-inch LCD, and interchangeable lenses. As for TVs, we have a variety of options including the CineView 4K TV with a 55-inch display, 4K resolution, HDR, and smart TV capabilities, the CineView 8K TV with a 65-inch display, 8K resolution, HDR, and smart TV capabilities, and the CineView OLED TV with a 55-inch display, 4K resolution, HDR, and smart TV capabilities. We also have the SoundMax Home Theater system with 5.1 channel, 1000W output, wireless subwoofer, and Bluetooth, and the SoundMax Soundbar with 2.1 channel, 300W output, wireless subwoofer, and Bluetooth. Is there anything else I can help you with?\n"
]
}
],
"source": [
"print(assistant_answer) "
]
},
{
"cell_type": "code",
"execution_count": 4,
"metadata": {},
"outputs": [],
"source": [
"'''\n",
"中文Prompt\n",
"注意限于模型对中文理解能力较弱中文Prompt可能会随机出现不成功可以多次运行也非常欢迎同学探究更稳定的中文 Prompt\n",
"'''\n",
"# 用户消息\n",
"customer_msg = f\"\"\"\n",
"告诉我有关 the smartx pro phone 和 the fotosnap camera, the dslr one 的信息。\n",
"另外,你们这有什么 TVs \"\"\"\n",
"\n",
"# 从问题中抽取商品名\n",
"products_by_category = utils_ch.get_products_from_query(customer_msg)\n",
"# 将商品名转化为列表\n",
"category_and_product_list = utils_ch.read_string_to_list(products_by_category)\n",
"# 查找商品对应的信息\n",
"product_info = utils_ch.get_mentioned_product_info(category_and_product_list)\n",
"# 由信息生成回答\n",
"assistant_answer = utils_ch.answer_user_msg(user_msg=customer_msg, product_info=product_info)"
]
},
{
"cell_type": "code",
"execution_count": 5,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"关于SmartX ProPhone和FotoSnap DSLR相机的信息\n",
"\n",
"SmartX ProPhone是一款功能强大的智能手机具有6.1英寸的显示屏128GB的存储空间12MP双摄像头和5G网络。它的价格为899.99美元保修期为1年。\n",
"\n",
"FotoSnap DSLR相机是一款功能强大的相机具有24.2MP传感器1080p视频3英寸LCD屏幕和可更换镜头。它的价格为599.99美元保修期为1年。\n",
"\n",
"关于电视的信息:\n",
"\n",
"我们有多种电视可供选择包括CineView 4K电视CineView 8K电视和CineView OLED电视。CineView 4K电视具有55英寸的显示屏4K分辨率HDR和智能电视功能价格为599.99美元保修期为2年。CineView 8K电视具有65英寸的显示屏8K分辨率HDR和智能电视功能价格为2999.99美元保修期为2年。CineView OLED电视具有55英寸的显示屏4K分辨率HDR和智能电视功能价格为1499.99美元保修期为2年。您需要哪种类型的电视\n"
]
}
],
"source": [
"print(assistant_answer) "
]
},
{
"attachments": {},
"cell_type": "markdown",
"metadata": {},
"source": [
"使用 GPT 评估回答是否正确"
]
},
{
"cell_type": "code",
"execution_count": 8,
"metadata": {},
"outputs": [],
"source": [
"# 问题、上下文\n",
"cust_prod_info = {\n",
" 'customer_msg': customer_msg,\n",
" 'context': product_info\n",
"}"
]
},
{
"cell_type": "code",
"execution_count": 11,
"metadata": {},
"outputs": [],
"source": [
"# 使用 GPT API 评估生成的回答\n",
"def eval_with_rubric(test_set, assistant_answer):\n",
"\n",
" cust_msg = test_set['customer_msg']\n",
" context = test_set['context']\n",
" completion = assistant_answer\n",
" \n",
" # 要求 GPT 作为一个助手评估回答正确性\n",
" system_message = \"\"\"\\\n",
" You are an assistant that evaluates how well the customer service agent \\\n",
" answers a user question by looking at the context that the customer service \\\n",
" agent is using to generate its response. \n",
" \"\"\"\n",
"\n",
" # 具体指令\n",
" user_message = f\"\"\"\\\n",
"You are evaluating a submitted answer to a question based on the context \\\n",
"that the agent uses to answer the question.\n",
"Here is the data:\n",
" [BEGIN DATA]\n",
" ************\n",
" [Question]: {cust_msg}\n",
" ************\n",
" [Context]: {context}\n",
" ************\n",
" [Submission]: {completion}\n",
" ************\n",
" [END DATA]\n",
"\n",
"Compare the factual content of the submitted answer with the context. \\\n",
"Ignore any differences in style, grammar, or punctuation.\n",
"Answer the following questions:\n",
" - Is the Assistant response based only on the context provided? (Y or N)\n",
" - Does the answer include information that is not provided in the context? (Y or N)\n",
" - Is there any disagreement between the response and the context? (Y or N)\n",
" - Count how many questions the user asked. (output a number)\n",
" - For each question that the user asked, is there a corresponding answer to it?\n",
" Question 1: (Y or N)\n",
" Question 2: (Y or N)\n",
" ...\n",
" Question N: (Y or N)\n",
" - Of the number of questions asked, how many of these questions were addressed by the answer? (output a number)\n",
"\"\"\"\n",
"\n",
" messages = [\n",
" {'role': 'system', 'content': system_message},\n",
" {'role': 'user', 'content': user_message}\n",
" ]\n",
"\n",
" response = get_completion_from_messages(messages)\n",
" return response"
]
},
{
"cell_type": "code",
"execution_count": 12,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"- Is the Assistant response based only on the context provided? (Y or N)\n",
"Y\n",
"- Does the answer include information that is not provided in the context? (Y or N)\n",
"N\n",
"- Is there any disagreement between the response and the context? (Y or N)\n",
"N\n",
"- Count how many questions the user asked. (output a number)\n",
"1\n",
"- For each question that the user asked, is there a corresponding answer to it?\n",
" Question 1: Y\n",
"- Of the number of questions asked, how many of these questions were addressed by the answer? (output a number)\n",
"1\n"
]
}
],
"source": [
"evaluation_output = eval_with_rubric(cust_prod_info, assistant_answer)\n",
"print(evaluation_output)"
]
},
{
"cell_type": "code",
"execution_count": 6,
"metadata": {},
"outputs": [],
"source": [
"'''中文Prompt'''\n",
"# 使用 GPT API 评估生成的回答\n",
"def eval_with_rubric(test_set, assistant_answer):\n",
"\n",
" cust_msg = test_set['customer_msg']\n",
" context = test_set['context']\n",
" completion = assistant_answer\n",
" \n",
" # 人设\n",
" system_message = \"\"\"\\\n",
" 你是一位助理,通过查看客户服务代理使用的上下文来评估客户服务代理回答用户问题的情况。\n",
" \"\"\"\n",
"\n",
" # 具体指令\n",
" user_message = f\"\"\"\\\n",
" 你正在根据代理使用的上下文评估对问题的提交答案。以下是数据:\n",
" [开始]\n",
" ************\n",
" [用户问题]: {cust_msg}\n",
" ************\n",
" [使用的上下文]: {context}\n",
" ************\n",
" [客户代理的回答]: {completion}\n",
" ************\n",
" [结束]\n",
"\n",
" 请将提交的答案的事实内容与上下文进行比较,忽略样式、语法或标点符号上的差异。\n",
" 回答以下问题:\n",
" 助手的回应是否只基于所提供的上下文?(是或否)\n",
" 回答中是否包含上下文中未提供的信息?(是或否)\n",
" 回应与上下文之间是否存在任何不一致之处?(是或否)\n",
" 计算用户提出了多少个问题。(输出一个数字)\n",
" 对于用户提出的每个问题,是否有相应的回答?\n",
" 问题1是或否\n",
" 问题2是或否\n",
" ...\n",
" 问题N是或否\n",
" 在提出的问题数量中,有多少个问题在回答中得到了回应?(输出一个数字)\n",
"\"\"\"\n",
"\n",
" messages = [\n",
" {'role': 'system', 'content': system_message},\n",
" {'role': 'user', 'content': user_message}\n",
" ]\n",
"\n",
" response = get_completion_from_messages(messages)\n",
" return response"
]
},
{
"cell_type": "code",
"execution_count": 10,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"助手的回应是基于所提供的上下文。回答中没有包含上下文中未提供的信息。回应与上下文之间没有任何不一致之处。\n",
"\n",
"用户提出了两个问题。\n",
"\n",
"对于用户提出的每个问题,都有相应的回答。\n",
"\n",
"问题1是\n",
"问题2是\n",
"\n",
"在提出的问题数量中所有问题都在回答中得到了回应因此输出数字为2。\n"
]
}
],
"source": [
"evaluation_output = eval_with_rubric(cust_prod_info, assistant_answer)\n",
"print(evaluation_output)"
]
},
{
"attachments": {},
"cell_type": "markdown",
"metadata": {},
"source": [
"给出一个标准回答,要求其评估生成回答与标准回答的差距"
]
},
{
"cell_type": "code",
"execution_count": 13,
"metadata": {},
"outputs": [],
"source": [
"test_set_ideal = {\n",
" 'customer_msg': \"\"\"\\\n",
"tell me about the smartx pro phone and the fotosnap camera, the dslr one.\n",
"Also, what TVs or TV related products do you have?\"\"\",\n",
" 'ideal_answer':\"\"\"\\\n",
"Of course! The SmartX ProPhone is a powerful \\\n",
"smartphone with advanced camera features. \\\n",
"For instance, it has a 12MP dual camera. \\\n",
"Other features include 5G wireless and 128GB storage. \\\n",
"It also has a 6.1-inch display. The price is $899.99.\n",
"\n",
"The FotoSnap DSLR Camera is great for \\\n",
"capturing stunning photos and videos. \\\n",
"Some features include 1080p video, \\\n",
"3-inch LCD, a 24.2MP sensor, \\\n",
"and interchangeable lenses. \\\n",
"The price is 599.99.\n",
"\n",
"For TVs and TV related products, we offer 3 TVs \\\n",
"\n",
"\n",
"All TVs offer HDR and Smart TV.\n",
"\n",
"The CineView 4K TV has vibrant colors and smart features. \\\n",
"Some of these features include a 55-inch display, \\\n",
"'4K resolution. It's priced at 599.\n",
"\n",
"The CineView 8K TV is a stunning 8K TV. \\\n",
"Some features include a 65-inch display and \\\n",
"8K resolution. It's priced at 2999.99\n",
"\n",
"The CineView OLED TV lets you experience vibrant colors. \\\n",
"Some features include a 55-inch display and 4K resolution. \\\n",
"It's priced at 1499.99.\n",
"\n",
"We also offer 2 home theater products, both which include bluetooth.\\\n",
"The SoundMax Home Theater is a powerful home theater system for \\\n",
"an immmersive audio experience.\n",
"Its features include 5.1 channel, 1000W output, and wireless subwoofer.\n",
"It's priced at 399.99.\n",
"\n",
"The SoundMax Soundbar is a sleek and powerful soundbar.\n",
"It's features include 2.1 channel, 300W output, and wireless subwoofer.\n",
"It's priced at 199.99\n",
"\n",
"Are there any questions additional you may have about these products \\\n",
"that you mentioned here?\n",
"Or may do you have other questions I can help you with?\n",
" \"\"\"\n",
"}"
]
},
{
"cell_type": "code",
"execution_count": 14,
"metadata": {},
"outputs": [],
"source": [
"def eval_vs_ideal(test_set, assistant_answer):\n",
"\n",
" cust_msg = test_set['customer_msg']\n",
" ideal = test_set['ideal_answer']\n",
" completion = assistant_answer\n",
" \n",
" system_message = \"\"\"\\\n",
" You are an assistant that evaluates how well the customer service agent \\\n",
" answers a user question by comparing the response to the ideal (expert) response\n",
" Output a single letter and nothing else. \n",
" \"\"\"\n",
"\n",
" user_message = f\"\"\"\\\n",
"You are comparing a submitted answer to an expert answer on a given question. Here is the data:\n",
" [BEGIN DATA]\n",
" ************\n",
" [Question]: {cust_msg}\n",
" ************\n",
" [Expert]: {ideal}\n",
" ************\n",
" [Submission]: {completion}\n",
" ************\n",
" [END DATA]\n",
"\n",
"Compare the factual content of the submitted answer with the expert answer. Ignore any differences in style, grammar, or punctuation.\n",
" The submitted answer may either be a subset or superset of the expert answer, or it may conflict with it. Determine which case applies. Answer the question by selecting one of the following options:\n",
" (A) The submitted answer is a subset of the expert answer and is fully consistent with it.\n",
" (B) The submitted answer is a superset of the expert answer and is fully consistent with it.\n",
" (C) The submitted answer contains all the same details as the expert answer.\n",
" (D) There is a disagreement between the submitted answer and the expert answer.\n",
" (E) The answers differ, but these differences don't matter from the perspective of factuality.\n",
" choice_strings: ABCDE\n",
"\"\"\"\n",
"\n",
" messages = [\n",
" {'role': 'system', 'content': system_message},\n",
" {'role': 'user', 'content': user_message}\n",
" ]\n",
"\n",
" response = get_completion_from_messages(messages)\n",
" return response"
]
},
{
"cell_type": "code",
"execution_count": 15,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"Sure, I'd be happy to help! The SmartX ProPhone is a powerful smartphone with a 6.1-inch display, 128GB storage, 12MP dual camera, and 5G capabilities. The FotoSnap DSLR Camera is a versatile camera with a 24.2MP sensor, 1080p video, 3-inch LCD, and interchangeable lenses. As for TVs, we have a variety of options including the CineView 4K TV with a 55-inch display, 4K resolution, HDR, and smart TV capabilities, the CineView 8K TV with a 65-inch display, 8K resolution, HDR, and smart TV capabilities, and the CineView OLED TV with a 55-inch display, 4K resolution, HDR, and smart TV capabilities. We also have the SoundMax Home Theater system with 5.1 channel, 1000W output, wireless subwoofer, and Bluetooth, and the SoundMax Soundbar with 2.1 channel, 300W output, wireless subwoofer, and Bluetooth. Is there anything else I can help you with?\n"
]
}
],
"source": [
"print(assistant_answer)"
]
},
{
"cell_type": "code",
"execution_count": 16,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"'A'"
]
},
"execution_count": 16,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"eval_vs_ideal(test_set_ideal, assistant_answer)\n",
"# 对于该生成回答GPT 判断生成内容是标准回答的一个子集"
]
},
{
"cell_type": "code",
"execution_count": 17,
"metadata": {},
"outputs": [],
"source": [
"assistant_answer_2 = \"life is like a box of chocolates\""
]
},
{
"cell_type": "code",
"execution_count": 18,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"'D'"
]
},
"execution_count": 18,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"eval_vs_ideal(test_set_ideal, assistant_answer_2)\n",
"# 对于明显异常答案GPT 判断为不一致"
]
},
{
"cell_type": "code",
"execution_count": 18,
"metadata": {},
"outputs": [],
"source": [
"'''基于中文Prompt的验证集'''\n",
"test_set_ideal = {\n",
" 'customer_msg': \"\"\"\\\n",
"告诉我有关 the smartx pro phone 和 the fotosnap camera, the dslr one 的信息。\\n另外你们这有什么 TVs \"\"\",\n",
" 'ideal_answer':\"\"\"\\\n",
"SmartX ProPhone是一款功能强大的智能手机具有6.1英寸的显示屏128GB的存储空间12MP双摄像头和5G网络。它的价格为899.99美元保修期为1年。\n",
"FotoSnap DSLR相机是一款功能强大的相机具有24.2MP传感器1080p视频3英寸LCD屏幕和可更换镜头。它的价格为599.99美元保修期为1年。\n",
"我们有多种电视可供选择包括CineView 4K电视CineView 8K电视和CineView OLED电视。CineView 4K电视具有55英寸的显示屏4K分辨率HDR和智能电视功能价格为599.99美元保修期为2年。CineView 8K电视具有65英寸的显示屏8K分辨率HDR和智能电视功能价格为2999.99美元保修期为2年。CineView OLED电视具有55英寸的显示屏4K分辨率HDR和智能电视功能价格为1499.99美元保修期为2年\n",
" \"\"\"\n",
"}"
]
},
{
"cell_type": "code",
"execution_count": 13,
"metadata": {},
"outputs": [],
"source": [
"def eval_vs_ideal(test_set, assistant_answer):\n",
"\n",
" cust_msg = test_set['customer_msg']\n",
" ideal = test_set['ideal_answer']\n",
" completion = assistant_answer\n",
" \n",
" system_message = \"\"\"\\\n",
" 您是一位助理,通过将客户服务代理的回答与理想(专家)回答进行比较,评估客户服务代理对用户问题的回答质量。\n",
" 请输出一个单独的字母A 、B、C、D、E不要包含其他内容。 \n",
" \"\"\"\n",
"\n",
" user_message = f\"\"\"\\\n",
" 您正在比较一个给定问题的提交答案和专家答案。数据如下:\n",
" [开始]\n",
" ************\n",
" [问题]: {cust_msg}\n",
" ************\n",
" [专家答案]: {ideal}\n",
" ************\n",
" [提交答案]: {completion}\n",
" ************\n",
" [结束]\n",
"\n",
" 比较提交答案的事实内容与专家答案。忽略样式、语法或标点符号上的差异。\n",
" 提交的答案可能是专家答案的子集、超集,或者与之冲突。确定适用的情况,并通过选择以下选项之一回答问题:\n",
" A提交的答案是专家答案的子集并且与之完全一致。\n",
" B提交的答案是专家答案的超集并且与之完全一致。\n",
" C提交的答案包含与专家答案完全相同的细节。\n",
" D提交的答案与专家答案存在分歧。\n",
" E答案存在差异但从事实的角度来看这些差异并不重要。\n",
" 选项ABCDE\n",
"\"\"\"\n",
"\n",
" messages = [\n",
" {'role': 'system', 'content': system_message},\n",
" {'role': 'user', 'content': user_message}\n",
" ]\n",
"\n",
" response = get_completion_from_messages(messages)\n",
" return response"
]
},
{
"cell_type": "code",
"execution_count": 14,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"关于SmartX ProPhone和FotoSnap DSLR相机的信息\n",
"\n",
"SmartX ProPhone是一款功能强大的智能手机具有6.1英寸的显示屏128GB的存储空间12MP双摄像头和5G网络。它的价格为899.99美元保修期为1年。\n",
"\n",
"FotoSnap DSLR相机是一款功能强大的相机具有24.2MP传感器1080p视频3英寸LCD屏幕和可更换镜头。它的价格为599.99美元保修期为1年。\n",
"\n",
"关于电视的信息:\n",
"\n",
"我们有多种电视可供选择包括CineView 4K电视CineView 8K电视和CineView OLED电视。CineView 4K电视具有55英寸的显示屏4K分辨率HDR和智能电视功能价格为599.99美元保修期为2年。CineView 8K电视具有65英寸的显示屏8K分辨率HDR和智能电视功能价格为2999.99美元保修期为2年。CineView OLED电视具有55英寸的显示屏4K分辨率HDR和智能电视功能价格为1499.99美元保修期为2年。您需要哪种类型的电视\n"
]
}
],
"source": [
"print(assistant_answer)"
]
},
{
"cell_type": "code",
"execution_count": 19,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"'B'"
]
},
"execution_count": 19,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"eval_vs_ideal(test_set_ideal, assistant_answer)"
]
},
{
"cell_type": "code",
"execution_count": 16,
"metadata": {},
"outputs": [],
"source": [
"assistant_answer_2 = \"life is like a box of chocolates\""
]
},
{
"cell_type": "code",
"execution_count": 17,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"'D'"
]
},
"execution_count": 17,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"eval_vs_ideal(test_set_ideal, assistant_answer_2)"
]
}
],
"metadata": {
"kernelspec": {
"display_name": "zyh_gpt",
"language": "python",
"name": "python3"
},
"language_info": {
"codemirror_mode": {
"name": "ipython",
"version": 3
},
"file_extension": ".py",
"mimetype": "text/x-python",
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.10.11"
},
"orig_nbformat": 4
},
"nbformat": 4,
"nbformat_minor": 2
}