一些适合入门的RAG材料
原理、科普
Retrieval Augmented Generation (RAG) for LLMs
Langchain|Retrieval augmented generation (RAG)
A Practitioners Guide to Retrieval Augmented Generation (RAG)
RAG for Beginners: The Complete Guide to Retrieval Augmented Generation
What is Retrieval Augmented Generation (RAG)?
Retrieval augmented generation (RAG)
思考题:
-
Why RAG? RAG解决了什么问题?RAG和SFT如何选择?RAG的优势?
-
什么样的问题是RAG无法解决的?什么样的数据适合使用RAG
-
为什么要做chunking?chunk-size受那些因素制约?
-
什么是embedding?向量库在做什么?一定要做语义匹配吗?什么是reranking?
-
如何evaluate 检索效果的好坏 ?
-
RAG中,LLMs起到的作用?
Hands-On
前置任务
获取模型服务:
-
获得LLMs供应商API: 推荐 OpenRouter(仅LLMs,需要梯子,境外信用卡或Crypto),SilconFlow(LLMs+embedding,无需梯子,有送token),Groq(仅LLMs, 需梯子,速度快,免费)
-
或采用本地部署: 推荐Ollama
例子,采用openRouter API实现LLM对话:
from openai import OpenAI
client = OpenAI(
base_url="https://openrouter.ai/api/v1",
api_key="<OPENROUTER_API_KEY>",
)
completion = client.chat.completions.create(
model="openai/gpt-4o",
messages=[
{
"role": "user",
"content": "What is the meaning of life?"
}
]
)
print(completion.choices[0].message.content)
例子:基于本地ollama启动的deepseek R1(蒸馏版本)对话
from openai import OpenAI
client = OpenAI(
base_url = 'http://localhost:11434/v1',
api_key='ollama', # required, but unused
)
response = client.chat.completions.create(
model="deepseek-r1:7b",
messages=[
{"role": "system", "content": "You are a helpful assistant."},
{"role": "user", "content": "Who won the world series in 2020?"}
]
)
print(response.choices[0].message.content)
使用ollama 的embdding model Embedding models
低代码方案
不愿意写代码,可以使用一些低代码Agent平台 好处:可以快速体验,上手,低代码 坏处:不支持自定义修改,很难理解这一系统原理
Dify官方教程:使用知识库搭建智能客服机器人
代码教程
A beginner’s guide to building a Retrieval Augmented Generation (RAG) application from scratch 不依赖框架,无需向量库和embedding模型, 采用本地部署的ollama,便于从第一性原理理解这一系统
Building RAG Applications Without LangChain or LlamaIndex 不依赖框架,从底层开发RAG应用,使用openAI调用LLMs,sentence-transformers 做embedding, chromadb作为向量库
Langchain 官方文档中关于RAG的教程:
Build a Retrieval Augmented Generation (RAG) App: Part 1
LangChain: Chat with Your Data
【关于为什么不推荐langchain之类的框架】:虽然能够快速上手跑通一个项目;langchain因其高度抽象化高度集成,很难进行进一步开发;langchain只适合快速做demo的场景使用;