def maybe_extract_from_json(data: dict, key: str = "text") -> Union[str, None]:
    """
    Extract text from a JSON response.

    Args:
        data (dict): The JSON response.

    Returns:
        Union[str, None]: The extracted text or None.

    """
    if "choices" in data:
        if len(data["choices"]) > 0 and key in data["choices"][0]:
            return data["choices"][0][key]
        else:
            return None
    else:
        return None

    Extract text from a JSON response.

    Args:
        data (dict): The JSON response.

    Returns:
        Union[str, None]: The extracted text or None.

    
llama-index-integrations/llms/llama-index-llms-deepinfra/llama_index/llms/deepinfra/utils.py