Skip to content

# `mistral_common.protocol.instruct.converters`

## `convert_openai_messages(messages)`

Convert OpenAI messages to Mistral messages.

Parameters:

Name | Type | Description | Default
--- | --- | --- | ---
`messages` | `list[dict[str, str | list[dict[str, str | dict[str, Any]]]]]` | The OpenAI messages to convert. | required

Returns:

Type | Description
--- | ---
`list[ChatMessage]` | The Mistral messages.

Source code in `src/mistral_common/protocol/instruct/converters.py`

    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39

    def convert_openai_messages(
        messages: list[dict[str, str | list[dict[str, str | dict[str, Any]]]]],
    ) -> list[ChatMessage]:
        r"""Convert OpenAI messages to Mistral messages.

        Args:
            messages: The OpenAI messages to convert.

        Returns:
            The Mistral messages.
        """
        converted_messages: list[ChatMessage] = []
        for openai_message in messages:
            message_role = openai_message.get("role")
            message: ChatMessage
            if message_role == "user":
                message = UserMessage.from_openai(openai_message)
            elif message_role == "assistant":
                message = AssistantMessage.from_openai(openai_message)
            elif message_role == "tool":
                message = ToolMessage.from_openai(openai_message)
            elif message_role == "system":
                message = SystemMessage.from_openai(openai_message)
            else:
                raise ValueError(f"Unknown message role: {message_role}")
            converted_messages.append(message)
        return converted_messages

## `convert_openai_tools(tools)`

Convert OpenAI tools to Mistral tools.

Parameters:

Name | Type | Description | Default
--- | --- | --- | ---
`tools` | `list[dict[str, Any]]` | The OpenAI tools to convert. | required

Returns:

Type | Description
--- | ---
`list[Tool]` | The Mistral tools.

Source code in `src/mistral_common/protocol/instruct/converters.py`

    42
    43
    44
    45
    46
    47
    48
    49
    50
    51
    52
    53
    54

    def convert_openai_tools(
        tools: list[dict[str, Any]],
    ) -> list[Tool]:
        r"""Convert OpenAI tools to Mistral tools.

        Args:
            tools: The OpenAI tools to convert.

        Returns:
            The Mistral tools.
        """
        converted_tools = [Tool.from_openai(openai_tool) for openai_tool in tools]
        return converted_tools