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.

config_manager.py 2.2 kB

2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061
  1. import os
  2. from typing import Optional, List, Dict
  3. import autogen
  4. class ConfigManager:
  5. DEFAULT_LLM_MODEL = "gpt-4-turbo"
  6. DEFAULT_MLM_MODEL = "gpt-4-1106-vision-preview"
  7. DEFAULT_TIMEOUT = 300
  8. def __init__(self):
  9. self.llm_config = None
  10. self.client = None
  11. self.mlm_config = None
  12. self.mlm_client = None
  13. self.bing_api_key = None
  14. def _get_config_list(self, config_path_or_env: Optional[str] = None) -> List[Dict[str, str]]:
  15. config_list = None
  16. try:
  17. config_list = autogen.config_list_from_json(config_path_or_env)
  18. except Exception as e: # config list may not be available
  19. api_key = os.environ.get("OPENAI_API_KEY", None)
  20. if api_key is None:
  21. raise Exception("No config list or OPENAI_API_KEY found", e)
  22. config_list = [
  23. {"model": self.DEFAULT_LLM_MODEL, "api_key": api_key, "tags": ["llm"]},
  24. {"model": self.DEFAULT_MLM_MODEL, "api_key": api_key, "tags": ["mlm"]},
  25. ]
  26. return config_list
  27. def _get_bing_api_key(self) -> str:
  28. bing_api_key = os.environ.get("BING_API_KEY", None)
  29. if bing_api_key is None:
  30. raise Exception("Please set BING_API_KEY environment variable.")
  31. return bing_api_key
  32. def initialize(self, config_path_or_env: Optional[str] = "OAI_CONFIG_LIST") -> None:
  33. config_list = self._get_config_list(config_path_or_env)
  34. llm_config_list = autogen.filter_config(config_list, {"tags": ["llm"]})
  35. assert len(llm_config_list) > 0, "No API key with 'llm' tag found in config list."
  36. mlm_config_list = autogen.filter_config(config_list, {"tags": ["mlm"]})
  37. assert len(mlm_config_list) > 0, "No API key with 'mlm' tag found in config list."
  38. self.llm_config = {"config_list": llm_config_list, "timeout": self.DEFAULT_TIMEOUT, "temperature": 0.1}
  39. self.mlm_config = {"config_list": mlm_config_list, "timeout": self.DEFAULT_TIMEOUT, "temperature": 0.1}
  40. self.client = autogen.OpenAIWrapper(**self.llm_config)
  41. self.mlm_client = autogen.OpenAIWrapper(**self.mlm_config)
  42. self.bing_api_key = self._get_bing_api_key()