大模型之RAG-关键字检索的认识与实战(混合检索进阶储备)

前言

按照我们之前的分享(大模型应用RAG系列3-1从0搭建一个RAG:做好文档切分):

RAG系统搭建的基本流程

  1. 准备对应的垂域资料
  2. 文档的读取解析,进行文档切分
  3. 将分割好的文本灌入检索引擎(向量数据库)
  4. 封装检索接口
  5. 构建流程:Query -> 检索 -> Prompt -> LLM -> 回复

今天我们分享一个实现的场景:基于ES实现的关键字检索

为什么还要了解关键字检索

看到本文,可能有的jy会问,现在不都是在讲基于向量数据库的向量检索么,还需要专门再学习关键字检索么。

随着RAG的发展,单纯的关键字检索或者向量检索都很难满足我们面临的复杂场景,了解关键字检索是为了后面更好的去了解混合检索,以及为什么会出现混合检索。

关键字检索的概念

关键字检索是通过匹配查询中的关键字与文档中的关键字来进行检索的。当用户输入一个查询时,系统会在文档集合中查找包含这些关键字的文档,并将它们返回给用户。

传统的一种检索方式,RAG所使用的外挂数据库不只是狭义的数据库,搜索引擎也可以成为真实数据的一种来源,对于部分数据,使用关键词检索会高效快速得多。

关键字检索可能会受到一些问题的影响,例如同义词、拼写错误等,这可能会导致一些相关的文档被漏掉或者一些不相关的文档被检索到。

搭建一个简单关键字检索

安装必要环境

python 复制代码
# 安装ES客户端
!pip install elasticsearch7

# 安装 NLTK(文本处理方法库)
!pip install nltk

文本的处理

这里要注意,针对英文文本和中文文本的处理方法是不一样的

针对英文文本的处理实现

python 复制代码
from elasticsearch7 import Elasticsearch, helpers
from nltk.stem import PorterStemmer
from nltk.tokenize import word_tokenize
from nltk.corpus import stopwords
import nltk
import re

import warnings
# 屏蔽 ES 的一些Warnings
warnings.simplefilter("ignore")  

# 英文切词、词根、切句等方法
nltk.download('punkt')  
# 英文停用词库
nltk.download('stopwords')  

def to_keywords(input_string):
    '''(英文)文本只保留关键字'''
    # 使用正则表达式替换所有非字母数字的字符为空格
    no_symbols = re.sub(r'[^a-zA-Z0-9\s]', ' ', input_string)
    word_tokens = word_tokenize(no_symbols)
    # 加载停用词表
    stop_words = set(stopwords.words('english'))
    ps = PorterStemmer()
    # 去停用词,取词根
    filtered_sentence = [ps.stem(w)
                         for w in word_tokens if not w.lower() in stop_words]
    return ' '.join(filtered_sentence)

针对中文文本的处理实现

python 复制代码
import re
import jieba
import nltk
from nltk.corpus import stopwords

nltk.download('stopwords')  

def to_keywords(input_string):
    """将句子转成检索关键词序列"""
    # 按搜索引擎模式分词
    word_tokens = jieba.cut_for_search(input_string)
    # 加载停用词表
    stop_words = set(stopwords.words('chinese'))
    # 去除停用词
    filtered_sentence = [w for w in word_tokens if not w in stop_words]
    return ' '.join(filtered_sentence)

def sent_tokenize(input_string):
    """按标点断句"""
    # 按标点切分
    sentences = re.split(r'(?<=[。!?;?!])', input_string)
    # 去掉空字符串
    return [sentence for sentence in sentences if sentence.strip()]

将文本灌入检索引擎

python 复制代码
import os, time

# 引入配置文件
ELASTICSEARCH_BASE_URL = os.getenv('ELASTICSEARCH_BASE_URL')
ELASTICSEARCH_PASSWORD = os.getenv('ELASTICSEARCH_PASSWORD')
ELASTICSEARCH_NAME= os.getenv('ELASTICSEARCH_NAME')

# tips: 如果想在本地运行,请在下面一行 print(ELASTICSEARCH_BASE_URL) 获取真实的配置

# 1. 创建Elasticsearch连接
es = Elasticsearch(
    hosts=[ELASTICSEARCH_BASE_URL],  # 服务地址与端口
    http_auth=(ELASTICSEARCH_NAME, ELASTICSEARCH_PASSWORD),  # 用户名,密码
)

# 2. 定义索引名称
index_name = "teacher_demo_index"

# 3. 如果索引已存在,删除它(仅供演示,实际应用时不需要这步)
if es.indices.exists(index=index_name):
    es.indices.delete(index=index_name)

# 4. 创建索引
es.indices.create(index=index_name)

# 5. 灌库指令
actions = [
    {
        "_index": index_name,
        "_source": {
            "keywords": to_keywords(para),
            "text": para
        }
    }
    for para in paragraphs
]

# 6. 文本灌库
helpers.bulk(es, actions)

# 灌库是异步的
time.sleep(2)

实现关键字检索

python 复制代码
def search(query_string, top_n=3):
    # ES 的查询语言
    search_query = {
        "match": {
            "keywords": to_keywords(query_string)
        }
    }
    res = es.search(index=index_name, query=search_query, size=top_n)
    return [hit["_source"]["text"] for hit in res["hits"]["hits"]]
    
