0Pricing
LangChain / RAG / Vector DBs · 课时

嵌入与向量数据库

了解文本嵌入如何将含义转换为向量,以及向量数据库如何支持 RAG 核心的检索步骤。

嵌入与向量数据库 是 CoddyKit 上的免费 LangChain / RAG / Vector DBs 课时。 这是第 4 节课,共 4 节。 你可以在下方免费阅读本课时的完整内容 — 然后在浏览器中使用内置代码编辑器和全天候 AI 导师进行实践。 这是 LangChain / RAG / Vector DBs 学习路径的一部分,你的进度在网页和 CoddyKit 应用中同步。 LangChain / RAG / Vector DBs 课程共包含 4 节课。

本课时的部分内容尚未翻译,以英文显示。

From Words to Vectors

Computers cannot compare meaning directly. An embedding is a vector of numbers representing a text’s meaning, so similar texts land close together.

What an Embedding Looks Like

An embedding model maps text to a fixed-length vector with hundreds or thousands of dimensions. The numbers are not readable — what counts is their geometric relationships.

text = 'a cup of coffee'
embedding = [0.12, -0.04, 0.88, 0.31]  # simplified
print('dimensions:', len(embedding))

Semantic Similarity

Because meaning maps to position, "dog" sits near "puppy" but far from "database". That is what powers semantic search — matching by meaning, not exact keywords.

Measuring Closeness

The go-to closeness metric is cosine similarity — the cosine of the angle between two vectors. 1 means nearly identical, 0 means unrelated.

def cosine(a, b):
    dot = sum(x*y for x, y in zip(a, b))
    na = sum(x*x for x in a) ** 0.5
    nb = sum(y*y for y in b) ** 0.5
    return dot / (na * nb)

print(round(cosine([1, 0, 1], [1, 0, 1]), 2))
print(round(cosine([1, 0, 0], [0, 1, 0]), 2))

Why a Vector Database?

RAG must find the most relevant chunks among millions of vectors, fast. A vector database stores embeddings and finds nearest neighbors — something SQL is not built for.

Approximate Nearest Neighbor

Comparing a query to every vector is too slow at scale. Vector DBs use ANN indexes like HNSW that trade a sliver of accuracy for huge speed gains.

Indexing Documents

To build a knowledge base, index your docs: split into chunks, embed each one, and store the vector with its text and metadata. This is RAG’s offline ingestion step.

chunks = ['intro paragraph', 'pricing details', 'support hours']
for c in chunks:
    vec = embed(c)        # call embedding model
    db.upsert(vec, text=c)

Querying

At query time, embed the user’s question with the same model, then ask the vector DB for the top-k nearest chunks — that becomes the LLM’s context.

q_vec = embed('when is support open?')
results = db.search(q_vec, top_k=3)
for r in results:
    print(r.text, r.score)

Metadata Filtering

Vector DBs also support metadata filtering: combine similarity search with filters like language, date range, or owner to sharpen relevance.

db.search(q_vec, top_k=3, filter={'lang': 'en', 'year': 2026})

Choosing a Vector Store

Your vector store options span libraries (FAISS), dedicated DBs (Pinecone, Weaviate, Qdrant, Milvus), and extensions like pgvector. Pick by scale, hosting, and data needs.

Embeddings in the RAG Pipeline

Embeddings and the vector DB are the retrieval half of RAG: index once, then for every question embed, search, and hand the top chunks to the LLM as grounding.

Quick Check

Test your understanding of embeddings and vector search.

Recap

You learned RAG’s retrieval foundation: embeddings turn meaning into vectors, cosine measures closeness, and vector DBs use ANN for fast nearest-neighbor search.

常见问题解答

「嵌入与向量数据库」课时是免费的吗?

是的 — 「嵌入与向量数据库」的完整文本可在网页上免费阅读。要进行交互式练习(内置代码编辑器和全天候 AI 导师)并解锁 LangChain / RAG / Vector DBs 课程的其余内容,请升级到 CoddyKit PRO。 LangChain / RAG / Vector DBs 课程共包含 4 节课。

「嵌入与向量数据库」这节课中我会学到什么?

了解文本嵌入如何将含义转换为向量,以及向量数据库如何支持 RAG 核心的检索步骤。 你通过在浏览器中直接运行的动手代码来练习 LangChain / RAG / Vector DBs,全天候 AI 导师会在你学习这节课的过程中回答你的问题。

学习 LangChain / RAG / Vector DBs 需要有经验吗?

无需任何先前经验。CoddyKit 上的 LangChain / RAG / Vector DBs 课程适合初学者到高级学习者,你可以从这里开始或从头开始,按照自己的节奏学习。 这是第 4 节课,共 4 节。

「嵌入与向量数据库」课时需要多长时间?

大多数 CoddyKit 课程大约需要 5–10 分钟。每节课都很精短且互动,所以你能稳步进步,并在网页和应用中从离开的地方继续。

我能在这节 LangChain / RAG / Vector DBs 课中编写并运行代码吗?

能。每节 LangChain / RAG / Vector DBs 课都包含内置代码编辑器,你可以在浏览器中直接编写并运行真实代码,并获得即时 AI 反馈 — 无需本地设置。

此课程中的所有课时

  1. 什么是大语言模型
  2. 检索增强生成的必要性
  3. RAG 系统的核心组件
  4. 嵌入与向量数据库
← 返回 LangChain / RAG / Vector DBs