大模型幻觉(Hallucination)控制方法Guardrails

Guardrails是一套规则和检查,旨在确保 LLM 的输出准确、适当且符合用户期望,控制幻觉。

这里介绍两种Guardrails的应用

  1. Input guardrails 针对输入到LLM的不合规的请求进行处理
  2. Output guardrails 对模型反馈内容到最终用户前进行验证。

Input guardrails

#step1
import openai

GPT_MODEL = 'gpt-4o-mini'
#step2
system_prompt = "You are a helpful assistant."

bad_request = "I want to talk about horses"
good_request = "What are the best breeds of dog for people that like cats?"

step3
import asyncio


async def get_chat_response(user_request):
    print("Getting LLM response")
    messages = [
        {
   "role": "system", "content": system_prompt},
        {
   "role": "user", "content": user_request},
    ]
    response = openai.chat.completions.create(
        model=GPT_MODEL, messages=messages, temperature=0.5
    )
    print("Got LLM response")

    return response.choices[0].message.content


async def topical_guardrail(user_request):
    print("Checking topical guardrail")
    messages = [
        {
   
            "role": "system",
            "content": "Your role is to assess whether the user question is allowed or not. The allowed topics are cats and dogs. If the topic is allowed, say 'allowed' otherwise say 'not_allowed'",
        },
        {
   
### 大模型幻觉的检测方法 大模型幻觉的检测是一个复杂的过程,涉及多种技术和算法。以下是几种常见的检测方法: #### 基于细粒度反馈的幻觉检测模型 一种有效的方式是利用细粒度的人工智能反馈来训练专门用于幻觉检测的模型。这类模型不仅可以识别是否存在幻觉,还可以进一步量化幻觉的严重程度[^2]。 #### 行为观察法 另一种常用的方法是基于行为的检测技术。这种方法的核心在于通过对大模型生成的不同样本进行一致性评估,从而判断其是否出现了与事实不符的情况。具体而言,可以通过多次采样模型的回答,并对比这些回答中的事实陈述是否具有一致性和准确性来进行幻觉检测[^4]。 #### 数据增强与指令优化 针对多模态大模型中存在的幻觉问题,研究人员还提出了通过改进数据质量和调整指令设计的方式来减少幻觉的发生概率。例如,LRV-Instruction 提供了一种新的视觉指令集合方案,其中特别加入了否定形式的指令类别,旨在纠正模型过度肯定化的问题。实验表明,采用此类经过优化的数据集对现有架构(如 MiniGPT-4)进行微调后,确实能够在一定程度上缓解幻觉现象[^5]。 ```python def hallucination_detection(model_output, fact_base): """ A simple example function to demonstrate a basic form of hallucination detection. Args: model_output (str): Output generated by the large language or multi-modal model. fact_base (dict): Dictionary containing factual information used as reference. Returns: bool: True if no hallucinations detected; False otherwise. """ for statement in model_output.split('.'): key_words = extract_keywords(statement) if not verify_against_facts(key_words, fact_base): return False return True def extract_keywords(text): # Placeholder for keyword extraction logic pass def verify_against_facts(keywords, facts): # Logic to compare keywords against known facts pass ``` 上述代码片段展示了一个简化版的大规模语言或多模态模型幻觉检测函数的概念实现方式。它通过提取关键词并与既定的事实库相比较完成初步验证过程。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

技术与健康

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

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

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

打赏作者

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

抵扣说明:

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

余额充值