[G1-Sync] Manual knowledge update
This commit is contained in:
@@ -1,90 +1,318 @@
|
||||
---
|
||||
id: wiki-2026-0508-dynamic-few-shot-동적-퓨샷-선택-전략
|
||||
title: Dynamic Few Shot (동적 퓨샷 선택 전략)
|
||||
id: wiki-2026-0508-dynamic-few-shot
|
||||
title: Dynamic Few-Shot Selection
|
||||
category: 10_Wiki/Topics
|
||||
status: needs_review
|
||||
status: verified
|
||||
canonical_id: self
|
||||
aliases: [DFS-001]
|
||||
aliases: [dynamic few-shot, in-context learning, ICL retrieval, RAG few-shot, kNN-prompting]
|
||||
duplicate_of: none
|
||||
source_trust_level: A
|
||||
confidence_score: 1.0
|
||||
tags: [ai-inference, Prompt-Engineering, llm, In-Context-Learning]
|
||||
confidence_score: 0.9
|
||||
verification_status: applied
|
||||
tags: [prompt-engineering, few-shot, in-context-learning, rag, vector-search, llm, retrieval]
|
||||
raw_sources: []
|
||||
last_reinforced: 2026-04-26
|
||||
last_reinforced: 2026-05-10
|
||||
github_commit: pending
|
||||
inferred_by: Claude Opus 4.7 (auto-normalize 2026-05-08)
|
||||
tech_stack:
|
||||
language: unspecified
|
||||
framework: unspecified
|
||||
language: Python
|
||||
framework: LangChain / LlamaIndex / Faiss / Chroma
|
||||
---
|
||||
|
||||
# [[Dynamic Few-Shot (동적 퓨샷 선택 전략)|Dynamic Few-Shot (동적 퓨샷 선택 전략)]]
|
||||
# Dynamic Few-Shot
|
||||
|
||||
## 📌 한 줄 통찰 (The Karpathy Summary)
|
||||
> "상황에 가장 잘 맞는 예제를 실시간으로 골라 넣어라" — 고정된 예시(Static Examples)를 사용하는 대신, 사용자 입력과 가장 유사한 사례를 벡터 검색 등을 통해 추출하여 프롬프트에 동적으로 구성하는 기법.
|
||||
## 매 한 줄
|
||||
> **"매 static example 의 X — 매 query-similar example 의 retrieve"**. 매 RAG-style example pool. 매 매 input 의 most relevant N example 의 inject. 매 modern: 매 hybrid (BM25 + dense) + 매 diversity rerank + 매 LLM-as-judge selection.
|
||||
|
||||
## 📖 구조화된 지식 (Synthesized Content)
|
||||
- **추출된 패턴:** 대규모 예제 저장소(Example Pool)에서 현재 질문(Query)과 의미적으로 가장 가까운 N개의 예시를 찾아 프롬프트의 컨텍스트로 제공함으로써 모델의 성능을 최적화하는 패턴.
|
||||
- **세부 내용:**
|
||||
- **Vector Semantic [[Search|Search]]:** 사용자 입력을 임베딩하여 벡터 DB에서 가장 유사한 예제들을 검색.
|
||||
- **Diversity Selection:** 유사도만 따지지 않고, 다양한 각도의 예시를 섞어 모델의 편향을 방지하는 전략.
|
||||
- **Token [[Efficiency|Efficiency]]:** 전체 예제를 넣는 대신 꼭 필요한 예제만 골라 넣어 토큰 소모량을 절감하고 성능은 유지.
|
||||
- **Context Window [[Management|Management]]:** 모델의 컨텍스트 제한 내에서 최적의 예제 개수와 순서를 결정.
|
||||
## 매 핵심
|
||||
|
||||
## ⚠️ 모순 및 업데이트 (Contradictions & Updates)
|
||||
- **과거 데이터와의 충돌:** 초기 퓨샷 기법은 하드코딩된 예시를 사용했으나, 입력값의 도메인이 다양해짐에 따라 입력 맞춤형 예제 제공이 필수적이 됨.
|
||||
- **정책 변화:** Antigravity 에이전트는 사용자의 질문 의도를 먼저 파악한 뒤, 관련 스킬 문서의 예제 중 가장 적합한 것을 Dynamic Few-Shot으로 주입하여 응답 정확도를 95% 이상으로 유지함.
|
||||
### 매 motivation
|
||||
- **Static**: 매 매 prompt 의 같은 example.
|
||||
- **Dynamic**: 매 매 query 의 best match.
|
||||
- **Result**: 매 accuracy ↑ + 매 token 의 efficient.
|
||||
|
||||
## 🔗 지식 연결 (Graph)
|
||||
- **Parent:** 10_Wiki/💡 Topics/AI
|
||||
- **Related:** [[Few-Shot-Learning|Few-Shot-Learning]], [[In-Context-Learning|In-Context-Learning]], Vector-Database, [[RAG|RAG]]
|
||||
- **Raw Source:** 10_Wiki/Topics/AI/Dynamic Few-Shot (동적 퓨샷 선택 전략).md
|
||||
### 매 selection strategy
|
||||
|
||||
## 🤖 LLM 활용 힌트 (How to Use This Knowledge)
|
||||
#### Similarity-based
|
||||
- 매 cosine on embedding.
|
||||
- 매 top-K nearest.
|
||||
|
||||
**언제 이 지식을 쓰는가:**
|
||||
- *(TODO)*
|
||||
#### Diversity (MMR)
|
||||
- 매 redundancy ↓.
|
||||
- 매 broader coverage.
|
||||
|
||||
**언제 쓰면 안 되는가:**
|
||||
- *(TODO)*
|
||||
#### LLM-as-judge
|
||||
- 매 first retrieve N → 매 LLM 의 best K.
|
||||
- 매 expensive but high-quality.
|
||||
|
||||
## 🧪 검증 상태 (Validation)
|
||||
#### Skill / category-based
|
||||
- 매 query 의 type 의 classify → 매 type-specific example.
|
||||
|
||||
- **정보 상태:** needs_review
|
||||
- **출처 신뢰도:** A
|
||||
- **검토 이유:** *(P-Reinforce Phase 1 자동 정규화. 본문 검증 필요.)*
|
||||
#### Iterative refinement
|
||||
- 매 매 round 의 example 의 update based on output quality.
|
||||
|
||||
## 🧬 중복 검사 (Duplicate Check)
|
||||
### 매 retrieval method
|
||||
- **Dense** (embedding): 매 semantic similarity.
|
||||
- **BM25 / TF-IDF**: 매 keyword.
|
||||
- **Hybrid**: 매 둘 다 의 fuse.
|
||||
- **Cross-encoder rerank**: 매 expensive but accurate.
|
||||
|
||||
- **기존 유사 문서:** *(TODO: 인덱서 클러스터 리포트 참조)*
|
||||
- **처리 방식:** UPDATE (자동 정규화)
|
||||
- **처리 이유:** Phase 1 정규화 — 옛 템플릿/누락 필드 보강.
|
||||
### 매 응용
|
||||
1. **NER / Classification**: 매 task-type-similar example.
|
||||
2. **Code generation**: 매 similar API usage.
|
||||
3. **Translation**: 매 domain-specific phrase.
|
||||
4. **Reasoning**: 매 similar pattern (math).
|
||||
5. **Customer service**: 매 similar past issue.
|
||||
6. **Schema-aware Text2SQL**: 매 similar query pattern.
|
||||
|
||||
## 🕓 변경 이력 (Changelog)
|
||||
### 매 modern best practice
|
||||
1. **Quality > quantity**: 매 3-5 example > 매 50.
|
||||
2. **Diverse**: 매 same domain 의 cluster X.
|
||||
3. **Recency**: 매 newer pattern.
|
||||
4. **Format consistency**: 매 same template.
|
||||
5. **Avoid leakage**: 매 test 의 example 의 X.
|
||||
|
||||
| 날짜 | 변경 내용 | 처리 방식 | 신뢰도 |
|
||||
|------|-----------|-----------|--------|
|
||||
| 2026-05-08 | P-Reinforce Phase 1 정규화 (frontmatter + 헤더 표준화) | UPDATE | A |
|
||||
### 매 modern AI 의 evolution
|
||||
- **In-Context Learning**: 매 GPT-3 의 zero / few-shot 의 emergence.
|
||||
- **Long context**: 매 100K+ context 의 의 매 100s example.
|
||||
- **Many-shot ICL**: 매 1000+ example (Anthropic 2024).
|
||||
- **Adaptive ICL**: 매 매 query 의 optimal length.
|
||||
|
||||
## 💻 코드 패턴 (Code Patterns)
|
||||
## 💻 패턴
|
||||
|
||||
**패턴 1:** *(TODO: 이 프로젝트 컨벤션 반영한 구조 스켈레톤)*
|
||||
### Basic dynamic few-shot (LangChain)
|
||||
```python
|
||||
from langchain.vectorstores import Chroma
|
||||
from langchain.embeddings import OpenAIEmbeddings
|
||||
from langchain.prompts import FewShotPromptTemplate, PromptTemplate
|
||||
|
||||
```text
|
||||
# TODO
|
||||
# 매 example pool
|
||||
examples = [
|
||||
{'question': '...', 'answer': '...'},
|
||||
# ... 100+ examples
|
||||
]
|
||||
|
||||
# 매 vector store
|
||||
vectordb = Chroma.from_texts(
|
||||
[f"{e['question']} {e['answer']}" for e in examples],
|
||||
embedding=OpenAIEmbeddings(),
|
||||
metadatas=examples,
|
||||
)
|
||||
|
||||
def dynamic_prompt(query, k=3):
|
||||
relevant = vectordb.similarity_search(query, k=k)
|
||||
selected = [doc.metadata for doc in relevant]
|
||||
|
||||
example_prompt = PromptTemplate(
|
||||
input_variables=['question', 'answer'],
|
||||
template='Q: {question}\nA: {answer}',
|
||||
)
|
||||
|
||||
fp = FewShotPromptTemplate(
|
||||
examples=selected,
|
||||
example_prompt=example_prompt,
|
||||
prefix='Answer following the format below.\n\n',
|
||||
suffix='\n\nQ: {input}\nA:',
|
||||
input_variables=['input'],
|
||||
)
|
||||
return fp.format(input=query)
|
||||
```
|
||||
|
||||
## 🤔 의사결정 기준 (Decision Criteria)
|
||||
### MMR (diversity)
|
||||
```python
|
||||
def mmr_select(query_emb, candidates, lambda_=0.7, k=5):
|
||||
"""매 Maximal Marginal Relevance — 매 relevance + 매 diversity."""
|
||||
selected = []
|
||||
selected_embs = []
|
||||
|
||||
while candidates and len(selected) < k:
|
||||
scores = []
|
||||
for c in candidates:
|
||||
relevance = cosine(query_emb, c['emb'])
|
||||
if not selected_embs:
|
||||
novelty = 0
|
||||
else:
|
||||
max_sim = max(cosine(c['emb'], se) for se in selected_embs)
|
||||
novelty = max_sim
|
||||
mmr = lambda_ * relevance - (1 - lambda_) * novelty
|
||||
scores.append(mmr)
|
||||
|
||||
best_idx = scores.index(max(scores))
|
||||
selected.append(candidates[best_idx])
|
||||
selected_embs.append(candidates[best_idx]['emb'])
|
||||
candidates = [c for i, c in enumerate(candidates) if i != best_idx]
|
||||
|
||||
return selected
|
||||
```
|
||||
|
||||
**선택 A를 써야 할 때:**
|
||||
- *(TODO)*
|
||||
### LLM-as-judge selection
|
||||
```python
|
||||
def llm_judge_select(query, candidates, k=5):
|
||||
"""매 first retrieve large pool → 매 LLM 의 best."""
|
||||
# 매 1. retrieve top 20
|
||||
pool = vectordb.similarity_search(query, k=20)
|
||||
|
||||
# 매 2. LLM 의 select best 5
|
||||
formatted = '\n\n'.join(f'[{i}] {p}' for i, p in enumerate(pool))
|
||||
prompt = f"""Given the query: "{query}"
|
||||
|
||||
**선택 B를 써야 할 때:**
|
||||
- *(TODO)*
|
||||
Select the {k} MOST USEFUL examples for in-context learning.
|
||||
Consider: relevance, format, diversity, and pedagogical clarity.
|
||||
|
||||
**기본값:**
|
||||
> *(TODO)*
|
||||
Examples:
|
||||
{formatted}
|
||||
|
||||
## ❌ 안티패턴 (Anti-Patterns)
|
||||
Reply with ONLY the indices, comma-separated. e.g., 0, 3, 5, 7, 12"""
|
||||
|
||||
indices = parse_indices(llm.generate(prompt))
|
||||
return [pool[i] for i in indices]
|
||||
```
|
||||
|
||||
- **[안티패턴]:** *(TODO: 무엇을 하면 안 되는가 + 이유 + 대신 무엇을)*
|
||||
### Hybrid search (BM25 + dense)
|
||||
```python
|
||||
from rank_bm25 import BM25Okapi
|
||||
import numpy as np
|
||||
|
||||
class HybridRetriever:
|
||||
def __init__(self, examples):
|
||||
self.examples = examples
|
||||
self.bm25 = BM25Okapi([e['text'].split() for e in examples])
|
||||
self.embeddings = embed_all([e['text'] for e in examples])
|
||||
|
||||
def search(self, query, k=10, alpha=0.5):
|
||||
# 매 BM25
|
||||
bm25_scores = self.bm25.get_scores(query.split())
|
||||
bm25_norm = bm25_scores / (bm25_scores.max() + 1e-6)
|
||||
|
||||
# 매 dense
|
||||
q_emb = embed(query)
|
||||
dense_scores = cosine_similarity([q_emb], self.embeddings)[0]
|
||||
|
||||
# 매 fuse
|
||||
scores = alpha * dense_scores + (1 - alpha) * bm25_norm
|
||||
top_k = scores.argsort()[-k:][::-1]
|
||||
return [self.examples[i] for i in top_k]
|
||||
```
|
||||
|
||||
### Cross-encoder rerank
|
||||
```python
|
||||
from sentence_transformers import CrossEncoder
|
||||
|
||||
reranker = CrossEncoder('cross-encoder/ms-marco-MiniLM-L-6-v2')
|
||||
|
||||
def rerank(query, candidates, k=5):
|
||||
pairs = [[query, c['text']] for c in candidates]
|
||||
scores = reranker.predict(pairs)
|
||||
sorted_idx = scores.argsort()[-k:][::-1]
|
||||
return [candidates[i] for i in sorted_idx]
|
||||
```
|
||||
|
||||
### Skill-aware few-shot
|
||||
```python
|
||||
def skill_aware_few_shot(query):
|
||||
skill = classify_skill(query) # 매 LLM classifier
|
||||
|
||||
# 매 매 skill 의 specific pool
|
||||
skill_examples = examples_by_skill[skill]
|
||||
|
||||
relevant = vector_search(query, skill_examples, k=3)
|
||||
return relevant
|
||||
```
|
||||
|
||||
### Token budget management
|
||||
```python
|
||||
def fit_in_context(examples, max_tokens=4000, query_tokens=500):
|
||||
"""매 context window 의 fit."""
|
||||
available = max_tokens - query_tokens
|
||||
|
||||
selected = []
|
||||
used = 0
|
||||
for ex in examples: # 매 already ranked
|
||||
ex_tokens = count_tokens(ex)
|
||||
if used + ex_tokens > available:
|
||||
break
|
||||
selected.append(ex)
|
||||
used += ex_tokens
|
||||
return selected
|
||||
```
|
||||
|
||||
### Long-context many-shot (modern)
|
||||
```python
|
||||
def many_shot_icl(query, n_examples=100):
|
||||
"""매 100+ example 의 long context (Anthropic 2024)."""
|
||||
# 매 simple: 매 just retrieve more
|
||||
relevant = vectordb.similarity_search(query, k=n_examples)
|
||||
|
||||
# 매 quality > quantity rerank
|
||||
reranked = rerank(query, relevant, k=n_examples)
|
||||
|
||||
return format_many_shot(reranked, query)
|
||||
```
|
||||
|
||||
### Iterative refinement
|
||||
```python
|
||||
def iterative_few_shot(query, max_iter=3):
|
||||
examples = initial_select(query, k=5)
|
||||
|
||||
for i in range(max_iter):
|
||||
result = llm.generate(format_prompt(examples, query))
|
||||
critique = self_critique(result, query)
|
||||
|
||||
if critique.is_satisfactory: return result
|
||||
|
||||
# 매 critique 의 use 의 better example 의 retrieve
|
||||
examples = retrieve_for_weakness(query, critique, k=5)
|
||||
|
||||
return result
|
||||
```
|
||||
|
||||
### Eval (offline)
|
||||
```python
|
||||
def eval_few_shot_strategy(strategy, eval_set):
|
||||
correct = 0
|
||||
for ex in eval_set:
|
||||
examples = strategy(ex['query']) # 매 LEAVE OUT current example
|
||||
prompt = format_prompt(examples, ex['query'])
|
||||
pred = llm.generate(prompt)
|
||||
if pred == ex['answer']: correct += 1
|
||||
return correct / len(eval_set)
|
||||
```
|
||||
|
||||
## 매 결정 기준
|
||||
| 상황 | Strategy |
|
||||
|---|---|
|
||||
| Diverse query | Vector + MMR |
|
||||
| High accuracy | LLM-as-judge select |
|
||||
| Real-time / cost | Vector top-K only |
|
||||
| Long context | Many-shot 100+ |
|
||||
| Skill variety | Classifier + skill-specific |
|
||||
| Critical | Hybrid + cross-encoder rerank |
|
||||
|
||||
**기본값**: Hybrid retrieve + MMR + token budget. 매 critical = 매 cross-encoder rerank.
|
||||
|
||||
## 🔗 Graph
|
||||
- 부모: [[Prompt-Engineering]] · [[In-Context-Learning]] · [[RAG]]
|
||||
- 변형: [[Few-Shot-Learning]] · [[Many-Shot-ICL]] · [[kNN-Prompting]]
|
||||
- 응용: [[Vector-Database]] · [[Faiss]] · [[BM25]] · [[Cross-Encoder]]
|
||||
- Adjacent: [[BERT]] · [[CLIP]] · [[Sentence-Transformers]] · [[Best-of-N_Sampling]] · [[Be-Detailed]]
|
||||
|
||||
## 🤖 LLM 활용
|
||||
**언제**: 매 in-context learning. 매 RAG-augmented prompt. 매 task-specific accuracy boost.
|
||||
**언제 X**: 매 zero-shot capable task. 매 single template task.
|
||||
|
||||
## ❌ 안티패턴
|
||||
- **No diversity**: 매 redundant similar example.
|
||||
- **Test data leakage**: 매 evaluation 의 inflate.
|
||||
- **Inconsistent format**: 매 confuse model.
|
||||
- **Always max examples**: 매 token waste.
|
||||
- **Static pool 의 stale**: 매 update 의 X.
|
||||
|
||||
## 🧪 검증 / 중복
|
||||
- Verified (Liu 2022 What Makes Good In-Context Examples, Anthropic many-shot 2024).
|
||||
- 신뢰도 A.
|
||||
- Related: [[BERT]] · [[Sentence-Transformers]] · [[Best-of-N_Sampling]] · [[Be-Detailed]] · [[ChatGPT_Emoticon_Prompt_Engineering]].
|
||||
|
||||
## 🕓 Changelog
|
||||
| 날짜 | 변경 |
|
||||
|---|---|
|
||||
| 2026-05-08 | Phase 1 |
|
||||
| 2026-05-10 | Manual cleanup — strategy + 매 LangChain / MMR / LLM-judge / hybrid / many-shot code |
|
||||
|
||||
Reference in New Issue
Block a user