From 2e1dc1f7d58f037e83a184f4c9e662696d1425be Mon Sep 17 00:00:00 2001 From: sigoden Date: Wed, 26 Jun 2024 13:48:55 +0800 Subject: [PATCH] refactor: improve azure-openai (#652) --- Argcfile.sh | 32 ++++++++++++++++++++------------ src/client/azure_openai.rs | 12 +++++++++--- 2 files changed, 29 insertions(+), 15 deletions(-) diff --git a/Argcfile.sh b/Argcfile.sh index 5f04b6f0..7433e26a 100755 --- a/Argcfile.sh +++ b/Argcfile.sh @@ -159,7 +159,11 @@ models() { # @flag -S --no-stream # @arg text~ chat-openai-compatible() { - _openai_chat "$@" + _wrapper curl -i "$argc_api_base/chat/completions" \ +-X POST \ +-H "Content-Type: application/json" \ +-H "Authorization: Bearer $argc_api_key" \ +-d "$(_build_body openai "$@")" } # @cmd List models by openai-compatible api @@ -169,6 +173,20 @@ models-openai-compatible() { _openai_models } +# @cmd Chat with azure-openai api +# @option --api-url! $$ +# @option --api-key! $$ +# @option -m --model! $$ +# @flag -S --no-stream +# @arg text~ +chat-azure-openai() { + _wrapper curl -i "$argc_api_url" \ +-X POST \ +-H "Content-Type: application/json" \ +-H "api-key: $argc_api_key" \ +-d "$(_build_body openai "$@")" +} + # @cmd Chat with gemini api # @env GEMINI_API_KEY! # @option -m --model=gemini-1.0-pro-latest $GEMINI_MODEL @@ -397,20 +415,10 @@ _argc_before() { fi } -_openai_chat() { - api_base="${api_base:-"$argc_api_base"}" - api_key="${api_key:-"$argc_api_key"}" - _wrapper curl -i $curl_args "$api_base/chat/completions" \ --X POST \ --H "Content-Type: application/json" \ --H "Authorization: Bearer $api_key" \ --d "$(_build_body openai "$@")" -} - _openai_models() { api_base="${api_base:-"$argc_api_base"}" api_key="${api_key:-"$argc_api_key"}" - _wrapper curl $curl_args "$api_base/models" \ + _wrapper curl "$api_base/models" \ -H "Authorization: Bearer $api_key" \ } diff --git a/src/client/azure_openai.rs b/src/client/azure_openai.rs index 058a53e1..e3e9f165 100644 --- a/src/client/azure_openai.rs +++ b/src/client/azure_openai.rs @@ -49,7 +49,7 @@ impl AzureOpenAIClient { self.model.name() ); - debug!("AzureOpenAI Request: {url} {body}"); + debug!("AzureOpenAI Chat Completions Request: {url} {body}"); let builder = client.post(url).header("api-key", api_key).json(&body); @@ -66,9 +66,15 @@ impl AzureOpenAIClient { let body = openai_build_embeddings_body(data, &self.model); - let url = format!("{api_base}/embeddings"); + let url = format!( + "{}/openai/deployments/{}/embeddings?api-version=2024-02-01", + &api_base, + self.model.name() + ); + + debug!("AzureOpenAI Embeddings Request: {url} {body}"); - let builder = client.post(url).bearer_auth(api_key).json(&body); + let builder = client.post(url).header("api-key", api_key).json(&body); Ok(builder) }