|
2 | 2 | import os
|
3 | 3 | from pathlib import Path
|
4 | 4 |
|
| 5 | +import azure.identity |
| 6 | +from azure.ai.evaluation import AzureOpenAIModelConfiguration, OpenAIModelConfiguration |
5 | 7 | from dotenv import load_dotenv
|
6 | 8 | from evaltools.eval.evaluate import run_evaluate_from_config
|
7 |
| -from promptflow.core import AzureOpenAIModelConfiguration, ModelConfiguration, OpenAIModelConfiguration |
| 9 | +from rich.logging import RichHandler |
8 | 10 |
|
9 | 11 | logger = logging.getLogger("ragapp")
|
10 | 12 |
|
11 | 13 |
|
12 |
| -def get_openai_config() -> ModelConfiguration: |
| 14 | +def get_openai_config() -> dict: |
13 | 15 | if os.environ.get("OPENAI_CHAT_HOST") == "azure":
|
14 | 16 | azure_endpoint = os.environ["AZURE_OPENAI_ENDPOINT"]
|
15 |
| - azure_deployment = os.environ.get("AZURE_OPENAI_CHAT_DEPLOYMENT") |
16 |
| - api_version = "2023-07-01-preview" |
| 17 | + azure_deployment = os.environ.get("AZURE_OPENAI_EVAL_DEPLOYMENT") |
17 | 18 | if os.environ.get("AZURE_OPENAI_KEY"):
|
18 | 19 | logger.info("Using Azure OpenAI Service with API Key from AZURE_OPENAI_KEY")
|
19 |
| - openai_config = AzureOpenAIModelConfiguration( |
20 |
| - azure_endpoint=azure_endpoint, |
21 |
| - azure_deployment=azure_deployment, |
22 |
| - api_version=api_version, |
23 |
| - api_key=os.environ["AZURE_OPENAI_KEY"], |
24 |
| - ) |
| 20 | + openai_config: AzureOpenAIModelConfiguration = { |
| 21 | + "azure_endpoint": azure_endpoint, |
| 22 | + "azure_deployment": azure_deployment, |
| 23 | + "api_key": os.environ["AZURE_OPENAI_KEY"], |
| 24 | + } |
25 | 25 | else:
|
26 |
| - logger.info("Using Azure OpenAI Service with Azure Developer CLI Credential") |
27 |
| - openai_config = AzureOpenAIModelConfiguration( |
28 |
| - azure_endpoint=azure_endpoint, azure_deployment=azure_deployment, api_version=api_version |
29 |
| - ) |
30 |
| - # PromptFlow will call DefaultAzureCredential behind the scenes |
31 |
| - openai_config.model = os.environ["AZURE_OPENAI_CHAT_DEPLOYMENT"] |
| 26 | + if tenant_id := os.getenv("AZURE_TENANT_ID"): |
| 27 | + logger.info("Authenticating to Azure using Azure Developer CLI Credential for tenant %s", tenant_id) |
| 28 | + azure_credential = azure.identity.AzureDeveloperCliCredential(tenant_id=tenant_id, process_timeout=60) |
| 29 | + else: |
| 30 | + logger.info("Authenticating to Azure using Azure Developer CLI Credential") |
| 31 | + azure_credential = azure.identity.AzureDeveloperCliCredential(process_timeout=60) |
| 32 | + openai_config: AzureOpenAIModelConfiguration = { |
| 33 | + "azure_endpoint": azure_endpoint, |
| 34 | + "azure_deployment": azure_deployment, |
| 35 | + "credential": azure_credential, |
| 36 | + } |
| 37 | + # azure-ai-evaluate will call DefaultAzureCredential behind the scenes, |
| 38 | + # so we must be logged in to Azure CLI with the correct tenant |
| 39 | + openai_config["model"] = os.environ["AZURE_OPENAI_EVAL_MODEL"] |
32 | 40 | else:
|
33 | 41 | logger.info("Using OpenAI Service with API Key from OPENAICOM_KEY")
|
34 |
| - openai_config = OpenAIModelConfiguration( |
35 |
| - model=os.environ["OPENAICOM_CHAT_MODEL"], api_key=os.environ.get("OPENAICOM_KEY") |
36 |
| - ) |
| 42 | + openai_config: OpenAIModelConfiguration = {"api_key": os.environ["OPENAICOM_KEY"], "model": "gpt-4"} |
37 | 43 | return openai_config
|
38 | 44 |
|
39 | 45 |
|
40 | 46 | if __name__ == "__main__":
|
41 |
| - logging.basicConfig(level=logging.WARNING) |
42 |
| - logger.setLevel(logging.INFO) |
| 47 | + logging.basicConfig( |
| 48 | + level=logging.INFO, format="%(message)s", datefmt="[%X]", handlers=[RichHandler(rich_tracebacks=True)] |
| 49 | + ) |
43 | 50 | load_dotenv(".env", override=True)
|
44 | 51 |
|
45 | 52 | openai_config = get_openai_config()
|
46 | 53 | run_evaluate_from_config(
|
47 |
| - working_dir=Path(__file__).parent, config_path="eval_config.json", openai_config=openai_config, num_questions=20 |
| 54 | + working_dir=Path(__file__).parent, config_path="eval_config.json", openai_config=openai_config, num_questions=2 |
48 | 55 | )
|
0 commit comments