From 68882ecd4dced38e92b6ef581a30e45358fc61e0 Mon Sep 17 00:00:00 2001 From: sigoden Date: Mon, 29 Apr 2024 08:43:23 +0800 Subject: refactor: abstract event stream handling (#458) --- src/client/cohere.rs | 11 ++++------- 1 file changed, 4 insertions(+), 7 deletions(-) (limited to 'src/client/cohere.rs') diff --git a/src/client/cohere.rs b/src/client/cohere.rs index de2a11c..8586b9d 100644 --- a/src/client/cohere.rs +++ b/src/client/cohere.rs @@ -28,7 +28,7 @@ impl CohereClient { [("api_key", "API Key:", false, PromptKind::String)]; fn request_builder(&self, client: &ReqwestClient, data: SendData) -> Result { - let api_key = self.get_api_key().ok(); + let api_key = self.get_api_key()?; let body = build_body(data, &self.model)?; @@ -36,10 +36,7 @@ impl CohereClient { debug!("Cohere Request: {url} {body}"); - let mut builder = client.post(url).json(&body); - if let Some(api_key) = api_key { - builder = builder.bearer_auth(api_key); - } + let builder = client.post(url).bearer_auth(api_key).json(&body); Ok(builder) } @@ -55,7 +52,7 @@ async fn send_message(builder: RequestBuilder) -> Result<(String, CompletionDeta catch_error(&data, status.as_u16())?; } - cohere_extract_completion(&data) + extract_completion(&data) } async fn send_message_streaming(builder: RequestBuilder, handler: &mut SseHandler) -> Result<()> { @@ -156,7 +153,7 @@ fn build_body(data: SendData, model: &Model) -> Result { Ok(body) } -fn cohere_extract_completion(data: &Value) -> Result<(String, CompletionDetails)> { +fn extract_completion(data: &Value) -> Result<(String, CompletionDetails)> { let text = data["text"] .as_str() .ok_or_else(|| anyhow!("Invalid response data: {data}"))?; -- cgit v1.2.3