mirror of
https://github.com/microsoft/autogen.git
synced 2025-12-27 15:09:41 +00:00
parent
a5681d73c6
commit
ad7433e817
@ -21,12 +21,10 @@ from typing import (
|
||||
get_origin,
|
||||
)
|
||||
|
||||
from pydantic import BaseModel, Field, create_model # type: ignore
|
||||
from pydantic import BaseModel, Field, TypeAdapter, create_model # type: ignore
|
||||
from pydantic_core import PydanticUndefined
|
||||
from typing_extensions import Literal
|
||||
|
||||
from ._pydantic_compat import model_dump, type2schema
|
||||
|
||||
logger = getLogger(__name__)
|
||||
|
||||
T = TypeVar("T")
|
||||
@ -141,7 +139,7 @@ def get_parameter_json_schema(k: str, v: Any, default_values: Dict[str, Any]) ->
|
||||
A Pydanitc model for the parameter
|
||||
"""
|
||||
|
||||
schema = type2schema(v)
|
||||
schema = TypeAdapter(v).json_schema()
|
||||
if k in default_values:
|
||||
dv = default_values[k]
|
||||
schema["default"] = dv
|
||||
@ -293,7 +291,7 @@ def get_function_schema(f: Callable[..., Any], *, name: Optional[str] = None, de
|
||||
)
|
||||
)
|
||||
|
||||
return model_dump(function)
|
||||
return function.model_dump()
|
||||
|
||||
|
||||
def normalize_annotated_type(type_hint: Type[Any]) -> Type[Any]:
|
||||
|
||||
@ -1,50 +0,0 @@
|
||||
# File based from: https://github.com/microsoft/autogen/blob/47f905267245e143562abfb41fcba503a9e1d56d/autogen/_pydantic.py
|
||||
# Credit to original authors
|
||||
|
||||
|
||||
from typing import Any, Dict, Tuple, Type, Union, get_args
|
||||
|
||||
from pydantic import BaseModel
|
||||
from pydantic.version import VERSION as PYDANTIC_VERSION
|
||||
from typing_extensions import get_origin
|
||||
|
||||
__all__ = ("model_dump", "type2schema")
|
||||
|
||||
PYDANTIC_V1 = PYDANTIC_VERSION.startswith("1.")
|
||||
|
||||
|
||||
def type2schema(t: Type[Any] | None) -> Dict[str, Any]:
|
||||
if PYDANTIC_V1:
|
||||
from pydantic import schema_of # type: ignore
|
||||
|
||||
if t is None:
|
||||
return {"type": "null"}
|
||||
elif get_origin(t) is Union:
|
||||
return {"anyOf": [type2schema(tt) for tt in get_args(t)]}
|
||||
elif get_origin(t) in [Tuple, tuple]:
|
||||
prefixItems = [type2schema(tt) for tt in get_args(t)]
|
||||
return {
|
||||
"maxItems": len(prefixItems),
|
||||
"minItems": len(prefixItems),
|
||||
"prefixItems": prefixItems,
|
||||
"type": "array",
|
||||
}
|
||||
|
||||
d = schema_of(t) # type: ignore
|
||||
if "title" in d:
|
||||
d.pop("title")
|
||||
if "description" in d:
|
||||
d.pop("description")
|
||||
|
||||
return d
|
||||
else:
|
||||
from pydantic import TypeAdapter
|
||||
|
||||
return TypeAdapter(t).json_schema()
|
||||
|
||||
|
||||
def model_dump(model: BaseModel) -> Dict[str, Any]:
|
||||
if PYDANTIC_V1:
|
||||
return model.dict() # type: ignore
|
||||
else:
|
||||
return model.model_dump()
|
||||
Loading…
x
Reference in New Issue
Block a user