The Complete Guide to Large‑Model Vector Techniques: From Concepts to Practice

This guide explains vectors, text embedding, embedding models, vector databases, cosine similarity, dimensional trade‑offs, and the full RAG workflow, providing concrete code samples, performance calculations, and real‑world use cases such as semantic search, recommendation, and intelligent customer service.

The Dominant Programmer
The Dominant Programmer
The Dominant Programmer
The Complete Guide to Large‑Model Vector Techniques: From Concepts to Practice

What Is a Vector?

A vector is a numeric array that represents magnitude and direction; examples include 2‑D coordinates [3, 5], 3‑D points [1.2, -0.5, 3.8], and high‑dimensional embeddings with 768, 1024 or 1536 dimensions.

What Is Text Embedding?

Text embedding converts words, sentences or paragraphs into fixed‑length numeric vectors, e.g. "人工智能" → [0.12, -0.45, 0.78, …]. It enables similarity calculation, search, classification, clustering and other mathematical operations on textual data.

Embedding Models

text-embedding-ada-002 – OpenAI – 1536 dimensions – strong general‑purpose performance.

bge-m3 – Beijing Zhiyuan – 1024 dimensions – Chinese‑optimized, open‑source.

text2vec – Domestic open‑source – 768 dimensions – lightweight for Chinese.

m3e – Alibaba Tongyi – 1024 dimensions – multilingual support.

ollama nomic-embed-text – Ollama – 768 dimensions – local deployment, privacy‑friendly.

How to Choose a Model

Language support: Chinese → bge‑m3, text2vec, m3e; English → text‑embedding‑ada‑002; multilingual → m3e, bge‑m3.

Deployment: Cloud API (OpenAI) vs. local (Ollama).

Performance: high precision → large dimensions (1536); speed → smaller dimensions (384‑768); balanced → 1024.

Cost: budget‑rich → OpenAI API; budget‑limited → open‑source local models.

Vector Databases

A vector database stores and retrieves high‑dimensional vectors efficiently. Unlike traditional keyword search, it performs semantic similarity search (ANN) using indexes such as HNSW or IVF.

Common options:

Milvus – open‑source, distributed, production‑grade.

Chroma – lightweight, easy to prototype.

FAISS – Facebook library, high‑performance single‑node.

Pinecone – fully managed cloud service.

Elasticsearch – hybrid keyword + vector search.

PostgreSQL (pgvector) – SQL‑friendly vector extension.

Core Functions of a Vector Database

Store vectors with metadata.

Build ANN indexes to accelerate similarity search.

Return top‑K most similar vectors with similarity scores.

Filter results by metadata conditions.

Cosine Similarity

Cosine similarity measures the angle between two vectors, ranging from –1 (opposite) to 1 (identical). Formula: cos(θ) = (A·B) / (||A|| × ||B||). Example in Python:

import numpy as np
from numpy.linalg import norm
A = np.array([0.1, 0.2, 0.3, 0.4])
B = np.array([0.15, 0.25, 0.35, 0.45])
cosine_similarity = np.dot(A, B) / (norm(A) * norm(B))
print(f"Cosine similarity: {cosine_similarity:.4f}")  # 0.9938

Typical thresholds: 0.9‑1.0 (extremely similar), 0.7‑0.9 (highly related), 0.5‑0.7 (moderately related), 0.3‑0.5 (weakly related), 0‑0.3 (almost unrelated).

Dimensions

Dimension is the number of elements in a vector. Low‑dim (64‑256) → fast, low expressiveness; medium‑dim (384‑768) → balanced; high‑dim (1024‑1536) → high precision but slower and larger storage. Example storage for 1 M vectors: 768 dim → ~2.86 GB; 1536 dim → ~5.72 GB.

Full RAG (Retrieval‑Augmented Generation) Workflow

Knowledge‑base construction: collect documents → chunk text → embed each chunk → store vectors + metadata in a vector DB → build ANN index.

Query processing: embed user question → retrieve top‑K similar chunks → assemble a prompt with system instruction, retrieved context and the question → call a large language model → return the generated answer.

Code Example (Spring AI + Ollama)

@Service
public class RagService {
    @Autowired private EmbeddingModel embeddingModel;
    @Autowired private VectorStore vectorStore;
    @Autowired private ChatClient chatClient;

    // Build knowledge base
    public void buildKnowledgeBase(List<String> documents) {
        for (String doc : documents) {
            List<String> chunks = splitText(doc, 500);
            for (String chunk : chunks) {
                EmbeddingResponse embedding = embeddingModel.embed(chunk);
                float[] vector = embedding.getResults().get(0).getOutput();
                Document document = new Document.Builder()
                        .text(chunk)
                        .vector(vector)
                        .metadata(Map.of("source", "knowledge_base"))
                        .build();
                vectorStore.add(List.of(document));
            }
        }
    }

    // Answer a user question
    public String answerQuestion(String question) {
        EmbeddingResponse queryEmbedding = embeddingModel.embed(question);
        SearchRequest searchRequest = SearchRequest.builder()
                .query(question)
                .topK(5)
                .similarityThreshold(0.7)
                .build();
        List<Document> relevantDocs = vectorStore.similaritySearch(searchRequest);
        StringBuilder context = new StringBuilder();
        for (int i = 0; i < relevantDocs.size(); i++) {
            context.append("Document" + (i + 1) + ": " + relevantDocs.get(i).getText() + "

");
        }
        String prompt = String.format("""
            Based on the following information, answer the question. If the information does not contain the answer, state that.
            %s
            Question: %s
            Answer:
            """, context.toString(), question);
        return chatClient.prompt(prompt).call().content();
    }

    private List<String> splitText(String text, int chunkSize) {
        List<String> chunks = new ArrayList<>();
        for (int i = 0; i < text.length(); i += chunkSize) {
            int end = Math.min(i + chunkSize, text.length());
            chunks.add(text.substring(i, end));
        }
        return chunks;
    }
}

Practical Use Cases

Intelligent customer service – vectorize FAQs, retrieve semantically similar answers.

Enterprise document search – semantic retrieval across mixed languages.

Recommendation systems – embed user behavior and items, recommend by similarity.

Deduplication & clustering – detect near‑duplicate content, auto‑group topics.

Code search – embed code snippets, retrieve functionally similar code.

Learning Path

Understand the concept of vectors.

Experiment with a text‑embedding API.

Manually compute cosine similarity between two vectors.

Install and use a vector database (e.g., Chroma or Milvus).

Build a complete RAG application.

Optimize by selecting appropriate models, dimensions and similarity thresholds.

Key Takeaways

Vectors turn any piece of information into a numeric identifier, enabling semantic search, RAG pipelines and modern AI applications.
Original Source

Signed-in readers can open the original source through BestHub's protected redirect.

Sign in to view source
Republication Notice

This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactadmin@besthub.devand we will review it promptly.

RAGVector DatabaseSpring AIcosine similarityOllamaLangChain4jvector embeddings
The Dominant Programmer
Written by

The Dominant Programmer

Resources and tutorials for programmers' advanced learning journey. Advanced tracks in Java, Python, and C#. Blog: https://blog.csdn.net/badao_liumang_qizhi

0 followers
Reader feedback

How this landed with the community

Sign in to like

Rate this article

Was this worth your time?

Sign in to rate
Discussion

0 Comments

Thoughtful readers leave field notes, pushback, and hard-won operational detail here.