baseten_performance_client_core to send concurrent inference requests from a Tokio application.
See the Performance Client overview for shared batching, retry, and connection settings.
Installation
First request
Set your API key, an embeddings deployment URL, and themodel value expected by that deployment:
src/main.rs in your Cargo project:
src/main.rs
cargo run. A successful response prints 2 embeddings and the elapsed time. For reranking, classification, or generic requests, set the base URL to a deployment that supports the corresponding operation.
PerformanceClientCore
Base URL for requests. Inference methods append their request path. With an endpoint pool, the first pool endpoint becomes the primary URL.
Explicit key, or
None to read BASETEN_API_KEY, then OPENAI_API_KEY. Missing all three returns InvalidParameter.Proxy URL for a newly created connection pool. If you supply a wrapper, the client uses the wrapper’s proxy configuration.
HttpClientWrapper::new(http_version, proxy)? to create an Arc<HttpClientWrapper>. Clone the Arc into multiple clients to share connections. client.get_client_wrapper() returns a clone of the client’s wrapper.
Inference methods
All methods are asynchronous and returnResult<_, ClientError>. Configure each call with a RequestProcessingPreference.
Generate embeddings
/v1/embeddings. Returns (CoreOpenAIEmbeddingsResponse, Vec<Duration>, Vec<HeaderMap>, Duration).
Batching, concurrency, timeout, and retry settings for the call.
CoreOpenAIEmbeddingsResponse combines results from all batches:
CoreOpenAIEmbeddingData
Match as
CoreEmbeddingVariant::FloatVector(Vec<f32>) or CoreEmbeddingVariant::Base64(String).CoreOpenAIUsage
Rerank texts
/rerank. Returns (CoreRerankResponse, Vec<Duration>, Vec<HeaderMap>, Duration).
Batching, concurrency, timeout, and retry settings for the call.
CoreRerankResponse combines results from all batches:
CoreRerankResult
Classify texts
/predict, wrapping each input string in a single-element list. Returns (CoreClassificationResponse, Vec<Duration>, Vec<HeaderMap>, Duration).
Batching, concurrency, timeout, and retry settings for the call.
CoreClassificationResult
Send generic requests
serde_json = "1" to your dependencies to construct payloads.
POST, GET, PUT, PATCH, DELETE, HEAD, or OPTIONS. Only POST, PUT, and PATCH send request bodies. DELETE, HEAD, and OPTIONS return empty maps instead of decoded response bodies.(Vec<(rmpv::Value, HeaderMap, Duration)>, Duration):
Response body, headers, and duration for each payload in input order.
Response timing
Embedding, reranking, and classification methods return a four-element tuple. Tuple positions are zero-based:Merged response. The response type and
data fields depend on the method. All three response types include the timing and header fields below.Request preferences
RequestProcessingPreference::new() and Default::default() leave every field as None. At request start, the client applies the defaults below to any unset fields. Set values with with_<field>(value) builders or assign Some(value) to public fields.
Maximum concurrent requests. Effective default:
128. Must be 1 to 1,024, or 1 to 512 when batch_size is below 16.Character threshold for splitting batches, from 50 to 1,048,576. Defaults to
8000. A single text that exceeds the threshold stays intact.Send all initial requests in this call to one endpoint from the pool. Effective default:
false.Delay in seconds before a duplicate attempt. Unset by default. Must be at least 0.045 seconds and less than
timeout_s - 0.045. Enabling hedging can make your model process the same input more than once.Deadline in seconds for the whole operation. Unset by default; must be at least
timeout_s when set.max_retries limit still applies. Leave hedge_delay unset to turn off hedging.
Maximum retries per request, from 0 to 6. Effective default:
5. By default, the client retries HTTP statuses 408, 409, 429, and 500–599. Set to 0 to turn off retries.Shared cancellation flag. The client creates a new token when unset. Calling
cancel() alone doesn’t stop requests. See cancellation behavior.API key sent with this call’s requests in place of the client’s key. Unset by default.
Cancellation and errors
Dropping an inference future aborts its in-flight request tasks. For example, use a timeout around the future or abort the Tokio task that owns it.CancellationToken::new(false) creates a shared flag. token.cancel() sets it, and token.is_cancelled() reads it. Inference requests don’t poll this flag, so calling token.cancel() alone doesn’t stop requests.
Match ClientError to handle failures:
Endpoint pools
Create endpoints inside a Tokio runtime, becauseEndpoint::new starts background health checks.
- Create a shared
HttpClientWrapper. - Build
EndpointConfig::new(base_url, health_api_key, wrapper)for each endpoint, then callEndpoint::new(config)?. - Pass the endpoints to
EndpointPoolConfig::new(endpoints). Optionally set.with_weights(weights). - Call
EndpointPool::new(config)?and pass itsArcto the client constructor.
Pool configuration
EndpointPoolConfig accepts these fields:
Routing weights, initially
None. When set, weights must match the endpoint count, be finite and nonnegative, and include at least one positive value.Health snapshot
pool.health_snapshot() returns an EndpointPoolHealthSnapshot:
EndpointHealthStatus
Endpoint configuration
EndpointConfig provides builders for these fields:
Interval between health checks. Defaults to 10 seconds. Set with
.with_health_check_interval(duration).Retry attempt concurrency limit. Minimum:
4. Set with .with_retry_attempt_concurrency_limit(limit).Custom health checks, initially
None. When unset, the endpoint uses a relative /health check. Set with .with_endpoint_health(config)..with_standard_health_checks(deep_health_url, fail_on_first, deployment_health_path, deployment_timeout_is_no_vote, deep_timeout_is_no_vote) to configure deployment and optional deep health checks together.
For custom checks, create an EndpointHealthConfig with these fields:
Health checks created with
EndpointHealthCheckConfig::relative(path) or ::absolute(url).EndpointHealthCheckConfig supports this option:
Ignore a timeout from this check when deciding whether the endpoint is healthy. Set with
.with_timeout_is_no_vote(value).Environment and crate features
The crate enables therustls TLS feature by default. native-tls is also available. The crate initializes a tracing subscriber automatically.