main 44559ca5f700 cached
27 files
510.6 KB
125.4k tokens
1 requests
Download .txt
Showing preview only (528K chars total). Download the full file or copy to clipboard to get everything.
Repository: NirDiamant/Prompt_Engineering
Branch: main
Commit: 44559ca5f700
Files: 27
Total size: 510.6 KB

Directory structure:
gitextract_qdq7ojn1/

├── .gitignore
├── CONTRIBUTING.md
├── LICENSE
├── README.md
├── all_prompt_engineering_techniques/
│   ├── ambiguity-clarity.ipynb
│   ├── basic-prompt-structures.ipynb
│   ├── constrained-guided-generation.ipynb
│   ├── cot-prompting.ipynb
│   ├── ethical-prompt-engineering.ipynb
│   ├── evaluating-prompt-effectiveness.ipynb
│   ├── few-shot-learning.ipynb
│   ├── instruction-engineering-notebook.ipynb
│   ├── intro-prompt-engineering-lesson.ipynb
│   ├── multilingual-prompting.ipynb
│   ├── negative-prompting.ipynb
│   ├── prompt-chaining-sequencing.ipynb
│   ├── prompt-formatting-structure.ipynb
│   ├── prompt-length-complexity-management.ipynb
│   ├── prompt-optimization-techniques.ipynb
│   ├── prompt-security-and-safety.ipynb
│   ├── prompt-templates-variables-jinja2.ipynb
│   ├── role-prompting.ipynb
│   ├── self-consistency.ipynb
│   ├── specific-task-prompts.ipynb
│   ├── task-decomposition-prompts.ipynb
│   └── zero-shot-prompting.ipynb
└── requirements.txt

================================================
FILE CONTENTS
================================================

================================================
FILE: .gitignore
================================================
# Byte-compiled / optimized / DLL files
__pycache__/
*.py[cod]
*$py.class

# C extensions
*.so

# Distribution / packaging
.Python
build/
develop-eggs/
dist/
downloads/
eggs/
.eggs/
lib/
lib64/
parts/
sdist/
var/
wheels/
share/python-wheels/
*.egg-info/
.installed.cfg
*.egg
MANIFEST

# PyInstaller
#  Usually these files are written by a python script from a template
#  before PyInstaller builds the exe, so as to inject date/other infos into it.
*.manifest
*.spec

# Installer logs
pip-log.txt
pip-delete-this-directory.txt

# Unit test / coverage reports
htmlcov/
.tox/
.nox/
.coverage
.coverage.*
.cache
nosetests.xml
coverage.xml
*.cover
*.py,cover
.hypothesis/
.pytest_cache/
cover/

# Translations
*.mo
*.pot

# Django stuff:
*.log
local_settings.py
db.sqlite3
db.sqlite3-journal

# Flask stuff:
instance/
.webassets-cache

# Scrapy stuff:
.scrapy

# Sphinx documentation
docs/_build/

# PyBuilder
.pybuilder/
target/

# Jupyter Notebook
.ipynb_checkpoints

# IPython
profile_default/
ipython_config.py

# pyenv
#   For a library or package, you might want to ignore these files since the code is
#   intended to run in multiple environments; otherwise, check them in:
# .python-version

# pipenv
#   According to pypa/pipenv#598, it is recommended to include Pipfile.lock in version control.
#   However, in case of collaboration, if having platform-specific dependencies or dependencies
#   having no cross-platform support, pipenv may install dependencies that don't work, or not
#   install all needed dependencies.
#Pipfile.lock

# poetry
#   Similar to Pipfile.lock, it is generally recommended to include poetry.lock in version control.
#   This is especially recommended for binary packages to ensure reproducibility, and is more
#   commonly ignored for libraries.
#   https://python-poetry.org/docs/basic-usage/#commit-your-poetrylock-file-to-version-control
#poetry.lock

# pdm
#   Similar to Pipfile.lock, it is generally recommended to include pdm.lock in version control.
#pdm.lock
#   pdm stores project-wide configurations in .pdm.toml, but it is recommended to not include it
#   in version control.
#   https://pdm.fming.dev/latest/usage/project/#working-with-version-control
.pdm.toml
.pdm-python
.pdm-build/

# PEP 582; used by e.g. github.com/David-OConnor/pyflow and github.com/pdm-project/pdm
__pypackages__/

# Celery stuff
celerybeat-schedule
celerybeat.pid

# SageMath parsed files
*.sage.py

# Environments
.env
.venv
env/
venv/
ENV/
env.bak/
venv.bak/

# Spyder project settings
.spyderproject
.spyproject

# Rope project settings
.ropeproject

# mkdocs documentation
/site

# mypy
.mypy_cache/
.dmypy.json
dmypy.json

# Pyre type checker
.pyre/

# pytype static type analyzer
.pytype/

# Cython debug symbols
cython_debug/

# PyCharm
#  JetBrains specific template is maintained in a separate JetBrains.gitignore that can
#  be found at https://github.com/github/gitignore/blob/main/Global/JetBrains.gitignore
#  and can be added to the global gitignore or merged into this file.  For a more nuclear
#  option (not recommended) you can uncomment the following to ignore the entire idea folder.
#.idea/

*.yml


================================================
FILE: CONTRIBUTING.md
================================================
# Contributing to Prompt Engineering Repository

Welcome to the world's most comprehensive repository of Prompt Engineering techniques and implementations! 🌟 We're thrilled you're interested in contributing to this dynamic knowledge base. Your expertise and creativity can help us push the boundaries of prompt engineering technology.

## Join Our Community

We have a vibrant Discord community where contributors can discuss ideas, ask questions, and collaborate on GenAI topics. Join us at:

