models.py 1.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879
  1. # Copyright (c) 2024 PaddlePaddle Authors. All Rights Reserved.
  2. #
  3. # Licensed under the Apache License, Version 2.0 (the "License");
  4. # you may not use this file except in compliance with the License.
  5. # You may obtain a copy of the License at
  6. #
  7. # http://www.apache.org/licenses/LICENSE-2.0
  8. #
  9. # Unless required by applicable law or agreed to in writing, software
  10. # distributed under the License is distributed on an "AS IS" BASIS,
  11. # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. # See the License for the specific language governing permissions and
  13. # limitations under the License.
  14. from typing import Dict, Generic, List, Tuple, TypeVar, Union
  15. from pydantic import BaseModel, Discriminator
  16. from typing_extensions import Annotated, Literal, TypeAlias
  17. from ....utils.deps import is_dep_available
  18. if is_dep_available("openai"):
  19. from openai.types.chat import ChatCompletion
  20. __all__ = [
  21. "AIStudioNoResultResponse",
  22. "ResultT",
  23. "AIStudioResultResponse",
  24. "Response",
  25. "ImageInfo",
  26. "PDFPageInfo",
  27. "PDFInfo",
  28. "DataInfo",
  29. "PrimaryOperations",
  30. ]
  31. class AIStudioNoResultResponse(BaseModel):
  32. logId: str
  33. errorCode: int
  34. errorMsg: str
  35. ResultT = TypeVar("ResultT", bound=BaseModel)
  36. class AIStudioResultResponse(BaseModel, Generic[ResultT]):
  37. logId: str
  38. result: ResultT
  39. errorCode: Literal[0] = 0
  40. errorMsg: Literal["Success"] = "Success"
  41. Response: TypeAlias = Union[
  42. AIStudioResultResponse, AIStudioNoResultResponse, "ChatCompletion"
  43. ]
  44. class ImageInfo(BaseModel):
  45. width: int
  46. height: int
  47. type: Literal["image"] = "image"
  48. class PDFPageInfo(BaseModel):
  49. width: int
  50. height: int
  51. class PDFInfo(BaseModel):
  52. numPages: int
  53. pages: List[PDFPageInfo]
  54. type: Literal["pdf"] = "pdf"
  55. DataInfo: TypeAlias = Annotated[Union[ImageInfo, PDFInfo], Discriminator("type")]
  56. # Should we use generics?
  57. PrimaryOperations: TypeAlias = Dict[str, Tuple[str, BaseModel, BaseModel]]