results = search("how many parameters does llama 2 have?", 2)
for r in results:
    print(r+"\n")

LLM 接口封装

python 复制代码
from openai import OpenAI
import os
# 加载环境变量
from dotenv import load_dotenv, find_dotenv
_ = load_dotenv(find_dotenv())  # 读取本地 .env 文件,里面定义了 OPENAI_API_KEY

client = OpenAI()

def get_completion(prompt, model="gpt-3.5-turbo"):
    '''封装 openai 接口'''
    messages = [{"role": "user", "content": prompt}]
    response = client.chat.completions.create(
        model=model,
        messages=messages,
        temperature=0,  # 模型输出的随机性,0 表示随机性最小
    )
    return response.choices[0].message.content

Prompt 模板

python 复制代码
def build_prompt(prompt_template, **kwargs):
    '''将 Prompt 模板赋值'''
    inputs = {}
    for k, v in kwargs.items():
        if isinstance(v, list) and all(isinstance(elem, str) for elem in v):
            val = '\n\n'.join(v)
        else:
            val = v
        inputs[k] = val
    return prompt_template.format(**inputs)
text 复制代码
prompt_template = """
你是一个问答机器人。
你的任务是根据下述给定的已知信息回答用户问题。

已知信息:
{context}

用户问:
{query}

如果已知信息不包含用户问题的答案,或者已知信息不足以回答用户的问题,请直接回复"我无法回答您的问题"。
请不要输出已知信息中不包含的信息或答案。
请用中文回答用户问题。
"""

一个简单的RAG Pipeline就产生了

什么是Pipeline

首先,RAG不是一个单项技术,它是一个流水线,行话叫pipeline。只有对流水线上的每一步骤都进行精细打磨,最后才能出来效果,我们的目标就是努力使每个环节都达到尽可能准确

让我们根据RAG的搭建基本流程来看这个demo

python 复制代码
user_query = "how many parameters does llama 2 have?"

# 1. 检索
search_results = search(user_query, 2)

# 2. 构建 Prompt
prompt = build_prompt(prompt_template, context=search_results, query=user_query)
print("===Prompt===")
print(prompt)

# 3. 调用 LLM
response = get_completion(prompt)

print("===回复===")
print(response)

再看下效果

python 复制代码
===Prompt===

你是一个问答机器人。
你的任务是根据下述给定的已知信息回答用户问题。

已知信息:
 1. Llama 2, an updated version of Llama 1, trained on a new mix of publicly available data. We also increased the size of the pretraining corpus by 40%, doubled the context length of the model, and adopted grouped-query attention (Ainslie et al., 2023). We are releasing variants of Llama 2 with 7B, 13B, and 70B parameters. We have also trained 34B variants, which we report on in this paper but are not releasing.§

 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 onour human evaluations for helpfulness and safety, may be a suitable substitute for closed 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.

用户问:
how many parameters does llama 2 have?

如果已知信息不包含用户问题的答案,或者已知信息不足以回答用户的问题,请直接回复"我无法回答您的问题"。
请不要输出已知信息中不包含的信息或答案。
请用中文回答用户问题。

===回复===
Llama 2有7B, 13B和70B参数。

总结

本文我们对关键字检索做了讲解以及从实战的角度做了讲解。

首先我们应该认识到RAG的核心在于外部知识库,这个外部知识库甚至可以是基于关系型数据库的sql查询。

而本文基于ES的关键字检索也是一个比较传统和基础的检索方式,他比较简单,也有它适用的场景

关键字检索的局限性

最关键的就在于:关键字检索可能会受到一些问题的影响,例如同义词、拼写错误等,这可能会导致一些相关的文档被漏掉或者一些不相关的文档被检索到。

关键字检索通常在简单的检索场景下使用,例如在搜索引擎中用户输入关键字进行网页检索。

这样我们就引出了向量检索,在下一篇文章中我们再进行分享。

最后,随着RAG的发展,单纯的关键字检索或者向量检索都很难满足我们面临的复杂场景,了解关键字检索是为了后面更好的了解混合检索

相关推荐
李加号pluuuus2 小时前
【扩散模型】LCM LoRA:一个通用的Stable Diffusion加速模块
人工智能·stable diffusion
Alkali!3 小时前
2-5 softmax 回归的简洁实现
人工智能·数据挖掘·回归
哥廷根数学学派4 小时前
基于Maximin的异常检测方法(MATLAB)
开发语言·人工智能·深度学习·机器学习
xrgs_shz4 小时前
人工智能、机器学习、神经网络、深度学习和卷积神经网络的概念和关系
人工智能·深度学习·神经网络·机器学习·卷积神经网络
zzyincsdn6 小时前
从FasterTransformer源码解读开始了解大模型(2.1)代码通读03
人工智能·llm·fastertransform
dc爱傲雪和技术6 小时前
相机参数与图像处理技术解析
图像处理·人工智能·数码相机
QQ21696368816 小时前
Hi6602 恒压恒流SSR电源方案
人工智能·硬件架构·硬件工程·信息与通信·智能硬件
wodertianna6 小时前
机器学习原理之 -- 神经网络:由来及原理详解
人工智能·神经网络·机器学习
续亮~7 小时前
9、程序化创意
前端·javascript·人工智能
yuzhangfeng7 小时前
【大模型】解锁语言模型潜能:提示工程的艺术
人工智能·语言模型·自然语言处理