में पार्ट 1, हम RG के उद्गम, मूलभूत, और क्यों यह बात है. आप उच्च स्तर की धारणा को समझते हैं: मूल्यवान जानकारी प्राप्त करने के लिए, फिर प्रतिक्रिया बनाने के लिए इसका उपयोग करें। अब हम तकनीकी निर्माण में गहरी कटौती करें - वास्तव में कैसे REG सिस्टमों के तहत, के तहत आंतरिक लक्षणों और KV के रूप में आंतरिक संकेतों और KV.
श्रेणी नेविगेशन: यह RAG श्रृंखला का भाग है:
आप भाग 1 नहीं पढ़ा है, तो मैं वहाँ समझने के लिए शुरू करने की सलाह देता हूँ:
यह लेख आपको उन बुनियादी बातों की समझ देता है और उन पर ध्यान देता है तकनीकी डिजाइन, कार्यान्वयन विवरण, तथा अलाईम आंतरिक.
हम वास्तव में एक RG व्यवस्था में क्या होता है तोड़ देते हैं, पल से आप एक दस्तावेज़ जोड़ जब एक उपयोगकर्ता उत्तर मिल जाता है।
RG कुछ भी प्राप्त करने से पहले, आपको अपने ज्ञान आधार की सूची करनी होगी. यह एक बार प्रक्रिया है (हालाँकि आप बाद में नए दस्तावेज़ों को जोड़ सकते हैं.
flowchart TB
A[Source Documents] -->|1. Extract Text| B[Text Extraction]
B -->|2. Split into Chunks| C[Chunking Service]
C -->|3. Generate Embeddings| D[Embedding Model]
D -->|4. Store Vectors| E[Vector Database]
B -.Metadata.-> E
subgraph "Example: Blog Post"
F["Understanding Docker: A containerization platform..."]
end
subgraph "Chunks"
G["Chunk 1: Title + Intro"]
H["Chunk 2: Benefits Section"]
end
subgraph "Embeddings"
I["0.234, 0.891, 0.567, ..."]
J["0.445, 0.123, 0.789, ..."]
end
F --> G
F --> H
G --> I
H --> J
style D stroke:#f9f,stroke-width:2px
style E stroke:#bbf,stroke-width:2px
आपके स्रोत दस्तावेज में से सादा पाठ निकालें. यह हो सकता है:
मेरे ब्लॉग से उदाहरण:
// From MarkdownRenderingService
public string ExtractPlainText(string markdown)
{
// Remove code blocks
var withoutCode = Regex.Replace(markdown, @"```[\s\S]*?```", "");
// Convert markdown to plain text
var document = Markdown.Parse(withoutCode);
var plainText = document.ToPlainText();
return plainText.Trim();
}
यह है जहां सबसे RGEG कार्यान्वयन असफल. आप सिर्फ पैराग्राफ सीमाओं पर विभाजित नहीं कर सकते हैं - आप सिर्फ sematss की आवश्यकता है.
क्यों ज़रूरी बातों को दोहराना:
खराब रीकरिंग:
Chunk 1: "Docker is a containerization platform. It allows you"
Chunk 2: "to package applications with their dependencies. This"
Chunk 3: "ensures consistency across environments."
अच्छी रेसिंग:
Chunk 1: "Docker is a containerization platform. It allows you to package applications with their dependencies. This ensures consistency across environments."
Chunk 2: "Benefits of Docker:
- Isolation: Each container runs in its own environment
- Portability: Containers run anywhere Docker is installed
- Efficiency: Lightweight compared to virtual machines"
मेरे पैरिक खोज कार्यान्वयन का उदाहरण:
public class TextChunker
{
private const int TargetChunkSize = 500; // ~500 words
private const int ChunkOverlap = 50; // 50 words overlap
public List<Chunk> ChunkDocument(string text, string sourceId)
{
var chunks = new List<Chunk>();
// Split on section boundaries first (## headers in markdown)
var sections = SplitOnHeaders(text);
foreach (var section in sections)
{
// If section is small enough, keep it whole
if (section.WordCount < TargetChunkSize)
{
chunks.Add(new Chunk
{
Text = section.Text,
SourceId = sourceId,
SectionHeader = section.Header
});
}
else
{
// Split large sections on sentence boundaries
var subChunks = SplitOnSentences(section.Text, TargetChunkSize, ChunkOverlap);
chunks.AddRange(subChunks.Select(c => new Chunk
{
Text = c,
SourceId = sourceId,
SectionHeader = section.Header
}));
}
}
return chunks;
}
}
सामान्यीकरण नीतिः
ये जादू हैं जो एक जादुई खोज संभव बनाता है ।
कुंजी संकल्पः समान अर्थ
"Docker container" → [0.234, -0.891, 0.567, ..., 0.123]
"containerization platform" → [0.221, -0.903, 0.534, ..., 0.119]
"apple fruit" → [0.891, 0.234, -0.567, ..., -0.789]
पहले दो सदिशों को सदिश स्थान (उच्च कोज्या मान) में "बंद" किया जाएगा, जबकि तीसरे दूर तक है.
कैसे एम्बेडिंग किया जाता है: आज के मशहूर मॉडलों की नकल करना, बड़े - बड़े पाठ के डेटा - डाटाओं को सीखने के लिए प्रशिक्षित किया जाता है ।
मेरे ऑनिंगिंग सेवा से उदाहरण:
public async Task<float[]> GenerateEmbeddingAsync(string text)
{
// Tokenize the input text
var tokens = Tokenize(text);
// Create input tensors for ONNX model
var inputIds = CreateInputTensor(tokens);
var attentionMask = CreateAttentionMaskTensor(tokens.Length);
var tokenTypeIds = CreateTokenTypeIdsTensor(tokens.Length);
// Run ONNX inference
var inputs = new List<NamedOnnxValue>
{
NamedOnnxValue.CreateFromTensor("input_ids", inputIds),
NamedOnnxValue.CreateFromTensor("attention_mask", attentionMask),
NamedOnnxValue.CreateFromTensor("token_type_ids", tokenTypeIds)
};
using var results = _session.Run(inputs);
// Extract the output (sentence embedding)
var output = results.First().AsTensor<float>();
var embedding = output.ToArray();
// L2 normalize the vector for cosine similarity
return NormalizeVector(embedding);
}
क्यों कुछ सामान्य बात नहीं: L2 को सामान्य रूप से समझने के बाद, कोसाइन संरक्षा एक सरल कार्य्य उत्पाद बन जाती है, जिससे बहुत तेज़ी से खोजती है ।
सदिश डाटाबेस उच्च- सदिश वेक्टर्स को भंडारित करने और ढूंढने के लिए डेज़ीशन्ड हैं. पारंपरिक डाटाबेस जो एसक्यूएल जांच का उपयोग करता है, सदिश डाटाबेस तुलनात्मक खोज का इस्तेमाल करता है.
कुंजी संचालन:
उदाहरण संपादन कार्यान्वयन:
public async Task IndexDocumentAsync(
string id,
float[] embedding,
Dictionary<string, object> metadata)
{
var point = new PointStruct
{
Id = new PointId { Uuid = id },
Vectors = embedding,
Payload =
{
["title"] = metadata["title"],
["source"] = metadata["source"],
["chunk_index"] = metadata["chunk_index"],
["created_at"] = DateTime.UtcNow.ToString("O")
}
};
await _client.UpsertAsync(
collectionName: "blog_posts",
points: new[] { point }
);
}
लोकप्रिय सदिश डाटाबेस:
हम भविष्य के लेखों में इन डाटाबेसओं की जांच करेंगे.
जब एक उपयोक्ता सवाल पूछता है, तो रीजी तंत्र को ज्ञान आधार से सबसे संबंधित जानकारी खोजनी चाहिए ।
flowchart LR
A["User Query:<br/>'How do I use Docker Compose?'"] --> B[Generate Query Embedding]
B --> C["Query Vector:<br/>[0.445, -0.123, ...]"]
C --> D[Vector Search]
D --> E[Vector Database]
E --> F[Top K Similar Chunks]
F --> G["Results:<br/>1. Docker Compose Basics 0.92<br/>2. Multi-Container Setup 0.87<br/>3. Service Configuration 0.83"]
style B stroke:#f9f,stroke-width:3px
style D stroke:#bbf,stroke-width:3px
उपयोक्ता का प्रश्न एक सदिश का उपयोग करता है वही एम्बेडिंग मॉडल इंडेक्सिंग के लिए उपयोग में लिया जाता है. यह महत्वपूर्ण मॉडल असंगत सदिश बनाता है.
public async Task<List<SearchResult>> SearchAsync(string query, int limit = 10)
{
// Same embedding model used for indexing
var queryEmbedding = await _embeddingService.GenerateEmbeddingAsync(query);
// Search in vector store
var results = await _vectorStoreService.SearchAsync(
queryEmbedding,
limit
);
return results;
}
सदिश डाटाबेस क्वैरी सदिश तथा सभी भंडारित सदिश के बीच तुलना करता है. सामान्य नियम:
कोसाइन सामान्य सदिशों के लिए सबसे लोकप्रिय (:
similarity = (A · B) / (||A|| × ||B||)
सीमा: - 1 से अधिक (उच्च = अधिक)
यूक्लिडियन दूरी (सामान्य सदिशों के लिए):
distance = sqrt(Σ(Ai - Bi)²)
सीमा: 0 से बाहर (बड़ा = अधिक)
उत्पाद बनाएँ (_t) (जब सदिश पहले से सामान्य किये जाते हैं):
similarity = A · B
सीमा: - 1 से अधिक (उच्च = अधिक)
मेरे प्रवरेज सेवा से उदाहरण:
var searchResults = await _client.SearchAsync(
collectionName: "blog_posts",
vector: queryEmbedding,
limit: (ulong)limit,
scoreThreshold: 0.7f, // Only return results with >70% similarity
payloadSelector: true // Include all metadata
);
return searchResults.Select(hit => new SearchResult
{
Text = hit.Payload["text"].StringValue,
Title = hit.Payload["title"].StringValue,
Score = hit.Score,
Source = hit.Payload["source"].StringValue
}).ToList();
फिर से प्राप्त करने का प्रयोग अधिक जटिल मॉडल का उपयोग करके शीर्ष के परिणामों को पुनः प्राप्त करने के लिए करता है।
flowchart LR
A[Vector Search:<br/>Top 50 Results] --> B[Reranking Model]
B --> C[Reranked:<br/>Top 10 Results]
style B stroke:#f9f,stroke-width:3px
फिर से तैयारी करने से हमें क्यों मदद मिलती है:
उदाहरण संपादन कार्यान्वयन:
public async Task<List<SearchResult>> SearchWithRerankAsync(
string query,
int initialLimit = 50,
int finalLimit = 10)
{
// Stage 1: Fast vector search
var candidates = await SearchAsync(query, initialLimit);
// Stage 2: Precise reranking
var rerankedResults = await _rerankingService.RerankAsync(
query,
candidates
);
return rerankedResults.Take(finalLimit).ToList();
}
अब कि हमारे पास संबंधित जानकारी है, हम इसे उपयोक्ता के प्रश्न के साथ साथ के लिए खाते हैं.
flowchart TB
A[User Query] --> B[Retrieved Context 1]
A --> C[Retrieved Context 2]
A --> D[Retrieved Context 3]
B --> E[Construct Prompt]
C --> E
D --> E
A --> E
E --> F["System: You are a helpful assistant...\n\nContext:\n1. Docker Compose allows...\n2. Services are defined...\n3. Volumes persist data...\n\nQuestion: How do I use Docker Compose?\n\nAnswer:"]
F --> G[LLM]
G --> H[Generated Answer with Citations]
style E stroke:#f9f,stroke-width:2px
style G stroke:#bbf,stroke-width:2px
यह है जहां RAG एक कला बन जाता है. आप की आवश्यकता है ताकि LLM:
एक उदाहरण प्रायोगिक टैम्प्लेट मेरे वकील जीबीडी तंत्र से:
public string BuildRAGPrompt(string query, List<SearchResult> context)
{
var sb = new StringBuilder();
sb.AppendLine("You are a technical writing assistant. Your task is to answer the user's question using ONLY the provided context from past blog posts.");
sb.AppendLine();
sb.AppendLine("CONTEXT:");
sb.AppendLine("========");
for (int i = 0; i < context.Count; i++)
{
sb.AppendLine($"[{i + 1}] {context[i].Title}");
sb.AppendLine($"Source: {context[i].Source}");
sb.AppendLine($"Content: {context[i].Text}");
sb.AppendLine($"Relevance: {context[i].Score:P0}");
sb.AppendLine();
}
sb.AppendLine("========");
sb.AppendLine();
sb.AppendLine("INSTRUCTIONS:");
sb.AppendLine("- Answer the question using the provided context");
sb.AppendLine("- Cite sources using [1], [2], etc.");
sb.AppendLine("- If the context doesn't contain enough information, say so");
sb.AppendLine("- Maintain the technical, practical tone of the blog");
sb.AppendLine();
sb.AppendLine($"QUESTION: {query}");
sb.AppendLine();
sb.AppendLine("ANSWER:");
return sb.ToString();
}
बनाया गया प्राम्प्ट पीढ़ी के लिएLLM में चला जाता है. यह हो सकता है:
स्थानीयLM का उपयोग कर उदाहरण:
public async Task<string> GenerateResponseAsync(string prompt)
{
var result = await _llamaSharp.InferAsync(prompt, new InferenceParams
{
Temperature = 0.7f, // Creativity (0 = deterministic, 1 = creative)
TopP = 0.9f, // Nucleus sampling
MaxTokens = 500, // Response length limit
StopSequences = new[] { "\n\n", "User:", "Question:" }
});
return result.Text.Trim();
}
कुंजी पैरामीटर स्पष्टः
LLM एक प्रतिक्रिया उत्पन्न करने के बाद, हम अक्सर की जरूरत है:
उदाहरण डाक- प्रक्रिया:
public RAGResponse PostProcess(string llmOutput, List<SearchResult> sources)
{
var response = new RAGResponse
{
Answer = llmOutput,
Sources = new List<Source>()
};
// Extract citations like [1], [2]
var citations = Regex.Matches(llmOutput, @"\[(\d+)\]");
foreach (Match match in citations)
{
int index = int.Parse(match.Groups[1].Value) - 1;
if (index >= 0 && index < sources.Count)
{
var source = sources[index];
response.Sources.Add(new Source
{
Title = source.Title,
Url = GenerateUrl(source.Source),
RelevanceScore = source.Score
});
}
}
// Convert markdown citations to hyperlinks
response.FormattedAnswer = Regex.Replace(
llmOutput,
@"\[(\d+)\]",
m => {
int index = int.Parse(m.Groups[1].Value) - 1;
if (index >= 0 && index < sources.Count)
{
var url = GenerateUrl(sources[index].Source);
return $"[[{m.Groups[1].Value}]]({url})";
}
return m.Value;
}
);
return response;
}
हम व्यावहारिक अनुप्रयोगों में कदम करने से पहले, यह समझने के लिए जरूरी है कि कैसे LLM आंतरिक काम करता है. यह ज्ञान आपको RELG सिस्टमों को स्तरित करने और आम फन्दों से बचने में मदद करता है.
Tomons बुनियादी इकाइयों है कि LLM प्रक्रिया. पाठ सीधे मॉडलों के लिए नहीं खिला रहा है - यह संकेतों में पहले टूट गया है.
उदाहरण टोकनाइज़ेशन:
Input: "Understanding Docker containers"
Tokens: ["Under", "standing", " Docker", " containers"]
भिन्न मॉडलों का उपयोग भिन्न टोकनीकरण नीतियाँ:
RAG के लिए लक्षणों को क्यों बनाएँ:
public class TokenCounter
{
// Rough approximation: 1 token ≈ 0.75 words (English)
public int EstimateTokens(string text)
{
var wordCount = text.Split(' ', StringSplitOptions.RemoveEmptyEntries).Length;
return (int)(wordCount / 0.75);
}
public int EstimateTokensAccurate(string text, ITokenizer tokenizer)
{
// Use actual tokenizer for precision
return tokenizer.Encode(text).Count;
}
}
कॉन्टेक्स्ट विंडो सीमाएँ:
RAG सिस्टम में, आपको फिट होना चाहिए:
Total tokens = System prompt + Retrieved context + User query + Response buffer
यदि तुम्हारा RG हर 500 संकेतों के 10 दस्तावेज़ों को खोजता है, तो यह 5,000 संकेत केवल संदर्भ और प्रतिक्रिया के लिए है!
व्यावहारिक RAG टोकन प्रबंधन:
public class ContextWindowManager
{
private readonly int _maxContextTokens;
private readonly int _systemPromptTokens;
private readonly int _responseBufferTokens;
public ContextWindowManager(
int totalContextWindow = 4096,
int systemPromptTokens = 300,
int responseBufferTokens = 500)
{
_maxContextTokens = totalContextWindow;
_systemPromptTokens = systemPromptTokens;
_responseBufferTokens = responseBufferTokens;
}
public List<SearchResult> FitContextInWindow(
List<SearchResult> retrievedDocs,
string query)
{
var queryTokens = EstimateTokens(query);
// Available tokens for retrieved context
var availableForContext = _maxContextTokens
- _systemPromptTokens
- queryTokens
- _responseBufferTokens;
var selectedDocs = new List<SearchResult>();
var currentTokens = 0;
foreach (var doc in retrievedDocs.OrderByDescending(d => d.Score))
{
var docTokens = EstimateTokens(doc.Text);
if (currentTokens + docTokens <= availableForContext)
{
selectedDocs.Add(doc);
currentTokens += docTokens;
}
else
{
break; // Context window full
}
}
return selectedDocs;
}
private int EstimateTokens(string text)
{
// Rule of thumb: 1 token ≈ 4 characters
return text.Length / 4;
}
}
जब एक TLM पाठ तैयार करता है, यह प्रत्येक संकेत के लिए शुरू से सब कुछ फिर से सक्रिय नहीं करता. यह एक का उपयोग करता है कुंजी- मूल्य (केवी) कैश याद रखने के लिए क्या वह पहले से ही गणना की है.
ट्रांसमीटर संदर्भ को समझने के लिए सभी पिछले संकेतों को "क्रमिक" का उपयोग करते हैं।
flowchart TB
subgraph "Generation Step 1: 'Docker'"
A1[Input: 'Docker'] --> B1[Compute K,V for 'Docker']
B1 --> C1[Store in KV Cache]
C1 --> D1[Generate: 'is']
end
subgraph "Generation Step 2: 'is'"
A2[Input: 'is'] --> B2[Compute K,V for 'is']
B2 --> C2[Store in KV Cache]
C2 --> E2[Retrieve KV for 'Docker']
E2 --> F2[Attend: 'is' to 'Docker']
F2 --> D2[Generate: 'a']
end
subgraph "Generation Step 3: 'a'"
A3[Input: 'a'] --> B3[Compute K,V for 'a']
B3 --> C3[Store in KV Cache]
C3 --> E3[Retrieve KV for 'Docker', 'is']
E3 --> F3[Attend: 'a' to all previous]
F3 --> D3[Generate: 'container']
end
D1 --> A2
D2 --> A3
style C1 stroke:#f9f,stroke-width:3px
style C2 stroke:#f9f,stroke-width:3px
style C3 stroke:#f9f,stroke-width:3px
केवी कैश के बगैर:
केवी कैश के साथ:
यह पीढ़ी बनाता है तीव्र गति - 10 चिह्न/second और 100 निशानियाँ/सेकेंड के बीच अंतर है.
केवी कैश एक "रेरे" का आकार बनाता है कि किस प्रकार रूपांतरणों में काम करता है. मॉडल में हर परत का अपना के-, ववीएस होता है.
graph TB
A[Input Tokens:<br/>'What is Docker?'] --> B[Layer 1 Attention]
B --> C[Layer 1 KV Cache]
B --> D[Layer 2 Attention]
D --> E[Layer 2 KV Cache]
D --> F[Layer 3 Attention]
F --> G[Layer 3 KV Cache]
F --> H[... up to Layer N]
H --> I[Output: 'Docker is']
C -.Key-Value pairs<br/>for all input tokens.-> C
E -.Key-Value pairs<br/>for all input tokens.-> E
G -.Key-Value pairs<br/>for all input tokens.-> G
style C stroke:#bbf,stroke-width:2px
style E stroke:#bbf,stroke-width:2px
style G stroke:#bbf,stroke-width:2px
प्रत्येक परत भंडारितः
इसके साथ मॉडल के लिए:
एक अनुक्रम के लिए केवी कैश है:
2 (K and V) × 32 layers × 4096 dimensions × 8192 tokens × 2 bytes (FP16)
≈ 4.3 GB of VRAM!
यही कारण है कि लंबे संदर्भ विंडो स्मृति- रचना हैं।
RAG सिस्टमों ने चालाक तरीक़ों से के- वीवी कैश का लाभ उठाया:
कैशिंग संकेत करें (कुछ एपीआई के द्वारा Athuts की तरह समर्थित है ):
public class CachedRAGService
{
// System prompt and retrieved context can be cached!
public async Task<string> GenerateWithCachedContextAsync(
string systemPrompt, // Cached
List<SearchResult> context, // Cached
string userQuery) // Not cached, changes each time
{
var contextText = FormatContext(context);
// The KV cache for systemPrompt + contextText is reused across queries
var prompt = $@"
{systemPrompt}
CONTEXT:
{contextText}
QUERY: {userQuery}
ANSWER:";
return await _llm.GenerateAsync(prompt, useCaching: true);
}
}
यह क्यों शक्तिशाली है:
व्यावहारिक उदाहरण:
Query 1: "How do I use Docker?" → 2 seconds (no cache)
Query 2: "What are Docker benefits?" → 0.2 seconds (cache hit!)
Query 3: "Docker vs VMs?" → 0.2 seconds (cache hit!)
सभी तीन गढ़ समान संदर्भ का प्रयोग करते हैं, इसलिए कि संदर्भों के लिए केवी कैश फिर से उपयोग किया जाता है ।
लक्षणों और केवीसी कैश को समझने से आपके RGGMPER के फैसलों की जानकारी मिलती है:
छोटा भाग = अधिक सटीक वापसी, लेकिन अधिक सिर:
// Option A: Small chunks (200 tokens each)
// Retrieve 20 chunks = 4,000 tokens
// Pro: Very precise, only relevant info
// Con: More KV cache entries, slower attention
// Option B: Larger chunks (500 tokens each)
// Retrieve 8 chunks = 4,000 tokens
// Pro: Better context coherence, fewer KV entries
// Con: More noise, less precise
public class AdaptiveChunker
{
public int DetermineChunkSize(int contextWindowSize)
{
if (contextWindowSize <= 4096)
return 200; // Small chunks for limited windows
if (contextWindowSize <= 16384)
return 500; // Medium chunks
return 1000; // Large chunks for big windows
}
}
संदर्भ विंडो अधिकतम मत करो - पीढ़ी के लिए जगह छोड़ दें:
public class SafeContextManager
{
public int GetSafeContextLimit(int totalContextWindow)
{
// Use only 75% for input, reserve 25% for output
return (int)(totalContextWindow * 0.75);
}
// Example: 4K model
// Total: 4096 tokens
// Safe input: 3072 tokens
// Reserved for output: 1024 tokens
}
गपशप में, वार्तालाप इतिहास हर मोड़ के साथ बढ़ता है:
Turn 1:
System + Context + Query1 = 3000 tokens
Response1 = 300 tokens
Total: 3300 tokens
Turn 2:
System + Context + Query1 + Response1 + Query2 = 3650 tokens
Response2 = 300 tokens
Total: 3950 tokens
Turn 3:
System + Context + Query1 + Response1 + Query2 + Response2 + Query3 = 4250 tokens
ERROR: Context window exceeded!
समाधानः फिर से प्रारंभ किए विंडो के साथ विस्तार किया जा रहा है
public class ConversationalRAG
{
private readonly int _maxHistoryTokens = 1000;
public async Task<string> ChatAsync(
List<ConversationTurn> history,
string newQuery)
{
// Re-retrieve context based on current query
var context = await RetrieveContextAsync(newQuery);
// Keep only recent conversation history
var relevantHistory = TrimHistory(history, _maxHistoryTokens);
var prompt = BuildPrompt(context, relevantHistory, newQuery);
return await _llm.GenerateAsync(prompt);
}
private List<ConversationTurn> TrimHistory(
List<ConversationTurn> history,
int maxTokens)
{
var trimmed = new List<ConversationTurn>();
var currentTokens = 0;
// Keep most recent turns
foreach (var turn in history.Reverse())
{
var turnTokens = EstimateTokens(turn.Query) + EstimateTokens(turn.Response);
if (currentTokens + turnTokens <= maxTokens)
{
trimmed.Insert(0, turn);
currentTokens += turnTokens;
}
else
{
break;
}
}
return trimmed;
}
}
एपीआई आधारित आरोप प्रति टोकन. RG को विस्फोटित कर सकते हैं यदि ध्यान नहीं दिया:
public class CostAwareRAG
{
// OpenAI GPT-4 pricing (example):
// Input: $0.03 per 1K tokens
// Output: $0.06 per 1K tokens
public decimal EstimateQueryCost(
int systemPromptTokens,
int retrievedContextTokens,
int queryTokens,
int expectedResponseTokens)
{
var inputTokens = systemPromptTokens + retrievedContextTokens + queryTokens;
var outputTokens = expectedResponseTokens;
var inputCost = (inputTokens / 1000m) * 0.03m;
var outputCost = (outputTokens / 1000m) * 0.06m;
return inputCost + outputCost;
}
// Example:
// System: 300 tokens
// Context: 3000 tokens (10 retrieved docs)
// Query: 50 tokens
// Response: 500 tokens
//
// Cost = ((300 + 3000 + 50) / 1000 * 0.03) + (500 / 1000 * 0.06)
// = (3350 / 1000 * 0.03) + (500 / 1000 * 0.06)
// = $0.1005 + $0.03
// = $0.1305 per query
//
// At 1000 queries/day = $130/day = $3,900/month!
}
लागत कमी पॉलिसी:
यहाँ संकेतों, केवी कैश, और RAG एक साथ फिट है:
flowchart TB
A[User Query:<br/>'How does Docker work?'<br/>≈ 12 tokens] --> B[Generate Query Embedding]
B --> C[Vector Search]
C --> D[Retrieved Docs:<br/>5 docs × 500 tokens<br/>= 2,500 tokens]
D --> E[Construct Prompt]
A --> E
E --> F["Complete Prompt:<br/>System: 300 tokens<br/>Context: 2,500 tokens<br/>Query: 12 tokens<br/>Total: 2,812 tokens"]
F --> G[Tokenize Prompt]
G --> H["Token IDs:<br/>[245, 1034, 8829, ...]<br/>2,812 token IDs"]
H --> I[LLM Layer 1]
I --> J[Compute K,V]
J --> K[KV Cache Layer 1:<br/>2,812 K,V pairs]
I --> L[LLM Layer 2]
L --> M[Compute K,V]
M --> N[KV Cache Layer 2:<br/>2,812 K,V pairs]
L --> O[... Layers 3-32]
O --> P[Generate Token 1: 'Docker']
P --> Q[Add to KV Cache]
Q --> R[Generate Token 2: 'is']
R --> S[Add to KV Cache]
S --> T[... until completion]
T --> U["Response: 'Docker is a containerization platform...'<br/>≈ 400 tokens"]
style K stroke:#f9f,stroke-width:2px
style N stroke:#f9f,stroke-width:2px
style Q stroke:#bbf,stroke-width:2px
style S stroke:#bbf,stroke-width:2px
मुख्य अंतर्दृष्टि:
लक्षणों और केवीवी कैश को समझने से रेजी डिज़ाइन बेहतर बनता है:
1. प्री- एजेंट तथा कैश सामान्य संदर्भ:
// Cache KV for frequently used system prompts + static context
var cachedSystemContext = await _llm.PrecomputeKVCache(systemPrompt + staticContext);
// Reuse for each query (much faster)
foreach (var query in userQueries)
{
var response = await _llm.GenerateAsync(query, reuseKVCache: cachedSystemContext);
}
2 सापेक्ष सीमाएँ नापते हुए:
// Bad: Arbitrary 500-character chunks
var chunks = text.Chunk(500);
// Good: Chunk on sentence boundaries, measure in tokens
public List<string> ChunkByTokens(string text, int maxTokensPerChunk)
{
var sentences = SplitIntoSentences(text);
var chunks = new List<string>();
var currentChunk = new StringBuilder();
var currentTokens = 0;
foreach (var sentence in sentences)
{
var sentenceTokens = EstimateTokens(sentence);
if (currentTokens + sentenceTokens > maxTokensPerChunk && currentTokens > 0)
{
chunks.Add(currentChunk.ToString());
currentChunk.Clear();
currentTokens = 0;
}
currentChunk.Append(sentence).Append(" ");
currentTokens += sentenceTokens;
}
if (currentTokens > 0)
chunks.Add(currentChunk.ToString());
return chunks;
}
3. मॉनीटर टोकन उपयोग से उत्पादन में:
public class RAGTelemetry
{
public void LogRAGQuery(
string query,
List<SearchResult> retrievedDocs,
string response)
{
var queryTokens = EstimateTokens(query);
var contextTokens = retrievedDocs.Sum(d => EstimateTokens(d.Text));
var responseTokens = EstimateTokens(response);
var totalTokens = queryTokens + contextTokens + responseTokens;
_logger.LogInformation(
"RAG Query: {Query} | Context: {ContextTokens} tokens from {DocCount} docs | " +
"Response: {ResponseTokens} tokens | Total: {TotalTokens} tokens",
query, contextTokens, retrievedDocs.Count, responseTokens, totalTokens
);
// Alert if approaching context limit
if (totalTokens > _maxTokens * 0.9)
{
_logger.LogWarning("Approaching token limit: {TotalTokens}/{MaxTokens}",
totalTokens, _maxTokens);
}
}
}
हमने रेजी सिस्टम की पूरी तकनीकी रचना को कवर कर दिया है:
फेस 1: निर्देशिकािंग
फेस 2: reting
फेस 3: तैयार करना
आंतरिक आंतरिक
मुख्य तकनीकी अंतर्दृष्टि:
अब आप समझते हैं RAG काम कैसे करता है आप इन व्यवस्थाओं का कैसे निर्माण करते हैं?
में **अभ्यास में भाग 3: RAG**हम पुस्तकालय से कार्यान्वयन के लिए स्थानांतरित कर रहे हैं:
वास्तविक विश्व अनुप्रयोग:
आम चुनौतियाँ और समाधान:
विस्तृत तकनीक:
प्रारंभ किया जा रहा है:
आधारित कागज:
औज़ार तथा फ्रेमवर्क:
आगे पढ़ा जा रहा है:
श्रेणी नेविगेशन:
© 2026 Scott Galloway — Unlicense — All content and source code on this site is free to use, copy, modify, and sell.