Source code for langchain.chat_models.minimax
"""Wrapper around Minimax chat models."""
import logging
from typing import Any, Dict, List, Optional
from langchain.callbacks.manager import (
    AsyncCallbackManagerForLLMRun,
    CallbackManagerForLLMRun,
)
from langchain.chat_models.base import BaseChatModel
from langchain.llms.minimax import MinimaxCommon
from langchain.llms.utils import enforce_stop_tokens
from langchain.schema import (
    AIMessage,
    BaseMessage,
    ChatResult,
    HumanMessage,
)
logger = logging.getLogger(__name__)
def _parse_message(msg_type: str, text: str) -> Dict:
    return {"sender_type": msg_type, "text": text}
def _parse_chat_history(history: List[BaseMessage]) -> List:
    """Parse a sequence of messages into history."""
    chat_history = []
    for message in history:
        if isinstance(message, HumanMessage):
            chat_history.append(_parse_message("USER", message.content))
        if isinstance(message, AIMessage):
            chat_history.append(_parse_message("BOT", message.content))
    return chat_history
[docs]class MiniMaxChat(MinimaxCommon, BaseChatModel):
    """Wrapper around Minimax large language models.
    To use, you should have the environment variable ``MINIMAX_GROUP_ID`` and
    ``MINIMAX_API_KEY`` set with your API token, or pass it as a named parameter to
    the constructor.
    Example:
        .. code-block:: python
            from langchain.chat_models import MiniMaxChat
            llm = MiniMaxChat(model_name="abab5-chat")
    """
    def _generate(
        self,
        messages: List[BaseMessage],
        stop: Optional[List[str]] = None,