使用大型语言模型 (LLM) 时,发现幻觉可能很棘手。我们可以使用困惑度、蕴涵和离散语义熵来更好地识别潜在的幻觉,而不是仅仅依靠法学硕士作为判断(这仍然可能会出错,并且许多评估框架仅使用它来检测幻觉)。虽然我在这里使用法学硕士来检测蕴涵,但这不是必要的。也就是说,这种方法最适合那些有直接、事实答案的问题——那些不太模糊或主观的问题。您如何看待使用这些组合指标来更好地检测幻觉?我知道代码可以改进/优化,但目标是快速测试它的工作原理。
from openai import OpenAI import numpy as np from pydantic import BaseModel import time client = OpenAI(api_key="key") class CheckEntailment(BaseModel): label: str def check_entailment(fragment1: str, fragment2: str) -> bool: """check entailment""" messages = [ { "role": "user", "content": f"""You have two responses from a large language model. Check if the meaning of one repsonse is entailed by the other, or if there is a contradiction. Return '0' if entailment. Return '1' if contradiction. Return only the label, without any explanation. \n Response1: \n {fragment1}\n\n Response2: \n {fragment2}""", } ] completion = client.beta.chat.completions.parse( model="gpt-4o-mini", messages=messages, temperature=0.1, logprobs=True, top_logprobs=2, response_format=CheckEntailment, ) entailment = False # print(completion.choices[0].logprobs.content[3].top_logprobs) for top_logprob in completion.choices[0].logprobs.content[3].top_logprobs: print(top_logprob.token, np.round(np.exp(top_logprob.logprob), 2)) if "0" in top_logprob.token and np.exp(top_logprob.logprob) > 0.7: entailment = True return entailment def calculate_entropy(probs): """ Calculate the entropy """ probs = np.array(probs) probs = probs / probs.sum() probs = probs[probs > 0] entropy = -np.sum(probs * np.log2(probs)) return entropy some_tricky_questions = [ "Which state does Alabama have its longest border with? Is it Florida or Tennessee?", "Who hosted the British Gameshow Countdown in 2007: a) Nick Hewer b) Richard Whiteley c) Jeff Stelling?", "Trivia question: Which Black Eyed Peas band member was the only one to host Saturday Night Live?", "What year in the 1980s were the FIS Alpine World Ski Championships hosted in Argentina?", "How many Brazilian numbers are there between 1-6?", "Which Israeli mathematician founded an online sequences repository in the 1970s?", "Write the 7 english words that have three consecutive double letters. No need to provide explanations, just say the words.", # adding two questions where it should not hallucinate "What is the capital of India?", "what is the full form of CPU?", ] for question in some_tricky_questions: print("question", question) messages = [{"role": "user", "content": f"{question}"}] gpt_response = client.chat.completions.create( model="gpt-4o-mini", messages=messages, temperature=0.1, logprobs=True, max_completion_tokens=60, ) time.sleep(2) # get perplexity score using a low temperature response logprobs = [token.logprob for token in gpt_response.choices[0].logprobs.content] perplexity_score = np.round(np.exp(-np.mean(logprobs)), 2) # initialize clusters with the first response clusters = [[gpt_response.choices[0].message.content]] # generate some more responses using higher temperature and check entailment gpt_response = client.chat.completions.create( model="gpt-4o-mini", messages=messages, n=7, temperature=0.9, logprobs=True, max_completion_tokens=60, ) time.sleep(2) # check entailment and form clusters responses = [choice.message.content for choice in gpt_response.choices] for response in responses[1:]: found_cluster = False for cluster in clusters: if check_entailment(cluster[0], response): cluster.append(response) found_cluster = True break if not found_cluster: clusters.append([response]) cluster_probs = [len(cluster) / (len(responses) + 1) for cluster in clusters] discrete_entropy = calculate_entropy(cluster_probs) print("clusters", clusters) print("no of clusters", len(clusters)) print("perplexity", perplexity_score) print("entropy", discrete_entropy)
以上是用离散语义熵和困惑度检测法学硕士的幻觉的详细内容。更多信息请关注PHP中文网其他相关文章!

Python和C 各有优势,选择应基于项目需求。1)Python适合快速开发和数据处理,因其简洁语法和动态类型。2)C 适用于高性能和系统编程,因其静态类型和手动内存管理。

选择Python还是C 取决于项目需求:1)如果需要快速开发、数据处理和原型设计,选择Python;2)如果需要高性能、低延迟和接近硬件的控制,选择C 。

通过每天投入2小时的Python学习,可以有效提升编程技能。1.学习新知识:阅读文档或观看教程。2.实践:编写代码和完成练习。3.复习:巩固所学内容。4.项目实践:应用所学于实际项目中。这样的结构化学习计划能帮助你系统掌握Python并实现职业目标。

在两小时内高效学习Python的方法包括:1.回顾基础知识,确保熟悉Python的安装和基本语法;2.理解Python的核心概念,如变量、列表、函数等;3.通过使用示例掌握基本和高级用法;4.学习常见错误与调试技巧;5.应用性能优化与最佳实践,如使用列表推导式和遵循PEP8风格指南。

Python适合初学者和数据科学,C 适用于系统编程和游戏开发。1.Python简洁易用,适用于数据科学和Web开发。2.C 提供高性能和控制力,适用于游戏开发和系统编程。选择应基于项目需求和个人兴趣。

Python更适合数据科学和快速开发,C 更适合高性能和系统编程。1.Python语法简洁,易于学习,适用于数据处理和科学计算。2.C 语法复杂,但性能优越,常用于游戏开发和系统编程。

每天投入两小时学习Python是可行的。1.学习新知识:用一小时学习新概念,如列表和字典。2.实践和练习:用一小时进行编程练习,如编写小程序。通过合理规划和坚持不懈,你可以在短时间内掌握Python的核心概念。

Python更易学且易用,C 则更强大但复杂。1.Python语法简洁,适合初学者,动态类型和自动内存管理使其易用,但可能导致运行时错误。2.C 提供低级控制和高级特性,适合高性能应用,但学习门槛高,需手动管理内存和类型安全。


热AI工具

Undresser.AI Undress
人工智能驱动的应用程序,用于创建逼真的裸体照片

AI Clothes Remover
用于从照片中去除衣服的在线人工智能工具。

Undress AI Tool
免费脱衣服图片

Clothoff.io
AI脱衣机

Video Face Swap
使用我们完全免费的人工智能换脸工具轻松在任何视频中换脸!

热门文章

热工具

ZendStudio 13.5.1 Mac
功能强大的PHP集成开发环境

mPDF
mPDF是一个PHP库,可以从UTF-8编码的HTML生成PDF文件。原作者Ian Back编写mPDF以从他的网站上“即时”输出PDF文件,并处理不同的语言。与原始脚本如HTML2FPDF相比,它的速度较慢,并且在使用Unicode字体时生成的文件较大,但支持CSS样式等,并进行了大量增强。支持几乎所有语言,包括RTL(阿拉伯语和希伯来语)和CJK(中日韩)。支持嵌套的块级元素(如P、DIV),

SublimeText3 Mac版
神级代码编辑软件(SublimeText3)

PhpStorm Mac 版本
最新(2018.2.1 )专业的PHP集成开发工具

Dreamweaver CS6
视觉化网页开发工具