| 40 | |
| 41 | |
| 42 | def get_openai_config() -> dict: |
| 43 | openai_config: dict[str, Any] |
| 44 | if os.environ.get("OPENAI_CHAT_HOST") == "azure": |
| 45 | azure_endpoint = os.environ["AZURE_OPENAI_ENDPOINT"] |
| 46 | azure_deployment = os.environ["AZURE_OPENAI_EVAL_DEPLOYMENT"] |
| 47 | if os.environ.get("AZURE_OPENAI_KEY"): |
| 48 | logger.info("Using Azure OpenAI Service with API Key from AZURE_OPENAI_KEY") |
| 49 | openai_config = { |
| 50 | "azure_endpoint": azure_endpoint, |
| 51 | "azure_deployment": azure_deployment, |
| 52 | "api_key": os.environ["AZURE_OPENAI_KEY"], |
| 53 | } |
| 54 | else: |
| 55 | if tenant_id := os.getenv("AZURE_TENANT_ID"): |
| 56 | logger.info("Authenticating to Azure using Azure Developer CLI Credential for tenant %s", tenant_id) |
| 57 | azure_credential = azure.identity.AzureDeveloperCliCredential(tenant_id=tenant_id, process_timeout=60) |
| 58 | else: |
| 59 | logger.info("Authenticating to Azure using Azure Developer CLI Credential") |
| 60 | azure_credential = azure.identity.AzureDeveloperCliCredential(process_timeout=60) |
| 61 | openai_config = { |
| 62 | "azure_endpoint": azure_endpoint, |
| 63 | "azure_deployment": azure_deployment, |
| 64 | "credential": azure_credential, |
| 65 | } |
| 66 | # azure-ai-evaluate will call DefaultAzureCredential behind the scenes, |
| 67 | # so we must be logged in to Azure CLI with the correct tenant |
| 68 | openai_config["model"] = os.environ["AZURE_OPENAI_EVAL_MODEL"] |
| 69 | elif os.environ.get("OPENAI_CHAT_HOST") == "ollama": |
| 70 | raise NotImplementedError("Ollama is not supported. Switch to Azure or OpenAI.com") |
| 71 | else: |
| 72 | logger.info("Using OpenAI Service with API Key from OPENAICOM_KEY") |
| 73 | openai_config = {"api_key": os.environ["OPENAICOM_KEY"], "model": "gpt-4"} |
| 74 | return openai_config |
| 75 | |
| 76 | |
| 77 | if __name__ == "__main__": |