-
-
Notifications
You must be signed in to change notification settings - Fork 1.3k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Support Azure OpenAI API endpoint as Chat Provider #1048
Conversation
api_key=openai_api_key, | ||
base_url=api_base_url, | ||
) | ||
if api_base_url and "openai.azure.com" in api_base_url: |
Check failure
Code scanning / CodeQL
Incomplete URL substring sanitization High
Show autofix suggestion
Hide autofix suggestion
Copilot Autofix AI 6 days ago
To fix the problem, we need to ensure that the api_base_url
is properly parsed and its hostname is checked to confirm it matches the expected domain. This can be achieved by using the urlparse
function from the urllib.parse
module to extract the hostname and then verifying that it ends with the expected domain.
- Parse the
api_base_url
usingurlparse
. - Extract the hostname from the parsed URL.
- Check if the hostname ends with the expected domain ".openai.azure.com".
-
Copy modified line R23 -
Copy modified lines R55-R62
@@ -22,2 +22,3 @@ | ||
from khoj.utils.helpers import get_chat_usage_metrics, is_promptrace_enabled | ||
from urllib.parse import urlparse | ||
|
||
@@ -53,8 +54,10 @@ | ||
if not client: | ||
if api_base_url and "openai.azure.com" in api_base_url: | ||
client = openai.AzureOpenAI( | ||
api_key=openai_api_key, | ||
azure_endpoint=api_base_url, | ||
api_version="2024-10-21", | ||
) | ||
if api_base_url: | ||
parsed_url = urlparse(api_base_url) | ||
if parsed_url.hostname and parsed_url.hostname.endswith("openai.azure.com"): | ||
client = openai.AzureOpenAI( | ||
api_key=openai_api_key, | ||
azure_endpoint=api_base_url, | ||
api_version="2024-10-21", | ||
) | ||
else: |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I want to work with khoj
e057838
to
275d70e
Compare
275d70e
to
f6948a2
Compare
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
🚀
OpenAI chat models deployed on Azure are (ironically) not OpenAI API compatible endpoints.
This change enables using OpenAI chat models deployed on Azure with Khoj.