Current section
Files
Jump to
Current section
Files
lib/snakebridge_generated/dspy/knn_few_shot.ex
# Generated by SnakeBridge v0.13.0 - DO NOT EDIT MANUALLY
# Regenerate with: mix compile
# Library: dspy 3.1.2
# Python module: dspy
# Python class: KNNFewShot
defmodule Dspy.KNNFewShot do
@moduledoc """
Wrapper for Python class KNNFewShot.
"""
def __snakebridge_python_name__, do: "dspy"
def __snakebridge_python_class__, do: "KNNFewShot"
def __snakebridge_library__, do: "dspy"
@opaque t :: SnakeBridge.Ref.t()
@doc """
KNNFewShot is an optimizer that uses an in-memory KNN retriever to find the k nearest neighbors
in a trainset at test time. For each input example in a forward call, it identifies the k most
similar examples from the trainset and attaches them as demonstrations to the student module.
## Parameters
- `k` - The number of nearest neighbors to attach to the student model.
- `trainset` - The training set to use for few-shot prompting.
- `vectorizer` - The `Embedder` to use for vectorization **few_shot_bootstrap_args: Additional arguments for the `BootstrapFewShot` optimizer.
## Examples
```python
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)
# Use the compiled module
result = compiled_qa("What is the capital of Belgium?")
```
"""
@spec new(
integer(),
list(Dspy.Primitives.Example.t()),
Dspy.Clients.Embedding.Embedder.t(),
keyword()
) :: {:ok, SnakeBridge.Ref.t()} | {:error, Snakepit.Error.t()}
def new(k, trainset, vectorizer, opts \\ []) do
SnakeBridge.Runtime.call_class(__MODULE__, :__init__, [k, trainset, vectorizer], opts)
end
@doc """
Optimize the student program.
## Parameters
- `student` - The student program to optimize.
- `trainset` - The training set to use for optimization.
- `teacher` - The teacher program to use for optimization.
- `valset` - The validation set to use for optimization.
## Returns
- `term()`
"""
@spec compile(SnakeBridge.Ref.t(), term(), keyword()) ::
{:ok, term()} | {:error, Snakepit.Error.t()}
def compile(ref, student, opts \\ []) do
SnakeBridge.Runtime.call_method(ref, :compile, [student], opts)
end
@doc """
Get the parameters of the teleprompter.
## Returns
- `%{optional(String.t()) => term()}`
"""
@spec get_params(SnakeBridge.Ref.t(), keyword()) ::
{:ok, %{optional(String.t()) => term()}} | {:error, Snakepit.Error.t()}
def get_params(ref, opts \\ []) do
SnakeBridge.Runtime.call_method(ref, :get_params, [], opts)
end
end