dspy.KNNFewShot

dspy.KNNFewShot

dspy.KNNFewShot는 테스트 시점에 인메모리 KNN 검색기를 사용해 trainset에서 k개의 최근접 이웃을 찾는 옵티마이저입니다. 각 forward 호출의 입력 example에 대해 trainset에서 가장 유사한 k개를 찾아 student 모듈에 데모(demonstration)로 붙입니다.

출처: 문서

본문

Requires numpy: dspy.KNNFewShot은 numpy가 필요합니다. pip install dspy[numpy]로 설치하세요.

dspy.KNNFewShot(
    k: int,
    trainset: list[Example],
    vectorizer: Embedder,
    **few_shot_bootstrap_args: dict[str, Any],
)
  • Bases: Teleprompter

KNNFewShot은 테스트 시점에 인메모리 KNN 검색기를 사용해 trainset에서 k개의 최근접 이웃을 찾는 옵티마이저입니다. 각 forward 호출의 입력 example에 대해 trainset에서 가장 유사한 k개를 찾아 student 모듈에 데모로 붙입니다.

Parameters:

Name Type Description Default
k int student 모델에 붙일 최근접 이웃의 수. required
trainset list[Example] few-shot 프롬프팅에 사용할 학습 세트. required
vectorizer Embedder 벡터화에 사용할 Embedder. required
**few_shot_bootstrap_args dict[str, Any] BootstrapFewShot 옵티마이저를 위한 추가 인자. {}

Examples

import dspy
from sentence_transformers import SentenceTransformer

# Define a QA module with chain of thought
qa = dspy.ChainOfThought("question -> answer")

# Create a training dataset with examples
trainset = [
    dspy.Example(question="What is the capital of France?", answer="Paris").with_inputs("question"),
    # ... more examples ...
]

# Initialize KNNFewShot with a sentence transformer model
knn_few_shot = KNNFewShot(
    k=3,
    trainset=trainset,
    vectorizer=dspy.Embedder(SentenceTransformer("all-MiniLM-L6-v2").encode)
)

# Compile the QA module with few-shot learning
compiled_qa = knn_few_shot.compile(qa)

Methods

compile(student, *, teacher=None)

student를 KNN few-shot 설정으로 컴파일합니다. few_shot_bootstrap_args의 추가 인자는 BootstrapFewShot로 전달됩니다.

get_params() -> dict[str, Any]

텔레프롬프터의 파라미터를 반환합니다.

더 알아보기 (Learn more)