diff options
| author | sigoden <sigoden@gmail.com> | 2024-04-29 08:43:23 +0800 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2024-04-29 08:43:23 +0800 |
| commit | 68882ecd4dced38e92b6ef581a30e45358fc61e0 (patch) | |
| tree | 356a3de5ff8d3a0cb7defea1ad91924894f2f9df /src/client/cohere.rs | |
| parent | 37a0cd08a92f07ef24e39bdab7c8bced5b59c146 (diff) | |
| download | aichat-68882ecd4dced38e92b6ef581a30e45358fc61e0.tar.gz | |
refactor: abstract event stream handling (#458)
Diffstat (limited to 'src/client/cohere.rs')
| -rw-r--r-- | src/client/cohere.rs | 11 |
1 files changed, 4 insertions, 7 deletions
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<RequestBuilder> { - 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<Value> { 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}"))?; |
