Skip to content

Commit

Permalink
refactor: improve azure-openai (#652)
Browse files Browse the repository at this point in the history
  • Loading branch information
sigoden committed Jun 26, 2024
1 parent 95bad97 commit 2e1dc1f
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 15 deletions.
32 changes: 20 additions & 12 deletions Argcfile.sh
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
Expand Down Expand Up @@ -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" \

}
Expand Down
12 changes: 9 additions & 3 deletions src/client/azure_openai.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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);

Expand All @@ -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)
}
Expand Down

0 comments on commit 2e1dc1f

Please sign in to comment.