|
| 1 | +# Copyright (c) Microsoft Corporation. |
| 2 | +# Licensed under the MIT License. |
| 3 | +from autogen_ext.models import AzureOpenAIChatCompletionClient |
| 4 | +from environment import IdentityType, get_identity_type |
| 5 | + |
| 6 | +from azure.identity import DefaultAzureCredential, get_bearer_token_provider |
| 7 | +import os |
| 8 | +import dotenv |
| 9 | + |
| 10 | +dotenv.load_dotenv() |
| 11 | + |
| 12 | + |
| 13 | +class LLMModelCreator: |
| 14 | + @classmethod |
| 15 | + def get_model(cls, model_name: str) -> AzureOpenAIChatCompletionClient: |
| 16 | + """Retrieves the model based on the model name. |
| 17 | +
|
| 18 | + Args: |
| 19 | + ---- |
| 20 | + model_name (str): The name of the model to retrieve. |
| 21 | +
|
| 22 | + Returns: |
| 23 | + AzureOpenAIChatCompletionClient: The model client.""" |
| 24 | + if model_name == "4o-mini": |
| 25 | + return cls.gpt_4o_mini_model() |
| 26 | + elif model_name == "4o": |
| 27 | + return cls.gpt_4o_model() |
| 28 | + else: |
| 29 | + raise ValueError(f"Model {model_name} not found") |
| 30 | + |
| 31 | + @classmethod |
| 32 | + def get_authentication_properties(cls) -> dict: |
| 33 | + if get_identity_type() == IdentityType.SYSTEM_ASSIGNED: |
| 34 | + # Create the token provider |
| 35 | + api_key = None |
| 36 | + token_provider = get_bearer_token_provider( |
| 37 | + DefaultAzureCredential(), "https://cognitiveservices.azure.com/.default" |
| 38 | + ) |
| 39 | + elif get_identity_type() == IdentityType.USER_ASSIGNED: |
| 40 | + # Create the token provider |
| 41 | + api_key = None |
| 42 | + token_provider = get_bearer_token_provider( |
| 43 | + DefaultAzureCredential( |
| 44 | + managed_identity_client_id=os.environ["ClientId"] |
| 45 | + ), |
| 46 | + "https://cognitiveservices.azure.com/.default", |
| 47 | + ) |
| 48 | + else: |
| 49 | + token_provider = None |
| 50 | + api_key = os.environ["OpenAI__ApiKey"] |
| 51 | + |
| 52 | + return token_provider, api_key |
| 53 | + |
| 54 | + @classmethod |
| 55 | + def gpt_4o_mini_model(cls) -> AzureOpenAIChatCompletionClient: |
| 56 | + token_provider, api_key = cls.get_authentication_properties() |
| 57 | + return AzureOpenAIChatCompletionClient( |
| 58 | + azure_deployment=os.environ["OpenAI__MiniCompletionDeployment"], |
| 59 | + model=os.environ["OpenAI__MiniCompletionDeployment"], |
| 60 | + api_version="2024-08-01-preview", |
| 61 | + azure_endpoint=os.environ["OpenAI__Endpoint"], |
| 62 | + azure_ad_token_provider=token_provider, |
| 63 | + api_key=api_key, |
| 64 | + model_capabilities={ |
| 65 | + "vision": False, |
| 66 | + "function_calling": True, |
| 67 | + "json_output": True, |
| 68 | + }, |
| 69 | + ) |
| 70 | + |
| 71 | + @classmethod |
| 72 | + def gpt_4o_model(cls) -> AzureOpenAIChatCompletionClient: |
| 73 | + token_provider, api_key = cls.get_authentication_properties() |
| 74 | + return AzureOpenAIChatCompletionClient( |
| 75 | + azure_deployment=os.environ["OpenAI__CompletionDeployment"], |
| 76 | + model=os.environ["OpenAI__CompletionDeployment"], |
| 77 | + api_version="2024-08-01-preview", |
| 78 | + azure_endpoint=os.environ["OpenAI__Endpoint"], |
| 79 | + azure_ad_token_provider=token_provider, |
| 80 | + api_key=api_key, |
| 81 | + model_capabilities={ |
| 82 | + "vision": False, |
| 83 | + "function_calling": True, |
| 84 | + "json_output": True, |
| 85 | + }, |
| 86 | + ) |
0 commit comments