def get_cohere_chat_request(
        self,
        messages: List[ChatMessage],
        *,
        connectors: Optional[List[Dict[str, str]]] = None,
        stop_sequences: Optional[List[str]] = None,
        **kwargs: Any,
    ) -> Dict[str, Any]:
        """
        Get the request for the Cohere chat API.

        Args:
            messages: The messages.
            connectors: The connectors.
            **kwargs: The keyword arguments.

        Returns:
            The request for the Cohere chat API.

        """
        additional_kwargs = messages[-1].additional_kwargs

        # cohere SDK will fail loudly if both connectors and documents are provided
        if additional_kwargs.get("documents", []) and documents and len(documents) > 0:
            raise ValueError(
                "Received documents both as a keyword argument and as an prompt additional keyword argument. Please choose only one option."
            )

        messages, documents = remove_documents_from_messages(messages)

        tool_results: Optional[List[Dict[str, Any]]] = (
            _messages_to_cohere_tool_results_curr_chat_turn(messages)
            or kwargs.get("tool_results")
        )
        if not tool_results:
            tool_results = None

        chat_history = []
        temp_tool_results = []
        # if force_single_step is set to False, then only message is empty in request if there is tool call
        if not kwargs.get("force_single_step"):
            for i, message in enumerate(messages[:-1]):
                # If there are multiple tool messages, then we need to aggregate them into one single tool message to pass into chat history
                if message.role == MessageRole.TOOL:
                    temp_tool_results += _message_to_cohere_tool_results(messages, i)

                    if (i == len(messages) - 1) or messages[
                        i + 1
                    ].role != MessageRole.TOOL:
                        cohere_message = _get_message_cohere_format(
                            message, temp_tool_results
                        )
                        chat_history.append(cohere_message)
                        temp_tool_results = []
                else:
                    chat_history.append(_get_message_cohere_format(message, None))

            message_str = "" if tool_results else messages[-1].content

        else:
            message_str = ""
            # if force_single_step is set to True, then message is the last human message in the conversation
            for message in messages[:-1]:
                if message.role in (
                    MessageRole.CHATBOT,
                    MessageRole.ASSISTANT,
                ) and message.additional_kwargs.get("tool_calls"):
                    continue

                # If there are multiple tool messages, then we need to aggregate them into one single tool message to pass into chat history
                if message.role == MessageRole.TOOL:
                    temp_tool_results += _message_to_cohere_tool_results(messages, i)

                    if (i == len(messages) - 1) or messages[
                        i + 1
                    ].role != MessageRole.TOOL:
                        cohere_message = _get_message_cohere_format(
                            message, temp_tool_results
                        )
                        chat_history.append(cohere_message)
                        temp_tool_results = []
                else:
                    chat_history.append(_get_message_cohere_format(message, None))
            # Add the last human message in the conversation to the message string
            for message in messages[::-1]:
                if (message.role == MessageRole.USER) and (message.content):
                    message_str = message.content
                    break

        req = {
            "message": message_str,
            "chat_history": chat_history,
            "tool_results": tool_results,
            "documents": documents,
            "connectors": connectors,
            "stop_sequences": stop_sequences,
            **kwargs,
        }
        return {k: v for k, v in req.items() if v is not None}

        Get the request for the Cohere chat API.

        Args:
            messages: The messages.
            connectors: The connectors.
            **kwargs: The keyword arguments.

        Returns:
            The request for the Cohere chat API.

        
llama-index-integrations/llms/llama-index-llms-cohere/llama_index/llms/cohere/base.py