使用 Prometheus 模型进行评估¶
评估是迭代优化 RAG(检索增强生成)流程的关键环节。该过程长期以来高度依赖 GPT-4,但近期出现了一个名为 Prometheus 的新型开源模型,可作为评估的替代方案。
本笔记本将演示如何结合 LlamaIndex 抽象层,利用 Prometheus 模型进行评估。
如果您对 Prometheus 模型不熟悉,可能会觉得 Andrei 准备的论文摘要很有帮助。需要注意的是,该模型要求提示中必须包含评分细则(rubric scores)才能进行有效评估。如需更详细信息,可查阅笔记本中列出的具体提示模板。

我们将使用 Prometheus 模型配合 Llama Datasets 中的两个数据集来演示正确性评估。如果您尚未了解 Llama Datasets,建议您花些时间阅读这篇介绍。
- Paul Graham 论文
- Llama2
注意:此处分析展示的是原始版 Prometheus 模型。您可以使用该模型的量化版本重新运行分析。¶
%pip install llama-index-llms-openai
%pip install llama-index-llms-huggingface-api
# attach to the same event-loop
import nest_asyncio
nest_asyncio.apply()
下载数据集¶
from llama_index.core.llama_dataset import download_llama_dataset
paul_graham_rag_dataset, paul_graham_documents = download_llama_dataset(
"PaulGrahamEssayDataset", "./data/paul_graham"
)
llama2_rag_dataset, llama2_documents = download_llama_dataset(
"Llama2PaperDataset", "./data/llama2"
)
定义托管在 HuggingFace 上的 Prometheus LLM 模型¶
我们使用英伟达 A10G GPU 在 HF 推理端点上部署了该模型。
from llama_index.llms.huggingface_api import HuggingFaceInferenceAPI
HF_TOKEN = "YOUR HF TOKEN"
HF_ENDPOINT_URL = (
"https://q3yljc2cypyrvw3i.us-east-1.aws.endpoints.huggingface.cloud"
)
prometheus_llm = HuggingFaceInferenceAPI(
model_name=HF_ENDPOINT_URL,
token=HF_TOKEN,
temperature=0.1,
do_sample=True,
top_p=0.95,
top_k=40,
repetition_penalty=1.1,
)
/opt/homebrew/lib/python3.11/site-packages/tqdm/auto.py:21: TqdmWarning: IProgress not found. Please update jupyter and ipywidgets. See https://ipywidgets.readthedocs.io/en/stable/user_install.html from .autonotebook import tqdm as notebook_tqdm
提示词模板¶
为确保性能对比的一致性,我们将为普罗米修斯模型和GPT-4使用相同的提示模板。
正确性评估提示¶
prometheus_correctness_eval_prompt_template = """###Task Description: An instruction (might include an Input inside it), a query, a response to evaluate, a reference answer that gets a score of 5, and a score rubric representing a evaluation criteria are given.
1. Write a detailed feedback that assesses the quality of the response strictly based on the given score rubric, not evaluating in general.
2. After writing a feedback, write a score that is either 1 or 2 or 3 or 4 or 5. You should refer to the score rubric.
3. The output format should look as follows: "Feedback: (write a feedback for criteria) [RESULT] (1 or 2 or 3 or 4 or 5)"
4. Please do not generate any other opening, closing, and explanations.
5. Only evaluate on common things between generated answer and reference answer. Don't evaluate on things which are present in reference answer but not in generated answer.
###The instruction to evaluate: Your task is to evaluate the generated answer and reference answer for the query: {query}
###Generate answer to evaluate: {generated_answer}
###Reference Answer (Score 5): {reference_answer}
###Score Rubrics:
Score 1: If the generated answer is not relevant to the user query and reference answer.
Score 2: If the generated answer is according to reference answer but not relevant to user query.
Score 3: If the generated answer is relevant to the user query and reference answer but contains mistakes.
Score 4: If the generated answer is relevant to the user query and has the exact same metrics as the reference answer, but it is not as concise.
Score 5: If the generated answer is relevant to the user query and fully correct according to the reference answer.
###Feedback:"""
prometheus_correctness_eval_prompt_template = """###Task Description: An instruction (might include an Input inside it), a query, a response to evaluate, a reference answer that gets a score of 5, and a score rubric representing a evaluation criteria are given.
1. Write a detailed feedback that assesses the quality of the response strictly based on the given score rubric, not evaluating in general.
2. After writing a feedback, write a score that is either 1 or 2 or 3 or 4 or 5. You should refer to the score rubric.
3. The output format should look as follows: "Feedback: (write a feedback for criteria) [RESULT] (1 or 2 or 3 or 4 or 5)"
4. Please do not generate any other opening, closing, and explanations.
5. Only evaluate on common things between generated answer and reference answer. Don't evaluate on things which are present in reference answer but not in generated answer.
###The instruction to evaluate: Your task is to evaluate the generated answer and reference answer for the query: {query}
###Generate answer to evaluate: {generated_answer}
###Reference Answer (Score 5): {reference_answer}
###Score Rubrics:
Score 1: If the generated answer is not relevant to the user query and reference answer.
Score 2: If the generated answer is correct according to reference answer but not relevant to user query.
Score 3: If the generated answer is relevant to the user query and correct according to reference answer but has some mistakes in facts.
Score 4: If the generated answer is relevant to the user query and has the exact same metrics and correct as the reference answer, but it is not as concise.
Score 5: If the generated answer is relevant to the user query and fully correct according to the reference answer.
###Feedback:"""
忠实度评估提示¶
prometheus_faithfulness_eval_prompt_template = """###Task Description: An instruction (might include an Input inside it), an information, a context, and a score rubric representing evaluation criteria are given.
1. You are provided with evaluation task with the help of information, context information to give result based on score rubrics.
2. Write a detailed feedback based on evaluation task and the given score rubric, not evaluating in general.
3. After writing a feedback, write a score that is YES or NO. You should refer to the score rubric.
4. The output format should look as follows: "Feedback: (write a feedback for criteria) [RESULT] (YES or NO)”
5. Please do not generate any other opening, closing, and explanations.
###The instruction to evaluate: Your task is to evaluate if the given piece of information is supported by context.
###Information: {query_str}
###Context: {context_str}
###Score Rubrics:
Score YES: If the given piece of information is supported by context.
Score NO: If the given piece of information is not supported by context
###Feedback: """
prometheus_faithfulness_refine_prompt_template = """###Task Description: An instruction (might include an Input inside it), a information, a context information, an existing answer, and a score rubric representing a evaluation criteria are given.
1. You are provided with evaluation task with the help of information, context information and an existing answer.
2. Write a detailed feedback based on evaluation task and the given score rubric, not evaluating in general.
3. After writing a feedback, write a score that is YES or NO. You should refer to the score rubric.
4. The output format should look as follows: "Feedback: (write a feedback for criteria) [RESULT] (YES or NO)"
5. Please do not generate any other opening, closing, and explanations.
###The instruction to evaluate: If the information is present in the context and also provided with an existing answer.
###Existing answer: {existing_answer}
###Information: {query_str}
###Context: {context_msg}
###Score Rubrics:
Score YES: If the existing answer is already YES or If the Information is present in the context.
Score NO: If the existing answer is NO and If the Information is not present in the context.
###Feedback: """
相关性评估提示¶
prometheus_relevancy_eval_prompt_template = """###Task Description: An instruction (might include an Input inside it), a query with response, context, and a score rubric representing evaluation criteria are given.
1. You are provided with evaluation task with the help of a query with response and context.
2. Write a detailed feedback based on evaluation task and the given score rubric, not evaluating in general.
3. After writing a feedback, write a score that is YES or NO. You should refer to the score rubric.
4. The output format should look as follows: "Feedback: (write a feedback for criteria) [RESULT] (YES or NO)”
5. Please do not generate any other opening, closing, and explanations.
###The instruction to evaluate: Your task is to evaluate if the response for the query is in line with the context information provided.
###Query and Response: {query_str}
###Context: {context_str}
###Score Rubrics:
Score YES: If the response for the query is in line with the context information provided.
Score NO: If the response for the query is not in line with the context information provided.
###Feedback: """
prometheus_relevancy_refine_prompt_template = """###Task Description: An instruction (might include an Input inside it), a query with response, context, an existing answer, and a score rubric representing a evaluation criteria are given.
1. You are provided with evaluation task with the help of a query with response and context and an existing answer.
2. Write a detailed feedback based on evaluation task and the given score rubric, not evaluating in general.
3. After writing a feedback, write a score that is YES or NO. You should refer to the score rubric.
4. The output format should look as follows: "Feedback: (write a feedback for criteria) [RESULT] (YES or NO)"
5. Please do not generate any other opening, closing, and explanations.
###The instruction to evaluate: Your task is to evaluate if the response for the query is in line with the context information provided.
###Query and Response: {query_str}
###Context: {context_str}
###Score Rubrics:
Score YES: If the existing answer is already YES or If the response for the query is in line with the context information provided.
Score NO: If the existing answer is NO and If the response for the query is in line with the context information provided.
###Feedback: """
设置用于索引的 OpenAI 密钥
import os
os.environ["OPENAI_API_KEY"] = "YOUR OPENAI API KEY"
from llama_index.llms.openai import OpenAI
gpt4_llm = OpenAI("gpt-4")
定义解析器函数¶
该函数将用于正确性评估器。
from typing import Tuple
import re
def parser_function(output_str: str) -> Tuple[float, str]:
# Pattern to match the feedback and response
# This pattern looks for any text ending with '[RESULT]' followed by a number
pattern = r"(.+?) \[RESULT\] (\d)"
# Using regex to find all matches
matches = re.findall(pattern, output_str)
# Check if any match is found
if matches:
# Assuming there's only one match in the text, extract feedback and response
feedback, score = matches[0]
score = float(score.strip()) if score is not None else score
return score, feedback.strip()
else:
return None, None
定义正确性、忠实度、相关性评估指标¶
from llama_index.core.evaluation import (
CorrectnessEvaluator,
FaithfulnessEvaluator,
RelevancyEvaluator,
)
from llama_index.core.callbacks import CallbackManager, TokenCountingHandler
import tiktoken
# CorrectnessEvaluator with Prometheus model
prometheus_correctness_evaluator = CorrectnessEvaluator(
llm=prometheus_llm,
parser_function=parser_function,
eval_template=prometheus_correctness_eval_prompt_template,
)
# FaithfulnessEvaluator with Prometheus model
prometheus_faithfulness_evaluator = FaithfulnessEvaluator(
llm=prometheus_llm,
eval_template=prometheus_faithfulness_eval_prompt_template,
refine_template=prometheus_faithfulness_refine_prompt_template,
)
# RelevancyEvaluator with Prometheus model
prometheus_relevancy_evaluator = RelevancyEvaluator(
llm=prometheus_llm,
eval_template=prometheus_relevancy_eval_prompt_template,
refine_template=prometheus_relevancy_refine_prompt_template,
)
# Set the encoding model to `gpt-4` for token counting.
token_counter = TokenCountingHandler(
tokenizer=tiktoken.encoding_for_model("gpt-4").encode
)
callback_manager = CallbackManager([token_counter])
gpt4_llm.callback_manager = callback_manager
# CorrectnessEvaluator with GPT-4 model
gpt4_correctness_evaluator = CorrectnessEvaluator(
llm=gpt4_llm,
# parser_function=parser_function,
)
# FaithfulnessEvaluator with GPT-4 model
gpt4_faithfulness_evaluator = FaithfulnessEvaluator(
llm=gpt4_llm,
eval_template=prometheus_faithfulness_eval_prompt_template,
refine_template=prometheus_faithfulness_refine_prompt_template,
)
# RelevancyEvaluator with GPT-4 model
gpt4_relevancy_evaluator = RelevancyEvaluator(
llm=gpt4_llm,
eval_template=prometheus_relevancy_eval_prompt_template,
refine_template=prometheus_relevancy_refine_prompt_template,
)
# create a dictionary of evaluators
prometheus_evaluators = {
"correctness": prometheus_correctness_evaluator,
"faithfulness": prometheus_faithfulness_evaluator,
"relevancy": prometheus_relevancy_evaluator,
}
gpt4_evaluators = {
"correctness": gpt4_correctness_evaluator,
"faithfulness": gpt4_faithfulness_evaluator,
"relevancy": gpt4_relevancy_evaluator,
}
为不同数据集创建 query_engine 和 rag_dataset 的函数实现¶
from llama_index.core.llama_dataset import LabelledRagDataset
from llama_index.core import SimpleDirectoryReader, VectorStoreIndex
def create_query_engine_rag_dataset(dataset_path):
rag_dataset = LabelledRagDataset.from_json(
f"{dataset_path}/rag_dataset.json"
)
documents = SimpleDirectoryReader(
input_dir=f"{dataset_path}/source_files"
).load_data()
index = VectorStoreIndex.from_documents(documents=documents)
query_engine = index.as_query_engine()
return query_engine, rag_dataset
对已定义评估器执行批量评估的函数¶
from llama_index.core.evaluation import BatchEvalRunner
async def batch_eval_runner(
evaluators, query_engine, questions, reference=None, num_workers=8
):
batch_runner = BatchEvalRunner(
evaluators, workers=num_workers, show_progress=True
)
eval_results = await batch_runner.aevaluate_queries(
query_engine, queries=questions, reference=reference
)
return eval_results
检查分数分布的函数¶
from collections import Counter
from typing import List, Dict
def get_scores_distribution(scores: List[float]) -> Dict[str, float]:
# Counting the occurrences of each score
score_counts = Counter(scores)
# Total number of scores
total_scores = len(scores)
# Calculating the percentage distribution
percentage_distribution = {
score: (count / total_scores) * 100
for score, count in score_counts.items()
}
return percentage_distribution
检查正确性、忠实度与相关性评估得分的函数¶
def get_eval_results(key, eval_results):
results = eval_results[key]
correct = 0
for result in results:
if result.passing:
correct += 1
score = correct / len(results)
print(f"{key} Score: {round(score, 2)}")
return score
计算 汉明距离 的函数¶
def hamming_distance(list1, list2):
if len(list1) != len(list2):
raise ValueError("Lists must be of the same length")
return sum(el1 != el2 for el1, el2 in zip(list1, list2))
保罗·格雷厄姆文章文本评估¶
query_engine, rag_dataset = create_query_engine_rag_dataset(
"./data/paul_graham"
)
# Get questions for evaluation
questions = [example.query for example in rag_dataset.examples]
# Get reference answers for evaluation
reference = [[example.reference_answer] for example in rag_dataset.examples]
计算正确性、忠实性与相关性评估¶
prometheus_eval_results = await batch_eval_runner(
prometheus_evaluators, query_engine, questions, reference
)
100%|██████████| 44/44 [00:30<00:00, 1.43it/s] 100%|██████████| 132/132 [01:56<00:00, 1.13it/s]
gpt4_eval_results = await batch_eval_runner(
gpt4_evaluators, query_engine, questions, reference
)
100%|██████████| 44/44 [00:26<00:00, 1.66it/s] 100%|██████████| 132/132 [02:32<00:00, 1.16s/it]
使用 Prometheus 评估器进行正确性评分的分布¶
prometheus_scores = [
result.score for result in prometheus_eval_results["correctness"]
]
get_scores_distribution(prometheus_scores)
{3.0: 50.0,
1.0: 43.18181818181818,
5.0: 2.272727272727273,
4.0: 4.545454545454546}
使用 GPT-4 评估器得出的正确性评分分布¶
gpt4_scores = [result.score for result in gpt4_eval_results["correctness"]]
get_scores_distribution(gpt4_scores)
{4.5: 50.0,
5.0: 34.090909090909086,
2.5: 9.090909090909092,
4.0: 2.272727272727273,
3.5: 4.545454545454546}
Prometheus 与 GPT-4 的反馈对比¶
query = prometheus_eval_results["correctness"][0].query
response = prometheus_eval_results["correctness"][0].response
reference_answer = reference[0][0]
# prometheus feedback and score
prometheus_feedback = prometheus_eval_results["correctness"][0].feedback
prometheus_score = prometheus_eval_results["correctness"][0].score
# GPT4 feedback and score
gpt4_feedback = gpt4_eval_results["correctness"][0].feedback
gpt4_score = gpt4_eval_results["correctness"][0].score
print(f"Query: {query} \n\n")
print(f"Generated Answer: {response} \n\n")
print(f"Reference Answer: {reference_answer} \n\n")
print(
f"Prometheus Feedback: {prometheus_feedback} \n\n {prometheus_score} \n\n"
)
print(f"GPT-4 Feedback: {gpt4_feedback} \n\n {gpt4_score}")
Query: In the essay, the author mentions his early experiences with programming. Describe the first computer he used for programming, the language he used, and the challenges he faced. Generated Answer: The author mentions that the first computer he used for programming was the IBM 1401, which was located in the basement of his junior high school. He used an early version of Fortran as the programming language. The author faced challenges in figuring out what to do with the computer, as the only form of input was data stored on punched cards, and he didn't have any. Additionally, he didn't know enough math to do anything interesting with the computer. Reference Answer: The first computer the author used for programming was the IBM 1401, which was used by his school district for data processing. He started using it in 9th grade, around the age of 13 or 14. The programming language he used was an early version of Fortran. The author faced several challenges while using this computer. The only form of input to programs was data stored on punched cards, and he didn't have any data stored on punched cards. The only other option was to do things that didn't rely on any input, like calculate approximations of pi, but he didn't know enough math to do anything interesting of that type. Therefore, he couldn't figure out what to do with it and in retrospect, he believes there's not much he could have done with it. Prometheus Feedback: The generated response is relevant to the user query and correctly describes the first computer the author used for programming, the programming language he used, and the challenges he faced. However, it has some inaccuracies in the details. The author did not use the IBM 1401 in the basement of his junior high school, but rather in 9th grade, around the age of 13 or 14. The author did not have any data stored on punched cards, but the only form of input was data stored on punched cards. The author did not know enough math to do anything interesting with the computer, but he didn't know enough math to do anything interesting of that type. So the overall score is 3. 3.0 GPT-4 Feedback: The generated answer is highly relevant and almost completely accurate. It correctly identifies the first computer the author used (IBM 1401), the programming language (Fortran), and the challenges he faced (lack of input data and insufficient math knowledge). However, it omits the detail about the author's age and grade level when he started programming, which was included in the reference answer. 4.5
观察结果:¶
Prometheus 提供的反馈更为详细,指出生成响应中遗漏了某些具体细节,因此给出了 3.0 的评分。相比之下,GPT-4 的反馈则更为宽泛且缺乏针对性,尽管同样缺失部分细节,却给出了 5.0 的评分。
Prometheus 忠实性与相关性评估分数¶
_ = get_eval_results("faithfulness", prometheus_eval_results)
_ = get_eval_results("relevancy", prometheus_eval_results)
faithfulness Score: 0.75 relevancy Score: 0.86
GPT-4 忠实度与相关性评估分数¶
_ = get_eval_results("faithfulness", gpt4_eval_results)
_ = get_eval_results("relevancy", gpt4_eval_results)
faithfulness Score: 0.98 relevancy Score: 0.95
Prometheus 与 GPT-4 的汉明距离对比¶
(数值越低越好)
prometheus_faithfulness_scores = [
result.score for result in prometheus_eval_results["faithfulness"]
]
prometheus_relevancy_scores = [
result.score for result in prometheus_eval_results["relevancy"]
]
gpt4_faithfulness_scores = [
result.score for result in gpt4_eval_results["faithfulness"]
]
gpt4_relevancy_scores = [
result.score for result in gpt4_eval_results["relevancy"]
]
faithfulness_hamming_distance = hamming_distance(
prometheus_faithfulness_scores, gpt4_faithfulness_scores
)
relevancy_hamming_distance = hamming_distance(
prometheus_relevancy_scores, gpt4_relevancy_scores
)
print(f"Faithfulness Hamming Distance: {faithfulness_hamming_distance}")
print(f"Relevancy Hamming Distance: {relevancy_hamming_distance}")
Faithfulness Hamming Distance: 10 Relevancy Hamming Distance: 8
观察结果:¶
对比分析显示,在Faithfulness(忠实度)和Relevancy(相关性)评估维度上,Prometheus模型与GPT-4模型的评分重合度分别达到约77%和81%。这表明两个模型在忠实度和相关性评分方面存在较好的相关性。
GPT-4 成本分析¶
prompt_token_count = token_counter.prompt_llm_token_count
completion_token_count = token_counter.completion_llm_token_count
total_cost_paul_graham_essay = (
prompt_token_count * 0.03 + completion_token_count * 0.06
) / 1000
token_counter.reset_counts()
基于Llama2论文的评估¶
query_engine, rag_dataset = create_query_engine_rag_dataset("./data/llama2")
questions = [example.query for example in rag_dataset.examples]
reference = [[example.reference_answer] for example in rag_dataset.examples]
计算正确性、忠实性与相关性评估¶
prometheus_eval_results = await batch_eval_runner(
prometheus_evaluators, query_engine, questions, reference
)
100%|██████████| 100/100 [01:02<00:00, 1.61it/s] 100%|██████████| 300/300 [04:34<00:00, 1.09it/s]
gpt4_eval_results = await batch_eval_runner(
gpt4_evaluators, query_engine, questions, reference
)
100%|██████████| 100/100 [01:06<00:00, 1.51it/s] 100%|██████████| 300/300 [06:22<00:00, 1.27s/it]
使用 Prometheus 评估器进行正确性评分的分布¶
prometheus_scores = [
result.score for result in prometheus_eval_results["correctness"]
]
get_scores_distribution(prometheus_scores)
{3.0: 56.00000000000001, 1.0: 26.0, 5.0: 9.0, 4.0: 8.0, 2.0: 1.0}
使用 GPT-4 评估器得出的正确性评估分数分布¶
gpt4_scores = [result.score for result in gpt4_eval_results["correctness"]]
get_scores_distribution(gpt4_scores)
{4.5: 57.99999999999999,
1.0: 6.0,
4.0: 12.0,
5.0: 10.0,
2.0: 5.0,
3.5: 5.0,
2.5: 3.0,
3.0: 1.0}
Prometheus 与 GPT-4 在准确性上的反馈对比¶
query = prometheus_eval_results["correctness"][0].query
response = prometheus_eval_results["correctness"][0].response
reference_answer = reference[0][0]
# prometheus feedback and score
prometheus_feedback = prometheus_eval_results["correctness"][0].feedback
prometheus_score = prometheus_eval_results["correctness"][0].score
# GPT4 feedback and score
gpt4_feedback = gpt4_eval_results["correctness"][0].feedback
gpt4_score = gpt4_eval_results["correctness"][0].score
print(f"Query: {query} \n\n")
print(f"Generated Answer: {response} \n\n")
print(f"Reference Answer: {reference_answer} \n\n")
print(
f"Prometheus Feedback: {prometheus_feedback} \n\n {prometheus_score} \n\n"
)
print(f"GPT-4 Feedback: {gpt4_feedback} \n\n {gpt4_score}")
Query: Based on the abstract of "Llama 2: Open Foundation and Fine-Tuned Chat Models," what are the two primary objectives achieved in this work, and what is the range of parameters for the large language models developed? Generated Answer: The two primary objectives achieved in this work are the development and release of Llama 2, a collection of pretrained and fine-tuned large language models (LLMs), and the optimization of these models for dialogue use cases. The range of parameters for the large language models developed is from 7 billion to 70 billion. Reference Answer: The two primary objectives achieved in the work described in the abstract of "Llama 2: Open Foundation and Fine-Tuned Chat Models" are: 1. The development and release of a collection of pretrained and fine-tuned large language models (LLMs) specifically optimized for dialogue use cases. 2. The demonstration that these fine-tuned LLMs, referred to as Llama 2-Chat, outperform open-source chat models on most benchmarks tested and may be a suitable substitute for closed-source models, particularly in terms of helpfulness and safety based on human evaluations. The range of parameters for the large language models developed in this work is from 7 billion to 70 billion parameters. Prometheus Feedback: The generated response is relevant to the user query and correctly identifies the two primary objectives of the work described in the abstract of "Llama 2: Open Foundation and Fine-Tuned Chat Models." However, it does not mention the demonstration of the fine-tuned LLMs outperforming open-source chat models on most benchmarks tested, which is a key point in the reference response. The range of parameters for the large language models developed is correctly identified, but the response does not mention the specific models referred to as Llama 2-Chat. So the overall score is 3. 3.0 GPT-4 Feedback: The generated answer is relevant and almost fully correct. It correctly identifies the two primary objectives and the range of parameters for the large language models. However, it misses the detail about Llama 2-Chat outperforming other models on most benchmarks and potentially being a suitable substitute for closed-source models. 4.5
观察结果:¶
与 GPT-4 相比,Prometheus 的反馈稍显精确,它会进行扣分并给出 3.0 的评分,而 GPT-4 则给出 4.5 分。
Prometheus 忠实性与相关性评估分数¶
_ = get_eval_results("faithfulness", prometheus_eval_results)
_ = get_eval_results("relevancy", prometheus_eval_results)
faithfulness Score: 0.39 relevancy Score: 0.57
GPT-4 忠实度与相关性评估分数¶
_ = get_eval_results("faithfulness", gpt4_eval_results)
_ = get_eval_results("relevancy", gpt4_eval_results)
faithfulness Score: 0.93 relevancy Score: 0.98
Prometheus 与 GPT-4 之间的汉明距离对比¶
prometheus_faithfulness_scores = [
result.score for result in prometheus_eval_results["faithfulness"]
]
prometheus_relevancy_scores = [
result.score for result in prometheus_eval_results["relevancy"]
]
gpt4_faithfulness_scores = [
result.score for result in gpt4_eval_results["faithfulness"]
]
gpt4_relevancy_scores = [
result.score for result in gpt4_eval_results["relevancy"]
]
faithfulness_hamming_distance = hamming_distance(
prometheus_faithfulness_scores, gpt4_faithfulness_scores
)
relevancy_hamming_distance = hamming_distance(
prometheus_relevancy_scores, gpt4_relevancy_scores
)
print(f"Faithfulness Hamming Distance: {faithfulness_hamming_distance}")
print(f"Relevancy Hamming Distance: {relevancy_hamming_distance}")
Faithfulness Hamming Distance: 58 Relevancy Hamming Distance: 41
观察结果:¶
对比显示,在忠实性(Faithfulness)评分方面,Prometheus与GPT-4评估结果有约44%的一致性;在相关性(Relevancy)评分方面,这一比例达到63%。这表明Prometheus模型与GPT-4模型在忠实性和相关性评分维度上存在相当程度的相关性。
Prometheus 与 GPT-4 在忠实性与相关性方面的反馈对比¶
# Get the query
query = questions[0]
# Get the response/ generated answer for the query
response = prometheus_eval_results["faithfulness"][0].response
# Get the retrieved contexts as they are used for faithfulness and relevancy
contexts = prometheus_eval_results["faithfulness"][0].contexts
# Get the faithfulness and relevancy feedbacks from prometheus model
prometheus_faithfulness_feedback = prometheus_eval_results["faithfulness"][
0
].feedback
prometheus_relevancy_feedback = prometheus_eval_results["relevancy"][
0
].feedback
# Get the faithfulness and relevancy feedbacks from gpt4 model
gpt4_faithfulness_feedback = gpt4_eval_results["faithfulness"][0].feedback
gpt4_relevancy_feedback = gpt4_eval_results["relevancy"][0].feedback
# Get the failthfulness and relevancy scores from prometheus model
prometheus_faithfulness_score = prometheus_eval_results["faithfulness"][
0
].score
prometheus_relevancy_score = prometheus_eval_results["relevancy"][0].score
# Get the faithfulness and relevancy scores from gpt4 model
gpt4_faithfulness_score = gpt4_eval_results["faithfulness"][0].score
gpt4_relevancy_score = gpt4_eval_results["relevancy"][0].score
print(f"Query: {query} \n\n")
print(f"Generated Answer: {response}")
Query: Based on the abstract of "Llama 2: Open Foundation and Fine-Tuned Chat Models," what are the two primary objectives achieved in this work, and what is the range of parameters for the large language models developed? Generated Answer: The two primary objectives achieved in this work are the development and release of Llama 2, a collection of pretrained and fine-tuned large language models (LLMs), and the optimization of these models for dialogue use cases. The range of parameters for the large language models developed is from 7 billion to 70 billion.
print(f"Context-1: {contexts[0]}")
Context-1: Llama 2 : Open Foundation and Fine-Tuned Chat Models
Hugo Touvron∗Louis Martin†Kevin Stone†
Peter Albert Amjad Almahairi Yasmine Babaei Nikolay Bashlykov Soumya Batra
Prajjwal Bhargava Shruti Bhosale Dan Bikel Lukas Blecher Cristian Canton Ferrer Moya Chen
Guillem Cucurull David Esiobu Jude Fernandes Jeremy Fu Wenyin Fu Brian Fuller
Cynthia Gao Vedanuj Goswami Naman Goyal Anthony Hartshorn Saghar Hosseini Rui Hou
Hakan Inan Marcin Kardas Viktor Kerkez Madian Khabsa Isabel Kloumann Artem Korenev
Punit Singh Koura Marie-Anne Lachaux Thibaut Lavril Jenya Lee Diana Liskovich
Yinghai Lu Yuning Mao Xavier Martinet Todor Mihaylov Pushkar Mishra
Igor Molybog Yixin Nie Andrew Poulton Jeremy Reizenstein Rashi Rungta Kalyan Saladi
Alan Schelten Ruan Silva Eric Michael Smith Ranjan Subramanian Xiaoqing Ellen Tan Binh Tang
Ross Taylor Adina Williams Jian Xiang Kuan Puxin Xu Zheng Yan Iliyan Zarov Yuchen Zhang
Angela Fan Melanie Kambadur Sharan Narang Aurelien Rodriguez Robert Stojnic
Sergey Edunov Thomas Scialom∗
GenAI, Meta
Abstract
In this work, we develop and release Llama 2, a collection of pretrained and fine-tuned
large language models (LLMs) ranging in scale from 7 billion to 70 billion parameters.
Our fine-tuned LLMs, called Llama 2-Chat , are optimized for dialogue use cases. Our
models outperform open-source chat models on most benchmarks we tested, and based on
ourhumanevaluationsforhelpfulnessandsafety,maybeasuitablesubstituteforclosed-
source models. We provide a detailed description of our approach to fine-tuning and safety
improvements of Llama 2-Chat in order to enable the community to build on our work and
contribute to the responsible development of LLMs.
∗Equal contribution, corresponding authors: {tscialom, htouvron}@meta.com
†Second author
Contributions for all the authors can be found in Section A.1.arXiv:2307.09288v2 [cs.CL] 19 Jul 2023
print(f"Context-2: {contexts[1]}")
Context-2: (2021)alsoilluminatesthedifficultiestiedtochatbot-oriented LLMs, with concerns ranging from privacy to misleading expertise claims. Deng et al. (2023) proposes a taxonomic framework to tackle these issues, and Bergman et al. (2022) delves into the balance between potential positive and negative impacts from releasing dialogue models. InvestigationsintoredteamingrevealspecificchallengesintunedLLMs,withstudiesbyGangulietal.(2022) and Zhuoet al. (2023) showcasing a variety ofsuccessful attack typesand their effects onthe generation of harmful content. National security agencies and various researchers, such as (Mialon et al., 2023), have also raisedredflagsaroundadvancedemergentmodelbehaviors,cyberthreats,andpotentialmisuseinareaslike biological warfare. Lastly, broader societal issues like job displacement due to accelerated AI research and an over-reliance on LLMs leading to training data degradation are also pertinent considerations (Acemoglu andRestrepo,2018;AutorandSalomons,2018;Webb,2019;Shumailovetal.,2023). Wearecommittedto continuing our work engaging with the broader policy, academic, and industry community on these issues. 7 Conclusion Inthisstudy,wehaveintroduced Llama 2,anewfamilyofpretrainedandfine-tunedmodelswithscales of7billionto70billionparameters. Thesemodelshavedemonstratedtheircompetitivenesswithexisting open-source chat models, as well as competency that is equivalent to some proprietary models on evaluation setsweexamined,althoughtheystilllagbehindothermodelslikeGPT-4. Wemeticulouslyelaboratedonthe methodsandtechniquesappliedinachievingourmodels,withaheavyemphasisontheiralignmentwiththe principlesofhelpfulnessandsafety. Tocontributemoresignificantlytosocietyandfosterthepaceofresearch, wehaveresponsiblyopenedaccessto Llama 2 andLlama 2-Chat . Aspartofourongoingcommitmentto transparency and safety, we plan to make further improvements to Llama 2-Chat in future work. 36
print(
f"Prometheus Faithfulness Feedback: {prometheus_faithfulness_feedback}\n\n"
)
print(f"Prometheus Faithfulness Score: {prometheus_faithfulness_score}\n\n")
print(f"Prometheus Relevancy Feedback: {prometheus_relevancy_feedback}\n\n")
print(f"Prometheus Relevancy Score: {prometheus_relevancy_score}")
Prometheus Faithfulness Feedback:
The information provided in the context is not supported by the given information. The context is about the development and release of Llama 2, a collection of pretrained and fine-tuned large language models (LLMs), and the optimization of these models for dialogue use cases. However, the information provided in the context does not align with the given information. The context does not mention the range of parameters for the large language models developed, which is the primary objective mentioned in the information. The context only talks about the development and release of Llama 2 and its optimization for dialogue use cases, but it does not provide any information about the range of parameters for the large language models developed. So the overall score is NO. [RESULT] NO
Prometheus Faithfulness Score: 0.0
Prometheus Relevancy Feedback:
The response is not in line with the context information provided. The query asked for the two primary objectives achieved in the work and the range of parameters for the large language models developed. However, the response provided the abstract of the paper and mentioned the authors, which is not relevant to the query. The response also did not mention the two primary objectives achieved in the work or the range of parameters for the large language models developed. So the overall score is NO. [RESULT] NO
Prometheus Relevancy Score: 0.0
若将反馈与上下文对比,会发现上下文和响应中都提及了参数范围,但反馈却称模型未能找到此类信息。¶
print(f"GPT-4 Faithfulness Feedback: {gpt4_faithfulness_feedback}\n\n")
print(f"GPT-4 Faithfulness Score: {gpt4_faithfulness_score}\n\n")
print(f"GPT-4 Relevancy Feedback: {gpt4_relevancy_feedback}\n\n")
print(f"GPT-4 Relevancy Score: {gpt4_relevancy_score}")
GPT-4 Faithfulness Feedback: The given piece of information is well supported by the context. The context clearly states that Llama 2, a collection of pretrained and fine-tuned large language models (LLMs), was developed and released. It also mentions that these models range in scale from 7 billion to 70 billion parameters. Furthermore, the context confirms that these models are optimized for dialogue use cases. Therefore, the information provided is accurate and is corroborated by the context. [RESULT] YES GPT-4 Faithfulness Score: 1.0 GPT-4 Relevancy Feedback: The response accurately reflects the context provided. The response correctly identifies the two primary objectives of the work as the development and release of Llama 2, a collection of pretrained and fine-tuned large language models (LLMs), and the optimization of these models for dialogue use cases. This is in line with the information provided in the abstract of the context. The response also correctly states the range of parameters for the large language models developed as being from 7 billion to 70 billion, which is also confirmed in the context. Therefore, the response is in line with the context information provided. [RESULT] YES GPT-4 Relevancy Score: 1.0
GPT-4 能正确评估,与 prometheus 模型不同。¶
GPT-4 成本分析¶
prompt_token_count = token_counter.prompt_llm_token_count
completion_token_count = token_counter.completion_llm_token_count
total_cost_llama2 = (
prompt_token_count * 0.03 + completion_token_count * 0.06
) / 1000
总成本分析¶
Prometheus 模型 - 144 次查询费用为 $2.167(其中 44 次用于 Paul Graham 文章分析,100 次用于 Llama2 论文解析),折合单次查询成本 $0.015。¶
GPT4 模型 - $22(总成本_paul_graham_文章 + 总成本_llama2) - 折合每次查询 $0.15¶
观察结果:¶
- 评估成本(约计):Prometheus 模型为
$2.167,GPT4 为$22。 - Prometheus 模型虽然比 GPT-4 提供更详细的反馈,但偶尔会给出错误反馈,需谨慎应用。
- 若生成答案缺少参考答案中的某些事实,Prometheus 模型会比 GPT-4 施加更严格的分数惩罚。
- 与 GPT-4 相比,Prometheus 在忠实性和相关性反馈中表现出更多幻觉/错误解读。
- Prometheus 与 GPT-4 在忠实性和相关性分数上的共性因数据集不同而存在差异,因此在生产环境中需谨慎使用。
注:HF 上的终端运行于 AWS Nvidia A100G · 1x GPU · 80 GB 环境,成本为 $6.5/小时。本文分析使用 Prometheus 模型。我们也对 GPTQ 量化版本 的 Prometheus 模型 进行了类似分析,发现其反馈中的幻觉现象较原始未量化模型略多。感谢论文作者和 Tom Jobbins 提供模型的量化版本。