pub struct SemanticSimilarityExampleSelector { /* private fields */ }Expand description
Selects the k examples most similar to the input, by embedding cosine similarity,
most similar first.
An example’s text is its values joined by spaces in key order. With
with_input_keys only those keys are used, both for the
examples and for the input (so the expected output doesn’t count).
ⓘ
let selector = SemanticSimilarityExampleSelector::from_examples(
examples,
OllamaEmbedder::default(),
2,
)
.await?
.with_input_keys(["input"]);Implementations§
Source§impl SemanticSimilarityExampleSelector
impl SemanticSimilarityExampleSelector
Sourcepub fn new<E: Embedder + 'static>(embedder: E) -> Self
pub fn new<E: Embedder + 'static>(embedder: E) -> Self
An empty selector; add examples with ExampleSelector::add_example.
Sourcepub fn with_embedder(embedder: Arc<dyn Embedder>) -> Self
pub fn with_embedder(embedder: Arc<dyn Embedder>) -> Self
Like new, with an embedder shared with other components.
Sourcepub async fn from_examples<E: Embedder + 'static>(
examples: Vec<PromptArgs>,
embedder: E,
k: usize,
) -> Result<Self, PromptError>
pub async fn from_examples<E: Embedder + 'static>( examples: Vec<PromptArgs>, embedder: E, k: usize, ) -> Result<Self, PromptError>
A selector over examples (embedded in one batch) that picks k of them.
Sourcepub fn with_input_keys<I, S>(self, keys: I) -> Self
pub fn with_input_keys<I, S>(self, keys: I) -> Self
Only these keys are embedded, for examples and input alike. Examples are re-embedded when added, so set this before adding them.
Sourcepub async fn add_examples(
&self,
examples: Vec<PromptArgs>,
) -> Result<(), PromptError>
pub async fn add_examples( &self, examples: Vec<PromptArgs>, ) -> Result<(), PromptError>
Adds several examples, embedded in one batch.
Trait Implementations§
Source§impl ExampleSelector for SemanticSimilarityExampleSelector
impl ExampleSelector for SemanticSimilarityExampleSelector
Source§fn add_example<'life0, 'async_trait>(
&'life0 self,
example: PromptArgs,
) -> Pin<Box<dyn Future<Output = Result<(), PromptError>> + Send + 'async_trait>>where
Self: 'async_trait,
'life0: 'async_trait,
fn add_example<'life0, 'async_trait>(
&'life0 self,
example: PromptArgs,
) -> Pin<Box<dyn Future<Output = Result<(), PromptError>> + Send + 'async_trait>>where
Self: 'async_trait,
'life0: 'async_trait,
Adds an example to the pool the selector chooses from.
Source§fn select_examples<'life0, 'life1, 'async_trait>(
&'life0 self,
input_variables: &'life1 PromptArgs,
) -> Pin<Box<dyn Future<Output = Result<Vec<PromptArgs>, PromptError>> + Send + 'async_trait>>where
Self: 'async_trait,
'life0: 'async_trait,
'life1: 'async_trait,
fn select_examples<'life0, 'life1, 'async_trait>(
&'life0 self,
input_variables: &'life1 PromptArgs,
) -> Pin<Box<dyn Future<Output = Result<Vec<PromptArgs>, PromptError>> + Send + 'async_trait>>where
Self: 'async_trait,
'life0: 'async_trait,
'life1: 'async_trait,
The examples to use for
input_variables (the prompt’s inputs), in the order
they should appear.Auto Trait Implementations§
impl !Freeze for SemanticSimilarityExampleSelector
impl !RefUnwindSafe for SemanticSimilarityExampleSelector
impl !UnwindSafe for SemanticSimilarityExampleSelector
impl Send for SemanticSimilarityExampleSelector
impl Sync for SemanticSimilarityExampleSelector
impl Unpin for SemanticSimilarityExampleSelector
impl UnsafeUnpin for SemanticSimilarityExampleSelector
Blanket Implementations§
Source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Mutably borrows from an owned value. Read more
impl<ST, DT> CastableFrom<ST, Initialized, Initialized> for DT
impl<ST, DT> CastableFrom<ST, Uninit, Uninit> for DT
impl<T> ErasedDestructor for Twhere
T: 'static,
Source§impl<T> Instrument for T
impl<T> Instrument for T
Source§fn instrument(self, span: Span) -> Instrumented<Self> ⓘ
fn instrument(self, span: Span) -> Instrumented<Self> ⓘ
Source§fn in_current_span(self) -> Instrumented<Self> ⓘ
fn in_current_span(self) -> Instrumented<Self> ⓘ
Source§impl<T> IntoEither for T
impl<T> IntoEither for T
Source§fn into_either(self, into_left: bool) -> Either<Self, Self> ⓘ
fn into_either(self, into_left: bool) -> Either<Self, Self> ⓘ
Converts
self into a Left variant of Either<Self, Self>
if into_left is true.
Converts self into a Right variant of Either<Self, Self>
otherwise. Read moreSource§fn into_either_with<F>(self, into_left: F) -> Either<Self, Self> ⓘ
fn into_either_with<F>(self, into_left: F) -> Either<Self, Self> ⓘ
Converts
self into a Left variant of Either<Self, Self>
if into_left(&self) returns true.
Converts self into a Right variant of Either<Self, Self>
otherwise. Read more