Skip to content

Commit

Permalink
feat(core): APP use new SDK component (eosphoros-ai#1050)
Browse files Browse the repository at this point in the history
  • Loading branch information
fangyinc authored and Hopshine committed Sep 10, 2024
1 parent 3804d8e commit 6b76154
Show file tree
Hide file tree
Showing 242 changed files with 2,700 additions and 2,095 deletions.
22 changes: 21 additions & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -38,21 +38,41 @@ fmt: setup ## Format Python code
# https://github.com/PyCQA/isort
# $(VENV_BIN)/isort .
$(VENV_BIN)/isort dbgpt/agent/
$(VENV_BIN)/isort dbgpt/app/
$(VENV_BIN)/isort dbgpt/cli/
$(VENV_BIN)/isort dbgpt/configs/
$(VENV_BIN)/isort dbgpt/core/
$(VENV_BIN)/isort dbgpt/datasource/
$(VENV_BIN)/isort dbgpt/model/
# TODO: $(VENV_BIN)/isort dbgpt/serve
$(VENV_BIN)/isort dbgpt/serve/core/
$(VENV_BIN)/isort dbgpt/serve/agent/
$(VENV_BIN)/isort dbgpt/serve/conversation/
$(VENV_BIN)/isort dbgpt/serve/utils/_template_files
$(VENV_BIN)/isort dbgpt/storage/
$(VENV_BIN)/isort dbgpt/train/
$(VENV_BIN)/isort dbgpt/util/
$(VENV_BIN)/isort dbgpt/vis/
$(VENV_BIN)/isort dbgpt/__init__.py
$(VENV_BIN)/isort dbgpt/component.py
$(VENV_BIN)/isort --extend-skip="examples/notebook" examples
# https://github.com/psf/black
$(VENV_BIN)/black --extend-exclude="examples/notebook" .
# TODO: Use blackdoc to format Python doctests.
# https://blackdoc.readthedocs.io/en/latest/
# $(VENV_BIN)/blackdoc .
$(VENV_BIN)/blackdoc dbgpt/core/
$(VENV_BIN)/blackdoc dbgpt/agent/
$(VENV_BIN)/blackdoc dbgpt/app/
$(VENV_BIN)/blackdoc dbgpt/cli/
$(VENV_BIN)/blackdoc dbgpt/configs/
$(VENV_BIN)/blackdoc dbgpt/core/
$(VENV_BIN)/blackdoc dbgpt/datasource/
$(VENV_BIN)/blackdoc dbgpt/model/
$(VENV_BIN)/blackdoc dbgpt/serve/
# TODO: $(VENV_BIN)/blackdoc dbgpt/storage/
$(VENV_BIN)/blackdoc dbgpt/train/
$(VENV_BIN)/blackdoc dbgpt/util/
$(VENV_BIN)/blackdoc dbgpt/vis/
$(VENV_BIN)/blackdoc examples
# TODO: Type checking of Python code.
# https://github.com/python/mypy
Expand Down
3 changes: 1 addition & 2 deletions dbgpt/__init__.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
from dbgpt.component import SystemApp, BaseComponent

from dbgpt.component import BaseComponent, SystemApp

__ALL__ = ["SystemApp", "BaseComponent"]

Expand Down
19 changes: 10 additions & 9 deletions dbgpt/app/_cli.py
Original file line number Diff line number Diff line change
@@ -1,11 +1,13 @@
import functools
import os
from typing import Optional

import click
import os
import functools

from dbgpt.app.base import WebServerParameters
from dbgpt.configs.model_config import LOGDIR
from dbgpt.util.parameter_utils import EnvArgumentParser
from dbgpt.util.command_utils import _run_current_with_daemon, _stop_service
from dbgpt.util.parameter_utils import EnvArgumentParser


@click.command(name="webserver")
Expand Down Expand Up @@ -117,8 +119,8 @@ def migrate(alembic_ini_path: str, script_location: str, message: str):
def upgrade(alembic_ini_path: str, script_location: str, sql_output: str):
"""Upgrade database to target version"""
from dbgpt.util._db_migration_utils import (
upgrade_database,
generate_sql_for_upgrade,
upgrade_database,
)

alembic_cfg, db_manager = _get_migration_config(alembic_ini_path, script_location)
Expand Down Expand Up @@ -210,9 +212,8 @@ def clean(
@add_migration_options
def list(alembic_ini_path: str, script_location: str):
"""List all versions in the migration history, marking the current one"""
from alembic.script import ScriptDirectory

from alembic.runtime.migration import MigrationContext
from alembic.script import ScriptDirectory

alembic_cfg, db_manager = _get_migration_config(alembic_ini_path, script_location)

Expand Down Expand Up @@ -269,12 +270,12 @@ def show(alembic_ini_path: str, script_location: str, revision: str):
def _get_migration_config(
alembic_ini_path: Optional[str] = None, script_location: Optional[str] = None
):
from dbgpt.storage.metadata.db_manager import db as db_manager
from dbgpt.util._db_migration_utils import create_alembic_config
from dbgpt.app.base import _initialize_db

# Import all models to make sure they are registered with SQLAlchemy.
from dbgpt.app.initialization.db_model_initialization import _MODELS
from dbgpt.app.base import _initialize_db
from dbgpt.storage.metadata.db_manager import db as db_manager
from dbgpt.util._db_migration_utils import create_alembic_config

# initialize db
default_meta_data_path = _initialize_db()
Expand Down
21 changes: 10 additions & 11 deletions dbgpt/app/base.py
Original file line number Diff line number Diff line change
@@ -1,16 +1,15 @@
import signal
import logging
import os
import threading
import signal
import sys
import logging
from typing import Optional
import threading
from dataclasses import dataclass, field
from typing import Optional

from dbgpt._private.config import Config
from dbgpt.component import SystemApp
from dbgpt.util.parameter_utils import BaseParameters


ROOT_PATH = os.path.dirname(os.path.dirname(os.path.dirname(os.path.abspath(__file__))))
sys.path.append(ROOT_PATH)

Expand All @@ -35,7 +34,6 @@ def server_init(param: "WebServerParameters", system_app: SystemApp):
from dbgpt.agent.plugin.commands.command_mange import CommandRegistry

# logger.info(f"args: {args}")

# init config
cfg = Config()
cfg.SYSTEM_APP = system_app
Expand Down Expand Up @@ -100,13 +98,12 @@ def _migration_db_storage(param: "WebServerParameters"):
"""Migration the db storage."""
# Import all models to make sure they are registered with SQLAlchemy.
from dbgpt.app.initialization.db_model_initialization import _MODELS

from dbgpt.configs.model_config import PILOT_PATH

default_meta_data_path = os.path.join(PILOT_PATH, "meta_data")
if not param.disable_alembic_upgrade:
from dbgpt.util._db_migration_utils import _ddl_init_and_upgrade
from dbgpt.storage.metadata.db_manager import db
from dbgpt.util._db_migration_utils import _ddl_init_and_upgrade

# try to create all tables
try:
Expand All @@ -123,9 +120,11 @@ def _initialize_db(
Now just support sqlite and mysql. If db type is sqlite, the db path is `pilot/meta_data/{db_name}.db`.
"""
from urllib.parse import quote
from urllib.parse import quote_plus as urlquote

from dbgpt.configs.model_config import PILOT_PATH
from dbgpt.storage.metadata.db_manager import initialize_db
from urllib.parse import quote_plus as urlquote, quote

CFG = Config()
db_name = CFG.LOCAL_DB_NAME
Expand Down Expand Up @@ -170,8 +169,8 @@ def _create_mysql_database(db_name: str, db_url: str, try_to_create_db: bool = F
Raises:
Exception: Raise exception if database operation failed
"""
from sqlalchemy import create_engine, DDL
from sqlalchemy.exc import SQLAlchemyError, OperationalError
from sqlalchemy import DDL, create_engine
from sqlalchemy.exc import OperationalError, SQLAlchemyError

if not try_to_create_db:
logger.info(f"Skipping creation of database {db_name}")
Expand Down
5 changes: 3 additions & 2 deletions dbgpt/app/chat_adapter.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,10 @@
# -*- coding: utf-8 -*-

from functools import cache
from typing import List, Dict, Tuple
from dbgpt.model.conversation import Conversation, get_conv_template
from typing import Dict, List, Tuple

from dbgpt.core.interface.message import ModelMessage, ModelMessageRoleType
from dbgpt.model.conversation import Conversation, get_conv_template


class BaseChatAdpter:
Expand Down
16 changes: 11 additions & 5 deletions dbgpt/app/component_configs.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,11 @@

import logging

from dbgpt.component import SystemApp
from dbgpt._private.config import Config
from dbgpt.app.base import WebServerParameters
from dbgpt.component import SystemApp
from dbgpt.configs.model_config import MODEL_DISK_CACHE_DIR
from dbgpt.util.executor_utils import DefaultExecutorFactory
from dbgpt.app.base import WebServerParameters


logger = logging.getLogger(__name__)

Expand All @@ -21,9 +20,9 @@ def initialize_components(
embedding_model_path: str,
):
# Lazy import to avoid high time cost
from dbgpt.model.cluster.controller.controller import controller
from dbgpt.app.initialization.embedding_component import _initialize_embedding_model
from dbgpt.app.initialization.serve_initialization import register_serve_apps
from dbgpt.model.cluster.controller.controller import controller

# Register global default executor factory first
system_app.register(DefaultExecutorFactory)
Expand All @@ -47,6 +46,7 @@ def initialize_components(
)
_initialize_model_cache(system_app)
_initialize_awel(system_app, param)
_initialize_openapi(system_app)
# Register serve apps
register_serve_apps(system_app, CFG)

Expand All @@ -65,8 +65,8 @@ def _initialize_model_cache(system_app: SystemApp):


def _initialize_awel(system_app: SystemApp, param: WebServerParameters):
from dbgpt.core.awel import initialize_awel
from dbgpt.configs.model_config import _DAG_DEFINITION_DIR
from dbgpt.core.awel import initialize_awel

# Add default dag definition dir
dag_dirs = [_DAG_DEFINITION_DIR]
Expand All @@ -75,3 +75,9 @@ def _initialize_awel(system_app: SystemApp, param: WebServerParameters):
dag_dirs = [x.strip() for x in dag_dirs]

initialize_awel(system_app, dag_dirs)


def _initialize_openapi(system_app: SystemApp):
from dbgpt.app.openapi.api_v1.editor.service import EditorService

system_app.register(EditorService)
51 changes: 23 additions & 28 deletions dbgpt/app/dbgpt_server.py
Original file line number Diff line number Diff line change
@@ -1,46 +1,45 @@
import os
import argparse
import os
import sys
from typing import List

ROOT_PATH = os.path.dirname(os.path.dirname(os.path.dirname(os.path.abspath(__file__))))
sys.path.append(ROOT_PATH)
from dbgpt.configs.model_config import (
LLM_MODEL_CONFIG,
EMBEDDING_MODEL_CONFIG,
LOGDIR,
ROOT_PATH,
)
from dbgpt._private.config import Config
from dbgpt.component import SystemApp
from fastapi import FastAPI
from fastapi.exceptions import RequestValidationError
from fastapi.middleware.cors import CORSMiddleware
from fastapi.openapi.docs import get_swagger_ui_html

# fastapi import time cost about 0.05s
from fastapi.staticfiles import StaticFiles

from dbgpt._private.config import Config
from dbgpt.app.base import (
server_init,
_migration_db_storage,
WebServerParameters,
_create_model_start_listener,
_migration_db_storage,
server_init,
)

# initialize_components import time cost about 0.1s
from dbgpt.app.component_configs import initialize_components

# fastapi import time cost about 0.05s
from fastapi.staticfiles import StaticFiles
from fastapi import FastAPI
from fastapi.exceptions import RequestValidationError
from fastapi.middleware.cors import CORSMiddleware
from fastapi.openapi.docs import get_swagger_ui_html

from dbgpt.app.openapi.base import validation_exception_handler
from dbgpt.component import SystemApp
from dbgpt.configs.model_config import (
EMBEDDING_MODEL_CONFIG,
LLM_MODEL_CONFIG,
LOGDIR,
ROOT_PATH,
)
from dbgpt.util.parameter_utils import _get_dict_from_obj
from dbgpt.util.system_utils import get_system_info
from dbgpt.util.tracer import SpanType, SpanTypeRunName, initialize_tracer, root_tracer
from dbgpt.util.utils import (
setup_logging,
_get_logging_level,
logging_str_to_uvicorn_level,
setup_http_service_logging,
setup_logging,
)
from dbgpt.util.tracer import root_tracer, initialize_tracer, SpanType, SpanTypeRunName
from dbgpt.util.parameter_utils import _get_dict_from_obj
from dbgpt.util.system_utils import get_system_info

static_file_path = os.path.join(ROOT_PATH, "dbgpt", "app/static")

Expand Down Expand Up @@ -89,9 +88,7 @@ async def custom_swagger_ui_html():
def mount_routers(app: FastAPI):
"""Lazy import to avoid high time cost"""
from dbgpt.app.knowledge.api import router as knowledge_router

from dbgpt.app.llm_manage.api import router as llm_manage_api

from dbgpt.app.openapi.api_v1.api_v1 import router as api_v1
from dbgpt.app.openapi.api_v1.editor.api_editor_v1 import (
router as api_editor_route_v1,
Expand All @@ -107,9 +104,7 @@ def mount_routers(app: FastAPI):


def mount_static_files(app: FastAPI):
from dbgpt.agent.plugin.commands.built_in.disply_type import (
static_message_img_path,
)
from dbgpt.agent.plugin.commands.built_in.disply_type import static_message_img_path

os.makedirs(static_message_img_path, exist_ok=True)
app.mount(
Expand Down
7 changes: 3 additions & 4 deletions dbgpt/app/initialization/db_model_initialization.py
Original file line number Diff line number Diff line change
@@ -1,14 +1,13 @@
"""Import all models to make sure they are registered with SQLAlchemy.
"""
from dbgpt.serve.agent.db.my_plugin_db import MyPluginEntity
from dbgpt.serve.agent.db.plugin_hub_db import PluginHubEntity
from dbgpt.app.knowledge.chunk_db import DocumentChunkEntity
from dbgpt.app.knowledge.document_db import KnowledgeDocumentEntity
from dbgpt.app.knowledge.space_db import KnowledgeSpaceEntity
from dbgpt.app.openapi.api_v1.feedback.feed_back_db import ChatFeedBackEntity

from dbgpt.serve.prompt.models.models import ServeEntity as PromptManageEntity
from dbgpt.datasource.manages.connect_config_db import ConnectConfigEntity
from dbgpt.serve.agent.db.my_plugin_db import MyPluginEntity
from dbgpt.serve.agent.db.plugin_hub_db import PluginHubEntity
from dbgpt.serve.prompt.models.models import ServeEntity as PromptManageEntity
from dbgpt.storage.chat_history.chat_history_db import (
ChatHistoryEntity,
ChatHistoryMessageEntity,
Expand Down
4 changes: 3 additions & 1 deletion dbgpt/app/initialization/embedding_component.py
Original file line number Diff line number Diff line change
@@ -1,12 +1,14 @@
from __future__ import annotations

import logging
from typing import Any, Type, TYPE_CHECKING
from typing import TYPE_CHECKING, Any, Type

from dbgpt.component import ComponentType, SystemApp
from dbgpt.rag.embedding.embedding_factory import EmbeddingFactory

if TYPE_CHECKING:
from langchain.embeddings.base import Embeddings

from dbgpt.app.base import WebServerParameters

logger = logging.getLogger(__name__)
Expand Down
13 changes: 10 additions & 3 deletions dbgpt/app/initialization/serve_initialization.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
from dbgpt.component import SystemApp
from dbgpt._private.config import Config
from dbgpt.component import SystemApp


def register_serve_apps(system_app: SystemApp, cfg: Config):
Expand All @@ -8,9 +8,9 @@ def register_serve_apps(system_app: SystemApp, cfg: Config):

# ################################ Prompt Serve Register Begin ######################################
from dbgpt.serve.prompt.serve import (
Serve as PromptServe,
SERVE_CONFIG_KEY_PREFIX as PROMPT_SERVE_CONFIG_KEY_PREFIX,
)
from dbgpt.serve.prompt.serve import Serve as PromptServe

# Replace old prompt serve
# Set config
Expand All @@ -21,8 +21,15 @@ def register_serve_apps(system_app: SystemApp, cfg: Config):
# ################################ Prompt Serve Register End ########################################

# ################################ Conversation Serve Register Begin ######################################
from dbgpt.serve.conversation.serve import (
SERVE_CONFIG_KEY_PREFIX as CONVERSATION_SERVE_CONFIG_KEY_PREFIX,
)
from dbgpt.serve.conversation.serve import Serve as ConversationServe

# Set config
system_app.config.set(
f"{CONVERSATION_SERVE_CONFIG_KEY_PREFIX}default_model", cfg.LLM_MODEL
)
# Register serve app
system_app.register(ConversationServe)
system_app.register(ConversationServe, api_prefix="/api/v1/chat/dialogue")
# ################################ Conversation Serve Register End ########################################
Loading

0 comments on commit 6b76154

Please sign in to comment.