[DiamantAI Discord Server](https://discord.gg/cA6Aa4uyDX)

Don't hesitate to introduce yourself and share your thoughts!

## Ways to Contribute

We welcome contributions of all kinds! Here are some ways you can help:

1. **Add New Prompt Engineering Techniques:** Create new notebooks showcasing novel prompt engineering implementations.
2. **Improve Existing Notebooks:** Enhance, update, or expand our current tutorials.
3. **Fix Bugs:** Help us squash bugs in existing code or explanations.
4. **Enhance Documentation:** Improve clarity, add examples, or fix typos in our docs.
5. **Share Creative Ideas:** Have an innovative idea for a new prompt engineering technique? We're all ears!
6. **Engage in Discussions:** Participate in our Discord community to help shape the future of Gen AI.

Remember, no contribution is too small. Every improvement helps make this repository an even better resource for the community.

## Reporting Issues

Found a problem or have a suggestion? Please create an issue on GitHub, providing as much detail as possible. You can also discuss issues in our Discord community.

## Contributing Code or Content

1. **Fork and Branch:** Fork the repository and create your branch from `main`.
2. **Make Your Changes:** Implement your contribution, following our best practices.
3. **Test:** Ensure your changes work as expected.
4. **Follow the Style:** Adhere to the coding and documentation conventions used throughout the project.
5. **Commit:** Make your git commits informative and concise.
6. **Stay Updated:** The main branch is frequently updated. Before opening a pull request, make sure your code is up-to-date with the current main branch and has no conflicts.
7. **Push and Pull Request:** Push to your fork and submit a pull request.
8. **Discuss:** Use the Discord community to discuss your contribution if you need feedback or have questions.

## Adding a New Prompt Engineering Technique

When adding a new prompt engineering technique to the repository, please follow these additional steps:

1. Create your notebook in the `all_prompt_engineering_techniques` folder.
2. Update the README.md file:
   - Add your new technique to the list of implementations in the README.
   - Place it in the appropriate category based on complexity and type.
   - Use the following format for the link:
     ```
     ### [Number]. [Your Technique Name 🏷️](https://github.com/NirDiamant/Prompt_Engineering/blob/main/all_prompt_engineering_techniques/your_file_name.ipynb)
     ```
   - Replace `[Number]` with the appropriate number, `[Your Technique Name]` with your technique's name, and `your_file_name.ipynb` with the actual name of your notebook file.
   - Choose an appropriate emoji that represents your technique.
   - After inserting your new technique, make sure to update the numbers of all subsequent techniques to maintain the correct order.

For example:
```
1. [Intro-prompt-engineering-lesson 📝](hhttps://github.com/NirDiamant/Prompt_Engineering/blob/main/all_prompt_engineering_techniques/basic_prompt_construction.ipynb)
2. [Your New Technique 🆕](https://github.com/NirDiamant/Prompt_Engineering/blob/main/all_prompt_engineering_techniques/your_new_technique.ipynb)
3. [Next Technique 🔜](https://github.com/NirDiamant/Prompt_Engineering/blob/main/all_prompt_engineering_techniques/next_technique.ipynb)
```

Remember to increment the numbers of all techniques that come after your newly inserted implementation.

## Notebook Structure

For new notebooks or significant additions to existing ones, please follow this structure:

1. **Title and Overview:** Clear title and brief overview of the prompt engineering technique.

2. **Detailed Explanation:** Cover motivation, key components, technique architecture, and conclusion.

3. **Implementation:** Step-by-step implementation with clear comments and explanations.

4. **Usage Example:** Demonstrate the technique with a practical example.

5. **Additional Considerations:** Discuss limitations, potential improvements, or specific use cases.

6. **References:** Include relevant citations or resources if you have.

## Notebook Best Practices

To ensure consistency and readability across all notebooks:

1. **Code Cell Descriptions:** Each code cell should be preceded by a markdown cell with a clear, concise title describing the cell's content or purpose.

2. **Consistent Formatting:** Maintain consistent formatting throughout the notebook, including regular use of markdown headers, code comments, and proper indentation.

## Code Quality and Readability

To ensure the highest quality and readability of our code:

1. **Write Clean Code:** Follow best practices for clean, readable code.
2. **Use Comments:** Add clear and concise comments to explain complex logic.
3. **Format Your Code:** Use consistent formatting throughout your contribution.
4. **Language Model Review:** After completing your code, consider passing it through a language model for additional formatting and readability improvements. This extra step can help make your code even more accessible and maintainable.

## Documentation

Clear documentation is crucial. Whether you're improving existing docs or adding new ones, follow the same process: fork, change, test, and submit a pull request.

## Final Notes

We're grateful for all our contributors and excited to see how you'll help expand the world's most comprehensive prompt engineering resource. Don't hesitate to ask questions in our Discord community if you're unsure about anything.

Let's harness our collective knowledge and creativity to push the boundaries of prompt engineering technology together!

Happy contributing! 🚀

================================================
FILE: LICENSE
================================================
Custom License Agreement

This License Agreement ("Agreement") is a legal agreement between Nir Diamant ("Licensor") and any individual or entity ("Licensee" or "Contributor") who accesses, uses, or contributes to this repository. By accessing, using, or contributing to the Repository, you agree to be bound by the terms of this Agreement.

1. Grant of License for Non-Commercial Use

1.1 Non-Commercial Use License: The Licensor grants the Licensee a worldwide, royalty-free, non-exclusive, non-transferable license to use, reproduce, modify, and distribute the content of the Repository ("Licensed Material") for non-commercial purposes only, subject to the terms and conditions of this Agreement.

1.2 Attribution Requirement: When using or distributing the Licensed Material, the Licensee must provide appropriate credit to the Licensor by:
    - Citing the Licensor's name as specified.
    - Including a link to the Repository.
    - Indicating if changes were made to the Licensed Material.

1.3 No Commercial Use: Licensees are expressly prohibited from using the Licensed Material, in whole or in part, for any commercial purpose without prior written permission from the Licensor.

2. Reservation of Commercial Rights

2.1 Exclusive Commercial Rights: All commercial rights to the Licensed Material are exclusively reserved by the Licensor. The Licensor retains the sole right to use, reproduce, modify, distribute, and sublicense the Licensed Material for commercial purposes.

2.2 Requesting Commercial Permission: Parties interested in using the Licensed Material for commercial purposes must obtain explicit written consent from the Licensor. Requests should be directed to the contact information provided at the end of this Agreement.

3. Contributions

3.1 Contributor License Grant: By submitting any content ("Contribution") to the Repository, the Contributor grants the Licensor an exclusive, perpetual, irrevocable, worldwide, royalty-free license to use, reproduce, modify, distribute, sublicense, and create derivative works from the Contribution for any purpose, including commercial purposes.

3.2 Non-Commercial Use by Contributor: Contributors retain the right to use their own Contributions for non-commercial purposes under the same terms as this Agreement.

3.3 Warranty of Originality: Contributors represent and warrant that their Contributions are original works and do not infringe upon the intellectual property rights of any third party.

3.4 No Commercial Rights for Contributors: Contributors acknowledge that they have no rights to use the Licensed Material or their Contributions for commercial purposes.

4. Restrictions

4.1 Prohibition of Commercial Exploitation: Licensees and Contributors may not:
    - Use the Licensed Material or any Contributions for commercial purposes.
    - Distribute the Licensed Material or any Contributions as part of any commercial product or service.
    - Sublicense the Licensed Material or any Contributions for commercial use.

4.2 No Endorsement: Licensees and Contributors may not imply endorsement or affiliation with the Licensor without explicit written permission.

5. Term and Termination

5.1 Term: This Agreement is effective upon acceptance and continues unless terminated as provided herein.

5.2 Termination for Breach: The Licensor may terminate this Agreement immediately if the Licensee or Contributor breaches any of its terms.

5.3 Effect of Termination: Upon termination, all rights granted under this Agreement cease, and the Licensee or Contributor must destroy all copies of the Licensed Material in their possession.

5.4 Survival: Sections 2, 3, 4, 6, and 7 survive termination of this Agreement.

6. Disclaimer of Warranties and Limitation of Liability

6.1 As-Is Basis: The Licensed Material and any Contributions are provided "AS IS," without warranties or conditions of any kind, either express or implied.

6.2 Disclaimer: The Licensor expressly disclaims all warranties, including but not limited to warranties of title, non-infringement, merchantability, and fitness for a particular purpose.

6.3 Limitation of Liability: In no event shall the Licensor be liable for any direct, indirect, incidental, special, exemplary, or consequential damages arising in any way out of the use of the Licensed Material or Contributions.

7. General Provisions

7.1 Entire Agreement: This Agreement constitutes the entire agreement between the parties concerning the subject matter hereof and supersedes all prior agreements and understandings.

7.2 Modification: The Licensor reserves the right to modify this Agreement at any time. Continued use of the Repository constitutes acceptance of the modified terms.

7.3 Severability: If any provision of this Agreement is found to be unenforceable, the remainder shall remain in full force and effect.

7.4 Waiver: Failure to enforce any provision of this Agreement shall not constitute a waiver of such provision.

7.5 Governing Law: This Agreement shall be governed by and construed in accordance with the laws of [Your Jurisdiction], without regard to its conflict of law principles.

7.6 Dispute Resolution: Any disputes arising under or in connection with this Agreement shall be subject to the exclusive jurisdiction of the courts located in [Your Jurisdiction].

8. Acceptance

By accessing, using, or contributing to the Repository, you acknowledge that you have read, understood, and agree to be bound by the terms and conditions of this Agreement.

Contact Information

For any questions or requests regarding this Agreement, please contact:

Name: Nir Diamant
Email: nirdiamant21@gmail.com

================================================
FILE: README.md
================================================
[![PRs Welcome](https://img.shields.io/badge/PRs-welcome-brightgreen.svg?style=flat-square)](http://makeapullrequest.com)
[![LinkedIn](https://img.shields.io/badge/LinkedIn-Connect-blue)](https://www.linkedin.com/in/nir-diamant-759323134/)
[![Twitter](https://img.shields.io/twitter/follow/NirDiamantAI?label=Follow%20@NirDiamantAI&style=social)](https://twitter.com/NirDiamantAI)
[![Discord](https://img.shields.io/badge/Discord-Join%20our%20community-7289da?style=flat-square&logo=discord&logoColor=white)](https://discord.gg/cA6Aa4uyDX)


> 🌟 **Support This Project:** Your sponsorship fuels innovation in prompt engineering development.  **[Become a sponsor](https://github.com/sponsors/NirDiamant)** to help maintain and expand this valuable resource!

# Prompt Engineering Techniques: Comprehensive Repository for Development and Implementation 🖋️

Welcome to one of the most extensive and dynamic collections of Prompt Engineering tutorials and implementations available today. This repository serves as a comprehensive resource for learning, building, and sharing prompt engineering techniques, ranging from basic concepts to advanced strategies for leveraging large language models.

## 🎯 Sponsors

<div align="center">

<a href="https://coderabbit.link/nir">
  <picture>
    <source media="(prefers-color-scheme: dark)" srcset="images/coderabbit_Dark_Type_Mark.png">
    <img src="images/coderabbit_Light_Type_Mark_Orange.png" height="60" alt="Coderabbit">
  </picture>
</a>

</div>

## 📫 Stay Updated!

<div align="center">
<table>
<tr>
<td align="center">🚀<br><b>Cutting-edge<br>Updates</b></td>
<td align="center">💡<br><b>Expert<br>Insights</b></td>
<td align="center">🎯<br><b>Top 0.1%<br>Content</b></td>
</tr>
</table>

[![Subscribe to DiamantAI Newsletter](images/subscribe-button.svg)](https://diamantai.substack.com/?r=336pe4&utm_campaign=pub-share-checklist)

*Join over 50,000 AI enthusiasts getting unique cutting-edge insights and free tutorials!* ***Plus, subscribers get exclusive early access and special discounts to our upcoming RAG Techniques course!***
</div>

[![DiamantAI's newsletter](images/substack_image.png)](https://diamantai.substack.com/?r=336pe4&utm_campaign=pub-share-checklist)

## Introduction

Prompt engineering is at the forefront of artificial intelligence, revolutionizing the way we interact with and leverage AI technologies. This repository is designed to guide you through the development journey, from basic prompt structures to advanced, cutting-edge techniques.

Our goal is to provide a valuable resource for everyone - from beginners taking their first steps in AI to seasoned practitioners pushing the boundaries of what's possible. By offering a range of examples from foundational to complex, we aim to facilitate learning, experimentation, and innovation in the rapidly evolving field of prompt engineering.

Furthermore, this repository serves as a platform for showcasing innovative prompt engineering techniques. Whether you've developed a novel approach or found an innovative application for existing techniques, we encourage you to share your work with the community.

## 📖 Get the Fully Explained Version of This Repo

This repository contains **22 hands-on Jupyter Notebook tutorials** covering **key prompt engineering techniques**.
If you want to go **deeper** with **full explanations, intuitive insights, and structured exercises**, check out the **expanded version in book format**:

📚 **Prompt Engineering from Zero to Hero**
- 📖 **All 22 techniques from this repo**, fully explained in depth
- 🧠 **Step-by-step breakdowns** of key concepts & best practices
- 🏋️ **Hands-on exercises** to sharpen your skills
- 🎯 **Designed for learners who want a structured, guided approach**
- 📄 **Instant access on any device – computer, tablet, or phone**

Available on:
- 📕 **[Amazon Kindle](https://www.amazon.com/dp/B0DZ85RPB5)** — $9.99
- 📗 **[Amazon Paperback](https://www.amazon.com/dp/B0DZ9RVKMJ)** — $24.99
- 📄 **[Gumroad (PDF)](https://nirdiamant.gumroad.com/l/mtxrfk)** — Full PDF version

💡 **Subscribers to the DiamantAI newsletter receive an exclusive 33% (!) discount on the book.**



## Related Projects

🚀 Level up with my **[Agents Towards Production](https://github.com/NirDiamant/agents-towards-production)** repository. It delivers horizontal, code-first tutorials that cover every tool and step in the lifecycle of building production-grade GenAI agents, guiding you from spark to scale with proven patterns and reusable blueprints for real-world launches, making it the smartest place to start if you're serious about shipping agents to production.

📚 Explore my **[comprehensive guide on RAG techniques](https://github.com/NirDiamant/RAG_Techniques)** to learn how to enhance AI systems with external knowledge retrieval, complementing language model capabilities with rich, up-to-date information.

🤖 Dive into my **[GenAI Agents Repository](https://github.com/NirDiamant/GenAI_Agents)** for a wide range of AI agent implementations and tutorials, from simple conversational bots to complex, multi-agent systems for various applications.

## A Community-Driven Knowledge Hub

**This repository grows stronger with your contributions!** Join our vibrant Discord community — the central hub for shaping and advancing this project together 🤝

**[DiamantAI Discord Community](https://discord.gg/cA6Aa4uyDX)**

Whether you're a novice eager to learn or an expert ready to share your knowledge, your insights can shape the future of prompt engineering. Join us to propose ideas, get feedback, and collaborate on innovative implementations. For contribution guidelines, please refer to our **[CONTRIBUTING.md](https://github.com/NirDiamant/Prompt_Engineering/blob/main/CONTRIBUTING.md)** file. Let's advance prompt engineering technology together!

🔗 For discussions on GenAI, or to explore knowledge-sharing opportunities, feel free to **[connect on LinkedIn](https://www.linkedin.com/in/nir-diamant-759323134/)**.


## Key Features

- 🎓 Learn prompt engineering techniques from beginner to advanced levels
- 🧠 Explore a wide range of prompt structures and applications
- 📚 Step-by-step tutorials and comprehensive documentation
- 🛠️ Practical, ready-to-use prompt implementations
- 🌟 Regular updates with the latest advancements in prompt engineering
- 🤝 Share your own prompt engineering creations with the community

## Prompt Engineering Techniques

Explore our extensive list of prompt engineering techniques, ranging from basic to advanced:

| # | Category | Technique | Description |
|---|----------|-----------|-------------|
| 1 | 🎓 **Fundamental Concepts** | [Introduction to Prompt Engineering](https://github.com/NirDiamant/Prompt_Engineering/blob/main/all_prompt_engineering_techniques/intro-prompt-engineering-lesson.ipynb) | Comprehensive introduction to fundamental concepts of prompt engineering |
| 2 | 🎓 **Fundamental Concepts** | [Basic Prompt Structures](https://github.com/NirDiamant/Prompt_Engineering/blob/main/all_prompt_engineering_techniques/basic-prompt-structures.ipynb) | Exploration of single-turn and multi-turn prompt structures |
| 3 | 🎓 **Fundamental Concepts** | [Prompt Templates and Variables](https://github.com/NirDiamant/Prompt_Engineering/blob/main/all_prompt_engineering_techniques/prompt-templates-variables-jinja2.ipynb) | Creating and using prompt templates with variables |
| 4 | 🔧 **Core Techniques** | [Zero-Shot Prompting](https://github.com/NirDiamant/Prompt_Engineering/blob/main/all_prompt_engineering_techniques/zero-shot-prompting.ipynb) | Performing tasks without specific examples |
| 5 | 🔧 **Core Techniques** | [Few-Shot Learning](https://github.com/NirDiamant/Prompt_Engineering/blob/main/all_prompt_engineering_techniques/few-shot-learning.ipynb) | Learning from a small number of examples |
| 6 | 🔧 **Core Techniques** | [Chain of Thought (CoT)](https://github.com/NirDiamant/Prompt_Engineering/blob/main/all_prompt_engineering_techniques/cot-prompting.ipynb) | Step-by-step reasoning processes |
| 7 | 🎯 **Advanced Strategies** | [Self-Consistency](https://github.com/NirDiamant/Prompt_Engineering/blob/main/all_prompt_engineering_techniques/self-consistency.ipynb) | Multiple reasoning paths and result aggregation |
| 8 | 🎯 **Advanced Strategies** | [Constrained Generation](https://github.com/NirDiamant/Prompt_Engineering/blob/main/all_prompt_engineering_techniques/constrained-guided-generation.ipynb) | Setting up output constraints |
| 9 | 🎯 **Advanced Strategies** | [Role Prompting](https://github.com/NirDiamant/Prompt_Engineering/blob/main/all_prompt_engineering_techniques/role-prompting.ipynb) | Assigning specific roles to AI models |
| 10 | 🚀 **Advanced Implementations** | [Task Decomposition](https://github.com/NirDiamant/Prompt_Engineering/blob/main/all_prompt_engineering_techniques/task-decomposition-prompts.ipynb) | Breaking down complex tasks |
| 11 | 🚀 **Advanced Implementations** | [Prompt Chaining](https://github.com/NirDiamant/Prompt_Engineering/blob/main/all_prompt_engineering_techniques/prompt-chaining-sequencing.ipynb) | Connecting multiple prompts |
| 12 | 🚀 **Advanced Implementations** | [Instruction Engineering](https://github.com/NirDiamant/Prompt_Engineering/blob/main/all_prompt_engineering_techniques/instruction-engineering-notebook.ipynb) | Crafting clear instructions |
| 13 | ⚡ **Optimization** | [Prompt Optimization](https://github.com/NirDiamant/Prompt_Engineering/blob/main/all_prompt_engineering_techniques/prompt-optimization-techniques.ipynb) | A/B testing and refinement |
| 14 | ⚡ **Optimization** | [Handling Ambiguity](https://github.com/NirDiamant/Prompt_Engineering/blob/main/all_prompt_engineering_techniques/ambiguity-clarity.ipynb) | Resolving ambiguous prompts |
| 15 | ⚡ **Optimization** | [Length Management](https://github.com/NirDiamant/Prompt_Engineering/blob/main/all_prompt_engineering_techniques/prompt-length-complexity-management.ipynb) | Managing prompt complexity |
| 16 | 🛠️ **Specialized Applications** | [Negative Prompting](https://github.com/NirDiamant/Prompt_Engineering/blob/main/all_prompt_engineering_techniques/negative-prompting.ipynb) | Avoiding undesired outputs |
| 17 | 🛠️ **Specialized Applications** | [Prompt Formatting](https://github.com/NirDiamant/Prompt_Engineering/blob/main/all_prompt_engineering_techniques/prompt-formatting-structure.ipynb) | Various prompt formats |
| 18 | 🛠️ **Specialized Applications** | [Task-Specific Prompts](https://github.com/NirDiamant/Prompt_Engineering/blob/main/all_prompt_engineering_techniques/specific-task-prompts.ipynb) | Prompts for specific tasks |
| 19 | 🌍 **Advanced Applications** | [Multilingual Prompting](https://github.com/NirDiamant/Prompt_Engineering/blob/main/all_prompt_engineering_techniques/multilingual-prompting.ipynb) | Cross-lingual techniques |
| 20 | 🌍 **Advanced Applications** | [Ethical Considerations](https://github.com/NirDiamant/Prompt_Engineering/blob/main/all_prompt_engineering_techniques/ethical-prompt-engineering.ipynb) | Bias avoidance and inclusivity |
| 21 | 🌍 **Advanced Applications** | [Prompt Security](https://github.com/NirDiamant/Prompt_Engineering/blob/main/all_prompt_engineering_techniques/prompt-security-and-safety.ipynb) | Preventing injections |
| 22 | 🌍 **Advanced Applications** | [Effectiveness Evaluation](https://github.com/NirDiamant/Prompt_Engineering/blob/main/all_prompt_engineering_techniques/evaluating-prompt-effectiveness.ipynb) | Evaluating prompt performance |

### 🌱 Fundamental Concepts

1. **[Introduction to Prompt Engineering](https://github.com/NirDiamant/Prompt_Engineering/blob/main/all_prompt_engineering_techniques/intro-prompt-engineering-lesson.ipynb)**
   
   #### Overview 🔎
   A comprehensive introduction to the fundamental concepts of prompt engineering in the context of AI and language models.

   #### Implementation 🛠️
   Combines theoretical explanations with practical demonstrations, covering basic concepts, structured prompts, comparative analysis, and problem-solving applications.

2. **[Basic Prompt Structures](https://github.com/NirDiamant/Prompt_Engineering/blob/main/all_prompt_engineering_techniques/basic-prompt-structures.ipynb)**
   
   #### Overview 🔎
   Explores two fundamental types of prompt structures: single-turn prompts and multi-turn prompts (conversations).

   #### Implementation 🛠️
   Uses OpenAI's GPT model and LangChain to demonstrate single-turn and multi-turn prompts, prompt templates, and conversation chains.

3. **[Prompt Templates and Variables](https://github.com/NirDiamant/Prompt_Engineering/blob/main/all_prompt_engineering_techniques/prompt-templates-variables-jinja2.ipynb)**
   
   #### Overview 🔎
   Introduces creating and using prompt templates with variables, focusing on Python and the Jinja2 templating engine.

   #### Implementation 🛠️
   Covers template creation, variable insertion, conditional content, list processing, and integration with the OpenAI API.

### 🔧 Core Techniques

4. **[Zero-Shot Prompting](https://github.com/NirDiamant/Prompt_Engineering/blob/main/all_prompt_engineering_techniques/zero-shot-prompting.ipynb)**
   
   #### Overview 🔎
   Explores zero-shot prompting, allowing language models to perform tasks without specific examples or prior training.

   #### Implementation 🛠️
   Demonstrates direct task specification, role-based prompting, format specification, and multi-step reasoning using OpenAI and LangChain.

5. **[Few-Shot Learning and In-Context Learning](https://github.com/NirDiamant/Prompt_Engineering/blob/main/all_prompt_engineering_techniques/few-shot-learning.ipynb)**
   
   #### Overview 🔎
   Covers Few-Shot Learning and In-Context Learning techniques using OpenAI's GPT models and the LangChain library.

   #### Implementation 🛠️
   Implements basic and advanced few-shot learning, in-context learning, and best practices for example selection and evaluation.

6. **[Chain of Thought (CoT) Prompting](https://github.com/NirDiamant/Prompt_Engineering/blob/main/all_prompt_engineering_techniques/cot-prompting.ipynb)**
   
   #### Overview 🔎
   Introduces Chain of Thought (CoT) prompting, encouraging AI models to break down complex problems into step-by-step reasoning processes.

   #### Implementation 🛠️
   Covers basic and advanced CoT techniques, applying them to various problem-solving scenarios and comparing results with standard prompts.

### 🔍 Advanced Strategies

7. **[Self-Consistency and Multiple Paths of Reasoning](https://github.com/NirDiamant/Prompt_Engineering/blob/main/all_prompt_engineering_techniques/self-consistency.ipynb)**
   
   #### Overview 🔎
   Explores techniques for generating diverse reasoning paths and aggregating results to improve AI-generated answers.

   #### Implementation 🛠️
   Demonstrates designing diverse reasoning prompts, generating multiple responses, implementing aggregation methods, and applying self-consistency checks.

8. **[Constrained and Guided Generation](https://github.com/NirDiamant/Prompt_Engineering/blob/main/all_prompt_engineering_techniques/constrained-guided-generation.ipynb)**
   
   #### Overview 🔎
   Focuses on techniques to set up constraints for model outputs and implement rule-based generation.

   #### Implementation 🛠️
   Uses LangChain's PromptTemplate for structured prompts, implements constraints, and explores rule-based generation techniques.

9. **[Role Prompting](https://github.com/NirDiamant/Prompt_Engineering/blob/main/all_prompt_engineering_techniques/role-prompting.ipynb)**
   
   #### Overview 🔎
   Explores assigning specific roles to AI models and crafting effective role descriptions.

   #### Implementation 🛠️
   Demonstrates creating role-based prompts, assigning roles to AI models, and refining role descriptions for various scenarios.

### 🚀 Advanced Implementations

10. **[Task Decomposition in Prompts](https://github.com/NirDiamant/Prompt_Engineering/blob/main/all_prompt_engineering_techniques/task-decomposition-prompts.ipynb)**
    
    #### Overview 🔎
    Explores techniques for breaking down complex tasks and chaining subtasks in prompts.

    #### Implementation 🛠️
    Covers problem analysis, subtask definition, targeted prompt engineering, sequential execution, and result synthesis.

11. **[Prompt Chaining and Sequencing](https://github.com/NirDiamant/Prompt_Engineering/blob/main/all_prompt_engineering_techniques/prompt-chaining-sequencing.ipynb)**
    
    #### Overview 🔎
    Demonstrates how to connect multiple prompts and build logical flows for complex AI-driven tasks.

    #### Implementation 🛠️
    Explores basic prompt chaining, sequential prompting, dynamic prompt generation, and error handling within prompt chains.

12. **[Instruction Engineering](https://github.com/NirDiamant/Prompt_Engineering/blob/main/all_prompt_engineering_techniques/instruction-engineering-notebook.ipynb)**
    
    #### Overview 🔎
    Focuses on crafting clear and effective instructions for language models, balancing specificity and generality.

    #### Implementation 🛠️
    Covers creating and refining instructions, experimenting with different structures, and implementing iterative improvement based on model responses.

### 🎨 Optimization and Refinement

13. **[Prompt Optimization Techniques](https://github.com/NirDiamant/Prompt_Engineering/blob/main/all_prompt_engineering_techniques/prompt-optimization-techniques.ipynb)**
    
    #### Overview 🔎
    Explores advanced techniques for optimizing prompts, focusing on A/B testing and iterative refinement.

    #### Implementation 🛠️
    Demonstrates A/B testing of prompts, iterative refinement processes, and performance evaluation using relevant metrics.

14. **[Handling Ambiguity and Improving Clarity](https://github.com/NirDiamant/Prompt_Engineering/blob/main/all_prompt_engineering_techniques/ambiguity-clarity.ipynb)**
    
    #### Overview 🔎
    Focuses on identifying and resolving ambiguous prompts and techniques for writing clearer prompts.

    #### Implementation 🛠️
    Covers analyzing ambiguous prompts, implementing strategies to resolve ambiguity, and exploring techniques for writing clearer prompts.

15. **[Prompt Length and Complexity Management](https://github.com/NirDiamant/Prompt_Engineering/blob/main/all_prompt_engineering_techniques/prompt-length-complexity-management.ipynb)**
    
    #### Overview 🔎
    Explores techniques for managing prompt length and complexity when working with large language models.

    #### Implementation 🛠️
    Demonstrates techniques for balancing detail and conciseness, and strategies for handling long contexts including chunking, summarization, and iterative processing.

### 🛠️ Specialized Applications

16. **[Negative Prompting and Avoiding Undesired Outputs](https://github.com/NirDiamant/Prompt_Engineering/blob/main/all_prompt_engineering_techniques/negative-prompting.ipynb)**
    
    #### Overview 🔎
    Explores negative prompting and techniques for avoiding undesired outputs from large language models.

    #### Implementation 🛠️
    Covers basic negative examples, explicit exclusions, constraint implementation using LangChain, and methods for evaluating and refining negative prompts.

17. **[Prompt Formatting and Structure](https://github.com/NirDiamant/Prompt_Engineering/blob/main/all_prompt_engineering_techniques/prompt-formatting-structure.ipynb)**
    
    #### Overview 🔎
    Explores various prompt formats and structural elements, demonstrating their impact on AI model responses.

    #### Implementation 🛠️
    Demonstrates creating various prompt formats, incorporating structural elements, and comparing responses from different prompt structures.

18. **[Prompts for Specific Tasks](https://github.com/NirDiamant/Prompt_Engineering/blob/main/all_prompt_engineering_techniques/specific-task-prompts.ipynb)**
    
    #### Overview 🔎
    Explores the creation and use of prompts for specific tasks: text summarization, question-answering, code generation, and creative writing.

    #### Implementation 🛠️
    Covers designing task-specific prompt templates, implementing them using LangChain, executing with sample inputs, and analyzing outputs for each task type.

### 🌍 Advanced Applications

19. **[Multilingual and Cross-lingual Prompting](https://github.com/NirDiamant/Prompt_Engineering/blob/main/all_prompt_engineering_techniques/multilingual-prompting.ipynb)**
    
    #### Overview 🔎
    Explores techniques for designing prompts that work effectively across multiple languages and for language translation tasks.

    #### Implementation 🛠️
    Covers creating multilingual prompts, implementing language detection and adaptation, designing cross-lingual translation prompts, and handling various writing systems and scripts.

20. **[Ethical Considerations in Prompt Engineering](https://github.com/NirDiamant/Prompt_Engineering/blob/main/all_prompt_engineering_techniques/ethical-prompt-engineering.ipynb)**
    
    #### Overview 🔎
    Explores the ethical dimensions of prompt engineering, focusing on avoiding biases and creating inclusive and fair prompts.

    #### Implementation 🛠️
    Covers identifying biases in prompts, implementing strategies to create inclusive prompts, and methods to evaluate and improve the ethical quality of AI outputs.

21. **[Prompt Security and Safety](https://github.com/NirDiamant/Prompt_Engineering/blob/main/all_prompt_engineering_techniques/prompt-security-and-safety.ipynb)**
    
    #### Overview 🔎
    Focuses on preventing prompt injections and implementing content filters in prompts for safe and secure AI applications.

    #### Implementation 🛠️
    Covers techniques for prompt injection prevention, content filtering implementation, and testing the effectiveness of security and safety measures.

22. **[Evaluating Prompt Effectiveness](https://github.com/NirDiamant/Prompt_Engineering/blob/main/all_prompt_engineering_techniques/evaluating-prompt-effectiveness.ipynb)**
    
    #### Overview 🔎
    Explores methods and techniques for evaluating the effectiveness of prompts in AI language models.

    #### Implementation 🛠️
    Covers setting up evaluation metrics, implementing manual and automated evaluation techniques, and providing practical examples using OpenAI and LangChain.

## Getting Started

To begin exploring and implementing prompt engineering techniques:

1. Clone this repository:
   ```
   git clone https://github.com/NirDiamant/Prompt_Engineering.git
   ```
2. Navigate to the technique you're interested in:
   ```
   cd all_prompt_engineering_techniques
   ```
3. Follow the detailed implementation guide in each technique's notebook.

## Contributing

We welcome contributions from the community! If you have a new technique or improvement to suggest:

1. Fork the repository
2. Create your feature branch: `git checkout -b feature/AmazingFeature`
3. Commit your changes: `git commit -m 'Add some AmazingFeature'`
4. Push to the branch: `git push origin feature/AmazingFeature`
5. Open a pull request

## License

This project is licensed under a custom non-commercial license - see the [LICENSE](LICENSE) file for details.

---

⭐️ If you find this repository helpful, please consider giving it a star!

Keywords: Prompt Engineering, AI, Machine Learning, Natural Language Processing, LLM, Language Models, NLP, Conversational AI, Zero-Shot Learning, Few-Shot Learning, Chain of Thought



================================================
FILE: all_prompt_engineering_techniques/ambiguity-clarity.ipynb
================================================
{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# Handling Ambiguity and Improving Clarity in Prompt Engineering\n",
    "\n",
    "## Overview\n",
    "\n",
    "This tutorial focuses on two critical aspects of prompt engineering: identifying and resolving ambiguous prompts, and techniques for writing clearer prompts. These skills are essential for effective communication with AI models and obtaining more accurate and relevant responses.\n",
    "\n",
    "## Motivation\n",
    "\n",
    "Ambiguity in prompts can lead to inconsistent or irrelevant AI responses, while lack of clarity can result in misunderstandings and inaccurate outputs. By mastering these aspects of prompt engineering, you can significantly improve the quality and reliability of AI-generated content across various applications.\n",
    "\n",
    "## Key Components\n",
    "\n",
    "1. Identifying ambiguous prompts\n",
    "2. Strategies for resolving ambiguity\n",
    "3. Techniques for writing clearer prompts\n",
    "4. Practical examples and exercises\n",
    "\n",
    "## Method Details\n",
    "\n",
    "We'll use OpenAI's GPT model and the LangChain library to demonstrate various techniques for handling ambiguity and improving clarity in prompts. The tutorial will cover:\n",
    "\n",
    "1. Setting up the environment and necessary libraries\n",
    "2. Analyzing ambiguous prompts and their potential interpretations\n",
    "3. Implementing strategies to resolve ambiguity, such as providing context and specifying parameters\n",
    "4. Exploring techniques for writing clearer prompts, including using specific language and structured formats\n",
    "5. Practical exercises to apply these concepts in real-world scenarios\n",
    "\n",
    "## Conclusion\n",
    "\n",
    "By the end of this tutorial, you'll have a solid understanding of how to identify and resolve ambiguity in prompts, as well as techniques for crafting clearer prompts. These skills will enable you to communicate more effectively with AI models, resulting in more accurate and relevant outputs across various applications."
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Setup\n",
    "\n",
    "First, let's import the necessary libraries and set up our environment."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [],
   "source": [
    "import os\n",
    "from langchain_openai import ChatOpenAI\n",
    "from langchain.prompts import PromptTemplate\n",
    "\n",
    "# Load environment variables\n",
    "from dotenv import load_dotenv\n",
    "load_dotenv()\n",
    "\n",
    "# Set up OpenAI API key\n",
    "os.environ[\"OPENAI_API_KEY\"] = os.getenv('OPENAI_API_KEY')\n",
    "\n",
    "# Initialize the language model\n",
    "llm = ChatOpenAI(model=\"gpt-4o-mini\")"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Identifying Ambiguous Prompts\n",
    "\n",
    "Let's start by examining some ambiguous prompts and analyzing their potential interpretations."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Prompt: Tell me about the bank.\n",
      "The prompt \"Tell me about the bank.\" is ambiguous for several reasons:\n",
      "\n",
      "1. **Type of Bank**: The term \"bank\" can refer to different types of financial institutions. It could signify a commercial bank, an investment bank, a savings bank, or even a central bank (like the Federal Reserve). Each type has distinct functions, services, and regulatory environments.\n",
      "\n",
      "2. **Context of Inquiry**: The prompt does not specify the context in which the bank is to be discussed. Are we looking for historical information, current services, financial performance, or perhaps regulatory issues? Different contexts would lead to different answers.\n",
      "\n",
      "3. **Location**: The prompt does not indicate whether it refers to a specific bank (e.g., Bank of America, JPMorgan Chase) or banks in general. Without a specified location or institution, the discussion could range from a local bank to international banking practices.\n",
      "\n",
      "4. **Aspects of Interest**: The prompt does not clarify which aspects of the bank the speaker is interested in. It could pertain to its services (loans, mortgages, checking accounts), its role in the economy, its history, recent news, or even customer service experiences.\n",
      "\n",
      "5. **Audience Knowledge**: The prompt does not consider the knowledge level of the audience. A detailed explanation about banking might be appropriate for someone with little understanding of finance, while an overview of current trends might be desired by someone with more expertise.\n",
      "\n",
      "### Possible Interpretations:\n",
      "1. **General Overview**: A request for a general description of what a bank is and its functions in the economy.\n",
      "2. **Specific Bank**: Information about a particular bank (e.g., \"Tell me about Chase Bank\" or \"Tell me about the Bank of England\").\n",
      "3. **Banking Products**: A focus on the types of products and services offered by banks, such as savings accounts, loans, and investment options.\n",
      "4. **Regulatory Issues**: An inquiry into the laws and regulations that govern banking practices.\n",
      "5. **Recent Developments**: An interest in recent news or changes in the banking sector, such as mergers, acquisitions, or technological innovations.\n",
      "6. **Historical Context**: A discussion about the history and evolution of banking as a practice.\n",
      "7. **Personal Experience**: A request for personal anecdotes or experiences related to using a bank.\n",
      "\n",
      "In conclusion, the ambiguity of the prompt arises from its vagueness in terms of context, specificity, and focus, allowing for multiple interpretations that could lead to different discussions about banking.\n",
      "--------------------------------------------------\n",
      "Prompt: What's the best way to get to school?\n",
      "The prompt \"What's the best way to get to school?\" is ambiguous due to several factors that can lead to different interpretations. \n",
      "\n",
      "1. **Mode of Transportation**: The phrase \"best way\" could refer to various modes of transportation, such as walking, biking, driving, taking public transport, or carpooling. Each mode could be considered the \"best\" based on different criteria (e.g., speed, cost, environmental impact, safety).\n",
      "\n",
      "2. **Criteria for \"Best\"**: The term \"best\" is subjective and can vary based on the criteria used. For instance, one might interpret \"best\" as:\n",
      "   - Fastest route\n",
      "   - Cheapest option\n",
      "   - Most environmentally friendly choice\n",
      "   - Safest route (considering traffic, road conditions, etc.)\n",
      "   - Most convenient (e.g., minimal transfers if using public transport)\n",
      "\n",
      "3. **Starting Point**: The prompt does not specify where the individual is starting from. The best route may vary significantly based on the starting location.\n",
      "\n",
      "4. **Destination**: While \"school\" is mentioned, it is unclear which school is being referred to, especially if there are multiple schools in the area or if the individual attends a specific institution with a particular address.\n",
      "\n",
      "5. **Time of Day**: The best route may depend on the time of day due to traffic patterns, public transportation schedules, or safety considerations (e.g., walking alone at night).\n",
      "\n",
      "6. **Personal Preferences**: Different individuals may have unique preferences or requirements that affect their choice of how to get to school (e.g., a preference for exercise, avoiding crowded public transport, etc.).\n",
      "\n",
      "### Possible Interpretations:\n",
      "1. **Mode of Transport**:\n",
      "   - \"What’s the fastest way to get to school by car?\"\n",
      "   - \"What’s the best route for walking to school?\"\n",
      "\n",
      "2. **Criteria**:\n",
      "   - \"What’s the cheapest way to get to school?\"\n",
      "   - \"What’s the safest route to take?\"\n",
      "\n",
      "3. **Starting Point**:\n",
      "   - \"What's the best way to get to school from my house?\"\n",
      "   - \"How do I get to school if I’m coming from downtown?\"\n",
      "\n",
      "4. **Destination**:\n",
      "   - \"What’s the best way to get to Lincoln High School?\"\n",
      "   - \"How do I get to the community college from my location?\"\n",
      "\n",
      "5. **Time of Day**:\n",
      "   - \"What’s the best route to school during rush hour?\"\n",
      "   - \"What time should I leave to avoid traffic?\"\n",
      "\n",
      "6. **Personal Preferences**:\n",
      "   - \"What’s the best way to bike to school?\"\n",
      "   - \"Is there a public transport option that’s less crowded?\"\n",
      "\n",
      "In summary, the ambiguity in the prompt arises from the multiple interpretations of the terms used, the lack of specific context, and the variability based on individual preferences and circumstances.\n",
      "--------------------------------------------------\n",
      "Prompt: Can you explain the theory?\n",
      "The prompt \"Can you explain the theory?\" is ambiguous for several reasons:\n",
      "\n",
      "1. **Lack of Context**: The term \"theory\" is vague without additional context. There are countless theories across various fields, such as science (e.g., the theory of evolution, quantum theory), philosophy (e.g., social contract theory), psychology (e.g., attachment theory), and many others. Without specifying which theory is being referred to, the question could be interpreted in multiple ways.\n",
      "\n",
      "2. **Assumed Knowledge**: The prompt assumes that the respondent knows which theory is being referenced. Depending on the respondent's background, they may not be familiar with the specific theory in question, leading to confusion.\n",
      "\n",
      "3. **Depth of Explanation**: The term \"explain\" is also ambiguous. It could imply a brief summary, a detailed analysis, or a layman's explanation. Different audiences may require different levels of detail, and the respondent may not know how comprehensive their explanation should be.\n",
      "\n",
      "4. **Audience**: The prompt does not specify who the explanation is for. An explanation suitable for a novice may differ significantly from one tailored for an expert audience.\n",
      "\n",
      "Possible interpretations of the prompt include:\n",
      "\n",
      "1. **Specific Theory Request**: The respondent might interpret the question as asking about a specific theory known to both parties, such as \"Can you explain the theory of relativity?\"\n",
      "\n",
      "2. **General Inquiry**: The respondent might consider it a general inquiry into theories in a particular field (e.g., \"Can you explain any psychological theory?\").\n",
      "\n",
      "3. **Field-Specific Request**: The respondent could interpret it as a request related to a specific academic discipline (e.g., \"Can you explain the theory of supply and demand in economics?\").\n",
      "\n",
      "4. **Nature of Explanation**: The respondent might wonder whether to provide a simple definition, a historical overview, or a technical breakdown of the theory.\n",
      "\n",
      "5. **Philosophical vs. Scientific Theory**: The respondent may consider whether the question refers to a scientific theory that is testable and empirical or a philosophical theory that may involve more abstract reasoning.\n",
      "\n",
      "In conclusion, the prompt's ambiguity arises from its lack of specificity regarding the theory in question, the depth of explanation needed, and the intended audience. Clarifying these aspects would help eliminate confusion and facilitate a more productive discussion.\n",
      "--------------------------------------------------\n"
     ]
    }
   ],
   "source": [
    "ambiguous_prompts = [\n",
    "    \"Tell me about the bank.\",\n",
    "    \"What's the best way to get to school?\",\n",
    "    \"Can you explain the theory?\"\n",
    "]\n",
    "\n",
    "for prompt in ambiguous_prompts:\n",
    "    analysis_prompt = f\"Analyze the following prompt for ambiguity: '{prompt}'. Explain why it's ambiguous and list possible interpretations.\"\n",
    "    print(f\"Prompt: {prompt}\")\n",
    "    print(llm.invoke(analysis_prompt).content)\n",
    "    print(\"-\" * 50)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Resolving Ambiguity\n",
    "\n",
    "Now, let's explore strategies for resolving ambiguity in prompts."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Context: You are a financial advisor discussing savings accounts.\n",
      "Clarified response: When discussing savings accounts, it's important to consider the role of the bank in managing these accounts. Here are some key points to understand about banks in this context:\n",
      "\n",
      "1. **Types of Banks**: Banks can be broadly categorized into commercial banks, credit unions, and online banks. Each type offers savings accounts but may have different terms, interest rates, and services.\n",
      "\n",
      "2. **Interest Rates**: Banks typically offer interest on savings accounts, which can vary widely. Online banks often provide higher interest rates compared to traditional brick-and-mortar banks due to lower overhead costs. It’s essential to compare rates when choosing a bank for your savings account.\n",
      "\n",
      "3. **Fees and Minimum Balances**: Some banks charge monthly maintenance fees or require a minimum balance to avoid these fees. It’s crucial to understand the fee structure before selecting a bank, as this can affect your overall savings.\n",
      "\n",
      "4. **FDIC Insurance**: In the United States, deposits in savings accounts at member banks are insured by the Federal Deposit Insurance Corporation (FDIC) up to $250,000 per depositor, per bank. This insurance provides security and peace of mind for your savings.\n",
      "\n",
      "5. **Accessibility and Convenience**: Consider how easy it is to access your funds. Many banks offer mobile banking apps, ATMs, and online account management, making it convenient to manage your savings. \n",
      "\n",
      "6. **Customer Service**: Good customer service can significantly enhance your banking experience. Look for banks that offer support through multiple channels, such as phone, chat, and in-person assistance.\n",
      "\n",
      "7. **Promotions and Offers**: Banks often run promotions for new savings accounts, such as cash bonuses for opening an account or higher introductory interest rates. These can be beneficial, but always read the fine print.\n",
      "\n",
      "8. **Account Features**: Some banks provide additional features like automatic savings plans, budgeting tools, or the ability to link to other accounts for easy transfers. These can help you grow your savings more effectively.\n",
      "\n",
      "When choosing a bank for your savings account, it’s important to evaluate these factors to find the best fit for your financial goals and needs.\n",
      "--------------------------------------------------\n",
      "Context: You are a geographer describing river formations.\n",
      "Clarified response: In the context of river formations, the term \"bank\" refers to the land alongside a river. Banks play a crucial role in shaping the river's flow and ecosystem. There are typically two banks in a river: the left bank and the right bank, determined by the perspective of looking downstream.\n",
      "\n",
      "**Characteristics of River Banks:**\n",
      "\n",
      "1. **Composition:** River banks can be made up of various materials, including soil, sand, silt, gravel, and rocks. The composition can affect erosion rates, sediment deposition, and the types of vegetation that can thrive in the area.\n",
      "\n",
      "2. **Erosion and Deposition:** The dynamic processes of erosion and deposition significantly shape river banks. Erosion occurs when water flow removes material from the bank, often resulting in steep, undercut banks. Conversely, deposition occurs when sediment carried by the river is dropped off, usually at points where the water slows down, leading to the formation of sandbars or point bars.\n",
      "\n",
      "3. **Ecology:** River banks are often rich in biodiversity. The vegetation found along banks, such as reeds, willows, and other riparian plants, provides habitat and food for various wildlife species. These plants also help stabilize the bank, reduce erosion, and improve water quality by filtering pollutants.\n",
      "\n",
      "4. **Human Impact:** Human activities, such as urban development, agriculture, and dam construction, can significantly alter river banks. These activities may lead to increased erosion, reduced habitat quality, and changes in sediment transport, which can affect the overall health of the river ecosystem.\n",
      "\n",
      "5. **Floodplain Interaction:** River banks are often part of a larger floodplain, which is the area adjacent to the river that may be inundated during periods of high flow. The interaction between the river and its banks during flooding can lead to the deposition of nutrient-rich sediments, benefiting the surrounding ecosystem.\n",
      "\n",
      "Understanding the formation and dynamics of river banks is essential for managing and preserving riverine environments, as they are integral to the health of aquatic and terrestrial ecosystems.\n",
      "--------------------------------------------------\n"
     ]
    }
   ],
   "source": [
    "def resolve_ambiguity(prompt, context):\n",
    "    \"\"\"\n",
    "    Resolve ambiguity in a prompt by providing additional context.\n",
    "    \n",
    "    Args:\n",
    "    prompt (str): The original ambiguous prompt\n",
    "    context (str): Additional context to resolve ambiguity\n",
    "    \n",
    "    Returns:\n",
    "    str: The AI's response to the clarified prompt\n",
    "    \"\"\"\n",
    "    clarified_prompt = f\"{context}\\n\\nBased on this context, {prompt}\"\n",
    "    return llm.invoke(clarified_prompt).content\n",
    "\n",
    "# Example usage\n",
    "ambiguous_prompt = \"Tell me about the bank.\"\n",
    "contexts = [\n",
    "    \"You are a financial advisor discussing savings accounts.\",\n",
    "    \"You are a geographer describing river formations.\"\n",
    "]\n",
    "\n",
    "for context in contexts:\n",
    "    print(f\"Context: {context}\")\n",
    "    print(f\"Clarified response: {resolve_ambiguity(ambiguous_prompt, context)}\")\n",
    "    print(\"-\" * 50)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Techniques for Writing Clearer Prompts\n",
    "\n",
    "Let's explore some techniques for writing clearer prompts to improve AI responses."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Original Prompt Response:\n",
      "Could you please clarify what you would like to make? Whether it's a recipe, a DIY project, or something else, I'd be happy to help!\n",
      "\n",
      "Improved Prompt Response:\n",
      "Sure! Here’s a step-by-step guide for making a classic Margherita pizza, which features a simple yet delicious combination of fresh ingredients.\n",
      "\n",
      "### Ingredients:\n",
      "\n",
      "#### For the Dough:\n",
      "- 2 ¼ cups (280g) all-purpose flour (plus extra for dusting)\n",
      "- 1 teaspoon salt\n",
      "- ¾ teaspoon instant yeast\n",
      "- ¾ cup (180ml) warm water (about 100°F/38°C)\n",
      "- 1 teaspoon sugar (optional, to help activate yeast)\n",
      "\n",
      "#### For the Toppings:\n",
      "- 1 cup (240ml) canned San Marzano tomatoes (or any good quality canned tomatoes)\n",
      "- 1 tablespoon olive oil (plus more for drizzling)\n",
      "- Salt to taste\n",
      "- 8 ounces (225g) fresh mozzarella cheese, preferably buffalo mozzarella\n",
      "- Fresh basil leaves\n",
      "- Freshly cracked black pepper (optional)\n",
      "\n",
      "### Equipment:\n",
      "- A mixing bowl\n",
      "- A baking sheet or pizza stone\n",
      "- A rolling pin (optional)\n",
      "- A pizza peel (optional, for transferring to the oven)\n",
      "- An oven (preferably with a pizza stone or steel for best results)\n",
      "\n",
      "### Instructions:\n",
      "\n",
      "#### Step 1: Make the Dough\n",
      "1. **Mix the dry ingredients**: In a mixing bowl, combine the flour, salt, and instant yeast. If you're using sugar, add it here as well.\n",
      "2. **Add water**: Slowly pour in the warm water while stirring the mixture with a spoon or your hand until it begins to come together into a shaggy dough.\n",
      "3. **Knead the dough**: Transfer the dough onto a lightly floured surface and knead for about 8-10 minutes until smooth and elastic. If the dough is too sticky, sprinkle a little more flour as needed.\n",
      "4. **Let it rise**: Form the dough into a ball and place it in a lightly greased bowl. Cover it with a damp cloth or plastic wrap and let it rise in a warm place for about 1-2 hours, or until it has doubled in size.\n",
      "\n",
      "#### Step 2: Prepare the Sauce\n",
      "1. **Blend the tomatoes**: In a bowl, crush the canned tomatoes by hand or use a blender for a smoother consistency. You want it to be a bit chunky for texture.\n",
      "2. **Season**: Add a little salt to taste and a tablespoon of olive oil to the tomato mixture. Mix well and set aside.\n",
      "\n",
      "#### Step 3: Preheat the Oven\n",
      "1. **Preheat your oven**: If using a pizza stone, place it in the oven and preheat to the highest setting (usually around 475°F to 500°F or 245°C to 260°C) for at least 30 minutes. If you don’t have a pizza stone, preheat a baking sheet.\n",
      "\n",
      "#### Step 4: Shape the Pizza\n",
      "1. **Divide the dough**: Once the dough has risen, punch it down and divide it into two equal pieces (for two pizzas). Shape each piece into a ball and let them rest for 10-15 minutes.\n",
      "2. **Shape the pizza**: On a lightly floured surface, take one dough ball and gently stretch it out with your hands or roll it out with a rolling pin into a 10-12 inch round. Make sure the edges are slightly thicker for the crust.\n",
      "\n",
      "#### Step 5: Assemble the Pizza\n",
      "1. **Add the sauce**: Spread a thin layer of the tomato sauce over the surface of the dough, leaving a small border around the edges.\n",
      "2. **Add cheese**: Tear the fresh mozzarella into small pieces and distribute them evenly over the sauce.\n",
      "3. **Add basil**: Tear a few fresh basil leaves and sprinkle them on top (you can also add them after baking for a fresher taste).\n",
      "4. **Drizzle olive oil**: Drizzle a little olive oil over the top for added flavor.\n",
      "\n",
      "#### Step 6: Bake the Pizza\n",
      "1. **Transfer to the oven**: If using a pizza peel, sprinkle it with flour or cornmeal and carefully transfer the assembled pizza onto it. Then slide the pizza onto the preheated stone or baking sheet in the oven.\n",
      "2. **Bake**: Bake for about 8-12 minutes, or until the crust is golden and the cheese is bubbling and starting to brown.\n",
      "3. **Check frequently**: Keep an eye on the pizza to avoid burning, especially if your oven runs hot.\n",
      "\n",
      "#### Step 7: Serve\n",
      "1. **Remove from oven**: Once done, carefully remove the pizza from the oven.\n",
      "2. **Garnish**: Add a few more fresh basil leaves, a drizzle of olive oil, and freshly cracked black pepper if desired.\n",
      "3. **Slice and enjoy**: Let it cool for a minute, slice it up, and enjoy your classic Margherita pizza!\n",
      "\n",
      "### Tips:\n",
      "- For the best flavor, use high-quality ingredients, especially the tomatoes and mozzarella.\n",
      "- If you have time, letting the dough rise slowly in the refrigerator overnight can enhance the flavor and texture.\n",
      "- Experiment with the thickness of the crust to find your preferred style.\n",
      "\n",
      "Enjoy your homemade Margherita pizza!\n"
     ]
    }
   ],
   "source": [
    "def compare_prompt_clarity(original_prompt, improved_prompt):\n",
    "    \"\"\"\n",
    "    Compare the responses to an original prompt and an improved, clearer version.\n",
    "    \n",
    "    Args:\n",
    "    original_prompt (str): The original, potentially unclear prompt\n",
    "    improved_prompt (str): An improved, clearer version of the prompt\n",
    "    \n",
    "    Returns:\n",
    "    tuple: Responses to the original and improved prompts\n",
    "    \"\"\"\n",
    "    original_response = llm.invoke(original_prompt).content\n",
    "    improved_response = llm.invoke(improved_prompt).content\n",
    "    return original_response, improved_response\n",
    "\n",
    "# Example usage\n",
    "original_prompt = \"How do I make it?\"\n",
    "improved_prompt = \"Provide a step-by-step guide for making a classic margherita pizza, including ingredients and cooking instructions.\"\n",
    "\n",
    "original_response, improved_response = compare_prompt_clarity(original_prompt, improved_prompt)\n",
    "\n",
    "print(\"Original Prompt Response:\")\n",
    "print(original_response)\n",
    "print(\"\\nImproved Prompt Response:\")\n",
    "print(improved_response)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Structured Prompts for Clarity\n",
    "\n",
    "Using structured prompts can significantly improve clarity and consistency in AI responses."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "To analyze the impact of social media on society, we can consider the following aspects: communication, mental health, and information dissemination. Each of these areas reveals both positive and negative consequences of social media usage.\n",
      "\n",
      "### 1. Communication\n",
      "\n",
      "**Positive Impact:**  \n",
      "Social media has revolutionized communication by making it easier and faster for people to connect across long distances. Platforms like Facebook, Twitter, and Instagram allow users to share moments, thoughts, and experiences with friends and family, regardless of geographic barriers. This instant connectivity can foster relationships and create a sense of belonging, especially for those who may feel isolated in their physical environments.\n",
      "\n",
      "**Negative Impact:**  \n",
      "Conversely, the nature of communication on social media can lead to misunderstandings and conflicts. The absence of non-verbal cues, such as tone and body language, can result in misinterpretations of messages. Furthermore, the prevalence of online arguments and cyberbullying can create a toxic environment, leading to strained relationships and a decline in face-to-face interactions.\n",
      "\n",
      "### 2. Mental Health\n",
      "\n",
      "**Positive Impact:**  \n",
      "Social media can serve as a supportive platform for individuals dealing with mental health issues. Online communities provide a space for individuals to share experiences and seek support from others facing similar challenges. Many organizations use social media to raise awareness about mental health, promoting resources and encouraging open discussions.\n",
      "\n",
      "**Negative Impact:**  \n",
      "On the flip side, social media can contribute to mental health issues such as anxiety, depression, and low self-esteem. The constant comparison with others' curated lives can lead to feelings of inadequacy. Additionally, the addictive nature of social media can exacerbate feelings of loneliness and isolation, as users may substitute online interactions for genuine social connections.\n",
      "\n",
      "### 3. Information Dissemination\n",
      "\n",
      "**Positive Impact:**  \n",
      "Social media has democratized the flow of information, allowing users to access a wide range of news and perspectives that may not be covered by traditional media outlets. This accessibility can empower individuals to engage in social and political discourse, mobilize for causes, and stay informed about global events in real-time.\n",
      "\n",
      "**Negative Impact:**  \n",
      "However, the rapid spread of information can also lead to the dissemination of misinformation and disinformation. False narratives can easily go viral, leading to public confusion and mistrust in credible sources. The algorithms that govern many social media platforms often prioritize sensational content, which can skew public perception and create echo chambers that reinforce existing biases.\n",
      "\n",
      "### Conclusion\n",
      "\n",
      "In summary, the impact of social media on society is multifaceted, encompassing both beneficial and detrimental effects. While it fosters communication, offers mental health support, and enhances information accessibility, it also presents challenges such as misunderstandings, mental health concerns, and the spread of misinformation. A balanced perspective requires recognizing these complexities and striving for responsible usage of social media to maximize its positive potential while mitigating its adverse effects.\n"
     ]
    }
   ],
   "source": [
    "structured_prompt = PromptTemplate(\n",
    "    input_variables=[\"topic\", \"aspects\", \"tone\"],\n",
    "    template=\"\"\"Provide an analysis of {topic} considering the following aspects:\n",
    "    1. {{aspects[0]}}\n",
    "    2. {{aspects[1]}}\n",
    "    3. {{aspects[2]}}\n",
    "    \n",
    "    Present the analysis in a {tone} tone.\n",
    "    \"\"\"\n",
    ")\n",
    "\n",
    "# Example usage\n",
    "input_variables = {\n",
    "    \"topic\": \"the impact of social media on society\",\n",
    "    \"aspects\": [\"communication patterns\", \"mental health\", \"information spread\"],\n",
    "    \"tone\": \"balanced and objective\"\n",
    "}\n",
    "\n",
    "chain = structured_prompt | llm\n",
    "response = chain.invoke(input_variables).content\n",
    "print(response)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Practical Exercise: Improving Prompt Clarity\n",
    "\n",
    "Now, let's practice improving the clarity of prompts."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Original: What's the difference?\n",
      "Improved: \"What are the differences between these two concepts/objects?\"\n",
      "--------------------------------------------------\n",
      "Original: How does it work?\n",
      "Improved: Can you explain the process or mechanism behind how this system or product functions?\n",
      "--------------------------------------------------\n",
      "Original: Why is it important?\n",
      "Improved: \"What is the significance of this topic, and how does it impact individuals or society?\"\n",
      "--------------------------------------------------\n"
     ]
    }
   ],
   "source": [
    "unclear_prompts = [\n",
    "    \"What's the difference?\",\n",
    "    \"How does it work?\",\n",
    "    \"Why is it important?\"\n",
    "]\n",
    "\n",
    "def improve_prompt_clarity(unclear_prompt):\n",
    "    \"\"\"\n",
    "    Improve the clarity of a given prompt.\n",
    "    \n",
    "    Args:\n",
    "    unclear_prompt (str): The original unclear prompt\n",
    "    \n",
    "    Returns:\n",
    "    str: An improved, clearer version of the prompt\n",
    "    \"\"\"\n",
    "    improvement_prompt = f\"The following prompt is unclear: '{unclear_prompt}'. Please provide a clearer, more specific version of this prompt. output just the improved prompt and nothing else.\" \n",
    "    return llm.invoke(improvement_prompt).content\n",
    "\n",
    "for prompt in unclear_prompts:\n",
    "    improved_prompt = improve_prompt_clarity(prompt)\n",
    "    print(f\"Original: {prompt}\")\n",
    "    print(f\"Improved: {improved_prompt}\")\n",
    "    print(\"-\" * 50)"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": ".venv",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.12.0"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 4
}


================================================
FILE: all_prompt_engineering_techniques/basic-prompt-structures.ipynb
================================================
{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# Basic Prompt Structures Tutorial\n",
    "\n",
    "## Overview\n",
    "\n",
    "This tutorial focuses on two fundamental types of prompt structures:\n",
    "1. Single-turn prompts\n",
    "2. Multi-turn prompts (conversations)\n",
    "\n",
    "We'll use OpenAI's GPT model and LangChain to demonstrate these concepts.\n",
    "\n",
    "## Motivation\n",
    "\n",
    "Understanding different prompt structures is crucial for effective communication with AI models. Single-turn prompts are useful for quick, straightforward queries, while multi-turn prompts enable more complex, context-aware interactions. Mastering these structures allows for more versatile and effective use of AI in various applications.\n",
    "\n",
    "## Key Components\n",
    "\n",
    "1. **Single-turn Prompts**: One-shot interactions with the language model.\n",
    "2. **Multi-turn Prompts**: Series of interactions that maintain context.\n",
    "3. **Prompt Templates**: Reusable structures for consistent prompting.\n",
    "4. **Conversation Chains**: Maintaining context across multiple interactions.\n",
    "\n",
    "## Method Details\n",
    "\n",
    "We'll use a combination of OpenAI's API and LangChain library to demonstrate these prompt structures. The tutorial will include practical examples and comparisons of different prompt types."
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Setup\n",
    "\n",
    "First, let's import the necessary libraries and set up our environment."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {},
   "outputs": [],
   "source": [
    "import os\n",
    "from langchain_openai import ChatOpenAI\n",
    "from langchain.prompts import PromptTemplate\n",
    "from langchain.chains import ConversationChain\n",
    "from langchain.memory import ConversationBufferMemory\n",
    "\n",
    "from dotenv import load_dotenv\n",
    "load_dotenv()\n",
    "\n",
    "os.environ[\"OPENAI_API_KEY\"] = os.getenv('OPENAI_API_KEY') # OpenAI API key\n",
    "# Initialize the language model\n",
    "llm = ChatOpenAI(model=\"gpt-4o-mini\")"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 1. Single-turn Prompts\n",
    "\n",
    "Single-turn prompts are one-shot interactions with the language model. They consist of a single input (prompt) and generate a single output (response)."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "The three primary colors are red, blue, and yellow. These colors cannot be created by mixing other colors together and are the foundation for creating a wide range of other colors through mixing. In the context of additive color mixing (like with light), the primary colors are red, green, and blue (RGB).\n"
     ]
    }
   ],
   "source": [
    "single_turn_prompt = \"What are the three primary colors?\"\n",
    "print(llm.invoke(single_turn_prompt).content)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Now, let's use a PromptTemplate to create a more structured single-turn prompt:"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Color theory is a framework used to understand how colors interact, complement each other, and can be combined to create various visual effects. It is essential in fields such as art, design, and photography, helping artists and designers make informed choices about color usage to evoke emotions, communicate messages, and create harmony in their work.\n",
      "\n",
      "The three main components of color theory are:\n",
      "\n",
      "1. **Color Wheel**: A circular diagram that shows the relationships between colors. It typically includes primary, secondary, and tertiary colors, providing a visual representation of how colors can be combined.\n",
      "\n",
      "2. **Color Harmony**: The concept of combining colors in a pleasing way. It involves using color schemes such as complementary, analogous, and triadic to create balance and visual interest.\n",
      "\n",
      "3. **Color Context**: This refers to how colors interact with one another and how they can change perception based on their surrounding colors. The same color can appear different depending on the colors next to it, which influences mood and interpretation.\n"
     ]
    }
   ],
   "source": [
    "structured_prompt = PromptTemplate(\n",
    "    input_variables=[\"topic\"],\n",
    "    template=\"Provide a brief explanation of {topic} and list its three main components.\"\n",
    ")\n",
    "\n",
    "chain = structured_prompt | llm\n",
    "print(chain.invoke({\"topic\": \"color theory\"}).content)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 2. Multi-turn Prompts (Conversations)\n",
    "\n",
    "Multi-turn prompts involve a series of interactions with the language model, allowing for more complex and context-aware conversations."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": "conversation = ConversationChain(\n    llm=llm, \n    verbose=True,\n    memory=ConversationBufferMemory()\n)\n\nprint(conversation.invoke(input=\"Hi, I'm learning about space. Can you tell me about planets?\")[\"response\"])\nprint(conversation.invoke(input=\"What's the largest planet in our solar system?\")[\"response\"])\nprint(conversation.invoke(input=\"How does its size compare to Earth?\")[\"response\"])"
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Let's compare how single-turn and multi-turn prompts handle a series of related questions:"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": "# Single-turn prompts\nprompts = [\n    \"What is the capital of France?\",\n    \"What is its population?\",\n    \"What is the city's most famous landmark?\"\n]\n\nprint(\"Single-turn responses:\")\nfor prompt in prompts:\n    print(f\"Q: {prompt}\")\n    print(f\"A: {llm.invoke(prompt).content}\\n\")\n\n# Multi-turn prompts\nprint(\"Multi-turn responses:\")\nconversation = ConversationChain(llm=llm, memory=ConversationBufferMemory())\nfor prompt in prompts:\n    print(f\"Q: {prompt}\")\n    print(f\"A: {conversation.invoke(input=prompt)['response']}\\n\")"
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Conclusion\n",
    "\n",
    "This tutorial has introduced you to the basics of single-turn and multi-turn prompt structures. We've seen how:\n",
    "\n",
    "1. Single-turn prompts are useful for quick, isolated queries.\n",
    "2. Multi-turn prompts maintain context across a conversation, allowing for more complex interactions.\n",
    "3. PromptTemplates can be used to create structured, reusable prompts.\n",
    "4. Conversation chains in LangChain help manage context in multi-turn interactions.\n",
    "\n",
    "Understanding these different prompt structures allows you to choose the most appropriate approach for various tasks and create more effective interactions with AI language models."
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": ".venv",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.12.0"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 4
}

================================================
FILE: all_prompt_engineering_techniques/constrained-guided-generation.ipynb
================================================
{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# Constrained and Guided Generation Tutorial\n",
    "\n",
    "## Overview\n",
    "\n",
    "This tutorial explores the concepts of constrained and guided generation in the context of large language models. We'll focus on techniques to set up constraints for model outputs and implement rule-based generation using OpenAI's GPT models and the LangChain library.\n",
    "\n",
    "## Motivation\n",
    "\n",
    "While large language models are powerful tools for generating text, they sometimes produce outputs that are too open-ended or lack specific desired characteristics. Constrained and guided generation techniques allow us to exert more control over the model's outputs, making them more suitable for specific tasks or adhering to certain rules and formats.\n",
    "\n",
    "## Key Components\n",
    "\n",
    "1. Setting up constraints for model outputs\n",
    "2. Implementing rule-based generation\n",
    "3. Using LangChain's PromptTemplate for structured prompts\n",
    "4. Leveraging OpenAI's GPT models for text generation\n",
    "\n",
    "## Method Details\n",
    "\n",
    "We'll use a combination of prompt engineering techniques and LangChain's utilities to implement constrained and guided generation:\n",
    "\n",
    "1. We'll start by setting up the environment and importing necessary libraries.\n",
    "2. We'll create structured prompts using LangChain's PromptTemplate to guide the model's output.\n",
    "3. We'll implement constraints by specifying rules and formats in our prompts.\n",
    "4. We'll use OpenAI's GPT model to generate text based on our constrained prompts.\n",
    "5. We'll explore different techniques for rule-based generation, including output parsing and regex-based validation.\n",
    "\n",
    "## Conclusion\n",
    "\n",
    "By the end of this tutorial, you'll have a solid understanding of how to implement constrained and guided generation techniques. These skills will enable you to create more controlled and specific outputs from large language models, making them more suitable for a wide range of applications where precise and rule-adherent text generation is required."
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Setup\n",
    "\n",
    "First, let's import the necessary libraries and set up our environment."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [],
   "source": [
    "import os\n",
    "import re\n",
    "\n",
    "from langchain_openai import ChatOpenAI\n",
    "from langchain.prompts import PromptTemplate\n",
    "from langchain.output_parsers import RegexParser\n",
    "\n",
    "from dotenv import load_dotenv\n",
    "load_dotenv()\n",
    "\n",
    "# Set up the OpenAI API key\n",
    "os.environ[\"OPENAI_API_KEY\"] = os.getenv('OPENAI_API_KEY')\n",
    "\n",
    "# Initialize the language model\n",
    "llm = ChatOpenAI(model=\"gpt-4o-mini\")\n",
    "\n",
    "# Function to display model outputs\n",
    "def display_output(output):\n",
    "    \"\"\"Display the model's output in a formatted manner.\"\"\"\n",
    "    print(\"Model Output:\")\n",
    "    print(\"-\" * 40)\n",
    "    print(output)\n",
    "    print(\"-\" * 40)\n",
    "    print()"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Setting Up Constraints for Model Outputs\n",
    "\n",
    "Let's start by creating a constrained prompt that generates a product description with specific requirements."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Model Output:\n",
      "----------------------------------------\n",
      "**Stay Hydrated, Stay Awesome!**  \n",
      "\n",
      "Meet your new hydration buddy! Our Smart Water Bottle tracks your water intake, reminds you to sip throughout the day, and syncs with your favorite fitness apps. Made from eco-friendly materials and designed for on-the-go lifestyles, it’s the perfect accessory for health-conscious millennials. Ready to elevate your hydration game? Grab yours today and drink up the good vibes!\n",
      "----------------------------------------\n",
      "\n"
     ]
    }
   ],
   "source": [
    "constrained_prompt = PromptTemplate(\n",
    "    input_variables=[\"product\", \"target_audience\", \"tone\", \"word_limit\"],\n",
    "    template=\"\"\"Create a product description for {product} targeted at {target_audience}.\n",
    "    Use a {tone} tone and keep it under {word_limit} words.\n",
    "    The description should include:\n",
    "    1. A catchy headline\n",
    "    2. Three key features\n",
    "    3. A call to action\n",
    "    \n",
    "    Product Description:\n",
    "    \"\"\"\n",
    ")\n",
    "\n",
    "# Generate the constrained output\n",
    "input_variables = {\n",
    "    \"product\": \"smart water bottle\",\n",
    "    \"target_audience\": \"health-conscious millennials\",\n",
    "    \"tone\": \"casual and friendly\",\n",
    "    \"word_limit\": \"75\"\n",
    "}\n",
    "\n",
    "chain = constrained_prompt | llm\n",
    "output = chain.invoke(input_variables).content\n",
    "display_output(output)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Implementing Rule-Based Generation\n",
    "\n",
    "Now, let's implement a rule-based generation system for creating structured job postings."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Model Output:\n",
      "----------------------------------------\n",
      "COMPANY: TechInnovate Solutions is a forward-thinking technology firm dedicated to developing cutting-edge software solutions that drive success for businesses worldwide. Located in the heart of San Francisco, we pride ourselves on fostering a collaborative and innovative work environment.\n",
      "\n",
      "RESPONSIBILITIES:\n",
      "- Design and implement robust software architectures to support scalable applications.\n",
      "- Lead cross-functional teams in the development and deployment of new features and enhancements.\n",
      "- Collaborate with product managers to define and prioritize product requirements.\n",
      "- Mentor junior engineers and provide guidance on best coding practices and methodologies.\n",
      "- Conduct code reviews and ensure adherence to industry standards and quality assurance processes.\n",
      "\n",
      "QUALIFICATIONS:\n",
      "- A minimum of 5 years of professional software engineering experience is required. \n",
      "- Proficiency in programming languages such as Java, Python, or JavaScript is essential.\n",
      "- Strong understanding of software development methodologies, including Agile and DevOps practices.\n",
      "- Experience with cloud platforms such as AWS, Azure, or Google Cloud is preferred.\n",
      "- Excellent problem-solving skills and the ability to work effectively in a team-oriented environment are necessary.\n",
      "\n",
      "EEO: TechInnovate Solutions is an equal opportunity employer. We celebrate diversity and are committed to creating an inclusive environment for all employees.\n",
      "----------------------------------------\n",
      "\n"
     ]
    }
   ],
   "source": [
    "job_posting_prompt = PromptTemplate(\n",
    "    input_variables=[\"job_title\", \"company\", \"location\", \"experience\"],\n",
    "    template=\"\"\"Create a job posting for a {job_title} position at {company} in {location}.\n",
    "    The candidate should have {experience} years of experience.\n",
    "    Follow these rules:\n",
    "    1. Start with a brief company description (2 sentences)\n",
    "    2. List 5 key responsibilities, each starting with an action verb\n",
    "    3. List 5 required qualifications, each in a single sentence\n",
    "    4. End with a standardized equal opportunity statement\n",
    "    \n",
    "    Format the output as follows:\n",
    "    COMPANY: [Company Description]\n",
    "    \n",
    "    RESPONSIBILITIES:\n",
    "    - [Responsibility 1]\n",
    "    - [Responsibility 2]\n",
    "    - [Responsibility 3]\n",
    "    - [Responsibility 4]\n",
    "    - [Responsibility 5]\n",
    "    \n",
    "    QUALIFICATIONS:\n",
    "    - [Qualification 1]\n",
    "    - [Qualification 2]\n",
    "    - [Qualification 3]\n",
    "    - [Qualification 4]\n",
    "    - [Qualification 5]\n",
    "    \n",
    "    EEO: [Equal Opportunity Statement]\n",
    "    \"\"\"\n",
    ")\n",
    "\n",
    "# Generate the rule-based output\n",
    "input_variables = {\n",
    "    \"job_title\": \"Senior Software Engineer\",\n",
    "    \"company\": \"TechInnovate Solutions\",\n",
    "    \"location\": \"San Francisco, CA\",\n",
    "    \"experience\": \"5+\"\n",
    "}\n",
    "\n",
    "chain = job_posting_prompt | llm\n",
    "output = chain.invoke(input_variables).content\n",
    "display_output(output)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Using Regex Parser for Structured Output\n",
    "\n",
    "Let's use a regex parser to ensure our output adheres to a specific structure."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Parsed Output:\n",
      "COMPANY_DESCRIPTION:\n",
      "TechInnovate Solutions is a leading technology firm based in San Francisco, CA, dedicated to creating cutting-edge software solutions that empower businesses to thrive in the digital age. Our team of innovative thinkers and problem solvers is committed to pushing the boundaries of technology to deliver exceptional products and services.\n",
      "\n",
      "RESPONSIBILITIES:\n",
      "- Design and develop scalable software applications that meet the needs of our clients.\n",
      "- Collaborate with cross-functional teams to define, design, and implement new features.\n",
      "- Mentor junior engineers, providing guidance and support for their professional growth.\n",
      "- Troubleshoot and resolve software defects and performance issues in a timely manner.\n",
      "- Stay updated with emerging technologies and industry trends to ensure best practices.\n",
      "\n",
      "QUALIFICATIONS:\n",
      "- A minimum of 5 years of experience in software development, with a strong focus on full-stack technologies. \n",
      "- Proficiency in programming languages such as Java, Python, or JavaScript, along with relevant frameworks.\n",
      "- Experience with cloud platforms such as AWS, Azure, or Google Cloud.\n",
      "- Strong understanding of software development life cycle (SDLC) and agile methodologies.\n",
      "- Excellent problem-solving skills and ability to work in a fast-paced environment.\n",
      "\n",
      "EEO_STATEMENT:\n",
      "TechInnovate Solutions is an equal opportunity employer. We celebrate diversity and are committed to creating an inclusive environment for all employees.\n",
      "\n"
     ]
    }
   ],
   "source": [
    "# Define a regex parser for structured output\n",
    "regex_parser = RegexParser(\n",
    "    regex=r\"COMPANY:\\s*([\\s\\S]*?)\\n\\s*RESPONSIBILITIES:\\s*([\\s\\S]*?)\\n\\s*QUALIFICATIONS:\\s*([\\s\\S]*?)\\n\\s*EEO:\\s*([\\s\\S]*)\",\n",
    "    output_keys=[\"company_description\", \"responsibilities\", \"qualifications\", \"eeo_statement\"]\n",
    ")\n",
    "# This regex pattern captures the company description, responsibilities, qualifications, and EEO statement from the output text.\n",
    "\n",
    "# Create a new prompt template that includes the parser instructions\n",
    "parsed_job_posting_prompt = PromptTemplate(\n",
    "    input_variables=[\"job_title\", \"company\", \"location\", \"experience\"],\n",
    "    template=\"\"\"Create a job posting for a {job_title} position at {company} in {location}.\n",
    "    The candidate should have {experience} years of experience.\n",
    "    Follow these rules:\n",
    "    1. Start with a brief company description (2 sentences)\n",
    "    2. List 5 key responsibilities, each starting with an action verb\n",
    "    3. List 5 required qualifications, each in a single sentence\n",
    "    4. End with a standardized equal opportunity statement\n",
    "    \n",
    "    Format the output EXACTLY as follows:\n",
    "    COMPANY: [Company Description]\n",
    "    \n",
    "    RESPONSIBILITIES:\n",
    "    - [Responsibility 1]\n",
    "    - [Responsibility 2]\n",
    "    - [Responsibility 3]\n",
    "    - [Responsibility 4]\n",
    "    - [Responsibility 5]\n",
    "    \n",
    "    QUALIFICATIONS:\n",
    "    - [Qualification 1]\n",
    "    - [Qualification 2]\n",
    "    - [Qualification 3]\n",
    "    - [Qualification 4]\n",
    "    - [Qualification 5]\n",
    "    \n",
    "    EEO: [Equal Opportunity Statement]\n",
    "    \"\"\"\n",
    ")\n",
    "\n",
    "def clean_output(output):\n",
    "    for key, value in output.items():\n",
    "        if isinstance(value, str):\n",
    "            # Remove leading/trailing whitespace and normalize newlines\n",
    "            output[key] = re.sub(r'\\n\\s*', '\\n', value.strip())\n",
    "    return output\n",
    "\n",
    "# Generate the parsed output\n",
    "chain = parsed_job_posting_prompt | llm\n",
    "raw_output = chain.invoke(input_variables).content\n",
    "\n",
    "# Parse and clean the output\n",
    "parsed_output = regex_parser.parse(raw_output)\n",
    "cleaned_output = clean_output(parsed_output)\n",
    "\n",
    "# Display the parsed output\n",
    "print(\"Parsed Output:\")\n",
    "for key, value in cleaned_output.items():\n",
    "    print(f\"{key.upper()}:\")\n",
    "    print(value)\n",
    "    print()"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Implementing Additional Constraints\n",
    "\n",
    "Let's create a more complex constrained generation task: generating a product review with specific criteria."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Model Output:\n",
      "----------------------------------------\n",
      "Rating: 4 out of 5 stars\n",
      "\n",
      "Pros:\n",
      "1. The camera quality on Smartphone X is outstanding, capturing vibrant colors and sharp details even in low light. The multiple lens options provide versatility for different photography styles, making it a great choice for both casual users and photography enthusiasts.\n",
      "2. Battery life is impressive, lasting a full day with heavy usage. Quick charging capabilities ensure that you can get back to using your phone in no time, which is a huge plus for those on the go.\n",
      "3. The sleek design and lightweight build make Smartphone X comfortable to hold and use throughout the day. Its premium feel and modern aesthetics also make it visually appealing.\n",
      "\n",
      "Cons:\n",
      "1. Although the performance is generally smooth, there can be occasional lag when multitasking with resource-heavy applications. This might be a drawback for users who rely heavily on their devices for productivity.\n",
      "2. The lack of expandable storage is a limitation for those who need extra space for apps, photos, and videos. Users may find themselves needing to manage their storage more frequently as a result.\n",
      "\n",
      "Recommendation: Overall, Smartphone X is a fantastic choice for anyone seeking a powerful and stylish device.\n",
      "----------------------------------------\n",
      "\n"
     ]
    }
   ],
   "source": [
    "review_prompt = PromptTemplate(\n",
    "    input_variables=[\"product\", \"rating\", \"pros\", \"cons\", \"word_limit\"],\n",
    "    template=\"\"\"Write a product review for {product} with the following constraints:\n",
    "    1. The review should have a {rating}-star rating (out of 5)\n",
    "    2. Include exactly {pros} pros and {cons} cons\n",
    "    3. Use between 2 and 3 sentences for each pro and con\n",
    "    4. The entire review should be under {word_limit} words\n",
    "    5. End with a one-sentence recommendation\n",
    "    \n",
    "    Format the review as follows:\n",
    "    Rating: [X] out of 5 stars\n",
    "    \n",
    "    Pros:\n",
    "    1. [Pro 1]\n",
    "    2. [Pro 2]\n",
    "    ...\n",
    "    \n",
    "    Cons:\n",
    "    1. [Con 1]\n",
    "    2. [Con 2]\n",
    "    ...\n",
    "    \n",
    "    Recommendation: [One-sentence recommendation]\n",
    "    \"\"\"\n",
    ")\n",
    "\n",
    "# Generate the constrained review\n",
    "input_variables = {\n",
    "    \"product\": \"Smartphone X\",\n",
    "    \"rating\": \"4\",\n",
    "    \"pros\": \"3\",\n",
    "    \"cons\": \"2\",\n",
    "    \"word_limit\": \"200\"\n",
    "}\n",
    "\n",
    "chain = review_prompt | llm\n",
    "output = chain.invoke(input_variables).content\n",
    "display_output(output)"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": ".venv",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.12.0"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 4
}


================================================
FILE: all_prompt_engineering_techniques/cot-prompting.ipynb
================================================
{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# Chain of Thought (CoT) Prompting Tutorial\n",
    "\n",
    "## Overview\n",
    "\n",
    "This tutorial introduces Chain of Thought (CoT) prompting, a powerful technique in prompt engineering that encourages AI models to break down complex problems into step-by-step reasoning processes. We'll explore how to implement CoT prompting using OpenAI's GPT models and the LangChain library.\n",
    "\n",
    "## Motivation\n",
    "\n",
    "As AI language models become more advanced, there's an increasing need to guide them towards producing more transparent, logical, and verifiable outputs. CoT prompting addresses this need by encouraging models to show their work, much like how humans approach complex problem-solving tasks. This technique not only improves the accuracy of AI responses but also makes them more interpretable and trustworthy.\n",
    "\n",
    "## Key Components\n",
    "\n",
    "1. **Basic CoT Prompting**: Introduction to the concept and simple implementation.\n",
    "2. **Advanced CoT Techniques**: Exploring more sophisticated CoT approaches.\n",
    "3. **Comparative Analysis**: Examining the differences between standard and CoT prompting.\n",
    "4. **Problem-Solving Applications**: Applying CoT to various complex tasks.\n",
    "\n",
    "## Method Details\n",
    "\n",
    "The tutorial will guide learners through the following methods:\n",
    "\n",
    "1. **Setting up the environment**: We'll start by importing necessary libraries and setting up the OpenAI API.\n",
    "\n",
    "2. **Basic CoT Implementation**: We'll create simple CoT prompts and compare their outputs to standard prompts.\n",
    "\n",
    "3. **Advanced CoT Techniques**: We'll explore more complex CoT strategies, including multi-step reasoning and self-consistency checks.\n",
    "\n",
    "4. **Practical Applications**: We'll apply CoT prompting to various problem-solving scenarios, such as mathematical word problems and logical reasoning tasks.\n",
    "\n",
    "\n",
    "## Conclusion\n",
    "\n",
    "By the end of this tutorial, learners will have a solid understanding of Chain of Thought prompting and its applications. They will be equipped with practical skills to implement CoT techniques in various scenarios, improving the quality and interpretability of AI-generated responses. This knowledge will be valuable for anyone working with large language models, from developers and researchers to business analysts and decision-makers relying on AI-powered insights."
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Setup\n",
    "\n",
    "Let's start by importing the necessary libraries and setting up our environment."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": "import os\nfrom dotenv import load_dotenv\nfrom langchain_openai import ChatOpenAI\nfrom langchain.prompts import PromptTemplate\n\n# Load environment variables\nload_dotenv()\n\n# Set up OpenAI API key\nos.environ[\"OPENAI_API_KEY\"] = os.getenv(\"OPENAI_API_KEY\")\n\n# Initialize the language model\nllm = ChatOpenAI(model=\"gpt-3.5-turbo\")"
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Basic Chain of Thought Prompting\n",
    "\n",
    "Let's start with a simple example to demonstrate the difference between a standard prompt and a Chain of Thought prompt."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 18,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Standard Response:\n",
      "The average speed of the train is 60 km/h.\n",
      "\n",
      "Chain of Thought Response:\n",
      "Step 1: Calculate the average speed by dividing the total distance traveled by the total time taken.\n",
      "\n",
      "Step 2: Average speed = Total distance / Total time\n",
      "\n",
      "Step 3: Average speed = 120 km / 2 hours\n",
      "\n",
      "Step 4: Average speed = 60 km/h\n",
      "\n",
      "Therefore, the average speed of the train is 60 km/h.\n"
     ]
    }
   ],
   "source": [
    "# Standard prompt\n",
    "standard_prompt = PromptTemplate(\n",
    "    input_variables=[\"question\"],\n",
    "    template=\"Answer the following question concisely: {question}.\"\n",
    ")\n",
    "\n",
    "# Chain of Thought prompt\n",
    "cot_prompt = PromptTemplate(\n",
    "    input_variables=[\"question\"],\n",
    "    template=\"Answer the following question step by step concisely: {question}\"\n",
    ")\n",
    "\n",
    "# Create chains\n",
    "standard_chain = standard_prompt | llm\n",
    "cot_chain = cot_prompt | llm\n",
    "\n",
    "# Example question\n",
    "question = \"If a train travels 120 km in 2 hours, what is its average speed in km/h?\"\n",
    "\n",
    "# Get responses\n",
    "standard_response = standard_chain.invoke(question).content\n",
    "cot_response = cot_chain.invoke(question).content\n",
    "\n",
    "print(\"Standard Response:\")\n",
    "print(standard_response)\n",
    "print(\"\\nChain of Thought Response:\")\n",
    "print(cot_response)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Advanced Chain of Thought Techniques\n",
    "\n",
    "Now, let's explore a more advanced CoT technique that encourages multi-step reasoning."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 19,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "1. Calculate the total distance traveled and the total time taken for the entire journey.\n",
      "2. Total distance = 150 km + 100 km = 250 km.\n",
      "   Total time = (150 km / 60 km/h) + (100 km / 50 km/h).\n",
      "3. Total time = (2.5 hours) + (2 hours) = 4.5 hours.\n",
      "4. The total distance traveled is 250 km, and the total time taken is 4.5 hours. To find the average speed, we divide the total distance by the total time:\n",
      "   Average speed = Total distance / Total time\n",
      "                   = 250 km / 4.5 hours\n",
      "                   ≈ 55.56 km/h.\n",
      "5. Therefore, the average speed for the entire journey is approximately 55.56 km/h.\n"
     ]
    }
   ],
   "source": [
    "advanced_cot_prompt = PromptTemplate(\n",
    "    input_variables=[\"question\"],\n",
    "    template=\"\"\"Solve the following problem step by step. For each step:\n",
    "1. State what you're going to calculate\n",
    "2. Write the formula you'll use (if applicable)\n",
    "3. Perform the calculation\n",
    "4. Explain the result\n",
    "\n",
    "Question: {question}\n",
    "\n",
    "Solution:\"\"\"\n",
    ")\n",
    "\n",
    "advanced_cot_chain = advanced_cot_prompt | llm\n",
    "\n",
    "complex_question = \"A car travels 150 km at 60 km/h, then another 100 km at 50 km/h. What is the average speed for the entire journey?\"\n",
    "\n",
    "advanced_cot_response = advanced_cot_chain.invoke(complex_question).content\n",
    "print(advanced_cot_response)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Comparative Analysis\n",
    "\n",
    "Let's compare the effectiveness of standard prompting vs. CoT prompting on a more challenging problem."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 23,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Standard Response:\n",
      "It will take approximately 3 hours and 56 minutes for the tank to overflow.\n",
      "\n",
      "Chain of Thought Response:\n",
      "Step 1: Calculate the volume of the water in the tank when it is 2/3 full.\n",
      "1. Calculate the volume of the cylinder\n",
      "   Formula: V = πr^2h\n",
      "   V = 3.14159 * (1.5)^2 * 4\n",
      "   V ≈ 28.27433 cubic meters\n",
      "\n",
      "2. Calculate the volume of water in the tank when it is 2/3 full\n",
      "   Volume = 2/3 * 28.27433\n",
      "   Volume ≈ 18.84955 cubic meters\n",
      "\n",
      "Step 2: Calculate how long it will take for the tank to overflow.\n",
      "1. Calculate the remaining volume until the tank overflows\n",
      "   Remaining Volume = 28.27433 - 18.84955\n",
      "   Remaining Volume ≈ 9.42478 cubic meters\n",
      "\n",
      "2. Convert the remaining volume to liters\n",
      "   Remaining Volume in liters = 9424.78 * 1000\n",
      "   Remaining Volume in liters = 9424.78 liters\n",
      "\n",
      "3. Calculate the time it will take for the tank to overflow\n",
      "   Time = Remaining Volume / Rate of water addition\n",
      "   Time = 9424.78 / 10\n",
      "   Time ≈ 942.478 minutes\n",
      "\n",
      "Step 3: Convert the time to hours and minutes\n",
      "1. Convert the time to hours\n",
      "   Hours = 942.478 / 60\n",
      "   Hours ≈ 15.70797 hours\n",
      "\n",
      "2. Calculate the remaining minutes\n",
      "   Remaining Minutes = 0.70797 * 60\n",
      "   Remaining Minutes ≈ 42.4782 minutes\n",
      "\n",
      "Step 4: Final answer\n",
      "It will take approximately 15 hours and 42 minutes for the tank to overflow when water is being added at a rate of 10 liters per minute.\n"
     ]
    }
   ],
   "source": [
    "challenging_question = \"\"\"\n",
    "A cylindrical water tank with a radius of 1.5 meters and a height of 4 meters is 2/3 full. \n",
    "If water is being added at a rate of 10 liters per minute, how long will it take for the tank to overflow? \n",
    "Give your answer in hours and minutes, rounded to the nearest minute. \n",
    "(Use 3.14159 for π and 1000 liters = 1 cubic meter)\"\"\"\n",
    "\n",
    "standard_response = standard_chain.invoke(challenging_question).content\n",
    "cot_response = advanced_cot_chain.invoke(challenging_question).content\n",
    "\n",
    "print(\"Standard Response:\")\n",
    "print(standard_response)\n",
    "print(\"\\nChain of Thought Response:\")\n",
    "print(cot_response)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Problem-Solving Applications\n",
    "\n",
    "Now, let's apply CoT prompting to a more complex logical reasoning task."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": "llm = ChatOpenAI(model=\"gpt-4o\")\n\nlogical_reasoning_prompt = PromptTemplate(\n    input_variables=[\"scenario\"],\n    template=\"\"\"Analyze the following logical puzzle thoroughly. Follow these steps in your analysis:\n\nList the Facts:\n\nSummarize all the given information and statements clearly.\nIdentify all the characters or elements involved.\nIdentify Possible Roles or Conditions:\n\nDetermine all possible roles, behaviors, or states applicable to the characters or elements (e.g., truth-teller, liar, alternator).\nNote the Constraints:\n\nOutline any rules, constraints, or relationships specified in the puzzle.\nGenerate Possible Scenarios:\n\nSystematically consider all possible combinations of roles or conditions for the characters or elements.\nEnsure that all permutations are accounted for.\nTest Each Scenario:\n\nFor each possible scenario:\nAssume the roles or conditions you've assigned.\nAnalyze each statement based on these assumptions.\nCheck for consistency or contradictions within the scenario.\nEliminate Inconsistent Scenarios:\n\nDiscard any scenarios that lead to contradictions or violate the constraints.\nKeep track of the reasoning for eliminating each scenario.\nConclude the Solution:\n\nIdentify the scenario(s) that remain consistent after testing.\nSummarize the findings.\nProvide a Clear Answer:\n\nState definitively the role or condition of each character or element.\nExplain why this is the only possible solution based on your analysis.\nScenario:\n\n{scenario}\n\nAnalysis:\"\"\")\n\nlogical_reasoning_chain = logical_reasoning_prompt | llm\n\nlogical_puzzle = \"\"\"In a room, there are three people: Amy, Bob, and Charlie. \nOne of them always tells the truth, one always lies, and one alternates between truth and lies. \nAmy says, 'Bob is a liar.' \nBob says, 'Charlie alternates between truth and lies.' \nCharlie says, 'Amy and I are both liars.' \nDetermine the nature (truth-teller, liar, or alternator) of each person.\"\"\"\n\nlogical_reasoning_response = logical_reasoning_chain.invoke(logical_puzzle).content\nprint(logical_reasoning_response)"
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": ".venv",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.12.0"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 4
}

================================================
FILE: all_prompt_engineering_techniques/ethical-prompt-engineering.ipynb
================================================
{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# Ethical Considerations in Prompt Engineering\n",
    "\n",
    "## Overview\n",
    "\n",
    "This tutorial explores the ethical dimensions of prompt engineering, focusing on two critical aspects: avoiding biases in prompts and creating inclusive and fair prompts. As AI language models become increasingly integrated into various applications, ensuring ethical use becomes paramount.\n",
    "\n",
    "## Motivation\n",
    "\n",
    "AI language models, trained on vast amounts of data, can inadvertently perpetuate or amplify existing biases. Prompt engineers play a crucial role in mitigating these biases and promoting fairness. This tutorial aims to equip learners with the knowledge and tools to create more ethical and inclusive prompts.\n",
    "\n",
    "## Key Components\n",
    "\n",
    "1. Understanding biases in AI\n",
    "2. Techniques for identifying biases in prompts\n",
    "3. Strategies for creating inclusive prompts\n",
    "4. Methods for evaluating fairness in AI outputs\n",
    "5. Practical examples and exercises\n",
    "\n",
    "## Method Details\n",
    "\n",
    "This tutorial employs a combination of theoretical explanations and practical demonstrations:\n",
    "\n",
    "1. We begin by setting up the necessary environment, including the OpenAI API and LangChain library.\n",
    "2. We explore common types of biases in AI and how they can manifest in prompts.\n",
    "3. Through examples, we demonstrate how to identify and mitigate biases in prompts.\n",
    "4. We introduce techniques for creating inclusive prompts that consider diverse perspectives.\n",
    "5. We implement methods to evaluate the fairness of AI-generated outputs.\n",
    "6. Throughout the tutorial, we provide exercises for hands-on learning and application of ethical prompt engineering principles.\n",
    "\n",
    "## Conclusion\n",
    "\n",
    "By the end of this tutorial, learners will have gained:\n",
    "1. An understanding of the ethical implications of prompt engineering\n",
    "2. Skills to identify and mitigate biases in prompts\n",
    "3. Techniques for creating inclusive and fair prompts\n",
    "4. Methods to evaluate and improve the ethical quality of AI outputs\n",
    "5. Practical experience in applying ethical considerations to real-world prompt engineering scenarios\n",
    "\n",
    "This knowledge will empower prompt engineers to create more responsible and equitable AI applications, contributing to the development of AI systems that benefit all members of society."
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Setup\n",
    "\n",
    "First, let's import the necessary libraries and set up our environment."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "metadata": {},
   "outputs": [],
   "source": [
    "import os\n",
    "from langchain_openai import ChatOpenAI\n",
    "from langchain.prompts import PromptTemplate\n",
    "\n",
    "from dotenv import load_dotenv\n",
    "load_dotenv()\n",
    "\n",
    "# Set up OpenAI API key\n",
    "os.environ[\"OPENAI_API_KEY\"] = os.getenv('OPENAI_API_KEY')\n",
    "\n",
    "# Initialize the language model\n",
    "llm = ChatOpenAI(model=\"gpt-3.5-turbo\")\n",
    "\n",
    "def get_model_response(prompt):\n",
    "    \"\"\"Helper function to get model response.\"\"\"\n",
    "    return llm.invoke(prompt).content"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Understanding Biases in AI\n",
    "\n",
    "Let's start by examining how biases can manifest in AI responses. We'll use a potentially biased prompt and analyze the output."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Potentially biased response:\n",
      "A typical programmer is someone who is highly analytical, detail-oriented, and logical. They are skilled in computer programming languages and have a strong understanding of algorithms and data structures. They are often passionate about problem-solving and enjoy working on complex technical challenges. Programmers are also typically self-motivated and enjoy learning new technologies to stay up-to-date in their field. They may work independently or as part of a team, collaborating with others to develop software solutions for a variety of industries and applications.\n"
     ]
    }
   ],
   "source": [
    "biased_prompt = \"Describe a typical programmer.\"\n",
    "biased_response = get_model_response(biased_prompt)\n",
    "print(\"Potentially biased response:\")\n",
    "print(biased_response)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Identifying and Mitigating Biases\n",
    "\n",
    "Now, let's create a more inclusive prompt and compare the results."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "More inclusive response:\n",
      "Computer programmers come from a wide range of backgrounds and bring diverse experiences and characteristics to their work. Some programmers have formal education in computer science or related fields, while others are self-taught or have learned through online courses and bootcamps. \n",
      "\n",
      "In terms of their backgrounds, programmers may come from various industries such as finance, healthcare, education, or entertainment, bringing with them domain knowledge that can be valuable in developing software for those specific sectors. Some programmers may have a background in mathematics or engineering, while others may have studied liberal arts or social sciences before transitioning to a career in programming.\n",
      "\n",
      "In terms of their experiences, programmers may have worked in different roles before becoming programmers, such as project management, quality assurance, or technical support. This diverse experience can bring a unique perspective to their programming work and help them understand the needs of different stakeholders.\n",
      "\n",
      "In terms of their characteristics, programmers may have a wide range of personalities and communication styles. Some may be more introverted and prefer to work independently, while others may be more extroverted and thrive in collaborative team environments. Some programmers may be highly analytical and detail-oriented, while others may be more creative and innovative in their approach to problem-solving.\n",
      "\n",
      "Overall, the diverse range of individuals who work as computer programmers brings a richness of perspectives and skills to the field, making it a dynamic and exciting profession to be a part of.\n"
     ]
    }
   ],
   "source": [
    "inclusive_prompt = PromptTemplate(\n",
    "    input_variables=[\"profession\"],\n",
    "    template=\"Describe the diverse range of individuals who work as {profession}, emphasizing the variety in their backgrounds, experiences, and characteristics.\"\n",
    ")\n",
    "\n",
    "inclusive_response = (inclusive_prompt | llm).invoke({\"profession\": \"computer programmers\"}).content\n",
    "print(\"More inclusive response:\")\n",
    "print(inclusive_response)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Creating Inclusive Prompts\n",
    "\n",
    "Let's explore techniques for creating prompts that encourage diverse and inclusive responses."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 10,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Inclusive perspective on leadership:\n",
      "Leadership is a complex and multifaceted concept that can be approached from a variety of perspectives, each offering valuable insights into what makes a successful leader. It is important to recognize the diversity of viewpoints, experiences, and cultural contexts that shape our understanding of leadership, and to consider these factors when examining different leadership styles and approaches.\n",
      "\n",
      "One perspective on leadership is that of transformational leadership, which emphasizes the importance of inspiring and motivating followers to achieve a common goal. Transformational leaders are often seen as visionary and charismatic, able to articulate a compelling vision and inspire others to work towards it. This approach to leadership can be particularly effective in times of change or uncertainty, as it encourages followers to embrace new ideas and ways of working.\n",
      "\n",
      "Another perspective on leadership is that of servant leadership, which focuses on the leader's role in serving the needs of their followers. Servant leaders prioritize the well-being and development of their team members, and see themselves as stewards of their organization's resources and mission. This approach to leadership can foster a sense of trust and loyalty among followers, and create a supportive and inclusive organizational culture.\n",
      "\n",
      "In addition to these perspectives, it is important to consider the impact of diverse experiences and cultural contexts on leadership. Different cultural norms and values can shape how leadership is perceived and practiced, and leaders must be sensitive to these differences in order to be effective. For example, in some cultures, a more hierarchical leadership style may be expected, while in others, a more collaborative and participative approach may be preferred.\n",
      "\n",
      "Ultimately, a balanced and inclusive perspective on leadership recognizes that there is no one-size-fits-all approach to leading others. Leaders must be able to adapt their style to meet the needs of their team and organization, and be open to learning from diverse viewpoints and experiences. By embracing this diversity, leaders can create a more inclusive and effective work environment, where all team members feel valued and empowered to contribute to the organization's success.\n",
      "\n",
      "--------------------------------------------------\n",
      "\n",
      "Inclusive perspective on family structures:\n",
      "Family structures vary greatly across different cultures and societies, and it is important to recognize and respect the diversity of family arrangements that exist. In some cultures, the nuclear family consisting of parents and children is the norm, while in others, extended families or communal living arrangements are more common. Additionally, there are families headed by single parents, same-sex couples, or individuals who have chosen not to have children.\n",
      "\n",
      "It is essential to acknowledge that there is no one-size-fits-all definition of what constitutes a family. Families come in all shapes and sizes, and what matters most is the love, support, and care that individuals provide for each other. Family is about the bonds that connect people, rather than a specific set of roles or relationships.\n",
      "\n",
      "It is also important to recognize that family structures can change over time and that individuals may have multiple families throughout their lives. Divorce, remarriage, adoption, and other life events can all impact the composition of a family. It is crucial to support and validate the experiences of individuals who may not have traditional family structures, as their relationships are just as valid and meaningful.\n",
      "\n",
      "Ultimately, the most important thing is to create a sense of belonging, love, and support within a family, regardless of its structure. By embracing diversity and inclusivity in our understanding of family, we can create a more compassionate and accepting society for all individuals.\n",
      "\n",
      "--------------------------------------------------\n",
      "\n",
      "Inclusive perspective on beauty standards:\n",
      "Beauty standards are a complex and multifaceted aspect of society that vary greatly across cultures, regions, and individuals. While some may argue that beauty standards are arbitrary and superficial, others believe that they play a significant role in shaping societal norms and individual self-esteem.\n",
      "\n",
      "On one hand, beauty standards can be seen as harmful and exclusionary, promoting a narrow and unrealistic ideal of beauty that can be damaging to those who do not fit that mold. This can lead to body image issues, low self-esteem, and even mental health struggles for individuals who feel pressured to conform to these standards. Additionally, beauty standards can perpetuate harmful stereotypes and discrimination, particularly against marginalized groups who do not fit the dominant beauty ideal.\n",
      "\n",
      "On the other hand, beauty standards can also be viewed as a form of cultural expression and identity. Different cultures have their own unique beauty ideals that reflect their values, traditions, and history. Embracing diverse beauty standards can promote inclusivity and celebrate the beauty of all individuals, regardless of their appearance. Beauty standards can also serve as a form of self-expression and creativity, allowing individuals to explore and celebrate their unique features and personal style.\n",
      "\n",
      "It is important to recognize the complexity and diversity of beauty standards, and to challenge narrow and exclusionary ideals that can harm individuals and perpetuate harmful stereotypes. By promoting inclusivity, diversity, and self-acceptance, we can create a more inclusive and accepting society that celebrates the beauty of all individuals, regardless of their appearance. Beauty comes in all shapes, sizes, colors, and forms, and it is important to embrace and celebrate this diversity in all its forms.\n",
      "\n",
      "--------------------------------------------------\n",
      "\n"
     ]
    }
   ],
   "source": [
    "def create_inclusive_prompt(topic):\n",
    "    \"\"\"Creates an inclusive prompt template for a given topic.\"\"\"\n",
    "    return PromptTemplate(\n",
    "        input_variables=[\"topic\"],\n",
    "        template=\"Provide a balanced and inclusive perspective on {topic}, considering diverse viewpoints, experiences, and cultural contexts.\"\n",
    "    )\n",
    "\n",
    "topics = [\"leadership\", \"family structures\", \"beauty standards\"]\n",
    "\n",
    "for topic in topics:\n",
    "    prompt = create_inclusive_prompt(topic)\n",
    "    response = (prompt | llm).invoke({\"topic\": topic}).content\n",
    "    print(f\"Inclusive perspective on {topic}:\")\n",
    "    print(response)\n",
    "    print(\"\\n\" + \"-\"*50 + \"\\n\")"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Evaluating Fairness in AI Outputs\n",
    "\n",
    "Now, let's implement a simple method to evaluate the fairness of AI-generated outputs."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 11,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Fairness Evaluation:\n",
      "This text may be biased towards a certain type of leadership style that values decisiveness and command. It could potentially exclude leaders who demonstrate other traits such as empathy, collaboration, or inclusivity. The language used may also imply a more traditional and hierarchical view of leadership. Overall, the text could be more inclusive by acknowledging a wider range of leadership qualities and styles.\n",
      "\n",
      "Fairness score: 5\n"
     ]
    }
   ],
   "source": [
    "def evaluate_fairness(text):\n",
    "    \"\"\"Evaluates the fairness of a given text.\"\"\"\n",
    "    evaluation_prompt = PromptTemplate(\n",
    "        input_variables=[\"text\"],\n",
    "        template=\"Evaluate the following text for fairness and inclusivity. Identify any potential biases or exclusionary language. Provide a fairness score from 1 to 10, where 10 is most fair and inclusive:\\n\\nText: {text}\\n\\nEvaluation:\"\n",
    "    )\n",
    "    return (evaluation_prompt | llm).invoke({\"text\": text}).content\n",
    "\n",
    "# Example usage\n",
    "sample_text = \"In the corporate world, strong leaders are often characterized by their decisiveness and ability to command respect.\"\n",
    "fairness_evaluation = evaluate_fairness(sample_text)\n",
    "print(\"Fairness Evaluation:\")\n",
    "print(fairness_evaluation)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Practical Exercise\n",
    "\n",
    "Let's apply what we've learned to improve a potentially biased prompt."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 12,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Original prompt:\n",
      "Describe the ideal candidate for a high-stress executive position.\n",
      "\n",
      "Original response:\n",
      "The ideal candidate for a high-stress executive position is someone who possesses strong leadership skills, exceptional decision-making abilities, and the ability to remain calm under pressure. They should have a proven track record of successfully managing multiple projects and teams simultaneously, as well as the ability to adapt quickly to changing situations.\n",
      "\n",
      "Additionally, the ideal candidate should have excellent communication skills and be able to effectively delegate tasks and responsibilities to others. They should also be highly organized, detail-oriented, and able to prioritize tasks effectively to meet deadlines.\n",
      "\n",
      "Furthermore, the ideal candidate should have a strong work ethic, determination, and resilience to overcome challenges and setbacks. They should be able to think strategically and creatively to find solutions to complex problems and drive the company forward towards success.\n",
      "\n",
      "Overall, the ideal candidate for a high-stress executive position should have a combination of leadership, communication, organization, and problem-solving skills, as well as the ability to thrive in a fast-paced and high-pressure environment.\n",
      "\n",
      "Improved prompt:\n",
      "Describe a range of qualities and skills that could make someone successful in a high-stress executive position, considering diverse backgrounds, experiences, and leadership styles. Emphasize the importance of work-life balance and mental health.\n",
      "\n",
      "Improved response:\n",
      "Success in a high-stress executive position requires a diverse range of qualities and skills that can be cultivated through various backgrounds, experiences, and leadership styles. Some key attributes that can contribute to success in such a role include:\n",
      "\n",
      "1. Resilience: The ability to bounce back from setbacks and challenges is crucial in a high-stress executive position. Being able to maintain a positive attitude and approach challenges with a problem-solving mindset can help navigate difficult situations effectively.\n",
      "\n",
      "2. Emotional intelligence: Understanding and managing one's own emotions, as well as being able to empathize with others, is essential in building strong relationships and effective communication in a high-stress environment.\n",
      "\n",
      "3. Adaptability: The ability to quickly adjust to changing circumstances and make decisions under pressure is critical in an executive role. Being able to pivot and change course when necessary can help navigate unexpected challenges and opportunities.\n",
      "\n",
      "4. Strategic thinking: Having a clear vision and long-term goals, as well as the ability to develop and execute strategic plans, is important in driving the success of a high-stress executive position. Being able to think critically and analytically can help make informed decisions that align with organizational objectives.\n",
      "\n",
      "5. Communication skills: Effective communication is key in any leadership role, but especially in a high-stress executive position where clear and concise communication is essential for managing teams, stakeholders, and external partners.\n",
      "\n",
      "6. Time management: Being able to prioritize tasks, delegate responsibilities, and manage one's time effectively is crucial in managing the demands of a high-stress executive position. Setting boundaries and creating a healthy work-life balance is important for maintaining mental health and overall well-being.\n",
      "\n",
      "7. Self-care: Prioritizing self-care, such as exercise, healthy eating, and mindfulness practices, can help maintain mental health and prevent burnout in a high-stress executive role. Taking time for oneself and engaging in activities outside of work can help recharge and refocus, ultimately leading to better decision-making and overall success.\n",
      "\n",
      "In conclusion, success in a high-stress executive position requires a combination of qualities and skills that can be developed through diverse backgrounds, experiences, and leadership styles. Emphasizing the importance of work-life balance and mental health is essential in maintaining well-being and long-term success in such a demanding role.\n",
      "\n",
      "Fairness evaluation of improved response:\n",
      "This text is fairly inclusive and fair in its content. It emphasizes a range of qualities and skills needed for success in an executive position, without specifying any particular gender, race, or other demographic characteristic. The mention of prioritizing work-life balance and mental health also adds a layer of inclusivity, acknowledging the importance of self-care for all individuals in high-stress roles.\n",
      "\n",
      "However, one potential bias in the text could be the assumption that all individuals in executive positions face the same level of stress and challenges. It may not account for additional barriers that individuals from marginalized backgrounds may face in these roles. \n",
      "\n",
      "Fairness Score: 8.5\n"
     ]
    }
   ],
   "source": [
    "biased_prompt = \"Describe the ideal candidate for a high-stress executive position.\"\n",
    "\n",
    "print(\"Original prompt:\")\n",
    "print(biased_prompt)\n",
    "print(\"\\nOriginal response:\")\n",
    "print(get_model_response(biased_prompt))\n",
    "\n",
    "# TODO: Improve this prompt to be more inclusive and fair\n",
    "improved_prompt = PromptTemplate(\n",
    "    input_variables=[\"position\"],\n",
    "    template=\"Describe a range of qualities and skills that could make someone successful in a {position}, considering diverse backgrounds, experiences, and leadership styles. Emphasize the importance of work-life balance and mental health.\"\n",
    ")\n",
    "\n",
    "print(\"\\nImproved prompt:\")\n",
    "print(improved_prompt.format(position=\"high-stress executive position\"))\n",
    "print(\"\\nImproved response:\")\n",
    "print((improved_prompt | llm).invoke({\"position\": \"high-stress executive position\"}).content)\n",
    "\n",
    "# Evaluate the fairness of the improved response\n",
    "fairness_score = evaluate_fairness((improved_prompt | llm).invoke({\"position\": \"high-stress executive position\"}).content)\n",
    "print(\"\\nFairness evaluation of improved response:\")\n",
    "print(fairness_score)"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": ".venv",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.12.0"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 4
}


================================================
FILE: all_prompt_engineering_techniques/evaluating-prompt-effectiveness.ipynb
================================================
{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# Evaluating Prompt Effectiveness\n",
    "\n",
    "## Overview\n",
    "This tutorial focuses on methods and techniques for evaluating the effectiveness of prompts in AI language models. We'll explore various metrics for measuring prompt performance and discuss both manual and automated evaluation techniques.\n",
    "\n",
    "## Motivation\n",
    "As prompt engineering becomes increasingly crucial in AI applications, it's essential to have robust methods for assessing prompt effectiveness. This enables developers and researchers to optimize their prompts, leading to better AI model performance and more reliable outputs.\n",
    "\n",
    "## Key Components\n",
    "1. Metrics for measuring prompt performance\n",
    "2. Manual evaluation techniques\n",
    "3. Automated evaluation techniques\n",
    "4. Practical examples using OpenAI and LangChain\n",
    "\n",
    "## Method Details\n",
    "We'll start by setting up our environment and introducing key metrics for evaluating prompts. We'll then explore manual evaluation techniques, including human assessment and comparative analysis. Next, we'll delve into automated evaluation methods, utilizing techniques like perplexity scoring and automated semantic similarity comparisons. Throughout the tutorial, we'll provide practical examples using OpenAI's GPT models and LangChain library to demonstrate these concepts in action.\n",
    "\n",
    "## Conclusion\n",
    "By the end of this tutorial, you'll have a comprehensive understanding of how to evaluate prompt effectiveness using both manual and automated techniques. You'll be equipped with practical tools and methods to optimize your prompts, leading to more efficient and accurate AI model interactions."
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Setup\n",
    "\n",
    "First, let's import the necessary libraries and set up our environment."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "import os\n",
    "from langchain_openai import ChatOpenAI\n",
    "from sklearn.metrics.pairwise import cosine_similarity\n",
    "from sentence_transformers import SentenceTransformer\n",
    "import numpy as np\n",
    "\n",
    "# Load environment variables\n",
    "from dotenv import load_dotenv\n",
    "load_dotenv()\n",
    "\n",
    "# Set up OpenAI API key\n",
    "os.environ[\"OPENAI_API_KEY\"] = os.getenv('OPENAI_API_KEY')\n",
    "\n",
    "# Initialize the language model\n",
    "llm = ChatOpenAI(model=\"gpt-4o-mini\")\n",
    "\n",
    "# Initialize sentence transformer for semantic similarity\n",
    "sentence_model = SentenceTransformer('all-MiniLM-L6-v2')\n",
    "\n",
    "def semantic_similarity(text1, text2):\n",
    "    \"\"\"Calculate semantic similarity between two texts using cosine similarity.\"\"\"\n",
    "    embeddings = sentence_model.encode([text1, text2])\n",
    "    return cosine_similarity([embeddings[0]], [embeddings[1]])[0][0]"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Metrics for Measuring Prompt Performance\n",
    "\n",
    "Let's define some key metrics for evaluating prompt effectiveness:"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "metadata": {},
   "outputs": [],
   "source": [
    "def relevance_score(response, expected_content):\n",
    "    \"\"\"Calculate relevance score based on semantic similarity to expected content.\"\"\"\n",
    "    return semantic_similarity(response, expected_content)\n",
    "\n",
    "def consistency_score(responses):\n",
    "    \"\"\"Calculate consistency score based on similarity between multiple responses.\"\"\"\n",
    "    if len(responses) < 2:\n",
    "        return 1.0  # Perfect consistency if there's only one response\n",
    "    similarities = []\n",
    "    for i in range(len(responses)):\n",
    "        for j in range(i+1, len(responses)):\n",
    "            similarities.append(semantic_similarity(responses[i], responses[j]))\n",
    "    return np.mean(similarities)\n",
    "\n",
    "def specificity_score(response):\n",
    "    \"\"\"Calculate specificity score based on response length and unique word count.\"\"\"\n",
    "    words = response.split()\n",
    "    unique_words = set(words)\n",
    "    return len(unique_words) / len(words) if words else 0"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Manual Evaluation Techniques\n",
    "\n",
    "Manual evaluation involves human assessment of prompt-response pairs. Let's create a function to simulate this process:"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Prompt: Explain the concept of machine learning in simple terms.\n",
      "Response: Machine learning is a type of computer technology that allows computers to learn from data and improve their performance over time without being explicitly programmed for every specific task. \n",
      "\n",
      "In simple terms, imagine teaching a child to recognize different animals. Instead of giving them a detailed description of each animal, you show them many pictures of cats, dogs, and birds. Over time, the child learns to identify these animals based on patterns they see in the images, like shapes, colors, and sizes. \n",
      "\n",
      "In the same way, machine learning involves feeding a computer lots of data (like pictures, numbers, or text) and letting it figure out patterns and make decisions on its own. For example, a machine learning model can be trained to recognize spam emails by analyzing examples of both spam and non-spam messages. Once trained, it can then automatically identify new emails as spam or not.\n",
      "\n",
      "So, in essence, machine learning is about teaching computers to learn from experience, adapt to new information, and make predictions or decisions based on what they’ve learned.\n",
      "\n",
      "Evaluation Criteria:\n",
      "Clarity: 5.0/10\n",
      "Accuracy: 5.0/10\n",
      "Simplicity: 5.0/10\n",
      "\n",
      "Additional Comments:\n",
      "Comments: 5\n"
     ]
    }
   ],
   "source": [
    "def manual_evaluation(prompt, response, criteria):\n",
    "    \"\"\"Simulate manual evaluation of a prompt-response pair.\"\"\"\n",
    "    print(f\"Prompt: {prompt}\")\n",
    "    print(f\"Response: {response}\")\n",
    "    print(\"\\nEvaluation Criteria:\")\n",
    "    for criterion in criteria:\n",
    "        score = float(input(f\"Score for {criterion} (0-10): \"))\n",
    "        print(f\"{criterion}: {score}/10\")\n",
    "    print(\"\\nAdditional Comments:\")\n",
    "    comments = input(\"Enter any additional comments: \")\n",
    "    print(f\"Comments: {comments}\")\n",
    "\n",
    "# Example usage\n",
    "prompt = \"Explain the concept of machine learning in simple terms.\"\n",
    "response = llm.invoke(prompt).content\n",
    "criteria = [\"Clarity\", \"Accuracy\", \"Simplicity\"]\n",
    "manual_evaluation(prompt, response, criteria)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Automated Evaluation Techniques\n",
    "\n",
    "Now, let's implement some automated evaluation techniques:"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Prompt: What are the three main types of machine learning?\n",
      "Response: The three main types of machine learning are:\n",
      "\n",
      "1. **Supervised Learning**: In supervised learning, the model is trained on a labeled dataset, which means that the input data is paired with the correct output. The goal is for the model to learn to map inputs to the correct outputs so that it can make predictions on new, unseen data. Common applications include classification (e.g., spam detection) and regression (e.g., predicting house prices).\n",
      "\n",
      "2. **Unsupervised Learning**: In unsupervised learning, the model is trained on data that does not have labeled outputs. The goal is to identify patterns, structures, or relationships within the data. Common techniques include clustering (e.g., grouping customers based on purchasing behavior) and dimensionality reduction (e.g., reducing the number of features while retaining important information).\n",
      "\n",
      "3. **Reinforcement Learning**: In reinforcement learning, an agent learns to make decisions by interacting with an environment. The agent receives feedback in the form of rewards or penalties based on its actions, and it aims to maximize the cumulative reward over time. This type of learning is commonly used in applications like game playing (e.g., AlphaGo) and robotics.\n",
      "\n",
      "These three types represent different approaches to learning from data and are used in various applications across multiple domains.\n",
      "\n",
      "Relevance Score: 0.74\n",
      "Specificity Score: 0.64\n"
     ]
    },
    {
     "data": {
      "text/plain": [
       "{'relevance': 0.73795843, 'specificity': 0.6403940886699507}"
      ]
     },
     "execution_count": 9,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "def automated_evaluation(prompt, response, expected_content):\n",
    "    \"\"\"Perform automated evaluation of a prompt-response pair.\"\"\"\n",
    "    relevance = relevance_score(response, expected_content)\n",
    "    specificity = specificity_score(response)\n",
    "    \n",
    "    print(f\"Prompt: {prompt}\")\n",
    "    print(f\"Response: {response}\")\n",
    "    print(f\"\\nRelevance Score: {relevance:.2f}\")\n",
    "    print(f\"Specificity Score: {specificity:.2f}\")\n",
    "    \n",
    "    return {\"relevance\": relevance, \"specificity\": specificity}\n",
    "\n",
    "# Example usage\n",
    "prompt = \"What are the three main types of machine learning?\"\n",
    "expected_content = \"The three main types of machine learning are supervised learning, unsupervised learning, and reinforcement learning.\"\n",
    "response = llm.invoke(prompt).content\n",
    "automated_evaluation(prompt, response, expected_content)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Comparative Analysis\n",
    "\n",
    "Let's compare the effectiveness of different prompts for the same task:"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Prompt: List the types of machine learning.\n",
      "Response: Machine learning can be broadly categorized into several types, each serving different purposes and applications. The main types of machine learning are:\n",
      "\n",
      "1. **Supervised Learning**:\n",
      "   - Involves training a model on a labeled dataset, where the input data is paired with the correct output. The model learns to map inputs to outputs, and its performance is evaluated based on how accurately it predicts the outcomes for new, unseen data.\n",
      "   - Common algorithms: Linear regression, logistic regression, decision trees, support vector machines, neural networks.\n",
      "\n",
      "2. **Unsupervised Learning**:\n",
      "   - Involves training a model on data without labeled responses. The model tries to learn the underlying structure or distribution in the data, often identifying patterns, clusters, or relationships.\n",
      "   - Common algorithms: K-means clustering, hierarchical clustering, principal component analysis (PCA), t-distributed stochastic neighbor embedding (t-SNE).\n",
      "\n",
      "3. **Semi-Supervised Learning**:\n",
      "   - Combines both labeled and unlabeled data for training. This approach is useful when obtaining a fully labeled dataset is expensive or time-consuming. The model leverages both types of data to improve learning accuracy.\n",
      "   - Common applications include image classification, text classification, and speech recognition.\n",
      "\n",
      "4. **Reinforcement Learning**:\n",
      "   - Involves training an agent to make decisions by interacting with an environment. The agent learns to achieve a goal by receiving feedback in the form of rewards or penalties. The learning process is based on trial and error.\n",
      "   - Common applications: Game playing (e.g., AlphaGo), robotics, recommendation systems.\n",
      "\n",
      "5. **Self-Supervised Learning**:\n",
      "   - A subset of unsupervised learning where the model generates its own labels from the input data, allowing it to learn representations of the data without needing labeled examples. It is often used in natural language processing and computer vision.\n",
      "   - Common techniques: Contrastive learning, predicting masked parts of input data (e.g., masked language modeling).\n",
      "\n",
      "6. **Multi-Instance Learning**:\n",
      "   - A type of learning where the model is trained on bags of instances rather than individual labeled instances. Each bag is labeled, but individual instances within the bag may not be labeled.\n",
      "   - Common applications: Drug activity prediction, image classification tasks.\n",
      "\n",
      "7. **Transfer Learning**:\n",
      "   - Involves taking a pre-trained model on one task and fine-tuning it on a different but related task. This approach is particularly useful when labeled data for the new task is scarce.\n",
      "   - Commonly used in deep learning applications, especially in computer vision and natural language processing.\n",
      "\n",
      "These types of machine learning can be applied in various domains, including healthcare, finance, marketing, and more, depending on the specific requirements of the task at hand.\n",
      "\n",
      "Relevance Score: 0.74\n",
      "Specificity Score: 0.57\n",
      "Prompt: What are the main categories of machine learning algorithms?\n",
      "Response: Machine learning algorithms can be broadly categorized into several main categories based on their learning styles and the types of problems they are designed to solve. Here are the primary categories:\n",
      "\n",
      "1. **Supervised Learning**: \n",
      "   - In this category, the algorithm is trained on labeled data, meaning that each training example is paired with an output label. The goal is to learn a mapping from inputs to outputs.\n",
      "   - Common algorithms include:\n",
      "     - Linear Regression\n",
      "     - Logistic Regression\n",
      "     - Decision Trees\n",
      "     - Support Vector Machines (SVM)\n",
      "     - Neural Networks\n",
      "     - Random Forests\n",
      "     - Gradient Boosting Machines (e.g., XGBoost)\n",
      "\n",
      "2. **Unsupervised Learning**: \n",
      "   - This type of learning deals with unlabeled data, where the algorithm tries to learn the underlying structure or distribution of the data without explicit outputs.\n",
      "   - Common algorithms include:\n",
      "     - K-Means Clustering\n",
      "     - Hierarchical Clustering\n",
      "     - Principal Component Analysis (PCA)\n",
      "     - t-Distributed Stochastic Neighbor Embedding (t-SNE)\n",
      "     - Autoencoders\n",
      "\n",
      "3. **Semi-Supervised Learning**: \n",
      "   - This category combines both labeled and unlabeled data during training. It is particularly useful when acquiring a fully labeled dataset is expensive or time-consuming.\n",
      "   - Common approaches include variations of supervised algorithms that incorporate unlabeled data to improve learning.\n",
      "\n",
      "4. **Reinforcement Learning**: \n",
      "   - In reinforcement learning, an agent learns to make decisions by taking actions in an environment to maximize a cumulative reward. The learning process involves exploration and exploitation.\n",
      "   - Common algorithms include:\n",
      "     - Q-Learning\n",
      "     - Deep Q-Networks (DQN)\n",
      "     - Policy Gradients\n",
      "     - Proximal Policy Optimization (PPO)\n",
      "     - Actor-Critic Methods\n",
      "\n",
      "5. **Self-Supervised Learning**: \n",
      "   - This is a form of unsupervised learning where the system generates its own supervisory signal from the input data. It’s particularly popular in natural language processing and computer vision.\n",
      "   - Techniques often involve predicting parts of the input data from other parts (e.g., masked language modeling in transformers).\n",
      "\n",
      "6. **Transfer Learning**: \n",
      "   - This approach involves taking a pre-trained model (often trained on a large dataset) and fine-tuning it on a smaller, task-specific dataset. This is especially useful in deep learning applications.\n",
      "\n",
      "7. **Ensemble Learning**: \n",
      "   - Ensemble methods combine multiple models to produce a better performance than any individual model. This can involve techniques such as bagging, boosting, and stacking.\n",
      "   - Common algorithms include Random Forests (bagging) and AdaBoost (boosting).\n",
      "\n",
      "These categories encompass a wide range of algorithms, each suited for different types of tasks and datasets. The choice of algorithm often depends on the problem at hand, the nature of the data, and the desired outcome.\n",
      "\n",
      "Relevance Score: 0.68\n",
      "Specificity Score: 0.60\n",
      "Prompt: Explain the different approaches to machine learning.\n",
      "Response: Machine learning (ML) is a subset of artificial intelligence that focuses on building systems that can learn from and make decisions based on data. There are several key approaches to machine learning, which can be broadly categorized into the following types:\n",
      "\n",
      "### 1. Supervised Learning\n",
      "In supervised learning, the model is trained on a labeled dataset, which means that each training example is associated with a corresponding output label. The goal is to learn a mapping from inputs to outputs so that the model can predict the label of new, unseen data.\n",
      "\n",
      "- **Examples**: \n",
      "  - Classification (e.g., spam detection, image recognition)\n",
      "  - Regression (e.g., predicting house prices, temperature forecasting)\n",
      "\n",
      "- **Common Algorithms**: \n",
      "  - Linear Regression\n",
      "  - Logistic Regression\n",
      "  - Decision Trees\n",
      "  - Support Vector Machines (SVM)\n",
      "  - Neural Networks\n",
      "\n",
      "### 2. Unsupervised Learning\n",
      "Unsupervised learning involves training a model on data that does not have labeled outputs. The goal is to find patterns, structures, or relationships within the data without explicit guidance on what to look for.\n",
      "\n",
      "- **Examples**: \n",
      "  - Clustering (e.g., customer segmentation, grouping similar items)\n",
      "  - Dimensionality Reduction (e.g., Principal Component Analysis, t-SNE)\n",
      "  - Anomaly Detection (e.g., fraud detection)\n",
      "\n",
      "- **Common Algorithms**: \n",
      "  - K-Means Clustering\n",
      "  - Hierarchical Clustering\n",
      "  - DBSCAN (Density-Based Spatial Clustering of Applications with Noise)\n",
      "  - Autoencoders\n",
      "\n",
      "### 3. Semi-Supervised Learning\n",
      "Semi-supervised learning is a hybrid approach that combines both labeled and unlabeled data for training. It is particularly useful when obtaining a fully labeled dataset is expensive or time-consuming. The model leverages the labeled data to guide the learning process while also benefiting from the structure present in the unlabeled data.\n",
      "\n",
      "- **Examples**: \n",
      "  - Text classification where only a few documents are labeled\n",
      "  - Image recognition tasks with limited labeled images\n",
      "\n",
      "- **Common Algorithms**: \n",
      "  - Self-training\n",
      "  - Co-training\n",
      "  - Graph-based methods\n",
      "\n",
      "### 4. Reinforcement Learning\n",
      "Reinforcement learning (RL) is a type of ML where an agent learns to make decisions by interacting with an environment. The agent receives feedback in the form of rewards or penalties based on its actions, allowing it to learn an optimal policy for maximizing cumulative rewards over time.\n",
      "\n",
      "- **Examples**: \n",
      "  - Game playing (e.g., AlphaGo)\n",
      "  - Robotics (e.g., robotic control systems)\n",
      "  - Autonomous vehicles\n",
      "\n",
      "- **Common Algorithms**: \n",
      "  - Q-Learning\n",
      "  - Deep Q-Networks (DQN)\n",
      "  - Proximal Policy Optimization (PPO)\n",
      "  - Actor-Critic methods\n",
      "\n",
      "### 5. Self-Supervised Learning\n",
      "Self-supervised learning is a technique where the model generates its own labels from the input data. This approach is often used in natural language processing and computer vision, where the model learns to predict missing parts of the input or to perform transformations on the input data.\n",
      "\n",
      "- **Examples**: \n",
      "  - Predicting the next word in a sentence (language models like GPT)\n",
      "  - Image inpainting where parts of an image are filled in\n",
      "\n",
      "- **Common Algorithms**: \n",
      "  - Contrastive Learning\n",
      "  - Masked Language Modeling\n",
      "\n",
      "### 6. Transfer Learning\n",
      "Transfer learning involves taking a pre-trained model (usually trained on a large dataset) and fine-tuning it on a smaller, specific dataset. This approach is particularly useful when the target domain has limited data, as it allows leveraging knowledge gained from a related task.\n",
      "\n",
      "- **Examples**: \n",
      "  - Using a model trained on ImageNet for a specific image classification task\n",
      "  - Fine-tuning a language model on domain-specific text\n",
      "\n",
      "- **Common Frameworks**: \n",
      "  - TensorFlow and PyTorch often provide pre-trained models for various tasks.\n",
      "\n",
      "### Conclusion\n",
      "Each of these approaches has its strengths and weaknesses, and the choice of which to use depends on the nature of the data, the specific problem being addressed, and the available resources. Many practical applications of machine learning may involve a combination of these approaches to achieve the best results.\n",
      "\n",
      "Relevance Score: 0.69\n",
      "Specificity Score: 0.52\n",
      "Prompt Comparison Results:\n",
      "\n",
      "1. Prompt: List the types of machine learning.\n",
      "   Relevance: 0.74\n",
      "   Specificity: 0.57\n",
      "\n",
      "2. Prompt: Explain the different approaches to machine learning.\n",
      "   Relevance: 0.69\n",
      "   Specificity: 0.52\n",
      "\n",
      "3. Prompt: What are the main categories of machine learning algorithms?\n",
      "   Relevance: 0.68\n",
      "   Specificity: 0.60\n"
     ]
    },
    {
     "data": {
      "text/plain": [
       "[{'prompt': 'List the types of machine learning.',\n",
       "  'relevance': 0.73586243,\n",
       "  'specificity': 0.5693430656934306},\n",
       " {'prompt': 'Explain the different approaches to machine learning.',\n",
       "  'relevance': 0.68791693,\n",
       "  'specificity': 0.5223880597014925},\n",
       " {'prompt': 'What are the main categories of machine learning algorithms?',\n",
       "  'relevance': 0.67862606,\n",
       "  'specificity': 0.6039603960396039}]"
      ]
     },
     "execution_count": 6,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "def compare_prompts(prompts, expected_content):\n",
    "    \"\"\"Compare the effectiveness of multiple prompts for the same task.\"\"\"\n",
    "    results = []\n",
    "    for prompt in prompts:\n",
    "        response = llm.invoke(prompt).content\n",
    "        evaluation = automated_evaluation(prompt, response, expected_content)\n",
    "        results.append({\"prompt\": prompt, **evaluation})\n",
    "    \n",
    "    # Sort results by relevance score\n",
    "    sorted_results = sorted(results, key=lambda x: x['relevance'], reverse=True)\n",
    "    \n",
    "    print(\"Prompt Comparison Results:\")\n",
    "    for i, result in enumerate(sorted_results, 1):\n",
    "        print(f\"\\n{i}. Prompt: {result['prompt']}\")\n",
    "        print(f\"   Relevance: {result['relevance']:.2f}\")\n",
    "        print(f\"   Specificity: {result['specificity']:.2f}\")\n",
    "    \n",
    "    return sorted_results\n",
    "\n",
    "# Example usage\n",
    "prompts = [\n",
    "    \"List the types of machine learning.\",\n",
    "    \"What are the main categories of machine learning algorithms?\",\n",
    "    \"Explain the different approaches to machine learning.\"\n",
    "]\n",
    "expected_content = \"The main types of machine learning are supervised learning, unsupervised learning, and reinforcement learning.\"\n",
    "compare_prompts(prompts, expected_content)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Putting It All Together\n",
    "\n",
    "Now, let's create a comprehensive prompt evaluation function that combines both manual and automated techniques:"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Automated Evaluation:\n",
      "Prompt: Explain the concept of overfitting in machine learning.\n",
      "Response: Overfitting is a common problem in machine learning where a model learns not only the underlying patterns in the training data but also the noise and random fluctuations. This leads to a model that performs exceptionally well on the training dataset but poorly on unseen data or the test dataset. In essence, the model becomes overly complex, capturing details that do not generalize to new data points.\n",
      "\n",
      "### Key Aspects of Overfitting:\n",
      "\n",
      "1. **Complexity of the Model**: Overfitting often occurs when a model is too complex relative to the amount of training data available. For example, a high-degree polynomial regression may fit a small set of data points perfectly but will not generalize well to new data.\n",
      "\n",
      "2. **Training vs. Validation Performance**: A clear sign of overfitting is when the performance metrics (such as accuracy, loss, etc.) on the training data are significantly better than those on the validation or test data. This disparity indicates that the model is not learning the true underlying relationships but rather memorizing the training examples.\n",
      "\n",
      "3. **Noise**: Overfitted models may learn from noise in the training data, treating random variations as important signals, which can lead to poor predictive performance.\n",
      "\n",
      "### Visual Representation:\n",
      "When visualizing the performance of a model, overfitting can often be seen in a plot where the model fits the training data very closely (high accuracy on training data) but diverges significantly on validation data, leading to a U-shaped curve when plotting training and validation performance against model complexity.\n",
      "\n",
      "### Mitigation Strategies:\n",
      "Several techniques can help mitigate overfitting:\n",
      "\n",
      "1. **Regularization**: Techniques like L1 (Lasso) and L2 (Ridge) regularization add a penalty for larger coefficients in the model, discouraging overly complex models.\n",
      "\n",
      "2. **Cross-Validation**: Using k-fold cross-validation helps ensure that the model's performance is consistent across different subsets of the data.\n",
      "\n",
      "3. **Pruning**: In decision trees, pruning can be used to remove branches that have little importance, simplifying the model.\n",
      "\n",
      "4. **Early Stopping**: In iterative models like neural networks, training can be halted when performance on a validation set begins to degrade, preventing the model from fitting too closely to the training data.\n",
      "\n",
      "5. **Data Augmentation**: Increasing the size of the training dataset through data augmentation techniques can help the model generalize better.\n",
      "\n",
      "6. **Simplifying the Model**: Choosing a simpler model that captures the essential features of the data can reduce the risk of overfitting.\n",
      "\n",
      "### Conclusion:\n",
      "In summary, overfitting is a critical issue in machine learning that impacts a model's ability to generalize to new, unseen data. It is essential for practitioners to recognize the signs of overfitting and implement strategies to mitigate it, ensuring that the models they create are robust and reliable.\n",
      "\n",
      "Relevance Score: 0.82\n",
      "Specificity Score: 0.54\n",
      "\n",
      "Manual Evaluation:\n",
      "Prompt: Explain the concept of overfitting in machine learning.\n",
      "Response: Overfitting is a common problem in machine learning where a model learns not only the underlying patterns in the training data but also the noise and random fluctuations. This leads to a model that performs exceptionally well on the training dataset but poorly on unseen data or the test dataset. In essence, the model becomes overly complex, capturing details that do not generalize to new data points.\n",
      "\n",
      "### Key Aspects of Overfitting:\n",
      "\n",
      "1. **Complexity of the Model**: Overfitting often occurs when a model is too complex relative to the amount of training data available. For example, a high-degree polynomial regression may fit a small set of data points perfectly but will not generalize well to new data.\n",
      "\n",
      "2. **Training vs. Validation Performance**: A clear sign of overfitting is when the performance metrics (such as accuracy, loss, etc.) on the training data are significantly better than those on the validation or test data. This disparity indicates that the model is not learning the true underlying relationships but rather memorizing the training examples.\n",
      "\n",
      "3. **Noise**: Overfitted models may learn from noise in the training data, treating random variations as important signals, which can lead to poor predictive performance.\n",
      "\n",
      "### Visual Representation:\n",
      "When visualizing the performance of a model, overfitting can often be seen in a plot where the model fits the training data very closely (high accuracy on training data) but diverges significantly on validation data, leading to a U-shaped curve when plotting training and validation performance against model complexity.\n",
      "\n",
      "### Mitigation Strategies:\n",
      "Several techniques can help mitigate overfitting:\n",
      "\n",
      "1. **Regularization**: Techniques like L1 (Lasso) and L2 (Ridge) regularization add a penalty for larger coefficients in the model, discouraging overly complex models.\n",
      "\n",
      "2. **Cross-Validation**: Using k-fold cross-validation helps ensure that the model's performance is consistent across different subsets of the data.\n",
      "\n",
      "3. **Pruning**: In decision trees, pruning can be used to remove branches that have little importance, simplifying the model.\n",
      "\n",
      "4. **Early Stopping**: In iterative models like neural networks, training can be halted when performance on a validation set begins to degrade, preventing the model from fitting too closely to the training data.\n",
      "\n",
      "5. **Data Augmentation**: Increasing the size of the training dataset through data augmentation techniques can help the model generalize better.\n",
      "\n",
      "6. **Simplifying the Model**: Choosing a simpler model that captures the essential features of the data can reduce the risk of overfitting.\n",
      "\n",
      "### Conclusion:\n",
      "In summary, overfitting is a critical issue in machine learning that impacts a model's ability to generalize to new, unseen data. It is essential for practitioners to recognize the signs of overfitting and implement strategies to mitigate it, ensuring that the models they create are robust and reliable.\n",
      "\n",
      "Evaluation Criteria:\n",
      "Clarity: 6.0/10\n",
      "Accuracy: 7.0/10\n",
      "Relevance: 6.0/10\n",
      "\n",
      "Additional Comments:\n",
      "Comments: no\n"
     ]
    },
    {
     "data": {
      "text/plain": [
       "{'prompt': 'Explain the concept of overfitting in machine learning.',\n",
       " 'response': \"Overfitting is a common problem in machine learning where a model learns not only the underlying patterns in the training data but also the noise and random fluctuations. This leads to a model that performs exceptionally well on the training dataset but poorly on unseen data or the test dataset. In essence, the model becomes overly complex, capturing details that do not generalize to new data points.\\n\\n### Key Aspects of Overfitting:\\n\\n1. **Complexity of the Model**: Overfitting often occurs when a model is too complex relative to the amount of training data available. For example, a high-degree polynomial regression may fit a small set of data points perfectly but will not generalize well to new data.\\n\\n2. **Training vs. Validation Performance**: A clear sign of overfitting is when the performance metrics (such as accuracy, loss, etc.) on the training data are significantly better than those on the validation or test data. This disparity indicates that the model is not learning the true underlying relationships but rather memorizing the training examples.\\n\\n3. **Noise**: Overfitted models may learn from noise in the training data, treating random variations as important signals, which can lead to poor predictive performance.\\n\\n### Visual Representation:\\nWhen visualizing the performance of a model, overfitting can often be seen in a plot where the model fits the training data very closely (high accuracy on training data) but diverges significantly on validation data, leading to a U-shaped curve when plotting training and validation performance against model complexity.\\n\\n### Mitigation Strategies:\\nSeveral techniques can help mitigate overfitting:\\n\\n1. **Regularization**: Techniques like L1 (Lasso) and L2 (Ridge) regularization add a penalty for larger coefficients in the model, discouraging overly complex models.\\n\\n2. **Cross-Validation**: Using k-fold cross-validation helps ensure that the model's performance is consistent across different subsets of the data.\\n\\n3. **Pruning**: In decision trees, pruning can be used to remove branches that have little importance, simplifying the model.\\n\\n4. **Early Stopping**: In iterative models like neural networks, training can be halted when performance on a validation set begins to degrade, preventing the model from fitting too closely to the training data.\\n\\n5. **Data Augmentation**: Increasing the size of the training dataset through data augmentation techniques can help the model generalize better.\\n\\n6. **Simplifying the Model**: Choosing a simpler model that captures the essential features of the data can reduce the risk of overfitting.\\n\\n### Conclusion:\\nIn summary, overfitting is a critical issue in machine learning that impacts a model's ability to generalize to new, unseen data. It is essential for practitioners to recognize the signs of overfitting and implement strategies to mitigate it, ensuring that the models they create are robust and reliable.\",\n",
       " 'relevance': 0.82301676,\n",
       " 'specificity': 0.5372460496613995}"
      ]
     },
     "execution_count": 7,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "def evaluate_prompt(prompt, expected_content, manual_criteria=['Clarity', 'Accuracy', 'Relevance']):\n",
    "    \"\"\"Perform a comprehensive evaluation of a prompt using both manual and automated techniques.\"\"\"\n",
    "    response = llm.invoke(prompt).content\n",
    "    \n",
    "    print(\"Automated Evaluation:\")\n",
    "    auto_results = automated_evaluation(prompt, response, expected_content)\n",
    "    \n",
    "    print(\"\\nManual Evaluation:\")\n",
    "    manual_evaluation(prompt, response, manual_criteria)\n",
    "    \n",
    "    return {\"prompt\": prompt, \"response\": response, **auto_results}\n",
    "\n",
    "# Example usage\n",
    "prompt = \"Explain the concept of overfitting in machine learning.\"\n",
    "expected_content = \"Overfitting occurs when a model learns the training data too well, including its noise and fluctuations, leading to poor generalization on new, unseen data.\"\n",
    "evaluate_prompt(prompt, expected_content)"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": ".venv",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.12.0"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 4
}


================================================
FILE: all_prompt_engineering_techniques/few-shot-learning.ipynb
================================================
{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# Few-Shot Learning and In-Context Learning Tutorial\n",
    "\n",
    "## Overview\n",
    "This tutorial explores the cutting-edge techniques of Few-Shot Learning and In-Context Learning using OpenAI's GPT models and the LangChain library. These methods enable AI models to perform complex tasks with minimal examples, revolutionizing the way we approach machine learning problems.\n",
    "\n",
    "## Motivation\n",
    "Traditional machine learning often requires large datasets for training, which can be time-consuming and resource-intensive. Few-Shot Learning and In-Context Learning address this limitation by leveraging the power of large language models to perform tasks with just a handful of examples. This approach is particularly valuable in scenarios where labeled data is scarce or expensive to obtain.\n",
    "\n",
    "## Key Components\n",
    "1. **OpenAI's GPT Models**: State-of-the-art language models that serve as the foundation for our learning techniques.\n",
    "2. **LangChain Library**: A powerful tool that simplifies the process of working with large language models.\n",
    "3. **PromptTemplate**: A structured way to format inputs for the language model.\n",
    "4. **LLMChain**: Manages the interaction between the prompt and the language model.\n",
    "\n",
    "## Method Details\n",
    "\n",
    "### 1. Basic Few-Shot Learning\n",
    "- Implementation of a sentiment classification task using few-shot learning.\n",
    "- Demonstration of how to structure a prompt with examples for the model to learn from.\n",
    "- Explanation of how the model generalizes from these examples to new inputs.\n",
    "\n",
    "### 2. Advanced Few-Shot Techniques\n",
    "- Exploration of multi-task learning for sentiment analysis and language detection.\n",
    "- Discussion on how to design prompts that enable a single model to perform multiple related tasks.\n",
    "- Insights into the benefits of this approach, such as improved efficiency and better generalization.\n",
    "\n",
    "### 3. In-Context Learning\n",
    "- Demonstration of in-context learning for a custom task (e.g., text transformation).\n",
    "- Explanation of how models can adapt to new tasks based solely on examples provided in the prompt.\n",
    "- Discussion on the flexibility and limitations of this approach.\n",
    "\n",
    "### 4. Best Practices and Evaluation\n",
    "- Guidelines for selecting effective examples for few-shot learning.\n",
    "- Techniques for prompt engineering to optimize model performance.\n",
    "- Implementation of an evaluation framework to assess model accuracy.\n",
    "- Discussion on the importance of diverse test cases and appropriate metrics.\n",
    "\n",
    "## Conclusion\n",
    "Few-Shot Learning and In-Context Learning represent a significant advancement in the field of artificial intelligence. By enabling models to perform complex tasks with minimal examples, these techniques open up new possibilities for AI applications in areas where data is limited. This tutorial provides a solid foundation for understanding and implementing these powerful methods, equipping learners with the tools to leverage large language models effectively in their own projects.\n",
    "\n",
    "As the field continues to evolve, mastering these techniques will be crucial for AI practitioners looking to stay at the forefront of natural language processing and machine learning."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Setup complete.\n"
     ]
    }
   ],
   "source": [
    "import os\n",
    "from dotenv import load_dotenv\n",
    "from langchain_openai import ChatOpenAI\n",
    "from langchain.prompts import PromptTemplate\n",
    "from langchain.chains import LLMChain\n",
    "\n",
    "load_dotenv()\n",
    "os.environ[\"OPENAI_API_KEY\"] = os.getenv('OPENAI_API_KEY') # OpenAI API key\n",
    "\n",
    "llm = ChatOpenAI(model=\"gpt-4o-mini\", temperature=0)\n",
    "print(\"Setup complete.\")"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Basic Few-Shot Learning\n",
    "\n",
    "We'll implement a basic few-shot learning scenario for sentiment classification.\n",
    "\n",
    "Sentiment Classification:\n",
    "- Definition: Determining the emotional tone behind a series of words.\n",
    "- Applications: Customer service, market research, social media analysis.\n",
    "\n",
    "Few-Shot Learning Approach:\n",
    "1. Provide a small set of labeled examples (3 in this case).\n",
    "2. Structure the prompt to clearly present examples and the new input.\n",
    "3. Leverage the pre-trained knowledge of the language model.\n",
    "\n",
    "Key Components:\n",
    "- PromptTemplate: Structures the input for the model.\n",
    "- LLMChain: Manages the interaction between the prompt and the language model."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 18,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Input: I can't believe how great this new restaurant is!\n",
      "Predicted Sentiment: Positive\n"
     ]
    }
   ],
   "source": [
    "def few_shot_sentiment_classification(input_text):\n",
    "    few_shot_prompt = PromptTemplate(\n",
    "        input_variables=[\"input_text\"],\n",
    "        template=\"\"\"\n",
    "        Classify the sentiment as Positive, Negative, or Neutral.\n",
    "        \n",
    "        Examples:\n",
    "        Text: I love this product! It's amazing.\n",
    "        Sentiment: Positive\n",
    "        \n",
    "        Text: This movie was terrible. I hated it.\n",
    "        Sentiment: Negative\n",
    "        \n",
    "        Text: The weather today is okay.\n",
    "        Sentiment: Neutral\n",
    "        \n",
    "        Now, classify the following:\n",
    "        Text: {input_text}\n",
    "        Sentiment:\n",
    "        \"\"\"\n",
    "    )\n",
    "    \n",
    "    chain = few_shot_prompt | llm\n",
    "    result = chain.invoke(input_text).content\n",
    "\n",
    "    # Clean up the result\n",
    "    result = result.strip()\n",
    "    # Extract only the sentiment label\n",
    "    if ':' in result:\n",
    "        result = result.split(':')[1].strip()\n",
    "    \n",
    "    return result  # This will now return just \"Positive\", \"Negative\", or \"Neutral\"\n",
    "\n",
    "test_text = \"I can't believe how great this new restaurant is!\"\n",
    "result = few_shot_sentiment_classification(test_text)\n",
    "print(f\"Input: {test_text}\")\n",
    "print(f\"Predicted Sentiment: {result}\")"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Advanced Few-Shot Techniques\n",
    "\n",
    "We'll now explore multi-task learning for sentiment analysis and language detection.\n",
    "\n",
    "Multi-task Learning:\n",
    "- Definition: Training a model to perform multiple related tasks simultaneously.\n",
    "- Benefits: Improved efficiency, better generalization, reduced overfitting.\n",
    "\n",
    "Implementation:\n",
    "1. Design a prompt template that includes examples for multiple tasks.\n",
    "2. Use task-specific instructions to guide the model's behavior.\n",
    "3. Demonstrate how the same model can switch between tasks based on input."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 10,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Positive\n",
      "Result: German\n"
     ]
    }
   ],
   "source": [
    "def multi_task_few_shot(input_text, task):\n",
    "    few_shot_prompt = PromptTemplate(\n",
    "        input_variables=[\"input_text\", \"task\"],\n",
    "        template=\"\"\"\n",
    "        Perform the specified task on the given text.\n",
    "        \n",
    "        Examples:\n",
    "        Text: I love this product! It's amazing.\n",
    "        Task: sentiment\n",
    "        Result: Positive\n",
    "        \n",
    "        Text: Bonjour, comment allez-vous?\n",
    "        Task: language\n",
    "        Result: French\n",
    "        \n",
    "        Now, perform the following task:\n",
    "        Text: {input_text}\n",
    "        Task: {task}\n",
    "        Result:\n",
    "        \"\"\"\n",
    "    )\n",
    "    \n",
    "    chain = few_shot_prompt | llm\n",
    "    return chain.invoke({\"input_text\": input_text, \"task\": task}).content\n",
    "\n",
    "print(multi_task_few_shot(\"I can't believe how great this is!\", \"sentiment\"))\n",
    "print(multi_task_few_shot(\"Guten Tag, wie geht es Ihnen?\", \"language\"))"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## In-Context Learning\n",
    "\n",
    "In-Context Learning allows models to adapt to new tasks based on examples provided in the prompt.\n",
    "\n",
    "Key Aspects:\n",
    "1. No fine-tuning required: The model learns from examples in the prompt.\n",
    "2. Flexibility: Can be applied to a wide range of tasks.\n",
    "3. Prompt engineering: Careful design of prompts is crucial for performance.\n",
    "\n",
    "Example Implementation:\n",
    "We'll demonstrate in-context learning for a custom task (converting text to pig latin)."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 11,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Input: python\n",
      "Output: Output: ythonpay\n"
     ]
    }
   ],
   "source": [
    "def in_context_learning(task_description, examples, input_text):\n",
    "    example_text = \"\".join([f\"Input: {e['input']}\\nOutput: {e['output']}\\n\\n\" for e in examples])\n",
    "    \n",
    "    in_context_prompt = PromptTemplate(\n",
    "        input_variables=[\"task_description\", \"examples\", \"input_text\"],\n",
    "        template=\"\"\"\n",
    "        Task: {task_description}\n",
    "        \n",
    "        Examples:\n",
    "        {examples}\n",
    "        \n",
    "        Now, perform the task on the following input:\n",
    "        Input: {input_text}\n",
    "        Output:\n",
    "        \"\"\"\n",
    "    )\n",
    "    \n",
    "    chain = in_context_prompt | llm\n",
    "    return chain.invoke({\"task_description\": task_description, \"examples\": example_text, \"input_text\": input_text}).content\n",
    "\n",
    "task_desc = \"Convert the given text to pig latin.\"\n",
    "examples = [\n",
    "    {\"input\": \"hello\", \"output\": \"ellohay\"},\n",
    "    {\"input\": \"apple\", \"output\": \"appleay\"}\n",
    "]\n",
    "test_input = \"python\"\n",
    "\n",
    "result = in_context_learning(task_desc, examples, test_input)\n",
    "print(f\"Input: {test_input}\")\n",
    "print(f\"Output: {result}\")"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Best Practices and Evaluation\n",
    "\n",
    "To maximize the effectiveness of few-shot and in-context learning:\n",
    "\n",
    "1. Example Selection:\n",
    "   - Diversity: Cover different aspects of the task.\n",
    "   - Clarity: Use unambiguous examples.\n",
    "   - Relevance: Choose examples similar to expected inputs.\n",
    "   - Balance: Ensure equal representation of classes/categories.\n",
    "   - Edge cases: Include examples of unusual or difficult cases.\n",
    "\n",
    "2. Prompt Engineering:\n",
    "   - Clear instructions: Specify the task explicitly.\n",
    "   - Consistent format: Maintain a uniform structure for examples and inputs.\n",
    "   - Conciseness: Avoid unnecessary information that may confuse the model.\n",
    "\n",
    "3. Evaluation:\n",
    "   - Create a diverse test set.\n",
    "   - Compare model predictions to true labels.\n",
    "   - Use appropriate metrics (e.g., accuracy, F1 score) based on the task."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 20,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Input: This product exceeded my expectations!\n",
      "Predicted: Positive\n",
      "Actual: Positive\n",
      "Correct: True\n",
      "\n",
      "Input: I'm utterly disappointed with the service.\n",
      "Predicted: Negative\n",
      "Actual: Negative\n",
      "Correct: True\n",
      "\n",
      "Input: The temperature today is 72 degrees.\n",
      "Predicted: Neutral\n",
      "Actual: Neutral\n",
      "Correct: True\n",
      "\n",
      "Model Accuracy: 1.00\n"
     ]
    }
   ],
   "source": [
    "def evaluate_model(model_func, test_cases):\n",
    "    '''\n",
    "    Evaluate the model on a set of test cases.\n",
    "\n",
    "    Args:\n",
    "    model_func: The function that makes predictions.\n",
    "    test_cases: A list of dictionaries, where each dictionary contains an \"input\" text and a \"label\" for the input.\n",
    "\n",
    "    Returns:\n",
    "    The accuracy of the model on the test cases. \n",
    "    '''\n",
    "    correct = 0\n",
    "    total = len(test_cases)\n",
    "    \n",
    "    for case in test_cases:\n",
    "        input_text = case['input']\n",
    "        true_label = case['label']\n",
    "        prediction = model_func(input_text).strip()\n",
    "        \n",
    "        is_correct = prediction.lower() == true_label.lower()\n",
    "        correct += int(is_correct)\n",
    "        \n",
    "        print(f\"Input: {input_text}\")\n",
    "        print(f\"Predicted: {prediction}\")\n",
    "        print(f\"Actual: {true_label}\")\n",
    "        print(f\"Correct: {is_correct}\\n\")\n",
    "    \n",
    "    accuracy = correct / total\n",
    "    return accuracy\n",
    "\n",
    "test_cases = [\n",
    "    {\"input\": \"This product exceeded my expectations!\", \"label\": \"Positive\"},\n",
    "    {\"input\": \"I'm utterly disappointed with the service.\", \"label\": \"Negative\"},\n",
    "    {\"input\": \"The temperature today is 72 degrees.\", \"label\": \"Neutral\"}\n",
    "]\n",
    "\n",
    "accuracy = evaluate_model(few_shot_sentiment_classification, test_cases)\n",
    "print(f\"Model Accuracy: {accuracy:.2f}\")"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.8.10"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 4
}


================================================
FILE: all_prompt_engineering_techniques/instruction-engineering-notebook.ipynb
================================================
{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# Instruction Engineering Tutorial\n",
    "\n",
    "## Overview\n",
    "\n",
    "This tutorial focuses on Instruction Engineering, a crucial aspect of prompt engineering that deals with crafting clear and effective instructions for language models. We'll explore techniques for creating well-structured prompts and balancing specificity with generality to achieve optimal results.\n",
    "\n",
    "## Motivation\n",
    "\n",
    "As language models become more advanced, the quality of instructions we provide becomes increasingly important. Well-crafted instructions can significantly improve the model's output, leading to more accurate, relevant, and useful responses. This tutorial aims to equip learners with the skills to create effective instructions that maximize the potential of AI language models.\n",
    "\n",
    "## Key Components\n",
    "\n",
    "1. Crafting Clear Instructions: Techniques for writing unambiguous and easily understandable prompts.\n",
    "2. Effective Instruction Structures: Exploring different ways to format and organize instructions.\n",
    "3. Balancing Specificity and Generality: Finding the right level of detail in instructions.\n",
    "4. Iterative Refinement: Techniques for improving instructions based on model outputs.\n",
    "\n",
    "## Method Details\n",
    "\n",
    "We'll use the OpenAI API and LangChain library to demonstrate instruction engineering techniques. The tutorial will cover:\n",
    "\n",
    "1. Setting up the environment and necessary libraries.\n",
    "2. Creating basic instructions and analyzing their effectiveness.\n",
    "3. Refining instructions for clarity and specificity.\n",
    "4. Experimenting with different instruction structures.\n",
    "5. Balancing specific and general instructions for versatile outputs.\n",
    "6. Iterative improvement of instructions based on model responses.\n",
    "\n",
    "Throughout the tutorial, we'll use practical examples to illustrate these concepts and provide hands-on experience in crafting effective instructions.\n",
    "\n",
    "## Conclusion\n",
    "\n",
    "By the end of this tutorial, learners will have gained practical skills in instruction engineering, including how to craft clear and effective instructions, balance specificity and generality, and iteratively refine prompts for optimal results. These skills are essential for anyone working with AI language models and can significantly enhance the quality and usefulness of AI-generated content across various applications."
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Setup\n",
    "\n",
    "First, let's import the necessary libraries and set up our environment."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [],
   "source": [
    "import os\n",
    "from langchain_openai import ChatOpenAI\n",
    "from langchain.prompts import PromptTemplate\n",
    "\n",
    "from dotenv import load_dotenv\n",
    "load_dotenv()\n",
    "\n",
    "# Set up OpenAI API key\n",
    "os.environ[\"OPENAI_API_KEY\"] = os.getenv('OPENAI_API_KEY')\n",
    "\n",
    "# Initialize the language model\n",
    "llm = ChatOpenAI(model=\"gpt-4o-mini\")\n",
    "\n",
    "def get_completion(prompt):\n",
    "    \"\"\"Helper function to get model completion.\"\"\"\n",
    "    return llm.invoke(prompt).content"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Crafting Clear Instructions\n",
    "\n",
    "Let's start by examining the importance of clarity in instructions. We'll compare vague and clear instructions to see the difference in model outputs."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Vague Instruction Output:\n",
      "Climate change refers to significant and lasting changes in global temperatures and weather patterns over time. While climate change is a natural phenomenon, human activities, particularly the burning of fossil fuels, deforestation, and industrial processes, have accelerated this process since the late 19th century. This has led to increased greenhouse gas emissions, primarily carbon dioxide and methane, trapping heat in the atmosphere.\n",
      "\n",
      "Consequences of climate change include rising global temperatures, melting ice caps, sea-level rise, more frequent and severe weather events (like hurricanes and droughts), and disruptions to ecosystems and biodiversity. Addressing climate change requires global cooperation to reduce greenhouse gas emissions, transition to renewable energy sources, and promote sustainable practices.\n",
      "\n",
      "Clear Instruction Output:\n",
      "**Primary Causes of Climate Change:**\n",
      "\n",
      "1. **Greenhouse Gas Emissions:** The combustion of fossil fuels (coal, oil, and natural gas) for energy and transportation is the largest contributor, releasing carbon dioxide (CO2) and methane (CH4).\n",
      "2. **Deforestation:** Trees absorb CO2, and large-scale deforestation reduces this capacity, while also releasing stored carbon.\n",
      "3. **Agricultural Practices:** Livestock production and certain agricultural methods contribute significant greenhouse gases, particularly methane and nitrous oxide.\n",
      "4. **Industrial Processes:** Manufacturing and chemical processes release various greenhouse gases and pollutants.\n",
      "\n",
      "**Effects of Climate Change:**\n",
      "\n",
      "1. **Temperature Rise:** Global temperatures have increased, leading to more frequent and severe heatwaves.\n",
      "2. **Extreme Weather Events:** Increased intensity and frequency of hurricanes, floods, droughts, and wildfires are observed.\n",
      "3. **Sea Level Rise:** Melting ice caps and glaciers, along with thermal expansion of water, contribute to rising sea levels, threatening coastal communities.\n",
      "4. **Ecosystem Disruption:** Altered habitats lead to shifts in biodiversity, threatening species extinction and disrupting food webs.\n",
      "5. **Public Health Risks:** Increased heat and pollution levels can exacerbate health issues, while changing climates can also affect the spread of diseases.\n",
      "\n",
      "The scientific consensus emphasizes that urgent action is needed to mitigate these causes and adapt to the impacts of climate change to ensure a sustainable future.\n"
     ]
    }
   ],
   "source": [
    "vague_instruction = \"Tell me about climate change concisely.\"\n",
    "clear_instruction = \"Provide a concise summary of the primary causes and effects of climate change, focusing on scientific consensus from the past five years concisely.\"\n",
    "\n",
    "print(\"Vague Instruction Output:\")\n",
    "print(get_completion(vague_instruction))\n",
    "\n",
    "print(\"\\nClear Instruction Output:\")\n",
    "print(get_completion(clear_instruction))"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Effective Instruction Structures\n",
    "\n",
    "Now, let's explore different structures for instructions to see how they affect the model's output."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Bullet Structure Output:\n",
      "### Photosynthesis\n",
      "\n",
      "**Definition:**  \n",
      "Photosynthesis is the biochemical process by which green plants, algae, and some bacteria convert light energy, usually from the sun, into chemical energy in the form of glucose, using carbon dioxide and water.\n",
      "\n",
      "**Main Components Involved:**  \n",
      "1. **Light Energy** (usually sunlight)\n",
      "2. **Chlorophyll** (pigment in chloroplasts)\n",
      "3. **Water (H₂O)**\n",
      "4. **Carbon Dioxide (CO₂)**\n",
      "5. **Glucose (C₆H₁₂O₆)**\n",
      "6. **Oxygen (O₂)**\n",
      "\n",
      "**Steps of Photosynthesis:**\n",
      "1. **Light Absorption:** Chlorophyll absorbs sunlight, primarily in the blue and red wavelengths.\n",
      "2. **Water Splitting (Photolysis):** The absorbed light energy splits water molecules into oxygen, protons, and electrons.\n",
      "3. **Oxygen Release:** Oxygen is released as a byproduct into the atmosphere.\n",
      "4. **Energy Conversion:** The electrons move through the electron transport chain, creating ATP (adenosine triphosphate) and NADPH (nicotinamide adenine dinucleotide phosphate) from ADP and NADP⁺.\n",
      "5. **Calvin Cycle:** In the stroma, ATP and NADPH are used to convert carbon dioxide into glucose through a series of reactions.\n",
      "\n",
      "**Importance for Life on Earth:**  \n",
      "Photosynthesis is crucial for life on Earth as it produces oxygen, which is essential for the respiration of most living organisms. Additionally, it forms the base of the food chain, providing energy and organic compounds for plants, animals, and humans.\n",
      "\n",
      "Narrative Structure Output:\n",
      "Photosynthesis is the process by which green plants, algae, and some bacteria convert light energy into chemical energy in the form of glucose, using carbon dioxide and water.\n",
      "\n",
      "Here's how it works, step-by-step:\n",
      "\n",
      "1. **Light Absorption**: Plants have a green pigment called chlorophyll, primarily found in chloroplasts, that captures sunlight. This light energy is essential for driving the photosynthesis process.\n",
      "\n",
      "2. **Water Uptake**: Roots absorb water (H₂O) from the soil and transport it to the leaves through specialized vessels known as xylem.\n",
      "\n",
      "3. **Carbon Dioxide Intake**: Plants take in carbon dioxide (CO₂) from the atmosphere through small openings in their leaves called stomata.\n",
      "\n",
      "4. **Light Reaction**: In the chloroplasts, the absorbed light energy splits water molecules into oxygen (O₂), protons, and electrons. This reaction releases oxygen as a byproduct, which is expelled into the atmosphere.\n",
      "\n",
      "5. **Calvin Cycle**: The electrons and energy produced in the light reaction are used in the Calvin Cycle to convert carbon dioxide and protons into glucose (C₆H₁₂O₆), a simple sugar that serves as an energy source for the plant.\n",
      "\n",
      "In summary, photosynthesis is crucial for life on Earth because it produces oxygen, which is vital for the survival of most living organisms, and it forms the base of the food chain by converting solar energy into a form that can be used by other organisms for energy. Without photosynthesis, life as we know it would 
Download .txt
gitextract_qdq7ojn1/

├── .gitignore
├── CONTRIBUTING.md
├── LICENSE
├── README.md
├── all_prompt_engineering_techniques/
│   ├── ambiguity-clarity.ipynb
│   ├── basic-prompt-structures.ipynb
│   ├── constrained-guided-generation.ipynb
│   ├── cot-prompting.ipynb
│   ├── ethical-prompt-engineering.ipynb
│   ├── evaluating-prompt-effectiveness.ipynb
│   ├── few-shot-learning.ipynb
│   ├── instruction-engineering-notebook.ipynb
│   ├── intro-prompt-engineering-lesson.ipynb
│   ├── multilingual-prompting.ipynb
│   ├── negative-prompting.ipynb
│   ├── prompt-chaining-sequencing.ipynb
│   ├── prompt-formatting-structure.ipynb
│   ├── prompt-length-complexity-management.ipynb
│   ├── prompt-optimization-techniques.ipynb
│   ├── prompt-security-and-safety.ipynb
│   ├── prompt-templates-variables-jinja2.ipynb
│   ├── role-prompting.ipynb
│   ├── self-consistency.ipynb
│   ├── specific-task-prompts.ipynb
│   ├── task-decomposition-prompts.ipynb
│   └── zero-shot-prompting.ipynb
└── requirements.txt
Condensed preview — 27 files, each showing path, character count, and a content snippet. Download the .json file or copy for the full structured content (564K chars).
[
  {
    "path": ".gitignore",
    "chars": 3146,
    "preview": "# Byte-compiled / optimized / DLL files\n__pycache__/\n*.py[cod]\n*$py.class\n\n# C extensions\n*.so\n\n# Distribution / packagi"
  },
  {
    "path": "CONTRIBUTING.md",
    "chars": 6161,
    "preview": "# Contributing to Prompt Engineering Repository\n\nWelcome to the world's most comprehensive repository of Prompt Engineer"
  },
  {
    "path": "LICENSE",
    "chars": 5644,
    "preview": "Custom License Agreement\n\nThis License Agreement (\"Agreement\") is a legal agreement between Nir Diamant (\"Licensor\") and"
  },
  {
    "path": "README.md",
    "chars": 23438,
    "preview": "[![PRs Welcome](https://img.shields.io/badge/PRs-welcome-brightgreen.svg?style=flat-square)](http://makeapullrequest.com"
  },
  {
    "path": "all_prompt_engineering_techniques/ambiguity-clarity.ipynb",
    "chars": 33460,
    "preview": "{\n \"cells\": [\n  {\n   \"cell_type\": \"markdown\",\n   \"metadata\": {},\n   \"source\": [\n    \"# Handling Ambiguity and Improving "
  },
  {
    "path": "all_prompt_engineering_techniques/basic-prompt-structures.ipynb",
    "chars": 7877,
    "preview": "{\n \"cells\": [\n  {\n   \"cell_type\": \"markdown\",\n   \"metadata\": {},\n   \"source\": [\n    \"# Basic Prompt Structures Tutorial\\"
  },
  {
    "path": "all_prompt_engineering_techniques/constrained-guided-generation.ipynb",
    "chars": 17809,
    "preview": "{\n \"cells\": [\n  {\n   \"cell_type\": \"markdown\",\n   \"metadata\": {},\n   \"source\": [\n    \"# Constrained and Guided Generation"
  },
  {
    "path": "all_prompt_engineering_techniques/cot-prompting.ipynb",
    "chars": 12980,
    "preview": "{\n \"cells\": [\n  {\n   \"cell_type\": \"markdown\",\n   \"metadata\": {},\n   \"source\": [\n    \"# Chain of Thought (CoT) Prompting "
  },
  {
    "path": "all_prompt_engineering_techniques/ethical-prompt-engineering.ipynb",
    "chars": 24137,
    "preview": "{\n \"cells\": [\n  {\n   \"cell_type\": \"markdown\",\n   \"metadata\": {},\n   \"source\": [\n    \"# Ethical Considerations in Prompt "
  },
  {
    "path": "all_prompt_engineering_techniques/evaluating-prompt-effectiveness.ipynb",
    "chars": 37755,
    "preview": "{\n \"cells\": [\n  {\n   \"cell_type\": \"markdown\",\n   \"metadata\": {},\n   \"source\": [\n    \"# Evaluating Prompt Effectiveness\\n"
  },
  {
    "path": "all_prompt_engineering_techniques/few-shot-learning.ipynb",
    "chars": 14899,
    "preview": "{\n \"cells\": [\n  {\n   \"cell_type\": \"markdown\",\n   \"metadata\": {},\n   \"source\": [\n    \"# Few-Shot Learning and In-Context "
  },
  {
    "path": "all_prompt_engineering_techniques/instruction-engineering-notebook.ipynb",
    "chars": 26057,
    "preview": "{\n \"cells\": [\n  {\n   \"cell_type\": \"markdown\",\n   \"metadata\": {},\n   \"source\": [\n    \"# Instruction Engineering Tutorial\\"
  },
  {
    "path": "all_prompt_engineering_techniques/intro-prompt-engineering-lesson.ipynb",
    "chars": 21348,
    "preview": "{\n \"cells\": [\n  {\n   \"cell_type\": \"markdown\",\n   \"metadata\": {},\n   \"source\": [\n    \"# Introduction to Prompt Engineerin"
  },
  {
    "path": "all_prompt_engineering_techniques/multilingual-prompting.ipynb",
    "chars": 19343,
    "preview": "{\n \"cells\": [\n  {\n   \"cell_type\": \"markdown\",\n   \"metadata\": {},\n   \"source\": [\n    \"# Multilingual and Cross-lingual Pr"
  },
  {
    "path": "all_prompt_engineering_techniques/negative-prompting.ipynb",
    "chars": 11646,
    "preview": "{\n \"cells\": [\n  {\n   \"cell_type\": \"markdown\",\n   \"metadata\": {},\n   \"source\": [\n    \"# Negative Prompting and Avoiding U"
  },
  {
    "path": "all_prompt_engineering_techniques/prompt-chaining-sequencing.ipynb",
    "chars": 23135,
    "preview": "{\n \"cells\": [\n  {\n   \"cell_type\": \"markdown\",\n   \"metadata\": {},\n   \"source\": [\n    \"# Prompt Chaining and Sequencing Tu"
  },
  {
    "path": "all_prompt_engineering_techniques/prompt-formatting-structure.ipynb",
    "chars": 32394,
    "preview": "{\n \"cells\": [\n  {\n   \"cell_type\": \"markdown\",\n   \"metadata\": {},\n   \"source\": [\n    \"# Prompt Formatting and Structure T"
  },
  {
    "path": "all_prompt_engineering_techniques/prompt-length-complexity-management.ipynb",
    "chars": 27049,
    "preview": "{\n \"cells\": [\n  {\n   \"cell_type\": \"markdown\",\n   \"metadata\": {},\n   \"source\": [\n    \"# Prompt Length and Complexity Mana"
  },
  {
    "path": "all_prompt_engineering_techniques/prompt-optimization-techniques.ipynb",
    "chars": 17281,
    "preview": "{\n \"cells\": [\n  {\n   \"cell_type\": \"markdown\",\n   \"metadata\": {},\n   \"source\": [\n    \"# Prompt Optimization Techniques\\n\""
  },
  {
    "path": "all_prompt_engineering_techniques/prompt-security-and-safety.ipynb",
    "chars": 17927,
    "preview": "{\n \"cells\": [\n  {\n   \"cell_type\": \"markdown\",\n   \"metadata\": {},\n   \"source\": [\n    \"# Prompt Security and Safety Tutori"
  },
  {
    "path": "all_prompt_engineering_techniques/prompt-templates-variables-jinja2.ipynb",
    "chars": 24716,
    "preview": "{\n \"cells\": [\n  {\n   \"cell_type\": \"markdown\",\n   \"metadata\": {},\n   \"source\": [\n    \"# Prompt Templates and Variables Tu"
  },
  {
    "path": "all_prompt_engineering_techniques/role-prompting.ipynb",
    "chars": 20482,
    "preview": "{\n \"cells\": [\n  {\n   \"cell_type\": \"markdown\",\n   \"metadata\": {},\n   \"source\": [\n    \"# Role Prompting Tutorial\\n\",\n    \""
  },
  {
    "path": "all_prompt_engineering_techniques/self-consistency.ipynb",
    "chars": 37302,
    "preview": "{\n \"cells\": [\n  {\n   \"cell_type\": \"markdown\",\n   \"metadata\": {},\n   \"source\": [\n    \"# Self-Consistency and Multiple Pat"
  },
  {
    "path": "all_prompt_engineering_techniques/specific-task-prompts.ipynb",
    "chars": 13840,
    "preview": "{\n \"cells\": [\n  {\n   \"cell_type\": \"markdown\",\n   \"metadata\": {},\n   \"source\": [\n    \"# Prompts for Specific Tasks\\n\",\n  "
  },
  {
    "path": "all_prompt_engineering_techniques/task-decomposition-prompts.ipynb",
    "chars": 23439,
    "preview": "{\n \"cells\": [\n  {\n   \"cell_type\": \"markdown\",\n   \"metadata\": {},\n   \"source\": [\n    \"# Task Decomposition in Prompts Tut"
  },
  {
    "path": "all_prompt_engineering_techniques/zero-shot-prompting.ipynb",
    "chars": 17911,
    "preview": "{\n \"cells\": [\n  {\n   \"cell_type\": \"markdown\",\n   \"metadata\": {},\n   \"source\": [\n    \"# Zero-Shot Prompting Tutorial\\n\",\n"
  },
  {
    "path": "requirements.txt",
    "chars": 1701,
    "preview": "aiohappyeyeballs==2.4.3\naiohttp==3.10.9\naiosignal==1.3.1\nannotated-types==0.7.0\nanyio==4.6.0\nasttokens==2.4.1\nattrs==24."
  }
]

About this extraction

This page contains the full source code of the NirDiamant/Prompt_Engineering GitHub repository, extracted and formatted as plain text for AI agents and large language models (LLMs). The extraction includes 27 files (510.6 KB), approximately 125.4k tokens. Use this with OpenClaw, Claude, ChatGPT, Cursor, Windsurf, or any other AI tool that accepts text input. You can copy the full output to your clipboard or download it as a .txt file.

Extracted by GitExtract — free GitHub repo to text converter for AI. Built by Nikandr Surkov.

Copied to clipboard!