def load_chat(
    base_url: str,
    model: str,
    token: str | None = None,
    *,
    file_types: Literal["text_encoded", "image"]
    | list[Literal["text_encoded", "image"]]
    | None = "text_encoded",
    system_message: str | None = None,
    streaming: bool = True,
    **kwargs,
) -> ChatInterface:
    """
    Load a chat interface from an OpenAI API chat compatible endpoint.
    Parameters:
        base_url: The base URL of the endpoint, e.g. "http://localhost:11434/v1/"
        model: The name of the model you are loading, e.g. "llama3.2"
        token: The API token or a placeholder string if you are using a local model, e.g. "ollama"
        file_types: The file types allowed to be uploaded by the user. "text_encoded" allows uploading any text-encoded file (which is simply appended to the prompt), and "image" adds image upload support. Set to None to disable file uploads.
        system_message: The system message to use for the conversation, if any.
        streaming: Whether the response should be streamed.
        kwargs: Additional keyword arguments to pass into ChatInterface for customization.
    Example:
        import gradio as gr
        gr.load_chat(
            "http://localhost:11434/v1/",
            model="qwen2.5",
            token="***",
            file_types=["text_encoded", "image"],
            system_message="You are a silly assistant.",
        ).launch()
    """
    try:
        from openai import OpenAI
    except ImportError as e:
        raise ImportError(
            "To use OpenAI API Client, you must install the `openai` package. You can install it with `pip install openai`."
        ) from e
    from gradio.chat_interface import ChatInterface

    client = OpenAI(api_key=token, base_url=base_url)
    start_message = (
        [{"role": "system", "content": system_message}] if system_message else []
    )
    file_types = utils.none_or_singleton_to_list(file_types)

    def open_api(message: str | MultimodalValue, history: list | None) -> str | None:
        history = history or start_message
        if len(history) > 0 and isinstance(history[0], (list, tuple)):
            history = ChatInterface._tuples_to_messages(history)
        conversation = format_conversation(history, message)  # type: ignore
        return (
            client.chat.completions.create(
                model=model,
                messages=conversation,  # type: ignore
            )
            .choices[0]
            .message.content
        )

    def open_api_stream(
        message: str | MultimodalValue, history: list | None
    ) -> Generator[str, None, None]:
        history = history or start_message
        if len(history) > 0 and isinstance(history[0], (list, tuple)):
            history = ChatInterface._tuples_to_messages(history)
        conversation = format_conversation(history, message)  # type: ignore
        stream = client.chat.completions.create(
            model=model,
            messages=conversation,  # type: ignore
            stream=True,
        )
        response = ""
        for chunk in stream:
            if chunk.choices[0].delta.content is not None:
                response += chunk.choices[0].delta.content
                yield response

    supported_extensions = []
    for file_type in file_types:
        if file_type == "text_encoded":
            supported_extensions += TEXT_FILE_EXTENSIONS
        elif file_type == "image":
            supported_extensions += IMAGE_FILE_EXTENSIONS
        else:
            raise ValueError(
                f"Invalid file type: {file_type}. Must be 'text_encoded' or 'image'."
            )

    if "chatbot" not in kwargs:
        from gradio.components import Chatbot

        kwargs["chatbot"] = Chatbot(type="messages", scale=1, allow_tags=True)

    return ChatInterface(
        open_api_stream if streaming else open_api,
        type="messages",
        multimodal=bool(file_types),
        textbox=gr.MultimodalTextbox(file_types=supported_extensions)
        if file_types
        else None,
        **kwargs,
    )

    Load a chat interface from an OpenAI API chat compatible endpoint.
    Parameters:
        base_url: The base URL of the endpoint, e.g. "http://localhost:11434/v1/"
        model: The name of the model you are loading, e.g. "llama3.2"
        token: The API token or a placeholder string if you are using a local model, e.g. "ollama"
        file_types: The file types allowed to be uploaded by the user. "text_encoded" allows uploading any text-encoded file (which is simply appended to the prompt), and "image" adds image upload support. Set to None to disable file uploads.
        system_message: The system message to use for the conversation, if any.
        streaming: Whether the response should be streamed.
        kwargs: Additional keyword arguments to pass into ChatInterface for customization.
    Example:
        import gradio as gr
        gr.load_chat(
            "http://localhost:11434/v1/",
            model="qwen2.5",
            token="***",
            file_types=["text_encoded", "image"],
            system_message="You are a silly assistant.",
        ).launch()
    
gradio/external.py