def test_env():
    """
    **Overview:**
        In this function, we test the language model environment and interact with it by typing prompts in the command line.
    """
    # Load the pretrained model and tokenizer.
    # When first call this function, the pretrained files will be automatically downloaded from <link https://huggingface.co/ link>.
    tokenizer = GPT2Tokenizer.from_pretrained('gpt2')
    tokenizer.pad_token = tokenizer.eos_token
    model = GPT2LMHeadModel.from_pretrained('gpt2')
    # For simplicity, we set the reward function to be the negative perplexity.
    reward_function = lambda lm, query, response: - calculate_perplexity(lm, query, response)
    # Arguments for text generation.
    generation_kwargs = {
        # The maximum number of tokens can be generated by language model is 20.
        'max_new_tokens': 20,
        # Use nondeterministic method to sample generated results each time.
        'do_sample': True,
        # The temperature of softmax function for sampling.
        'temperature': 0.7,
        # Penalize the language model to generate repeated words.
        'repetition_penalty': 2.0
    }
    # Initialize the environment.
    env = TextEnvironment(model=model, tokenizer=tokenizer, max_turns=3, reward_fn=reward_function,
                          generation_kwargs=generation_kwargs)
    env.reset()

    # Interaction loop.
    while True:
        # User input the question.
        q = input("Please type in your question:")
        # The env step once.
        obs, reward, done, info = env.step(q)
        # Print the response and reward.
        print("Response (Reward={:.2f}):{}".format(reward, env.history.last_text_segment))
        # If the environment is done, break the interaction loop.
        if done:
            break

    **Overview:**
        In this function, we test the language model environment and interact with it by typing prompts in the command line.
    
chapter8_large/lm_env.py