pub struct OpenAIEmbeddingClient { /* private fields */ }
Expand description
§OpenAIEmbeddingClient
Allows for interacting with the OpenAI API to generate embeddings. You can either embed a single string or a batch of strings.
§Examples
use rag_toolchain::common::*;
use rag_toolchain::clients::*;
async fn generate_embedding() {
let client: OpenAIEmbeddingClient = OpenAIEmbeddingClient::try_new(OpenAIEmbeddingModel::TextEmbeddingAda002).unwrap();
let chunk: Chunk = Chunk::new("this would be the text you are embedding");
let embedding: Embedding = client.generate_embedding(chunk).await.unwrap();
// This would be the vector representation of the text
let vector: Vec<f32> = embedding.vector();
}
§Required Environment Variables
OPENAI_API_KEY: The API key to use for the OpenAI API
Implementations§
Source§impl OpenAIEmbeddingClient
impl OpenAIEmbeddingClient
Sourcepub fn try_new(
embedding_model: OpenAIEmbeddingModel,
) -> Result<OpenAIEmbeddingClient, VarError>
pub fn try_new( embedding_model: OpenAIEmbeddingModel, ) -> Result<OpenAIEmbeddingClient, VarError>
§OpenAIEmbeddingClient::try_new
Constructor to create a new OpenAIEmbeddingClient. This will fail if the OPENAI_API_KEY environment variable is not set.
§Arguments
embedding_model
:OpenAIEmbeddingModel
- The model to use for the embeddings
§Errors
VarError
- If the OPENAI_API_KEY environment variable is not set.
§Returns
OpenAIEmbeddingClient
- The newly created OpenAIEmbeddingClient
Trait Implementations§
Source§impl AsyncEmbeddingClient for OpenAIEmbeddingClient
impl AsyncEmbeddingClient for OpenAIEmbeddingClient
Source§async fn generate_embeddings(
&self,
text: Chunks,
) -> Result<Vec<Embedding>, OpenAIError>
async fn generate_embeddings( &self, text: Chunks, ) -> Result<Vec<Embedding>, OpenAIError>
§OpenAIEmbeddingClient::generate_embeddings
Function to generate embeddings for Chunks
.
Allows you to get an embedding for multiple strings.
§Arguments
text
:Chunk
- The text chunks/strings to generate an embeddings for.
§Errors
OpenAIError
- If the request to OpenAI fails.
§Returns
Vec<Embedding>
- A result containing pairs of the original text and the embedding that was generated.
Source§async fn generate_embedding(
&self,
text: Chunk,
) -> Result<Embedding, Self::ErrorType>
async fn generate_embedding( &self, text: Chunk, ) -> Result<Embedding, Self::ErrorType>
§OpenAIEmbeddingClient::generate_embedding
Function to generate an embedding for a Chunk
.
Allows you to get an embedding for a single string.
§Arguments
text
:Chunk
- The text chunk/string to generate an embedding for.
§Errors
OpenAIError
- If the request to OpenAI fails.
§Returns
Embedding
- the generated embedding
type ErrorType = OpenAIError
Auto Trait Implementations§
impl Freeze for OpenAIEmbeddingClient
impl !RefUnwindSafe for OpenAIEmbeddingClient
impl Send for OpenAIEmbeddingClient
impl Sync for OpenAIEmbeddingClient
impl Unpin for OpenAIEmbeddingClient
impl !UnwindSafe for OpenAIEmbeddingClient
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