(
self,
*,
messages: List[ChatCompletionRequestMessage],
media_inputs: List[MediaInput],
functions: Optional[List[ChatTemplateFunctionDefinition]],
function_call: Optional[Union[str, ChatTemplateFunctionCall]],
tools: Optional[List[ChatTemplateTool]],
tool_choice: Optional[Union[str, ChatTemplateToolChoice]],
reasoning_effort: Optional[str],
)
| 10996 | ) |
| 10997 | |
| 10998 | def _build_prompt_plan_locked( |
| 10999 | self, |
| 11000 | *, |
| 11001 | messages: List[ChatCompletionRequestMessage], |
| 11002 | media_inputs: List[MediaInput], |
| 11003 | functions: Optional[List[ChatTemplateFunctionDefinition]], |
| 11004 | function_call: Optional[Union[str, ChatTemplateFunctionCall]], |
| 11005 | tools: Optional[List[ChatTemplateTool]], |
| 11006 | tool_choice: Optional[Union[str, ChatTemplateToolChoice]], |
| 11007 | reasoning_effort: Optional[str], |
| 11008 | ) -> PromptPlan: |
| 11009 | prompt, generation_prompt, _ = self.chat_formatter.format( |
| 11010 | messages=messages, |
| 11011 | media_marker=self.media_marker, |
| 11012 | functions=functions, |
| 11013 | function_call=function_call, |
| 11014 | tools=tools, |
| 11015 | tool_choice=tool_choice, |
| 11016 | reasoning_effort=reasoning_effort, |
| 11017 | ) |
| 11018 | media_bytes_by_index = [self.load_media(media) for media in media_inputs] |
| 11019 | loaded_media: List[MTMDLoadedMedia] = [] |
| 11020 | chunks: Optional[Any] = None |
| 11021 | try: |
| 11022 | loaded_media = [ |
| 11023 | self._create_loaded_media(media, media_bytes) |
| 11024 | for media, media_bytes in zip(media_inputs, media_bytes_by_index) |
| 11025 | ] |
| 11026 | loaded_media_by_key = {media.key: media for media in loaded_media} |
| 11027 | video_media = [media for media in loaded_media if media.media.kind == "video"] |
| 11028 | if len(video_media) > 1 and any(media.video_frame_count <= 0 for media in video_media): |
| 11029 | raise CompletionRequestValidationError( |
| 11030 | "multiple videos require MTMD to report frame counts" |
| 11031 | ) |
| 11032 | input_text = mtmd_cpp.mtmd_input_text() |
| 11033 | input_text.text = prompt.encode("utf-8") |
| 11034 | input_text.add_special = False |
| 11035 | input_text.parse_special = True |
| 11036 | chunks = mtmd_cpp.mtmd_input_chunks_init() |
| 11037 | if chunks is None: |
| 11038 | raise CompletionRequestValidationError("failed to create MTMD input chunks") |
| 11039 | bitmap_array = (mtmd_cpp.mtmd_bitmap_p_ctypes * len(loaded_media))( |
| 11040 | *(media.bitmap for media in loaded_media) |
| 11041 | ) |
| 11042 | result = int( |
| 11043 | mtmd_cpp.mtmd_tokenize( |
| 11044 | self.ctx, |
| 11045 | chunks, |
| 11046 | ctypes.byref(input_text), |
| 11047 | bitmap_array, |
| 11048 | len(loaded_media), |
| 11049 | ) |
| 11050 | ) |
| 11051 | if result != 0: |
| 11052 | raise CompletionRequestValidationError( |
| 11053 | f"failed to tokenize MTMD prompt: error code {result}" |
| 11054 | ) |
| 11055 | parsed_chunks: List[MTMDProcessor.ParsedChunk] = [] |
no test coverage detected