A comprehensive Jupyter notebook exploring various prompt engineering techniques and strategies for working with Large Language Models (LLMs). This notebook demonstrates core prompting patterns using OpenAI's GPT models.
This notebook serves as both a tutorial and a practical guide to prompt engineering, covering techniques from basic zero-shot prompting to advanced methods like Tree-of-Thought and Graph-of-Thought reasoning. Each technique includes explanations, examples, and working code demonstrations (for most).
- Zero-Shot Prompting - Direct instructions without examples
- One-Shot Prompting - Single example demonstration
- Few-Shot Prompting - Multiple examples for pattern recognition
- Chain-of-Thought (CoT) - Step-by-step reasoning
- Meta Prompting - Structured templates and formats
- Generated Knowledge Prompting - Generating background knowledge first
- Self-Consistency Prompting - Multiple reasoning paths with consensus
- Prompt Chaining - Sequential prompts with output feeding
- Directional Stimulus Prompting - Hints and cues for guidance
- Tree-of-Thought (ToT) - Branching reasoning paths
- ReAct Prompting - Reasoning + Acting loops
- Reflection Prompting - Learning from feedback
- Graph-of-Thought (GoT) - Graph-structured reasoning
- Python 3.10 or higher
- OpenAI API key (Get one here)
- Jupyter Notebook or JupyterLab
-
Clone or download this repository
-
Create a virtual environment (recommended):
python3 -m venv .venv source .venv/bin/activate # On Windows: .venv\Scripts\activate
-
Install required packages:
pip install langchain-openai openai pandas numpy ipython jupyter
-
Set up your API key:
Option A: Environment Variable (Recommended)
export OPENAI_API_KEY="sk-your-api-key-here" # On Windows: set OPENAI_API_KEY=sk-your-api-key-here
Option B: Using a Python module
- Create a
chatgpt_key.pyfile in your parent directory with:OPENAI_API_KEY = "sk-your-api-key-here"
- Update the
sys.path.append()line in the notebook to point to the directory containingchatgpt_key.py
- Create a
-
Launch Jupyter:
jupyter notebook
-
Open
Prompt Engineering.ipynb -
Update the path in the setup cell if using Option B for API key:
sys.path.append("/path/to/your/parent/directory")
-
Run cells sequentially to follow along with the examples
The notebook uses gpt-5.2 by default. To use a different model, update the model name in the initialization cell:
openai_chat_client = ChatOpenAI(
model="gpt-4", # or "gpt-3.5-turbo", "gpt-4-turbo", etc.
api_key=OPENAI_API_KEY,
temperature=0
)The notebook uses temperature=0 for deterministic outputs. Adjust this value (0.0 to 2.0) to control randomness:
0- Most deterministic, consistent outputs0.7- Balanced creativity and consistency1.0+- More creative and varied outputs
- API Costs: Running all cells will make multiple API calls. Monitor your usage on the OpenAI dashboard.
- Sequential Execution: Some cells depend on previous cells. Run them in order.
- Customization: Feel free to modify prompts, add your own examples, or experiment with different models.
- Path Configuration: Update the
sys.path.append()line to match your directory structure.
- Verify your API key is set correctly
- Check that the key has sufficient credits
- Ensure the key has access to the model you're using
- Verify the model name is correct (e.g.,
gpt-4,gpt-3.5-turbo) - Check OpenAI's model availability page
Feel free to:
- Add new prompting techniques
- Improve existing examples
- Fix bugs or typos
- Enhance documentation