doc_understanding.py 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114
  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. import time
  15. from typing import Any, List
  16. from .....utils.deps import function_requires_deps, is_dep_available
  17. from ...infra import utils as serving_utils
  18. from ...infra.config import AppConfig
  19. from ...schemas.doc_understanding import (
  20. INFER_ENDPOINT,
  21. ImageContent,
  22. ImageUrl,
  23. InferRequest,
  24. Message,
  25. RoleType,
  26. TextContent,
  27. )
  28. from .._app import create_app, primary_operation
  29. if is_dep_available("fastapi"):
  30. from fastapi import FastAPI
  31. if is_dep_available("openai"):
  32. from openai.types.chat import ChatCompletion
  33. from openai.types.chat.chat_completion import Choice as ChatCompletionChoice
  34. from openai.types.chat.chat_completion_message import ChatCompletionMessage
  35. @function_requires_deps("fastapi", "openai")
  36. def create_pipeline_app(pipeline: Any, app_config: AppConfig) -> "FastAPI":
  37. app, ctx = create_app(
  38. pipeline=pipeline, app_config=app_config, app_aiohttp_session=True
  39. )
  40. @primary_operation(
  41. app,
  42. "/chat/completions",
  43. "inferA",
  44. )
  45. @primary_operation(
  46. app,
  47. INFER_ENDPOINT,
  48. "infer",
  49. )
  50. async def _infer(request: InferRequest) -> "ChatCompletion":
  51. pipeline = ctx.pipeline
  52. def _process_messages(messages: List[Message]):
  53. system_message = ""
  54. user_message = ""
  55. image_url = ""
  56. for msg in messages:
  57. if msg.role == RoleType.SYSTEM:
  58. if isinstance(msg.content, list):
  59. for content in msg.content:
  60. if isinstance(content, TextContent):
  61. system_message = content.text
  62. break
  63. else:
  64. system_message = msg.content
  65. elif msg.role == RoleType.USER:
  66. if isinstance(msg.content, list):
  67. for content in msg.content:
  68. if isinstance(content, str):
  69. user_message = content
  70. else:
  71. if isinstance(content, TextContent):
  72. user_message = content.text
  73. elif isinstance(content, ImageContent):
  74. image_url = content.image_url
  75. if isinstance(image_url, ImageUrl):
  76. image_url = image_url.url
  77. else:
  78. user_message = msg.content
  79. return system_message, user_message, image_url
  80. system_message, user_message, image_url = _process_messages(request.messages)
  81. result = (
  82. await pipeline.infer(
  83. {"image": image_url, "query": user_message},
  84. )
  85. )[0]
  86. return ChatCompletion(
  87. id=serving_utils.generate_log_id(),
  88. model=request.model,
  89. choices=[
  90. ChatCompletionChoice(
  91. index=0,
  92. finish_reason="stop",
  93. message=ChatCompletionMessage(
  94. role="assistant",
  95. content=result["result"],
  96. ),
  97. )
  98. ],
  99. created=int(time.time()),
  100. object="chat.completion",
  101. )
  102. return app