def remove_documents_from_messages(
    messages: Sequence[ChatMessage],
) -> Tuple[Sequence[ChatMessage], Optional[List[Dict[str, str]]]]:
    """
    Splits messages into two lists: `remaining` and `documents`.
    `remaining` contains all messages that aren't of type DocumentMessage (e.g. history, query).
    `documents` contains the retrieved documents, formatted as expected by Cohere RAG inference calls.

    NOTE: this will mix turns for multi-turn RAG
    """
    documents = []
    remaining = []
    for msg in messages:
        if isinstance(msg, DocumentMessage):
            documents.append(msg)
        else:
            remaining.append(msg)
    return remaining, messages_to_cohere_documents(documents)

    Splits messages into two lists: `remaining` and `documents`.
    `remaining` contains all messages that aren't of type DocumentMessage (e.g. history, query).
    `documents` contains the retrieved documents, formatted as expected by Cohere RAG inference calls.

    NOTE: this will mix turns for multi-turn RAG
    
llama-index-integrations/llms/llama-index-llms-cohere/llama_index/llms/cohere/utils.py