Poll for prediction completion
(
&self,
prediction_id: &str,
)
| 350 | impl ReplicateProvider { |
| 351 | /// Poll for prediction completion |
| 352 | async fn poll_prediction( |
| 353 | &self, |
| 354 | prediction_id: &str, |
| 355 | ) -> GraphBitResult<ReplicatePredictionResponse> { |
| 356 | let url = format!("{}/predictions/{}", self.base_url, prediction_id); |
| 357 | let mut attempts = 0; |
| 358 | const MAX_ATTEMPTS: u32 = 60; // 5 minutes with 5-second intervals |
| 359 | |
| 360 | loop { |
| 361 | tokio::time::sleep(Duration::from_secs(5)).await; |
| 362 | attempts += 1; |
| 363 | |
| 364 | let response = self |
| 365 | .client |
| 366 | .get(&url) |
| 367 | .header("Authorization", format!("Token {}", self.api_key)) |
| 368 | .send() |
| 369 | .await |
| 370 | .map_err(|e| { |
| 371 | GraphBitError::llm_provider("replicate", format!("Polling request failed: {e}")) |
| 372 | })?; |
| 373 | |
| 374 | if !response.status().is_success() { |
| 375 | let error_text = response |
| 376 | .text() |
| 377 | .await |
| 378 | .unwrap_or_else(|_| "Unknown error".to_string()); |
| 379 | return Err(GraphBitError::llm_provider( |
| 380 | "replicate", |
| 381 | format!("Polling API error: {error_text}"), |
| 382 | )); |
| 383 | } |
| 384 | |
| 385 | let prediction: ReplicatePredictionResponse = response.json().await.map_err(|e| { |
| 386 | GraphBitError::llm_provider( |
| 387 | "replicate", |
| 388 | format!("Failed to parse polling response: {e}"), |
| 389 | ) |
| 390 | })?; |
| 391 | |
| 392 | match prediction.status.as_str() { |
| 393 | "succeeded" => return Ok(prediction), |
| 394 | "failed" | "canceled" => { |
| 395 | let error_msg = prediction.error.unwrap_or_else(|| { |
| 396 | format!( |
| 397 | "Prediction {} with status: {}", |
| 398 | prediction.status, prediction.status |
| 399 | ) |
| 400 | }); |
| 401 | return Err(GraphBitError::llm_provider("replicate", error_msg)); |
| 402 | } |
| 403 | "starting" | "processing" => { |
| 404 | if attempts >= MAX_ATTEMPTS { |
| 405 | return Err(GraphBitError::llm_provider( |
| 406 | "replicate", |
| 407 | "Prediction timed out after 5 minutes".to_string(), |
| 408 | )); |
| 409 | } |
no test coverage detected