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.

initialization.py 3.4 kB

Add Token Streaming in AGS , Support Env variables (#5659) <!-- Thank you for your contribution! Please review https://microsoft.github.io/autogen/docs/Contribute before opening a pull request. --> This PR has 3 main improvements. - Token streaming - Adds support for environment variables in the app settings - Updates AGS to persist Gallery entry in db. ## Adds Token Streaming in AGS. Agentchat now supports streaming of tokens via `ModelClientStreamingChunkEvent `. This PR is to track progress on supporting that in the AutoGen Studio UI. If `model_client_stream` is enabled in an assitant agent, then token will be streamed in UI. ```python streaming_assistant = AssistantAgent( name="assistant", model_client=model_client, system_message="You are a helpful assistant.", model_client_stream=True, # Enable streaming tokens. ) ``` https://github.com/user-attachments/assets/74d43d78-6359-40c3-a78e-c84dcb5e02a1 ## Env Variables Also adds support for env variables in AGS Settings You can set env variables that are loaded just before a team is run. Handy to set variable to be used by tools etc. <img width="1291" alt="image" src="https://github.com/user-attachments/assets/437b9d90-ccee-42f7-be5d-94ab191afd67" /> > Note: the set variables are available to the server process. <!-- Please add a reviewer to the assignee section when you create a PR. If you don't have the access to it, we will shortly find a reviewer and assign them to your PR. --> ## Why are these changes needed? <!-- Please give a short summary of the change and the problem this solves. --> ## Related issue number <!-- For example: "Closes #1234" --> Closes #5627 Closes #5662 Closes #5619 ## Checks - [ ] I've included any doc changes needed for <https://microsoft.github.io/autogen/>. See <https://github.com/microsoft/autogen/blob/main/CONTRIBUTING.md> to build and test documentation locally. - [ ] I've added tests (if relevant) corresponding to the changes introduced in this PR. - [ ] I've made sure all auto checks have passed.
1 year ago
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108
  1. # api/initialization.py
  2. import os
  3. from pathlib import Path
  4. from dotenv import load_dotenv
  5. from loguru import logger
  6. from pydantic import BaseModel
  7. from .config import Settings
  8. class _AppPaths(BaseModel):
  9. """Internal model representing all application paths"""
  10. app_root: Path
  11. static_root: Path
  12. user_files: Path
  13. ui_root: Path
  14. config_dir: Path
  15. database_uri: str
  16. class AppInitializer:
  17. """Handles application initialization including paths and environment setup"""
  18. def __init__(self, settings: Settings, app_path: str):
  19. """
  20. Initialize the application structure.
  21. Args:
  22. settings: Application settings
  23. app_path: Path to the application code directory
  24. """
  25. self.settings = settings
  26. self._app_path = Path(app_path)
  27. self._paths = self._init_paths()
  28. self._create_directories()
  29. self._load_environment()
  30. logger.info(f"Initializing application data folder: {self.app_root} ")
  31. def _get_app_root(self) -> Path:
  32. """Determine application root directory"""
  33. if app_dir := os.getenv("AUTOGENSTUDIO_APPDIR"):
  34. return Path(app_dir)
  35. return Path.home() / ".autogenstudio"
  36. def _get_database_uri(self, app_root: Path) -> str:
  37. """Generate database URI based on settings or environment"""
  38. if db_uri := os.getenv("AUTOGENSTUDIO_DATABASE_URI"):
  39. return db_uri
  40. return self.settings.DATABASE_URI.replace("./", str(app_root) + "/")
  41. def _init_paths(self) -> _AppPaths:
  42. """Initialize and return AppPaths instance"""
  43. app_root = self._get_app_root()
  44. return _AppPaths(
  45. app_root=app_root,
  46. static_root=app_root / "files",
  47. user_files=app_root / "files" / "user",
  48. ui_root=self._app_path / "ui",
  49. config_dir=app_root / self.settings.CONFIG_DIR,
  50. database_uri=self._get_database_uri(app_root),
  51. )
  52. def _create_directories(self) -> None:
  53. """Create all required directories"""
  54. self.app_root.mkdir(parents=True, exist_ok=True)
  55. dirs = [self.static_root, self.user_files, self.ui_root, self.config_dir]
  56. for path in dirs:
  57. path.mkdir(parents=True, exist_ok=True)
  58. def _load_environment(self) -> None:
  59. """Load environment variables from .env file if it exists"""
  60. env_file = self.app_root / ".env"
  61. if env_file.exists():
  62. # logger.info(f"Loading environment variables from {env_file}")
  63. load_dotenv(str(env_file))
  64. # Properties for accessing paths
  65. @property
  66. def app_root(self) -> Path:
  67. """Root directory for the application"""
  68. return self._paths.app_root
  69. @property
  70. def static_root(self) -> Path:
  71. """Directory for static files"""
  72. return self._paths.static_root
  73. @property
  74. def user_files(self) -> Path:
  75. """Directory for user files"""
  76. return self._paths.user_files
  77. @property
  78. def ui_root(self) -> Path:
  79. """Directory for UI files"""
  80. return self._paths.ui_root
  81. @property
  82. def config_dir(self) -> Path:
  83. """Directory for configuration files"""
  84. return self._paths.config_dir
  85. @property
  86. def database_uri(self) -> str:
  87. """Database connection URI"""
  88. return self._paths.database_uri