pub async fn forward_request(
client: &Client,
upstream_url: &str,
path: &str,
body: &Value,
headers: Vec<(String, String)>,
timeout: Duration,
) -> Result<Response, GuardrailError>Expand description
Forward a (possibly redacted) request body to the upstream LLM API.
§Arguments
client— a sharedreqwest::Client(connection-pooled).upstream_url— base URL of the upstream, e.g.https://api.openai.com.path— the request path and query string, e.g./v1/chat/completionsor/openai/deployments/prod/chat/completions?api-version=2024-10-21.body— the JSON body to send (seecrate::translate::ScannedBody::apply).headers— headers from the original client request to forward (seeforwardable_headers).timeout— per-request timeout.
§Errors
Returns GuardrailError::Upstream if the request fails to send or the
connection is refused/times out.
§Examples
use guardrail_proxy::forward::forward_request;
use reqwest::Client;
use std::time::Duration;
let client = Client::new();
let body = serde_json::json!({"model": "gpt-4o", "messages": []});
let resp = forward_request(
&client,
"https://api.openai.com",
"/v1/chat/completions",
&body,
vec![("authorization".to_string(), "Bearer sk-...".to_string())],
Duration::from_secs(60),
).await;