0Pricing
LLM Apps in Production (RAG + Vector DB + Caching) · บทเรียน

การเขียนคำค้นใหม่และการจัดอันดับซ้ำ

สำรวจเทคนิคการปรับคำค้นของผู้ใช้ให้เหมาะสมและจัดอันดับเอกสารที่ค้นคืนได้ใหม่ เพื่อให้เกี่ยวข้องกับ LLM มากยิ่งขึ้น

การเขียนคำค้นใหม่และการจัดอันดับซ้ำ เป็นบทเรียน LLM Apps in Production (RAG + Vector DB + Caching) ฟรีบน CoddyKit นี่คือบทเรียนที่ 1 จากทั้งหมด 4 บทเรียน คุณสามารถอ่านบทเรียนทั้งหมดด้านล่างฟรี — จากนั้นลองปฏิบัติด้วยตัวคุณเองในเบราว์เซอร์พร้อมตัวแก้ไขโค้ดในตัวและติวเตอร์ AI ตลอด 24/7 บทเรียนนี้เป็นส่วนหนึ่งของเส้นทางการเรียน LLM Apps in Production (RAG + Vector DB + Caching) และความก้าวหน้าของคุณจะซิงค์ข้ามเว็บและแอป CoddyKit คอร์ส LLM Apps in Production (RAG + Vector DB + Caching) มีบทเรียนทั้งหมด 4 บทเรียน

บางส่วนของบทเรียนนี้ยังไม่ได้รับการแปล และแสดงเป็นภาษาอังกฤษ

Optimizing Queries for RAG

Welcome to advanced RAG techniques! In this lesson, we'll explore two powerful methods to make your Retrieval Augmented Generation (RAG) system even smarter: Query Rewriting and Reranking.

These techniques help ensure your LLM gets the most relevant information possible, leading to better and more accurate responses.

Why Raw Queries Fall Short

When a user asks a question, their initial query might not be perfect for searching your knowledge base. It could be:

  • Too short or vague: Lacking specific keywords.
  • Ambiguous: Having multiple possible meanings.
  • Missing synonyms: Not using the exact terms found in your documents.

This can lead to your retriever fetching less relevant documents.

Understanding Query Rewriting

Query rewriting is the process of modifying the user's original query before it's sent to your document retriever.

The goal is to transform the query into a more effective search term that is more likely to match relevant documents in your vector database.

Techniques for Rewriting Queries

Query rewriting can involve several strategies:

  • Query Expansion: Adding synonyms or related terms to broaden the search.
  • Query Rephrasing: Changing the query's structure or wording to improve clarity.
  • Query Decomposition: Breaking a complex, multi-part query into simpler, individual sub-queries.

Often, another LLM is used to perform these rewriting tasks.

Code: Simple Query Rewriting

Here's a conceptual Python example of how a simple query expansion might work. In a real system, an LLM would do the heavy lifting.

class QueryRewriter:
    def rewrite(self, query):
        # Simulate an LLM or a rule-based system
        if "LLM performance" in query:
            return query + " large language model efficiency optimization"
        if "vector db" in query:
            return query + " vector database semantic search"
        return query

rewriter = QueryRewriter()
user_query_1 = "improve LLM performance"
rewritten_1 = rewriter.rewrite(user_query_1)
print(f"Original 1: {user_query_1}")
print(f"Rewritten 1: {rewritten_1}\n")

user_query_2 = "how to use vector db"
rewritten_2 = rewriter.rewrite(user_query_2)
print(f"Original 2: {user_query_2}")
print(f"Rewritten 2: {rewritten_2}")

Why We Need Reranking

Even after a great initial search (perhaps with a rewritten query!), the top 'N' documents returned by your retriever might not be perfectly ordered by relevance.

The retriever's job is often to find potential matches. Reranking steps in to refine this order, ensuring the absolute best documents are at the very top.

The Reranking Process

Reranking works like this:

  1. Your initial retriever fetches a larger set of candidate documents (e.g., top 50).
  2. A specialized reranker model then takes each of these candidate documents, along with the original user query, and provides a more precise relevance score.
  3. The documents are then sorted again based on these new, more accurate scores.

This ensures the most relevant documents are passed to the LLM.

Specialized Reranking Models

Unlike a retriever that often uses embeddings for approximate similarity, rerankers typically use more sophisticated models, often called cross-encoders.

  • Cross-encoders take both the query AND a document as input.
  • They consider the interaction between the query and document terms directly.
  • This allows for a much more nuanced understanding of relevance, though it's computationally more intensive, hence why it's only applied to a smaller subset of documents.

Code: Simulating Reranking

This example shows how a reranker might re-score and reorder an initially retrieved list of documents based on their relevance to the query.

class Reranker:
    def rerank(self, query, documents):
        # Simulate a cross-encoder model scoring documents
        scores = {}
        for doc in documents:
            if "vector database" in doc.lower() and "fast" in query.lower():
                scores[doc] = 0.95 # Highly relevant
            elif "llm" in doc.lower() and "improve" in query.lower():
                scores[doc] = 0.85
            elif "database" in doc.lower():
                scores[doc] = 0.7
            else:
                scores[doc] = 0.3 # Less relevant
        
        # Sort documents by score in descending order
        sorted_docs = sorted(documents, key=lambda d: scores.get(d, 0), reverse=True)
        return sorted_docs

