เทคนิคการแปลงคำค้น
สำรวจวิธีเรียบเรียงใหม่หรือขยายคำค้นของผู้ใช้ เพื่อให้ค้นคืนข้อมูลจากฐานข้อมูลเวกเตอร์ได้อย่างมีประสิทธิภาพยิ่งขึ้น
เทคนิคการแปลงคำค้น เป็นบทเรียน Vector Databases: Pinecone, Weaviate & pgvector ฟรีบน CoddyKit นี่คือบทเรียนที่ 1 จากทั้งหมด 4 บทเรียน คุณสามารถอ่านบทเรียนทั้งหมดด้านล่างฟรี — จากนั้นลองปฏิบัติด้วยตัวคุณเองในเบราว์เซอร์พร้อมตัวแก้ไขโค้ดในตัวและติวเตอร์ AI ตลอด 24/7 บทเรียนนี้เป็นส่วนหนึ่งของเส้นทางการเรียน Vector Databases: Pinecone, Weaviate & pgvector และความก้าวหน้าของคุณจะซิงค์ข้ามเว็บและแอป CoddyKit คอร์ส Vector Databases: Pinecone, Weaviate & pgvector มีบทเรียนทั้งหมด 4 บทเรียน
บางส่วนของบทเรียนนี้ยังไม่ได้รับการแปล และแสดงเป็นภาษาอังกฤษ
What's Query Transformation?
In Retrieval Augmented Generation (RAG), the quality of the information retrieved from your vector database directly impacts the LLM's response. Sometimes, the raw user query isn't ideal for retrieval.
Query transformation is the process of modifying a user's original query to make it more effective for searching your vector database.
Why Original Queries Fall Short
User queries can be:
- Too short or vague: Lacking enough detail for precise retrieval.
- Ambiguous: Open to multiple interpretations.
- Colloquial: Using informal language that doesn't match document embeddings.
- Complex: Asking multiple questions at once.
These issues lead to irrelevant context being retrieved, impacting RAG quality.
Bridging the Semantic Gap
Your vector database stores information as numerical vectors (embeddings). For effective retrieval, the query's embedding needs to be semantically similar to the embeddings of relevant documents.
Query transformation helps by:
- Adding more context.
- Clarifying intent.
- Aligning query terms with document vocabulary.
This "bridges the semantic gap" for better matches.
Technique 1: Query Expansion
Query expansion involves adding more terms or phrases to the original query. This makes the search broader and increases the chances of hitting relevant documents.
Common methods include:
- Adding synonyms.
- Including related concepts.
- Using domain-specific jargon.
This technique is especially useful for short, vague queries.
Simple Keyword Expansion
Here's a basic Python example where we manually expand a query with related terms. In real systems, an LLM might generate these expansions automatically.
def expand_query_keywords(query):
expansion_map = {
"AI": ["artificial intelligence", "machine learning", "deep learning"],
"vector DB": ["vector database", "embedding store", "Pinecone", "Weaviate"]
}
expanded_terms = []
for keyword, additions in expansion_map.items():
if keyword.lower() in query.lower():
expanded_terms.extend(additions)
return query + " " + " ".join(expanded_terms)
# Example usage
user_query = "What is AI?"
transformed_query = expand_query_keywords(user_query)
print(f"Original: {user_query}")
print(f"Transformed: {transformed_query}")Technique 2: Query Rewriting/Rephrasing
Query rewriting (or rephrasing) uses an LLM to generate a completely new query that is clearer, more specific, or better aligned with the expected content of your documents.
This is particularly effective for:
- Ambiguous questions.
- Conversational queries.
- Queries that implicitly refer to past turns in a conversation.
Rewriting with an LLM
This Python snippet demonstrates how you might use an LLM to rephrase a user query. In a real application, llm_api_call would interact with models like OpenAI GPT or a local LLM.
import os
# Placeholder for an actual LLM API call
def llm_api_call(prompt):
# For demonstration, we'll simulate a response.
if "rephrase" in prompt.lower() and "vector databases" in prompt.lower():
return "Explain the core components and function of vector databases."
return "Could not rephrase query effectively."
def rephrase_query_with_llm(original_query):
prompt = f"Rephrase the following user query to be more effective for searching a technical documentation database about vector databases: '{original_query}'"
rephrased_query = llm_api_call(prompt)
return rephrased_query
# Example usage
user_query = "Tell me about vector DBs."
transformed_query = rephrase_query_with_llm(user_query)
print(f"Original: {user_query}")
print(f"Transformed: {transformed_query}")Technique 3: Sub-Query Generation
When a user asks a complex question that involves multiple aspects, a single query might not retrieve all necessary information. Sub-query generation breaks down a complex query into several simpler, more focused queries.
Each sub-query can then be used to retrieve specific pieces of context, which are then combined for the LLM.
Generating Sub-Queries
Here's a conceptual Python example for generating sub-queries. An LLM is often used to parse the original query and identify distinct questions.
import os
# Placeholder for an actual LLM API call
def llm_api_call_sub_query(prompt):
if "break down" in prompt.lower() and "Pinecone" in prompt.lower():
return ["What is Pinecone?", "How do I upsert data into Pinecone?", "What is a Pinecone index?"]
return ["Could not break down query."]
def generate_sub_queries(original_query):
prompt = f"Break down the following complex user query into a list of simpler, distinct questions for retrieving information from a vector database: '{original_query}'"
sub_queries = llm_api_call_sub_query(prompt)
return sub_queries
# Example usage
user_query = "How do I use Pinecone for storing and querying embeddings?"
transformed_queries = generate_sub_queries(user_query)
print(f"Original: {user_query}")
print(f"Sub-queries: {transformed_queries}")Hybrid & Contextual Transformations
Effective RAG often combines these techniques. You might first rephrase a query, then expand it. Also, consider the conversational context.
- Multi-turn awareness: Use previous turns to enrich the current query.
- Hybrid approaches: Combine transformations with traditional keyword search.
Experimentation is key to finding what works best for your data!
Understanding Query Transformation
Which of the following scenarios would MOST benefit from using query expansion as a transformation technique?
Recap: Transforming Queries
You've learned that query transformation is crucial for improving RAG performance by making user queries more effective for vector database retrieval.
- Query Expansion: Adds terms to broaden search.
- Query Rewriting: Rephrases for clarity and specificity.
- Sub-Query Generation: Breaks down complex queries.
Mastering these techniques will significantly enhance the quality of your RAG applications!
คำถามที่พบบ่อย
บทเรียน “เทคนิคการแปลงคำค้น” ฟรีหรือไม่
ใช่ — ข้อความเต็มของ “เทคนิคการแปลงคำค้น” ฟรีให้อ่านที่นี่บนเว็บ เพื่อปฏิบัติแบบโต้ตอบ (ตัวแก้ไขโค้ดในตัวและติวเตอร์ AI ตลอด 24/7) และปลดล็อคส่วนที่เหลือของคอร์ส Vector Databases: Pinecone, Weaviate & pgvector ให้อัปเกรดเป็น CoddyKit PRO คอร์ส Vector Databases: Pinecone, Weaviate & pgvector มีบทเรียนทั้งหมด 4 บทเรียน
คุณจะเรียนรู้อะไรในบทเรียน “เทคนิคการแปลงคำค้น”
สำรวจวิธีเรียบเรียงใหม่หรือขยายคำค้นของผู้ใช้ เพื่อให้ค้นคืนข้อมูลจากฐานข้อมูลเวกเตอร์ได้อย่างมีประสิทธิภาพยิ่งขึ้น คุณปฏิบัติ Vector Databases: Pinecone, Weaviate & pgvector ด้วยโค้ดที่ใช้งานได้จริงที่คุณเรียกใช้โดยตรงในเบราว์เซอร์ และติวเตอร์ AI ตลอด 24/7 ตอบคำถามของคุณขณะที่คุณไปผ่านบทเรียน
คุณต้องมีประสบการณ์ก่อนที่จะเริ่มเรียน Vector Databases: Pinecone, Weaviate & pgvector หรือไม่
ไม่จำเป็นต้องมีประสบการณ์มาก่อน Vector Databases: Pinecone, Weaviate & pgvector บน CoddyKit ออกแบบมาสำหรับผู้เริ่มต้นไปจนถึงผู้เรียนขั้นสูง คุณสามารถเริ่มต้นที่นี่หรือเริ่มจากตัวแรกและเรียนด้วยความเร็วของคุณเอง นี่คือบทเรียนที่ 1 จากทั้งหมด 4 บทเรียน
บทเรียน “เทคนิคการแปลงคำค้น” ใช้เวลานานแค่ไหน
บทเรียน CoddyKit ส่วนใหญ่ใช้เวลาประมาณ 5–10 นาที แต่ละบทเรียนจึงสั้นและเป็นแบบโต้ตอบ คุณสามารถก้าวหน้าอย่างต่อเนื่องและกลับมาเรียนต่อจากตรงที่เพิ่งหยุดบนเว็บและแอปได้เลย
ฉันเขียนและรันโค้ดในบทเรียน Vector Databases: Pinecone, Weaviate & pgvector นี้ได้ไหม
ได้ บทเรียน Vector Databases: Pinecone, Weaviate & pgvector ทุกบทมีตัวแก้ไขโค้ดในตัว คุณจึงเขียนและรันโค้ดจริงได้เลยในเบราว์เซอร์ และได้รับข้อเสนอแนะจาก AI ในทันที — ไม่ต้องติดตั้งในเครื่องของคุณ