attach images to message

This commit is contained in:
garylin2099 2024-08-14 20:12:17 +08:00
parent dce5502c07
commit e9984f2bf8
6 changed files with 83 additions and 33 deletions

View file

@ -840,12 +840,6 @@ def decode_image(img_url_or_b64: str) -> Image:
return img
def is_support_image_input(model_name: str) -> bool:
# model name can be gpt-4o-2024-08-06
support_models = ["gpt-4o", "gpt-4o-mini"] # FIXME: hard code for now
return any([m in model_name for m in support_models])
def extract_image_paths(content: str) -> bool:
# We require that the path must have a space preceding it, like "xxx /an/absolute/path.jpg xxx"
pattern = r"[^\s]+\.(?:png|jpe?g|gif|bmp|tiff)"
@ -853,6 +847,14 @@ def extract_image_paths(content: str) -> bool:
return image_paths
def extract_and_encode_images(content: str) -> list[str]:
images = []
for path in extract_image_paths(content):
if os.path.exists(path):
images.append(encode_image(path))
return images
def log_and_reraise(retry_state: RetryCallState):
logger.error(f"Retry attempts exhausted. Last exception: {retry_state.outcome.exception()}")
logger.warning(