diff options
Diffstat (limited to 'src/client/common.rs')
| -rw-r--r-- | src/client/common.rs | 19 |
1 files changed, 15 insertions, 4 deletions
diff --git a/src/client/common.rs b/src/client/common.rs index d0c23e9..58aaf3a 100644 --- a/src/client/common.rs +++ b/src/client/common.rs @@ -77,8 +77,14 @@ pub trait Client: Sync + Send { return Ok(ChatCompletionsOutput::new(&content)); } let client = self.build_client()?; - let data = input.prepare_completion_data(self.model(), false)?; + let mut data = input.prepare_completion_data(self.model(), false)?; let retries = self.extra_config().map(|v| v.retries()).unwrap_or(0); + if self.extra_config().is_some_and(|v| v.idempotency()) { + // A single key is generated once and reused across every retry so + // the provider debounces duplicate submissions for this logical + // request. + data.idempotency_key = Some(uuid::Uuid::new_v4().to_string()); + } if retries == 0 { return self .chat_completions_inner(&client, data) @@ -132,7 +138,10 @@ pub trait Client: Sync + Send { return Ok(()); } let client = self.build_client()?; - let data = input.prepare_completion_data(self.model(), true)?; + let mut data = input.prepare_completion_data(self.model(), true)?; + if self.extra_config().is_some_and(|v| v.idempotency()) { + data.idempotency_key = Some(uuid::Uuid::new_v4().to_string()); + } let retry_delay = self .extra_config() .map(|v| v.retry_delay()) @@ -222,10 +231,11 @@ pub trait Client: Sync + Send { &self, client: &reqwest::Client, mut request_data: RequestData, + idempotency_key: Option<&str>, ) -> RequestBuilder { self.patch_request_data(&mut request_data); - if self.extra_config().is_some_and(|v| v.idempotency()) { - request_data.header("idempotency-key", uuid::Uuid::new_v4().to_string()); + if let Some(key) = idempotency_key { + request_data.header("idempotency-key", key); } request_data.into_builder(client) } @@ -398,6 +408,7 @@ pub struct ChatCompletionsData { pub top_p: Option<f64>, pub functions: Option<Vec<FunctionDeclaration>>, pub stream: bool, + pub idempotency_key: Option<String>, } #[derive(Debug, Clone, Default)] |
