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
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109
  1. # api/initialization.py
  2. import os
  3. from pathlib import Path
  4. from typing import Dict
  5. from dotenv import load_dotenv
  6. from loguru import logger
  7. from pydantic import BaseModel
  8. from .config import Settings
  9. class _AppPaths(BaseModel):
  10. """Internal model representing all application paths"""
  11. app_root: Path
  12. static_root: Path
  13. user_files: Path
  14. ui_root: Path
  15. config_dir: Path
  16. database_uri: str
  17. class AppInitializer:
  18. """Handles application initialization including paths and environment setup"""
  19. def __init__(self, settings: Settings, app_path: str):
  20. """
  21. Initialize the application structure.
  22. Args:
  23. settings: Application settings
  24. app_path: Path to the application code directory
  25. """
  26. self.settings = settings
  27. self._app_path = Path(app_path)
  28. self._paths = self._init_paths()
  29. self._create_directories()
  30. self._load_environment()
  31. logger.info(f"Initializing application data folder: {self.app_root} ")
  32. def _get_app_root(self) -> Path:
  33. """Determine application root directory"""
  34. if app_dir := os.getenv("AUTOGENSTUDIO_APPDIR"):
  35. return Path(app_dir)
  36. return Path.home() / ".autogenstudio"
  37. def _get_database_uri(self, app_root: Path) -> str:
  38. """Generate database URI based on settings or environment"""
  39. if db_uri := os.getenv("AUTOGENSTUDIO_DATABASE_URI"):
  40. return db_uri
  41. return self.settings.DATABASE_URI.replace("./", str(app_root) + "/")
  42. def _init_paths(self) -> _AppPaths:
  43. """Initialize and return AppPaths instance"""
  44. app_root = self._get_app_root()
  45. return _AppPaths(
  46. app_root=app_root,
  47. static_root=app_root / "files",
  48. user_files=app_root / "files" / "user",
  49. ui_root=self._app_path / "ui",
  50. config_dir=app_root / self.settings.CONFIG_DIR,
  51. database_uri=self._get_database_uri(app_root),
  52. )
  53. def _create_directories(self) -> None:
  54. """Create all required directories"""
  55. self.app_root.mkdir(parents=True, exist_ok=True)
  56. dirs = [self.static_root, self.user_files, self.ui_root, self.config_dir]
  57. for path in dirs:
  58. path.mkdir(parents=True, exist_ok=True)
  59. def _load_environment(self) -> None:
  60. """Load environment variables from .env file if it exists"""
  61. env_file = self.app_root / ".env"
  62. if env_file.exists():
  63. # logger.info(f"Loading environment variables from {env_file}")
  64. load_dotenv(str(env_file))
  65. # Properties for accessing paths
  66. @property
  67. def app_root(self) -> Path:
  68. """Root directory for the application"""
  69. return self._paths.app_root
  70. @property
  71. def static_root(self) -> Path:
  72. """Directory for static files"""
  73. return self._paths.static_root
  74. @property
  75. def user_files(self) -> Path:
  76. """Directory for user files"""
  77. return self._paths.user_files
  78. @property
  79. def ui_root(self) -> Path:
  80. """Directory for UI files"""
  81. return self._paths.ui_root
  82. @property
  83. def config_dir(self) -> Path:
  84. """Directory for configuration files"""
  85. return self._paths.config_dir
  86. @property
  87. def database_uri(self) -> str:
  88. """Database connection URI"""
  89. return self._paths.database_uri