Skip to Content
向量数据库 (Chinese Edition)
book

向量数据库 (Chinese Edition)

by Nitin Borwankar
April 2026
Intermediate
292 pages
3h 58m
Chinese
O'Reilly Media, Inc.
Content preview from 向量数据库 (Chinese Edition)

第 6 章. 使用SQLite VSS 和 Ollama构建 检索增强生成系统

本作品已使用人工智能进行翻译。欢迎您提供反馈和意见:translation-feedback@oreilly.com

在前几章中,我们重点探讨了向量搜索的各个组成部分:创建嵌入向量和执行相似度查询。现在,是时候将这些组件整合成一个可运行的检索增强生成(RAG)系统了。

与依赖分布式 Cloud 集群的生产级 Web 应用不同,我们的目标是构建一个高性能、私有且完全本地化的 RAG 系统,使其能在单台桌面电脑上运行。我们将使用 SQLite VSS 作为搜索引擎,并采用 Ollama 作为本地 LLM“大脑”。

RAG系统解决了LLMs的一个根本性局限:其知识在训练时便已固化,无法获取私有或最新信息。通过为LLMs增强检索机制,我们构建了一个能够利用最新、领域特定知识来回答问题的系统。

我们的目标是构建一个能够智能响应关于Reddit内容查询的问答系统。当用户提出问题时,系统将:(1) 搜索存储的Reddit帖子以查找最相关信息;(2) 检索最匹配的内容片段;(3) 将此上下文提供给LLM;(4) 仅基于检索到的信息生成自然语言答案。

这种方法通过将LLM的回答锚定在实际数据上,确保了事实准确性,显著减少了(尽管未能完全消除)幻觉现象,并使系统能够处理LLM训练数据中未包含的私有或专业内容。

系统架构概述

我们的 RAG 系统由五个协同工作的主要组件构成:

向量数据库层(SQLite VSS)

将内容片段与其嵌入向量一同存储,从而实现快速相似度搜索

嵌入式引擎(SentenceTransformers

将文本转换为能够捕捉语义含义的密集向量表示

混合搜索系统

将语义向量搜索与传统关键词搜索相结合,实现最优检索

LLM集成(Ollama)

提供本地 LLM 推理以生成自然语言响应

RAG 管道协调器

协调检索与生成流程

数据流遵循以下路径:内容摄取 → 文本分块 → 嵌入向量生成 → 向量存储 → 查询处理 → 混合搜索 → 上下文检索 → prompt构建 → LLM生成。参见图6-1

Diagram showing the system architecture with components for content ingestion, embedding, storage, search, and LLM integration, illustrating the data flow through ingestion and query processes.
图 6-1. 系统 架构概览

让我们逐步构建每个组件。

支持向量搜索的数据库基础

首先,我们需要搭建数据库基础设施。我们使用带 VSS 扩展的 SQLite,该扩展为 SQLite 增添了向量搜索功能。这为我们提供了一个轻量级的嵌入式解决方案,非常适合 RAG 应用:

# Chapter 6: Minimal RAG System with SQLite VSS and Ollama
# Simplified version demonstrating core retrieval-augmented generation
import sqlite3
import ollama
import requests
import json
import time
import hashlib
from sentence_transformers import SentenceTransformer
from typing import List, Dict, Optional

导入语句揭示了我们的技术栈:SQLite 用于存储,requests 用于调用 Ollama ...

Become an O’Reilly member and get unlimited access to this title plus top books and audiobooks from O’Reilly and nearly 200 top publishers, thousands of courses curated by job role, 150+ live events each month,
and much more.

Read now

Unlock full access

More than 5,000 organizations count on O’Reilly

AirBnbBlueOriginElectronic ArtsHomeDepotNasdaqRakutenTata Consultancy Services

QuotationMarkO’Reilly covers everything we've got, with content to help us build a world-class technology community, upgrade the capabilities and competencies of our teams, and improve overall team performance as well as their engagement.
Julian F.
Head of Cybersecurity
QuotationMarkI wanted to learn C and C++, but it didn't click for me until I picked up an O'Reilly book. When I went on the O’Reilly platform, I was astonished to find all the books there, plus live events and sandboxes so you could play around with the technology.
Addison B.
Field Engineer
QuotationMarkI’ve been on the O’Reilly platform for more than eight years. I use a couple of learning platforms, but I'm on O'Reilly more than anybody else. When you're there, you start learning. I'm never disappointed.
Amir M.
Data Platform Tech Lead
QuotationMarkI'm always learning. So when I got on to O'Reilly, I was like a kid in a candy store. There are playlists. There are answers. There's on-demand training. It's worth its weight in gold, in terms of what it allows me to do.
Mark W.
Embedded Software Engineer

You might also like

技术主管的进阶之路 (Chinese Edition)

技术主管的进阶之路 (Chinese Edition)

Anemari Fiser
企业级Java开发中的应用人工智能 (Chinese Edition)

企业级Java开发中的应用人工智能 (Chinese Edition)

Alex Soto Bueno, Markus Eisele, Natale Vinto

Publisher Resources

ISBN: 0642572369859