Skip to main content

Problem Statement

  1. Use GPT-2 to solve a simple mathematical problem through text generation.
  2. Apply few-shot prompting and step-by-step reasoning by providing solved examples before asking the model to solve a new problem.

1. Import Pipeline

Imports the pipeline function from Hugging Face Transformers.

2. Load GPT-2

Loads the GPT-2 model for text generation.

3. Few-Shot Prompt

The prompt contains two solved examples and one new math problem. This demonstrates:
  • Few-shot prompting: The model is given examples.
  • Step-by-step reasoning style: The examples show how the problem should be solved.

4. Generate Response

  • prompt: Input given to GPT-2.
  • max_new_tokens=100: Generates up to 100 new tokens.
  • do_sample=False: Makes generation more deterministic.
  • pad_token_id=50256: Uses GPT-2’s end-of-text token for padding.

5. Print Output

Prints the original prompt along with the text generated by GPT-2.

Flow