@@ -340,31 +340,6 @@ def preprocess_prompts(self, input: Union[str, list], tools=None, tool_choice=No
340340 else :
341341 prompt = self .process_tool .get_prompt (input )
342342 return prompt
343- else :
344- if isinstance (input , list ) and input and isinstance (input [0 ], dict ):
345- prompt = self .predictor .tokenizer .apply_chat_template (input , tokenize = False )
346- elif isinstance (input , list ) and input and isinstance (input [0 ], list ):
347- prompt = [
348- self .predictor .tokenizer .apply_chat_template (t , tokenize = False )
349- for t in input
350- ]
351- elif isinstance (input , list ) and input and isinstance (input [0 ], ChatMessage ):
352- messages = []
353- for chat_message in input :
354- message = {"role" : chat_message .role , "content" : chat_message .content }
355- messages .append (message )
356- prompt = self .predictor .tokenizer .apply_chat_template (
357- messages , tokenize = False
358- )
359- elif isinstance (input , list ) and input and isinstance (input [0 ], str ):
360- prompt = input
361- elif isinstance (input , str ):
362- prompt = input
363- else :
364- raise TypeError (
365- f"Unsupported type { type (input )} for text. Expected dict or list of dicts."
366- )
367- return prompt
368343 elif prompt_format == PromptFormat .PROMPTS_FORMAT :
369344 raise HTTPException (400 , "Invalid prompt format." )
370345 return input
@@ -411,63 +386,18 @@ async def openai_call(
411386 tool_choice = None ,
412387 ):
413388 self .use_openai = True
389+ print ("openai_call" )
390+ print (input )
391+ print (type (input ))
414392
415393 # return prompt or list of prompts preprocessed
416394 prompts = self .preprocess_prompts (input , tools , tool_choice )
395+ print (prompts )
396+ print (type (prompts ))
417397
418398 # Handle streaming response
419399 if streaming_response :
420400 async for result in self .handle_streaming (prompts , config ):
421401 yield result
422402 else :
423403 yield await self .handle_non_streaming (prompts , config )
424-
425- def _extract_messages (self , messages ):
426- texts , images = [], []
427- for message in messages :
428- if message ["role" ] == "user" and isinstance (message ["content" ], list ):
429- texts .append ({"role" : "user" , "content" : message ["content" ][0 ]["text" ]})
430- images .append (
431- {"role" : "user" , "content" : message ["content" ][1 ]["image_url" ]["url" ]}
432- )
433- else :
434- texts .append (message )
435- return texts , images
436-
437- def _prepare_image (self , messages : list ):
438- """Prepare image from history messages."""
439- from PIL import Image
440- import requests
441- from io import BytesIO
442- import base64
443- import re
444-
445- # prepare images
446- images : List = []
447- if isinstance (messages [0 ], List ):
448- for i in range (len (messages )):
449- for msg in messages [i ]:
450- msg = dict (msg )
451- content = msg ["content" ]
452- if "url" not in content :
453- continue
454- is_data = len (re .findall ("^data:image/.+;base64," , content ["url" ])) > 0
455- if is_data :
456- encoded_str = re .sub ("^data:image/.+;base64," , "" , content ["url" ])
457- images [i ].append (Image .open (BytesIO (base64 .b64decode (encoded_str ))))
458- else :
459- images [i ].append (Image .open (requests .get (content ["url" ], stream = True ).raw ))
460- elif isinstance (messages [0 ], dict ):
461- for msg in messages :
462- msg = dict (msg )
463- content = msg ["content" ]
464- if "url" not in content :
465- continue
466- is_data = len (re .findall ("^data:image/.+;base64," , content ["url" ])) > 0
467- if is_data :
468- encoded_str = re .sub ("^data:image/.+;base64," , "" , content ["url" ])
469- images .append (Image .open (BytesIO (base64 .b64decode (encoded_str ))))
470- else :
471- images .append (Image .open (requests .get (content ["url" ], stream = True ).raw ))
472-
473- return images
0 commit comments