pub struct PostgresVectorRetriever<T>where
T: AsyncEmbeddingClient,{ /* private fields */ }
Expand description
§PostgresVectorRetriever
This struct is a allows for the retrieval of similar text from a postgres database. It is parameterized over a type T which implements the AsyncEmbeddingClient trait. This is because text needs to be embeded before it can be compared to other text. You must connect first create a PostgresVectorStore as this handles connecting to the database. then you can calle .as_retriever() to convert it to retriever.
§Examples
use rag_toolchain::retrievers::*;
use rag_toolchain::clients::*;
use rag_toolchain::common::*;
use rag_toolchain::stores::*;
use std::num::NonZeroU32;
async fn retrieve() {
let chunk: Chunk = Chunk::new("This is the text you want to retrieve something similar to");
let top_k: NonZeroU32 = NonZeroU32::new(5).unwrap();
let distance_function: DistanceFunction = DistanceFunction::Cosine;
let embedding_model: OpenAIEmbeddingModel = OpenAIEmbeddingModel::TextEmbedding3Small;
let client: OpenAIEmbeddingClient = OpenAIEmbeddingClient::try_new(embedding_model).unwrap();
let store: PostgresVectorStore = PostgresVectorStore::try_new("table_name", embedding_model).await.unwrap();
let retriever: PostgresVectorRetriever<OpenAIEmbeddingClient> = store.as_retriever(client, distance_function);
// This will return the top 5 most similar chunks to the input text.
let similar_text: Chunks = retriever.retrieve(chunk.content(), top_k).await.unwrap();
}
Trait Implementations§
Source§impl<T> AsyncRetriever for PostgresVectorRetriever<T>
impl<T> AsyncRetriever for PostgresVectorRetriever<T>
Source§async fn retrieve(
&self,
text: &str,
top_k: NonZeroU32,
) -> Result<Chunks, Self::ErrorType>
async fn retrieve( &self, text: &str, top_k: NonZeroU32, ) -> Result<Chunks, Self::ErrorType>
§PostgresVectorRetriever::retrieve
Implementation of the retrieve function for PostgresVectorRetriever
.
This allows us to retrieve similar text from the vector database.
§Arguments
text
: &str
- The text we are searching for similar text against.top_k
:NonZeroU32
- The number of results to return.
§Errors
PostgresRetrieverError::EmbeddingClientError
- If the embedding client returns an error.PostgresRetrieverError::QueryError
- If there is an error querying the database.
§Returns
Chunks
which are the most similar to the input text.
Source§type ErrorType = PostgresRetrieverError<<T as AsyncEmbeddingClient>::ErrorType>
type ErrorType = PostgresRetrieverError<<T as AsyncEmbeddingClient>::ErrorType>
Custom error type for the retriever
Auto Trait Implementations§
impl<T> Freeze for PostgresVectorRetriever<T>where
T: Freeze,
impl<T> !RefUnwindSafe for PostgresVectorRetriever<T>
impl<T> Send for PostgresVectorRetriever<T>where
T: Send,
impl<T> Sync for PostgresVectorRetriever<T>where
T: Sync,
impl<T> Unpin for PostgresVectorRetriever<T>where
T: Unpin,
impl<T> !UnwindSafe for PostgresVectorRetriever<T>
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
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