可配置的模块化RAG框架
环境依赖
本教程基于docker安装Qdrant数据库,在此之前请先安装docker.
-
Docker - The easiest way to use Qdrant is to run a pre-built Docker image.
-
Python version >=3.8
启动Qdrant容器
1.拉取镜像
docker pull qdrant/qdrant
2.启动qdrant容器服务
docker run -d \ --name qdrant_server \ -v $(pwd)/qdrant_storage:/qdrant/storage \ -p 6333:6333 \ qdrant/qdrant
-
从
qdrant/qdrant
镜像创建一个名为qdrant_server
的容器。 -
将宿主机的
$(pwd)/qdrant_storage
目录挂载到容器的/qdrant/storage
目录,以实现数据持久化。 -
将宿主机的
6333
端口映射到容器的6333
端口,以便通过宿主机访问 Qdrant 服务。 -
容器在后台运行,不会占用当前终端。
docker logs qdrant_server
可以看到下面日志:通过 http://localhost:6333/dashboard 地址访问web ui
基于RESTful API 操作向量数据库
第一步:创建一个集合
Qdrant向量数据库的集合概念可以类比MYSQL的表结构,用于统一存储同一类向量数据,集合中存储的每一条数据,在Qdrant中称为点(points),这里的点有数学几何空间的点类似的意思,代表向量在几何空间中的表示(你就当成一条数据看待就行)。
首先,我们需要创建一个名为star_charts
的集合,用来存储殖民地数据。每个位置都会用一个四维向量来表示,并且我们会使用点积(Dot Product)作为相似度搜索的距离度量。
运行以下命令来创建集合:
PUT collections/star_charts { "vectors": { "size": 4, "distance": "Dot" } }
第二步:将数据加载到集合中
创建好集合之后,我们可以向集合添加向量数据,在Qdrant中向量数据使用point表示,一条point数据包括三部分id、payload(关联数据)、向量数据(vector)三部分。
现在集合已经设置好了,接下来我们添加一些数据。每个位置都会有一个向量和一些额外的信息(称为 payload),比如它的名字。
运行以下请求来添加数据:
PUT collections/star_charts/points { "points": [ { "id": 1, "vector": [0.05, 0.61, 0.76, 0.74], "payload": { "colony": "Mars" } }, { "id": 2, "vector": [0.19, 0.81, 0.75, 0.11], "payload": { "colony": "Jupiter" } }, { "id": 3, "vector": [0.36, 0.55, 0.47, 0.94], "payload": { "colony": "Venus" } }, { "id": 4, "vector": [0.18, 0.01, 0.85, 0.80], "payload": { "colony": "Moon" } }, { "id": 5, "vector": [0.24, 0.18, 0.22, 0.44], "payload": { "colony": "Pluto" } } ] }
第三步:运行搜索查询
现在,我们来搜索一下与某个特定向量(代表一个空间位置)最接近的三个殖民地。这个查询会返回这些殖民地以及它们的 payload 信息。
运行以下查询来找到最近的殖民地:
POST collections/star_charts/points/search { "vector": [0.2, 0.1, 0.9, 0.7], "limit": 3, "with_payload": true }
这样,你就可以找到与给定向量最接近的三个殖民地了!
上面命令,我们都可以在面板里面执行,点击集合可以看到我们刚刚创建的例子:
点击可视化,我们可以看到集合里面的向量(point)
更多高级用法可以查看面板中的教程:
http://localhost:6333/dashboard#/tutorial
基于qdrant_client操作向量数据库
以下是将上述内容转换为 Markdown 格式的版本:
`# Qdrant 快速入门指南 ## 安装 `qdrant-client` 包(Python) ```bash pip install qdrant-client `
初始化客户端
from qdrant_client import QdrantClient client = QdrantClient(url="http://localhost:6333")
创建 Collection
所有的向量数据(vector data)都存储在 Qdrant Collection 上。创建一个名为test_collection
的 collection,该 collection 使用dot product
作为比较向量的指标。
from qdrant_client.models import Distance, VectorParams client.create_collection( collection_name="test_collection", vectors_config=VectorParams(size=4, distance=Distance.DOT), )
添加带 Payload 的向量
Payload 是与向量相关联的数据。
from qdrant_client.models import PointStruct operation_info = client.upsert( collection_name="test_collection", wait=True, points=[ PointStruct(id=1, vector=[0.05, 0.61, 0.76, 0.74], payload={"city": "Berlin"}), PointStruct(id=2, vector=[0.19, 0.81, 0.75, 0.11], payload={"city": "London"}), PointStruct(id=3, vector=[0.36, 0.55, 0.47, 0.94], payload={"city": "Moscow"}), PointStruct(id=4, vector=[0.18, 0.01, 0.85, 0.80], payload={"city": "New York"}), PointStruct(id=5, vector=[0.24, 0.18, 0.22, 0.44], payload={"city": "Beijing"}), PointStruct(id=6, vector=[0.35, 0.08, 0.11, 0.44], payload={"city": "Mumbai"}), ] ) print(operation_info)
运行查询
search_result = client.query_points( collection_name="test_collection", query=[0.2, 0.1, 0.9, 0.7], limit=3 ).points print(search_result)
输出
[ { "id": 4, "version": 0, "score": 1.362, "payload": null, "vector": null }, { "id": 1, "version": 0, "score": 1.273, "payload": null, "vector": null }, { "id": 3, "version": 0, "score": 1.208, "payload": null, "vector": null } ]
添加过滤器
from qdrant_client.models import Filter, FieldCondition, MatchValue search_result = client.query_points( collection_name="test_collection", query=[0.2, 0.1, 0.9, 0.7], query_filter=Filter( must=[FieldCondition(key="city", match=MatchValue(value="London"))] ), with_payload=True, limit=3, ).points print(search_result)
[ { "id": 2, "version": 0, "score": 0.871, "payload": { "city": "London" }, "vector": null } ]
语义搜索入门实现
以官方教程为例,我在TrustRAG中对Qdrant进行了封装改造:
官方教程:**https://qdrant.tech/documentation/beginner-tutorials/neural-search/**TrusRAG实现代码
QdrantEngine
:https://github.com/gomate-community/TrustRAG/blob/main/trustrag/modules/engine/qdrant.py
以下为使用完整代码:
from trustrag.modules.engine.qdrant import QdrantEngine from trustrag.modules.engine.qdrant import SentenceTransformerEmbedding if __name__ == "__main__": # Initialize embedding generators local_embedding_generator = SentenceTransformerEmbedding(model_name_or_path="all-MiniLM-L6-v2", device="cpu") # openai_embedding_generator = OpenAIEmbedding(api_key="your_key", base_url="https://ark.cn-beijing.volces.com/api/v3", model="your_model_id") # Initialize QdrantEngine with local embedding generator qdrant_engine = QdrantEngine( collection_name="startups", embedding_generator=local_embedding_generator, qdrant_client_params={"host": "192.168.1.5", "port": 6333}, ) documents=[ {"name": "SaferCodes", "images": "https:\/\/safer.codes\/img\/brand\/logo-icon.png", "alt": "SaferCodes Logo QR codes generator system forms for COVID-19", "description": "QR codes systems for COVID-19.\nSimple tools for bars, restaurants, offices, and other small proximity businesses.", "link": "https:\/\/safer.codes", "city": "Chicago"}, {"name": "Human Practice", "images": "https:\/\/d1qb2nb5cznatu.cloudfront.net\/startups\/i\/373036-94d1e190f12f2c919c3566ecaecbda68-thumb_jpg.jpg?buster=1396498835", "alt": "Human Practice - health care information technology", "description": "Point-of-care word of mouth\nPreferral is a mobile platform that channels physicians\u2019 interest in networking with their peers to build referrals within a hospital system.\nHospitals are in a race to employ physicians, even though they lose billions each year ($40B in 2014) on employment. Why ...", "link": "http:\/\/humanpractice.com", "city": "Chicago"}, {"name": "StyleSeek", "images": "https:\/\/d1qb2nb5cznatu.cloudfront.net\/startups\/i\/3747-bb0338d641617b54f5234a1d3bfc6fd0-thumb_jpg.jpg?buster=1329158692", "alt": "StyleSeek - e-commerce fashion mass customization online shopping", "description": "Personalized e-commerce for lifestyle products\nStyleSeek is a personalized e-commerce site for lifestyle products.\nIt works across the style spectrum by enabling users (both men and women) to create and refine their unique StyleDNA.\nStyleSeek also promotes new products via its email newsletter, 100% personalized ...", "link": "http:\/\/styleseek.com", "city": "Chicago"}, {"name": "Scout", "images": "https:\/\/d1qb2nb5cznatu.cloudfront.net\/startups\/i\/190790-dbe27fe8cda0614d644431f853b64e8f-thumb_jpg.jpg?buster=1389652078", "alt": "Scout - security consumer electronics internet of things", "description": "Hassle-free Home Security\nScout is a self-installed, wireless home security system. We've created a more open, affordable and modern system than what is available on the market today. With month-to-month contracts and portable devices, Scout is a renter-friendly solution for the other ...", "link": "http:\/\/www.scoutalarm.com", "city": "Chicago"}, {"name": "Invitation codes", "images": "https:\/\/invitation.codes\/img\/inv-brand-fb3.png", "alt": "Invitation App - Share referral codes community ", "description": "The referral community\nInvitation App is a social network where people post their referral codes and collect rewards on autopilot.", "link": "https:\/\/invitation.codes", "city": "Chicago"}, {"name": "Hyde Park Angels", "images": "https:\/\/d1qb2nb5cznatu.cloudfront.net\/startups\/i\/61114-35cd9d9689b70b4dc1d0b3c5f11c26e7-thumb_jpg.jpg?buster=1427395222", "alt": "Hyde Park Angels - ", "description": "Hyde Park Angels is the largest and most active angel group in the Midwest. With a membership of over 100 successful entrepreneurs, executives, and venture capitalists, the organization prides itself on providing critical strategic expertise to entrepreneurs and ...", "link": "http:\/\/hydeparkangels.com", "city": "Chicago"}, {"name": "GiveForward", "images": "https:\/\/d1qb2nb5cznatu.cloudfront.net\/startups\/i\/1374-e472ccec267bef9432a459784455c133-thumb_jpg.jpg?buster=1397666635", "alt": "GiveForward - health care startups crowdfunding", "description": "Crowdfunding for medical and life events\nGiveForward lets anyone to create a free fundraising page for a friend or loved one's uncovered medical bills, memorial fund, adoptions or any other life events in five minutes or less. Millions of families have used GiveForward to raise more than $165M to let ...", "link": "http:\/\/giveforward.com", "city": "Chicago"}, {"name": "MentorMob", "images": "https:\/\/d1qb2nb5cznatu.cloudfront.net\/startups\/i\/19374-3b63fcf38efde624dd79c5cbd96161db-thumb_jpg.jpg?buster=1315734490", "alt": "MentorMob - digital media education ventures for good crowdsourcing", "description": "Google of Learning, indexed by experts\nProblem: Google doesn't index for learning. Nearly 1 billion Google searches are done for \"how to\" learn various topics every month, from photography to entrepreneurship, forcing learners to waste their time sifting through the millions of results.\nMentorMob is ...", "link": "http:\/\/www.mentormob.com", "city": "Chicago"}, {"name": "The Boeing Company", "images": "https:\/\/d1qb2nb5cznatu.cloudfront.net\/startups\/i\/49394-df6be7a1eca80e8e73cc6699fee4f772-thumb_jpg.jpg?buster=1406172049", "alt": "The Boeing Company - manufacturing transportation", "description": "", "link": "http:\/\/www.boeing.com", "city": "Berlin"}, {"name": "NowBoarding \u2708\ufe0f", "images": "https:\/\/static.above.flights\/img\/lowcost\/envelope_blue.png", "alt": "Lowcost Email cheap flights alerts", "description": "Invite-only mailing list.\n\nWe search the best weekend and long-haul flight deals\nso you can book before everyone else.", "link": "https:\/\/nowboarding.club\/", "city": "Berlin"}, {"name": "Rocketmiles", "images": "https:\/\/d1qb2nb5cznatu.cloudfront.net\/startups\/i\/158571-e53ddffe9fb3ed5e57080db7134117d0-thumb_jpg.jpg?buster=1361371304", "alt": "Rocketmiles - e-commerce online travel loyalty programs hotels", "description": "Fueling more vacations\nWe enable our customers to travel more, travel better and travel further. 20M+ consumers stock away miles & points to satisfy their wanderlust.\nFlying around or using credit cards are the only good ways to fill the stockpile today. We've built the third way. Customers ...", "link": "http:\/\/www.Rocketmiles.com", "city": "Berlin"} ] vectors = qdrant_engine.embedding_generator.generate_embedding([doc["description"] for doc in documents]) print(vectors.shape) payload = [doc for doc in documents] # Upload vectors and payload qdrant_engine.upload_vectors(vectors=vectors, payload=payload) # Build a filter for city and category conditions = [ {"key": "city", "match": "Berlin"}, ] custom_filter = qdrant_engine.build_filter(conditions) # Search for startups related to "vacations" in Berlin results = qdrant_engine.search(text="vacations", query_filter=custom_filter, limit=5) for result in results: print(result)
如何学习大模型 AI ?
由于新岗位的生产效率,要优于被取代岗位的生产效率,所以实际上整个社会的生产效率是提升的。
但是具体到个人,只能说是:
“最先掌握AI的人,将会比较晚掌握AI的人有竞争优势”。
这句话,放在计算机、互联网、移动互联网的开局时期,都是一样的道理。
我在一线互联网企业工作十余年里,指导过不少同行后辈。帮助很多人得到了学习和成长。
我意识到有很多经验和知识值得分享给大家,也可以通过我们的能力和经验解答大家在人工智能学习中的很多困惑,所以在工作繁忙的情况下还是坚持各种整理和分享。但苦于知识传播途径有限,很多互联网行业朋友无法获得正确的资料得到学习提升,故此将并将重要的AI大模型资料包括AI大模型入门学习思维导图、精品AI大模型学习书籍手册、视频教程、实战学习等录播视频免费分享出来。
第一阶段(10天):初阶应用
该阶段让大家对大模型 AI有一个最前沿的认识,对大模型 AI 的理解超过 95% 的人,可以在相关讨论时发表高级、不跟风、又接地气的见解,别人只会和 AI 聊天,而你能调教 AI,并能用代码将大模型和业务衔接。
- 大模型 AI 能干什么?
- 大模型是怎样获得「智能」的?
- 用好 AI 的核心心法
- 大模型应用业务架构
- 大模型应用技术架构
- 代码示例:向 GPT-3.5 灌入新知识
- 提示工程的意义和核心思想
- Prompt 典型构成
- 指令调优方法论
- 思维链和思维树
- Prompt 攻击和防范
- …
第二阶段(30天):高阶应用
该阶段我们正式进入大模型 AI 进阶实战学习,学会构造私有知识库,扩展 AI 的能力。快速开发一个完整的基于 agent 对话机器人。掌握功能最强的大模型开发框架,抓住最新的技术进展,适合 Python 和 JavaScript 程序员。
- 为什么要做 RAG
- 搭建一个简单的 ChatPDF
- 检索的基础概念
- 什么是向量表示(Embeddings)
- 向量数据库与向量检索
- 基于向量检索的 RAG
- 搭建 RAG 系统的扩展知识
- 混合检索与 RAG-Fusion 简介
- 向量模型本地部署
- …
第三阶段(30天):模型训练
恭喜你,如果学到这里,你基本可以找到一份大模型 AI相关的工作,自己也能训练 GPT 了!通过微调,训练自己的垂直大模型,能独立训练开源多模态大模型,掌握更多技术方案。
到此为止,大概2个月的时间。你已经成为了一名“AI小子”。那么你还想往下探索吗?
- 为什么要做 RAG
- 什么是模型
- 什么是模型训练
- 求解器 & 损失函数简介
- 小实验2:手写一个简单的神经网络并训练它
- 什么是训练/预训练/微调/轻量化微调
- Transformer结构简介
- 轻量化微调
- 实验数据集的构建
- …
第四阶段(20天):商业闭环
对全球大模型从性能、吞吐量、成本等方面有一定的认知,可以在云端和本地等多种环境下部署大模型,找到适合自己的项目/创业方向,做一名被 AI 武装的产品经理。
- 硬件选型
- 带你了解全球大模型
- 使用国产大模型服务
- 搭建 OpenAI 代理
- 热身:基于阿里云 PAI 部署 Stable Diffusion
- 在本地计算机运行大模型
- 大模型的私有化部署
- 基于 vLLM 部署大模型
- 案例:如何优雅地在阿里云私有部署开源大模型
- 部署一套开源 LLM 项目
- 内容安全
- 互联网信息服务算法备案
- …
学习是一个过程,只要学习就会有挑战。天道酬勤,你越努力,就会成为越优秀的自己。
如果你能在15天内完成所有的任务,那你堪称天才。然而,如果你能完成 60-70% 的内容,你就已经开始具备成为一名大模型 AI 的正确特征了。