def replace_image_filenames_with_urls()

in tools/ragindex/vector_index_retrieval.py [0:0]


def replace_image_filenames_with_urls(content: str, related_images: list) -> str:
    """
    Replace image filenames or relative paths in the content string with their corresponding full URLs
    from the related_images list.
    """
    for image_url in related_images:
        # Parse the URL and remove the leading slash from the path
        logging.debug(f"[multimodal_vector_index_retrieve] image_url: {image_url}.")
        parsed_url = urlparse(image_url)
        image_path = parsed_url.path.lstrip('/')  # e.g., 'documents-images/myfolder/filename.png'
        logging.debug(f"[multimodal_vector_index_retrieve] image_path: {image_path}.")
        # Replace occurrences of the relative path in the content with the full URL
        content = content.replace(image_path, image_url)
        logging.debug(f"[multimodal_vector_index_retrieve] content: {content}.")
        # Also replace only the filename if it appears alone
        # filename = image_path.split('/')[-1]
        # content = content.replace(filename, image_url)

    return content