def truncate_messages()

in aidial_adapter_vertexai/chat/gemini/prompt/gemini_1_0_pro_vision.py [0:0]


def truncate_messages(messages: List[Message]) -> List[Message]:
    """
    Only a single message is supported by Gemini 1.0 Pro Vision,
    when non-text parts are present in the request.

    Otherwise, the following error is thrown:
        400 Unable to submit request because it has more than
        one contents field but model gemini-pro-vision only supports one.
        Remove all but one contents and try again.

    Conversely, multi-turn chat with text-only content is supported.
    However, we disable it, because it's hard to make it clear to the user,
    when the model works in a single-turn mode and when in a multi-turn mode.
    Thus, we make the model single-turn altogether.
    """

    msg1 = messages[-1]

    # Supporting a typical use-case when a user asks
    # a question about assistant-generated attachments:
    #   [-2]: Assistant message with attachments
    #   [-1]: User message with a question about attachments
    #         without attachments of their own
    if len(messages) > 1:
        msg2 = messages[-2]
        attachments1 = get_attachments(msg1)
        attachments2 = get_attachments(msg2)
        if msg2.role == Role.ASSISTANT and attachments2 and not attachments1:
            msg1 = msg1.copy()
            msg1.custom_content = msg2.custom_content

    return [msg1]