feat: Add dbgpt client and add api v2

This commit is contained in:
Fangyin Cheng
2024-03-18 18:24:08 +08:00
parent 4970c9f813
commit 0ed30aa44a
39 changed files with 2663 additions and 143 deletions

View File

@@ -1,7 +1,7 @@
from functools import cache
from typing import List, Optional, Union
from fastapi import APIRouter, Depends, HTTPException, Query
from fastapi import APIRouter, Depends, HTTPException, Query, Request
from fastapi.security.http import HTTPAuthorizationCredentials, HTTPBearer
from dbgpt.component import SystemApp
@@ -9,7 +9,7 @@ from dbgpt.core.awel.flow import ResourceMetadata, ViewMetadata
from dbgpt.serve.core import Result
from dbgpt.util import PaginationResult
from ..config import APP_NAME, SERVE_APP_NAME, SERVE_SERVICE_COMPONENT_NAME, ServeConfig
from ..config import APP_NAME, SERVE_SERVICE_COMPONENT_NAME, ServeConfig
from ..service.service import Service
from .schemas import ServeRequest, ServerResponse
@@ -45,6 +45,7 @@ def _parse_api_keys(api_keys: str) -> List[str]:
async def check_api_key(
auth: Optional[HTTPAuthorizationCredentials] = Depends(get_bearer_token),
request: Request = None,
service: Service = Depends(get_service),
) -> Optional[str]:
"""Check the api key
@@ -63,6 +64,10 @@ async def check_api_key(
assert res.status_code == 200
"""
if request.url.path.startswith(f"/api/v1"):
return None
# for api_version in serve.serve_versions():
if service.config.api_keys:
api_keys = _parse_api_keys(service.config.api_keys)
if auth is None or (token := auth.credentials) not in api_keys:

View File

@@ -27,11 +27,13 @@ class Serve(BaseServe):
def __init__(
self,
system_app: SystemApp,
api_prefix: Optional[str] = f"/api/v1/serve/awel",
api_prefix: Optional[List[str]] = None,
api_tags: Optional[List[str]] = None,
db_url_or_db: Union[str, URL, DatabaseManager] = None,
try_create_tables: Optional[bool] = False,
):
if api_prefix is None:
api_prefix = [f"/api/v1/serve/awel", "/api/v2/serve/awel"]
if api_tags is None:
api_tags = [SERVE_APP_NAME_HUMP]
super().__init__(
@@ -43,9 +45,10 @@ class Serve(BaseServe):
if self._app_has_initiated:
return
self._system_app = system_app
self._system_app.app.include_router(
router, prefix=self._api_prefix, tags=self._api_tags
)
for prefix in self._api_prefix:
self._system_app.app.include_router(
router, prefix=prefix, tags=self._api_tags
)
init_endpoints(self._system_app)
self._app_has_initiated = True