Skip to content

Conversation

@caiorg
Copy link

@caiorg caiorg commented Jun 19, 2025

Description

This pull request introduces support for using Azure AI Foundry (Azure OpenAI Service) as an alternative backend for LLM and embedding operations in the Crawl4AI RAG MCP Server. Now it's possible to configure the server to use your Azure OpenAI deployments instead of the default OpenAI API.

Motivation

The primary motivations for this change are to offer greater flexibility and control by enabling the use of Azure OpenAI Service. This allows to:

  • Leverage an existing Microsoft Azure infrastructure and billing.
  • Potentially manage costs for LLM and embedding operations differently.
  • Align with specific compliance and data governance frameworks available within Azure.

An additional consideration may be the evolving landscape of data privacy and retention in AI services. For instance, recent discussions, such as those highlighted by Theo - t3.gg (OpenAI's privacy disaster (it isn’t their fault)) concerning legal proceedings like The New York Times lawsuit, underscore the importance of understanding data handling policies. OpenAI has also provided its perspective on such matters (see OpenAI's response on their blog). By offering Azure OpenAI as an alternative, comes an additional option to select a service provider whose data processing and retention policies, within the Azure ecosystem, that aligns with specific requirements and comfort levels, particularly when dealing with sensitive information.

Key Changes

  • src/utils.py:
    • Modified to conditionally initialize an AzureOpenAI client if Azure-specific environment variables are provided (AZURE_OPENAI_API_KEY, AZURE_OPENAI_ENDPOINT, AZURE_OPENAI_API_VERSION).
    • Updated functions (create_embeddings_batch, create_embedding, generate_contextual_embedding, generate_code_example_summary, extract_source_summary) to use the Azure client for API calls if it's initialized and relevant deployment names (AZURE_OPENAI_CHAT_DEPLOYMENT, AZURE_OPENAI_EMBEDDING_DEPLOYMENT) are set.
    • Includes fallback logic to the standard OpenAI client if Azure is not configured or if Azure API calls fail.
  • README.md:
    • Updated the "Configuration" section to include documentation for the new Azure OpenAI environment variables:
      • AZURE_OPENAI_API_KEY
      • AZURE_OPENAI_ENDPOINT
      • AZURE_OPENAI_API_VERSION
      • AZURE_OPENAI_CHAT_DEPLOYMENT
      • AZURE_OPENAI_EMBEDDING_DEPLOYMENT
    • Provided clear instructions on how to set up these variables and notes on their behavior (priority, fallback, compatibility).
  • tests/test_utils.py (if including tests):
    • Added unit tests to verify the Azure OpenAI integration, including successful calls via Azure, fallback to standard OpenAI when Azure is not configured, and fallback during Azure API errors.

How to Configure for Azure OpenAI

  1. Set the following environment variables in the .env file:
    • AZURE_OPENAI_API_KEY: Your Azure OpenAI API key.
    • AZURE_OPENAI_ENDPOINT: Your Azure OpenAI resource endpoint (e.g., https://your-resource.openai.azure.com/).
    • AZURE_OPENAI_API_VERSION: The API version for your Azure OpenAI service (e.g., 2023-07-01-preview).
    • AZURE_OPENAI_CHAT_DEPLOYMENT: The name of your chat model deployment in Azure.
    • AZURE_OPENAI_EMBEDDING_DEPLOYMENT: The name of your embedding model deployment in Azure.

The server will automatically prioritize Azure OpenAI if these core variables are correctly configured.

Considerations

  • This implementation adds Azure OpenAI as an alternative; the existing OpenAI API functionality remains the default.
  • Always ensure chosen Azure model deployments are compatible with the expected functionalities (e.g., embedding dimensions).

A note on development: The Azure integration was initiated with AI assistance (Jules, Google AI) and then thoroughly reviewed, tested, and refined by me (@caiorg).

Feedback and review are welcome!

google-labs-jules bot and others added 4 commits June 17, 2025 15:56
This commit introduces support for using Azure AI Studio as an alternative to OpenAI for generating embeddings and chat completions.

Key changes:
- I modified `src/utils.py` to include logic for using Azure OpenAI services if Azure-specific environment variables are set. The system gracefully falls back to the standard OpenAI API if Azure credentials are not provided or if Azure calls fail.
- I added new environment variables:
    - `AZURE_OPENAI_API_KEY`
    - `AZURE_OPENAI_ENDPOINT`
    - `AZURE_OPENAI_CHAT_DEPLOYMENT`
    - `AZURE_OPENAI_EMBEDDING_DEPLOYMENT`
- I updated `README.md` to document the new Azure AI Studio integration, including setup instructions for the new environment variables.
- I added unit tests in `tests/test_utils.py` to cover the new Azure integration logic and fallback mechanisms. Note: I was unable to run the tests due to Python environment version limitations, but the tests are structured to validate the implementation.
This commit updates the documentation to reflect the correct product name "Azure AI Foundry".

- I replaced "Azure AI Studio" with "Azure AI Foundry" in README.md.
- No code comment changes were necessary in src/utils.py as they did not specifically reference "Azure AI Studio".
This commit refactors the Azure OpenAI integration in `src/utils.py` to more closely align with the official openai-python SDK's recommendations for Azure endpoints.

Key changes:
- Modified `src/utils.py`:
    - The `AzureOpenAI` client is now instantiated with `api_key`, `azure_endpoint`, and `api_version`.
    - Introduced a new environment variable `AZURE_OPENAI_API_VERSION` which is now required for Azure client initialization.
    - Ensured that `AZURE_OPENAI_CHAT_DEPLOYMENT` and `AZURE_OPENAI_EMBEDDING_DEPLOYMENT` are correctly used as the `model` parameter in API calls.
    - Maintained fallback logic to standard OpenAI if Azure configuration is incomplete or if Azure API calls fail.
- Updated `README.md`:
    - Documented the new `AZURE_OPENAI_API_VERSION` environment variable.
    - Clarified the role of deployment name variables.
    - Updated example configurations.
- Updated `tests/test_utils.py`:
    - Aligned tests with the new Azure client instantiation and the `AZURE_OPENAI_API_VERSION` environment variable.
    - Ensured test mocks and assertions reflect the refactored logic. (I'm currently unable to run the tests due to Python environment limitations).
@caiorg caiorg marked this pull request as draft June 19, 2025 21:02
@caiorg
Copy link
Author

caiorg commented Jun 19, 2025

Changed to draft due to some rebasing inconsistencies. Working on it.

Consolidates Azure OpenAI environment variables into the main
.env example and adds a dedicated notes section for clarity.
@caiorg caiorg force-pushed the azure-ai-studio-integration branch from a4ce113 to f0937bb Compare June 19, 2025 21:24
@caiorg
Copy link
Author

caiorg commented Jun 19, 2025

Reviewed and fixed!
Took some time to do some more testing to make sure everything works as expected.

@caiorg caiorg marked this pull request as ready for review June 19, 2025 23:26
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.

2 participants