I've tried several prompts to understand how does it "know" that it's an AI, but it's answers are inconclusive for me. It says that it was hard-coded to recognize keywords in this regard, which might mean that OpenAI might have trained it intensively on text about itself, but how exactly? my question remains
Unlike GPT-2/3 (and other language models around, such as OPT, PaLM, and BLOOM), it is trained not only on the texts downloaded from the Internet. After pre-training on purely textual data (OpenAI calls the base model GPT-3.5, and it is not entirely clear how it differs from GPT-3), it trained using human feedback. People involved in the training were instructed on the desired behavior of such a model. The instructions probably include reminding the users that ChatGPT is an AI. (There were in fact two types of human feedback: one was writing the desired answer, and the other was ranking or already generated model outputs.)
Another way is adding this information to the initial prompt, which might not be visible to the user. It might be something like: "I am an AI assistant called ChatGPT; I am polite and always helpful." Language models try to generate text that is coherent with what was previously written.