By Fatskills Exam Guides Team — the exam nerds behind 28,500+ quizzes and 2.1M practice questions across 500+ global exams.
Retrieval Augmented Generation (RAG) is a hybrid AI pattern that combines information retrieval (fetching relevant documents) with generative AI (answering questions in natural language). Instead of relying solely on an LLM’s pre-trained knowledge (which may be outdated or incomplete), RAG dynamically pulls in up-to-date, domain-specific data from a knowledge base (e.g., PDFs, databases, or FAQs) to generate more accurate and context-aware responses.
Real-world scenario:A healthcare chatbot needs to answer patient questions about symptoms, treatments, and insurance coverage. Instead of fine-tuning an LLM on medical data (expensive and risky), the team uses Azure AI Search to index clinical guidelines, drug databases, and patient records. When a user asks, "What are the side effects of Drug X?", the system retrieves the latest FDA documentation and passes it to Azure OpenAI’s GPT-4 to generate a precise, cited response—without hallucinations.
Azure AI Search (formerly Azure Cognitive Search): Microsoft’s managed vector + keyword search service. Indexes structured and unstructured data (PDFs, Word docs, databases) and supports semantic search (understanding intent) and vector search (finding similar embeddings). Best for RAG pipelines where you need fast, scalable retrieval.
Azure OpenAI Service: Microsoft’s managed API for OpenAI models (GPT-4, GPT-3.5, DALL·E, etc.). Provides chat completions, embeddings, and fine-tuning (limited). Used in RAG to generate responses after retrieving relevant documents.
Embeddings: Numerical representations of text (or other data) that capture semantic meaning. Azure OpenAI’s text-embedding-ada-002 model converts text into vectors for vector search in Azure AI Search.
Vector Search: A search method that finds similar items by comparing embeddings (e.g., "Find documents about diabetes treatments"). Azure AI Search supports approximate nearest neighbor (ANN) search for low-latency retrieval.
Semantic Search: A search method that understands context and intent (e.g., "What’s the best medication for high blood pressure?" vs. "hypertension drugs"). Azure AI Search uses Microsoft’s semantic ranking to improve relevance.
Chunking: Splitting large documents into smaller chunks (e.g., paragraphs, sections) before indexing. Critical for RAG because LLMs have token limits (e.g., GPT-4 supports 8K–32K tokens). Poor chunking leads to context truncation or irrelevant retrievals.
Prompt Engineering: Crafting input prompts to guide an LLM’s output. In RAG, prompts include retrieved documents + user questions (e.g., "Answer the question using only the following context: [retrieved docs]. Question: [user input]").
Grounding: Ensuring an LLM’s response is based on retrieved data (not hallucinated). Azure OpenAI’s content filtering and prompt engineering help enforce grounding.
Indexer (Azure AI Search): A pipeline that automatically extracts, transforms, and loads (ETL) data from sources (Blob Storage, SQL DB, etc.) into an Azure AI Search index. Supports custom skills (e.g., OCR, entity recognition).
Skillset (Azure AI Search): A collection of AI enrichments (e.g., text translation, key phrase extraction, OCR) applied during indexing. Used to pre-process data before retrieval.
Retrieval Score: A metric (0–1) indicating how relevant a retrieved document is to the query. Azure AI Search returns scores for keyword and vector matches.
Hybrid Search: Combining keyword search (BM25) + vector search (ANN) for better recall. Azure AI Search supports hybrid queries out of the box.
id
content
metadata
Collection(Edm.Single)
from azure.search.documents.indexes import SearchIndexClient from azure.search.documents.indexes.models import * # Define index schema fields = [ SimpleField(name="id", type=SearchFieldDataType.String, key=True), SearchableField(name="content", type=SearchFieldDataType.String), SearchField(name="embedding", type=SearchFieldDataType.Collection(SearchFieldDataType.Single), vector_search_dimensions=1536, # For text-embedding-ada-002 vector_search_profile_name="my-vector-profile") ] # Create index index_client = SearchIndexClient(endpoint, credential) index = SearchIndex(name="rag-index", fields=fields) index_client.create_index(index)
text-embedding-ada-002
from openai import AzureOpenAI client = AzureOpenAI( api_key="YOUR_KEY", api_version="2023-05-15", azure_endpoint="YOUR_ENDPOINT" ) def generate_embeddings(text): response = client.embeddings.create(input=text, model="text-embedding-ada-002") return response.data[0].embedding
from azure.search.documents import SearchClient search_client = SearchClient(endpoint, "rag-index", credential) results = search_client.search( search_text="What are the side effects of Drug X?", # Keyword search vector_queries=[VectorizedQuery(vector=query_embedding, k_nearest_neighbors=3, fields="embedding")], # Vector search select=["content", "metadata"], top=3 # Return top 3 results )
"Answer the question using only the following context. If you don't know the answer, say 'I don't know.' Context: {document_1} {document_2} {document_3} Question: {user_question} Answer:"
response = client.chat.completions.create( model="gpt-4", messages=[ {"role": "system", "content": "You are a helpful assistant."}, {"role": "user", "content": prompt_template.format( document_1=results[0]["content"], document_2=results[1]["content"], document_3=results[2]["content"], user_question="What are the side effects of Drug X?" )} ] ) print(response.choices[0].message.content)
if score > 0.6
k_nearest_neighbors
Why? AI Search indexes the PDFs, OpenAI generates answers.
Q: "A team wants to improve search relevance for a legal document RAG system. They need to understand query intent. Which Azure AI Search feature should they enable?"
Join 4M+ learners. Unlock unlimited quizzes, wrong-answer tracking, flashcards + reminders, study guides, and 1-on-1 challenges.