You could add restrictive licensing conditions, but realistically that doesn't stop people from using your code anyways. Unless you hold the exclusive copy of your source code, it's hard to imagine a good way to prevent AI from using it.
Yeah that doesn’t really work I.e. GitHub lawsuits
It doesn't really work because anything public can be assimilated into AI. If you don't want your content to get used for AI training, don't make it freely available on the internet.