feat(core): Upgrade pydantic to 2.x (#1428)

This commit is contained in:
Fangyin Cheng
2024-04-20 09:41:16 +08:00
committed by GitHub
parent baa1e3f9f6
commit 57be1ece18
103 changed files with 1146 additions and 534 deletions

View File

@@ -138,7 +138,9 @@ async def update(
@router.delete("/flows/{uid}")
async def delete(uid: str, service: Service = Depends(get_service)) -> Result[None]:
async def delete(
uid: str, service: Service = Depends(get_service)
) -> Result[ServerResponse]:
"""Delete a Flow entity
Args:

View File

@@ -1,3 +1,5 @@
from dbgpt._private.pydantic import ConfigDict
# Define your Pydantic schemas here
from dbgpt.core.awel.flow.flow_factory import FlowPanel
@@ -10,5 +12,5 @@ class ServerResponse(FlowPanel):
"""Flow response model"""
# TODO define your own fields here
class Config:
title = f"ServerResponse for {SERVE_APP_NAME_HUMP}"
model_config = ConfigDict(title=f"ServerResponse for {SERVE_APP_NAME_HUMP}")

View File

@@ -7,6 +7,7 @@ from typing import Any, Dict, Union
from sqlalchemy import Column, DateTime, Integer, String, Text, UniqueConstraint
from dbgpt._private.pydantic import model_to_dict
from dbgpt.core.awel.flow.flow_factory import State
from dbgpt.storage.metadata import BaseDao, Model
from dbgpt.storage.metadata._base_dao import QUERY_SPEC
@@ -82,7 +83,9 @@ class ServeDao(BaseDao[ServeEntity, ServeRequest, ServerResponse]):
Returns:
T: The entity
"""
request_dict = request.dict() if isinstance(request, ServeRequest) else request
request_dict = (
model_to_dict(request) if isinstance(request, ServeRequest) else request
)
flow_data = json.dumps(request_dict.get("flow_data"), ensure_ascii=False)
state = request_dict.get("state", State.INITIALIZING.value)
error_message = request_dict.get("error_message")
@@ -184,7 +187,7 @@ class ServeDao(BaseDao[ServeEntity, ServeRequest, ServerResponse]):
entry.flow_category = update_request.flow_category
if update_request.flow_data:
entry.flow_data = json.dumps(
update_request.flow_data.dict(), ensure_ascii=False
model_to_dict(update_request.flow_data), ensure_ascii=False
)
if update_request.description:
entry.description = update_request.description

View File

@@ -1,6 +1,5 @@
import json
import logging
import time
import traceback
from typing import Any, AsyncIterator, List, Optional, cast
@@ -236,6 +235,8 @@ class Service(BaseService[ServeEntity, ServeRequest, ServerResponse]):
flow.uid = exist_inst.uid
self.update_flow(flow, check_editable=False, save_failed_flow=True)
except Exception as e:
import traceback
message = traceback.format_exc()
logger.warning(
f"Load DAG {flow.name} from dbgpts error: {str(e)}, detail: {message}"
@@ -296,7 +297,10 @@ class Service(BaseService[ServeEntity, ServeRequest, ServerResponse]):
)
return self.create_and_save_dag(update_obj)
except Exception as e:
if old_data:
if old_data and old_data.state == State.RUNNING:
# Old flow is running, try to recover it
# first set the state to DEPLOYED
old_data.state = State.DEPLOYED
self.create_and_save_dag(old_data)
raise e
@@ -387,8 +391,8 @@ class Service(BaseService[ServeEntity, ServeRequest, ServerResponse]):
request.incremental = False
async for output in self.safe_chat_stream_flow(flow_uid, request):
text = output.text
# if text:
# text = text.replace("\n", "\\n")
if text:
text = text.replace("\n", "\\n")
if output.error_code != 0:
yield f"data:[SERVER_ERROR]{text}\n\n"
break
@@ -407,7 +411,9 @@ class Service(BaseService[ServeEntity, ServeRequest, ServerResponse]):
chunk = ChatCompletionStreamResponse(
id=conv_uid, choices=[choice_data], model=request.model
)
yield f"data: {chunk.json(exclude_unset=True, ensure_ascii=False)}\n\n"
json_data = model_to_json(chunk, exclude_unset=True, ensure_ascii=False)
yield f"data: {json_data}\n\n"
request.incremental = True
async for output in self.safe_chat_stream_flow(flow_uid, request):