>大语言模型(LLM)的基本原理非常简单:根据培训数据中的统计模式,以一系列单词来预测下一个单词(或令牌)。但是,当它可以执行许多惊人的任务,例如文本摘要,想法产生,集思广益,代码生成,信息处理和内容创建等令人惊叹的任务时,这种看似简单的功能就非常复杂。也就是说,LLM没有任何记忆,除了坚持其基本功能外,他们实际上“理解”了任何东西:预测下一个单词。
下一字预测的过程是概率的。 LLM必须从概率分布中选择每个单词。在此过程中,它们通常会产生错误,捏造或不一致的内容,以尝试产生连贯的响应并填补外观合理但不正确的信息的空白。这种现象称为幻觉,这是LLM的不可避免的,众所周知的特征,需要验证和证实其产出。 使LLM与外部知识源一起工作的检索增强生成(RAG)方法在某种程度上可以最大程度地减少幻觉,但不能完全消除它们。尽管高级破布可以提供文本引用和URL,但验证这些参考可能会忙碌而耗时。因此,我们需要一个客观的标准来评估LLM响应的可靠性或可信度,无论是根据自己的知识还是外部知识基础(RAG)产生的。 在本文中,我们将讨论如何通过值得信赖的语言模型来评估LLM的输出,该模型为LLM的输出分配分数。我们将首先讨论如何使用值得信赖的语言模型将分数分配给LLM的答案并解释可信度。随后,我们将与Llamaparse和Llamaindex一起开发一个例子抹布,以评估抹布的可信度答案。
本文的整个代码可在github的jupyter笔记本中找到。>
>将可信度分数分配给LLM的答案
>演示我们如何为LLM的响应分配一个可信度分数,我将使用清洁行的可信语言模型(TLM)。这样的TLM结合了不确定性量化
pip install --upgrade cleanlab-studio
>清洁列表支持几个专有模型,例如'gpt-4o','gpt-> gpt-4o-mini ',' o1-preview ',' claude-3-sonnet',''claude-3.5-sonnet ',, ‘claude-3.5-sonnet-v2'等。这是TLM将Trustworhiness分数分配给GPT-4O的答案的方式。值得信赖的评分范围从0到1,其中较高的值表示更高的可信度。
from cleanlab_studio import Studio studio = Studio("<CLEANLAB_API_KEY>") # Get your API key from above tlm = studio.TLM(options={"log": ["explanation"], "model": "gpt-4o"}) # GPT, Claude, etc #set the prompt out = tlm.prompt("How many vowels are there in the word 'Abracadabra'.?") #the TLM response contains the actual output 'response', trustworthiness score and explanation print(f"Model's response = {out['response']}") print(f"Trustworthiness score = {out['trustworthiness_score']}") print(f"Explanation = {out['log']['explanation']}")上面的代码测试了GPT-4O对“
Model's response = The word "Abracadabra" contains 6 vowels. The vowels are: A, a, a, a, a, and a. Trustworthiness score = 0.6842228802750124 Explanation = This response is untrustworthy due to a lack of consistency in possible responses from the model. Here's one inconsistent alternate response that the model considered (which may not be accurate either): 5.
> claude-3.5-sonnet-v2
产生正确的输出。让我们将两个模型的回答与另一个问题进行比较。Model's response = Let me count the vowels in 'Abracadabra': A-b-r-a-c-a-d-a-b-r-a The vowels are: A, a, a, a, a There are 5 vowels in the word 'Abracadabra'. Trustworthiness score = 0.9378276048845285 Explanation = Did not find a reason to doubt trustworthiness.>
这是两个模型的响应:
from cleanlab_studio import Studio import markdown from IPython.core.display import display, Markdown # Initialize the Cleanlab Studio with API key studio = Studio("<CLEANLAB_API_KEY>") # Replace with your actual API key # List of models to evaluate models = ["gpt-4o", "claude-3.5-sonnet-v2"] # Define the prompt prompt_text = "Which one of 9.11 and 9.9 is bigger?" # Loop through each model and evaluate for model in models: tlm = studio.TLM(options={"log": ["explanation"], "model": model}) out = tlm.prompt(prompt_text) md_content = f""" ## Model: {model} **Response:** {out['response']} **Trustworthiness Score:** {out['trustworthiness_score']} **Explanation:** {out['log']['explanation']} --- """ display(Markdown(md_content))llama-3.3–70b-Instruct Model
这是> deepSeek-r1-distill-lalama-70b型号的输出
开发一个值得信赖的抹布import streamlit as st from langchain_groq.chat_models import ChatGroq import os os.environ["GROQ_API_KEY"]=st.secrets["GROQ_API_KEY"] # Initialize the Groq Llama Instant model groq_llm = ChatGroq(model="deepseek-r1-distill-llama-70b", temperature=0.5) prompt = "Which one of 9.11 and 9.9 is bigger?" # Get the response from the model response = groq_llm.invoke(prompt) #Initialize Cleanlab's studio studio = Studio("226eeab91e944b23bd817a46dbe3c8ae") cleanlab_tlm = studio.TLM(options={"log": ["explanation"]}) #for explanations #Get the output containing trustworthiness score and explanation output = cleanlab_tlm.get_trustworthiness_score(prompt, response=response.content.strip()) md_content = f""" ## Model: {model} **Response:** {response.content.strip()} **Trustworthiness Score:** {output['trustworthiness_score']} **Explanation:** {output['log']['explanation']} --- """ display(Markdown(md_content))
>我们现在将开发一个抹布,以演示如何衡量抹布中LLM响应的可信度。将通过从给定链接中刮擦数据,以降价格式解析并创建向量存储。
需要为下一个代码安装以下库。
下一步将涉及使用python's
beautifuresoup库从给定URL刮除数据,使用
> pdfkitpip install llama-parse llama-index-core llama-index-embeddings-huggingface llama-index-llms-cleanlab requests beautifulsoup4 pdfkit nest-asyncio保存删除的数据,然后从pdf中解析数据( s)使用
> llamaparse降低文件,这是一个使用LLMS构建的Genai-native文档解析平台,用于LLM用例。
>我们将首先配置Cleanlabtlm和嵌入模型( huggingface 嵌入模型baai/bge-small-en-v1.5 )的LLM要计算刮擦数据的嵌入以创建向量存储。
pip install --upgrade cleanlab-studio>现在,我们将定义一个自定义事件处理程序,
from cleanlab_studio import Studio studio = Studio("<CLEANLAB_API_KEY>") # Get your API key from above tlm = studio.TLM(options={"log": ["explanation"], "model": "gpt-4o"}) # GPT, Claude, etc #set the prompt out = tlm.prompt("How many vowels are there in the word 'Abracadabra'.?") #the TLM response contains the actual output 'response', trustworthiness score and explanation print(f"Model's response = {out['response']}") print(f"Trustworthiness score = {out['trustworthiness_score']}") print(f"Explanation = {out['log']['explanation']}")Creative Commons Attribution-ShareAlike 4.0许可证
)。> >
note:建议读者始终仔细检查他们将要刮擦的内容/数据的状态,并确保允许他们这样做。
以下代码通过提出HTTP请求并使用> BeautifulSoup pdfkit。
从刮擦数据中生成pdf(s)后,我们使用> llamaparse
>现在,我们创建一个矢量商店和一个查询引擎。我们定义了一个客户提示模板,以指导LLM回答问题的行为。最后,我们使用创建索引创建一个查询引擎来回答查询。对于每个查询,我们根据查询的语义相似性从矢量存储中检索了前3个节点。 LLM使用这些检索的节点生成最终答案。
现在,让我们测试一些查询及其相应的可信度分数的抹布。
这就是所有人!如果您喜欢这篇文章,请在>中关注我,和> linkedIn 。Model's response = Let me count the vowels in 'Abracadabra':
A-b-r-a-c-a-d-a-b-r-a
The vowels are: A, a, a, a, a
There are 5 vowels in the word 'Abracadabra'.
Trustworthiness score = 0.9378276048845285
Explanation = Did not find a reason to doubt trustworthiness.
from cleanlab_studio import Studio
import markdown
from IPython.core.display import display, Markdown
# Initialize the Cleanlab Studio with API key
studio = Studio("<CLEANLAB_API_KEY>") # Replace with your actual API key
# List of models to evaluate
models = ["gpt-4o", "claude-3.5-sonnet-v2"]
# Define the prompt
prompt_text = "Which one of 9.11 and 9.9 is bigger?"
# Loop through each model and evaluate
for model in models:
tlm = studio.TLM(options={"log": ["explanation"], "model": model})
out = tlm.prompt(prompt_text)
md_content = f"""
## Model: {model}
**Response:** {out['response']}
**Trustworthiness Score:** {out['trustworthiness_score']}
**Explanation:** {out['log']['explanation']}
---
"""
display(Markdown(md_content))
>为LLM的响应分配一个可信度分数,无论是通过直接推理还是通过抹布生成,都有助于定义AI输出的可靠性,并在需要的情况下确定人类验证的优先级。对于错误或不可靠的响应可能会产生严重后果的关键领域,这一点尤其重要。
以上是如何衡量大语模型的响应的可靠性的详细内容。更多信息请关注PHP中文网其他相关文章!