MCPcopy
hub / github.com/AsyncFuncAI/deepwiki-open / convert_inputs_to_api_kwargs

Method convert_inputs_to_api_kwargs

api/openai_client.py:270–382  ·  view source on GitHub ↗

r""" Specify the API input type and output api_kwargs that will be used in _call and _acall methods. Convert the Component's standard input, and system_input(chat model) and model_kwargs into API-specific format. For multimodal inputs, images can be provided in model_kwargs["

(
        self,
        input: Optional[Any] = None,
        model_kwargs: Dict = {},
        model_type: ModelType = ModelType.UNDEFINED,
    )

Source from the content-addressed store, hash-verified

268 return EmbedderOutput(data=[], error=str(e), raw_response=response)
269
270 def convert_inputs_to_api_kwargs(
271 self,
272 input: Optional[Any] = None,
273 model_kwargs: Dict = {},
274 model_type: ModelType = ModelType.UNDEFINED,
275 ) -> Dict:
276 r"""
277 Specify the API input type and output api_kwargs that will be used in _call and _acall methods.
278 Convert the Component's standard input, and system_input(chat model) and model_kwargs into API-specific format.
279 For multimodal inputs, images can be provided in model_kwargs["images"] as a string path, URL, or list of them.
280 The model specified in model_kwargs["model"] must support multimodal capabilities when using images.
281
282 Args:
283 input: The input text or messages to process
284 model_kwargs: Additional parameters including:
285 - images: Optional image source(s) as path, URL, or list of them
286 - detail: Image detail level ('auto', 'low', or 'high'), defaults to 'auto'
287 - model: The model to use (must support multimodal inputs if images are provided)
288 model_type: The type of model (EMBEDDER or LLM)
289
290 Returns:
291 Dict: API-specific kwargs for the model call
292 """
293
294 final_model_kwargs = model_kwargs.copy()
295 if model_type == ModelType.EMBEDDER:
296 if isinstance(input, str):
297 input = [input]
298 # convert input to input
299 if not isinstance(input, Sequence):
300 raise TypeError("input must be a sequence of text")
301 final_model_kwargs["input"] = input
302 elif model_type == ModelType.LLM:
303 # convert input to messages
304 messages: List[Dict[str, str]] = []
305 images = final_model_kwargs.pop("images", None)
306 detail = final_model_kwargs.pop("detail", "auto")
307
308 if self._input_type == "messages":
309 system_start_tag = "<START_OF_SYSTEM_PROMPT>"
310 system_end_tag = "<END_OF_SYSTEM_PROMPT>"
311 user_start_tag = "<START_OF_USER_PROMPT>"
312 user_end_tag = "<END_OF_USER_PROMPT>"
313
314 # new regex pattern to ignore special characters such as \n
315 pattern = (
316 rf"{system_start_tag}\s*(.*?)\s*{system_end_tag}\s*"
317 rf"{user_start_tag}\s*(.*?)\s*{user_end_tag}"
318 )
319
320 # Compile the regular expression
321
322 # re.DOTALL is to allow . to match newline so that (.*?) does not match in a single line
323 regex = re.compile(pattern, re.DOTALL)
324 # Match the pattern
325 match = regex.match(input)
326 system_prompt, input_str = None, None
327

Callers 3

handle_websocket_chatFunction · 0.45
chat_completions_streamFunction · 0.45
response_streamFunction · 0.45

Calls 2

_encode_imageMethod · 0.95

Tested by

no test coverage detected