<?xml version="1.0" encoding="utf-8"?>
<rss version="2.0">
<channel>
<title>Coder Legion - Recent posts tagged embedding-models</title>
<link>https://coderlegion.com/tag/embedding-models</link>
<description>Powered by Question2Answer</description>
<item>
<title>Understanding Semantic Search: Vector Embeddings and Similarity Search</title>
<link>https://coderlegion.com/10918/understanding-semantic-search-vector-embeddings-and-similarity-search</link>
<description>&lt;p&gt;Semantic search represents a fundamental shift in how we retrieve information from databases and search engines. Unlike traditional keyword-based search that relies on exact text matches, semantic search understands the meaning and context behind queries, enabling more intuitive and accurate information retrieval.&lt;/p&gt;
&lt;h3 id=&quot;what-is-semantic-search-&quot;&gt;What is Semantic Search?&lt;/h3&gt;
&lt;p&gt;Semantic search goes beyond keyword matching to understand the intent and contextual meaning behind a query. Instead of looking for exact word matches, it retrieves results based on semantic similarity—finding content that means the same thing, even when different words are used.&lt;/p&gt;
&lt;p&gt;For example, searching for &quot;healthy dinner ideas&quot; could return results like &quot;nutritious meal prep for busy nights&quot; even though the exact keywords don't match. This is possible because semantic search operates on the underlying meaning of the content.&lt;/p&gt;
&lt;h3 id=&quot;understanding-vector-data-distribution&quot;&gt;Understanding Vector Data Distribution&lt;/h3&gt;
&lt;p&gt;Vector embeddings, which power semantic search, have unique characteristics:&lt;/p&gt;
&lt;h4 id=&quot;uneven-distribution&quot;&gt;Uneven Distribution&lt;/h4&gt;
&lt;p&gt;Vector data points are not uniformly distributed across vector space. Instead, they cluster around regions of semantic similarity, reflecting how related concepts group together in meaning.&lt;/p&gt;
&lt;h4 id=&quot;semantic-clustering&quot;&gt;Semantic Clustering&lt;/h4&gt;
&lt;p&gt;Vectors representing similar concepts naturally cluster together:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;Words like &quot;king,&quot; &quot;queen,&quot; &quot;prince,&quot; and &quot;princess&quot; form a royalty cluster&lt;/li&gt;
&lt;li&gt;Technical terms like &quot;algorithm,&quot; &quot;function,&quot; and &quot;code&quot; cluster in programming regions&lt;/li&gt;
&lt;li&gt;Synonyms and semantically related phrases position close to each other&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;This clustering property is fundamental to how semantic search works—we find related content by finding nearby vectors in this space.&lt;/p&gt;
&lt;h3 id=&quot;how-similarity-search-works-k-nn-principle&quot;&gt;How Similarity Search Works: k-NN Principle&lt;/h3&gt;
&lt;p&gt;At its core, semantic search relies on &lt;strong&gt;k-Nearest Neighbors (k-NN)&lt;/strong&gt; search. When you perform a similarity search:&lt;/p&gt;
&lt;ol&gt;
&lt;li&gt;Convert your query into a vector embedding&lt;/li&gt;
&lt;li&gt;Find the &lt;strong&gt;k nearest vectors&lt;/strong&gt; to your query vector in the vector space&lt;/li&gt;
&lt;li&gt;Retrieve the corresponding documents or data points&lt;/li&gt;
&lt;/ol&gt;
&lt;p&gt;The result is an &lt;strong&gt;ordered list ranked by similarity&lt;/strong&gt;, with the most semantically similar items appearing first.&lt;/p&gt;
&lt;h4 id=&quot;distance-metrics&quot;&gt;Distance Metrics&lt;/h4&gt;
&lt;p&gt;Similarity between vectors is measured using:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;&lt;strong&gt;Cosine Similarity&lt;/strong&gt;: Measures the angle between vectors (commonly used for text)&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Euclidean Distance&lt;/strong&gt;: Straight-line distance between points&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Dot Product&lt;/strong&gt;: Useful for normalized vectors&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Manhattan Distance&lt;/strong&gt;: Sum of absolute differences along each dimension&lt;/li&gt;
&lt;/ul&gt;
&lt;h3 id=&quot;types-of-similarity-search&quot;&gt;Types of Similarity Search&lt;/h3&gt;
&lt;h4 id=&quot;1-exact-search-exhaustive-search-&quot;&gt;1. Exact Search (Exhaustive Search)&lt;/h4&gt;
&lt;p&gt;&lt;strong&gt;How It Works:&lt;/strong&gt; Compares the query vector against every single vector in the database.&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;Characteristics:&lt;/strong&gt;&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;&lt;strong&gt;Accuracy&lt;/strong&gt;: 100% accurate—guarantees finding actual nearest neighbors&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Speed&lt;/strong&gt;: Slow for large datasets (can take hours for millions of vectors)&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Use Cases&lt;/strong&gt;: Small datasets (&amp;lt; 10,000 documents) or when perfect accuracy is critical&lt;/li&gt;
&lt;/ul&gt;
&lt;h4 id=&quot;2-approximate-search-ann-&quot;&gt;2. Approximate Search (ANN)&lt;/h4&gt;
&lt;p&gt;&lt;strong&gt;How It Works:&lt;/strong&gt; Uses specialized algorithms (like HNSW, IVF, or LSH) to efficiently search through large datasets by narrowing down the search space.&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;Characteristics:&lt;/strong&gt;&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;&lt;strong&gt;Accuracy&lt;/strong&gt;: High accuracy (typically 90-99%)&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Speed&lt;/strong&gt;: Dramatically faster—searches that take 65 hours with exact search complete in seconds&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Use Cases&lt;/strong&gt;: Large datasets (hundreds of thousands to billions of vectors)&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;&lt;strong&gt;Popular ANN Algorithms:&lt;/strong&gt;&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;&lt;strong&gt;HNSW&lt;/strong&gt;: Graph-based, extremely fast for queries&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;IVF&lt;/strong&gt;: Cluster-based, good for very large datasets&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;LSH&lt;/strong&gt;: Hash-based, excellent for high-dimensional data&lt;/li&gt;
&lt;/ul&gt;
&lt;table&gt;
&lt;thead&gt;
&lt;tr&gt;&lt;th&gt;Aspect &lt;/th&gt;&lt;th&gt;Exact Search &lt;/th&gt;&lt;th&gt;Approximate Search&lt;/th&gt;&lt;/tr&gt;
&lt;/thead&gt;
&lt;tbody&gt;
&lt;tr&gt;&lt;td&gt;&lt;strong&gt;Accuracy&lt;/strong&gt; &lt;/td&gt;&lt;td&gt;100% &lt;/td&gt;&lt;td&gt;90-99%&lt;/td&gt;&lt;/tr&gt;
&lt;tr&gt;&lt;td&gt;&lt;strong&gt;Speed&lt;/strong&gt; &lt;/td&gt;&lt;td&gt;Slow (linear) &lt;/td&gt;&lt;td&gt;Fast (sub-linear)&lt;/td&gt;&lt;/tr&gt;
&lt;tr&gt;&lt;td&gt;&lt;strong&gt;Scalability&lt;/strong&gt; &lt;/td&gt;&lt;td&gt;Poor for large datasets &lt;/td&gt;&lt;td&gt;Excellent&lt;/td&gt;&lt;/tr&gt;
&lt;tr&gt;&lt;td&gt;&lt;strong&gt;Best For&lt;/strong&gt; &lt;/td&gt;&lt;td&gt;&amp;lt; 10K documents &lt;/td&gt;&lt;td&gt;&amp;gt; 10K documents&lt;/td&gt;&lt;/tr&gt;
&lt;/tbody&gt;
&lt;/table&gt;
&lt;h3 id=&quot;vector-embedding-models&quot;&gt;Vector Embedding Models&lt;/h3&gt;
&lt;p&gt;Vector embeddings are the &quot;translation layer&quot; that converts human-readable content into machine-understandable numerical representations.&lt;/p&gt;
&lt;h4 id=&quot;what-are-embedding-models-&quot;&gt;What Are Embedding Models?&lt;/h4&gt;
&lt;p&gt;Embedding models are machine learning models—typically based on &lt;strong&gt;transformer architectures&lt;/strong&gt;—that convert data into dense vector representations trained on massive datasets to understand semantic relationships.&lt;/p&gt;
&lt;h4 id=&quot;key-capabilities&quot;&gt;Key Capabilities&lt;/h4&gt;
&lt;p&gt;&lt;strong&gt;Contextual Understanding:&lt;/strong&gt; Assign meaning based on context (e.g., &quot;bank&quot; in &quot;river bank&quot; vs. &quot;financial bank&quot; gets different embeddings)&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;Feature Extraction:&lt;/strong&gt; Identify and quantify relevant features:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;In text: semantic meaning, sentiment, topic&lt;/li&gt;
&lt;li&gt;In images: shapes, colors, textures, objects&lt;/li&gt;
&lt;li&gt;In audio: pitch, rhythm, timbre, speech patterns&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;&lt;strong&gt;Transformer Architecture:&lt;/strong&gt; Excel at processing sequences, capturing long-range dependencies, and parallel processing for efficiency.&lt;/p&gt;
&lt;h4 id=&quot;popular-models&quot;&gt;Popular Models&lt;/h4&gt;
&lt;p&gt;&lt;strong&gt;For Text:&lt;/strong&gt;&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;&lt;strong&gt;Sentence Transformers&lt;/strong&gt; (all-MiniLM-L6-v2): 384 dimensions, fast and lightweight&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;BERT&lt;/strong&gt;: 768 dimensions, general-purpose language understanding&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;GPT Embeddings&lt;/strong&gt; (text-embedding-ada-002): 1536 dimensions, production-grade&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;&lt;strong&gt;For Images:&lt;/strong&gt;&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;&lt;strong&gt;CLIP&lt;/strong&gt;: Jointly embeds images and text in the same space&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;ResNet&lt;/strong&gt;: Deep convolutional neural network for image features&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;ViT&lt;/strong&gt;: Transformer-based image understanding&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;&lt;strong&gt;For Audio:&lt;/strong&gt;&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;&lt;strong&gt;Wav2Vec 2.0&lt;/strong&gt;: Speech and audio embeddings&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;CLAP&lt;/strong&gt;: Contrastive Language-Audio Pre-training&lt;/li&gt;
&lt;/ul&gt;
&lt;h3 id=&quot;types-of-embedding-models&quot;&gt;Types of Embedding Models&lt;/h3&gt;
&lt;h4 id=&quot;1-pre-trained-open-source-models&quot;&gt;1. Pre-trained Open Source Models&lt;/h4&gt;
&lt;p&gt;&lt;strong&gt;Advantages:&lt;/strong&gt; Zero training cost, proven performance, large community support&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;When to Use:&lt;/strong&gt; General semantic search, quick prototyping, resource-constrained environments&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;Examples:&lt;/strong&gt; Sentence Transformers library (15,000+ models), BERT variants, Universal Sentence Encoder&lt;/p&gt;
&lt;h4 id=&quot;2-custom-models-based-on-your-dataset&quot;&gt;2. Custom Models Based on Your Dataset&lt;/h4&gt;
&lt;p&gt;&lt;strong&gt;Advantages:&lt;/strong&gt; Optimal performance for specific use cases, understands proprietary terminology&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;Process:&lt;/strong&gt; Start with pre-trained model → Fine-tune on labeled data → Evaluate → Iterate&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;Use Cases:&lt;/strong&gt; Medical terminology, legal documents, e-commerce catalogs, scientific research&lt;/p&gt;
&lt;h3 id=&quot;generating-vector-embeddings&quot;&gt;Generating Vector Embeddings&lt;/h3&gt;
&lt;h4 id=&quot;1-outside-the-database&quot;&gt;1. Outside the Database&lt;/h4&gt;
&lt;p&gt;&lt;strong&gt;Third-Party APIs:&lt;/strong&gt; OpenAI, Cohere, Google Vertex AI, Hugging Face&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;Local Inference:&lt;/strong&gt; Python libraries (sentence-transformers, transformers), ONNX Runtime, TensorFlow/PyTorch&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;Example:&lt;/strong&gt;&lt;/p&gt;
&lt;pre&gt;&lt;code class=&quot;language-python&quot;&gt;from sentence_transformers import SentenceTransformer
model = SentenceTransformer('all-MiniLM-L6-v2')
embeddings = model.encode([&quot;Semantic search is powerful&quot;])
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;&lt;strong&gt;Advantages:&lt;/strong&gt; Flexibility in model choice, control over pipeline&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;Disadvantages:&lt;/strong&gt; Requires additional infrastructure, data movement between systems&lt;/p&gt;
&lt;h4 id=&quot;2-within-the-database-onnx-&quot;&gt;2. Within the Database (ONNX)&lt;/h4&gt;
&lt;p&gt;&lt;strong&gt;ONNX&lt;/strong&gt; (Open Neural Network Exchange) is an open format enabling models trained in one framework to be deployed in another.&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;Supported Databases:&lt;/strong&gt; Oracle Database 23ai, PostgreSQL (with extensions), Microsoft SQL Server, SingleStore&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;Example (Oracle):&lt;/strong&gt;&lt;/p&gt;
&lt;pre&gt;&lt;code class=&quot;language-sql&quot;&gt;BEGIN
  DBMS_VECTOR.LOAD_ONNX_MODEL(
    directory =&amp;gt; 'MODEL_DIR',
    file_name =&amp;gt; 'all-MiniLM-L6-v2.onnx',
    model_name =&amp;gt; 'text_embedding_model'
  );
