惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

酷 壳 – CoolShell
酷 壳 – CoolShell
H
Hacker News: Front Page
P
Palo Alto Networks Blog
T
ThreatConnect
Apple Machine Learning Research
Apple Machine Learning Research
博客园_首页
T
True Tiger Recordings
P
Privacy & Cybersecurity Law Blog
B
Blog
IT之家
IT之家
Last Week in AI
Last Week in AI
F
Full Disclosure
Hacker News: Ask HN
Hacker News: Ask HN
C
Comments on: Blog
Microsoft Azure Blog
Microsoft Azure Blog
C
Cybersecurity and Infrastructure Security Agency CISA
Microsoft Security Blog
Microsoft Security Blog
博客园 - 【当耐特】
N
News and Events Feed by Topic
NISL@THU
NISL@THU
腾讯CDC
雷峰网
雷峰网
Security Latest
Security Latest
李成银的技术随笔
M
Microsoft Research Blog - Microsoft Research
L
LangChain Blog
L
Lohrmann on Cybersecurity
cs.CL updates on arXiv.org
cs.CL updates on arXiv.org
C
Check Point Blog
Y
Y Combinator Blog
Recent Announcements
Recent Announcements
博客园 - Franky
N
News | PayPal Newsroom
V
V2EX
A
About on SuperTechFans
The Register - Security
The Register - Security
月光博客
月光博客
奇客Solidot–传递最新科技情报
奇客Solidot–传递最新科技情报
Google Online Security Blog
Google Online Security Blog
MyScale Blog
MyScale Blog
Cisco Talos Blog
Cisco Talos Blog
Vercel News
Vercel News
WordPress大学
WordPress大学
C
Cyber Attacks, Cyber Crime and Cyber Security
The Hacker News
The Hacker News
IntelliJ IDEA : IntelliJ IDEA – the Leading IDE for Professional Development in Java and Kotlin | The JetBrains Blog
IntelliJ IDEA : IntelliJ IDEA – the Leading IDE for Professional Development in Java and Kotlin | The JetBrains Blog
爱范儿
爱范儿
A
Arctic Wolf
L
LINUX DO - 最新话题
freeCodeCamp Programming Tutorials: Python, JavaScript, Git & More

博客园 - 软件心理学工程师

后量子密码学(post-quantum cryptography):为什么重要以及如何使用 密钥管理碎碎念 SpringBoot集成resilience4j进行熔断和一个响应式编程的问题 凤凰项目&DevOps实践指南 OAuth2和OpenID API横向越权问题的分析和解决 基于AWS的密钥管理系统 加密算法的使用场景 Linux透明大页(Transparent Huge Pages)对ES性能对影响 《java8 in action》读书笔记 ES索引的一些长度限制 初识redis Elasticsearch慢查询故障诊断 Elasticsearch搜索调优 elasticsearch基础知识杂记 elasticsearch.yml 常用参数说明 linux log rotate elasticsearch local debug环境搭建 CAP碎碎念 彻底删除kafka topic步骤
初识RAG
软件心理学工程师 · 2024-08-27 · via 博客园 - 软件心理学工程师

检索增强生成(RAG,Retrieval-Augmented Generation)能够对大语言模型(LLM)的输出进行优化,使其能够在生成响应(response)之前引用训练数据来源之外的知识库中的数据对输入提示词(prompt)进行润色,从而让大模型给出更准确的答案。但大模型本身受限于训练时所采用的语料库,只能给出通用的响应。RAG可以在一定程度上弥补这方面的不足,使得响应的真实性,本地化和时效性方面有所提高。

最近参加了AWS的一个AI培训,简单的学习了RGA相关的知识。RAG系统业务流程图如下:
1. 构建向量数据库。把数据切分成chunks,就是把海量文本按一定长度或自然段进行切分;再通过Amazon Titan Embeddings library转成向量,最后再把转成的向量存入向量数据库,比如Elasticsearch。
2. 用户输入问题。
3. 通过查询向量数据库,找到跟问题相关的数据,然后构造prompt,通常不同类型的问题需要构造不同的prompt模版,然后把从向量数据库中查到的相似度较高的文本填充到模版里,生成一个prompt
4. 把生成的prompt输入给大模型(这里是Amazon Bedrock Foundation Model),最后大模型给出相关的回复(response)。

如何切分chunks会对生成prompt的结果有一定的影响,比如每个chunk的size,两个相近的chunk之间的overlap

text_splitter = RecursiveCharacterTextSplitter( #create a text splitter
        separators=["\n\n", "\n", ".", " "], #split chunks at (1) paragraph, (2) line, (3) sentence, or (4) word, in that order
        chunk_size=500, #divide into 1000-character chunks using the separators above
        chunk_overlap=50 #number of characters that can overlap with previous chunk
    ) 

当然,采用的文本转向量的模型以及向量数据库(相似度搜索方法)也会对生成的prompt有影响。跟传统的软件开发差别较大的是,传统软件开发中有办法定位到问题的根因并有针对性的解决,但在RAG的系统中,很难定位根因是chunk size的问题,还是转向量的问题或其他问题。

构建RAG的关键是构建Knowledage base。通常的步骤是:构造数据源=>切分成chunks=>转向量=>存入向量数据库

生成高质量的Prompt是RAG的主要功能。通常不同类型的问题对应不同的Prompt模版。一般prompt的开头都是“你是一个某某方面的工程师/咨询师/科学家/...”,让AI扮演改角色,然后描绘背景知识(来自向量数据)和问题。
比如下面这Prompt模版,告诉大模型师一个财务咨询系统,回答相关财务方面的问题。

prompt = f"""
Human: You are a financial advisor AI system, and provides answers to questions by using fact based and statistical information when possible. 
Use the following pieces of information to provide a concise answer to the question enclosed in <question> tags. 
If you don't know the answer, just say that you don't know, don't try to make up an answer.
<context>
{contexts}
</context>

<question>
{query}
</question>

The response should be specific and use statistics or numbers when possible.

Assistant:"""

对于一些连续的问答,把上一次的答案作为下一次的prompt中的content再输入给大模型,一般会得到更准确的结果。通常在内存里或者利用向量数据库来临时存储之前对话的结果。