Skip to content

Commit

Permalink
Fix Header Handling for Azure-Specific Authorization (#162)
Browse files Browse the repository at this point in the history
# Description
This pull request fixes #161 by updating header management for
Azure-specific authorization requirements in both the
`lib/modelHelpers.py` and `Images/ai-images.py` files.

## Changes

1. **lib/modelHelpers.py:**
- **Moved header setup:** The Authorization and Content-Type headers
were moved closer to the request execution.
- **Conditional logic for Azure endpoints:** For Azure endpoints, the
api-key header is used instead of Authorization. Non-Azure endpoints
continue using the Authorization header.

2. **Images/ai-images.py:**
- **Modified header setup:** Added conditional logic to handle the
api-key header for Azure and Authorization for OpenAI.

## Note
While the headers have been updated for Azure compatibility, image
generation will still not function with Azure, as the endpoint remains
hardcoded to OpenAI. A future PR will be required to support Azure image
generation.

---------

Co-authored-by: pre-commit-ci[bot] <66853113+pre-commit-ci[bot]@users.noreply.github.com>
Co-authored-by: Colton Loftus <[email protected]>
  • Loading branch information
3 people authored Sep 26, 2024
1 parent e98c2d0 commit 52bb75e
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 9 deletions.
11 changes: 7 additions & 4 deletions Images/ai-images.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,10 +15,13 @@ def image_generate(prompt: str):

url = "https://api.openai.com/v1/images/generations"
TOKEN = get_token()
headers = {
"Content-Type": "application/json",
"Authorization": f"Bearer {TOKEN}",
}
headers = {"Content-Type": "application/json"}
# If the model endpoint is Azure, we need to use a different header
if "azure.com" in url:
headers["api-key"] = TOKEN
# otherwise default to the standard header format for openai
else:
headers["Authorization"] = f"Bearer {TOKEN}"
data = {
"model": "dall-e-3",
"prompt": prompt,
Expand Down
12 changes: 7 additions & 5 deletions lib/modelHelpers.py
Original file line number Diff line number Diff line change
Expand Up @@ -133,11 +133,6 @@ def send_request(

system_messages += GPTState.context

headers = {
"Content-Type": "application/json",
"Authorization": f"Bearer {TOKEN}",
}

content: list[GPTMessageItem] = []
if content_to_process is not None:
if content_to_process["type"] == "image_url":
Expand Down Expand Up @@ -180,6 +175,13 @@ def send_request(
data["tools"] = tools

url: str = settings.get("user.model_endpoint") # type: ignore
headers = {"Content-Type": "application/json"}
# If the model endpoint is Azure, we need to use a different header
if "azure.com" in url:
headers["api-key"] = TOKEN
else:
headers["Authorization"] = f"Bearer {TOKEN}"

raw_response = requests.post(url, headers=headers, data=json.dumps(data))

match raw_response.status_code:
Expand Down

0 comments on commit 52bb75e

Please sign in to comment.