Skip to content
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

Merged
merged 1 commit into from
Jan 10, 2025

Conversation

debanjum
Copy link
Member

@debanjum debanjum commented Jan 8, 2025

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.

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

The string openai.azure.com may be at an arbitrary position in the sanitized URL.

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 using urlparse.
  • Extract the hostname from the parsed URL.
  • Check if the hostname ends with the expected domain ".openai.azure.com".
Suggested changeset 1
src/khoj/processor/conversation/openai/utils.py

Autofix patch

Autofix patch
Run the following command in your local git repository to apply this patch
cat << 'EOF' | git apply
diff --git a/src/khoj/processor/conversation/openai/utils.py b/src/khoj/processor/conversation/openai/utils.py
--- a/src/khoj/processor/conversation/openai/utils.py
+++ b/src/khoj/processor/conversation/openai/utils.py
@@ -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:
EOF
@@ -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:
Copilot is powered by AI and may make mistakes. Always verify output.
Unable to commit as this autofix suggestion is now outdated
Positive Feedback
Negative Feedback

Provide additional feedback

Please help us improve GitHub Copilot by sharing more details about this comment.

Please select one or more of the options

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

@debanjum debanjum force-pushed the support-azure-openai-api-endpoint branch from e057838 to 275d70e Compare January 9, 2025 03:43
src/khoj/utils/helpers.py Fixed Show fixed Hide fixed
@debanjum debanjum force-pushed the support-azure-openai-api-endpoint branch from 275d70e to f6948a2 Compare January 9, 2025 03:48
Copy link
Member

@sabaimran sabaimran left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🚀

@debanjum debanjum merged commit 3cc6597 into master Jan 10, 2025
10 checks passed
@debanjum debanjum deleted the support-azure-openai-api-endpoint branch January 10, 2025 16:41
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants