Expand description
Portable embedding requests and results.
Select one exact Model before submitting an EmbedRequest. Input order maps
directly to EmbedResponse::vectors. Requested dimensions are optional and
model support is authoritative at the operation boundary.
§Embed an ordered collection
This example needs an OpenAI key and access to the selected embedding model. Only compare or store vectors using the same selected model/dimension convention. The OpenAI operation validates vector count and index correlation. This example separately checks that each vector has the requested dimensions; neither check establishes semantic retrieval quality.
use providers_kit::{Provider, ProviderBackend, ProviderConfig, ModelBackend, ModelId};
use providers_kit::config::{ApiKey, OpenAiConfig, ProviderKind, TransportConfig};
use providers_kit::embedding::EmbedRequest;
let provider = Provider::from_config(ProviderConfig::new(
ProviderKind::OpenAi(OpenAiConfig::new(ApiKey::new("replace-with-your-key")?)),
TransportConfig::default(),
))?;
let work = async {
let model = provider.model(ModelId::new("text-embedding-3-small")?)?;
let request = EmbedRequest::new(vec!["first document".into(), "second document".into()])?
.with_dimensions(std::num::NonZeroU32::new(256).unwrap());
let response = model.embed(&request).await?;
assert_eq!(response.vectors.len(), request.inputs().len());
assert!(response.vectors.iter().all(|vector| vector.len() == 256));
Ok::<_, providers_kit::KitError>(response)
}.await;
let cleanup = provider.shutdown().await;
let _ordered_vectors = work?;
cleanup?;Structs§
- Embed
Request - Validated text-embedding request.
- Embed
Response - Embedding vectors returned in request-input order.