mirror of
https://github.com/PaddlePaddle/FastDeploy.git
synced 2025-10-04 08:16:42 +08:00
Sync v2.0 version of code to github repo
This commit is contained in:
@@ -28,23 +28,12 @@ from fastdeploy.input.multimodal.video import VideoMediaIO
|
||||
from fastdeploy.input.multimodal.image import ImageMediaIO
|
||||
|
||||
class VideoURL(TypedDict, total=False):
|
||||
"""
|
||||
Represents a video URL or base64 encoded video data.
|
||||
|
||||
Attributes:
|
||||
url: Required string containing either a URL or base64 encoded video data
|
||||
"""
|
||||
"""Video URL object"""
|
||||
url: Required[str]
|
||||
"""Either a URL of the video or the base64 encoded video data"""
|
||||
|
||||
class CustomChatCompletionContentPartVideoParam(TypedDict, total=False):
|
||||
"""
|
||||
Custom video content part parameter for chat completion.
|
||||
|
||||
Attributes:
|
||||
video_url: Required VideoURL object containing video data
|
||||
type: Required literal string "video_url" indicating content type
|
||||
"""
|
||||
"""Custom Video URL object"""
|
||||
video_url: Required[VideoURL]
|
||||
|
||||
type: Required[Literal["video_url"]]
|
||||
@@ -54,15 +43,8 @@ CustomChatCompletionContentPartParam: TypeAlias = Union[
|
||||
OpenAIChatCompletionContentPartParam, CustomChatCompletionContentPartVideoParam
|
||||
]
|
||||
|
||||
class CustomUserChatCompletionMessageParam(TypedDict, total=False):
|
||||
"""
|
||||
Custom user chat message parameter for chat completion.
|
||||
|
||||
Attributes:
|
||||
content: Required content of the message (string or list of content parts)
|
||||
role: Required string indicating the role of message author (should be 'user')
|
||||
name: Optional name to differentiate between participants of same role
|
||||
"""
|
||||
class CustomChatCompletionMessageParam(TypedDict, total=False):
|
||||
"""Custom User chat message parameter."""
|
||||
|
||||
content: Required[Union[str, List[CustomChatCompletionContentPartParam]]]
|
||||
"""The contents of the user message"""
|
||||
@@ -76,63 +58,29 @@ class CustomUserChatCompletionMessageParam(TypedDict, total=False):
|
||||
Provides the model information to differentiate between participants of the same role.
|
||||
"""
|
||||
|
||||
ChatCompletionMessageParam = Union[OpenAIChatCompletionMessageParam, CustomUserChatCompletionMessageParam]
|
||||
ChatCompletionMessageParam = Union[OpenAIChatCompletionMessageParam, CustomChatCompletionMessageParam]
|
||||
|
||||
|
||||
class MultiModalPartParser(object):
|
||||
"""
|
||||
Parser for handling multi-modal content parts (images, videos, etc.)
|
||||
|
||||
Attributes:
|
||||
image_io: ImageMediaIO instance for handling image operations
|
||||
video_io: VideoMediaIO instance for handling video operations
|
||||
"""
|
||||
"""Multi Modal Part parser"""
|
||||
def __init__(self):
|
||||
self.image_io = ImageMediaIO()
|
||||
self.video_io = VideoMediaIO(self.image_io)
|
||||
self.video_io = VideoMediaIO()
|
||||
|
||||
def parse_image(self, image_url):
|
||||
"""
|
||||
Parse an image from given URL.
|
||||
|
||||
Args:
|
||||
image_url: URL or base64 string of the image
|
||||
|
||||
Returns:
|
||||
Parsed image data
|
||||
"""
|
||||
# image_io = ImageMediaIO()
|
||||
""""Parse Image"""
|
||||
return self.load_from_url(image_url, self.image_io)
|
||||
|
||||
def parse_video(self, video_url):
|
||||
"""
|
||||
Parse a video from given URL.
|
||||
|
||||
Args:
|
||||
video_url: URL or base64 string of the video
|
||||
|
||||
Returns:
|
||||
Parsed video data
|
||||
"""
|
||||
# video_io = VideoMediaIO()
|
||||
return self.get_bytes(video_url)
|
||||
|
||||
"""Parse Video"""
|
||||
return self.load_from_url(video_url, self.video_io)
|
||||
|
||||
def load_from_url(self, url, media_io):
|
||||
"""
|
||||
Load media content from URL or base64 string.
|
||||
|
||||
Args:
|
||||
url: URL or base64 string of the media
|
||||
media_io: MediaIO instance for handling the specific media type
|
||||
|
||||
Returns:
|
||||
Loaded media data
|
||||
"""
|
||||
"""Load media from URL"""
|
||||
|
||||
parsed = urlparse(url)
|
||||
if parsed.scheme.startswith("http"):
|
||||
media_bytes = self.get_bytes(url)
|
||||
media_bytes = requests.get(url).content
|
||||
return media_io.load_bytes(media_bytes)
|
||||
|
||||
if parsed.scheme.startswith("data"):
|
||||
@@ -144,35 +92,8 @@ class MultiModalPartParser(object):
|
||||
localpath = parsed.path
|
||||
return media_io.load_file(localpath)
|
||||
|
||||
def get_bytes(self, url):
|
||||
"""
|
||||
Fetch raw bytes from a URL.
|
||||
|
||||
Args:
|
||||
url: URL to fetch data from
|
||||
|
||||
Returns:
|
||||
bytes: Raw content from the URL
|
||||
"""
|
||||
# TODO: Add error handling and timeout
|
||||
return requests.get(url).content
|
||||
|
||||
|
||||
def parse_content_part(mm_parser, part):
|
||||
"""
|
||||
Parse a single content part (text, image or video).
|
||||
Currently supports OpenAI-compatible formats.
|
||||
|
||||
Args:
|
||||
mm_parser: MultiModalPartParser instance
|
||||
part: Content part to parse
|
||||
|
||||
Returns:
|
||||
dict: Parsed content part
|
||||
|
||||
Raises:
|
||||
ValueError: If content part type is unknown
|
||||
"""
|
||||
"""only support openai compatible format for now"""
|
||||
|
||||
part_type = part.get("type", None)
|
||||
|
||||
@@ -202,15 +123,7 @@ def parse_content_part(mm_parser, part):
|
||||
#TODO async
|
||||
#def parse_chat_messages(messages: List[ChatCompletionMessageParam]):
|
||||
def parse_chat_messages(messages):
|
||||
"""
|
||||
Parse a list of chat messages into standardized format.
|
||||
|
||||
Args:
|
||||
messages: List of chat messages to parse
|
||||
|
||||
Returns:
|
||||
list: Parsed conversation in standardized format
|
||||
"""
|
||||
"""Parse chat messages to [dict]"""
|
||||
|
||||
mm_parser = MultiModalPartParser()
|
||||
|
||||
@@ -228,4 +141,4 @@ def parse_chat_messages(messages):
|
||||
parsed_content = [parse_content_part(mm_parser, part) for part in content]
|
||||
|
||||
conversation.append({"role": role, "content": parsed_content})
|
||||
return conversation
|
||||
return conversation
|
Reference in New Issue
Block a user