reranker = Reranker()
user_query = "How to build a fast vector database?"
initial_docs = [
    "Introduction to LLMs",
    "Building a scalable vector database",
    "Optimizing LLM inference",
    "Fast data ingestion for databases"
]

reranked_docs = reranker.rerank(user_query, initial_docs)
print(f"Original documents: {initial_docs}\n")
print("Reranked documents (most relevant first):")
for doc in reranked_docs:
    print(f"- {doc}")

The Power of Combination

The true power comes from combining both techniques:

  • First, Query Rewriting creates a better search query.
  • Then, your retriever uses this improved query to fetch a broader, more relevant set of documents.
  • Finally, Reranking fine-tunes the order of these documents, ensuring the LLM receives the absolute best context to generate a response.

This multi-stage approach significantly boosts the quality and accuracy of your RAG system.

Check Your Understanding

Time to test what you've learned about optimizing RAG through query rewriting and reranking.

Recap & Next Steps

Great job! In this lesson, you learned about Query Rewriting and Reranking.

  • Query Rewriting modifies the user's input to create a more effective search query.
  • Reranking reorders initially retrieved documents using a more precise model to surface the most relevant ones.

Together, these techniques significantly enhance the quality and accuracy of your RAG applications. Next, we'll explore even more advanced RAG architectures!

คำถามที่พบบ่อย

บทเรียน “การเขียนคำค้นใหม่และการจัดอันดับซ้ำ” ฟรีหรือไม่

ใช่ — ข้อความเต็มของ “การเขียนคำค้นใหม่และการจัดอันดับซ้ำ” ฟรีให้อ่านที่นี่บนเว็บ เพื่อปฏิบัติแบบโต้ตอบ (ตัวแก้ไขโค้ดในตัวและติวเตอร์ AI ตลอด 24/7) และปลดล็อคส่วนที่เหลือของคอร์ส LLM Apps in Production (RAG + Vector DB + Caching) ให้อัปเกรดเป็น CoddyKit PRO คอร์ส LLM Apps in Production (RAG + Vector DB + Caching) มีบทเรียนทั้งหมด 4 บทเรียน

คุณจะเรียนรู้อะไรในบทเรียน “การเขียนคำค้นใหม่และการจัดอันดับซ้ำ”

สำรวจเทคนิคการปรับคำค้นของผู้ใช้ให้เหมาะสมและจัดอันดับเอกสารที่ค้นคืนได้ใหม่ เพื่อให้เกี่ยวข้องกับ LLM มากยิ่งขึ้น คุณปฏิบัติ LLM Apps in Production (RAG + Vector DB + Caching) ด้วยโค้ดที่ใช้งานได้จริงที่คุณเรียกใช้โดยตรงในเบราว์เซอร์ และติวเตอร์ AI ตลอด 24/7 ตอบคำถามของคุณขณะที่คุณไปผ่านบทเรียน

คุณต้องมีประสบการณ์ก่อนที่จะเริ่มเรียน LLM Apps in Production (RAG + Vector DB + Caching) หรือไม่

ไม่จำเป็นต้องมีประสบการณ์มาก่อน LLM Apps in Production (RAG + Vector DB + Caching) บน CoddyKit ออกแบบมาสำหรับผู้เริ่มต้นไปจนถึงผู้เรียนขั้นสูง คุณสามารถเริ่มต้นที่นี่หรือเริ่มจากตัวแรกและเรียนด้วยความเร็วของคุณเอง นี่คือบทเรียนที่ 1 จากทั้งหมด 4 บทเรียน

บทเรียน “การเขียนคำค้นใหม่และการจัดอันดับซ้ำ” ใช้เวลานานแค่ไหน

บทเรียน CoddyKit ส่วนใหญ่ใช้เวลาประมาณ 5–10 นาที แต่ละบทเรียนจึงสั้นและเป็นแบบโต้ตอบ คุณสามารถก้าวหน้าอย่างต่อเนื่องและกลับมาเรียนต่อจากตรงที่เพิ่งหยุดบนเว็บและแอปได้เลย

ฉันเขียนและรันโค้ดในบทเรียน LLM Apps in Production (RAG + Vector DB + Caching) นี้ได้ไหม

ได้ บทเรียน LLM Apps in Production (RAG + Vector DB + Caching) ทุกบทมีตัวแก้ไขโค้ดในตัว คุณจึงเขียนและรันโค้ดจริงได้เลยในเบราว์เซอร์ และได้รับข้อเสนอแนะจาก AI ในทันที — ไม่ต้องติดตั้งในเครื่องของคุณ

บทเรียนทั้งหมดในหลักสูตรนี้

  1. การเขียนคำค้นใหม่และการจัดอันดับซ้ำ
  2. รูปแบบ RAG หลายขั้นตอนและแบบใช้เอเจนต์
  3. การจัดการโครงสร้างเอกสารที่ซับซ้อน
  4. การค้นหาด้วยตนเองและการอ้างอิงแหล่งที่มา
← กลับไปที่ LLM Apps in Production (RAG + Vector DB + Caching)