END;
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;&lt;strong&gt;Advantages:&lt;/strong&gt; No data movement, reduced latency, simplified architecture&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;Disadvantages:&lt;/strong&gt; Limited to ONNX-compatible models, database computational overhead&lt;/p&gt;
&lt;table&gt;
&lt;thead&gt;
&lt;tr&gt;&lt;th&gt;Factor &lt;/th&gt;&lt;th&gt;External Generation &lt;/th&gt;&lt;th&gt;In-Database (ONNX)&lt;/th&gt;&lt;/tr&gt;
&lt;/thead&gt;
&lt;tbody&gt;
&lt;tr&gt;&lt;td&gt;&lt;strong&gt;Model Flexibility&lt;/strong&gt; &lt;/td&gt;&lt;td&gt;High &lt;/td&gt;&lt;td&gt;Medium&lt;/td&gt;&lt;/tr&gt;
&lt;tr&gt;&lt;td&gt;&lt;strong&gt;Latency&lt;/strong&gt; &lt;/td&gt;&lt;td&gt;Higher &lt;/td&gt;&lt;td&gt;Lower&lt;/td&gt;&lt;/tr&gt;
&lt;tr&gt;&lt;td&gt;&lt;strong&gt;Data Security&lt;/strong&gt; &lt;/td&gt;&lt;td&gt;Requires data export &lt;/td&gt;&lt;td&gt;Data stays in DB&lt;/td&gt;&lt;/tr&gt;
&lt;tr&gt;&lt;td&gt;&lt;strong&gt;Best For&lt;/strong&gt; &lt;/td&gt;&lt;td&gt;Batch processing, custom models &lt;/td&gt;&lt;td&gt;Real-time apps, integrated systems&lt;/td&gt;&lt;/tr&gt;
&lt;/tbody&gt;
&lt;/table&gt;
&lt;h3 id=&quot;real-world-applications&quot;&gt;Real-World Applications&lt;/h3&gt;
&lt;ol&gt;
&lt;li&gt;&lt;strong&gt;Document Search&lt;/strong&gt;: Find relevant documents based on meaning rather than keywords&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Recommendation Systems&lt;/strong&gt;: Suggest products/content based on similarity&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Question Answering&lt;/strong&gt;: Match user questions to relevant answers&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Content Moderation&lt;/strong&gt;: Identify similar or duplicate content&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Image Search&lt;/strong&gt;: Enable search by visual similarity&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Customer Support&lt;/strong&gt;: Route tickets and find similar past issues&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Fraud Detection&lt;/strong&gt;: Identify unusual patterns&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Code Search&lt;/strong&gt;: Find similar code snippets or search by natural language&lt;/li&gt;
&lt;/ol&gt;
&lt;h3 id=&quot;performance-optimization-tips&quot;&gt;Performance Optimization Tips&lt;/h3&gt;
&lt;ol&gt;
&lt;li&gt;&lt;strong&gt;Choose the Right Balance&lt;/strong&gt;: Use exact search for &amp;lt; 10K documents, approximate for &amp;gt; 100K&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Tune Parameters&lt;/strong&gt;: Adjust &lt;code&gt;ef_search&lt;/code&gt; (HNSW) or &lt;code&gt;nprobe&lt;/code&gt; (IVF) for accuracy vs. speed&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Optimize Dimensions&lt;/strong&gt;: Choose appropriate dimension counts, consider quantization&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Implement Caching&lt;/strong&gt;: Cache frequent embeddings, pre-compute for static content&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Batch Processing&lt;/strong&gt;: Generate embeddings in batches, leverage GPU acceleration&lt;/li&gt;
&lt;/ol&gt;
&lt;h3 id=&quot;key-takeaways&quot;&gt;Key Takeaways&lt;/h3&gt;
&lt;ul&gt;
&lt;li&gt;Vector embeddings capture semantic meaning in numerical form&lt;/li&gt;
&lt;li&gt;Vector data naturally clusters by semantic similarity&lt;/li&gt;
&lt;li&gt;Choose between exact search (accurate, slow) and approximate search (fast, 90-99% accurate) based on your needs&lt;/li&gt;
&lt;li&gt;Transformer-based embedding models provide state-of-the-art semantic understanding&lt;/li&gt;
&lt;li&gt;Models can be pre-trained, custom-trained, or fine-tuned for specific domains&lt;/li&gt;
&lt;li&gt;Embeddings can be generated externally or within databases using ONNX&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;Whether you're building a search engine, recommendation system, or AI-powered application, understanding these concepts is crucial for leveraging the full power of modern semantic search technologies.&lt;/p&gt;
</description>
<category>Articles</category>
<guid isPermaLink="true">https://coderlegion.com/10918/understanding-semantic-search-vector-embeddings-and-similarity-search</guid>
<pubDate>Mon, 02 Feb 2026 07:14:35 +0000</pubDate>
</item>
<item>
<title>RAG Pipeline Deep Dive: Ingestion, Chunking, Embedding, and Vector Search</title>
<link>https://coderlegion.com/10137/rag-pipeline-deep-dive-ingestion-chunking-embedding-and-vector-search</link>
<description>&lt;p&gt;Retrieval-Augmented Generation (RAG) has become the default approach for providing context and memory to AI applications. Understanding the RAG pipeline—from document ingestion to final response generation—is essential for building high-performance AI systems. This comprehensive guide explores each stage of the RAG workflow, with special focus on Oracle Database 23ai's native vector capabilities.&lt;/p&gt;
&lt;h3 id=&quot;the-rag-pipeline-an-overview&quot;&gt;The RAG Pipeline: An Overview&lt;/h3&gt;
&lt;p&gt;A complete RAG pipeline consists of two main phases:&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;Ingestion Phase (Offline):&lt;/strong&gt;&lt;br&gt;
Documents → Chunking → Embedding → Index/Store in Vector Database&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;Retrieval Phase (Runtime):&lt;/strong&gt;&lt;br&gt;
User Query → Embed Query → Similarity Search → Top-K Results → LLM Generation → Response&lt;/p&gt;
&lt;p&gt;The RAG pipeline connects the model to the information it needs at query time. When a user asks a question, the pipeline retrieves the most relevant documents, prepares that text as context, and includes it in the prompt so the model can generate an answer grounded in retrieved material rather than training data alone.&lt;/p&gt;
&lt;h3 id=&quot;phase-1-document-ingestion&quot;&gt;Phase 1: Document Ingestion&lt;/h3&gt;
&lt;p&gt;Ingestion is the process of loading documents from multiple sources and in multiple formats, transforming them into a structured form suitable for embedding and retrieval.&lt;/p&gt;
&lt;h4 id=&quot;document-loading&quot;&gt;Document Loading&lt;/h4&gt;
&lt;p&gt;Raw documents—whether PDFs, Word documents, web pages, or database records—must be transformed into manageable chunks suitable for retrieval.&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;Common Sources:&lt;/strong&gt;&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;Local files (PDF, DOCX, TXT, CSV)&lt;/li&gt;
&lt;li&gt;Web pages and APIs&lt;/li&gt;
&lt;li&gt;Databases and data warehouses&lt;/li&gt;
&lt;li&gt;Cloud storage (S3, GCS, Azure Blob)&lt;/li&gt;
&lt;li&gt;Enterprise systems (SharePoint, Confluence, Notion)&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;&lt;strong&gt;Loading Libraries:&lt;/strong&gt;&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;&lt;strong&gt;PyPDF2&lt;/strong&gt;: For PDF documents&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;python-docx&lt;/strong&gt;: For Word documents&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;LangChain Document Loaders&lt;/strong&gt;: Unified interface for multiple formats&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;LlamaIndex Data Connectors&lt;/strong&gt;: Specialized loaders for various sources&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Oracle OracleDocLoader&lt;/strong&gt;: Native loader for Oracle Database 23ai&lt;/li&gt;
&lt;/ul&gt;
&lt;h4 id=&quot;text-extraction-and-cleaning&quot;&gt;Text Extraction and Cleaning&lt;/h4&gt;
&lt;p&gt;A clean corpus improves both retrieval accuracy and embedding quality. Use parsing libraries to extract only the primary content from web pages or PDFs, and remove boilerplate, navigation links, and disclaimers.&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;Cleaning Steps:&lt;/strong&gt;&lt;/p&gt;
&lt;ol&gt;
&lt;li&gt;&lt;strong&gt;Remove Noise&lt;/strong&gt;: Headers, footers, navigation, ads&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Normalize Structure&lt;/strong&gt;: Convert to consistent format (JSONL, Parquet)&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Preserve Metadata&lt;/strong&gt;: Source, timestamp, author, document type&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Handle Special Characters&lt;/strong&gt;: Normalize encoding (UTF-8)&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Maintain Logical Structure&lt;/strong&gt;: Keep headings, lists, tables&lt;/li&gt;
&lt;/ol&gt;
&lt;h4 id=&quot;metadata-enrichment&quot;&gt;Metadata Enrichment&lt;/h4&gt;
&lt;p&gt;Each chunk should carry metadata such as topic, date, and source, so that retrieval can filter and rank results more effectively.&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;Important Metadata Fields:&lt;/strong&gt;&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;&lt;strong&gt;source_id&lt;/strong&gt;: Unique document identifier&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;title&lt;/strong&gt;: Document title&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;created_date&lt;/strong&gt;: Document creation timestamp&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;author&lt;/strong&gt;: Document creator&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;document_type&lt;/strong&gt;: PDF, email, webpage&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;topic/category&lt;/strong&gt;: For domain filtering&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;page_number&lt;/strong&gt;: For PDF sources&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;chunk_id&lt;/strong&gt;: Unique identifier for the chunk&lt;/li&gt;
&lt;/ul&gt;
&lt;h3 id=&quot;phase-2-chunking---the-critical-step&quot;&gt;Phase 2: Chunking - The Critical Step&lt;/h3&gt;
&lt;p&gt;Chunking is simply the act of splitting larger documents into smaller units (&quot;chunks&quot;). Each chunk can be individually indexed, embedded, and retrieved.&lt;/p&gt;
&lt;h4 id=&quot;why-chunking-matters&quot;&gt;Why Chunking Matters&lt;/h4&gt;
&lt;p&gt;The chunking process directly feeds into the embedding step, where each chunk is converted into vector representations that capture semantic meaning.&lt;/p&gt;
&lt;p&gt;A common source of inaccurate or unstable answers lies in a structural conflict within the traditional &quot;chunk-embed-retrieve&quot; pipeline: using a single-granularity, fixed-size text chunk to perform two inherently conflicting tasks:&lt;/p&gt;
&lt;ol&gt;
&lt;li&gt;&lt;strong&gt;Semantic matching (recall)&lt;/strong&gt;: Smaller chunks (100-256 tokens) needed for precise similarity search&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Context understanding (utilization)&lt;/strong&gt;: Larger chunks (1024+ tokens) needed for coherent LLM generation&lt;/li&gt;
&lt;/ol&gt;
&lt;p&gt;This creates a difficult trade-off between &quot;precise but fragmented&quot; and &quot;complete but vague.&quot;&lt;/p&gt;
&lt;h4 id=&quot;pre-chunking-vs-post-chunking&quot;&gt;Pre-Chunking vs Post-Chunking&lt;/h4&gt;
&lt;p&gt;Pre-chunking is the most common method. It processes documents asynchronously by breaking them into smaller pieces before embedding and storing them in the vector database. This enables fast retrieval at query time since all chunks are pre-computed.&lt;/p&gt;
&lt;p&gt;Post-chunking takes a different approach by embedding entire documents first, then performing chunking at query time only on the documents that are actually retrieved. The chunked results can be cached, so the system becomes faster over time.&lt;/p&gt;
&lt;h4 id=&quot;chunking-strategies&quot;&gt;Chunking Strategies&lt;/h4&gt;
&lt;p&gt;Fixed-Length Chunking: This is the simplest approach, segmenting text into equally sized pieces using character, token, or word counts.&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;Common Chunk Sizes:&lt;/strong&gt;&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;&lt;strong&gt;300-500 tokens&lt;/strong&gt;: Typical chunks for most use cases, depending on the model and content type&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;100-256 tokens&lt;/strong&gt;: For high-precision recall in semantic search&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;1024+ tokens&lt;/strong&gt;: For providing complete context to LLMs&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;Adopt recursive/semantic chunkers with 10-20% overlap to maintain context across boundaries.&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;Advanced Chunking Approaches:&lt;/strong&gt;&lt;/p&gt;
&lt;p&gt;Recursive Chunking: This method is a bit smarter. It splits text hierarchically based on cues like paragraphs, line breaks, or sentences. If a section is too long, it recursively divides it further.&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;Semantic Chunking&lt;/strong&gt;: Instead of letting token limits decide where ideas get chopped, it uses embeddings to detect where the meaning actually shifts—grouping semantically related sentences together.&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;Document-Aware Chunking&lt;/strong&gt;: Preserves document structure (sections, paragraphs, tables) while chunking.&lt;/p&gt;
&lt;h4 id=&quot;chunk-overlap&quot;&gt;Chunk Overlap&lt;/h4&gt;
&lt;p&gt;&lt;strong&gt;Chunk overlap is the number of overlapping characters (or tokens) between adjacent chunks&lt;/strong&gt;. This ensures that context isn't lost when information spans chunk boundaries.&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;Recommended Overlap:&lt;/strong&gt;&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;10-20% overlap is standard practice&lt;/li&gt;
&lt;li&gt;For 500-token chunks, use 50-100 token overlap&lt;/li&gt;
&lt;li&gt;Maintains continuity without excessive redundancy&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;&lt;strong&gt;Example:&lt;/strong&gt;&lt;/p&gt;
&lt;pre&gt;&lt;code&gt;Chunk 1: [Tokens 0-500]
Chunk 2: [Tokens 450-950]  ← 50 token overlap
Chunk 3: [Tokens 900-1400] ← 50 token overlap
&lt;/code&gt;&lt;/pre&gt;
&lt;h4 id=&quot;splitting-process&quot;&gt;Splitting Process&lt;/h4&gt;
&lt;p&gt;Documents are split recursively until the chunks are small enough, using text splitters that respect:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;Sentence boundaries (don't break mid-sentence)&lt;/li&gt;
&lt;li&gt;Paragraph boundaries (maintain logical grouping)&lt;/li&gt;
&lt;li&gt;Section boundaries (preserve document structure)&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;&lt;strong&gt;Popular Chunking Libraries:&lt;/strong&gt;&lt;/p&gt;
&lt;p&gt;The LLM community often turns to two powerful open-source libraries: LangChain and LlamaIndex:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;&lt;strong&gt;LangChain&lt;/strong&gt;: Broad framework with flexible chunking strategies&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;LlamaIndex&lt;/strong&gt;: Designed specifically for RAG pipelines with sophisticated NodeParsers&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Chonkie&lt;/strong&gt;: Lightweight, dedicated chunking library focusing solely on text splitting&lt;/li&gt;
&lt;/ul&gt;
&lt;h3 id=&quot;phase-3-embedding-and-storage&quot;&gt;Phase 3: Embedding and Storage&lt;/h3&gt;
&lt;p&gt;Chunking makes information retrievable; embeddings make it searchable by meaning rather than keywords.&lt;/p&gt;
&lt;h4 id=&quot;what-are-embeddings-&quot;&gt;What Are Embeddings?&lt;/h4&gt;
&lt;p&gt;&lt;strong&gt;Embeddings are numerical representations that capture semantic relationships&lt;/strong&gt; between text. A piece of text—whether a word, phrase, sentence, or paragraph—is converted to a vector of numbers (typically 384-1536 dimensions).&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;Key Properties:&lt;/strong&gt;&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;&lt;strong&gt;Semantic similarity&lt;/strong&gt;: Embeddings that are numerically similar are also semantically similar&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Dense representation&lt;/strong&gt;: High-dimensional vectors capture nuanced meaning&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Model-specific&lt;/strong&gt;: Different embedding models produce different vector spaces&lt;/li&gt;
&lt;/ul&gt;
&lt;h4 id=&quot;embedding-models&quot;&gt;Embedding Models&lt;/h4&gt;
&lt;p&gt;&lt;strong&gt;Popular Options:&lt;/strong&gt;&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;&lt;strong&gt;OpenAI text-embedding-3-large&lt;/strong&gt;: 3072 dimensions, high quality&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Cohere embed-v4.0&lt;/strong&gt;: 1024 dimensions, multimodal (text + images)&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Sentence-BERT (SBERT)&lt;/strong&gt;: 768 dimensions, open-source&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;all-MiniLM-L6-v2&lt;/strong&gt;: 384 dimensions, lightweight and fast&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;Choose MiniLM/SBERT/Instruct/E5 for text, CLIP for images. Normalize embeddings for consistent similarity calculations.&lt;/p&gt;
&lt;h4 id=&quot;embedding-generation-outside-vs-inside-the-database&quot;&gt;Embedding Generation: Outside vs Inside the Database&lt;/h4&gt;
&lt;h5 id=&quot;option-1-generate-embeddings-outside-oracle-database-23ai&quot;&gt;Option 1: Generate Embeddings Outside Oracle Database 23ai&lt;/h5&gt;
&lt;p&gt;&lt;strong&gt;Vector embeddings can be generated outside Oracle Database 23ai using third-party embedding models&lt;/strong&gt;, then imported into the database for storage and search.&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;Workflow:&lt;/strong&gt;&lt;/p&gt;
&lt;pre&gt;&lt;code class=&quot;language-python&quot;&gt;from sentence_transformers import SentenceTransformer

# Load embedding model
model = SentenceTransformer('all-MiniLM-L6-v2')

# Generate embeddings
chunks = [&quot;First chunk text&quot;, &quot;Second chunk text&quot;]
embeddings = model.encode(chunks)

# Store in Oracle Database 23ai
# INSERT INTO documents (text, embedding) VALUES (?, ?)
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;&lt;strong&gt;Advantages:&lt;/strong&gt;&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;Flexibility in choosing any embedding model&lt;/li&gt;
&lt;li&gt;Can use cloud-based embedding services&lt;/li&gt;
&lt;li&gt;Easy integration with existing ML pipelines&lt;/li&gt;
&lt;/ul&gt;
&lt;h5 id=&quot;option-2-generate-embeddings-inside-oracle-database-23ai&quot;&gt;Option 2: Generate Embeddings Inside Oracle Database 23ai&lt;/h5&gt;
&lt;p&gt;&lt;strong&gt;Embeddings can also be generated inside Oracle Database 23ai by downloading pretrained embedding models, converting them to ONNX format, and importing the ONNX format models into Database 23ai&lt;/strong&gt;.&lt;/p&gt;
&lt;p&gt;ONNX (Open Neural Network Exchange) is an open-source format designed to represent deep learning models. It aims to provide interoperability between different deep learning frameworks.&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;Process:&lt;/strong&gt;&lt;/p&gt;
&lt;ol&gt;
&lt;li&gt;&lt;strong&gt;Download Pretrained Model&lt;/strong&gt; from Hugging Face or similar source&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Convert to ONNX Format&lt;/strong&gt; using Oracle OML4Py utility&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Import into Database 23ai&lt;/strong&gt; using DBMS_VECTOR package&lt;/li&gt;
&lt;/ol&gt;
&lt;p&gt;The command can be run to import the ONNX file into the database:&lt;/p&gt;
&lt;pre&gt;&lt;code class=&quot;language-sql&quot;&gt;EXEC dbms_vector.load_onnx_model(
  'DM_DUMP',
  'all_MiniLM_L12_v2.onnx',
  'minilm_l12_v2',
  JSON('{&quot;function&quot;: &quot;embedding&quot;, &quot;embeddingOutput&quot;: &quot;embedding&quot;, &quot;input&quot;: {&quot;input&quot;: [&quot;DATA&quot;]}}')
);
&lt;/code&gt;&lt;/pre&gt;
&lt;ol&gt;
&lt;li&gt;&lt;strong&gt;Generate Embeddings in SQL&lt;/strong&gt;:&lt;/li&gt;
&lt;/ol&gt;
&lt;pre&gt;&lt;code class=&quot;language-sql&quot;&gt;SELECT VECTOR_EMBEDDING(minilm_l12_v2 USING 'Your text here' AS data) AS embedding
FROM DUAL;
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;&lt;strong&gt;Advantages:&lt;/strong&gt;&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;Oracle Database implements an ONNX runtime directly within the database. This allows you to generate vector embeddings directly within Oracle Database using SQL&lt;/li&gt;
&lt;li&gt;Eliminates data movement between systems&lt;/li&gt;
&lt;li&gt;Simplified architecture and security&lt;/li&gt;
&lt;li&gt;Native integration with Oracle's vector capabilities&lt;/li&gt;
&lt;/ul&gt;
&lt;h3 id=&quot;phase-4-vector-indexes-for-fast-search&quot;&gt;Phase 4: Vector Indexes for Fast Search&lt;/h3&gt;
&lt;p&gt;Once embeddings are generated and stored, &lt;strong&gt;vector indexes are specialized data structures designed for similarity searches in high-dimensional vector spaces&lt;/strong&gt;. They help accelerate vector similarity searches by reducing the number of distance calculations needed.&lt;/p&gt;
&lt;h4 id=&quot;why-vector-indexes-&quot;&gt;Why Vector Indexes?&lt;/h4&gt;
&lt;p&gt;Exact search to find the closest matches for a given vector are accurate but can be slow, since costly vector distance computations are needed for all vectors in a column.&lt;/p&gt;
&lt;p&gt;Vector indexes use techniques like clustering, partitioning, and neighbor graphs to group similar vectors together, reducing the search space dramatically.&lt;/p&gt;
&lt;h4 id=&quot;oracle-ai-vector-search-supported-index-types&quot;&gt;Oracle AI Vector Search: Supported Index Types&lt;/h4&gt;
&lt;p&gt;Oracle AI Vector Search supports Hierarchical Navigable Small World (HNSW) indexes and Inverted File (IVF) indexes.&lt;/p&gt;
&lt;h5 id=&quot;1-hnsw-in-memory-neighbor-graph&quot;&gt;1. HNSW: In-Memory Neighbor Graph&lt;/h5&gt;
&lt;p&gt;&lt;strong&gt;Hierarchical Navigable Small World (HNSW)&lt;/strong&gt; creates a navigable graph structure to locate similar vectors with exceptional speed and accuracy.&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;Characteristics:&lt;/strong&gt;&lt;/p&gt;
&lt;p&gt;HNSW is an In-Memory Neighbor Graph Vector Index fully built in-memory. You need to setup a new memory pool with VECTOR_MEMORY_SIZE in the SGA to accommodate it.&lt;/p&gt;
&lt;p&gt;Since the HNSW index consumes a lot of memory, Oracle Database 23ai makes this index available by allocating a memory area called Vector Pool.&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;Memory Calculation:&lt;/strong&gt;&lt;/p&gt;
&lt;p&gt;To roughly determine the memory size needed to store an HNSW index, use the following formula: 1.3 × number of vectors × number of dimensions × size of your vector dimension type (for example, a FLOAT32 is 4 bytes).&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;Creating HNSW Index:&lt;/strong&gt;&lt;/p&gt;
&lt;pre&gt;&lt;code class=&quot;language-sql&quot;&gt;-- Configure vector pool
ALTER SYSTEM SET vector_memory_size=800M SCOPE=SPFILE;
-- Restart required

-- Create HNSW index
CREATE VECTOR INDEX documents_hnsw_idx 
ON documents (embedding)
ORGANIZATION NEIGHBOR GRAPH
DISTANCE COSINE
WITH TARGET ACCURACY 95;
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;&lt;strong&gt;Performance:&lt;/strong&gt;&lt;/p&gt;
&lt;p&gt;HNSW enables sub-100ms retrieval at 95%+ recall—extremely fast for real-time applications.&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;Limitations:&lt;/strong&gt;&lt;/p&gt;
&lt;p&gt;DML operations (insert, update, delete) are not allowed after creating an HNSW index—designed for read-heavy workloads.&lt;/p&gt;
&lt;p&gt;HNSW indexes are not available in RAC environments.&lt;/p&gt;
&lt;h5 id=&quot;2-ivf-neighbor-partition-index&quot;&gt;2. IVF: Neighbor Partition Index&lt;/h5&gt;
&lt;p&gt;&lt;strong&gt;Inverted File Flat (IVF)&lt;/strong&gt; indexes partition data points into clusters, enabling efficient searching within relevant clusters.&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;Characteristics:&lt;/strong&gt;&lt;/p&gt;
&lt;p&gt;IVF Flat index is a Neighbor Partition Vector index built on disk, and its blocks are cached in the regular buffer cache.&lt;/p&gt;
&lt;p&gt;A significant advantage of an IVF vector index is that it is not constrained by the amount of memory available in the vector pool like an HNSW vector index is.&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;How IVF Works:&lt;/strong&gt;&lt;/p&gt;
&lt;p&gt;It is designed to enhance search efficiency by narrowing the search area through the use of neighbor partitions or clusters. The center of each partition, or the centroid, represents the average vector for each partition. By default, the number of partitions and their corresponding centroids is set to the square root of the dataset's size.&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;Creating IVF Index:&lt;/strong&gt;&lt;/p&gt;
&lt;pre&gt;&lt;code class=&quot;language-sql&quot;&gt;CREATE VECTOR INDEX documents_ivf_idx 
ON documents (embedding)
ORGANIZATION NEIGHBOR PARTITIONS
DISTANCE COSINE
WITH TARGET ACCURACY 95;
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;&lt;strong&gt;Advantages:&lt;/strong&gt;&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;Works with massive datasets&lt;/li&gt;
&lt;li&gt;Supports global and local indexes on partitioned tables&lt;/li&gt;
&lt;li&gt;DML operations can degrade accuracy over time. Periodically rebuilding the index may be necessary&lt;/li&gt;
&lt;li&gt;More memory-efficient than HNSW&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;&lt;strong&gt;Performance:&lt;/strong&gt;&lt;/p&gt;
&lt;p&gt;Although an IVF index won't be as fast as an equivalent HNSW index, it can be used for very large data sets and still provide excellent performance when compared to an exhaustive similarity search.&lt;/p&gt;
&lt;h5 id=&quot;3-hybrid-vector-indexes&quot;&gt;3. Hybrid Vector Indexes&lt;/h5&gt;
&lt;p&gt;Hybrid vector index combines full text and semantic search in one index, allowing users to easily index and query their documents using a combination of full text search and semantic vector search.&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;Use Case:&lt;/strong&gt;&lt;br&gt;
When you need to match both semantic meaning AND specific keywords/terms in a single query.&lt;/p&gt;
&lt;h4 id=&quot;gpu-acceleration&quot;&gt;GPU Acceleration&lt;/h4&gt;
&lt;p&gt;At Oracle CloudWorld 2024, Oracle demonstrated GPU-accelerated creation of vector embeddings and HNSW index creation using NVIDIA GPUs.&lt;/p&gt;
&lt;p&gt;When a user sends a request to create an HNSW vector index to Oracle Database, the database intelligently compiles and redirects the task to a Compute Offload Server process that leverages GPUs and the CAGRA algorithm from NVIDIA cuVS library to rapidly generate a graph index.&lt;/p&gt;
&lt;h3 id=&quot;phase-5-retrieval---finding-relevant-context&quot;&gt;Phase 5: Retrieval - Finding Relevant Context&lt;/h3&gt;
&lt;p&gt;At runtime, when a user submits a query, the retrieval phase locates the most relevant chunks to provide as context to the LLM.&lt;/p&gt;
&lt;h4 id=&quot;query-embedding&quot;&gt;Query Embedding&lt;/h4&gt;
&lt;p&gt;The system encodes the user's query with the same model used to embed documents during ingestion.&lt;/p&gt;
&lt;pre&gt;&lt;code class=&quot;language-sql&quot;&gt;-- Generate query embedding
SELECT VECTOR_EMBEDDING(minilm_l12_v2 USING 'What is RAG?' AS data) AS query_vector
FROM DUAL;
&lt;/code&gt;&lt;/pre&gt;
&lt;h4 id=&quot;similarity-search&quot;&gt;Similarity Search&lt;/h4&gt;
&lt;p&gt;The query vector is compared against all document vectors in the database using a distance metric.&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;Distance Metrics:&lt;/strong&gt;&lt;/p&gt;
&lt;p&gt;Common options include EUCLIDEAN, L2_SQUARED, COSINE, DOT, MANHATTAN, and HAMMING. You can specify the metric during index creation or within the search query itself.&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;&lt;strong&gt;Cosine Similarity&lt;/strong&gt;: Most common for text (measures angle between vectors)&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Euclidean Distance&lt;/strong&gt;: Straight-line distance in vector space&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Dot Product&lt;/strong&gt;: Efficient for normalized vectors&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;&lt;strong&gt;Performing Similarity Search:&lt;/strong&gt;&lt;/p&gt;
&lt;pre&gt;&lt;code class=&quot;language-sql&quot;&gt;SELECT 
  text,
  VECTOR_DISTANCE(embedding, :query_vector, COSINE) AS distance
FROM documents
ORDER BY distance
FETCH APPROX FIRST 5 ROWS ONLY;
&lt;/code&gt;&lt;/pre&gt;
&lt;h4 id=&quot;top-k-results&quot;&gt;Top-K Results&lt;/h4&gt;
&lt;p&gt;The system searches the index for the nearest vectors, retrieves the top-k chunks, and includes those chunks with the query in the prompt.&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;Choosing K:&lt;/strong&gt;&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;&lt;strong&gt;K=3-5&lt;/strong&gt;: For focused, specific queries&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;K=10-20&lt;/strong&gt;: For broader research queries&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;K=50+&lt;/strong&gt;: For comprehensive document analysis&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;&lt;strong&gt;Challenges:&lt;/strong&gt;&lt;/p&gt;
&lt;p&gt;Top-k returns near duplicates or shallow snippets, so the prompt lacks diversity. Retrieval misses proper nouns, IDs, or acronyms because they occur rarely.&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;Solutions:&lt;/strong&gt;&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;&lt;strong&gt;Reranking&lt;/strong&gt;: Rerank with cross-encoders (Cohere ReRank, ColBERT) when precision matters&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Hybrid Search&lt;/strong&gt;: Combine BM25 (keyword) with dense vectors to boost tail recall&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Metadata Filtering&lt;/strong&gt;: Filter by date, source, document type before retrieval&lt;/li&gt;
&lt;/ul&gt;
&lt;h4 id=&quot;monitoring-index-accuracy&quot;&gt;Monitoring Index Accuracy&lt;/h4&gt;
&lt;p&gt;A valuable feature is Oracle's provision of functions to report index accuracy based on query vectors used in approximate searches. This allows you to continuously monitor and optimize the accuracy of your vector search implementation.&lt;/p&gt;
&lt;h3 id=&quot;phase-6-generation---creating-the-final-response&quot;&gt;Phase 6: Generation - Creating the Final Response&lt;/h3&gt;
&lt;p&gt;Once relevant chunks are retrieved, they're combined with the user's query and sent to an LLM for response generation.&lt;/p&gt;
&lt;h4 id=&quot;prompt-assembly&quot;&gt;Prompt Assembly&lt;/h4&gt;
&lt;p&gt;Retrieved chunks are formatted into a coherent context block:&lt;/p&gt;
&lt;pre&gt;&lt;code&gt;Context:
[Chunk 1 text]
[Chunk 2 text]
[Chunk 3 text]

Question: [User query]

Answer based on the provided context:
&lt;/code&gt;&lt;/pre&gt;
&lt;h4 id=&quot;llm-generation&quot;&gt;LLM Generation&lt;/h4&gt;
&lt;p&gt;The complete prompt (context + query) is sent to the LLM, which generates a response grounded in the retrieved information rather than relying solely on parametric knowledge.&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;Benefits:&lt;/strong&gt;&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;Reduces hallucinations&lt;/li&gt;
&lt;li&gt;Provides traceable sources&lt;/li&gt;
&lt;li&gt;Enables up-to-date information&lt;/li&gt;
&lt;li&gt;Maintains factual accuracy&lt;/li&gt;
&lt;/ul&gt;
&lt;h3 id=&quot;best-practices-for-production-rag-pipelines&quot;&gt;Best Practices for Production RAG Pipelines&lt;/h3&gt;
&lt;h4 id=&quot;1-optimize-chunking-strategy&quot;&gt;1. Optimize Chunking Strategy&lt;/h4&gt;
&lt;p&gt;Align chunk size with context windows, use task-specific sentence transformers, and default to HNSW + metadata filtering for sub-100ms retrieval at 95%+ recall.&lt;/p&gt;
&lt;h4 id=&quot;2-choose-appropriate-embedding-models&quot;&gt;2. Choose Appropriate Embedding Models&lt;/h4&gt;
&lt;ul&gt;
&lt;li&gt;Match embedding dimensions to your dataset size&lt;/li&gt;
&lt;li&gt;Consider domain-specific models for specialized content&lt;/li&gt;
&lt;li&gt;Test multiple models and measure retrieval quality&lt;/li&gt;
&lt;/ul&gt;
&lt;h4 id=&quot;3-implement-proper-indexing&quot;&gt;3. Implement Proper Indexing&lt;/h4&gt;
&lt;ul&gt;
&lt;li&gt;&lt;strong&gt;Small-Medium datasets (&amp;lt; 1M vectors)&lt;/strong&gt;: HNSW for maximum speed&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Large datasets (1M+ vectors)&lt;/strong&gt;: IVF for scalability&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Keyword + Semantic&lt;/strong&gt;: Hybrid vector indexes&lt;/li&gt;
&lt;/ul&gt;
&lt;h4 id=&quot;4-monitor-and-iterate&quot;&gt;4. Monitor and Iterate&lt;/h4&gt;
&lt;p&gt;Monitor recall@k, latency, and index memory; re-embed on model upgrades.&lt;/p&gt;
&lt;p&gt;Track metrics:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;Retrieval accuracy (precision@k, recall@k)&lt;/li&gt;
&lt;li&gt;Query latency (p50, p95, p99)&lt;/li&gt;
&lt;li&gt;Index build time&lt;/li&gt;
&lt;li&gt;Storage requirements&lt;/li&gt;
&lt;/ul&gt;
&lt;h4 id=&quot;5-handle-edge-cases&quot;&gt;5. Handle Edge Cases&lt;/h4&gt;
&lt;ul&gt;
&lt;li&gt;Query reformulation for better retrieval&lt;/li&gt;
&lt;li&gt;Fallback strategies when retrieval fails&lt;/li&gt;
&lt;li&gt;Confidence thresholds for generated responses&lt;/li&gt;
&lt;li&gt;Citation and source tracking&lt;/li&gt;
&lt;/ul&gt;
&lt;h3 id=&quot;advanced-rag-techniques&quot;&gt;Advanced RAG Techniques&lt;/h3&gt;
&lt;h4 id=&quot;1-self-correction-crag-&quot;&gt;1. Self-Correction (CRAG)&lt;/h4&gt;
&lt;p&gt;When strong retrieval pipelines return incomplete or irrelevant context, CRAG adds a lightweight feedback loop: before generating, the system checks whether the retrieved set is good enough. If it looks weak, the system triggers another retrieval pass.&lt;/p&gt;
&lt;h4 id=&quot;2-agentic-rag&quot;&gt;2. Agentic RAG&lt;/h4&gt;
&lt;p&gt;Agentic RAG systems plan retrieval strategies and iterate based on partial results—dynamically deciding what to retrieve next.&lt;/p&gt;
&lt;h4 id=&quot;3-graphrag&quot;&gt;3. GraphRAG&lt;/h4&gt;
&lt;p&gt;Build an advanced RAG system by first structuring data into a knowledge graph, then layering graph-aware retrieval to capture entity relationships.&lt;/p&gt;
&lt;h4 id=&quot;4-multimodal-rag&quot;&gt;4. Multimodal RAG&lt;/h4&gt;
&lt;p&gt;Combining text, images, audio, and video retrieval for richer context.&lt;/p&gt;
&lt;p&gt;A robust RAG pipeline requires careful attention to every stage:&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;Ingestion&lt;/strong&gt;: Clean, structured data with rich metadata&lt;br&gt;
&lt;strong&gt;Chunking&lt;/strong&gt;: Balanced chunk sizes with appropriate overlap (10-20%)&lt;br&gt;
&lt;strong&gt;Embedding&lt;/strong&gt;: Semantic representations using appropriate models&lt;br&gt;
&lt;strong&gt;Indexing&lt;/strong&gt;: Fast similarity search with HNSW or IVF indexes&lt;br&gt;
&lt;strong&gt;Retrieval&lt;/strong&gt;: Accurate top-K results with metadata filtering&lt;br&gt;
&lt;strong&gt;Generation&lt;/strong&gt;: Grounded responses with proper citations&lt;/p&gt;
&lt;p&gt;Oracle Database 23ai provides a complete, integrated platform for RAG with native vector support, ONNX model runtime, advanced indexing (HNSW, IVF, Hybrid), and SQL-native operations—eliminating the need for separate vector databases.&lt;/p&gt;
&lt;p&gt;By mastering these components and following best practices, you can build production-grade RAG systems that deliver accurate, relevant, and trustworthy AI-powered experiences.&lt;/p&gt;
</description>
<category>Articles</category>
<guid isPermaLink="true">https://coderlegion.com/10137/rag-pipeline-deep-dive-ingestion-chunking-embedding-and-vector-search</guid>
<pubDate>Wed, 21 Jan 2026 06:23:00 +0000</pubDate>
</item>
<item>
<title>Chat and Embedding Models in OCI Generative AI</title>
<link>https://coderlegion.com/8520/chat-and-embedding-models-in-oci-generative-ai</link>
<description>&lt;p&gt;Oracle Cloud Infrastructure (OCI) Generative AI provides two powerful categories of models: chat models for conversational AI and text generation, and embedding models for semantic understanding and search. Understanding how to configure and optimize these models is essential for building effective AI applications.&lt;/p&gt;
&lt;h3 id=&quot;understanding-tokens&quot;&gt;Understanding Tokens&lt;/h3&gt;
&lt;p&gt;Before diving into model parameters, it's crucial to understand that &lt;strong&gt;LLMs understand tokens rather than characters&lt;/strong&gt;. A token can be a word, part of a word, or even punctuation. As a general rule, estimate &lt;strong&gt;approximately 4 characters per token&lt;/strong&gt;—meaning 100 tokens is roughly 60-80 words.&lt;/p&gt;
&lt;p&gt;This matters because all LLM limits, costs, and parameters are expressed in tokens, not words or characters.&lt;/p&gt;
&lt;h3 id=&quot;chat-models-configuration-parameters&quot;&gt;Chat Models: Configuration Parameters&lt;/h3&gt;
&lt;p&gt;Chat models in OCI Generative AI offer extensive configuration options to control their behavior and output characteristics.&lt;/p&gt;
&lt;h4 id=&quot;max-output-tokens&quot;&gt;Max Output Tokens&lt;/h4&gt;
&lt;p&gt;The &lt;strong&gt;maximum number of tokens&lt;/strong&gt; that you want the model to generate per response. Each model has different maximum output token limits specified in their key features.&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;Important&lt;/strong&gt;: Because you're prompting a chat model, the response depends on the prompt and each response doesn't necessarily use up the maximum allocated tokens. Setting this parameter essentially creates a ceiling—the model will stop generating once it hits this limit.&lt;/p&gt;
&lt;h4 id=&quot;preamble-override&quot;&gt;Preamble Override&lt;/h4&gt;
&lt;p&gt;An &lt;strong&gt;initial context or guiding message&lt;/strong&gt; for a chat model that changes the model's overall chat behavior and conversation style. When you don't provide a preamble, the model uses its default.&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;Default Preamble for Cohere Command R Family:&lt;/strong&gt;&lt;/p&gt;
&lt;pre&gt;&lt;code&gt;You are Command. You are an extremely capable large language model built by Cohere. 
You are given instructions programmatically via an API that you follow to the best 
of your ability.
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;&lt;strong&gt;Custom Preamble Example:&lt;/strong&gt;&lt;/p&gt;
&lt;pre&gt;&lt;code&gt;You are a helpful assistant specialized in Italian cuisine. 
Respond with enthusiasm and include regional context when discussing dishes.
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;With this preamble override, when asked about cannolis, the model responds with regional details about Sicily and specific bakeries like Caffe Sierra and Pasticceria Cappello.&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;Alternative Approach&lt;/strong&gt;: You can also include preamble-like instructions directly in your conversation. For example: &quot;Answer the following question in a marketing tone. Where's the best place to go sailing?&quot;&lt;/p&gt;
&lt;h4 id=&quot;temperature&quot;&gt;Temperature&lt;/h4&gt;
&lt;p&gt;&lt;strong&gt;Temperature controls the randomness of the output&lt;/strong&gt;. It's one of the most important parameters for shaping model behavior.&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;How Temperature Works:&lt;/strong&gt;&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;Temperature modulates the probability distribution over words in the vocabulary&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Temperature = 0&lt;/strong&gt;: Makes the model deterministic, always selecting the word with the highest probability (essentially greedy decoding)&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Low temperature (&amp;lt; 1.0)&lt;/strong&gt;: Distribution is more peaked around the most likely tokens, producing focused, predictable outputs&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Temperature = 1.0&lt;/strong&gt;: Standard probability distribution with no modification&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;High temperature (&amp;gt; 1.0)&lt;/strong&gt;: Distribution flattens across all words, increasing randomness and creativity&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;&lt;strong&gt;Best Practices:&lt;/strong&gt;&lt;/p&gt;
&lt;p&gt;Start with temperature set to 0 or less than one, and increase it as you regenerate prompts for more creative output. Be cautious: high temperatures can introduce hallucinations and factually incorrect information.&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;Use Cases:&lt;/strong&gt;&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;&lt;strong&gt;Low temperature (0.0-0.3)&lt;/strong&gt;: Code generation, factual Q&amp;amp;A, technical documentation&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Medium temperature (0.5-0.7)&lt;/strong&gt;: General chatbots, customer service, balanced responses&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;High temperature (0.8-1.0+)&lt;/strong&gt;: Creative writing, brainstorming, diverse idea generation&lt;/li&gt;
&lt;/ul&gt;
&lt;h4 id=&quot;top-k-sampling&quot;&gt;Top-K Sampling&lt;/h4&gt;
&lt;p&gt;&lt;strong&gt;Top-K&lt;/strong&gt; tells the model to pick the next token from the &lt;strong&gt;top K tokens&lt;/strong&gt; in its list sorted by probability.&lt;/p&gt;
&lt;p&gt;A higher value for K generates more random output, making the text sound more natural. The parameter essentially limits the selection pool to the K most likely candidates at each step.&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;Range:&lt;/strong&gt;&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;&lt;strong&gt;Minimum&lt;/strong&gt;: 1 (considers only the single most probable token, essentially deterministic)&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Maximum&lt;/strong&gt;: Vocabulary size (considers all possible tokens)&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;OCI Default&lt;/strong&gt;: 0 for Command models, -1 for Llama models (meaning the model considers all tokens and doesn't use this method)&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;&lt;strong&gt;Example&lt;/strong&gt;: If K=3 and the top three tokens are:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;&quot;favorite&quot; (40% probability)&lt;/li&gt;
&lt;li&gt;&quot;best&quot; (30% probability)  &lt;/li&gt;
&lt;li&gt;&quot;preferred&quot; (15% probability)&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;The model randomly selects from only these three options, weighted by their probabilities.&lt;/p&gt;
&lt;h4 id=&quot;top-p-nucleus-sampling-&quot;&gt;Top-P (Nucleus Sampling)&lt;/h4&gt;
&lt;p&gt;&lt;strong&gt;Top-P&lt;/strong&gt; is similar to Top-K but &lt;strong&gt;picks from top tokens based on the sum of their probabilities&lt;/strong&gt;. It's more dynamic than Top-K because the number of tokens considered varies.&lt;/p&gt;
&lt;p&gt;Top-P selects tokens whose cumulative probability exceeds a threshold (p), limiting the pool to the smallest set meeting this sum.&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;How It Works:&lt;/strong&gt;&lt;/p&gt;
&lt;p&gt;Top-P picks the next token based on cumulative probability, ensuring the sum of probabilities is below a certain threshold.&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;Example&lt;/strong&gt;: If Top-P = 0.5 and tokens have probabilities:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;Token A: 0.3 (30%)&lt;/li&gt;
&lt;li&gt;Token B: 0.2 (20%)&lt;/li&gt;
&lt;li&gt;Token C: 0.1 (10%)&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;The model only considers A and B (cumulative 50%), excluding C.&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;Range:&lt;/strong&gt;&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;&lt;strong&gt;Minimum&lt;/strong&gt;: 0.0 (considers only the single most probable token)&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Maximum&lt;/strong&gt;: 1.0 (considers all tokens)&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;A high top-p value means the model looks at more possible words, even less likely ones, making generated text more diverse.&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;Practical Guidelines:&lt;/strong&gt;&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;&lt;strong&gt;Top-P = 0.5&lt;/strong&gt;: Only considers words adding up to 50% probability—focused responses&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Top-P = 0.9&lt;/strong&gt;: Includes many more words—varied and original responses&lt;/li&gt;
&lt;/ul&gt;
&lt;h4 id=&quot;the-interaction-temperature-top-k-and-top-p&quot;&gt;The Interaction: Temperature, Top-K, and Top-P&lt;/h4&gt;
&lt;p&gt;The selection process works in stages: Top-K tokens with highest probabilities are sampled first, then tokens are further filtered based on Top-P, with the final token being selected using temperature.&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;Decision Framework:&lt;/strong&gt;&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;For tasks requiring accuracy and focus (like code completion), use lower temperature, higher Top-P/Top-K, and moderate max tokens&lt;/li&gt;
&lt;li&gt;For creative writing, experiment with higher temperature, lower Top-P/Top-K, and larger max token limits&lt;/li&gt;
&lt;/ul&gt;
&lt;h4 id=&quot;frequency-and-presence-penalties&quot;&gt;Frequency and Presence Penalties&lt;/h4&gt;
&lt;p&gt;These parameters are &lt;strong&gt;useful if you want to get rid of repetition in your outputs&lt;/strong&gt;.&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;Frequency Penalty:&lt;/strong&gt;&lt;br&gt;
A penalty assigned to a token when that token appears frequently. High penalties encourage fewer repeated tokens and produce more random output.&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;Presence Penalty:&lt;/strong&gt;&lt;br&gt;
A penalty assigned to each token when it appears in the output to encourage generating outputs with tokens that haven't been used.&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;Use Case&lt;/strong&gt;: If your model keeps repeating the same phrases or concepts, increase these penalties to encourage more diverse vocabulary and concepts.&lt;/p&gt;
&lt;h4 id=&quot;seed-parameter-reproducibility-&quot;&gt;Seed Parameter (Reproducibility)&lt;/h4&gt;
&lt;p&gt;Assigning a number for the seed parameter is similar to tagging the request with a number. The LLM aims to generate the same set of tokens for the same integer in consecutive requests.&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;Important Notes:&lt;/strong&gt;&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;Allowed values are integers; assigning large or small seed values doesn't affect the result&lt;/li&gt;
&lt;li&gt;Especially useful for debugging and testing&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Warning&lt;/strong&gt;: The seed parameter might not produce the same result long-term because model updates might invalidate the seed&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;&lt;strong&gt;OCI Limits:&lt;/strong&gt;&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;API: No maximum value&lt;/li&gt;
&lt;li&gt;Console: Maximum value is 9999&lt;/li&gt;
&lt;li&gt;Leave blank (Console) or null (API) to disable&lt;/li&gt;
&lt;/ul&gt;
&lt;h4 id=&quot;stop-sequences&quot;&gt;Stop Sequences&lt;/h4&gt;
&lt;p&gt;A sequence of characters—such as a word, phrase, newline (\n), or period—that tells the model when to stop the generated output.&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;Example&lt;/strong&gt;: If the stop sequence is a period (.), the model stops generating text once it reaches the end of the first sentence, even if the token limit is much higher.&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;Use Case&lt;/strong&gt;: Useful for controlling output length and format, especially when you want responses limited to single sentences or specific sections.&lt;/p&gt;
&lt;h4 id=&quot;likelihood-display&quot;&gt;Likelihood Display&lt;/h4&gt;
&lt;p&gt;Shows how likely a token is to follow the current generated token. Likelihood is defined by a number between -15 and 0, where more negative numbers mean less likely tokens.&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;Example&lt;/strong&gt;: The word &quot;favorite&quot; is more likely to be followed by &quot;food&quot; or &quot;book&quot; rather than &quot;zebra.&quot;&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;Important&lt;/strong&gt;: This parameter doesn't influence the generation process itself but serves as a diagnostic tool to help understand the model's behavior.&lt;/p&gt;
&lt;h3 id=&quot;embedding-models-converting-text-to-vectors&quot;&gt;Embedding Models: Converting Text to Vectors&lt;/h3&gt;
&lt;p&gt;Embedding models transform text into numerical representations that capture semantic meaning, enabling AI systems to understand relationships between words, sentences, and documents.&lt;/p&gt;
&lt;h4 id=&quot;what-are-embeddings--1&quot;&gt;What Are Embeddings?&lt;/h4&gt;
&lt;p&gt;&lt;strong&gt;Embeddings are numerical representations of text converted to number sequences&lt;/strong&gt;. A piece of text can be a phrase, sentence, or one or more paragraphs.&lt;/p&gt;
&lt;p&gt;A vector embedding is a mapping from input (like a word, list of words, or image) into a list of floating-point numbers. That list represents the input in the multidimensional embedding space of the model.&lt;/p&gt;
&lt;h4 id=&quot;oci-embedding-model-dimensions&quot;&gt;OCI Embedding Model Dimensions&lt;/h4&gt;
&lt;p&gt;OCI Generative AI embedding models transform each phrase, sentence, or paragraph into an array with 384 (light models) or 1024 numbers, depending on the embedding model selected.&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;Available Models:&lt;/strong&gt;&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;&lt;strong&gt;Cohere Embed English V3&lt;/strong&gt;: 1024 dimensions&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Cohere Embed English Light V3&lt;/strong&gt;: 384 dimensions  &lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Cohere Embed Multilingual V3&lt;/strong&gt;: 1024 dimensions&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Cohere Embed Multilingual Light V3&lt;/strong&gt;: 384 dimensions&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Cohere Embed 4&lt;/strong&gt;: Latest version supporting text and images&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;&lt;strong&gt;Trade-offs:&lt;/strong&gt;&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;&lt;strong&gt;Light models (384d)&lt;/strong&gt;: Faster processing, lower memory usage, suitable for most applications&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Standard models (1024d)&lt;/strong&gt;: Richer semantic representation, better for complex similarity tasks&lt;/li&gt;
&lt;/ul&gt;
&lt;h4 id=&quot;types-of-embeddings&quot;&gt;Types of Embeddings&lt;/h4&gt;
&lt;p&gt;&lt;strong&gt;Word Embeddings:&lt;/strong&gt;&lt;br&gt;
Capture properties and relationships of individual words. Word2vec was the most well-known embedding model for a long time, accepting only single words but very good at representing semantic meaning, typically outputting 300-dimensional vectors.&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;Sentence Embeddings:&lt;/strong&gt;&lt;br&gt;
Associate every sentence with a vector of numbers, capturing meaning at the sentence level rather than individual words.&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;Document Embeddings:&lt;/strong&gt;&lt;br&gt;
Represent entire paragraphs or documents as single vectors, useful for document similarity and classification tasks.&lt;/p&gt;
&lt;h4 id=&quot;semantic-similarity-the-core-principle&quot;&gt;Semantic Similarity: The Core Principle&lt;/h4&gt;
&lt;p&gt;&lt;strong&gt;Embeddings that are numerically similar are also semantically similar.&lt;/strong&gt; This is the fundamental principle that makes embeddings powerful.&lt;/p&gt;
&lt;p&gt;Similar words, documents, or images have vectors that are also similar. For example, &quot;basketball&quot; and &quot;baseball&quot; have embedding vectors much closer to each other than &quot;rainforest&quot;.&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;Measuring Similarity:&lt;/strong&gt;&lt;/p&gt;
&lt;p&gt;The most popular way to compare vectors is cosine similarity, which measures the cosine of the angle between two vectors in multi-dimensional space. The closer the vectors, the smaller the angle.&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;Other Distance Metrics:&lt;/strong&gt;&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;&lt;strong&gt;Euclidean distance&lt;/strong&gt;: Straight-line distance between vectors&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Dot product (inner product)&lt;/strong&gt;: Especially effective for unit vectors&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Manhattan distance&lt;/strong&gt;: Sum of absolute differences&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;Cosine similarity is popular for text embeddings, measuring the angle between vectors. Dot product can also work, especially for unit vectors, offering performance benefits in vector databases.&lt;/p&gt;
&lt;h4 id=&quot;embedding-model-evolution&quot;&gt;Embedding Model Evolution&lt;/h4&gt;
&lt;p&gt;Word2vec was the pioneer, focusing on single words with 300 dimensions—lightweight and great for semantic meaning. Then came OpenAI's text-embedding-ada-002 in 2022, supporting up to 8192 tokens and outputting 1536 dimensions.&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;2025 State of the Art:&lt;/strong&gt;&lt;/p&gt;
&lt;p&gt;Advances in 2025, driven by LLMs and benchmarks, show transformer-based and instruction-tuned embeddings achieving top performance. Key trends include multilingual models (1000+ languages), domain-specific models (medicine, code), and multimodal models (text-image-audio).&lt;/p&gt;
&lt;h4 id=&quot;embedding-use-cases&quot;&gt;Embedding Use Cases&lt;/h4&gt;
&lt;p&gt;&lt;strong&gt;1. Vector Databases and Semantic Search&lt;/strong&gt;&lt;/p&gt;
&lt;p&gt;The most common use case: Embeddings are mostly used for semantic searches where the search function focuses on the meaning of the text rather than finding results based on keywords.&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;Workflow:&lt;/strong&gt;&lt;/p&gt;
&lt;ol&gt;
&lt;li&gt;User query is converted to a vector representation using an embedding model&lt;/li&gt;
&lt;li&gt;Query vector is stored/compared in a vector database&lt;/li&gt;
&lt;li&gt;Similar vectors are retrieved from private content&lt;/li&gt;
&lt;li&gt;Retrieved content is sent to an LLM for response generation&lt;/li&gt;
&lt;li&gt;LLM sends response back to the user&lt;/li&gt;
&lt;/ol&gt;
&lt;p&gt;We use the embedding model to create vector embeddings for content we want to index. The vector embedding is inserted into the vector database with some reference to the original content.&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;2. Recommendation Systems&lt;/strong&gt;&lt;/p&gt;
&lt;p&gt;By embedding items (products, articles, movies) and user preferences, systems can recommend semantically similar items even if they don't share obvious keywords.&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;3. Text Classification and Clustering&lt;/strong&gt;&lt;/p&gt;
&lt;p&gt;Embeddings enable text classification and text clustering by grouping semantically similar items together.&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;Applications:&lt;/strong&gt;&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;Classifying support tickets by department&lt;/li&gt;
&lt;li&gt;Categorizing documents by topic&lt;/li&gt;
&lt;li&gt;Detecting duplicate or similar content&lt;/li&gt;
&lt;li&gt;Sentiment analysis grouping&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;&lt;strong&gt;4. Retrieval-Augmented Generation (RAG)&lt;/strong&gt;&lt;/p&gt;
&lt;p&gt;While the most common scenario for vector databases is retrieval-augmented generation (RAG), other possible use cases include recommendations, anomaly detection, and more.&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;5. Question Answering Systems&lt;/strong&gt;&lt;/p&gt;
&lt;p&gt;Create chatbots that respond to questions about your own data—for instance, a chatbot responding to employee questions on healthcare coverage. Hundreds of pages of documentation can be split into chunks, converted into embeddings, and searched based on vector similarity.&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;6. Semantic Caching&lt;/strong&gt;&lt;/p&gt;
&lt;p&gt;Reduce cost and latency of LLMs by caching LLM completions. LLM queries are compared using vector similarity—if a new query is similar enough to a previously cached query, the cached query is returned.&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;7. LLM Conversation Memory&lt;/strong&gt;&lt;/p&gt;
&lt;p&gt;Persist conversation history with an LLM as embeddings in a vector database. Applications can use vector search to pull relevant history or &quot;memories&quot; into LLM responses.&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;8. Cross-Lingual and Multimodal Search&lt;/strong&gt;&lt;/p&gt;
&lt;p&gt;We can use vector search across languages since embedding models are frequently trained on more than just English data, and we can use vector search with images if we use multimodal embedding models trained on both text and images.&lt;/p&gt;
&lt;h4 id=&quot;vector-databases-storage-and-retrieval&quot;&gt;Vector Databases: Storage and Retrieval&lt;/h4&gt;
&lt;p&gt;A vector database is a database that can store, manage, retrieve, and compare vectors.&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;Key Capabilities:&lt;/strong&gt;&lt;/p&gt;
&lt;p&gt;Vector databases are purpose-built to manage vector embeddings, offering data management (insert, delete, update), metadata storage and filtering, scalability with distributed processing, and real-time query performance.&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;Popular Vector Databases:&lt;/strong&gt;&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;Pinecone&lt;/li&gt;
&lt;li&gt;Weaviate&lt;/li&gt;
&lt;li&gt;Qdrant&lt;/li&gt;
&lt;li&gt;Milvus&lt;/li&gt;
&lt;li&gt;Chroma&lt;/li&gt;
&lt;li&gt;Oracle Database 23ai (with AI Vector Search)&lt;/li&gt;
&lt;li&gt;Azure Redis&lt;/li&gt;
&lt;li&gt;MyScale&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;&lt;strong&gt;Advanced Features:&lt;/strong&gt;&lt;/p&gt;
&lt;p&gt;Some vector databases can perform hybrid searches by first narrowing results based on characteristics or metadata before conducting vector search—making searches more effective and customizable.&lt;/p&gt;
&lt;h4 id=&quot;oci-embedding-model-input-requirements&quot;&gt;OCI Embedding Model Input Requirements&lt;/h4&gt;
&lt;p&gt;Input data for text embeddings must meet the following requirements: maximum of 96 inputs allowed for each run, with each input less than 512 tokens for text-only models.&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;Handling Long Inputs:&lt;/strong&gt;&lt;/p&gt;
&lt;p&gt;If an input exceeds the 512 token limit, you can set the Truncate parameter to Start or End to cut off text to fit within the token limit. Setting it to None will produce an error if inputs exceed limits.&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;Multimodal Support:&lt;/strong&gt;&lt;/p&gt;
&lt;p&gt;For text and image embed models like Cohere Embed English Image V3, you can add either text or one image only.&lt;/p&gt;
&lt;h3 id=&quot;best-practices-for-oci-generative-ai&quot;&gt;Best Practices for OCI Generative AI&lt;/h3&gt;
&lt;h4 id=&quot;chat-model-optimization&quot;&gt;Chat Model Optimization&lt;/h4&gt;
&lt;ol&gt;
&lt;li&gt;&lt;strong&gt;Start Simple&lt;/strong&gt;: Begin with default parameters, then adjust based on results&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Temperature First&lt;/strong&gt;: Adjust temperature before other sampling parameters&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Use Preambles Wisely&lt;/strong&gt;: Set clear behavioral expectations with custom preambles&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Implement Stop Sequences&lt;/strong&gt;: Control output format and length precisely&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Test Reproducibility&lt;/strong&gt;: Use seed parameter during development for consistent testing&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Monitor Penalties&lt;/strong&gt;: Add frequency/presence penalties if seeing repetition&lt;/li&gt;
&lt;/ol&gt;
&lt;h4 id=&quot;embedding-model-selection&quot;&gt;Embedding Model Selection&lt;/h4&gt;
&lt;ol&gt;
&lt;li&gt;&lt;strong&gt;Choose Appropriate Dimensions&lt;/strong&gt;: Use light models (384d) for most applications; standard models (1024d) for complex similarity tasks&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Select Right Model&lt;/strong&gt;: English vs. Multilingual based on your data&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Batch Process&lt;/strong&gt;: Take advantage of the 96-input limit for efficient processing&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Index Strategically&lt;/strong&gt;: Store embeddings in vector databases with appropriate indexes&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Measure Appropriately&lt;/strong&gt;: Use cosine similarity for most text applications&lt;/li&gt;
&lt;/ol&gt;
&lt;h4 id=&quot;integration-patterns&quot;&gt;Integration Patterns&lt;/h4&gt;
&lt;p&gt;OCI Generative AI integrates with LangChain for building context-augmented applications and RAG solutions. It also works with LlamaIndex for accessing pretrained models or creating custom models on dedicated AI clusters.&lt;/p&gt;
&lt;p&gt;Understanding chat and embedding models in OCI Generative AI enables you to build sophisticated AI applications. Chat models with properly tuned parameters (temperature, top-k, top-p, penalties) produce high-quality, contextually appropriate responses. Embedding models convert text into semantic vectors that power search, recommendations, and retrieval systems.&lt;/p&gt;
&lt;p&gt;Key takeaways:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;&lt;strong&gt;Tokens are the fundamental unit&lt;/strong&gt;—everything is measured in tokens, not characters&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Temperature controls creativity&lt;/strong&gt;—low for precision, high for creativity&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Sampling parameters work together&lt;/strong&gt;—Top-K and Top-P filter candidates before temperature applies&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Embeddings enable semantic understanding&lt;/strong&gt;—similar meanings produce similar vectors&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Vector databases make it practical&lt;/strong&gt;—efficient storage and retrieval of embeddings at scale&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;Whether building chatbots, implementing semantic search, or creating RAG applications, mastering these parameters and concepts is essential for production-grade AI systems on OCI.&lt;/p&gt;
</description>
<category>Deep Learning</category>
<guid isPermaLink="true">https://coderlegion.com/8520/chat-and-embedding-models-in-oci-generative-ai</guid>
<pubDate>Tue, 16 Dec 2025 08:45:24 +0000</pubDate>
</item>
</channel>
</rss>