You can not select more than 25 topics Topics must start with a chinese character,a letter or number, can include dashes ('-') and can be up to 35 characters long.

_utils.py 1.9 kB

12345678910111213141516171819202122232425262728293031323334353637383940414243
  1. import logging
  2. import os
  3. from typing import Any, Iterable, Type
  4. import yaml
  5. from _types import AppConfig
  6. from autogen_core import MessageSerializer, try_get_known_serializers_for_type
  7. from autogen_ext.models.openai.config import AzureOpenAIClientConfiguration
  8. from azure.identity import DefaultAzureCredential, get_bearer_token_provider
  9. def load_config(file_path: str = os.path.join(os.path.dirname(__file__), "config.yaml")) -> AppConfig:
  10. model_client = {}
  11. with open(file_path, "r") as file:
  12. config_data = yaml.safe_load(file)
  13. model_client = config_data["client_config"]
  14. del config_data["client_config"]
  15. app_config = AppConfig(**config_data)
  16. # This was required as it couldn't automatically instantiate AzureOpenAIClientConfiguration
  17. aad_params = {}
  18. if len(model_client.get("api_key", "")) == 0:
  19. aad_params["azure_ad_token_provider"] = get_bearer_token_provider(
  20. DefaultAzureCredential(), "https://cognitiveservices.azure.com/.default"
  21. )
  22. app_config.client_config = AzureOpenAIClientConfiguration(**model_client, **aad_params) # type: ignore[typeddict-item]
  23. return app_config
  24. def get_serializers(types: Iterable[Type[Any]]) -> list[MessageSerializer[Any]]:
  25. serializers = []
  26. for type in types:
  27. serializers.extend(try_get_known_serializers_for_type(type)) # type: ignore
  28. return serializers # type: ignore [reportUnknownVariableType]
  29. # TODO: This is a helper function to get rid of a lot of logs until we find exact loggers to properly set log levels ...
  30. def set_all_log_levels(log_leve: int):
  31. # Iterate through all existing loggers and set their levels
  32. for _, logger in logging.root.manager.loggerDict.items():
  33. if isinstance(logger, logging.Logger): # Ensure it's actually a Logger object
  34. logger.setLevel(log_leve) # Adjust to DEBUG or another level as needed