|
| 1 | +import argparse |
| 2 | +import logging |
| 3 | +import os |
| 4 | +from pathlib import Path |
| 5 | + |
| 6 | +from azure.identity import AzureDeveloperCliCredential |
| 7 | +from dotenv_azd import load_azd_env |
| 8 | +from evaltools.eval.evaluate import run_evaluate_from_config |
| 9 | +from rich.logging import RichHandler |
| 10 | + |
| 11 | +logger = logging.getLogger("ragapp") |
| 12 | + |
| 13 | + |
| 14 | +def get_openai_config(): |
| 15 | + azure_endpoint = f"https://{os.getenv('AZURE_OPENAI_SERVICE')}.openai.azure.com" |
| 16 | + azure_deployment = os.environ["AZURE_OPENAI_EVAL_DEPLOYMENT"] |
| 17 | + openai_config = {"azure_endpoint": azure_endpoint, "azure_deployment": azure_deployment} |
| 18 | + # azure-ai-evaluate will call DefaultAzureCredential behind the scenes, |
| 19 | + # so we must be logged in to Azure CLI with the correct tenant |
| 20 | + return openai_config |
| 21 | + |
| 22 | + |
| 23 | +def get_azure_credential(): |
| 24 | + AZURE_TENANT_ID = os.getenv("AZURE_TENANT_ID") |
| 25 | + if AZURE_TENANT_ID: |
| 26 | + logger.info("Setting up Azure credential using AzureDeveloperCliCredential with tenant_id %s", AZURE_TENANT_ID) |
| 27 | + azure_credential = AzureDeveloperCliCredential(tenant_id=AZURE_TENANT_ID, process_timeout=60) |
| 28 | + else: |
| 29 | + logger.info("Setting up Azure credential using AzureDeveloperCliCredential for home tenant") |
| 30 | + azure_credential = AzureDeveloperCliCredential(process_timeout=60) |
| 31 | + return azure_credential |
| 32 | + |
| 33 | + |
| 34 | +if __name__ == "__main__": |
| 35 | + logging.basicConfig( |
| 36 | + level=logging.INFO, format="%(message)s", datefmt="[%X]", handlers=[RichHandler(rich_tracebacks=True)] |
| 37 | + ) |
| 38 | + load_azd_env() |
| 39 | + |
| 40 | + parser = argparse.ArgumentParser(description="Run evaluation with OpenAI configuration.") |
| 41 | + parser.add_argument("--targeturl", type=str, help="Specify the target URL.") |
| 42 | + parser.add_argument("--resultsdir", type=Path, help="Specify the results directory.") |
| 43 | + parser.add_argument("--numquestions", type=int, help="Specify the number of questions.") |
| 44 | + |
| 45 | + args = parser.parse_args() |
| 46 | + |
| 47 | + openai_config = get_openai_config() |
| 48 | + |
| 49 | + run_evaluate_from_config( |
| 50 | + working_dir=Path(__file__).parent, |
| 51 | + config_path="evaluate_config.json", |
| 52 | + num_questions=args.numquestions, |
| 53 | + target_url=args.targeturl, |
| 54 | + results_dir=args.resultsdir, |
| 55 | + openai_config=openai_config, |
| 56 | + model=os.environ["AZURE_OPENAI_EVAL_MODEL"], |
| 57 | + azure_credential=get_azure_credential(), |
| 58 | + ) |
0 commit comments