Skip to content

Commit

Permalink
feat(ui): support setting the number of threads and asynchronous calls
Browse files Browse the repository at this point in the history
  • Loading branch information
BroKun committed Sep 9, 2024
1 parent e6bd5ee commit 89cf11b
Show file tree
Hide file tree
Showing 26 changed files with 295 additions and 237 deletions.
2 changes: 1 addition & 1 deletion .vscode/cspell.json
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
// language - current active spelling language
"language": "en",
// words - list of words to be always considered correct
"words": ["ahooks", "difizen", "dumi", "dumirc", "magent"],
"words": ["ahooks", "aiofiles", "difizen", "dumi", "dumirc", "magent"],
// flagWords - list of words to be always considered incorrect
// This is useful for offensive words and common spelling errors.
// For example "hte" should be "the"
Expand Down
12 changes: 12 additions & 0 deletions .vscode/settings.json
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,18 @@
"**/.DS_Store": true,
"**/Thumbs.db": true
},

"search.exclude": {
"packages/**/dist": true,
"packages/**/es": true,
"packages/**/lib": true,
"web-packages/**/dist": true,
"web-packages/**/es": true,
"web-packages/**/lib": true,
"web-apps/**/dist": true,
"web-apps/**/es": true,
"web-apps/**/lib": true
},
"[python]": {
"editor.codeActionsOnSave": {
"source.organizeImports": "never",
Expand Down
1 change: 1 addition & 0 deletions packages/magent-ui/pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ dependencies = [
"sse-starlette>=2.1.2",
"jinja2>=3.1.4",
"python-multipart>=0.0.9",
"aiofiles>=24.1.0",
]
readme = "README.md"
requires-python = ">= 3.10"
Expand Down
11 changes: 7 additions & 4 deletions packages/magent-ui/src/magent_ui/app.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
from fastapi.templating import Jinja2Templates
from contextlib import asynccontextmanager
import webbrowser
from magent_ui.utils import AsyncTask
import uvicorn
import logging
from magent_ui.routers.main import api_router
Expand All @@ -13,7 +14,7 @@
from uvicorn.config import LOGGING_CONFIG

# Use uvicorn's default logging configuration
logging.config.dictConfig(LOGGING_CONFIG) # type: ignore
logging.config.dictConfig(LOGGING_CONFIG) # type: ignore

# Get the uvicorn logger
logger = logging.getLogger("uvicorn")
Expand All @@ -26,6 +27,7 @@

templates = Jinja2Templates(directory=templates_dir)


def launch(**kwargs):
logger.info("Current log level is")
project_root_path = get_project_root_path()
Expand All @@ -35,7 +37,8 @@ def launch(**kwargs):
if app_config.log_level is not None:
logger.setLevel(app_config.log_level)


if app_config.thread_worker is not None:
AsyncTask.set_max_worker(app_config.thread_worker)

@asynccontextmanager
async def lifespan(app: FastAPI):
Expand All @@ -54,7 +57,7 @@ async def lifespan(app: FastAPI):
# static
if os.path.exists(static_dir):
app.mount(app_config.full_static_path, StaticFiles(directory=static_dir,
html=True), name="static")
html=True), name="static")
else:
logger.info('Can not find static directory. ', static_dir)

Expand All @@ -65,7 +68,7 @@ async def lifespan(app: FastAPI):
os.makedirs(app_config.resource_dir_path)

app.mount(app_config.full_resource_path, StaticFiles(directory=app_config.resource_dir_path,
html=True))
html=True))

# auto redirect to app url
@app.get(app_config.root_path, response_class=HTMLResponse)
Expand Down
109 changes: 62 additions & 47 deletions packages/magent-ui/src/magent_ui/config.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
logger = logging.getLogger("uvicorn")

