Skip to content

Conversation

@guming3d
Copy link

This pull request improves Azure OpenAI example scripts by enhancing environment variable management, updating OpenAI client usage for Azure compatibility, and refactoring imports for clarity and maintainability. The most important changes are grouped below.

Environment Variable Handling:

  • Added dotenv support and automatic loading of environment variables from a .env file (with override) in both aoai_finetune.py and capital_agent.py to simplify local development and configuration. [1] [2]

OpenAI Client Usage and Azure Compatibility:

  • Updated the OpenAI client instantiation in capital_agent.py to use openai.AzureOpenAI, requiring explicit api_version and proper Azure endpoint configuration, improving compatibility with Azure OpenAI deployments.
  • In aoai_finetune.py, set up the OpenAI client with Azure-specific API key and endpoint before fine-tuning, ensuring correct authentication and endpoint usage.

Import and Logging Refactoring:

  • Refactored imports in train_capital_agent.py to use explicit module paths for TraceToMessages, Trainer, and logging configuration, improving code clarity and maintainability.
  • Replaced setup_logging() with configure_logger() in train_capital_agent.py for more explicit logging setup.

Internal Utility Import Update:

  • Changed the import of batch_iter_over_dataset in aoai_finetune.py to reference its new location in agentlightning.algorithm.apo.apo, reflecting internal codebase restructuring.

Copilot AI review requested due to automatic review settings December 23, 2025 08:24
Copy link
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

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

Pull request overview

This pull request enhances Azure OpenAI example scripts by improving configuration management through environment variable loading, updating client initialization for proper Azure compatibility, and refactoring imports for better code organization.

Key changes:

  • Added dotenv integration with .env file support for simplified local development configuration
  • Updated OpenAI client instantiation in capital_agent.py to use AzureOpenAI with required API version validation
  • Refactored imports to use explicit module paths and replaced deprecated setup_logging() with configure_logger()

Reviewed changes

Copilot reviewed 3 out of 3 changed files in this pull request and generated 5 comments.

File Description
examples/azure/train_capital_agent.py Refactored imports to explicit module paths and updated logging function from setup_logging() to configure_logger()
examples/azure/capital_agent.py Added dotenv support, updated OpenAI client to use AzureOpenAI with API version validation
examples/azure/aoai_finetune.py Added dotenv support, updated import path for batch_iter_over_dataset, and initialized OpenAI client within the training loop

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

Comment on lines +184 to +187
self.openai_client = OpenAI(
api_key=self.azure_openai_api_key,
base_url=self.azure_openai_endpoint + "/openai/v1/",
)
Copy link

Copilot AI Dec 23, 2025

Choose a reason for hiding this comment

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

The OpenAI client is being re-initialized inside the run loop, which overrides the client already created in __init__ at lines 134-137. This re-initialization changes the base_url by appending "/openai/v1/" to the endpoint. If this path modification is necessary for the fine-tuning API, it should be done in __init__ instead to avoid confusion and ensure consistency. If both URLs are needed for different purposes, consider using separate client instances with descriptive names.

Suggested change
self.openai_client = OpenAI(
api_key=self.azure_openai_api_key,
base_url=self.azure_openai_endpoint + "/openai/v1/",
)

Copilot uses AI. Check for mistakes.
openai_client = openai.OpenAI(base_url=llm.endpoint, api_key=os.getenv("AZURE_OPENAI_API_KEY", ""))
api_version = os.getenv("AZURE_OPENAI_API_VERSION", "")
if not api_version:
raise ValueError("Set AZURE_OPENAI_API_VERSION to match your Azure OpenAI resource.")
Copy link

Copilot AI Dec 23, 2025

Choose a reason for hiding this comment

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

The error message states "Set AZURE_OPENAI_API_VERSION to match your Azure OpenAI resource" but doesn't provide guidance on what a valid value looks like. Consider enhancing the error message to include an example of a valid API version format (e.g., "2024-08-01-preview") to help users configure this correctly.

Suggested change
raise ValueError("Set AZURE_OPENAI_API_VERSION to match your Azure OpenAI resource.")
raise ValueError(
"Set AZURE_OPENAI_API_VERSION (for example, '2024-08-01-preview') to match your Azure OpenAI resource's API version."
)

Copilot uses AI. Check for mistakes.
load_dotenv(override=True)



Copy link

Copilot AI Dec 23, 2025

Choose a reason for hiding this comment

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

There are extra blank lines after the dotenv loading code. According to PEP 8 style guidelines, there should only be two blank lines between top-level code blocks. Remove one of the blank lines to maintain consistency.

Suggested change

Copilot uses AI. Check for mistakes.

def main():
setup_logging()
configure_logger()
Copy link

Copilot AI Dec 23, 2025

Choose a reason for hiding this comment

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

The function configure_logger() is deprecated in favor of setup_logging(). According to the documentation in agentlightning/logging.py, this function emits a deprecation warning. Consider using setup_logging() instead, which is imported as from agentlightning import setup_logging (or use the setup function from agentlightning.logging).

Copilot uses AI. Check for mistakes.
from agentlightning.adapter.messages import OpenAIMessages, TraceToMessages
from agentlightning.algorithm import Algorithm
from agentlightning.algorithm.utils import batch_iter_over_dataset
from agentlightning.algorithm.apo.apo import batch_iter_over_dataset
Copy link

Copilot AI Dec 23, 2025

Choose a reason for hiding this comment

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

The import path change is incorrect. The function batch_iter_over_dataset is defined in agentlightning.algorithm.utils (not in agentlightning.algorithm.apo.apo). While apo.py imports it from utils, importing directly from apo.py creates an indirect dependency. Please import from the original location: from agentlightning.algorithm.utils import batch_iter_over_dataset.

Suggested change
from agentlightning.algorithm.apo.apo import batch_iter_over_dataset
from agentlightning.algorithm.utils import batch_iter_over_dataset

Copilot uses AI. Check for mistakes.
Copy link
Contributor

@ultmaster ultmaster left a comment

Choose a reason for hiding this comment

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

please see whether the copilot comments make sense.

training_data = await self.prepare_data_for_training(messages_group, reward_group, "train")
self._log_info(f"[Stage 4] Prepared {len(training_data)} training examples after filtering.")

self.openai_client = OpenAI(
Copy link
Contributor

Choose a reason for hiding this comment

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

Maybe we should encourage users to use the correct endpoint ending with /openai/v1. In other words, the documentation should be updated instead of the code.

from dotenv import load_dotenv

# Load environment variables from .env file (override existing)
load_dotenv(override=True)
Copy link
Contributor

Choose a reason for hiding this comment

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

dotenv run is a better idea than load_dotenv.

raise ValueError("Set AZURE_OPENAI_API_VERSION to match your Azure OpenAI resource.")

# Use AzureOpenAI client for Azure endpoints
openai_client = openai.AzureOpenAI(
Copy link
Contributor

Choose a reason for hiding this comment

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

Why AzureOpenAI is a must here?

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