default_config = {
'thread_worker': 20,
'host': '0.0.0.0',
'port': 8888,
'base_url': None,
Expand All @@ -27,6 +28,8 @@ def to_uvicorn_config(config: dict):
# root path has already taken effect
del uvicorn_config['root_path']

if uvicorn_config.get('thread_worker', None) is not None:
del uvicorn_config['thread_worker']

return uvicorn_config

Expand Down Expand Up @@ -116,54 +119,66 @@ def load_config(config=default_config, project_root_path=None):
resource_path = 'resources'
app_path = 'app'


config_count = 0


class AppConfig():
config: dict
project_root_path:Path
resource_dir_path: Path

port: int
root_path: str
base_url: str
open_browser: bool
log_level: str

full_api_path: str
full_static_path: str
full_resource_path: str

api_url: str
static_url: str
resource_url: str
app_url: str

def load_config(self, project_root_path:Path, **kwargs):
config = load_config(kwargs, project_root_path)
self.project_root_path = project_root_path
self.resource_dir_path = project_root_path / 'app' / 'resources'
self.config = config
self.port = config.get('port', 8888)
base_root_path = '/'
root_path = config.get('root_path', base_root_path)
self.root_path = root_path
self.base_url = config.get('base_url', None)
self.open_browser = config.get('open_browser', True)
self.log_level = config.get('log_level', None)
if self.base_url is None:
self.base_url = root_path

if not root_path.startswith('/'):
logger.info('[magent] root_path should start with "/" ', root_path)
root_path = f'/{root_path}'
config['root_path'] = root_path

self.full_api_path = os.path.join(root_path, api_path)
self.full_static_path = os.path.join(root_path, static_path)
self.full_resource_path = os.path.join(root_path, resource_path)

self.api_url = os.path.join(self.base_url, api_path)
self.static_url = os.path.join(self.base_url, static_path)
self.resource_url = os.path.join(self.base_url, resource_path)
self.app_url = os.path.join(self.base_url, app_path)
config: dict
project_root_path: Path
resource_dir_path: Path

port: int
root_path: str
base_url: str
open_browser: bool
log_level: str

full_api_path: str
full_static_path: str
full_resource_path: str

api_url: str
static_url: str
resource_url: str
app_url: str

thread_worker: int

def __init__(self):
global config_count
config_count += 1
print('config:', config_count)

def load_config(self, project_root_path: Path, **kwargs):
config = load_config(kwargs, project_root_path)
self.project_root_path = project_root_path
self.resource_dir_path = project_root_path / 'app' / 'resources'
self.config = config
self.port = config.get('port', 8888)
base_root_path = '/'
root_path = config.get('root_path', base_root_path)
self.root_path = root_path
self.base_url = config.get('base_url', None)
self.open_browser = config.get('open_browser', True)
self.log_level = config.get('log_level', None)
self.thread_worker = config.get('thread_worker', None)
if self.base_url is None:
self.base_url = root_path

if not root_path.startswith('/'):
logger.info('[magent] root_path should start with "/" ', root_path)
root_path = f'/{root_path}'
config['root_path'] = root_path

self.full_api_path = os.path.join(root_path, api_path)
self.full_static_path = os.path.join(root_path, static_path)
self.full_resource_path = os.path.join(root_path, resource_path)

self.api_url = os.path.join(self.base_url, api_path)
self.static_url = os.path.join(self.base_url, static_path)
self.resource_url = os.path.join(self.base_url, resource_path)
self.app_url = os.path.join(self.base_url, app_path)


app_config = AppConfig()
20 changes: 0 additions & 20 deletions packages/magent-ui/src/magent_ui/models/agent.py

This file was deleted.

9 changes: 0 additions & 9 deletions packages/magent-ui/src/magent_ui/models/knowledge.py

This file was deleted.

10 changes: 0 additions & 10 deletions packages/magent-ui/src/magent_ui/models/llm.py

This file was deleted.

11 changes: 0 additions & 11 deletions packages/magent-ui/src/magent_ui/models/message.py

This file was deleted.

8 changes: 0 additions & 8 deletions packages/magent-ui/src/magent_ui/models/planner.py

This file was deleted.

9 changes: 0 additions & 9 deletions packages/magent-ui/src/magent_ui/models/prompt.py

This file was deleted.

19 changes: 0 additions & 19 deletions packages/magent-ui/src/magent_ui/models/session.py

This file was deleted.

18 changes: 0 additions & 18 deletions packages/magent-ui/src/magent_ui/models/tool.py

This file was deleted.

Loading

0 comments on commit 89cf11b

Please sign in to comment.