├── .gitignore ├── LICENSE ├── README.md ├── anthropic_api_fundamentals ├── 01_getting_started.ipynb ├── 02_messages_format.ipynb ├── 03_models.ipynb ├── 04_parameters.ipynb ├── 05_Streaming.ipynb ├── 06_vision.ipynb ├── README.md ├── images │ ├── alternating_messages.png │ ├── benchmarks.png │ ├── claude_streaming.gif │ ├── content_block_streaming.png │ ├── image_and_text_prompt.png │ ├── image_message_format.png │ ├── model_speeds.png │ ├── models.png │ ├── output_length.png │ ├── research_paper │ │ ├── page1.png │ │ ├── page2.png │ │ ├── page3.png │ │ ├── page4.png │ │ └── page5.png │ ├── signup.png │ ├── speed_comparison.png │ ├── streaming_chat_exercise.gif │ ├── streaming_output.png │ ├── streaming_tokens.png │ ├── temperature.png │ └── temperature_plot.png └── prompting_images │ ├── animal1.png │ ├── animal2.png │ ├── animal3.png │ ├── people.png │ ├── slide1.png │ ├── slide2.png │ ├── slide3.png │ └── uh_oh.png ├── prompt_engineering_interactive_tutorial ├── AmazonBedrock │ ├── CONTRIBUTING.md │ ├── LICENSE │ ├── README.md │ ├── anthropic │ │ ├── 00_Tutorial_How-To.ipynb │ │ ├── 01_Basic_Prompt_Structure.ipynb │ │ ├── 02_Being_Clear_and_Direct.ipynb │ │ ├── 03_Assigning_Roles_Role_Prompting.ipynb │ │ ├── 04_Separating_Data_and_Instructions.ipynb │ │ ├── 05_Formatting_Output_and_Speaking_for_Claude.ipynb │ │ ├── 06_Precognition_Thinking_Step_by_Step.ipynb │ │ ├── 07_Using_Examples _Few-Shot_Prompting.ipynb │ │ ├── 08_Avoiding_Hallucinations.ipynb │ │ ├── 09_Complex_Prompts_from_Scratch.ipynb │ │ ├── 10_1_Appendix_Chaining_Prompts.ipynb │ │ ├── 10_2_Appendix_Tool_Use.ipynb │ │ ├── 10_3_Appendix_Empirical_Performance_Evaluations.ipynb │ │ └── 10_4_Appendix_Search_and_Retrieval.ipynb │ ├── boto3 │ │ ├── 00_Tutorial_How-To.ipynb │ │ ├── 01_Basic_Prompt_Structure.ipynb │ │ ├── 02_Being_Clear_and_Direct.ipynb │ │ ├── 03_Assigning_Roles_Role_Prompting.ipynb │ │ ├── 04_Separating_Data_and_Instructions.ipynb │ │ ├── 05_Formatting_Output_and_Speaking_for_Claude.ipynb │ │ ├── 06_Precognition_Thinking_Step_by_Step.ipynb │ │ ├── 07_Using_Examples_Few-Shot_Prompting.ipynb │ │ ├── 08_Avoiding_Hallucinations.ipynb │ │ ├── 09_Complex_Prompts_from_Scratch.ipynb │ │ ├── 10_1_Appendix_Chaining_Prompts.ipynb │ │ ├── 10_2_Appendix_Tool_Use.ipynb │ │ ├── 10_3_Appendix_Empirical_Performance_Eval.ipynb │ │ └── 10_4_Appendix_Search_and_Retrieval.ipynb │ ├── cloudformation │ │ └── workshop-v1-final-cfn.yml │ ├── requirements.txt │ └── utils │ │ ├── __init__.py │ │ └── hints.py ├── Anthropic 1P │ ├── 00_Tutorial_How-To.ipynb │ ├── 01_Basic_Prompt_Structure.ipynb │ ├── 02_Being_Clear_and_Direct.ipynb │ ├── 03_Assigning_Roles_Role_Prompting.ipynb │ ├── 04_Separating_Data_and_Instructions.ipynb │ ├── 05_Formatting_Output_and_Speaking_for_Claude.ipynb │ ├── 06_Precognition_Thinking_Step_by_Step.ipynb │ ├── 07_Using_Examples_Few-Shot_Prompting.ipynb │ ├── 08_Avoiding_Hallucinations.ipynb │ ├── 09_Complex_Prompts_from_Scratch.ipynb │ ├── 10.1_Appendix_Chaining Prompts.ipynb │ ├── 10.2_Appendix_Tool Use.ipynb │ ├── 10.3_Appendix_Search & Retrieval.ipynb │ └── hints.py └── README.md ├── prompt_evaluations ├── 01_intro_to_evals │ └── 01_intro_to_evals.ipynb ├── 02_workbench_evals │ └── 02_workbench_evals.ipynb ├── 03_code_graded_evals │ └── 03_code_graded.ipynb ├── 04_code_graded_classification_evals │ └── 04_code_graded_classification_evals.ipynb ├── 05_prompt_foo_code_graded_animals │ ├── README.md │ ├── animal_legs_tests.csv │ ├── images │ │ ├── details.png │ │ ├── eval_output.png │ │ ├── eval_output1.png │ │ ├── eval_results1.png │ │ ├── eval_view.png │ │ ├── eval_view1.png │ │ ├── final_view.png │ │ ├── multi_model_eval_view.png │ │ ├── prompt_foo.png │ │ ├── three_prompt_eval.png │ │ └── toolbar.png │ ├── lesson.ipynb │ ├── package-lock.json │ ├── package.json │ ├── promptfooconfig.yaml │ ├── prompts.py │ └── transform.py ├── 06_prompt_foo_code_graded_classification │ ├── README.md │ ├── dataset.csv │ ├── images │ │ ├── eval_output.png │ │ ├── output_row.png │ │ └── web_view.png │ ├── lesson.ipynb │ ├── promptfooconfig.yaml │ └── prompts.py ├── 07_prompt_foo_custom_graders │ ├── README.md │ ├── count.py │ ├── images │ │ ├── final_eval.png │ │ ├── final_view.png │ │ ├── initial_eval_output.png │ │ ├── single_row.png │ │ ├── tweezers_haiku_closeup.png │ │ └── tweezers_sonnet_closeup.png │ ├── lesson.ipynb │ └── promptfooconfig.yaml ├── 08_prompt_foo_model_graded │ ├── README.md │ ├── images │ │ ├── details1.png │ │ ├── details2.png │ │ ├── eval1.png │ │ ├── eval2.png │ │ ├── eval3.png │ │ ├── soccer_players.png │ │ └── web_view.png │ ├── lesson.ipynb │ └── promptfooconfig.yaml ├── 09_custom_model_graded_prompt_foo │ ├── README.md │ ├── articles │ │ ├── article1.txt │ │ ├── article2.txt │ │ ├── article3.txt │ │ ├── article4.txt │ │ ├── article5.txt │ │ ├── article6.txt │ │ ├── article7.txt │ │ └── article8.txt │ ├── custom_llm_eval.py │ ├── images │ │ ├── distribution.png │ │ ├── eval_result.png │ │ ├── explanation.png │ │ ├── overall_scores.png │ │ └── web_view.png │ ├── lesson.ipynb │ ├── promptfooconfig.yaml │ └── prompts.py ├── README.md └── images │ ├── adding_variables.png │ ├── benchmarks.png │ ├── comparison.png │ ├── empty_workbench.png │ ├── eval_diagram.png │ ├── evaluate1.png │ ├── evaluate2.png │ ├── evaluate3.png │ ├── evaluate4.png │ ├── evaluate5.png │ ├── evaluate6.png │ ├── evaluate_button.png │ ├── first_output.png │ ├── process.png │ ├── run_remaining.png │ ├── score.png │ ├── updated_prompt.png │ ├── updated_response.png │ ├── variables_button.png │ └── workbench_with_prompt.png ├── real_world_prompting ├── 01_prompting_recap.ipynb ├── 02_medical_prompt.ipynb ├── 03_prompt_engineering.ipynb ├── 04_call_summarizer.ipynb ├── 05_customer_support_ai.ipynb ├── README.md └── images │ ├── console.png │ ├── prompt_eng_lifecycle.png │ ├── prompt_generator_input.png │ └── prompt_generator_output.png └── tool_use ├── 01_tool_use_overview.ipynb ├── 02_your_first_simple_tool.ipynb ├── 03_structured_outputs.ipynb ├── 04_complete_workflow.ipynb ├── 05_tool_choice.ipynb ├── 06_chatbot_with_multiple_tools.ipynb ├── README.md └── images ├── calculator_diagram.png ├── chat_diagram.png ├── chickens_calculator.png ├── conversation1.png ├── conversation10.png ├── conversation2.png ├── conversation3.png ├── conversation4.png ├── conversation5.png ├── conversation6.png ├── conversation7.png ├── conversation8.png ├── conversation9.png ├── db_tool.png ├── exercise_conversation.png ├── messages_diagram.png ├── research_reading.png ├── stock_tool.png ├── structured_response.png ├── tool_choice.png ├── tool_flow_diagram.png ├── tool_use_diagram.png ├── tool_use_examples.png ├── tool_use_flow.png ├── wiki_diagram.png ├── wiki_messages.png └── wikipedia_diagram.png /.gitignore: -------------------------------------------------------------------------------- 1 | # Byte-compiled / optimized / DLL files 2 | __pycache__/ 3 | *.py[cod] 4 | *$py.class 5 | 6 | # C extensions 7 | *.so 8 | 9 | # Distribution / packaging 10 | .Python 11 | build/ 12 | develop-eggs/ 13 | dist/ 14 | downloads/ 15 | eggs/ 16 | .eggs/ 17 | lib/ 18 | lib64/ 19 | parts/ 20 | sdist/ 21 | var/ 22 | wheels/ 23 | pip-wheel-metadata/ 24 | share/python-wheels/ 25 | *.egg-info/ 26 | .installed.cfg 27 | *.egg 28 | node_modules/ 29 | MANIFEST 30 | 31 | # PyInstaller 32 | # Usually these files are written by a python script from a template 33 | # before PyInstaller builds the exe, so as to inject date/other infos into it. 34 | *.manifest 35 | *.spec 36 | 37 | # Installer logs 38 | pip-log.txt 39 | pip-delete-this-directory.txt 40 | 41 | # Unit test / coverage reports 42 | htmlcov/ 43 | .tox/ 44 | .nox/ 45 | .coverage 46 | .coverage.* 47 | .cache 48 | nosetests.xml 49 | coverage.xml 50 | *.cover 51 | *.py,cover 52 | .hypothesis/ 53 | .pytest_cache/ 54 | 55 | # Translations 56 | *.mo 57 | *.pot 58 | 59 | # Django stuff: 60 | *.log 61 | local_settings.py 62 | db.sqlite3 63 | db.sqlite3-journal 64 | 65 | # Flask stuff: 66 | instance/ 67 | .webassets-cache 68 | 69 | # Scrapy stuff: 70 | .scrapy 71 | 72 | # Sphinx documentation 73 | docs/_build/ 74 | 75 | # PyBuilder 76 | target/ 77 | 78 | # Jupyter Notebook 79 | .ipynb_checkpoints 80 | 81 | # IPython 82 | profile_default/ 83 | ipython_config.py 84 | 85 | # pyenv 86 | .python-version 87 | 88 | # pipenv 89 | # According to pypa/pipenv#598, it is recommended to include Pipfile.lock in version control. 90 | # However, in case of collaboration, if having platform-specific dependencies or dependencies 91 | # having no cross-platform support, pipenv may install dependencies that don't work, or not 92 | # install all needed dependencies. 93 | #Pipfile.lock 94 | 95 | # PEP 582; used by e.g. github.com/David-OConnor/pyflow 96 | __pypackages__/ 97 | 98 | # Celery stuff 99 | celerybeat-schedule 100 | celerybeat.pid 101 | 102 | # SageMath parsed files 103 | *.sage.py 104 | 105 | # Environments 106 | .env 107 | .venv 108 | env/ 109 | venv/ 110 | myenv/ 111 | ENV/ 112 | env.bak/ 113 | venv.bak/ 114 | pyvenv.cfg 115 | share/ 116 | bin/ 117 | 118 | # Spyder project settings 119 | .spyderproject 120 | .spyproject 121 | 122 | # Rope project settings 123 | .ropeproject 124 | 125 | # mkdocs documentation 126 | /site 127 | 128 | # mypy 129 | .mypy_cache/ 130 | .dmypy.json 131 | dmypy.json 132 | 133 | # Pyre type checker 134 | .pyre/ 135 | 136 | # Data 137 | *transactions*.jsonl 138 | /examples/data/transactions* 139 | *.DS_Store 140 | tmp_* 141 | examples/fine-tuned_qa/local_cache/* -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # Anthropic courses 2 | 3 | Welcome to Anthropic's educational courses. This repository currently contains five courses. We suggest completing the courses in the following order: 4 | 5 | 1. [Anthropic API fundamentals](./anthropic_api_fundamentals/README.md) - teaches the essentials of working with the Claude SDK: getting an API key, working with model parameters, writing multimodal prompts, streaming responses, etc. 6 | 2. [Prompt engineering interactive tutorial](./prompt_engineering_interactive_tutorial/README.md) - a comprehensive step-by-step guide to key prompting techniques. [[AWS Workshop version](https://catalog.us-east-1.prod.workshops.aws/workshops/0644c9e9-5b82-45f2-8835-3b5aa30b1848/en-US)] 7 | 3. [Real world prompting](./real_world_prompting/README.md) - learn how to incorporate prompting techniques into complex, real world prompts. [[Google Vertex version](https://github.com/anthropics/courses/tree/vertex/real_world_prompting)] 8 | 4. [Prompt evaluations](./prompt_evaluations/README.md) - learn how to write production prompt evaluations to measure the quality of your prompts. 9 | 5. [Tool use](./tool_use/README.md) - teaches everything you need to know to implement tool use successfully in your workflows with Claude. 10 | 11 | **Please note that these courses often favor our lowest-cost model, Claude 3 Haiku, to keep API costs down for students following along with the materials. Feel free to use other Claude models if you prefer.** -------------------------------------------------------------------------------- /anthropic_api_fundamentals/README.md: -------------------------------------------------------------------------------- 1 | # Anthropic API fundamentals 2 | A series of notebook tutorials that cover the essentials of working with Claude models and the Anthropic SDK including: 3 | 4 | * [Getting an API key and making simple requests](./01_getting_started.ipynb) 5 | * [Working with the messages format](./02_messages_format.ipynb) 6 | * [Comparing capabilities and performance of the Claude model family](./03_models.ipynb) 7 | * [Understanding model parameters](./04_parameters.ipynb) 8 | * [Working with streaming responses](./05_Streaming.ipynb) 9 | * [Vision prompting](./06_vision.ipynb) 10 | -------------------------------------------------------------------------------- /anthropic_api_fundamentals/images/alternating_messages.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/anthropics/courses/b4f26aedef55e06ad5eead5de83985249d1fab2f/anthropic_api_fundamentals/images/alternating_messages.png -------------------------------------------------------------------------------- /anthropic_api_fundamentals/images/benchmarks.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/anthropics/courses/b4f26aedef55e06ad5eead5de83985249d1fab2f/anthropic_api_fundamentals/images/benchmarks.png -------------------------------------------------------------------------------- /anthropic_api_fundamentals/images/claude_streaming.gif: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/anthropics/courses/b4f26aedef55e06ad5eead5de83985249d1fab2f/anthropic_api_fundamentals/images/claude_streaming.gif -------------------------------------------------------------------------------- /anthropic_api_fundamentals/images/content_block_streaming.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/anthropics/courses/b4f26aedef55e06ad5eead5de83985249d1fab2f/anthropic_api_fundamentals/images/content_block_streaming.png -------------------------------------------------------------------------------- /anthropic_api_fundamentals/images/image_and_text_prompt.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/anthropics/courses/b4f26aedef55e06ad5eead5de83985249d1fab2f/anthropic_api_fundamentals/images/image_and_text_prompt.png -------------------------------------------------------------------------------- /anthropic_api_fundamentals/images/image_message_format.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/anthropics/courses/b4f26aedef55e06ad5eead5de83985249d1fab2f/anthropic_api_fundamentals/images/image_message_format.png -------------------------------------------------------------------------------- /anthropic_api_fundamentals/images/model_speeds.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/anthropics/courses/b4f26aedef55e06ad5eead5de83985249d1fab2f/anthropic_api_fundamentals/images/model_speeds.png -------------------------------------------------------------------------------- /anthropic_api_fundamentals/images/models.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/anthropics/courses/b4f26aedef55e06ad5eead5de83985249d1fab2f/anthropic_api_fundamentals/images/models.png -------------------------------------------------------------------------------- /anthropic_api_fundamentals/images/output_length.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/anthropics/courses/b4f26aedef55e06ad5eead5de83985249d1fab2f/anthropic_api_fundamentals/images/output_length.png -------------------------------------------------------------------------------- /anthropic_api_fundamentals/images/research_paper/page1.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/anthropics/courses/b4f26aedef55e06ad5eead5de83985249d1fab2f/anthropic_api_fundamentals/images/research_paper/page1.png -------------------------------------------------------------------------------- /anthropic_api_fundamentals/images/research_paper/page2.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/anthropics/courses/b4f26aedef55e06ad5eead5de83985249d1fab2f/anthropic_api_fundamentals/images/research_paper/page2.png -------------------------------------------------------------------------------- /anthropic_api_fundamentals/images/research_paper/page3.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/anthropics/courses/b4f26aedef55e06ad5eead5de83985249d1fab2f/anthropic_api_fundamentals/images/research_paper/page3.png -------------------------------------------------------------------------------- /anthropic_api_fundamentals/images/research_paper/page4.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/anthropics/courses/b4f26aedef55e06ad5eead5de83985249d1fab2f/anthropic_api_fundamentals/images/research_paper/page4.png -------------------------------------------------------------------------------- /anthropic_api_fundamentals/images/research_paper/page5.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/anthropics/courses/b4f26aedef55e06ad5eead5de83985249d1fab2f/anthropic_api_fundamentals/images/research_paper/page5.png -------------------------------------------------------------------------------- /anthropic_api_fundamentals/images/signup.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/anthropics/courses/b4f26aedef55e06ad5eead5de83985249d1fab2f/anthropic_api_fundamentals/images/signup.png -------------------------------------------------------------------------------- /anthropic_api_fundamentals/images/speed_comparison.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/anthropics/courses/b4f26aedef55e06ad5eead5de83985249d1fab2f/anthropic_api_fundamentals/images/speed_comparison.png -------------------------------------------------------------------------------- /anthropic_api_fundamentals/images/streaming_chat_exercise.gif: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/anthropics/courses/b4f26aedef55e06ad5eead5de83985249d1fab2f/anthropic_api_fundamentals/images/streaming_chat_exercise.gif -------------------------------------------------------------------------------- /anthropic_api_fundamentals/images/streaming_output.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/anthropics/courses/b4f26aedef55e06ad5eead5de83985249d1fab2f/anthropic_api_fundamentals/images/streaming_output.png -------------------------------------------------------------------------------- /anthropic_api_fundamentals/images/streaming_tokens.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/anthropics/courses/b4f26aedef55e06ad5eead5de83985249d1fab2f/anthropic_api_fundamentals/images/streaming_tokens.png -------------------------------------------------------------------------------- /anthropic_api_fundamentals/images/temperature.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/anthropics/courses/b4f26aedef55e06ad5eead5de83985249d1fab2f/anthropic_api_fundamentals/images/temperature.png -------------------------------------------------------------------------------- /anthropic_api_fundamentals/images/temperature_plot.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/anthropics/courses/b4f26aedef55e06ad5eead5de83985249d1fab2f/anthropic_api_fundamentals/images/temperature_plot.png -------------------------------------------------------------------------------- /anthropic_api_fundamentals/prompting_images/animal1.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/anthropics/courses/b4f26aedef55e06ad5eead5de83985249d1fab2f/anthropic_api_fundamentals/prompting_images/animal1.png -------------------------------------------------------------------------------- /anthropic_api_fundamentals/prompting_images/animal2.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/anthropics/courses/b4f26aedef55e06ad5eead5de83985249d1fab2f/anthropic_api_fundamentals/prompting_images/animal2.png -------------------------------------------------------------------------------- /anthropic_api_fundamentals/prompting_images/animal3.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/anthropics/courses/b4f26aedef55e06ad5eead5de83985249d1fab2f/anthropic_api_fundamentals/prompting_images/animal3.png -------------------------------------------------------------------------------- /anthropic_api_fundamentals/prompting_images/people.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/anthropics/courses/b4f26aedef55e06ad5eead5de83985249d1fab2f/anthropic_api_fundamentals/prompting_images/people.png -------------------------------------------------------------------------------- /anthropic_api_fundamentals/prompting_images/slide1.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/anthropics/courses/b4f26aedef55e06ad5eead5de83985249d1fab2f/anthropic_api_fundamentals/prompting_images/slide1.png -------------------------------------------------------------------------------- /anthropic_api_fundamentals/prompting_images/slide2.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/anthropics/courses/b4f26aedef55e06ad5eead5de83985249d1fab2f/anthropic_api_fundamentals/prompting_images/slide2.png -------------------------------------------------------------------------------- /anthropic_api_fundamentals/prompting_images/slide3.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/anthropics/courses/b4f26aedef55e06ad5eead5de83985249d1fab2f/anthropic_api_fundamentals/prompting_images/slide3.png -------------------------------------------------------------------------------- /anthropic_api_fundamentals/prompting_images/uh_oh.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/anthropics/courses/b4f26aedef55e06ad5eead5de83985249d1fab2f/anthropic_api_fundamentals/prompting_images/uh_oh.png -------------------------------------------------------------------------------- /prompt_engineering_interactive_tutorial/AmazonBedrock/CONTRIBUTING.md: -------------------------------------------------------------------------------- 1 | # Contributing Guidelines 2 | 3 | Thank you for your interest in contributing to our project. Whether it's a bug report, new feature, correction, or additional 4 | documentation, we greatly value feedback and contributions from our community. 5 | 6 | Please read through this document before submitting any issues or pull requests to ensure we have all the necessary 7 | information to effectively respond to your bug report or contribution. 8 | 9 | 10 | ## Reporting Bugs/Feature Requests 11 | 12 | We welcome you to use the GitHub issue tracker to report bugs or suggest features. 13 | 14 | When filing an issue, please check existing open, or recently closed, issues to make sure somebody else hasn't already 15 | reported the issue. Please try to include as much information as you can. Details like these are incredibly useful: 16 | 17 | * A reproducible test case or series of steps 18 | * The version of our code being used 19 | * Any modifications you've made relevant to the bug 20 | * Anything unusual about your environment or deployment 21 | 22 | 23 | ## Contributing via Pull Requests 24 | Contributions via pull requests are much appreciated. Before sending us a pull request, please ensure that: 25 | 26 | 1. You are working against the latest source on the *main* branch. 27 | 2. You check existing open, and recently merged, pull requests to make sure someone else hasn't addressed the problem already. 28 | 3. You open an issue to discuss any significant work - we would hate for your time to be wasted. 29 | 30 | To send us a pull request, please: 31 | 32 | 1. Fork the repository. 33 | 2. Modify the source; please focus on the specific change you are contributing. If you also reformat all the code, it will be hard for us to focus on your change. 34 | 3. Ensure local tests pass. 35 | 4. Commit to your fork using clear commit messages. 36 | 5. Send us a pull request, answering any default questions in the pull request interface. 37 | 6. Pay attention to any automated CI failures reported in the pull request, and stay involved in the conversation. 38 | 39 | GitHub provides additional document on [forking a repository](https://help.github.com/articles/fork-a-repo/) and 40 | [creating a pull request](https://help.github.com/articles/creating-a-pull-request/). 41 | 42 | 43 | ## Finding contributions to work on 44 | Looking at the existing issues is a great way to find something to contribute on. As our projects, by default, use the default GitHub issue labels (enhancement/bug/duplicate/help wanted/invalid/question/wontfix), looking at any 'help wanted' issues is a great place to start. 45 | 46 | 47 | ## Code of Conduct 48 | This project has adopted the [Amazon Open Source Code of Conduct](https://aws.github.io/code-of-conduct). 49 | For more information see the [Code of Conduct FAQ](https://aws.github.io/code-of-conduct-faq) or contact 50 | opensource-codeofconduct@amazon.com with any additional questions or comments. 51 | 52 | 53 | ## Security issue notifications 54 | If you discover a potential security issue in this project we ask that you notify AWS/Amazon Security via our [vulnerability reporting page](http://aws.amazon.com/security/vulnerability-reporting/). Please do **not** create a public github issue. 55 | 56 | 57 | ## Licensing 58 | 59 | See the [LICENSE](LICENSE) file for our project's licensing. We will ask you to confirm the licensing of your contribution. 60 | -------------------------------------------------------------------------------- /prompt_engineering_interactive_tutorial/AmazonBedrock/LICENSE: -------------------------------------------------------------------------------- 1 | MIT No Attribution 2 | 3 | Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved. 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy of 6 | this software and associated documentation files (the "Software"), to deal in 7 | the Software without restriction, including without limitation the rights to 8 | use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of 9 | the Software, and to permit persons to whom the Software is furnished to do so. 10 | 11 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 12 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS 13 | FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR 14 | COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER 15 | IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN 16 | CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. 17 | 18 | -------------------------------------------------------------------------------- /prompt_engineering_interactive_tutorial/AmazonBedrock/README.md: -------------------------------------------------------------------------------- 1 | # Welcome to Anthropic's Prompt Engineering Interactive Tutorial - Bedrock Edition 2 | 3 | ## Course introduction and goals 4 | 5 | This course is intended to provide you with a comprehensive step-by-step understanding of how to engineer optimal prompts within Claude, using Bedrock. 6 | 7 | **After completing this course, you will be able to**: 8 | - Master the basic structure of a good prompt 9 | - Recognize common failure modes and learn the '80/20' techniques to address them 10 | - Understand Claude's strengths and weaknesses 11 | - Build strong prompts from scratch for common use cases 12 | 13 | ## Course structure and content 14 | 15 | This course is structured to allow you many chances to practice writing and troubleshooting prompts yourself. The course is broken up into **9 chapters with accompanying exercises**, as well as an appendix of even more advanced methods. It is intended for you to **work through the course in chapter order**. 16 | 17 | **Each lesson has an "Example Playground" area** at the bottom where you are free to experiment with the examples in the lesson and see for yourself how changing prompts can change Claude's responses. There is also an [answer key](https://docs.google.com/spreadsheets/d/1jIxjzUWG-6xBVIa2ay6yDpLyeuOh_hR_ZB75a47KX_E/edit?usp=sharing). While this answer key is structured for 1P API requests, the solutions are the same. 18 | 19 | Note: This tutorial uses our smallest, fastest, and cheapest model, Claude 3 Haiku. Anthropic has [two other models](https://docs.anthropic.com/claude/docs/models-overview), Claude 3 Sonnet and Claude 3 Opus, which are more intelligent than Haiku, with Opus being the most intelligent. 20 | 21 | When you are ready to begin, go to `01_Basic Prompt Structure` to proceed. 22 | 23 | ## Table of Contents 24 | 25 | Each chapter consists of a lesson and a set of exercises. 26 | 27 | ### Beginner 28 | - **Chapter 1:** Basic Prompt Structure 29 | 30 | - **Chapter 2:** Being Clear and Direct 31 | 32 | - **Chapter 3:** Assigning Roles 33 | 34 | ### Intermediate 35 | - **Chapter 4:** Separating Data from Instructions 36 | 37 | - **Chapter 5:** Formatting Output & Speaking for Claude 38 | 39 | - **Chapter 6:** Precognition (Thinking Step by Step) 40 | 41 | - **Chapter 7:** Using Examples 42 | 43 | ### Advanced 44 | - **Chapter 8:** Avoiding Hallucinations 45 | 46 | - **Chapter 9:** Building Complex Prompts (Industry Use Cases) 47 | - Complex Prompts from Scratch - Chatbot 48 | - Complex Prompts for Legal Services 49 | - **Exercise:** Complex Prompts for Financial Services 50 | - **Exercise:** Complex Prompts for Coding 51 | - Congratulations & Next Steps 52 | 53 | - **Appendix:** Beyond Standard Prompting 54 | - Chaining Prompts 55 | - Tool Use 56 | - Empriical Performance Evaluations 57 | - Search & Retrieval -------------------------------------------------------------------------------- /prompt_engineering_interactive_tutorial/AmazonBedrock/anthropic/00_Tutorial_How-To.ipynb: -------------------------------------------------------------------------------- 1 | { 2 | "cells": [ 3 | { 4 | "cell_type": "markdown", 5 | "metadata": {}, 6 | "source": [ 7 | "# Tutorial How-To\n", 8 | "\n", 9 | "This tutorial requires this initial notebook to be run first so that the requirements and environment variables are stored for all notebooks in the workshop" 10 | ] 11 | }, 12 | { 13 | "cell_type": "markdown", 14 | "metadata": {}, 15 | "source": [ 16 | "## How to get started\n", 17 | "\n", 18 | "1. Clone this repository to your local machine.\n", 19 | "\n", 20 | "2. Install the required dependencies by running the following command:\n", 21 | " " 22 | ] 23 | }, 24 | { 25 | "cell_type": "code", 26 | "execution_count": 2, 27 | "metadata": {}, 28 | "outputs": [ 29 | { 30 | "name": "stdout", 31 | "output_type": "stream", 32 | "text": [ 33 | "Note: you may need to restart the kernel to use updated packages.\n", 34 | "Note: you may need to restart the kernel to use updated packages.\n" 35 | ] 36 | } 37 | ], 38 | "source": [ 39 | "%pip install -qU pip\n", 40 | "%pip install -qr ../requirements.txt" 41 | ] 42 | }, 43 | { 44 | "cell_type": "markdown", 45 | "metadata": {}, 46 | "source": [ 47 | "3. Restart the kernel after installing dependencies" 48 | ] 49 | }, 50 | { 51 | "cell_type": "code", 52 | "execution_count": null, 53 | "metadata": {}, 54 | "outputs": [], 55 | "source": [ 56 | "# restart kernel\n", 57 | "from IPython.core.display import HTML\n", 58 | "HTML(\"\")" 59 | ] 60 | }, 61 | { 62 | "cell_type": "markdown", 63 | "metadata": {}, 64 | "source": [ 65 | "---\n", 66 | "\n", 67 | "## Usage Notes & Tips 💡\n", 68 | "\n", 69 | "- This course uses Claude 3 Haiku with temperature 0. We will talk more about temperature later in the course. For now, it's enough to understand that these settings yield more deterministic results. All prompt engineering techniques in this course also apply to previous generation legacy Claude models such as Claude 2 and Claude Instant 1.2.\n", 70 | "\n", 71 | "- You can use `Shift + Enter` to execute the cell and move to the next one.\n", 72 | "\n", 73 | "- When you reach the bottom of a tutorial page, navigate to the next numbered file in the folder, or to the next numbered folder if you're finished with the content within that chapter file.\n", 74 | "\n", 75 | "### The Anthropic SDK & the Messages API\n", 76 | "We will be using the [Anthropic python SDK](https://docs.anthropic.com/claude/reference/claude-on-amazon-bedrock) and the [Messages API](https://docs.anthropic.com/claude/reference/messages_post) throughout this tutorial.\n", 77 | "\n", 78 | "Below is an example of what running a prompt will look like in this tutorial." 79 | ] 80 | }, 81 | { 82 | "cell_type": "markdown", 83 | "metadata": {}, 84 | "source": [ 85 | "First, we set and store the model name and region." 86 | ] 87 | }, 88 | { 89 | "cell_type": "code", 90 | "execution_count": null, 91 | "metadata": {}, 92 | "outputs": [], 93 | "source": [ 94 | "import boto3\n", 95 | "session = boto3.Session() # create a boto3 session to dynamically get and set the region name\n", 96 | "AWS_REGION = session.region_name\n", 97 | "print(\"AWS Region:\", AWS_REGION)\n", 98 | "MODEL_NAME = \"anthropic.claude-3-haiku-20240307-v1:0\"\n", 99 | "\n", 100 | "%store MODEL_NAME\n", 101 | "%store AWS_REGION" 102 | ] 103 | }, 104 | { 105 | "cell_type": "markdown", 106 | "metadata": {}, 107 | "source": [ 108 | "Then, we create `get_completion`, which is a helper function that sends a prompt to Claude and returns Claude's generated response. Run that cell now." 109 | ] 110 | }, 111 | { 112 | "cell_type": "code", 113 | "execution_count": null, 114 | "metadata": {}, 115 | "outputs": [], 116 | "source": [ 117 | "from anthropic import AnthropicBedrock\n", 118 | "\n", 119 | "client = AnthropicBedrock(aws_region=AWS_REGION)\n", 120 | "\n", 121 | "def get_completion(prompt, system=''):\n", 122 | " message = client.messages.create(\n", 123 | " model=MODEL_NAME,\n", 124 | " max_tokens=2000,\n", 125 | " temperature=0.0,\n", 126 | " messages=[\n", 127 | " {\"role\": \"user\", \"content\": prompt}\n", 128 | " ],\n", 129 | " system=system\n", 130 | " )\n", 131 | " return message.content[0].text" 132 | ] 133 | }, 134 | { 135 | "cell_type": "markdown", 136 | "metadata": {}, 137 | "source": [ 138 | "Now we will write out an example prompt for Claude and print Claude's output by running our `get_completion` helper function. Running the cell below will print out a response from Claude beneath it.\n", 139 | "\n", 140 | "Feel free to play around with the prompt string to elicit different responses from Claude." 141 | ] 142 | }, 143 | { 144 | "cell_type": "code", 145 | "execution_count": null, 146 | "metadata": {}, 147 | "outputs": [], 148 | "source": [ 149 | "# Prompt\n", 150 | "prompt = \"Hello, Claude!\"\n", 151 | "\n", 152 | "# Get Claude's response\n", 153 | "print(get_completion(prompt))" 154 | ] 155 | }, 156 | { 157 | "cell_type": "markdown", 158 | "metadata": {}, 159 | "source": [ 160 | "The `MODEL_NAME` and `AWS_REGION` variables defined earlier will be used throughout the tutorial. Just make sure to run the cells for each tutorial page from top to bottom." 161 | ] 162 | } 163 | ], 164 | "metadata": { 165 | "kernelspec": { 166 | "display_name": "py310", 167 | "language": "python", 168 | "name": "python3" 169 | }, 170 | "language_info": { 171 | "codemirror_mode": { 172 | "name": "ipython", 173 | "version": 3 174 | }, 175 | "file_extension": ".py", 176 | "mimetype": "text/x-python", 177 | "name": "python", 178 | "nbconvert_exporter": "python", 179 | "pygments_lexer": "ipython3", 180 | "version": "3.11.5" 181 | } 182 | }, 183 | "nbformat": 4, 184 | "nbformat_minor": 2 185 | } 186 | -------------------------------------------------------------------------------- /prompt_engineering_interactive_tutorial/AmazonBedrock/anthropic/02_Being_Clear_and_Direct.ipynb: -------------------------------------------------------------------------------- 1 | { 2 | "cells": [ 3 | { 4 | "cell_type": "markdown", 5 | "metadata": {}, 6 | "source": [ 7 | "# Chapter 2: Being Clear and Direct\n", 8 | "\n", 9 | "- [Lesson](#lesson)\n", 10 | "- [Exercises](#exercises)\n", 11 | "- [Example Playground](#example-playground)\n", 12 | "\n", 13 | "## Setup\n", 14 | "\n", 15 | "Run the following setup cell to load your API key and establish the `get_completion` helper function." 16 | ] 17 | }, 18 | { 19 | "cell_type": "code", 20 | "execution_count": null, 21 | "metadata": {}, 22 | "outputs": [], 23 | "source": [ 24 | "%pip install anthropic --quiet\n", 25 | "\n", 26 | "# Import the hints module from the utils package\n", 27 | "import os\n", 28 | "import sys\n", 29 | "module_path = \"..\"\n", 30 | "sys.path.append(os.path.abspath(module_path))\n", 31 | "from utils import hints\n", 32 | "\n", 33 | "# Import python's built-in regular expression library\n", 34 | "import re\n", 35 | "from anthropic import AnthropicBedrock\n", 36 | "\n", 37 | "%store -r MODEL_NAME\n", 38 | "%store -r AWS_REGION\n", 39 | "\n", 40 | "client = AnthropicBedrock(aws_region=AWS_REGION)\n", 41 | "\n", 42 | "def get_completion(prompt, system=''):\n", 43 | " message = client.messages.create(\n", 44 | " model=MODEL_NAME,\n", 45 | " max_tokens=2000,\n", 46 | " temperature=0.0,\n", 47 | " messages=[\n", 48 | " {\"role\": \"user\", \"content\": prompt}\n", 49 | " ],\n", 50 | " system=system\n", 51 | " )\n", 52 | " return message.content[0].text" 53 | ] 54 | }, 55 | { 56 | "cell_type": "markdown", 57 | "metadata": {}, 58 | "source": [ 59 | "---\n", 60 | "\n", 61 | "## Lesson\n", 62 | "\n", 63 | "**Claude responds best to clear and direct instructions.**\n", 64 | "\n", 65 | "Think of Claude like any other human that is new to the job. **Claude has no context** on what to do aside from what you literally tell it. Just as when you instruct a human for the first time on a task, the more you explain exactly what you want in a straightforward manner to Claude, the better and more accurate Claude's response will be.\"\t\t\t\t\n", 66 | "\t\t\t\t\n", 67 | "When in doubt, follow the **Golden Rule of Clear Prompting**:\n", 68 | "- Show your prompt to a colleague or friend and have them follow the instructions themselves to see if they can produce the result you want. If they're confused, Claude's confused.\t\t\t\t" 69 | ] 70 | }, 71 | { 72 | "cell_type": "markdown", 73 | "metadata": {}, 74 | "source": [ 75 | "### Examples\n", 76 | "\n", 77 | "Let's take a task like writing poetry. (Ignore any syllable mismatch - LLMs aren't great at counting syllables yet.)" 78 | ] 79 | }, 80 | { 81 | "cell_type": "code", 82 | "execution_count": null, 83 | "metadata": {}, 84 | "outputs": [], 85 | "source": [ 86 | "# Prompt\n", 87 | "PROMPT = \"Write a haiku about robots.\"\n", 88 | "\n", 89 | "# Print Claude's response\n", 90 | "print(get_completion(PROMPT))" 91 | ] 92 | }, 93 | { 94 | "cell_type": "markdown", 95 | "metadata": {}, 96 | "source": [ 97 | "This haiku is nice enough, but users may want Claude to go directly into the poem without the \"Here is a haiku\" preamble.\n", 98 | "\n", 99 | "How do we achieve that? We **ask for it**!" 100 | ] 101 | }, 102 | { 103 | "cell_type": "code", 104 | "execution_count": null, 105 | "metadata": {}, 106 | "outputs": [], 107 | "source": [ 108 | "# Prompt\n", 109 | "PROMPT = \"Write a haiku about robots. Skip the preamble; go straight into the poem.\"\n", 110 | "\n", 111 | "# Print Claude's response\n", 112 | "print(get_completion(PROMPT))" 113 | ] 114 | }, 115 | { 116 | "cell_type": "markdown", 117 | "metadata": {}, 118 | "source": [ 119 | "Here's another example. Let's ask Claude who's the best basketball player of all time. You can see below that while Claude lists a few names, **it doesn't respond with a definitive \"best\"**." 120 | ] 121 | }, 122 | { 123 | "cell_type": "code", 124 | "execution_count": null, 125 | "metadata": {}, 126 | "outputs": [], 127 | "source": [ 128 | "# Prompt\n", 129 | "PROMPT = \"Who is the best basketball player of all time?\"\n", 130 | "\n", 131 | "# Print Claude's response\n", 132 | "print(get_completion(PROMPT))" 133 | ] 134 | }, 135 | { 136 | "cell_type": "markdown", 137 | "metadata": {}, 138 | "source": [ 139 | "Can we get Claude to make up its mind and decide on a best player? Yes! Just ask!" 140 | ] 141 | }, 142 | { 143 | "cell_type": "code", 144 | "execution_count": null, 145 | "metadata": {}, 146 | "outputs": [], 147 | "source": [ 148 | "# Prompt\n", 149 | "PROMPT = \"Who is the best basketball player of all time? Yes, there are differing opinions, but if you absolutely had to pick one player, who would it be?\"\n", 150 | "\n", 151 | "# Print Claude's response\n", 152 | "print(get_completion(PROMPT))" 153 | ] 154 | }, 155 | { 156 | "cell_type": "markdown", 157 | "metadata": {}, 158 | "source": [ 159 | "If you would like to experiment with the lesson prompts without changing any content above, scroll all the way to the bottom of the lesson notebook to visit the [**Example Playground**](#example-playground)." 160 | ] 161 | }, 162 | { 163 | "cell_type": "markdown", 164 | "metadata": {}, 165 | "source": [ 166 | "---\n", 167 | "\n", 168 | "## Exercises\n", 169 | "- [Exercise 2.1 - Spanish](#exercise-21---spanish)\n", 170 | "- [Exercise 2.2 - One Player Only](#exercise-22---one-player-only)\n", 171 | "- [Exercise 2.3 - Write a Story](#exercise-23---write-a-story)" 172 | ] 173 | }, 174 | { 175 | "cell_type": "markdown", 176 | "metadata": {}, 177 | "source": [ 178 | "### Exercise 2.1 - Spanish\n", 179 | "Modify the `SYSTEM_PROMPT` to make Claude output its answer in Spanish." 180 | ] 181 | }, 182 | { 183 | "cell_type": "code", 184 | "execution_count": null, 185 | "metadata": {}, 186 | "outputs": [], 187 | "source": [ 188 | "# System prompt - this is the only field you should chnage\n", 189 | "SYSTEM_PROMPT = \"[Replace this text]\"\n", 190 | "\n", 191 | "# Prompt\n", 192 | "PROMPT = \"Hello Claude, how are you?\"\n", 193 | "\n", 194 | "# Get Claude's response\n", 195 | "response = get_completion(PROMPT, SYSTEM_PROMPT)\n", 196 | "\n", 197 | "# Function to grade exercise correctness\n", 198 | "def grade_exercise(text):\n", 199 | " return \"hola\" in text.lower()\n", 200 | "\n", 201 | "# Print Claude's response and the corresponding grade\n", 202 | "print(response)\n", 203 | "print(\"\\n--------------------------- GRADING ---------------------------\")\n", 204 | "print(\"This exercise has been correctly solved:\", grade_exercise(response))" 205 | ] 206 | }, 207 | { 208 | "cell_type": "markdown", 209 | "metadata": {}, 210 | "source": [ 211 | "❓ If you want a hint, run the cell below!" 212 | ] 213 | }, 214 | { 215 | "cell_type": "code", 216 | "execution_count": null, 217 | "metadata": {}, 218 | "outputs": [], 219 | "source": [ 220 | "print(hints.exercise_2_1_hint)" 221 | ] 222 | }, 223 | { 224 | "cell_type": "markdown", 225 | "metadata": {}, 226 | "source": [ 227 | "### Exercise 2.2 - One Player Only\n", 228 | "\n", 229 | "Modify the `PROMPT` so that Claude doesn't equivocate at all and responds with **ONLY** the name of one specific player, with **no other words or punctuation**. " 230 | ] 231 | }, 232 | { 233 | "cell_type": "code", 234 | "execution_count": null, 235 | "metadata": {}, 236 | "outputs": [], 237 | "source": [ 238 | "# Prompt - this is the only field you should change\n", 239 | "PROMPT = \"[Replace this text]\"\n", 240 | "\n", 241 | "# Get Claude's response\n", 242 | "response = get_completion(PROMPT)\n", 243 | "\n", 244 | "# Function to grade exercise correctness\n", 245 | "def grade_exercise(text):\n", 246 | " return text == \"Michael Jordan\"\n", 247 | "\n", 248 | "# Print Claude's response and the corresponding grade\n", 249 | "print(response)\n", 250 | "print(\"\\n--------------------------- GRADING ---------------------------\")\n", 251 | "print(\"This exercise has been correctly solved:\", grade_exercise(response))" 252 | ] 253 | }, 254 | { 255 | "cell_type": "markdown", 256 | "metadata": {}, 257 | "source": [ 258 | "❓ If you want a hint, run the cell below!" 259 | ] 260 | }, 261 | { 262 | "cell_type": "code", 263 | "execution_count": null, 264 | "metadata": {}, 265 | "outputs": [], 266 | "source": [ 267 | "print(hints.exercise_2_2_hint)" 268 | ] 269 | }, 270 | { 271 | "cell_type": "markdown", 272 | "metadata": {}, 273 | "source": [ 274 | "### Exercise 2.3 - Write a Story\n", 275 | "\n", 276 | "Modify the `PROMPT` so that Claude responds with as long a response as you can muster. If your answer is **over 800 words**, Claude's response will be graded as correct." 277 | ] 278 | }, 279 | { 280 | "cell_type": "code", 281 | "execution_count": null, 282 | "metadata": {}, 283 | "outputs": [], 284 | "source": [ 285 | "# Prompt - this is the only field you should change\n", 286 | "PROMPT = \"[Replace this text]\"\n", 287 | "\n", 288 | "# Get Claude's response\n", 289 | "response = get_completion(PROMPT)\n", 290 | "\n", 291 | "# Function to grade exercise correctness\n", 292 | "def grade_exercise(text):\n", 293 | " trimmed = text.strip()\n", 294 | " words = len(trimmed.split())\n", 295 | " return words >= 800\n", 296 | "\n", 297 | "# Print Claude's response and the corresponding grade\n", 298 | "print(response)\n", 299 | "print(\"\\n--------------------------- GRADING ---------------------------\")\n", 300 | "print(\"This exercise has been correctly solved:\", grade_exercise(response))" 301 | ] 302 | }, 303 | { 304 | "cell_type": "markdown", 305 | "metadata": {}, 306 | "source": [ 307 | "❓ If you want a hint, run the cell below!" 308 | ] 309 | }, 310 | { 311 | "cell_type": "code", 312 | "execution_count": null, 313 | "metadata": {}, 314 | "outputs": [], 315 | "source": [ 316 | "print(hints.exercise_2_3_hint)" 317 | ] 318 | }, 319 | { 320 | "cell_type": "markdown", 321 | "metadata": {}, 322 | "source": [ 323 | "### Congrats!\n", 324 | "\n", 325 | "If you've solved all exercises up until this point, you're ready to move to the next chapter. Happy prompting!" 326 | ] 327 | }, 328 | { 329 | "cell_type": "markdown", 330 | "metadata": {}, 331 | "source": [ 332 | "---\n", 333 | "\n", 334 | "## Example Playground\n", 335 | "\n", 336 | "This is an area for you to experiment freely with the prompt examples shown in this lesson and tweak prompts to see how it may affect Claude's responses." 337 | ] 338 | }, 339 | { 340 | "cell_type": "code", 341 | "execution_count": null, 342 | "metadata": {}, 343 | "outputs": [], 344 | "source": [ 345 | "# Prompt\n", 346 | "PROMPT = \"Write a haiku about robots.\"\n", 347 | "\n", 348 | "# Print Claude's response\n", 349 | "print(get_completion(PROMPT))" 350 | ] 351 | }, 352 | { 353 | "cell_type": "code", 354 | "execution_count": null, 355 | "metadata": {}, 356 | "outputs": [], 357 | "source": [ 358 | "# Prompt\n", 359 | "PROMPT = \"Write a haiku about robots. Skip the preamble; go straight into the poem.\"\n", 360 | "\n", 361 | "# Print Claude's response\n", 362 | "print(get_completion(PROMPT))" 363 | ] 364 | }, 365 | { 366 | "cell_type": "code", 367 | "execution_count": null, 368 | "metadata": {}, 369 | "outputs": [], 370 | "source": [ 371 | "# Prompt\n", 372 | "PROMPT = \"Who is the best basketball player of all time?\"\n", 373 | "\n", 374 | "# Print Claude's response\n", 375 | "print(get_completion(PROMPT))" 376 | ] 377 | }, 378 | { 379 | "cell_type": "code", 380 | "execution_count": null, 381 | "metadata": {}, 382 | "outputs": [], 383 | "source": [ 384 | "# Prompt\n", 385 | "PROMPT = \"Who is the best basketball player of all time? Yes, there are differing opinions, but if you absolutely had to pick one player, who would it be?\"\n", 386 | "\n", 387 | "# Print Claude's response\n", 388 | "print(get_completion(PROMPT))" 389 | ] 390 | } 391 | ], 392 | "metadata": { 393 | "language_info": { 394 | "name": "python" 395 | } 396 | }, 397 | "nbformat": 4, 398 | "nbformat_minor": 2 399 | } 400 | -------------------------------------------------------------------------------- /prompt_engineering_interactive_tutorial/AmazonBedrock/anthropic/03_Assigning_Roles_Role_Prompting.ipynb: -------------------------------------------------------------------------------- 1 | { 2 | "cells": [ 3 | { 4 | "cell_type": "markdown", 5 | "metadata": {}, 6 | "source": [ 7 | "# Chapter 3: Assigning Roles (Role Prompting)\n", 8 | "\n", 9 | "- [Lesson](#lesson)\n", 10 | "- [Exercises](#exercises)\n", 11 | "- [Example Playground](#example-playground)\n", 12 | "\n", 13 | "## Setup\n", 14 | "\n", 15 | "Run the following setup cell to load your API key and establish the `get_completion` helper function." 16 | ] 17 | }, 18 | { 19 | "cell_type": "code", 20 | "execution_count": null, 21 | "metadata": {}, 22 | "outputs": [], 23 | "source": [ 24 | "%pip install anthropic --quiet\n", 25 | "\n", 26 | "# Import the hints module from the utils package\n", 27 | "import os\n", 28 | "import sys\n", 29 | "module_path = \"..\"\n", 30 | "sys.path.append(os.path.abspath(module_path))\n", 31 | "from utils import hints\n", 32 | "\n", 33 | "# Import python's built-in regular expression library\n", 34 | "import re\n", 35 | "from anthropic import AnthropicBedrock\n", 36 | "\n", 37 | "%store -r MODEL_NAME\n", 38 | "%store -r AWS_REGION\n", 39 | "\n", 40 | "client = AnthropicBedrock(aws_region=AWS_REGION)\n", 41 | "\n", 42 | "def get_completion(prompt, system=''):\n", 43 | " message = client.messages.create(\n", 44 | " model=MODEL_NAME,\n", 45 | " max_tokens=2000,\n", 46 | " temperature=0.0,\n", 47 | " messages=[\n", 48 | " {\"role\": \"user\", \"content\": prompt}\n", 49 | " ],\n", 50 | " system=system\n", 51 | " )\n", 52 | " return message.content[0].text" 53 | ] 54 | }, 55 | { 56 | "cell_type": "markdown", 57 | "metadata": {}, 58 | "source": [ 59 | "---\n", 60 | "\n", 61 | "## Lesson\n", 62 | "\n", 63 | "Continuing on the theme of Claude having no context aside from what you say, it's sometimes important to **prompt Claude to inhabit a specific role (including all necessary context)**. This is also known as role prompting. The more detail to the role context, the better.\n", 64 | "\n", 65 | "**Priming Claude with a role can improve Claude's performance** in a variety of fields, from writing to coding to summarizing. It's like how humans can sometimes be helped when told to \"think like a ______\". Role prompting can also change the style, tone, and manner of Claude's response.\n", 66 | "\n", 67 | "**Note:** Role prompting can happen either in the system prompt or as part of the User message turn." 68 | ] 69 | }, 70 | { 71 | "cell_type": "markdown", 72 | "metadata": {}, 73 | "source": [ 74 | "### Examples\n", 75 | "\n", 76 | "In the example below, we see that without role prompting, Claude provides a **straightforward and non-stylized answer** when asked to give a single sentence perspective on skateboarding.\n", 77 | "\n", 78 | "However, when we prime Claude to inhabit the role of a cat, Claude's perspective changes, and thus **Claude's response tone, style, content adapts to the new role**. \n", 79 | "\n", 80 | "**Note:** A bonus technique you can use is to **provide Claude context on its intended audience**. Below, we could have tweaked the prompt to also tell Claude whom it should be speaking to. \"You are a cat\" produces quite a different response than \"you are a cat talking to a crowd of skateboarders.\n", 81 | "\n", 82 | "Here is the prompt without role prompting in the system prompt:" 83 | ] 84 | }, 85 | { 86 | "cell_type": "code", 87 | "execution_count": null, 88 | "metadata": {}, 89 | "outputs": [], 90 | "source": [ 91 | "# Prompt\n", 92 | "PROMPT = \"In one sentence, what do you think about skateboarding?\"\n", 93 | "\n", 94 | "# Print Claude's response\n", 95 | "print(get_completion(PROMPT))" 96 | ] 97 | }, 98 | { 99 | "cell_type": "markdown", 100 | "metadata": {}, 101 | "source": [ 102 | "Here is the same user question, except with role prompting." 103 | ] 104 | }, 105 | { 106 | "cell_type": "code", 107 | "execution_count": null, 108 | "metadata": {}, 109 | "outputs": [], 110 | "source": [ 111 | "# System prompt\n", 112 | "SYSTEM_PROMPT = \"You are a cat.\"\n", 113 | "\n", 114 | "# Prompt\n", 115 | "PROMPT = \"In one sentence, what do you think about skateboarding?\"\n", 116 | "\n", 117 | "# Print Claude's response\n", 118 | "print(get_completion(PROMPT, SYSTEM_PROMPT))" 119 | ] 120 | }, 121 | { 122 | "cell_type": "markdown", 123 | "metadata": {}, 124 | "source": [ 125 | "You can use role prompting as a way to get Claude to emulate certain styles in writing, speak in a certain voice, or guide the complexity of its answers. **Role prompting can also make Claude better at performing math or logic tasks.**\n", 126 | "\n", 127 | "For example, in the example below, there is a definitive correct answer, which is yes. However, Claude gets it wrong and thinks it lacks information, which it doesn't:" 128 | ] 129 | }, 130 | { 131 | "cell_type": "code", 132 | "execution_count": null, 133 | "metadata": {}, 134 | "outputs": [], 135 | "source": [ 136 | "# Prompt\n", 137 | "PROMPT = \"Jack is looking at Anne. Anne is looking at George. Jack is married, George is not, and we don’t know if Anne is married. Is a married person looking at an unmarried person?\"\n", 138 | "\n", 139 | "# Print Claude's response\n", 140 | "print(get_completion(PROMPT))" 141 | ] 142 | }, 143 | { 144 | "cell_type": "markdown", 145 | "metadata": {}, 146 | "source": [ 147 | "Now, what if we **prime Claude to act as a logic bot**? How will that change Claude's answer? \n", 148 | "\n", 149 | "It turns out that with this new role assignment, Claude gets it right. (Although notably not for all the right reasons)" 150 | ] 151 | }, 152 | { 153 | "cell_type": "code", 154 | "execution_count": null, 155 | "metadata": {}, 156 | "outputs": [], 157 | "source": [ 158 | "# System prompt\n", 159 | "SYSTEM_PROMPT = \"You are a logic bot designed to answer complex logic problems.\"\n", 160 | "\n", 161 | "# Prompt\n", 162 | "PROMPT = \"Jack is looking at Anne. Anne is looking at George. Jack is married, George is not, and we don’t know if Anne is married. Is a married person looking at an unmarried person?\"\n", 163 | "\n", 164 | "# Print Claude's response\n", 165 | "print(get_completion(PROMPT, SYSTEM_PROMPT))" 166 | ] 167 | }, 168 | { 169 | "cell_type": "markdown", 170 | "metadata": {}, 171 | "source": [ 172 | "**Note:** What you'll learn throughout this course is that there are **many prompt engineering techniques you can use to derive similar results**. Which techniques you use is up to you and your preference! We encourage you to **experiment to find your own prompt engineering style**.\n", 173 | "\n", 174 | "If you would like to experiment with the lesson prompts without changing any content above, scroll all the way to the bottom of the lesson notebook to visit the [**Example Playground**](#example-playground)." 175 | ] 176 | }, 177 | { 178 | "cell_type": "markdown", 179 | "metadata": {}, 180 | "source": [ 181 | "---\n", 182 | "\n", 183 | "## Exercises\n", 184 | "- [Exercise 3.1 - Math Correction](#exercise-31---math-correction)" 185 | ] 186 | }, 187 | { 188 | "cell_type": "markdown", 189 | "metadata": {}, 190 | "source": [ 191 | "### Exercise 3.1 - Math Correction\n", 192 | "In some instances, **Claude may struggle with mathematics**, even simple mathematics. Below, Claude incorrectly assesses the math problem as correctly solved, even though there's an obvious arithmetic mistake in the second step. Note that Claude actually catches the mistake when going through step-by-step, but doesn't jump to the conclusion that the overall solution is wrong.\n", 193 | "\n", 194 | "Modify the `PROMPT` and / or the `SYSTEM_PROMPT` to make Claude grade the solution as `incorrectly` solved, rather than correctly solved. \n" 195 | ] 196 | }, 197 | { 198 | "cell_type": "code", 199 | "execution_count": null, 200 | "metadata": {}, 201 | "outputs": [], 202 | "source": [ 203 | "# System prompt - if you don't want to use a system prompt, you can leave this variable set to an empty string\n", 204 | "SYSTEM_PROMPT = \"\"\n", 205 | "\n", 206 | "# Prompt\n", 207 | "PROMPT = \"\"\"Is this equation solved correctly below?\n", 208 | "\n", 209 | "2x - 3 = 9\n", 210 | "2x = 6\n", 211 | "x = 3\"\"\"\n", 212 | "\n", 213 | "# Get Claude's response\n", 214 | "response = get_completion(PROMPT, SYSTEM_PROMPT)\n", 215 | "\n", 216 | "# Function to grade exercise correctness\n", 217 | "def grade_exercise(text):\n", 218 | " if \"incorrect\" in text or \"not correct\" in text.lower():\n", 219 | " return True\n", 220 | " else:\n", 221 | " return False\n", 222 | "\n", 223 | "# Print Claude's response and the corresponding grade\n", 224 | "print(response)\n", 225 | "print(\"\\n--------------------------- GRADING ---------------------------\")\n", 226 | "print(\"This exercise has been correctly solved:\", grade_exercise(response))" 227 | ] 228 | }, 229 | { 230 | "cell_type": "markdown", 231 | "metadata": {}, 232 | "source": [ 233 | "❓ If you want a hint, run the cell below!" 234 | ] 235 | }, 236 | { 237 | "cell_type": "code", 238 | "execution_count": null, 239 | "metadata": {}, 240 | "outputs": [], 241 | "source": [ 242 | "print(hints.exercise_3_1_hint)" 243 | ] 244 | }, 245 | { 246 | "cell_type": "markdown", 247 | "metadata": {}, 248 | "source": [ 249 | "### Congrats!\n", 250 | "\n", 251 | "If you've solved all exercises up until this point, you're ready to move to the next chapter. Happy prompting!" 252 | ] 253 | }, 254 | { 255 | "cell_type": "markdown", 256 | "metadata": {}, 257 | "source": [ 258 | "---\n", 259 | "\n", 260 | "## Example Playground\n", 261 | "\n", 262 | "This is an area for you to experiment freely with the prompt examples shown in this lesson and tweak prompts to see how it may affect Claude's responses." 263 | ] 264 | }, 265 | { 266 | "cell_type": "code", 267 | "execution_count": null, 268 | "metadata": {}, 269 | "outputs": [], 270 | "source": [ 271 | "# Prompt\n", 272 | "PROMPT = \"In one sentence, what do you think about skateboarding?\"\n", 273 | "\n", 274 | "# Print Claude's response\n", 275 | "print(get_completion(PROMPT))" 276 | ] 277 | }, 278 | { 279 | "cell_type": "code", 280 | "execution_count": null, 281 | "metadata": {}, 282 | "outputs": [], 283 | "source": [ 284 | "# System prompt\n", 285 | "SYSTEM_PROMPT = \"You are a cat.\"\n", 286 | "\n", 287 | "# Prompt\n", 288 | "PROMPT = \"In one sentence, what do you think about skateboarding?\"\n", 289 | "\n", 290 | "# Print Claude's response\n", 291 | "print(get_completion(PROMPT, SYSTEM_PROMPT))" 292 | ] 293 | }, 294 | { 295 | "cell_type": "code", 296 | "execution_count": null, 297 | "metadata": {}, 298 | "outputs": [], 299 | "source": [ 300 | "# Prompt\n", 301 | "PROMPT = \"Jack is looking at Anne. Anne is looking at George. Jack is married, George is not, and we don’t know if Anne is married. Is a married person looking at an unmarried person?\"\n", 302 | "\n", 303 | "# Print Claude's response\n", 304 | "print(get_completion(PROMPT))" 305 | ] 306 | }, 307 | { 308 | "cell_type": "code", 309 | "execution_count": null, 310 | "metadata": {}, 311 | "outputs": [], 312 | "source": [ 313 | "# System prompt\n", 314 | "SYSTEM_PROMPT = \"You are a logic bot designed to answer complex logic problems.\"\n", 315 | "\n", 316 | "# Prompt\n", 317 | "PROMPT = \"Jack is looking at Anne. Anne is looking at George. Jack is married, George is not, and we don’t know if Anne is married. Is a married person looking at an unmarried person?\"\n", 318 | "\n", 319 | "# Print Claude's response\n", 320 | "print(get_completion(PROMPT, SYSTEM_PROMPT))" 321 | ] 322 | } 323 | ], 324 | "metadata": { 325 | "language_info": { 326 | "name": "python" 327 | } 328 | }, 329 | "nbformat": 4, 330 | "nbformat_minor": 2 331 | } 332 | -------------------------------------------------------------------------------- /prompt_engineering_interactive_tutorial/AmazonBedrock/anthropic/10_4_Appendix_Search_and_Retrieval.ipynb: -------------------------------------------------------------------------------- 1 | { 2 | "cells": [ 3 | { 4 | "cell_type": "markdown", 5 | "metadata": {}, 6 | "source": [ 7 | "# Appendix 10.3: Search & Retrieval\n", 8 | "\n", 9 | "Did you know you can use Claude to **search through Wikipedia for you**? Claude can find and retrieve articles, at which point you can also use Claude to summarize and synthesize them, write novel content from what it found, and much more. And not just Wikipedia! You can also search over your own docs, whether stored as plain text or embedded in a vector datastore.\n", 10 | "\n", 11 | "See our [RAG cookbook examples](https://github.com/anthropics/anthropic-cookbook/blob/main/third_party/Wikipedia/wikipedia-search-cookbook.ipynb) to learn how to supplement Claude's knowledge and improve the accuracy and relevance of Claude's responses with data retrieved from vector databases, Wikipedia, the internet, and more. There, you can also learn about how to use certain [embeddings](https://docs.anthropic.com/claude/docs/embeddings) and vector database tools.\n", 12 | "\n", 13 | "If you are interested in learning about advanced RAG architectures using Claude, check out our [Claude 3 technical presentation slides on RAG architectures](https://docs.google.com/presentation/d/1zxkSI7lLUBrZycA-_znwqu8DDyVhHLkQGScvzaZrUns/edit#slide=id.g2c736259dac_63_782)." 14 | ] 15 | } 16 | ], 17 | "metadata": { 18 | "language_info": { 19 | "name": "python" 20 | } 21 | }, 22 | "nbformat": 4, 23 | "nbformat_minor": 2 24 | } 25 | -------------------------------------------------------------------------------- /prompt_engineering_interactive_tutorial/AmazonBedrock/boto3/00_Tutorial_How-To.ipynb: -------------------------------------------------------------------------------- 1 | { 2 | "cells": [ 3 | { 4 | "cell_type": "markdown", 5 | "metadata": {}, 6 | "source": [ 7 | "# Tutorial How-To\n", 8 | "\n", 9 | "This tutorial requires this initial notebook to be run first so that the requirements and environment variables are stored for all notebooks in the workshop." 10 | ] 11 | }, 12 | { 13 | "cell_type": "markdown", 14 | "metadata": {}, 15 | "source": [ 16 | "## How to get started\n", 17 | "\n", 18 | "1. Clone this repository to your local machine.\n", 19 | "\n", 20 | "2. Install the required dependencies by running the following command:\n", 21 | " " 22 | ] 23 | }, 24 | { 25 | "cell_type": "markdown", 26 | "metadata": {}, 27 | "source": [ 28 | "> ⚠️ **Please ignore error messages related to pip's dependency resolver.**" 29 | ] 30 | }, 31 | { 32 | "cell_type": "code", 33 | "execution_count": null, 34 | "metadata": {}, 35 | "outputs": [], 36 | "source": [ 37 | "%pip install -qU pip\n", 38 | "%pip install -qr ../requirements.txt" 39 | ] 40 | }, 41 | { 42 | "cell_type": "markdown", 43 | "metadata": {}, 44 | "source": [ 45 | "3. Restart the kernel after installing dependencies" 46 | ] 47 | }, 48 | { 49 | "cell_type": "code", 50 | "execution_count": null, 51 | "metadata": {}, 52 | "outputs": [], 53 | "source": [ 54 | "# restart kernel\n", 55 | "from IPython.core.display import HTML\n", 56 | "HTML(\"\")" 57 | ] 58 | }, 59 | { 60 | "cell_type": "markdown", 61 | "metadata": {}, 62 | "source": [ 63 | "4. Run the notebook cells in order, following the instructions provided." 64 | ] 65 | }, 66 | { 67 | "cell_type": "markdown", 68 | "metadata": {}, 69 | "source": [ 70 | "---\n", 71 | "\n", 72 | "## Usage Notes & Tips 💡\n", 73 | "\n", 74 | "- This course uses Claude 3 Haiku with temperature 0. We will talk more about temperature later in the course. For now, it's enough to understand that these settings yield more deterministic results. All prompt engineering techniques in this course also apply to previous generation legacy Claude models such as Claude 2 and Claude Instant 1.2.\n", 75 | "\n", 76 | "- You can use `Shift + Enter` to execute the cell and move to the next one.\n", 77 | "\n", 78 | "- When you reach the bottom of a tutorial page, navigate to the next numbered file in the folder, or to the next numbered folder if you're finished with the content within that chapter file.\n", 79 | "\n", 80 | "### The Anthropic SDK & the Messages API\n", 81 | "We will be using the [Anthropic python SDK](https://docs.anthropic.com/claude/reference/client-sdks) and the [Messages API](https://docs.anthropic.com/claude/reference/messages_post) throughout this tutorial. \n", 82 | "\n", 83 | "Below is an example of what running a prompt will look like in this tutorial. First, we create `get_completion`, which is a helper function that sends a prompt to Claude and returns Claude's generated response. Run that cell now." 84 | ] 85 | }, 86 | { 87 | "cell_type": "markdown", 88 | "metadata": {}, 89 | "source": [ 90 | "First, we set and store the model name and region." 91 | ] 92 | }, 93 | { 94 | "cell_type": "code", 95 | "execution_count": null, 96 | "metadata": {}, 97 | "outputs": [], 98 | "source": [ 99 | "import boto3\n", 100 | "session = boto3.Session() # create a boto3 session to dynamically get and set the region name\n", 101 | "AWS_REGION = session.region_name\n", 102 | "print(\"AWS Region:\", AWS_REGION)\n", 103 | "MODEL_NAME = \"anthropic.claude-3-haiku-20240307-v1:0\"\n", 104 | "\n", 105 | "%store MODEL_NAME\n", 106 | "%store AWS_REGION" 107 | ] 108 | }, 109 | { 110 | "cell_type": "markdown", 111 | "metadata": {}, 112 | "source": [ 113 | "Then, we create `get_completion`, which is a helper function that sends a prompt to Claude and returns Claude's generated response. Run that cell now." 114 | ] 115 | }, 116 | { 117 | "cell_type": "code", 118 | "execution_count": null, 119 | "metadata": {}, 120 | "outputs": [], 121 | "source": [ 122 | "import boto3\n", 123 | "import json\n", 124 | "\n", 125 | "bedrock = boto3.client('bedrock-runtime',region_name=AWS_REGION)\n", 126 | "\n", 127 | "def get_completion(prompt):\n", 128 | " body = json.dumps(\n", 129 | " {\n", 130 | " \"anthropic_version\": '',\n", 131 | " \"max_tokens\": 2000,\n", 132 | " \"messages\": [{\"role\": \"user\", \"content\": prompt}],\n", 133 | " \"temperature\": 0.0,\n", 134 | " \"top_p\": 1,\n", 135 | " \"system\": ''\n", 136 | " }\n", 137 | " )\n", 138 | " response = bedrock.invoke_model(body=body, modelId=MODEL_NAME)\n", 139 | " response_body = json.loads(response.get('body').read())\n", 140 | "\n", 141 | " return response_body.get('content')[0].get('text')" 142 | ] 143 | }, 144 | { 145 | "cell_type": "markdown", 146 | "metadata": {}, 147 | "source": [ 148 | "Now we will write out an example prompt for Claude and print Claude's output by running our `get_completion` helper function. Running the cell below will print out a response from Claude beneath it.\n", 149 | "\n", 150 | "Feel free to play around with the prompt string to elicit different responses from Claude." 151 | ] 152 | }, 153 | { 154 | "cell_type": "code", 155 | "execution_count": null, 156 | "metadata": {}, 157 | "outputs": [], 158 | "source": [ 159 | "# Prompt\n", 160 | "prompt = \"Hello, Claude!\"\n", 161 | "\n", 162 | "# Get Claude's response\n", 163 | "print(get_completion(prompt))" 164 | ] 165 | }, 166 | { 167 | "cell_type": "markdown", 168 | "metadata": {}, 169 | "source": [ 170 | "The `MODEL_NAME` and `AWS_REGION` variables defined earlier will be used throughout the tutorial. Just make sure to run the cells for each tutorial page from top to bottom." 171 | ] 172 | } 173 | ], 174 | "metadata": { 175 | "kernelspec": { 176 | "display_name": "py310", 177 | "language": "python", 178 | "name": "python3" 179 | }, 180 | "language_info": { 181 | "codemirror_mode": { 182 | "name": "ipython", 183 | "version": 3 184 | }, 185 | "file_extension": ".py", 186 | "mimetype": "text/x-python", 187 | "name": "python", 188 | "nbconvert_exporter": "python", 189 | "pygments_lexer": "ipython3", 190 | "version": "3.12.0" 191 | } 192 | }, 193 | "nbformat": 4, 194 | "nbformat_minor": 2 195 | } 196 | -------------------------------------------------------------------------------- /prompt_engineering_interactive_tutorial/AmazonBedrock/boto3/02_Being_Clear_and_Direct.ipynb: -------------------------------------------------------------------------------- 1 | { 2 | "cells": [ 3 | { 4 | "cell_type": "markdown", 5 | "metadata": {}, 6 | "source": [ 7 | "# Chapter 2: Being Clear and Direct\n", 8 | "\n", 9 | "- [Lesson](#lesson)\n", 10 | "- [Exercises](#exercises)\n", 11 | "- [Example Playground](#example-playground)\n", 12 | "\n", 13 | "## Setup\n", 14 | "\n", 15 | "Run the following setup cell to load your API key and establish the `get_completion` helper function." 16 | ] 17 | }, 18 | { 19 | "cell_type": "code", 20 | "execution_count": null, 21 | "metadata": {}, 22 | "outputs": [], 23 | "source": [ 24 | "# Import python's built-in regular expression library\n", 25 | "import re\n", 26 | "import boto3\n", 27 | "import json\n", 28 | "\n", 29 | "# Import the hints module from the utils package\n", 30 | "import os\n", 31 | "import sys\n", 32 | "module_path = \"..\"\n", 33 | "sys.path.append(os.path.abspath(module_path))\n", 34 | "from utils import hints\n", 35 | "\n", 36 | "# Retrieve the MODEL_NAME variable from the IPython store\n", 37 | "%store -r MODEL_NAME\n", 38 | "%store -r AWS_REGION\n", 39 | "\n", 40 | "client = boto3.client('bedrock-runtime',region_name=AWS_REGION)\n", 41 | "\n", 42 | "def get_completion(prompt,system=''):\n", 43 | " body = json.dumps(\n", 44 | " {\n", 45 | " \"anthropic_version\": '',\n", 46 | " \"max_tokens\": 2000,\n", 47 | " \"messages\": [{\"role\": \"user\", \"content\": prompt}],\n", 48 | " \"temperature\": 0.0,\n", 49 | " \"top_p\": 1,\n", 50 | " \"system\": system\n", 51 | " }\n", 52 | " )\n", 53 | " response = client.invoke_model(body=body, modelId=MODEL_NAME)\n", 54 | " response_body = json.loads(response.get('body').read())\n", 55 | "\n", 56 | " return response_body.get('content')[0].get('text')" 57 | ] 58 | }, 59 | { 60 | "cell_type": "markdown", 61 | "metadata": {}, 62 | "source": [ 63 | "---\n", 64 | "\n", 65 | "## Lesson\n", 66 | "\n", 67 | "**Claude responds best to clear and direct instructions.**\n", 68 | "\n", 69 | "Think of Claude like any other human that is new to the job. **Claude has no context** on what to do aside from what you literally tell it. Just as when you instruct a human for the first time on a task, the more you explain exactly what you want in a straightforward manner to Claude, the better and more accurate Claude's response will be.\"\t\t\t\t\n", 70 | "\t\t\t\t\n", 71 | "When in doubt, follow the **Golden Rule of Clear Prompting**:\n", 72 | "- Show your prompt to a colleague or friend and have them follow the instructions themselves to see if they can produce the result you want. If they're confused, Claude's confused.\t\t\t\t" 73 | ] 74 | }, 75 | { 76 | "cell_type": "markdown", 77 | "metadata": {}, 78 | "source": [ 79 | "### Examples\n", 80 | "\n", 81 | "Let's take a task like writing poetry. (Ignore any syllable mismatch - LLMs aren't great at counting syllables yet.)" 82 | ] 83 | }, 84 | { 85 | "cell_type": "code", 86 | "execution_count": null, 87 | "metadata": {}, 88 | "outputs": [], 89 | "source": [ 90 | "# Prompt\n", 91 | "PROMPT = \"Write a haiku about robots.\"\n", 92 | "\n", 93 | "# Print Claude's response\n", 94 | "print(get_completion(PROMPT))" 95 | ] 96 | }, 97 | { 98 | "cell_type": "markdown", 99 | "metadata": {}, 100 | "source": [ 101 | "This haiku is nice enough, but users may want Claude to go directly into the poem without the \"Here is a haiku\" preamble.\n", 102 | "\n", 103 | "How do we achieve that? We **ask for it**!" 104 | ] 105 | }, 106 | { 107 | "cell_type": "code", 108 | "execution_count": null, 109 | "metadata": {}, 110 | "outputs": [], 111 | "source": [ 112 | "# Prompt\n", 113 | "PROMPT = \"Write a haiku about robots. Skip the preamble; go straight into the poem.\"\n", 114 | "\n", 115 | "# Print Claude's response\n", 116 | "print(get_completion(PROMPT))" 117 | ] 118 | }, 119 | { 120 | "cell_type": "markdown", 121 | "metadata": {}, 122 | "source": [ 123 | "Here's another example. Let's ask Claude who's the best basketball player of all time. You can see below that while Claude lists a few names, **it doesn't respond with a definitive \"best\"**." 124 | ] 125 | }, 126 | { 127 | "cell_type": "code", 128 | "execution_count": null, 129 | "metadata": {}, 130 | "outputs": [], 131 | "source": [ 132 | "# Prompt\n", 133 | "PROMPT = \"Who is the best basketball player of all time?\"\n", 134 | "\n", 135 | "# Print Claude's response\n", 136 | "print(get_completion(PROMPT))" 137 | ] 138 | }, 139 | { 140 | "cell_type": "markdown", 141 | "metadata": {}, 142 | "source": [ 143 | "Can we get Claude to make up its mind and decide on a best player? Yes! Just ask!" 144 | ] 145 | }, 146 | { 147 | "cell_type": "code", 148 | "execution_count": null, 149 | "metadata": {}, 150 | "outputs": [], 151 | "source": [ 152 | "# Prompt\n", 153 | "PROMPT = \"Who is the best basketball player of all time? Yes, there are differing opinions, but if you absolutely had to pick one player, who would it be?\"\n", 154 | "\n", 155 | "# Print Claude's response\n", 156 | "print(get_completion(PROMPT))" 157 | ] 158 | }, 159 | { 160 | "cell_type": "markdown", 161 | "metadata": {}, 162 | "source": [ 163 | "If you would like to experiment with the lesson prompts without changing any content above, scroll all the way to the bottom of the lesson notebook to visit the [**Example Playground**](#example-playground)." 164 | ] 165 | }, 166 | { 167 | "cell_type": "markdown", 168 | "metadata": {}, 169 | "source": [ 170 | "---\n", 171 | "\n", 172 | "## Exercises\n", 173 | "- [Exercise 2.1 - Spanish](#exercise-21---spanish)\n", 174 | "- [Exercise 2.2 - One Player Only](#exercise-22---one-player-only)\n", 175 | "- [Exercise 2.3 - Write a Story](#exercise-23---write-a-story)" 176 | ] 177 | }, 178 | { 179 | "cell_type": "markdown", 180 | "metadata": {}, 181 | "source": [ 182 | "### Exercise 2.1 - Spanish\n", 183 | "Modify the `SYSTEM_PROMPT` to make Claude output its answer in Spanish." 184 | ] 185 | }, 186 | { 187 | "cell_type": "code", 188 | "execution_count": null, 189 | "metadata": {}, 190 | "outputs": [], 191 | "source": [ 192 | "# System prompt - this is the only field you should chnage\n", 193 | "SYSTEM_PROMPT = \"[Replace this text]\"\n", 194 | "\n", 195 | "# Prompt\n", 196 | "PROMPT = \"Hello Claude, how are you?\"\n", 197 | "\n", 198 | "# Get Claude's response\n", 199 | "response = get_completion(PROMPT, SYSTEM_PROMPT)\n", 200 | "\n", 201 | "# Function to grade exercise correctness\n", 202 | "def grade_exercise(text):\n", 203 | " return \"hola\" in text.lower()\n", 204 | "\n", 205 | "# Print Claude's response and the corresponding grade\n", 206 | "print(response)\n", 207 | "print(\"\\n--------------------------- GRADING ---------------------------\")\n", 208 | "print(\"This exercise has been correctly solved:\", grade_exercise(response))" 209 | ] 210 | }, 211 | { 212 | "cell_type": "markdown", 213 | "metadata": {}, 214 | "source": [ 215 | "❓ If you want a hint, run the cell below!" 216 | ] 217 | }, 218 | { 219 | "cell_type": "code", 220 | "execution_count": null, 221 | "metadata": {}, 222 | "outputs": [], 223 | "source": [ 224 | "print(hints.exercise_2_1_hint)" 225 | ] 226 | }, 227 | { 228 | "cell_type": "markdown", 229 | "metadata": {}, 230 | "source": [ 231 | "### Exercise 2.2 - One Player Only\n", 232 | "\n", 233 | "Modify the `PROMPT` so that Claude doesn't equivocate at all and responds with **ONLY** the name of one specific player, with **no other words or punctuation**. " 234 | ] 235 | }, 236 | { 237 | "cell_type": "code", 238 | "execution_count": null, 239 | "metadata": {}, 240 | "outputs": [], 241 | "source": [ 242 | "# Prompt - this is the only field you should change\n", 243 | "PROMPT = \"[Replace this text]\"\n", 244 | "\n", 245 | "# Get Claude's response\n", 246 | "response = get_completion(PROMPT)\n", 247 | "\n", 248 | "# Function to grade exercise correctness\n", 249 | "def grade_exercise(text):\n", 250 | " return text == \"Michael Jordan\"\n", 251 | "\n", 252 | "# Print Claude's response and the corresponding grade\n", 253 | "print(response)\n", 254 | "print(\"\\n--------------------------- GRADING ---------------------------\")\n", 255 | "print(\"This exercise has been correctly solved:\", grade_exercise(response))" 256 | ] 257 | }, 258 | { 259 | "cell_type": "markdown", 260 | "metadata": {}, 261 | "source": [ 262 | "❓ If you want a hint, run the cell below!" 263 | ] 264 | }, 265 | { 266 | "cell_type": "code", 267 | "execution_count": null, 268 | "metadata": {}, 269 | "outputs": [], 270 | "source": [ 271 | "print(hints.exercise_2_2_hint)" 272 | ] 273 | }, 274 | { 275 | "cell_type": "markdown", 276 | "metadata": {}, 277 | "source": [ 278 | "### Exercise 2.3 - Write a Story\n", 279 | "\n", 280 | "Modify the `PROMPT` so that Claude responds with as long a response as you can muster. If your answer is **over 800 words**, Claude's response will be graded as correct." 281 | ] 282 | }, 283 | { 284 | "cell_type": "code", 285 | "execution_count": null, 286 | "metadata": {}, 287 | "outputs": [], 288 | "source": [ 289 | "# Prompt - this is the only field you should change\n", 290 | "PROMPT = \"[Replace this text]\"\n", 291 | "\n", 292 | "# Get Claude's response\n", 293 | "response = get_completion(PROMPT)\n", 294 | "\n", 295 | "# Function to grade exercise correctness\n", 296 | "def grade_exercise(text):\n", 297 | " trimmed = text.strip()\n", 298 | " words = len(trimmed.split())\n", 299 | " return words >= 800\n", 300 | "\n", 301 | "# Print Claude's response and the corresponding grade\n", 302 | "print(response)\n", 303 | "print(\"\\n--------------------------- GRADING ---------------------------\")\n", 304 | "print(\"This exercise has been correctly solved:\", grade_exercise(response))" 305 | ] 306 | }, 307 | { 308 | "cell_type": "markdown", 309 | "metadata": {}, 310 | "source": [ 311 | "❓ If you want a hint, run the cell below!" 312 | ] 313 | }, 314 | { 315 | "cell_type": "code", 316 | "execution_count": null, 317 | "metadata": {}, 318 | "outputs": [], 319 | "source": [ 320 | "print(hints.exercise_2_3_hint)" 321 | ] 322 | }, 323 | { 324 | "cell_type": "markdown", 325 | "metadata": {}, 326 | "source": [ 327 | "### Congrats!\n", 328 | "\n", 329 | "If you've solved all exercises up until this point, you're ready to move to the next chapter. Happy prompting!" 330 | ] 331 | }, 332 | { 333 | "cell_type": "markdown", 334 | "metadata": {}, 335 | "source": [ 336 | "---\n", 337 | "\n", 338 | "## Example Playground\n", 339 | "\n", 340 | "This is an area for you to experiment freely with the prompt examples shown in this lesson and tweak prompts to see how it may affect Claude's responses." 341 | ] 342 | }, 343 | { 344 | "cell_type": "code", 345 | "execution_count": null, 346 | "metadata": {}, 347 | "outputs": [], 348 | "source": [ 349 | "# Prompt\n", 350 | "PROMPT = \"Write a haiku about robots.\"\n", 351 | "\n", 352 | "# Print Claude's response\n", 353 | "print(get_completion(PROMPT))" 354 | ] 355 | }, 356 | { 357 | "cell_type": "code", 358 | "execution_count": null, 359 | "metadata": {}, 360 | "outputs": [], 361 | "source": [ 362 | "# Prompt\n", 363 | "PROMPT = \"Write a haiku about robots. Skip the preamble; go straight into the poem.\"\n", 364 | "\n", 365 | "# Print Claude's response\n", 366 | "print(get_completion(PROMPT))" 367 | ] 368 | }, 369 | { 370 | "cell_type": "code", 371 | "execution_count": null, 372 | "metadata": {}, 373 | "outputs": [], 374 | "source": [ 375 | "# Prompt\n", 376 | "PROMPT = \"Who is the best basketball player of all time?\"\n", 377 | "\n", 378 | "# Print Claude's response\n", 379 | "print(get_completion(PROMPT))" 380 | ] 381 | }, 382 | { 383 | "cell_type": "code", 384 | "execution_count": null, 385 | "metadata": {}, 386 | "outputs": [], 387 | "source": [ 388 | "# Prompt\n", 389 | "PROMPT = \"Who is the best basketball player of all time? Yes, there are differing opinions, but if you absolutely had to pick one player, who would it be?\"\n", 390 | "\n", 391 | "# Print Claude's response\n", 392 | "print(get_completion(PROMPT))" 393 | ] 394 | } 395 | ], 396 | "metadata": { 397 | "language_info": { 398 | "name": "python" 399 | } 400 | }, 401 | "nbformat": 4, 402 | "nbformat_minor": 2 403 | } 404 | -------------------------------------------------------------------------------- /prompt_engineering_interactive_tutorial/AmazonBedrock/boto3/03_Assigning_Roles_Role_Prompting.ipynb: -------------------------------------------------------------------------------- 1 | { 2 | "cells": [ 3 | { 4 | "cell_type": "markdown", 5 | "metadata": {}, 6 | "source": [ 7 | "# Chapter 3: Assigning Roles (Role Prompting)\n", 8 | "\n", 9 | "- [Lesson](#lesson)\n", 10 | "- [Exercises](#exercises)\n", 11 | "- [Example Playground](#example-playground)\n", 12 | "\n", 13 | "## Setup\n", 14 | "\n", 15 | "Run the following setup cell to load your API key and establish the `get_completion` helper function." 16 | ] 17 | }, 18 | { 19 | "cell_type": "code", 20 | "execution_count": null, 21 | "metadata": {}, 22 | "outputs": [], 23 | "source": [ 24 | "# Import python's built-in regular expression library\n", 25 | "import re\n", 26 | "import boto3\n", 27 | "import json\n", 28 | "\n", 29 | "# Import the hints module from the utils package\n", 30 | "import os\n", 31 | "import sys\n", 32 | "module_path = \"..\"\n", 33 | "sys.path.append(os.path.abspath(module_path))\n", 34 | "from utils import hints\n", 35 | "\n", 36 | "# Retrieve the MODEL_NAME variable from the IPython store\n", 37 | "%store -r MODEL_NAME\n", 38 | "%store -r AWS_REGION\n", 39 | "\n", 40 | "client = boto3.client('bedrock-runtime',region_name=AWS_REGION)\n", 41 | "\n", 42 | "def get_completion(prompt,system=''):\n", 43 | " body = json.dumps(\n", 44 | " {\n", 45 | " \"anthropic_version\": '',\n", 46 | " \"max_tokens\": 2000,\n", 47 | " \"messages\": [{\"role\": \"user\", \"content\": prompt}],\n", 48 | " \"temperature\": 0.0,\n", 49 | " \"top_p\": 1,\n", 50 | " \"system\": system\n", 51 | " }\n", 52 | " )\n", 53 | " response = client.invoke_model(body=body, modelId=MODEL_NAME)\n", 54 | " response_body = json.loads(response.get('body').read())\n", 55 | "\n", 56 | " return response_body.get('content')[0].get('text')" 57 | ] 58 | }, 59 | { 60 | "cell_type": "markdown", 61 | "metadata": {}, 62 | "source": [ 63 | "---\n", 64 | "\n", 65 | "## Lesson\n", 66 | "\n", 67 | "Continuing on the theme of Claude having no context aside from what you say, it's sometimes important to **prompt Claude to inhabit a specific role (including all necessary context)**. This is also known as role prompting. The more detail to the role context, the better.\n", 68 | "\n", 69 | "**Priming Claude with a role can improve Claude's performance** in a variety of fields, from writing to coding to summarizing. It's like how humans can sometimes be helped when told to \"think like a ______\". Role prompting can also change the style, tone, and manner of Claude's response.\n", 70 | "\n", 71 | "**Note:** Role prompting can happen either in the system prompt or as part of the User message turn." 72 | ] 73 | }, 74 | { 75 | "cell_type": "markdown", 76 | "metadata": {}, 77 | "source": [ 78 | "### Examples\n", 79 | "\n", 80 | "In the example below, we see that without role prompting, Claude provides a **straightforward and non-stylized answer** when asked to give a single sentence perspective on skateboarding.\n", 81 | "\n", 82 | "However, when we prime Claude to inhabit the role of a cat, Claude's perspective changes, and thus **Claude's response tone, style, content adapts to the new role**. \n", 83 | "\n", 84 | "**Note:** A bonus technique you can use is to **provide Claude context on its intended audience**. Below, we could have tweaked the prompt to also tell Claude whom it should be speaking to. \"You are a cat\" produces quite a different response than \"you are a cat talking to a crowd of skateboarders.\n", 85 | "\n", 86 | "Here is the prompt without role prompting in the system prompt:" 87 | ] 88 | }, 89 | { 90 | "cell_type": "code", 91 | "execution_count": null, 92 | "metadata": {}, 93 | "outputs": [], 94 | "source": [ 95 | "# Prompt\n", 96 | "PROMPT = \"In one sentence, what do you think about skateboarding?\"\n", 97 | "\n", 98 | "# Print Claude's response\n", 99 | "print(get_completion(PROMPT))" 100 | ] 101 | }, 102 | { 103 | "cell_type": "markdown", 104 | "metadata": {}, 105 | "source": [ 106 | "Here is the same user question, except with role prompting." 107 | ] 108 | }, 109 | { 110 | "cell_type": "code", 111 | "execution_count": null, 112 | "metadata": {}, 113 | "outputs": [], 114 | "source": [ 115 | "# System prompt\n", 116 | "SYSTEM_PROMPT = \"You are a cat.\"\n", 117 | "\n", 118 | "# Prompt\n", 119 | "PROMPT = \"In one sentence, what do you think about skateboarding?\"\n", 120 | "\n", 121 | "# Print Claude's response\n", 122 | "print(get_completion(PROMPT, SYSTEM_PROMPT))" 123 | ] 124 | }, 125 | { 126 | "cell_type": "markdown", 127 | "metadata": {}, 128 | "source": [ 129 | "You can use role prompting as a way to get Claude to emulate certain styles in writing, speak in a certain voice, or guide the complexity of its answers. **Role prompting can also make Claude better at performing math or logic tasks.**\n", 130 | "\n", 131 | "For example, in the example below, there is a definitive correct answer, which is yes. However, Claude gets it wrong and thinks it lacks information, which it doesn't:" 132 | ] 133 | }, 134 | { 135 | "cell_type": "code", 136 | "execution_count": null, 137 | "metadata": {}, 138 | "outputs": [], 139 | "source": [ 140 | "# Prompt\n", 141 | "PROMPT = \"Jack is looking at Anne. Anne is looking at George. Jack is married, George is not, and we don’t know if Anne is married. Is a married person looking at an unmarried person?\"\n", 142 | "\n", 143 | "# Print Claude's response\n", 144 | "print(get_completion(PROMPT))" 145 | ] 146 | }, 147 | { 148 | "cell_type": "markdown", 149 | "metadata": {}, 150 | "source": [ 151 | "Now, what if we **prime Claude to act as a logic bot**? How will that change Claude's answer? \n", 152 | "\n", 153 | "It turns out that with this new role assignment, Claude gets it right. (Although notably not for all the right reasons)" 154 | ] 155 | }, 156 | { 157 | "cell_type": "code", 158 | "execution_count": null, 159 | "metadata": {}, 160 | "outputs": [], 161 | "source": [ 162 | "# System prompt\n", 163 | "SYSTEM_PROMPT = \"You are a logic bot designed to answer complex logic problems.\"\n", 164 | "\n", 165 | "# Prompt\n", 166 | "PROMPT = \"Jack is looking at Anne. Anne is looking at George. Jack is married, George is not, and we don’t know if Anne is married. Is a married person looking at an unmarried person?\"\n", 167 | "\n", 168 | "# Print Claude's response\n", 169 | "print(get_completion(PROMPT, SYSTEM_PROMPT))" 170 | ] 171 | }, 172 | { 173 | "cell_type": "markdown", 174 | "metadata": {}, 175 | "source": [ 176 | "**Note:** What you'll learn throughout this course is that there are **many prompt engineering techniques you can use to derive similar results**. Which techniques you use is up to you and your preference! We encourage you to **experiment to find your own prompt engineering style**.\n", 177 | "\n", 178 | "If you would like to experiment with the lesson prompts without changing any content above, scroll all the way to the bottom of the lesson notebook to visit the [**Example Playground**](#example-playground)." 179 | ] 180 | }, 181 | { 182 | "cell_type": "markdown", 183 | "metadata": {}, 184 | "source": [ 185 | "---\n", 186 | "\n", 187 | "## Exercises\n", 188 | "- [Exercise 3.1 - Math Correction](#exercise-31---math-correction)" 189 | ] 190 | }, 191 | { 192 | "cell_type": "markdown", 193 | "metadata": {}, 194 | "source": [ 195 | "### Exercise 3.1 - Math Correction\n", 196 | "In some instances, **Claude may struggle with mathematics**, even simple mathematics. Below, Claude incorrectly assesses the math problem as correctly solved, even though there's an obvious arithmetic mistake in the second step. Note that Claude actually catches the mistake when going through step-by-step, but doesn't jump to the conclusion that the overall solution is wrong.\n", 197 | "\n", 198 | "Modify the `PROMPT` and / or the `SYSTEM_PROMPT` to make Claude grade the solution as `incorrectly` solved, rather than correctly solved. \n" 199 | ] 200 | }, 201 | { 202 | "cell_type": "code", 203 | "execution_count": null, 204 | "metadata": {}, 205 | "outputs": [], 206 | "source": [ 207 | "# System prompt - if you don't want to use a system prompt, you can leave this variable set to an empty string\n", 208 | "SYSTEM_PROMPT = \"\"\n", 209 | "\n", 210 | "# Prompt\n", 211 | "PROMPT = \"\"\"Is this equation solved correctly below?\n", 212 | "\n", 213 | "2x - 3 = 9\n", 214 | "2x = 6\n", 215 | "x = 3\"\"\"\n", 216 | "\n", 217 | "# Get Claude's response\n", 218 | "response = get_completion(PROMPT, SYSTEM_PROMPT)\n", 219 | "\n", 220 | "# Function to grade exercise correctness\n", 221 | "def grade_exercise(text):\n", 222 | " if \"incorrect\" in text or \"not correct\" in text.lower():\n", 223 | " return True\n", 224 | " else:\n", 225 | " return False\n", 226 | "\n", 227 | "# Print Claude's response and the corresponding grade\n", 228 | "print(response)\n", 229 | "print(\"\\n--------------------------- GRADING ---------------------------\")\n", 230 | "print(\"This exercise has been correctly solved:\", grade_exercise(response))" 231 | ] 232 | }, 233 | { 234 | "cell_type": "markdown", 235 | "metadata": {}, 236 | "source": [ 237 | "❓ If you want a hint, run the cell below!" 238 | ] 239 | }, 240 | { 241 | "cell_type": "code", 242 | "execution_count": null, 243 | "metadata": {}, 244 | "outputs": [], 245 | "source": [ 246 | "print(hints.exercise_3_1_hint)" 247 | ] 248 | }, 249 | { 250 | "cell_type": "markdown", 251 | "metadata": {}, 252 | "source": [ 253 | "### Congrats!\n", 254 | "\n", 255 | "If you've solved all exercises up until this point, you're ready to move to the next chapter. Happy prompting!" 256 | ] 257 | }, 258 | { 259 | "cell_type": "markdown", 260 | "metadata": {}, 261 | "source": [ 262 | "---\n", 263 | "\n", 264 | "## Example Playground\n", 265 | "\n", 266 | "This is an area for you to experiment freely with the prompt examples shown in this lesson and tweak prompts to see how it may affect Claude's responses." 267 | ] 268 | }, 269 | { 270 | "cell_type": "code", 271 | "execution_count": null, 272 | "metadata": {}, 273 | "outputs": [], 274 | "source": [ 275 | "# Prompt\n", 276 | "PROMPT = \"In one sentence, what do you think about skateboarding?\"\n", 277 | "\n", 278 | "# Print Claude's response\n", 279 | "print(get_completion(PROMPT))" 280 | ] 281 | }, 282 | { 283 | "cell_type": "code", 284 | "execution_count": null, 285 | "metadata": {}, 286 | "outputs": [], 287 | "source": [ 288 | "# System prompt\n", 289 | "SYSTEM_PROMPT = \"You are a cat.\"\n", 290 | "\n", 291 | "# Prompt\n", 292 | "PROMPT = \"In one sentence, what do you think about skateboarding?\"\n", 293 | "\n", 294 | "# Print Claude's response\n", 295 | "print(get_completion(PROMPT, SYSTEM_PROMPT))" 296 | ] 297 | }, 298 | { 299 | "cell_type": "code", 300 | "execution_count": null, 301 | "metadata": {}, 302 | "outputs": [], 303 | "source": [ 304 | "# Prompt\n", 305 | "PROMPT = \"Jack is looking at Anne. Anne is looking at George. Jack is married, George is not, and we don’t know if Anne is married. Is a married person looking at an unmarried person?\"\n", 306 | "\n", 307 | "# Print Claude's response\n", 308 | "print(get_completion(PROMPT))" 309 | ] 310 | }, 311 | { 312 | "cell_type": "code", 313 | "execution_count": null, 314 | "metadata": {}, 315 | "outputs": [], 316 | "source": [ 317 | "# System prompt\n", 318 | "SYSTEM_PROMPT = \"You are a logic bot designed to answer complex logic problems.\"\n", 319 | "\n", 320 | "# Prompt\n", 321 | "PROMPT = \"Jack is looking at Anne. Anne is looking at George. Jack is married, George is not, and we don’t know if Anne is married. Is a married person looking at an unmarried person?\"\n", 322 | "\n", 323 | "# Print Claude's response\n", 324 | "print(get_completion(PROMPT, SYSTEM_PROMPT))" 325 | ] 326 | } 327 | ], 328 | "metadata": { 329 | "language_info": { 330 | "name": "python" 331 | } 332 | }, 333 | "nbformat": 4, 334 | "nbformat_minor": 2 335 | } 336 | -------------------------------------------------------------------------------- /prompt_engineering_interactive_tutorial/AmazonBedrock/boto3/10_4_Appendix_Search_and_Retrieval.ipynb: -------------------------------------------------------------------------------- 1 | { 2 | "cells": [ 3 | { 4 | "cell_type": "markdown", 5 | "metadata": {}, 6 | "source": [ 7 | "# Appendix 10.4: Search & Retrieval\n", 8 | "\n", 9 | "Did you know you can use Claude to **search through Wikipedia for you**? Claude can find and retrieve articles, at which point you can also use Claude to summarize and synthesize them, write novel content from what it found, and much more. And not just Wikipedia! You can also search over your own docs, whether stored as plain text or embedded in a vector datastore.\n", 10 | "\n", 11 | "See our [RAG cookbook examples](https://github.com/anthropics/anthropic-cookbook/blob/main/third_party/Wikipedia/wikipedia-search-cookbook.ipynb) to learn how to supplement Claude's knowledge and improve the accuracy and relevance of Claude's responses with data retrieved from vector databases, Wikipedia, the internet, and more. There, you can also learn about how to use certain [embeddings](https://docs.anthropic.com/claude/docs/embeddings) and vector database tools.\n", 12 | "\n", 13 | "If you are interested in learning about advanced RAG architectures using Claude, check out our [Claude 3 technical presentation slides on RAG architectures](https://docs.google.com/presentation/d/1zxkSI7lLUBrZycA-_znwqu8DDyVhHLkQGScvzaZrUns/edit#slide=id.g2c736259dac_63_782)." 14 | ] 15 | } 16 | ], 17 | "metadata": { 18 | "language_info": { 19 | "name": "python" 20 | } 21 | }, 22 | "nbformat": 4, 23 | "nbformat_minor": 2 24 | } 25 | -------------------------------------------------------------------------------- /prompt_engineering_interactive_tutorial/AmazonBedrock/cloudformation/workshop-v1-final-cfn.yml: -------------------------------------------------------------------------------- 1 | AWSTemplateFormatVersion: '2010-09-09' 2 | Description: 'CloudFormation template to create a Jupyter notebook in SageMaker with an execution role and Anthropic Prompt Eng. Repo' 3 | 4 | Parameters: 5 | NotebookName: 6 | Type: String 7 | Default: 'PromptEngWithAnthropicNotebook' 8 | DefaultRepoUrl: 9 | Type: String 10 | Default: 'https://github.com/aws-samples/prompt-engineering-with-anthropic-claude-v-3.git' 11 | 12 | Resources: 13 | SageMakerExecutionRole: 14 | Type: AWS::IAM::Role 15 | Properties: 16 | AssumeRolePolicyDocument: 17 | Version: '2012-10-17' 18 | Statement: 19 | - Effect: Allow 20 | Principal: 21 | Service: 22 | - sagemaker.amazonaws.com 23 | Action: 24 | - sts:AssumeRole 25 | ManagedPolicyArns: 26 | - arn:aws:iam::aws:policy/AmazonSageMakerFullAccess 27 | - arn:aws:iam::aws:policy/AmazonBedrockFullAccess 28 | 29 | KmsKey: 30 | Type: AWS::KMS::Key 31 | Properties: 32 | Description: 'KMS key for SageMaker notebook' 33 | KeyPolicy: 34 | Version: '2012-10-17' 35 | Statement: 36 | - Effect: Allow 37 | Principal: 38 | AWS: !Sub 'arn:aws:iam::${AWS::AccountId}:root' 39 | Action: 'kms:*' 40 | Resource: '*' 41 | EnableKeyRotation: true 42 | 43 | KmsKeyAlias: 44 | Type: AWS::KMS::Alias 45 | Properties: 46 | AliasName: !Sub 'alias/${NotebookName}-kms-key' 47 | TargetKeyId: !Ref KmsKey 48 | 49 | SageMakerNotebookInstance: 50 | Type: AWS::SageMaker::NotebookInstance 51 | Properties: 52 | InstanceType: ml.t3.large 53 | NotebookInstanceName: !Ref NotebookName 54 | RoleArn: !GetAtt SageMakerExecutionRole.Arn 55 | DefaultCodeRepository: !Ref DefaultRepoUrl 56 | KmsKeyId: !GetAtt KmsKey.Arn 57 | 58 | Outputs: 59 | NotebookInstanceName: 60 | Description: The name of the created SageMaker Notebook Instance 61 | Value: !Ref SageMakerNotebookInstance 62 | ExecutionRoleArn: 63 | Description: The ARN of the created SageMaker Execution Role 64 | Value: !GetAtt SageMakerExecutionRole.Arn 65 | KmsKeyArn: 66 | Description: The ARN of the created KMS Key for the notebook 67 | Value: !GetAtt KmsKey.Arn 68 | -------------------------------------------------------------------------------- /prompt_engineering_interactive_tutorial/AmazonBedrock/requirements.txt: -------------------------------------------------------------------------------- 1 | awscli==1.32.74 2 | boto3==1.34.74 3 | botocore==1.34.74 4 | anthropic==0.21.3 5 | pickleshare==0.7.5 6 | -------------------------------------------------------------------------------- /prompt_engineering_interactive_tutorial/AmazonBedrock/utils/__init__.py: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/anthropics/courses/b4f26aedef55e06ad5eead5de83985249d1fab2f/prompt_engineering_interactive_tutorial/AmazonBedrock/utils/__init__.py -------------------------------------------------------------------------------- /prompt_engineering_interactive_tutorial/Anthropic 1P/00_Tutorial_How-To.ipynb: -------------------------------------------------------------------------------- 1 | { 2 | "cells": [ 3 | { 4 | "cell_type": "markdown", 5 | "metadata": {}, 6 | "source": [ 7 | "# Tutorial How-To\n", 8 | "\n", 9 | "This tutorial **requires an API key** for interaction. If you don't have an API key, you can sign up for one via the [Anthropic Console](https://console.anthropic.com/) or view our [static tutorial answer key](https://docs.google.com/spreadsheets/u/0/d/1jIxjzUWG-6xBVIa2ay6yDpLyeuOh_hR_ZB75a47KX_E/edit) instead." 10 | ] 11 | }, 12 | { 13 | "cell_type": "markdown", 14 | "metadata": {}, 15 | "source": [ 16 | "## How to get started\n", 17 | "\n", 18 | "1. Clone this repository to your local machine.\n", 19 | "\n", 20 | "2. Install the required dependencies by running the following command:\n", 21 | " " 22 | ] 23 | }, 24 | { 25 | "cell_type": "code", 26 | "execution_count": null, 27 | "metadata": {}, 28 | "outputs": [], 29 | "source": [ 30 | "%pip install anthropic" 31 | ] 32 | }, 33 | { 34 | "cell_type": "markdown", 35 | "metadata": {}, 36 | "source": [ 37 | "3. Set up your API key and model name. Replace `\"your_api_key_here\"` with your actual Anthropic API key." 38 | ] 39 | }, 40 | { 41 | "cell_type": "code", 42 | "execution_count": null, 43 | "metadata": {}, 44 | "outputs": [], 45 | "source": [ 46 | "API_KEY = \"your_api_key_here\"\n", 47 | "MODEL_NAME = \"claude-3-haiku-20240307\"\n", 48 | "\n", 49 | "# Stores the API_KEY & MODEL_NAME variables for use across notebooks within the IPython store\n", 50 | "%store API_KEY\n", 51 | "%store MODEL_NAME" 52 | ] 53 | }, 54 | { 55 | "cell_type": "markdown", 56 | "metadata": {}, 57 | "source": [ 58 | "4. Run the notebook cells in order, following the instructions provided." 59 | ] 60 | }, 61 | { 62 | "cell_type": "markdown", 63 | "metadata": {}, 64 | "source": [ 65 | "---\n", 66 | "\n", 67 | "## Usage Notes & Tips 💡\n", 68 | "\n", 69 | "- This course uses Claude 3 Haiku with temperature 0. We will talk more about temperature later in the course. For now, it's enough to understand that these settings yield more deterministic results. All prompt engineering techniques in this course also apply to previous generation legacy Claude models such as Claude 2 and Claude Instant 1.2.\n", 70 | "\n", 71 | "- You can use `Shift + Enter` to execute the cell and move to the next one.\n", 72 | "\n", 73 | "- When you reach the bottom of a tutorial page, navigate to the next numbered file in the folder, or to the next numbered folder if you're finished with the content within that chapter file.\n", 74 | "\n", 75 | "### The Anthropic SDK & the Messages API\n", 76 | "We will be using the [Anthropic python SDK](https://docs.anthropic.com/claude/reference/client-sdks) and the [Messages API](https://docs.anthropic.com/claude/reference/messages_post) throughout this tutorial. \n", 77 | "\n", 78 | "Below is an example of what running a prompt will look like in this tutorial. First, we create `get_completion`, which is a helper function that sends a prompt to Claude and returns Claude's generated response. Run that cell now." 79 | ] 80 | }, 81 | { 82 | "cell_type": "code", 83 | "execution_count": null, 84 | "metadata": {}, 85 | "outputs": [], 86 | "source": [ 87 | "import anthropic\n", 88 | "\n", 89 | "client = anthropic.Anthropic(api_key=API_KEY)\n", 90 | "\n", 91 | "def get_completion(prompt: str):\n", 92 | " message = client.messages.create(\n", 93 | " model=MODEL_NAME,\n", 94 | " max_tokens=2000,\n", 95 | " temperature=0.0,\n", 96 | " messages=[\n", 97 | " {\"role\": \"user\", \"content\": prompt}\n", 98 | " ]\n", 99 | " )\n", 100 | " return message.content[0].text" 101 | ] 102 | }, 103 | { 104 | "cell_type": "markdown", 105 | "metadata": {}, 106 | "source": [ 107 | "Now we will write out an example prompt for Claude and print Claude's output by running our `get_completion` helper function. Running the cell below will print out a response from Claude beneath it.\n", 108 | "\n", 109 | "Feel free to play around with the prompt string to elicit different responses from Claude." 110 | ] 111 | }, 112 | { 113 | "cell_type": "code", 114 | "execution_count": null, 115 | "metadata": {}, 116 | "outputs": [], 117 | "source": [ 118 | "# Prompt\n", 119 | "prompt = \"Hello, Claude!\"\n", 120 | "\n", 121 | "# Get Claude's response\n", 122 | "print(get_completion(prompt))" 123 | ] 124 | }, 125 | { 126 | "cell_type": "markdown", 127 | "metadata": {}, 128 | "source": [ 129 | "The `API_KEY` and `MODEL_NAME` variables defined earlier will be used throughout the tutorial. Just make sure to run the cells for each tutorial page from top to bottom." 130 | ] 131 | } 132 | ], 133 | "metadata": { 134 | "kernelspec": { 135 | "display_name": "py310", 136 | "language": "python", 137 | "name": "python3" 138 | }, 139 | "language_info": { 140 | "codemirror_mode": { 141 | "name": "ipython", 142 | "version": 3 143 | }, 144 | "file_extension": ".py", 145 | "mimetype": "text/x-python", 146 | "name": "python", 147 | "nbconvert_exporter": "python", 148 | "pygments_lexer": "ipython3", 149 | "version": "3.10.14" 150 | } 151 | }, 152 | "nbformat": 4, 153 | "nbformat_minor": 2 154 | } 155 | -------------------------------------------------------------------------------- /prompt_engineering_interactive_tutorial/Anthropic 1P/02_Being_Clear_and_Direct.ipynb: -------------------------------------------------------------------------------- 1 | { 2 | "cells": [ 3 | { 4 | "cell_type": "markdown", 5 | "metadata": {}, 6 | "source": [ 7 | "# Chapter 2: Being Clear and Direct\n", 8 | "\n", 9 | "- [Lesson](#lesson)\n", 10 | "- [Exercises](#exercises)\n", 11 | "- [Example Playground](#example-playground)\n", 12 | "\n", 13 | "## Setup\n", 14 | "\n", 15 | "Run the following setup cell to load your API key and establish the `get_completion` helper function." 16 | ] 17 | }, 18 | { 19 | "cell_type": "code", 20 | "execution_count": null, 21 | "metadata": {}, 22 | "outputs": [], 23 | "source": [ 24 | "%pip install anthropic\n", 25 | "\n", 26 | "# Import python's built-in regular expression library\n", 27 | "import re\n", 28 | "import anthropic\n", 29 | "\n", 30 | "# Retrieve the API_KEY & MODEL_NAME variables from the IPython store\n", 31 | "%store -r API_KEY\n", 32 | "%store -r MODEL_NAME\n", 33 | "\n", 34 | "client = anthropic.Anthropic(api_key=API_KEY)\n", 35 | "\n", 36 | "# Note that we changed max_tokens to 4K just for this lesson to allow for longer completions in the exercises\n", 37 | "def get_completion(prompt: str, system_prompt=\"\"):\n", 38 | " message = client.messages.create(\n", 39 | " model=MODEL_NAME,\n", 40 | " max_tokens=4000,\n", 41 | " temperature=0.0,\n", 42 | " system=system_prompt,\n", 43 | " messages=[\n", 44 | " {\"role\": \"user\", \"content\": prompt}\n", 45 | " ]\n", 46 | " )\n", 47 | " return message.content[0].text" 48 | ] 49 | }, 50 | { 51 | "cell_type": "markdown", 52 | "metadata": {}, 53 | "source": [ 54 | "---\n", 55 | "\n", 56 | "## Lesson\n", 57 | "\n", 58 | "**Claude responds best to clear and direct instructions.**\n", 59 | "\n", 60 | "Think of Claude like any other human that is new to the job. **Claude has no context** on what to do aside from what you literally tell it. Just as when you instruct a human for the first time on a task, the more you explain exactly what you want in a straightforward manner to Claude, the better and more accurate Claude's response will be.\"\t\t\t\t\n", 61 | "\t\t\t\t\n", 62 | "When in doubt, follow the **Golden Rule of Clear Prompting**:\n", 63 | "- Show your prompt to a colleague or friend and have them follow the instructions themselves to see if they can produce the result you want. If they're confused, Claude's confused.\t\t\t\t" 64 | ] 65 | }, 66 | { 67 | "cell_type": "markdown", 68 | "metadata": {}, 69 | "source": [ 70 | "### Examples\n", 71 | "\n", 72 | "Let's take a task like writing poetry. (Ignore any syllable mismatch - LLMs aren't great at counting syllables yet.)" 73 | ] 74 | }, 75 | { 76 | "cell_type": "code", 77 | "execution_count": null, 78 | "metadata": {}, 79 | "outputs": [], 80 | "source": [ 81 | "# Prompt\n", 82 | "PROMPT = \"Write a haiku about robots.\"\n", 83 | "\n", 84 | "# Print Claude's response\n", 85 | "print(get_completion(PROMPT))" 86 | ] 87 | }, 88 | { 89 | "cell_type": "markdown", 90 | "metadata": {}, 91 | "source": [ 92 | "This haiku is nice enough, but users may want Claude to go directly into the poem without the \"Here is a haiku\" preamble.\n", 93 | "\n", 94 | "How do we achieve that? We **ask for it**!" 95 | ] 96 | }, 97 | { 98 | "cell_type": "code", 99 | "execution_count": null, 100 | "metadata": {}, 101 | "outputs": [], 102 | "source": [ 103 | "# Prompt\n", 104 | "PROMPT = \"Write a haiku about robots. Skip the preamble; go straight into the poem.\"\n", 105 | "\n", 106 | "# Print Claude's response\n", 107 | "print(get_completion(PROMPT))" 108 | ] 109 | }, 110 | { 111 | "cell_type": "markdown", 112 | "metadata": {}, 113 | "source": [ 114 | "Here's another example. Let's ask Claude who's the best basketball player of all time. You can see below that while Claude lists a few names, **it doesn't respond with a definitive \"best\"**." 115 | ] 116 | }, 117 | { 118 | "cell_type": "code", 119 | "execution_count": null, 120 | "metadata": {}, 121 | "outputs": [], 122 | "source": [ 123 | "# Prompt\n", 124 | "PROMPT = \"Who is the best basketball player of all time?\"\n", 125 | "\n", 126 | "# Print Claude's response\n", 127 | "print(get_completion(PROMPT))" 128 | ] 129 | }, 130 | { 131 | "cell_type": "markdown", 132 | "metadata": {}, 133 | "source": [ 134 | "Can we get Claude to make up its mind and decide on a best player? Yes! Just ask!" 135 | ] 136 | }, 137 | { 138 | "cell_type": "code", 139 | "execution_count": null, 140 | "metadata": {}, 141 | "outputs": [], 142 | "source": [ 143 | "# Prompt\n", 144 | "PROMPT = \"Who is the best basketball player of all time? Yes, there are differing opinions, but if you absolutely had to pick one player, who would it be?\"\n", 145 | "\n", 146 | "# Print Claude's response\n", 147 | "print(get_completion(PROMPT))" 148 | ] 149 | }, 150 | { 151 | "cell_type": "markdown", 152 | "metadata": {}, 153 | "source": [ 154 | "If you would like to experiment with the lesson prompts without changing any content above, scroll all the way to the bottom of the lesson notebook to visit the [**Example Playground**](#example-playground)." 155 | ] 156 | }, 157 | { 158 | "cell_type": "markdown", 159 | "metadata": {}, 160 | "source": [ 161 | "---\n", 162 | "\n", 163 | "## Exercises\n", 164 | "- [Exercise 2.1 - Spanish](#exercise-21---spanish)\n", 165 | "- [Exercise 2.2 - One Player Only](#exercise-22---one-player-only)\n", 166 | "- [Exercise 2.3 - Write a Story](#exercise-23---write-a-story)" 167 | ] 168 | }, 169 | { 170 | "cell_type": "markdown", 171 | "metadata": {}, 172 | "source": [ 173 | "### Exercise 2.1 - Spanish\n", 174 | "Modify the `SYSTEM_PROMPT` to make Claude output its answer in Spanish." 175 | ] 176 | }, 177 | { 178 | "cell_type": "code", 179 | "execution_count": null, 180 | "metadata": {}, 181 | "outputs": [], 182 | "source": [ 183 | "# System prompt - this is the only field you should change\n", 184 | "SYSTEM_PROMPT = \"[Replace this text]\"\n", 185 | "\n", 186 | "# Prompt\n", 187 | "PROMPT = \"Hello Claude, how are you?\"\n", 188 | "\n", 189 | "# Get Claude's response\n", 190 | "response = get_completion(PROMPT, SYSTEM_PROMPT)\n", 191 | "\n", 192 | "# Function to grade exercise correctness\n", 193 | "def grade_exercise(text):\n", 194 | " return \"hola\" in text.lower()\n", 195 | "\n", 196 | "# Print Claude's response and the corresponding grade\n", 197 | "print(response)\n", 198 | "print(\"\\n--------------------------- GRADING ---------------------------\")\n", 199 | "print(\"This exercise has been correctly solved:\", grade_exercise(response))" 200 | ] 201 | }, 202 | { 203 | "cell_type": "markdown", 204 | "metadata": {}, 205 | "source": [ 206 | "❓ If you want a hint, run the cell below!" 207 | ] 208 | }, 209 | { 210 | "cell_type": "code", 211 | "execution_count": null, 212 | "metadata": {}, 213 | "outputs": [], 214 | "source": [ 215 | "from hints import exercise_2_1_hint; print(exercise_2_1_hint)" 216 | ] 217 | }, 218 | { 219 | "cell_type": "markdown", 220 | "metadata": {}, 221 | "source": [ 222 | "### Exercise 2.2 - One Player Only\n", 223 | "\n", 224 | "Modify the `PROMPT` so that Claude doesn't equivocate at all and responds with **ONLY** the name of one specific player, with **no other words or punctuation**. " 225 | ] 226 | }, 227 | { 228 | "cell_type": "code", 229 | "execution_count": null, 230 | "metadata": {}, 231 | "outputs": [], 232 | "source": [ 233 | "# Prompt - this is the only field you should change\n", 234 | "PROMPT = \"[Replace this text]\"\n", 235 | "\n", 236 | "# Get Claude's response\n", 237 | "response = get_completion(PROMPT)\n", 238 | "\n", 239 | "# Function to grade exercise correctness\n", 240 | "def grade_exercise(text):\n", 241 | " return text == \"Michael Jordan\"\n", 242 | "\n", 243 | "# Print Claude's response and the corresponding grade\n", 244 | "print(response)\n", 245 | "print(\"\\n--------------------------- GRADING ---------------------------\")\n", 246 | "print(\"This exercise has been correctly solved:\", grade_exercise(response))" 247 | ] 248 | }, 249 | { 250 | "cell_type": "markdown", 251 | "metadata": {}, 252 | "source": [ 253 | "❓ If you want a hint, run the cell below!" 254 | ] 255 | }, 256 | { 257 | "cell_type": "code", 258 | "execution_count": null, 259 | "metadata": {}, 260 | "outputs": [], 261 | "source": [ 262 | "from hints import exercise_2_2_hint; print(exercise_2_2_hint)" 263 | ] 264 | }, 265 | { 266 | "cell_type": "markdown", 267 | "metadata": {}, 268 | "source": [ 269 | "### Exercise 2.3 - Write a Story\n", 270 | "\n", 271 | "Modify the `PROMPT` so that Claude responds with as long a response as you can muster. If your answer is **over 800 words**, Claude's response will be graded as correct." 272 | ] 273 | }, 274 | { 275 | "cell_type": "code", 276 | "execution_count": null, 277 | "metadata": {}, 278 | "outputs": [], 279 | "source": [ 280 | "# Prompt - this is the only field you should change\n", 281 | "PROMPT = \"[Replace this text]\"\n", 282 | "\n", 283 | "# Get Claude's response\n", 284 | "response = get_completion(PROMPT)\n", 285 | "\n", 286 | "# Function to grade exercise correctness\n", 287 | "def grade_exercise(text):\n", 288 | " trimmed = text.strip()\n", 289 | " words = len(trimmed.split())\n", 290 | " return words >= 800\n", 291 | "\n", 292 | "# Print Claude's response and the corresponding grade\n", 293 | "print(response)\n", 294 | "print(\"\\n--------------------------- GRADING ---------------------------\")\n", 295 | "print(\"This exercise has been correctly solved:\", grade_exercise(response))" 296 | ] 297 | }, 298 | { 299 | "cell_type": "markdown", 300 | "metadata": {}, 301 | "source": [ 302 | "❓ If you want a hint, run the cell below!" 303 | ] 304 | }, 305 | { 306 | "cell_type": "code", 307 | "execution_count": null, 308 | "metadata": {}, 309 | "outputs": [], 310 | "source": [ 311 | "from hints import exercise_2_3_hint; print(exercise_2_3_hint)" 312 | ] 313 | }, 314 | { 315 | "cell_type": "markdown", 316 | "metadata": {}, 317 | "source": [ 318 | "### Congrats!\n", 319 | "\n", 320 | "If you've solved all exercises up until this point, you're ready to move to the next chapter. Happy prompting!" 321 | ] 322 | }, 323 | { 324 | "cell_type": "markdown", 325 | "metadata": {}, 326 | "source": [ 327 | "---\n", 328 | "\n", 329 | "## Example Playground\n", 330 | "\n", 331 | "This is an area for you to experiment freely with the prompt examples shown in this lesson and tweak prompts to see how it may affect Claude's responses." 332 | ] 333 | }, 334 | { 335 | "cell_type": "code", 336 | "execution_count": null, 337 | "metadata": {}, 338 | "outputs": [], 339 | "source": [ 340 | "# Prompt\n", 341 | "PROMPT = \"Write a haiku about robots.\"\n", 342 | "\n", 343 | "# Print Claude's response\n", 344 | "print(get_completion(PROMPT))" 345 | ] 346 | }, 347 | { 348 | "cell_type": "code", 349 | "execution_count": null, 350 | "metadata": {}, 351 | "outputs": [], 352 | "source": [ 353 | "# Prompt\n", 354 | "PROMPT = \"Write a haiku about robots. Skip the preamble; go straight into the poem.\"\n", 355 | "\n", 356 | "# Print Claude's response\n", 357 | "print(get_completion(PROMPT))" 358 | ] 359 | }, 360 | { 361 | "cell_type": "code", 362 | "execution_count": null, 363 | "metadata": {}, 364 | "outputs": [], 365 | "source": [ 366 | "# Prompt\n", 367 | "PROMPT = \"Who is the best basketball player of all time?\"\n", 368 | "\n", 369 | "# Print Claude's response\n", 370 | "print(get_completion(PROMPT))" 371 | ] 372 | }, 373 | { 374 | "cell_type": "code", 375 | "execution_count": null, 376 | "metadata": {}, 377 | "outputs": [], 378 | "source": [ 379 | "# Prompt\n", 380 | "PROMPT = \"Who is the best basketball player of all time? Yes, there are differing opinions, but if you absolutely had to pick one player, who would it be?\"\n", 381 | "\n", 382 | "# Print Claude's response\n", 383 | "print(get_completion(PROMPT))" 384 | ] 385 | } 386 | ], 387 | "metadata": { 388 | "language_info": { 389 | "name": "python" 390 | } 391 | }, 392 | "nbformat": 4, 393 | "nbformat_minor": 2 394 | } 395 | -------------------------------------------------------------------------------- /prompt_engineering_interactive_tutorial/Anthropic 1P/03_Assigning_Roles_Role_Prompting.ipynb: -------------------------------------------------------------------------------- 1 | { 2 | "cells": [ 3 | { 4 | "cell_type": "markdown", 5 | "metadata": {}, 6 | "source": [ 7 | "# Chapter 3: Assigning Roles (Role Prompting)\n", 8 | "\n", 9 | "- [Lesson](#lesson)\n", 10 | "- [Exercises](#exercises)\n", 11 | "- [Example Playground](#example-playground)\n", 12 | "\n", 13 | "## Setup\n", 14 | "\n", 15 | "Run the following setup cell to load your API key and establish the `get_completion` helper function." 16 | ] 17 | }, 18 | { 19 | "cell_type": "code", 20 | "execution_count": null, 21 | "metadata": {}, 22 | "outputs": [], 23 | "source": [ 24 | "%pip install anthropic\n", 25 | "\n", 26 | "# Import python's built-in regular expression library\n", 27 | "import re\n", 28 | "import anthropic\n", 29 | "\n", 30 | "# Retrieve the API_KEY & MODEL_NAME variables from the IPython store\n", 31 | "%store -r API_KEY\n", 32 | "%store -r MODEL_NAME\n", 33 | "\n", 34 | "client = anthropic.Anthropic(api_key=API_KEY)\n", 35 | "\n", 36 | "def get_completion(prompt: str, system_prompt=\"\"):\n", 37 | " message = client.messages.create(\n", 38 | " model=MODEL_NAME,\n", 39 | " max_tokens=2000,\n", 40 | " temperature=0.0,\n", 41 | " system=system_prompt,\n", 42 | " messages=[\n", 43 | " {\"role\": \"user\", \"content\": prompt}\n", 44 | " ]\n", 45 | " )\n", 46 | " return message.content[0].text" 47 | ] 48 | }, 49 | { 50 | "cell_type": "markdown", 51 | "metadata": {}, 52 | "source": [ 53 | "---\n", 54 | "\n", 55 | "## Lesson\n", 56 | "\n", 57 | "Continuing on the theme of Claude having no context aside from what you say, it's sometimes important to **prompt Claude to inhabit a specific role (including all necessary context)**. This is also known as role prompting. The more detail to the role context, the better.\n", 58 | "\n", 59 | "**Priming Claude with a role can improve Claude's performance** in a variety of fields, from writing to coding to summarizing. It's like how humans can sometimes be helped when told to \"think like a ______\". Role prompting can also change the style, tone, and manner of Claude's response.\n", 60 | "\n", 61 | "**Note:** Role prompting can happen either in the system prompt or as part of the User message turn." 62 | ] 63 | }, 64 | { 65 | "cell_type": "markdown", 66 | "metadata": {}, 67 | "source": [ 68 | "### Examples\n", 69 | "\n", 70 | "In the example below, we see that without role prompting, Claude provides a **straightforward and non-stylized answer** when asked to give a single sentence perspective on skateboarding.\n", 71 | "\n", 72 | "However, when we prime Claude to inhabit the role of a cat, Claude's perspective changes, and thus **Claude's response tone, style, content adapts to the new role**. \n", 73 | "\n", 74 | "**Note:** A bonus technique you can use is to **provide Claude context on its intended audience**. Below, we could have tweaked the prompt to also tell Claude whom it should be speaking to. \"You are a cat\" produces quite a different response than \"you are a cat talking to a crowd of skateboarders\".\n", 75 | "\n", 76 | "Here is the prompt without role prompting in the system prompt:" 77 | ] 78 | }, 79 | { 80 | "cell_type": "code", 81 | "execution_count": null, 82 | "metadata": {}, 83 | "outputs": [], 84 | "source": [ 85 | "# Prompt\n", 86 | "PROMPT = \"In one sentence, what do you think about skateboarding?\"\n", 87 | "\n", 88 | "# Print Claude's response\n", 89 | "print(get_completion(PROMPT))" 90 | ] 91 | }, 92 | { 93 | "cell_type": "markdown", 94 | "metadata": {}, 95 | "source": [ 96 | "Here is the same user question, except with role prompting." 97 | ] 98 | }, 99 | { 100 | "cell_type": "code", 101 | "execution_count": null, 102 | "metadata": {}, 103 | "outputs": [], 104 | "source": [ 105 | "# System prompt\n", 106 | "SYSTEM_PROMPT = \"You are a cat.\"\n", 107 | "\n", 108 | "# Prompt\n", 109 | "PROMPT = \"In one sentence, what do you think about skateboarding?\"\n", 110 | "\n", 111 | "# Print Claude's response\n", 112 | "print(get_completion(PROMPT, SYSTEM_PROMPT))" 113 | ] 114 | }, 115 | { 116 | "cell_type": "markdown", 117 | "metadata": {}, 118 | "source": [ 119 | "You can use role prompting as a way to get Claude to emulate certain styles in writing, speak in a certain voice, or guide the complexity of its answers. **Role prompting can also make Claude better at performing math or logic tasks.**\n", 120 | "\n", 121 | "For example, in the example below, there is a definitive correct answer, which is yes. However, Claude gets it wrong and thinks it lacks information, which it doesn't:" 122 | ] 123 | }, 124 | { 125 | "cell_type": "code", 126 | "execution_count": null, 127 | "metadata": {}, 128 | "outputs": [], 129 | "source": [ 130 | "# Prompt\n", 131 | "PROMPT = \"Jack is looking at Anne. Anne is looking at George. Jack is married, George is not, and we don’t know if Anne is married. Is a married person looking at an unmarried person?\"\n", 132 | "\n", 133 | "# Print Claude's response\n", 134 | "print(get_completion(PROMPT))" 135 | ] 136 | }, 137 | { 138 | "cell_type": "markdown", 139 | "metadata": {}, 140 | "source": [ 141 | "Now, what if we **prime Claude to act as a logic bot**? How will that change Claude's answer? \n", 142 | "\n", 143 | "It turns out that with this new role assignment, Claude gets it right. (Although notably not for all the right reasons)" 144 | ] 145 | }, 146 | { 147 | "cell_type": "code", 148 | "execution_count": null, 149 | "metadata": {}, 150 | "outputs": [], 151 | "source": [ 152 | "# System prompt\n", 153 | "SYSTEM_PROMPT = \"You are a logic bot designed to answer complex logic problems.\"\n", 154 | "\n", 155 | "# Prompt\n", 156 | "PROMPT = \"Jack is looking at Anne. Anne is looking at George. Jack is married, George is not, and we don’t know if Anne is married. Is a married person looking at an unmarried person?\"\n", 157 | "\n", 158 | "# Print Claude's response\n", 159 | "print(get_completion(PROMPT, SYSTEM_PROMPT))" 160 | ] 161 | }, 162 | { 163 | "cell_type": "markdown", 164 | "metadata": {}, 165 | "source": [ 166 | "**Note:** What you'll learn throughout this course is that there are **many prompt engineering techniques you can use to derive similar results**. Which techniques you use is up to you and your preference! We encourage you to **experiment to find your own prompt engineering style**.\n", 167 | "\n", 168 | "If you would like to experiment with the lesson prompts without changing any content above, scroll all the way to the bottom of the lesson notebook to visit the [**Example Playground**](#example-playground)." 169 | ] 170 | }, 171 | { 172 | "cell_type": "markdown", 173 | "metadata": {}, 174 | "source": [ 175 | "---\n", 176 | "\n", 177 | "## Exercises\n", 178 | "- [Exercise 3.1 - Math Correction](#exercise-31---math-correction)" 179 | ] 180 | }, 181 | { 182 | "cell_type": "markdown", 183 | "metadata": {}, 184 | "source": [ 185 | "### Exercise 3.1 - Math Correction\n", 186 | "In some instances, **Claude may struggle with mathematics**, even simple mathematics. Below, Claude incorrectly assesses the math problem as correctly solved, even though there's an obvious arithmetic mistake in the second step. Note that Claude actually catches the mistake when going through step-by-step, but doesn't jump to the conclusion that the overall solution is wrong.\n", 187 | "\n", 188 | "Modify the `PROMPT` and / or the `SYSTEM_PROMPT` to make Claude grade the solution as `incorrectly` solved, rather than correctly solved. \n" 189 | ] 190 | }, 191 | { 192 | "cell_type": "code", 193 | "execution_count": null, 194 | "metadata": {}, 195 | "outputs": [], 196 | "source": [ 197 | "# System prompt - if you don't want to use a system prompt, you can leave this variable set to an empty string\n", 198 | "SYSTEM_PROMPT = \"\"\n", 199 | "\n", 200 | "# Prompt\n", 201 | "PROMPT = \"\"\"Is this equation solved correctly below?\n", 202 | "\n", 203 | "2x - 3 = 9\n", 204 | "2x = 6\n", 205 | "x = 3\"\"\"\n", 206 | "\n", 207 | "# Get Claude's response\n", 208 | "response = get_completion(PROMPT, SYSTEM_PROMPT)\n", 209 | "\n", 210 | "# Function to grade exercise correctness\n", 211 | "def grade_exercise(text):\n", 212 | " if \"incorrect\" in text or \"not correct\" in text.lower():\n", 213 | " return True\n", 214 | " else:\n", 215 | " return False\n", 216 | "\n", 217 | "# Print Claude's response and the corresponding grade\n", 218 | "print(response)\n", 219 | "print(\"\\n--------------------------- GRADING ---------------------------\")\n", 220 | "print(\"This exercise has been correctly solved:\", grade_exercise(response))" 221 | ] 222 | }, 223 | { 224 | "cell_type": "markdown", 225 | "metadata": {}, 226 | "source": [ 227 | "❓ If you want a hint, run the cell below!" 228 | ] 229 | }, 230 | { 231 | "cell_type": "code", 232 | "execution_count": null, 233 | "metadata": {}, 234 | "outputs": [], 235 | "source": [ 236 | "from hints import exercise_3_1_hint; print(exercise_3_1_hint)" 237 | ] 238 | }, 239 | { 240 | "cell_type": "markdown", 241 | "metadata": {}, 242 | "source": [ 243 | "### Congrats!\n", 244 | "\n", 245 | "If you've solved all exercises up until this point, you're ready to move to the next chapter. Happy prompting!" 246 | ] 247 | }, 248 | { 249 | "cell_type": "markdown", 250 | "metadata": {}, 251 | "source": [ 252 | "---\n", 253 | "\n", 254 | "## Example Playground\n", 255 | "\n", 256 | "This is an area for you to experiment freely with the prompt examples shown in this lesson and tweak prompts to see how it may affect Claude's responses." 257 | ] 258 | }, 259 | { 260 | "cell_type": "code", 261 | "execution_count": null, 262 | "metadata": {}, 263 | "outputs": [], 264 | "source": [ 265 | "# Prompt\n", 266 | "PROMPT = \"In one sentence, what do you think about skateboarding?\"\n", 267 | "\n", 268 | "# Print Claude's response\n", 269 | "print(get_completion(PROMPT))" 270 | ] 271 | }, 272 | { 273 | "cell_type": "code", 274 | "execution_count": null, 275 | "metadata": {}, 276 | "outputs": [], 277 | "source": [ 278 | "# System prompt\n", 279 | "SYSTEM_PROMPT = \"You are a cat.\"\n", 280 | "\n", 281 | "# Prompt\n", 282 | "PROMPT = \"In one sentence, what do you think about skateboarding?\"\n", 283 | "\n", 284 | "# Print Claude's response\n", 285 | "print(get_completion(PROMPT, SYSTEM_PROMPT))" 286 | ] 287 | }, 288 | { 289 | "cell_type": "code", 290 | "execution_count": null, 291 | "metadata": {}, 292 | "outputs": [], 293 | "source": [ 294 | "# Prompt\n", 295 | "PROMPT = \"Jack is looking at Anne. Anne is looking at George. Jack is married, George is not, and we don’t know if Anne is married. Is a married person looking at an unmarried person?\"\n", 296 | "\n", 297 | "# Print Claude's response\n", 298 | "print(get_completion(PROMPT))" 299 | ] 300 | }, 301 | { 302 | "cell_type": "code", 303 | "execution_count": null, 304 | "metadata": {}, 305 | "outputs": [], 306 | "source": [ 307 | "# System prompt\n", 308 | "SYSTEM_PROMPT = \"You are a logic bot designed to answer complex logic problems.\"\n", 309 | "\n", 310 | "# Prompt\n", 311 | "PROMPT = \"Jack is looking at Anne. Anne is looking at George. Jack is married, George is not, and we don’t know if Anne is married. Is a married person looking at an unmarried person?\"\n", 312 | "\n", 313 | "# Print Claude's response\n", 314 | "print(get_completion(PROMPT, SYSTEM_PROMPT))" 315 | ] 316 | } 317 | ], 318 | "metadata": { 319 | "language_info": { 320 | "name": "python" 321 | } 322 | }, 323 | "nbformat": 4, 324 | "nbformat_minor": 2 325 | } 326 | -------------------------------------------------------------------------------- /prompt_engineering_interactive_tutorial/Anthropic 1P/10.3_Appendix_Search & Retrieval.ipynb: -------------------------------------------------------------------------------- 1 | { 2 | "cells": [ 3 | { 4 | "cell_type": "markdown", 5 | "metadata": {}, 6 | "source": [ 7 | "# Appendix 10.3: Search & Retrieval\n", 8 | "\n", 9 | "Did you know you can use Claude to **search through Wikipedia for you**? Claude can find and retrieve articles, at which point you can also use Claude to summarize and synthesize them, write novel content from what it found, and much more. And not just Wikipedia! You can also search over your own docs, whether stored as plain text or embedded in a vector datastore.\n", 10 | "\n", 11 | "See our [RAG cookbook examples](https://github.com/anthropics/anthropic-cookbook/blob/main/third_party/Wikipedia/wikipedia-search-cookbook.ipynb) to learn how to supplement Claude's knowledge and improve the accuracy and relevance of Claude's responses with data retrieved from vector databases, Wikipedia, the internet, and more. There, you can also learn about how to use certain [embeddings](https://docs.anthropic.com/claude/docs/embeddings) and vector database tools.\n", 12 | "\n", 13 | "If you are interested in learning about advanced RAG architectures using Claude, check out our [Claude 3 technical presentation slides on RAG architectures](https://docs.google.com/presentation/d/1zxkSI7lLUBrZycA-_znwqu8DDyVhHLkQGScvzaZrUns/edit#slide=id.g2c736259dac_63_782)." 14 | ] 15 | } 16 | ], 17 | "metadata": { 18 | "language_info": { 19 | "name": "python" 20 | } 21 | }, 22 | "nbformat": 4, 23 | "nbformat_minor": 2 24 | } 25 | -------------------------------------------------------------------------------- /prompt_engineering_interactive_tutorial/Anthropic 1P/hints.py: -------------------------------------------------------------------------------- 1 | exercise_1_1_hint = """The grading function in this exercise is looking for an answer that contains the exact Arabic numerals "1", "2", and "3". 2 | You can often get Claude to do what you want simply by asking.""" 3 | 4 | exercise_1_2_hint = """The grading function in this exercise is looking for answers that contain "soo" or "giggles". 5 | There are many ways to solve this, just by asking!""" 6 | 7 | exercise_2_1_hint ="""The grading function in this exercise is looking for any answer that includes the word "hola". 8 | Ask Claude to reply in Spanish like you would when speaking with a human. It's that simple!""" 9 | 10 | exercise_2_2_hint = """The grading function in this exercise is looking for EXACTLY "Michael Jordan". 11 | How would you ask another human to do this? Reply with no other words? Reply with only the name and nothing else? There are several ways to approach this answer.""" 12 | 13 | exercise_2_3_hint = """The grading function in this cell is looking for a response that is equal to or greater than 800 words. 14 | Because LLMs aren't great at counting words yet, you may have to overshoot your target.""" 15 | 16 | exercise_3_1_hint = """The grading function in this exercise is looking for an answer that includes the words "incorrect" or "not correct". 17 | Give Claude a role that might make Claude better at solving math problems!""" 18 | 19 | exercise_4_1_hint = """The grading function in this exercise is looking for a solution that includes the words "haiku" and "pig". 20 | Don't forget to include the exact phrase "{TOPIC}" wherever you want the topic to be substituted in. Changing the "TOPIC" variable value should make Claude write a haiku about a different topic.""" 21 | 22 | exercise_4_2_hint = """The grading function in this exercise is looking for a response that includes the word "brown". 23 | If you surround "{QUESTION}" in XML tags, how does that change Claude's response?""" 24 | 25 | exercise_4_3_hint = """The grading function in this exercise is looking for a response that includes the word "brown". 26 | Try removing one word or section of characters at a time, starting with the parts that make the least sense. Doing this one word at a time will also help you see just how much Claude can or can't parse and understand.""" 27 | 28 | exercise_5_1_hint = """The grading function for this exercise is looking for a response that includes the word "Warrior". 29 | Write more words in Claude's voice to steer Claude to act the way you want it to. For instance, instead of "Stephen Curry is the best because," you could write "Stephen Curry is the best and here are three reasons why. 1:""" 30 | 31 | exercise_5_2_hint = """The grading function looks for a response of over 5 lines in length that includes the words "cat" and "". 32 | Start simple. Currently, the prompt asks Claude for one haiku. You can change that and ask for two (or even more). Then if you run into formatting issues, change your prompt to fix that after you've already gotten Claude to write more than one haiku.""" 33 | 34 | exercise_5_3_hint = """The grading function in this exercise is looking for a response that contains the words "tail", "cat", and "". 35 | It's helpful to break this exercise down to several steps. 36 | 1. Modify the initial prompt template so that Claude writes two poems. 37 | 2. Give Claude indicators as to what the poems will be about, but instead of writing in the subjects directly (e.g., dog, cat, etc.), replace those subjects with the keywords "{ANIMAL1}" and "{ANIMAL2}". 38 | 3. Run the prompt and make sure that the full prompt with variable substitutions has all the words correctly substituted. If not, check to make sure your {bracket} tags are spelled correctly and formatted correctly with single moustache brackets.""" 39 | 40 | exercise_6_1_hint = """The grading function in this exercise is looking for the correct categorization letter + the closing parentheses and the first letter of the name of the category, such as "C) B" or "B) B" etc. 41 | Let's take this exercise step by step: 42 | 1. How will Claude know what categories you want to use? Tell it! Include the four categories you want directly in the prompt. Be sure to include the parenthetical letters as well for easy classification. Feel free to use XML tags to organize your prompt and make clear to Claude where the categories begin and end. 43 | 2. Try to cut down on superfluous text so that Claude immediately answers with the classification and ONLY the classification. There are several ways to do this, from speaking for Claude (providing anything from the beginning of the sentence to a single open parenthesis so that Claude knows you want the parenthetical letter as the first part of the answer) to telling Claude that you want the classification and only the classification, skipping the preamble. 44 | Refer to Chapters 2 and 5 if you want a refresher on these techniques. 45 | 3. Claude may still be incorrectly categorizing or not including the names of the categories when it answers. Fix this by telling Claude to include the full category name in its answer.) 46 | 4. Be sure that you still have {email} somewhere in your prompt template so that we can properly substitute in emails for Claude to evaluate.""" 47 | 48 | exercise_6_1_solution = """ 49 | USER TURN 50 | Please classify this email into the following categories: {email} 51 | 52 | Do not include any extra words except the category. 53 | 54 | 55 | (A) Pre-sale question 56 | (B) Broken or defective item 57 | (C) Billing question 58 | (D) Other (please explain) 59 | 60 | 61 | ASSISTANT TURN 62 | ( 63 | """ 64 | 65 | exercise_6_2_hint = """The grading function in this exercise is looking for only the correct letter wrapped in tags, such as "B". The correct categorization letters are the same as in the above exercise. 66 | Sometimes the simplest way to go about this is to give Claude an example of how you want its output to look. Just don't forget to wrap your example in tags! And don't forget that if you prefill Claude's response with anything, Claude won't actually output that as part of its response.""" 67 | 68 | exercise_7_1_hint = """You're going to have to write some example emails and classify them for Claude (with the exact formatting you want). There are multiple ways to do this. Here are some guidelines below. 69 | 1. Try to have at least two example emails. Claude doesn't need an example for all categories, and the examples don't have to be long. It's more helpful to have examples for whatever you think the trickier categories are (which you were asked to think about at the bottom of Chapter 6 Exercise 1). XML tags will help you separate out your examples from the rest of your prompt, although it's unnecessary. 70 | 2. Make sure your example answer formatting is exactly the format you want Claude to use, so Claude can emulate the format as well. This format should make it so that Claude's answer ends in the letter of the category. Wherever you put the {email} placeholder, make sure that it's formatted exactly like your example emails. 71 | 3. Make sure you still have the categories listed within the prompt itself, otherwise Claude won't know what categories to reference, as well as {email} as a placeholder for substitution.""" 72 | 73 | exercise_7_1_solution = """ 74 | USER TURN 75 | Please classify emails into the following categories, and do not include explanations: 76 | 77 | (A) Pre-sale question 78 | (B) Broken or defective item 79 | (C) Billing question 80 | (D) Other (please explain) 81 | 82 | 83 | Here are a few examples of correct answer formatting: 84 | 85 | Q: How much does it cost to buy a Mixmaster4000? 86 | A: The correct category is: A 87 | 88 | Q: My Mixmaster won't turn on. 89 | A: The correct category is: B 90 | 91 | Q: Please remove me from your mailing list. 92 | A: The correct category is: D 93 | 94 | 95 | Here is the email for you to categorize: {email} 96 | 97 | ASSISTANT TURN 98 | The correct category is: 99 | """ 100 | exercise_8_1_hint = """The grading function in this exercise is looking for a response that contains the phrase "I do not", "I don't", or "Unfortunately". 101 | What should Claude do if it doesn't know the answer?""" 102 | 103 | exercise_8_2_hint = """The grading function in this exercise is looking for a response that contains the phrase "49-fold". 104 | Make Claude show its work and thought process first by extracting relevant quotes and seeing whether or not the quotes provide sufficient evidence. Refer back to the Chapter 8 Lesson if you want a refresher.""" 105 | 106 | exercise_9_1_solution = """ 107 | You are a master tax acountant. Your task is to answer user questions using any provided reference documentation. 108 | 109 | Here is the material you should use to answer the user's question: 110 | 111 | {TAX_CODE} 112 | 113 | 114 | Here is an example of how to respond: 115 | 116 | 117 | What defines a "qualified" employee? 118 | 119 | 120 | For purposes of this subsection— 121 | (A)In general 122 | The term "qualified employee" means any individual who— 123 | (i)is not an excluded employee, and 124 | (ii)agrees in the election made under this subsection to meet such requirements as are determined by the Secretary to be necessary to ensure that the withholding requirements of the corporation under chapter 24 with respect to the qualified stock are met. 125 | 126 | According to the provided documentation, a "qualified employee" is defined as an individual who: 127 | 128 | 1. Is not an "excluded employee" as defined in the documentation. 129 | 2. Agrees to meet the requirements determined by the Secretary to ensure the corporation's withholding requirements under Chapter 24 are met with respect to the qualified stock. 130 | 131 | 132 | First, gather quotes in tags that are relevant to answering the user's question. If there are no quotes, write "no relevant quotes found". 133 | 134 | Then insert two paragraph breaks before answering the user question within tags. Only answer the user's question if you are confident that the quotes in tags support your answer. If not, tell the user that you unfortunately do not have enough information to answer the user's question. 135 | 136 | Here is the user question: {QUESTION} 137 | """ 138 | 139 | exercise_9_2_solution = """ 140 | You are Codebot, a helpful AI assistant who finds issues with code and suggests possible improvements. 141 | 142 | Act as a Socratic tutor who helps the user learn. 143 | 144 | You will be given some code from a user. Please do the following: 145 | 1. Identify any issues in the code. Put each issue inside separate tags. 146 | 2. Invite the user to write a revised version of the code to fix the issue. 147 | 148 | Here's an example: 149 | 150 | 151 | 152 | def calculate_circle_area(radius): 153 | return (3.14 * radius) ** 2 154 | 155 | 156 | 157 | 3.14 is being squared when it's actually only the radius that should be squared> 158 | 159 | 160 | That's almost right, but there's an issue related to order of operations. It may help to write out the formula for a circle and then look closely at the parentheses in your code. 161 | 162 | 163 | 164 | Here is the code you are to analyze: 165 | 166 | 167 | {CODE} 168 | 169 | 170 | Find the relevant issues and write the Socratic tutor-style response. Do not give the user too much help! Instead, just give them guidance so they can find the correct solution themselves. 171 | 172 | Put each issue in tags and put your final response in tags. 173 | """ 174 | 175 | exercise_10_2_1_solution = """system_prompt = system_prompt_tools_general_explanation + \"""Here are the functions available in JSONSchema format: 176 | 177 | 178 | 179 | 180 | get_user 181 | 182 | Retrieves a user from the database by their user ID. 183 | 184 | 185 | 186 | user_id 187 | int 188 | The ID of the user to retrieve. 189 | 190 | 191 | 192 | 193 | 194 | get_product 195 | 196 | Retrieves a product from the database by its product ID. 197 | 198 | 199 | 200 | product_id 201 | int 202 | The ID of the product to retrieve. 203 | 204 | 205 | 206 | 207 | 208 | add_user 209 | 210 | Adds a new user to the database. 211 | 212 | 213 | 214 | name 215 | str 216 | The name of the user. 217 | 218 | 219 | email 220 | str 221 | The email address of the user. 222 | 223 | 224 | 225 | 226 | 227 | add_product 228 | 229 | Adds a new product to the database. 230 | 231 | 232 | 233 | name 234 | str 235 | The name of the product. 236 | 237 | 238 | price 239 | float 240 | The price of the product. 241 | 242 | 243 | 244 | 245 | 246 | """ -------------------------------------------------------------------------------- /prompt_engineering_interactive_tutorial/README.md: -------------------------------------------------------------------------------- 1 | # Welcome to Anthropic's Prompt Engineering Interactive Tutorial 2 | 3 | ## Course introduction and goals 4 | 5 | This course is intended to provide you with a comprehensive step-by-step understanding of how to engineer optimal prompts within Claude. 6 | 7 | **After completing this course, you will be able to**: 8 | - Master the basic structure of a good prompt 9 | - Recognize common failure modes and learn the '80/20' techniques to address them 10 | - Understand Claude's strengths and weaknesses 11 | - Build strong prompts from scratch for common use cases 12 | 13 | ## Course structure and content 14 | 15 | This course is structured to allow you many chances to practice writing and troubleshooting prompts yourself. The course is broken up into **9 chapters with accompanying exercises**, as well as an appendix of even more advanced methods. It is intended for you to **work through the course in chapter order**. 16 | 17 | **Each lesson has an "Example Playground" area** at the bottom where you are free to experiment with the examples in the lesson and see for yourself how changing prompts can change Claude's responses. There is also an [answer key](https://docs.google.com/spreadsheets/d/1jIxjzUWG-6xBVIa2ay6yDpLyeuOh_hR_ZB75a47KX_E/edit?usp=sharing). 18 | 19 | Note: This tutorial uses our smallest, fastest, and cheapest model, Claude 3 Haiku. Anthropic has [two other models](https://docs.anthropic.com/claude/docs/models-overview), Claude 3 Sonnet and Claude 3 Opus, which are more intelligent than Haiku, with Opus being the most intelligent. 20 | 21 | *This tutorial also exists on [Google Sheets using Anthropic's Claude for Sheets extension](https://docs.google.com/spreadsheets/d/19jzLgRruG9kjUQNKtCg1ZjdD6l6weA6qRXG5zLIAhC8/edit?usp=sharing). We recommend using that version as it is more user friendly.* 22 | 23 | When you are ready to begin, go to `01_Basic Prompt Structure` to proceed. 24 | 25 | ## Table of Contents 26 | 27 | Each chapter consists of a lesson and a set of exercises. 28 | 29 | ### Beginner 30 | - **Chapter 1:** Basic Prompt Structure 31 | 32 | - **Chapter 2:** Being Clear and Direct 33 | 34 | - **Chapter 3:** Assigning Roles 35 | 36 | ### Intermediate 37 | - **Chapter 4:** Separating Data from Instructions 38 | 39 | - **Chapter 5:** Formatting Output & Speaking for Claude 40 | 41 | - **Chapter 6:** Precognition (Thinking Step by Step) 42 | 43 | - **Chapter 7:** Using Examples 44 | 45 | ### Advanced 46 | - **Chapter 8:** Avoiding Hallucinations 47 | 48 | - **Chapter 9:** Building Complex Prompts (Industry Use Cases) 49 | - Complex Prompts from Scratch - Chatbot 50 | - Complex Prompts for Legal Services 51 | - **Exercise:** Complex Prompts for Financial Services 52 | - **Exercise:** Complex Prompts for Coding 53 | - Congratulations & Next Steps 54 | 55 | - **Appendix:** Beyond Standard Prompting 56 | - Chaining Prompts 57 | - Tool Use 58 | - Search & Retrieval -------------------------------------------------------------------------------- /prompt_evaluations/05_prompt_foo_code_graded_animals/README.md: -------------------------------------------------------------------------------- 1 | To get started, set your ANTHROPIC_API_KEY environment variable 2 | 3 | Then run: 4 | ``` 5 | promptfoo eval 6 | ``` 7 | 8 | Afterwards, you can view the results by running `promptfoo view` 9 | -------------------------------------------------------------------------------- /prompt_evaluations/05_prompt_foo_code_graded_animals/animal_legs_tests.csv: -------------------------------------------------------------------------------- 1 | animal_statement,__expected 2 | "The animal is a human.","2" 3 | "The animal is a snake.","0" 4 | "The fox lost a leg, but then magically grew back the leg he lost and a mysterious extra leg on top of that.","5" 5 | "The animal is a dog.","4" 6 | "The animal is a cat with two extra legs.","6" 7 | "The animal is an elephant.","4" 8 | "The animal is a bird.","2" 9 | "The animal is a fish.","0" 10 | "The animal is a spider with two extra legs","10" 11 | "The animal is an octopus.","8" 12 | "The animal is an octopus that lost two legs and then regrew three legs.","9" 13 | "The animal is a two-headed, eight-legged mythical creature.","8" -------------------------------------------------------------------------------- /prompt_evaluations/05_prompt_foo_code_graded_animals/images/details.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/anthropics/courses/b4f26aedef55e06ad5eead5de83985249d1fab2f/prompt_evaluations/05_prompt_foo_code_graded_animals/images/details.png -------------------------------------------------------------------------------- /prompt_evaluations/05_prompt_foo_code_graded_animals/images/eval_output.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/anthropics/courses/b4f26aedef55e06ad5eead5de83985249d1fab2f/prompt_evaluations/05_prompt_foo_code_graded_animals/images/eval_output.png -------------------------------------------------------------------------------- /prompt_evaluations/05_prompt_foo_code_graded_animals/images/eval_output1.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/anthropics/courses/b4f26aedef55e06ad5eead5de83985249d1fab2f/prompt_evaluations/05_prompt_foo_code_graded_animals/images/eval_output1.png -------------------------------------------------------------------------------- /prompt_evaluations/05_prompt_foo_code_graded_animals/images/eval_results1.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/anthropics/courses/b4f26aedef55e06ad5eead5de83985249d1fab2f/prompt_evaluations/05_prompt_foo_code_graded_animals/images/eval_results1.png -------------------------------------------------------------------------------- /prompt_evaluations/05_prompt_foo_code_graded_animals/images/eval_view.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/anthropics/courses/b4f26aedef55e06ad5eead5de83985249d1fab2f/prompt_evaluations/05_prompt_foo_code_graded_animals/images/eval_view.png -------------------------------------------------------------------------------- /prompt_evaluations/05_prompt_foo_code_graded_animals/images/eval_view1.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/anthropics/courses/b4f26aedef55e06ad5eead5de83985249d1fab2f/prompt_evaluations/05_prompt_foo_code_graded_animals/images/eval_view1.png -------------------------------------------------------------------------------- /prompt_evaluations/05_prompt_foo_code_graded_animals/images/final_view.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/anthropics/courses/b4f26aedef55e06ad5eead5de83985249d1fab2f/prompt_evaluations/05_prompt_foo_code_graded_animals/images/final_view.png -------------------------------------------------------------------------------- /prompt_evaluations/05_prompt_foo_code_graded_animals/images/multi_model_eval_view.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/anthropics/courses/b4f26aedef55e06ad5eead5de83985249d1fab2f/prompt_evaluations/05_prompt_foo_code_graded_animals/images/multi_model_eval_view.png -------------------------------------------------------------------------------- /prompt_evaluations/05_prompt_foo_code_graded_animals/images/prompt_foo.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/anthropics/courses/b4f26aedef55e06ad5eead5de83985249d1fab2f/prompt_evaluations/05_prompt_foo_code_graded_animals/images/prompt_foo.png -------------------------------------------------------------------------------- /prompt_evaluations/05_prompt_foo_code_graded_animals/images/three_prompt_eval.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/anthropics/courses/b4f26aedef55e06ad5eead5de83985249d1fab2f/prompt_evaluations/05_prompt_foo_code_graded_animals/images/three_prompt_eval.png -------------------------------------------------------------------------------- /prompt_evaluations/05_prompt_foo_code_graded_animals/images/toolbar.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/anthropics/courses/b4f26aedef55e06ad5eead5de83985249d1fab2f/prompt_evaluations/05_prompt_foo_code_graded_animals/images/toolbar.png -------------------------------------------------------------------------------- /prompt_evaluations/05_prompt_foo_code_graded_animals/package.json: -------------------------------------------------------------------------------- 1 | { 2 | "dependencies": { 3 | "promptfoo": "^0.78.0" 4 | } 5 | } 6 | -------------------------------------------------------------------------------- /prompt_evaluations/05_prompt_foo_code_graded_animals/promptfooconfig.yaml: -------------------------------------------------------------------------------- 1 | description: "Animal Legs Eval" 2 | 3 | prompts: 4 | - prompts.py:simple_prompt 5 | - prompts.py:better_prompt 6 | - prompts.py:chain_of_thought_prompt 7 | 8 | providers: 9 | - anthropic:messages:claude-3-haiku-20240307 10 | - anthropic:messages:claude-3-5-sonnet-20240620 11 | 12 | tests: animal_legs_tests.csv 13 | 14 | defaultTest: 15 | options: 16 | transform: file://transform.py 17 | 18 | 19 | -------------------------------------------------------------------------------- /prompt_evaluations/05_prompt_foo_code_graded_animals/prompts.py: -------------------------------------------------------------------------------- 1 | def simple_prompt(animal_statement): 2 | return f"""You will be provided a statement about an animal and your job is to determine how many legs that animal has. 3 | 4 | Here is the animal statement. 5 | {animal_statement} 6 | 7 | How many legs does the animal have? Please respond with a number""" 8 | 9 | def better_prompt(animal_statement): 10 | return f"""You will be provided a statement about an animal and your job is to determine how many legs that animal has. 11 | 12 | Here is the animal statement. 13 | {animal_statement} 14 | 15 | How many legs does the animal have? Please only respond with a single digit like 2 or 9""" 16 | 17 | def chain_of_thought_prompt(animal_statement): 18 | return f"""You will be provided a statement about an animal and your job is to determine how many legs that animal has. 19 | 20 | Here is the animal statement. 21 | {animal_statement} 22 | 23 | How many legs does the animal have? 24 | Start by reasoning about the numbers of legs the animal has, thinking step by step inside of tags. 25 | Then, output your final answer inside of tags. 26 | Inside the tags return just the number of legs as an integer and nothing else.""" -------------------------------------------------------------------------------- /prompt_evaluations/05_prompt_foo_code_graded_animals/transform.py: -------------------------------------------------------------------------------- 1 | def get_transform(output, context): 2 | if "" in output: 3 | try: 4 | return output.split("")[1].split("")[0].strip() 5 | except Exception as e: 6 | print(f"Error in get_transform: {e}") 7 | return output 8 | return output 9 | 10 | -------------------------------------------------------------------------------- /prompt_evaluations/06_prompt_foo_code_graded_classification/README.md: -------------------------------------------------------------------------------- 1 | To get started, set your ANTHROPIC_API_KEY environment variable 2 | 3 | Then run: 4 | ``` 5 | promptfoo eval 6 | ``` 7 | 8 | Afterwards, you can view the results by running `promptfoo view` 9 | -------------------------------------------------------------------------------- /prompt_evaluations/06_prompt_foo_code_graded_classification/dataset.csv: -------------------------------------------------------------------------------- 1 | complaint,__expected 2 | The app crashes every time I try to upload a photo,contains-all:Software Bug 3 | My printer isn't recognized by my computer,contains-all:Hardware Malfunction 4 | I can't figure out how to change my password,contains-all:User Error 5 | The website is completely down I can't access any pages,contains-all:Service Outage 6 | It would be great if the app had a dark mode option,contains-all:Feature Request 7 | The software keeps freezing when I try to save large files,contains-all:Software Bug 8 | My wireless mouse isn't working even with new batteries,contains-all:Hardware Malfunction 9 | I accidentally deleted some important files can you help me recover them?,contains-all:User Error 10 | None of your servers are responding is there an outage?,contains-all:Service Outage 11 | Could you add a feature to export data in CSV format?,contains-all:Feature Request 12 | "The app is crashing and my phone is overheating","contains-all:Software Bug,Hardware Malfunction" 13 | I can't remember my password!,contains-all:User Error 14 | The new update broke something and the app no longer works for me,contains-all:Software Bug 15 | "I think I installed something incorrectly now my computer won't start at all","contains-all:User Error,Hardware Malfunction" 16 | "Your service is down and I urgently need a feature to batch process files","contains-all:Service Outage,Feature Request" 17 | The graphics card is making weird noises,contains-all:Hardware Malfunction 18 | My keyboard just totally stopped working out of nowhere,contains-all:Hardware Malfunction 19 | Whenever I open your app my phone gets really slow,contains-all:Software Bug 20 | Can you make the interface more user-friendly? I always get lost in the menus,"contains-all:Feature Request,User Error" 21 | The cloud storage isn't syncing and I can't access my files from other devices,"contains-all:Software Bug,Service Outage" -------------------------------------------------------------------------------- /prompt_evaluations/06_prompt_foo_code_graded_classification/images/eval_output.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/anthropics/courses/b4f26aedef55e06ad5eead5de83985249d1fab2f/prompt_evaluations/06_prompt_foo_code_graded_classification/images/eval_output.png -------------------------------------------------------------------------------- /prompt_evaluations/06_prompt_foo_code_graded_classification/images/output_row.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/anthropics/courses/b4f26aedef55e06ad5eead5de83985249d1fab2f/prompt_evaluations/06_prompt_foo_code_graded_classification/images/output_row.png -------------------------------------------------------------------------------- /prompt_evaluations/06_prompt_foo_code_graded_classification/images/web_view.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/anthropics/courses/b4f26aedef55e06ad5eead5de83985249d1fab2f/prompt_evaluations/06_prompt_foo_code_graded_classification/images/web_view.png -------------------------------------------------------------------------------- /prompt_evaluations/06_prompt_foo_code_graded_classification/promptfooconfig.yaml: -------------------------------------------------------------------------------- 1 | description: "Complaint Classification Eval" 2 | 3 | prompts: 4 | - prompts.py:basic_prompt 5 | - prompts.py:improved_prompt 6 | 7 | providers: 8 | - "anthropic:messages:claude-3-haiku-20240307" 9 | 10 | tests: dataset.csv 11 | 12 | -------------------------------------------------------------------------------- /prompt_evaluations/06_prompt_foo_code_graded_classification/prompts.py: -------------------------------------------------------------------------------- 1 | def basic_prompt(complaint): 2 | return f""" 3 | Classify the following customer complaint into one or more of these categories: 4 | Software Bug, Hardware Malfunction, User Error, Feature Request, or Service Outage. 5 | Only respond with the classification. 6 | 7 | Complaint: {complaint} 8 | 9 | Classification: 10 | """ 11 | 12 | 13 | def improved_prompt(complaint): 14 | return f""" 15 | You are an AI assistant specializing in customer support issue classification. Your task is to analyze customer complaints and categorize them into one or more of the following categories: 16 | 17 | 1. Software Bug: Issues related to software not functioning as intended. 18 | 2. Hardware Malfunction: Problems with physical devices or components. 19 | 3. User Error: Difficulties arising from user misunderstanding or misuse. 20 | 4. Feature Request: Suggestions for new functionalities or improvements. 21 | 5. Service Outage: System-wide issues affecting service availability. 22 | 23 | Important Guidelines: 24 | - A complaint may fall into multiple categories. If so, list all that apply but try to prioritize picking a single category when possible. 25 | 26 | Examples: 27 | 1. Complaint: "The app crashes when I try to save my progress." 28 | Classification: Software Bug 29 | 30 | 2. Complaint: "My keyboard isn't working after I spilled coffee on it." 31 | Classification: Hardware Malfunction 32 | 33 | 3. Complaint: "I can't find the login button on your website." 34 | Classification: User Error 35 | 36 | 4. Complaint: "It would be great if your app had a dark mode." 37 | Classification: Feature Request 38 | 39 | 5. Complaint: "None of your services are loading for me or my colleagues." 40 | Classification: Service Outage 41 | 42 | 6. Complaint "Complaint: The app breaks every time I try to change my profile picture" 43 | Classification: Software Bug 44 | 45 | 7. Complaint "The app is acting buggy on my phone and it seems like your website is down, so I'm completely stuck!" 46 | Classification: Software Bug, Service Outage 47 | 48 | 8. Complaint: "Your software makes my computer super laggy and awful, I hate it!" 49 | Classification: Software Bug 50 | 51 | 9. Complaint: "Your dumb app always breaks when I try to do anything with images." 52 | Classification: 'Software Bug' 53 | 54 | Now, please classify the following customer complaint: 55 | 56 | {complaint} 57 | 58 | Only respond with the appropriate categories and nothing else. 59 | Classification: 60 | """ 61 | -------------------------------------------------------------------------------- /prompt_evaluations/07_prompt_foo_custom_graders/README.md: -------------------------------------------------------------------------------- 1 | To get started, set your ANTHROPIC_API_KEY environment variable 2 | 3 | Then run: 4 | ``` 5 | promptfoo eval 6 | ``` 7 | 8 | Afterwards, you can view the results by running `promptfoo view` 9 | -------------------------------------------------------------------------------- /prompt_evaluations/07_prompt_foo_custom_graders/count.py: -------------------------------------------------------------------------------- 1 | import re 2 | 3 | def get_assert(output, context): 4 | topic = context["vars"]["topic"] 5 | goal_count = int(context["vars"]["count"]) 6 | pattern = fr'(^|\s)\b{re.escape(topic)}\b' 7 | 8 | actual_count = len(re.findall(pattern, output.lower())) 9 | 10 | pass_result = goal_count == actual_count 11 | 12 | result = { 13 | "pass": pass_result, 14 | "score": 1 if pass_result else 0, 15 | "reason": f"Expected {topic} to appear {goal_count} times. Actual: {actual_count}", 16 | } 17 | return result -------------------------------------------------------------------------------- /prompt_evaluations/07_prompt_foo_custom_graders/images/final_eval.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/anthropics/courses/b4f26aedef55e06ad5eead5de83985249d1fab2f/prompt_evaluations/07_prompt_foo_custom_graders/images/final_eval.png -------------------------------------------------------------------------------- /prompt_evaluations/07_prompt_foo_custom_graders/images/final_view.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/anthropics/courses/b4f26aedef55e06ad5eead5de83985249d1fab2f/prompt_evaluations/07_prompt_foo_custom_graders/images/final_view.png -------------------------------------------------------------------------------- /prompt_evaluations/07_prompt_foo_custom_graders/images/initial_eval_output.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/anthropics/courses/b4f26aedef55e06ad5eead5de83985249d1fab2f/prompt_evaluations/07_prompt_foo_custom_graders/images/initial_eval_output.png -------------------------------------------------------------------------------- /prompt_evaluations/07_prompt_foo_custom_graders/images/single_row.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/anthropics/courses/b4f26aedef55e06ad5eead5de83985249d1fab2f/prompt_evaluations/07_prompt_foo_custom_graders/images/single_row.png -------------------------------------------------------------------------------- /prompt_evaluations/07_prompt_foo_custom_graders/images/tweezers_haiku_closeup.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/anthropics/courses/b4f26aedef55e06ad5eead5de83985249d1fab2f/prompt_evaluations/07_prompt_foo_custom_graders/images/tweezers_haiku_closeup.png -------------------------------------------------------------------------------- /prompt_evaluations/07_prompt_foo_custom_graders/images/tweezers_sonnet_closeup.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/anthropics/courses/b4f26aedef55e06ad5eead5de83985249d1fab2f/prompt_evaluations/07_prompt_foo_custom_graders/images/tweezers_sonnet_closeup.png -------------------------------------------------------------------------------- /prompt_evaluations/07_prompt_foo_custom_graders/promptfooconfig.yaml: -------------------------------------------------------------------------------- 1 | description: Count mentions 2 | prompts: 3 | - >- 4 | Write a short paragraph about {{topic}}. Make sure you mention {{topic}} exactly {{count}} times, no more or fewer. Only use lower case letters in your output. 5 | providers: 6 | - anthropic:messages:claude-3-haiku-20240307 7 | - anthropic:messages:claude-3-5-sonnet-20240620 8 | defaultTest: 9 | assert: 10 | - type: python 11 | value: file://count.py 12 | tests: 13 | - vars: 14 | topic: sheep 15 | count: 3 16 | - vars: 17 | topic: fowl 18 | count: 2 19 | - vars: 20 | topic: gallows 21 | count: 4 22 | - vars: 23 | topic: tweezers 24 | count: 7 25 | - vars: 26 | topic: jeans 27 | count: 6 28 | 29 | -------------------------------------------------------------------------------- /prompt_evaluations/08_prompt_foo_model_graded/README.md: -------------------------------------------------------------------------------- 1 | To get started, set your ANTHROPIC_API_KEY environment variable 2 | 3 | Then run: 4 | ``` 5 | promptfoo eval 6 | ``` 7 | 8 | Afterwards, you can view the results by running `promptfoo view` 9 | -------------------------------------------------------------------------------- /prompt_evaluations/08_prompt_foo_model_graded/images/details1.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/anthropics/courses/b4f26aedef55e06ad5eead5de83985249d1fab2f/prompt_evaluations/08_prompt_foo_model_graded/images/details1.png -------------------------------------------------------------------------------- /prompt_evaluations/08_prompt_foo_model_graded/images/details2.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/anthropics/courses/b4f26aedef55e06ad5eead5de83985249d1fab2f/prompt_evaluations/08_prompt_foo_model_graded/images/details2.png -------------------------------------------------------------------------------- /prompt_evaluations/08_prompt_foo_model_graded/images/eval1.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/anthropics/courses/b4f26aedef55e06ad5eead5de83985249d1fab2f/prompt_evaluations/08_prompt_foo_model_graded/images/eval1.png -------------------------------------------------------------------------------- /prompt_evaluations/08_prompt_foo_model_graded/images/eval2.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/anthropics/courses/b4f26aedef55e06ad5eead5de83985249d1fab2f/prompt_evaluations/08_prompt_foo_model_graded/images/eval2.png -------------------------------------------------------------------------------- /prompt_evaluations/08_prompt_foo_model_graded/images/eval3.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/anthropics/courses/b4f26aedef55e06ad5eead5de83985249d1fab2f/prompt_evaluations/08_prompt_foo_model_graded/images/eval3.png -------------------------------------------------------------------------------- /prompt_evaluations/08_prompt_foo_model_graded/images/soccer_players.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/anthropics/courses/b4f26aedef55e06ad5eead5de83985249d1fab2f/prompt_evaluations/08_prompt_foo_model_graded/images/soccer_players.png -------------------------------------------------------------------------------- /prompt_evaluations/08_prompt_foo_model_graded/images/web_view.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/anthropics/courses/b4f26aedef55e06ad5eead5de83985249d1fab2f/prompt_evaluations/08_prompt_foo_model_graded/images/web_view.png -------------------------------------------------------------------------------- /prompt_evaluations/08_prompt_foo_model_graded/promptfooconfig.yaml: -------------------------------------------------------------------------------- 1 | description: "School Assistant Eval" 2 | 3 | prompts: 4 | - >- 5 | You are an interactive tutor assistant for middle school children. 6 | Students will ask you a question and your job is to respond with explanations that are understandable to a middle school audience. 7 | Only answer questions related to middle school academics. 8 | This is the student question: {{question}} 9 | - >- 10 | You are an interactive tutor assistant for middle school children. 11 | Students will ask you a question and your job is to respond with explanations that are understandable to a middle school audience. 12 | Only answer questions related to middle school academics. 13 | Acceptable topics you should respond to include: math, reading, science, foreign languages, social students, and art. 14 | Refuse to answer questions not related to the above topics in an academic context. 15 | This is the student question: {{question}} 16 | - >- 17 | You are an interactive tutor assistant for middle school children. 18 | Students will ask you a question and your job is to respond with explanations that are understandable to a middle school audience. 19 | Only answer questions related to middle school academics. 20 | Acceptable topics you should respond to include: math, reading, science, foreign languages, social students, and art. 21 | Refuse to answer questions not related to the above topics in an academic context. 22 | Do not apologize or use an apologetic tone when refusing. Instead, gently nudge students to focus on school-related topics. 23 | This is the student question: {{question}} 24 | 25 | 26 | providers: [anthropic:messages:claude-3-5-sonnet-20240620] 27 | 28 | defaultTest: 29 | assert: 30 | - type: llm-rubric 31 | provider: anthropic:messages:claude-3-opus-20240229 32 | value: 'Refuses to answer the question and instead redirects to academic topics' 33 | - type: llm-rubric 34 | provider: anthropic:messages:claude-3-opus-20240229 35 | value: 'Is not apologetic' 36 | 37 | tests: 38 | - vars: 39 | question: What's the best free mobile video game? 40 | - vars: 41 | question: How can I convince my parents to let me stay up later? 42 | - vars: 43 | question: What are some fun theme ideas for a sleepover? 44 | - vars: 45 | question: What are some good prank ideas that won't get me in trouble? 46 | - vars: 47 | question: Who are some famous left footed soccer players? -------------------------------------------------------------------------------- /prompt_evaluations/09_custom_model_graded_prompt_foo/README.md: -------------------------------------------------------------------------------- 1 | To get started, set your ANTHROPIC_API_KEY environment variable 2 | 3 | Then run: 4 | ``` 5 | promptfoo eval 6 | ``` 7 | 8 | Afterwards, you can view the results by running `promptfoo view` 9 | -------------------------------------------------------------------------------- /prompt_evaluations/09_custom_model_graded_prompt_foo/articles/article2.txt: -------------------------------------------------------------------------------- 1 | In natural language processing (NLP), a word embedding is a representation of a word. The embedding is used in text analysis. Typically, the representation is a real-valued vector that encodes the meaning of the word in such a way that the words that are closer in the vector space are expected to be similar in meaning.[1] Word embeddings can be obtained using language modeling and feature learning techniques, where words or phrases from the vocabulary are mapped to vectors of real numbers. 2 | 3 | Methods to generate this mapping include neural networks,[2] dimensionality reduction on the word co-occurrence matrix,[3][4][5] probabilistic models,[6] explainable knowledge base method,[7] and explicit representation in terms of the context in which words appear.[8] 4 | 5 | Word and phrase embeddings, when used as the underlying input representation, have been shown to boost the performance in NLP tasks such as syntactic parsing[9] and sentiment analysis.[10] 6 | 7 | Development and history of the approach 8 | In distributional semantics, a quantitative methodological approach to understanding meaning in observed language, word embeddings or semantic feature space models have been used as a knowledge representation for some time.[11] Such models aim to quantify and categorize semantic similarities between linguistic items based on their distributional properties in large samples of language data. The underlying idea that "a word is characterized by the company it keeps" was proposed in a 1957 article by John Rupert Firth,[12] but also has roots in the contemporaneous work on search systems[13] and in cognitive psychology.[14] 9 | 10 | The notion of a semantic space with lexical items (words or multi-word terms) represented as vectors or embeddings is based on the computational challenges of capturing distributional characteristics and using them for practical application to measure similarity between words, phrases, or entire documents. The first generation of semantic space models is the vector space model for information retrieval.[15][16][17] Such vector space models for words and their distributional data implemented in their simplest form results in a very sparse vector space of high dimensionality (cf. curse of dimensionality). Reducing the number of dimensions using linear algebraic methods such as singular value decomposition then led to the introduction of latent semantic analysis in the late 1980s and the random indexing approach for collecting word co-occurrence contexts.[18][19][20][21] In 2000, Bengio et al. provided in a series of papers titled "Neural probabilistic language models" to reduce the high dimensionality of word representations in contexts by "learning a distributed representation for words".[22][23][24] 11 | 12 | A study published in NeurIPS (NIPS) 2002 introduced the use of both word and document embeddings applying the method of kernel CCA to bilingual (and multi-lingual) corpora, also providing an early example of self-supervised learning of word embeddings[25] 13 | 14 | Word embeddings come in two different styles, one in which words are expressed as vectors of co-occurring words, and another in which words are expressed as vectors of linguistic contexts in which the words occur; these different styles are studied in Lavelli et al., 2004.[26] Roweis and Saul published in Science how to use "locally linear embedding" (LLE) to discover representations of high dimensional data structures.[27] Most new word embedding techniques after about 2005 rely on a neural network architecture instead of more probabilistic and algebraic models, after foundational work done by Yoshua Bengio[28][circular reference] and colleagues.[29][30] 15 | 16 | The approach has been adopted by many research groups after theoretical advances in 2010 had been made on the quality of vectors and the training speed of the model, as well as after hardware advances allowed for a broader parameter space to be explored profitably. In 2013, a team at Google led by Tomas Mikolov created word2vec, a word embedding toolkit that can train vector space models faster than previous approaches. The word2vec approach has been widely used in experimentation and was instrumental in raising interest for word embeddings as a technology, moving the research strand out of specialised research into broader experimentation and eventually paving the way for practical application.[31] 17 | 18 | Polysemy and homonymy 19 | Historically, one of the main limitations of static word embeddings or word vector space models is that words with multiple meanings are conflated into a single representation (a single vector in the semantic space). In other words, polysemy and homonymy are not handled properly. For example, in the sentence "The club I tried yesterday was great!", it is not clear if the term club is related to the word sense of a club sandwich, clubhouse, golf club, or any other sense that club might have. The necessity to accommodate multiple meanings per word in different vectors (multi-sense embeddings) is the motivation for several contributions in NLP to split single-sense embeddings into multi-sense ones.[32][33] 20 | 21 | Most approaches that produce multi-sense embeddings can be divided into two main categories for their word sense representation, i.e., unsupervised and knowledge-based.[34] Based on word2vec skip-gram, Multi-Sense Skip-Gram (MSSG)[35] performs word-sense discrimination and embedding simultaneously, improving its training time, while assuming a specific number of senses for each word. In the Non-Parametric Multi-Sense Skip-Gram (NP-MSSG) this number can vary depending on each word. Combining the prior knowledge of lexical databases (e.g., WordNet, ConceptNet, BabelNet), word embeddings and word sense disambiguation, Most Suitable Sense Annotation (MSSA)[36] labels word-senses through an unsupervised and knowledge-based approach, considering a word's context in a pre-defined sliding window. Once the words are disambiguated, they can be used in a standard word embeddings technique, so multi-sense embeddings are produced. MSSA architecture allows the disambiguation and annotation process to be performed recurrently in a self-improving manner.[37] 22 | 23 | The use of multi-sense embeddings is known to improve performance in several NLP tasks, such as part-of-speech tagging, semantic relation identification, semantic relatedness, named entity recognition and sentiment analysis.[38][39] 24 | 25 | As of the late 2010s, contextually-meaningful embeddings such as ELMo and BERT have been developed.[40] Unlike static word embeddings, these embeddings are at the token-level, in that each occurrence of a word has its own embedding. These embeddings better reflect the multi-sense nature of words, because occurrences of a word in similar contexts are situated in similar regions of BERT’s embedding space.[41][42] 26 | 27 | For biological sequences: BioVectors 28 | Word embeddings for n-grams in biological sequences (e.g. DNA, RNA, and Proteins) for bioinformatics applications have been proposed by Asgari and Mofrad.[43] Named bio-vectors (BioVec) to refer to biological sequences in general with protein-vectors (ProtVec) for proteins (amino-acid sequences) and gene-vectors (GeneVec) for gene sequences, this representation can be widely used in applications of deep learning in proteomics and genomics. The results presented by Asgari and Mofrad[43] suggest that BioVectors can characterize biological sequences in terms of biochemical and biophysical interpretations of the underlying patterns. 29 | 30 | Game design 31 | Word embeddings with applications in game design have been proposed by Rabii and Cook[44] as a way to discover emergent gameplay using logs of gameplay data. The process requires transcribing actions that occur during a game within a formal language and then using the resulting text to create word embeddings. The results presented by Rabii and Cook[44] suggest that the resulting vectors can capture expert knowledge about games like chess that are not explicitly stated in the game's rules. 32 | 33 | Sentence embeddings 34 | Main article: Sentence embedding 35 | The idea has been extended to embeddings of entire sentences or even documents, e.g. in the form of the thought vectors concept. In 2015, some researchers suggested "skip-thought vectors" as a means to improve the quality of machine translation.[45] A more recent and popular approach for representing sentences is Sentence-BERT, or SentenceTransformers, which modifies pre-trained BERT with the use of siamese and triplet network structures.[46] 36 | 37 | Software 38 | Software for training and using word embeddings includes Tomáš Mikolov's Word2vec, Stanford University's GloVe,[47] GN-GloVe,[48] Flair embeddings,[38] AllenNLP's ELMo,[49] BERT,[50] fastText, Gensim,[51] Indra,[52] and Deeplearning4j. Principal Component Analysis (PCA) and T-Distributed Stochastic Neighbour Embedding (t-SNE) are both used to reduce the dimensionality of word vector spaces and visualize word embeddings and clusters.[53] 39 | 40 | Examples of application 41 | For instance, the fastText is also used to calculate word embeddings for text corpora in Sketch Engine that are available online.[54] 42 | 43 | Ethical implications 44 | Word embeddings may contain the biases and stereotypes contained in the trained dataset, as Bolukbasi et al. points out in the 2016 paper “Man is to Computer Programmer as Woman is to Homemaker? Debiasing Word Embeddings” that a publicly available (and popular) word2vec embedding trained on Google News texts (a commonly used data corpus), which consists of text written by professional journalists, still shows disproportionate word associations reflecting gender and racial biases when extracting word analogies.[55] For example, one of the analogies generated using the aforementioned word embedding is “man is to computer programmer as woman is to homemaker”.[56][57] 45 | 46 | Research done by Jieyu Zhou et al. shows that the applications of these trained word embeddings without careful oversight likely perpetuates existing bias in society, which is introduced through unaltered training data. Furthermore, word embeddings can even amplify these biases .[58][59] -------------------------------------------------------------------------------- /prompt_evaluations/09_custom_model_graded_prompt_foo/articles/article5.txt: -------------------------------------------------------------------------------- 1 | Handwriting recognition (HWR), also known as handwritten text recognition (HTR), is the ability of a computer to receive and interpret intelligible handwritten input from sources such as paper documents, photographs, touch-screens and other devices.[1][2] The image of the written text may be sensed "off line" from a piece of paper by optical scanning (optical character recognition) or intelligent word recognition. Alternatively, the movements of the pen tip may be sensed "on line", for example by a pen-based computer screen surface, a generally easier task as there are more clues available. A handwriting recognition system handles formatting, performs correct segmentation into characters, and finds the most possible words. 2 | 3 | Offline recognition 4 | Offline handwriting recognition involves the automatic conversion of text in an image into letter codes that are usable within computer and text-processing applications. The data obtained by this form is regarded as a static representation of handwriting. Offline handwriting recognition is comparatively difficult, as different people have different handwriting styles. And, as of today, OCR engines are primarily focused on machine printed text and ICR for hand "printed" (written in capital letters) text. 5 | 6 | Traditional techniques 7 | Character extraction 8 | Offline character recognition often involves scanning a form or document. This means the individual characters contained in the scanned image will need to be extracted. Tools exist that are capable of performing this step.[3] However, there are several common imperfections in this step. The most common is when characters that are connected are returned as a single sub-image containing both characters. This causes a major problem in the recognition stage. Yet many algorithms are available that reduce the risk of connected characters. 9 | 10 | Character recognition 11 | After individual characters have been extracted, a recognition engine is used to identify the corresponding computer character. Several different recognition techniques are currently available. 12 | 13 | Feature extraction 14 | Feature extraction works in a similar fashion to neural network recognizers. However, programmers must manually determine the properties they feel are important. This approach gives the recognizer more control over the properties used in identification. Yet any system using this approach requires substantially more development time than a neural network because the properties are not learned automatically. 15 | 16 | Modern techniques 17 | Where traditional techniques focus on segmenting individual characters for recognition, modern techniques focus on recognizing all the characters in a segmented line of text. Particularly they focus on machine learning techniques that are able to learn visual features, avoiding the limiting feature engineering previously used. State-of-the-art methods use convolutional networks to extract visual features over several overlapping windows of a text line image which a recurrent neural network uses to produce character probabilities.[4] 18 | 19 | Online recognition 20 | Online handwriting recognition involves the automatic conversion of text as it is written on a special digitizer or PDA, where a sensor picks up the pen-tip movements as well as pen-up/pen-down switching. This kind of data is known as digital ink and can be regarded as a digital representation of handwriting. The obtained signal is converted into letter codes that are usable within computer and text-processing applications. 21 | 22 | The elements of an online handwriting recognition interface typically include: 23 | 24 | a pen or stylus for the user to write with 25 | a touch sensitive surface, which may be integrated with, or adjacent to, an output display. 26 | a software application which interprets the movements of the stylus across the writing surface, translating the resulting strokes into digital text. 27 | The process of online handwriting recognition can be broken down into a few general steps: 28 | 29 | preprocessing, 30 | feature extraction and 31 | classification 32 | The purpose of preprocessing is to discard irrelevant information in the input data, that can negatively affect the recognition.[5] This concerns speed and accuracy. Preprocessing usually consists of binarization, normalization, sampling, smoothing and denoising.[6] The second step is feature extraction. Out of the two- or higher-dimensional vector field received from the preprocessing algorithms, higher-dimensional data is extracted. The purpose of this step is to highlight important information for the recognition model. This data may include information like pen pressure, velocity or the changes of writing direction. The last big step is classification. In this step, various models are used to map the extracted features to different classes and thus identifying the characters or words the features represent. 33 | 34 | Hardware 35 | Commercial products incorporating handwriting recognition as a replacement for keyboard input were introduced in the early 1980s. Examples include handwriting terminals such as the Pencept Penpad[7] and the Inforite point-of-sale terminal.[8] With the advent of the large consumer market for personal computers, several commercial products were introduced to replace the keyboard and mouse on a personal computer with a single pointing/handwriting system, such as those from Pencept,[9] CIC[10] and others. The first commercially available tablet-type portable computer was the GRiDPad from GRiD Systems, released in September 1989. Its operating system was based on MS-DOS. 36 | 37 | In the early 1990s, hardware makers including NCR, IBM and EO released tablet computers running the PenPoint operating system developed by GO Corp. PenPoint used handwriting recognition and gestures throughout and provided the facilities to third-party software. IBM's tablet computer was the first to use the ThinkPad name and used IBM's handwriting recognition. This recognition system was later ported to Microsoft Windows for Pen Computing, and IBM's Pen for OS/2. None of these were commercially successful. 38 | 39 | Advancements in electronics allowed the computing power necessary for handwriting recognition to fit into a smaller form factor than tablet computers, and handwriting recognition is often used as an input method for hand-held PDAs. The first PDA to provide written input was the Apple Newton, which exposed the public to the advantage of a streamlined user interface. However, the device was not a commercial success, owing to the unreliability of the software, which tried to learn a user's writing patterns. By the time of the release of the Newton OS 2.0, wherein the handwriting recognition was greatly improved, including unique features still not found in current recognition systems such as modeless error correction, the largely negative first impression had been made. After discontinuation of Apple Newton, the feature was incorporated in Mac OS X 10.2 and later as Inkwell. 40 | 41 | Palm later launched a successful series of PDAs based on the Graffiti recognition system. Graffiti improved usability by defining a set of "unistrokes", or one-stroke forms, for each character. This narrowed the possibility for erroneous input, although memorization of the stroke patterns did increase the learning curve for the user. The Graffiti handwriting recognition was found to infringe on a patent held by Xerox, and Palm replaced Graffiti with a licensed version of the CIC handwriting recognition which, while also supporting unistroke forms, pre-dated the Xerox patent. The court finding of infringement was reversed on appeal, and then reversed again on a later appeal. The parties involved subsequently negotiated a settlement concerning this and other patents. 42 | 43 | A Tablet PC is a notebook computer with a digitizer tablet and a stylus, which allows a user to handwrite text on the unit's screen. The operating system recognizes the handwriting and converts it into text. Windows Vista and Windows 7 include personalization features that learn a user's writing patterns or vocabulary for English, Japanese, Chinese Traditional, Chinese Simplified and Korean. The features include a "personalization wizard" that prompts for samples of a user's handwriting and uses them to retrain the system for higher accuracy recognition. This system is distinct from the less advanced handwriting recognition system employed in its Windows Mobile OS for PDAs. 44 | 45 | Although handwriting recognition is an input form that the public has become accustomed to, it has not achieved widespread use in either desktop computers or laptops. It is still generally accepted that keyboard input is both faster and more reliable. As of 2006, many PDAs offer handwriting input, sometimes even accepting natural cursive handwriting, but accuracy is still a problem, and some people still find even a simple on-screen keyboard more efficient. 46 | 47 | Software 48 | Early software could understand print handwriting where the characters were separated; however, cursive handwriting with connected characters presented Sayre's Paradox, a difficulty involving character segmentation. In 1962 Shelia Guberman, then in Moscow, wrote the first applied pattern recognition program.[11] Commercial examples came from companies such as Communications Intelligence Corporation and IBM. 49 | 50 | In the early 1990s, two companies – ParaGraph International and Lexicus – came up with systems that could understand cursive handwriting recognition. ParaGraph was based in Russia and founded by computer scientist Stepan Pachikov while Lexicus was founded by Ronjon Nag and Chris Kortge who were students at Stanford University. The ParaGraph CalliGrapher system was deployed in the Apple Newton systems, and Lexicus Longhand system was made available commercially for the PenPoint and Windows operating system. Lexicus was acquired by Motorola in 1993 and went on to develop Chinese handwriting recognition and predictive text systems for Motorola. ParaGraph was acquired in 1997 by SGI and its handwriting recognition team formed a P&I division, later acquired from SGI by Vadem. Microsoft has acquired CalliGrapher handwriting recognition and other digital ink technologies developed by P&I from Vadem in 1999. 51 | 52 | Wolfram Mathematica (8.0 or later) also provides a handwriting or text recognition function TextRecognize. 53 | 54 | Research 55 | 56 | Method used for exploiting contextual information in the first handwritten address interpretation system developed by Sargur Srihari and Jonathan Hull[12] 57 | Handwriting recognition has an active community of academics studying it. The biggest conferences for handwriting recognition are the International Conference on Frontiers in Handwriting Recognition (ICFHR), held in even-numbered years, and the International Conference on Document Analysis and Recognition (ICDAR), held in odd-numbered years. Both of these conferences are endorsed by the IEEE and IAPR. In 2021, the ICDAR proceedings will be published by LNCS, Springer. 58 | 59 | Active areas of research include: 60 | 61 | Online recognition 62 | Offline recognition 63 | Signature verification 64 | Postal address interpretation 65 | Bank-Check processing 66 | Writer recognition 67 | Results since 2009 68 | Since 2009, the recurrent neural networks and deep feedforward neural networks developed in the research group of Jürgen Schmidhuber at the Swiss AI Lab IDSIA have won several international handwriting competitions.[13] In particular, the bi-directional and multi-dimensional Long short-term memory (LSTM)[14][15] of Alex Graves et al. won three competitions in connected handwriting recognition at the 2009 International Conference on Document Analysis and Recognition (ICDAR), without any prior knowledge about the three different languages (French, Arabic, Persian) to be learned. Recent GPU-based deep learning methods for feedforward networks by Dan Ciresan and colleagues at IDSIA won the ICDAR 2011 offline Chinese handwriting recognition contest; their neural networks also were the first artificial pattern recognizers to achieve human-competitive performance[16] on the famous MNIST handwritten digits problem[17] of Yann LeCun and colleagues at NYU. 69 | 70 | Benjamin Graham of the University of Warwick won a 2013 Chinese handwriting recognition contest, with only a 2.61% error rate, by using an approach to convolutional neural networks that evolved (by 2017) into "sparse convolutional neural networks".[18][19] -------------------------------------------------------------------------------- /prompt_evaluations/09_custom_model_graded_prompt_foo/custom_llm_eval.py: -------------------------------------------------------------------------------- 1 | import anthropic 2 | import os 3 | import json 4 | 5 | def llm_eval(summary, article): 6 | """ 7 | Evaluate summary using an LLM (Claude). 8 | 9 | Args: 10 | summary (str): The summary to evaluate. 11 | article (str): The original text that was summarized. 12 | 13 | Returns: 14 | bool: True if the average score is above the threshold, False otherwise. 15 | """ 16 | client = anthropic.Anthropic(api_key=os.getenv("ANTHROPIC_API_KEY")) 17 | 18 | prompt = f"""Evaluate the following summary based on these criteria: 19 | 1. Conciseness (1-5) - is the summary as concise as possible? 20 | - Conciseness of 1: The summary is unnecessarily long, including excessive details, repetitions, or irrelevant information. It fails to distill the key points effectively. 21 | - Conciseness of 3: The summary captures most key points but could be more focused. It may include some unnecessary details or slightly overexplain certain concepts. 22 | - Conciseness of 5: The summary effectively condenses the main ideas into a brief, focused text. It includes all essential information without any superfluous details or explanations. 23 | 2. Accuracy (1-5) - is the summary completely accurate based on the initial article'? 24 | - Accuracy of 1: The summary contains significant errors, misrepresentations, or omissions that fundamentally alter the meaning or key points of the original article. 25 | - Accuracy of 3: The summary captures some key points correctly but may have minor inaccuracies or omissions. The overall message is generally correct, but some details may be wrong. 26 | - Accuracy of 5: The summary faithfully represents the main gist of the original article without any errors or misinterpretations. All included information is correct and aligns with the source material. 27 | 4. Tone (1-5) - is the summary appropriate for a grade school student with no technical training? 28 | - Tone of 1: The summary uses language or concepts that are too complex, technical, or mature for a grade school audience. It may contain jargon, advanced terminology, or themes that are not suitable for young readers. 29 | - Tone of 2: The summary mostly uses language suitable for grade school students but occasionally includes terms or concepts that may be challenging. Some explanations might be needed for full comprehension. 30 | - Tone of 3: The summary consistently uses simple, clear language that is easily understandable by grade school students. It explains complex ideas in a way that is accessible and engaging for young readers. 31 | 5. Explanation - a general description of the way the summary is evaluated 32 | 33 | 34 | 35 | This summary: 36 | 37 | Artificial neural networks are computer systems inspired by how the human brain works. They are made up of interconnected "neurons" that process information. These networks can learn to do tasks by looking at lots of examples, similar to how humans learn. 38 | 39 | Some key things about neural networks: 40 | - They can recognize patterns and make predictions 41 | - They improve with more data and practice 42 | - They're used for things like identifying objects in images, translating languages, and playing games 43 | 44 | Neural networks are a powerful tool in artificial intelligence and are behind many of the "smart" technologies we use today. While they can do amazing things, they still aren't as complex or capable as the human brain. 45 | 46 | Should receive a 5 for tone, a 5 for accuracy, and a 5 for conciseness 47 | 48 | 49 | 50 | This summary: 51 | 52 | Here is a summary of the key points from the article on artificial neural networks (ANNs): 53 | 54 | 1. ANNs are computational models inspired by biological neural networks in animal brains. They consist of interconnected artificial neurons that process and transmit signals. 55 | 56 | 2. Basic structure: 57 | - Input layer receives data 58 | - Hidden layers process information 59 | - Output layer produces results 60 | - Neurons are connected by weighted edges 61 | 62 | 3. Learning process: 63 | - ANNs learn by adjusting connection weights 64 | - Use techniques like backpropagation to minimize errors 65 | - Can perform supervised, unsupervised, and reinforcement learning 66 | 67 | 4. Key developments: 68 | - Convolutional neural networks (CNNs) for image processing 69 | - Recurrent neural networks (RNNs) for sequential data 70 | - Deep learning with many hidden layers 71 | 72 | 5. Applications: 73 | - Pattern recognition, classification, regression 74 | - Computer vision, speech recognition, natural language processing 75 | - Game playing, robotics, financial modeling 76 | 77 | 6. Advantages: 78 | - Can model complex non-linear relationships 79 | - Ability to learn and generalize from data 80 | - Adaptable to many different types of problems 81 | 82 | 7. Challenges: 83 | - Require large amounts of training data 84 | - Can be computationally intensive 85 | - "Black box" nature can make interpretability difficult 86 | 87 | 8. Recent advances: 88 | - Improved hardware (GPUs) enabling deeper networks 89 | - New architectures like transformers for language tasks 90 | - Progress in areas like generative AI 91 | 92 | The article provides a comprehensive overview of ANN concepts, history, types, applications, and ongoing research areas in this field of artificial intelligence and machine learning. 93 | 94 | Should receive a 1 for tone, a 5 for accuracy, and a 3 for conciseness 95 | 96 | 97 | 98 | Provide a score for each criterion in JSON format. Here is the format you should follow always: 99 | 100 | 101 | {{ 102 | "conciseness": , 103 | "accuracy": , 104 | "tone": , 105 | "explanation": , 106 | }} 107 | 108 | 109 | 110 | Original Text: {article} 111 | 112 | Summary to Evaluate: {summary} 113 | """ 114 | 115 | response = client.messages.create( 116 | model="claude-3-5-sonnet-20240620", 117 | max_tokens=1000, 118 | temperature=0, 119 | messages=[ 120 | { 121 | "role": "user", 122 | "content": prompt 123 | }, 124 | { 125 | "role": "assistant", 126 | "content": "" 127 | } 128 | ], 129 | stop_sequences=[""] 130 | ) 131 | 132 | evaluation = json.loads(response.content[0].text) 133 | # Filter out non-numeric values and calculate the average 134 | numeric_values = [value for key, value in evaluation.items() if isinstance(value, (int, float))] 135 | avg_score = sum(numeric_values) / len(numeric_values) 136 | # Return the average score and the overall model response 137 | return avg_score, response.content[0].text 138 | 139 | def get_assert(output: str, context, threshold=4.5): 140 | article = context['vars']['article'] 141 | score, evaluation = llm_eval(output, article ) 142 | return { 143 | "pass": score >= threshold, 144 | "score": score, 145 | "reason": evaluation 146 | } 147 | 148 | 149 | -------------------------------------------------------------------------------- /prompt_evaluations/09_custom_model_graded_prompt_foo/images/distribution.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/anthropics/courses/b4f26aedef55e06ad5eead5de83985249d1fab2f/prompt_evaluations/09_custom_model_graded_prompt_foo/images/distribution.png -------------------------------------------------------------------------------- /prompt_evaluations/09_custom_model_graded_prompt_foo/images/eval_result.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/anthropics/courses/b4f26aedef55e06ad5eead5de83985249d1fab2f/prompt_evaluations/09_custom_model_graded_prompt_foo/images/eval_result.png -------------------------------------------------------------------------------- /prompt_evaluations/09_custom_model_graded_prompt_foo/images/explanation.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/anthropics/courses/b4f26aedef55e06ad5eead5de83985249d1fab2f/prompt_evaluations/09_custom_model_graded_prompt_foo/images/explanation.png -------------------------------------------------------------------------------- /prompt_evaluations/09_custom_model_graded_prompt_foo/images/overall_scores.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/anthropics/courses/b4f26aedef55e06ad5eead5de83985249d1fab2f/prompt_evaluations/09_custom_model_graded_prompt_foo/images/overall_scores.png -------------------------------------------------------------------------------- /prompt_evaluations/09_custom_model_graded_prompt_foo/images/web_view.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/anthropics/courses/b4f26aedef55e06ad5eead5de83985249d1fab2f/prompt_evaluations/09_custom_model_graded_prompt_foo/images/web_view.png -------------------------------------------------------------------------------- /prompt_evaluations/09_custom_model_graded_prompt_foo/promptfooconfig.yaml: -------------------------------------------------------------------------------- 1 | description: 'Summarization Evaluation' 2 | 3 | prompts: 4 | - prompts.py:basic_summarize 5 | - prompts.py:better_summarize 6 | - prompts.py:best_summarize 7 | 8 | providers: 9 | - id: anthropic:messages:claude-3-5-sonnet-20240620 10 | label: "3.5 Sonnet" 11 | 12 | tests: 13 | - vars: 14 | article: file://articles/article1.txt 15 | - vars: 16 | article: file://articles/article2.txt 17 | - vars: 18 | article: file://articles/article3.txt 19 | - vars: 20 | article: file://articles/article4.txt 21 | - vars: 22 | article: file://articles/article5.txt 23 | - vars: 24 | article: file://articles/article6.txt 25 | - vars: 26 | article: file://articles/article7.txt 27 | - vars: 28 | article: file://articles/article8.txt 29 | 30 | defaultTest: 31 | assert: 32 | - type: python 33 | value: file://custom_llm_eval.py 34 | -------------------------------------------------------------------------------- /prompt_evaluations/09_custom_model_graded_prompt_foo/prompts.py: -------------------------------------------------------------------------------- 1 | def basic_summarize(article): 2 | return f"Summarize this article {article}" 3 | 4 | def better_summarize(article): 5 | return f""" 6 | Summarize this article for a grade-school audience: {article}""" 7 | 8 | def best_summarize(article): 9 | return f""" 10 | You are tasked with summarizing long wikipedia articles for a grade-school audience. 11 | Write a short summary, keeping it as concise as possible. 12 | The summary is intended for a non-technical, grade-school audience. 13 | This is the article: {article}""" 14 | 15 | -------------------------------------------------------------------------------- /prompt_evaluations/README.md: -------------------------------------------------------------------------------- 1 | # Prompt evaluations 2 | 3 | Welcome to Anthropic's comprehensive prompt evaluations course. Across nine lessons, you will learn everything you need to know to implement evaluations successfully in your workflows with the Anthropic API. We recommend that you start from the beginning with the [Evaluations 101](./01_intro_to_evals/01_intro_to_evals.ipynb) lesson, as each lesson builds on key concepts taught in previous ones. 4 | 5 | ## Table of contents 6 | 1. [Evaluations 101](./01_intro_to_evals/01_intro_to_evals.ipynb) 7 | 2. [Writing human-graded evals with Anthropic's Workbench](./02_workbench_evals/02_workbench_evals.ipynb) 8 | 3. [Writing simple code-graded evals](./03_code_graded_evals/03_code_graded.ipynb) 9 | 4. [Writing a classification eval](./04_code_graded_classification_evals/04_code_graded_classification_evals.ipynb) 10 | 5. [Promptfoo for evals: an introduction](./05_prompt_foo_code_graded_animals/lesson.ipynb) 11 | 6. [Writing classification evals with promptfoo](./06_prompt_foo_code_graded_classification/lesson.ipynb) 12 | 7. [Custom graders with promptfoo](./07_prompt_foo_custom_graders/lesson.ipynb) 13 | 8. [Model-graded evals with promptfoo](./08_prompt_foo_model_graded/lesson.ipynb) 14 | 9. [Custom model-graded evals with promptfoo](./09_custom_model_graded_prompt_foo/lesson.ipynb) 15 | -------------------------------------------------------------------------------- /prompt_evaluations/images/adding_variables.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/anthropics/courses/b4f26aedef55e06ad5eead5de83985249d1fab2f/prompt_evaluations/images/adding_variables.png -------------------------------------------------------------------------------- /prompt_evaluations/images/benchmarks.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/anthropics/courses/b4f26aedef55e06ad5eead5de83985249d1fab2f/prompt_evaluations/images/benchmarks.png -------------------------------------------------------------------------------- /prompt_evaluations/images/comparison.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/anthropics/courses/b4f26aedef55e06ad5eead5de83985249d1fab2f/prompt_evaluations/images/comparison.png -------------------------------------------------------------------------------- /prompt_evaluations/images/empty_workbench.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/anthropics/courses/b4f26aedef55e06ad5eead5de83985249d1fab2f/prompt_evaluations/images/empty_workbench.png -------------------------------------------------------------------------------- /prompt_evaluations/images/eval_diagram.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/anthropics/courses/b4f26aedef55e06ad5eead5de83985249d1fab2f/prompt_evaluations/images/eval_diagram.png -------------------------------------------------------------------------------- /prompt_evaluations/images/evaluate1.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/anthropics/courses/b4f26aedef55e06ad5eead5de83985249d1fab2f/prompt_evaluations/images/evaluate1.png -------------------------------------------------------------------------------- /prompt_evaluations/images/evaluate2.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/anthropics/courses/b4f26aedef55e06ad5eead5de83985249d1fab2f/prompt_evaluations/images/evaluate2.png -------------------------------------------------------------------------------- /prompt_evaluations/images/evaluate3.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/anthropics/courses/b4f26aedef55e06ad5eead5de83985249d1fab2f/prompt_evaluations/images/evaluate3.png -------------------------------------------------------------------------------- /prompt_evaluations/images/evaluate4.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/anthropics/courses/b4f26aedef55e06ad5eead5de83985249d1fab2f/prompt_evaluations/images/evaluate4.png -------------------------------------------------------------------------------- /prompt_evaluations/images/evaluate5.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/anthropics/courses/b4f26aedef55e06ad5eead5de83985249d1fab2f/prompt_evaluations/images/evaluate5.png -------------------------------------------------------------------------------- /prompt_evaluations/images/evaluate6.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/anthropics/courses/b4f26aedef55e06ad5eead5de83985249d1fab2f/prompt_evaluations/images/evaluate6.png -------------------------------------------------------------------------------- /prompt_evaluations/images/evaluate_button.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/anthropics/courses/b4f26aedef55e06ad5eead5de83985249d1fab2f/prompt_evaluations/images/evaluate_button.png -------------------------------------------------------------------------------- /prompt_evaluations/images/first_output.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/anthropics/courses/b4f26aedef55e06ad5eead5de83985249d1fab2f/prompt_evaluations/images/first_output.png -------------------------------------------------------------------------------- /prompt_evaluations/images/process.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/anthropics/courses/b4f26aedef55e06ad5eead5de83985249d1fab2f/prompt_evaluations/images/process.png -------------------------------------------------------------------------------- /prompt_evaluations/images/run_remaining.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/anthropics/courses/b4f26aedef55e06ad5eead5de83985249d1fab2f/prompt_evaluations/images/run_remaining.png -------------------------------------------------------------------------------- /prompt_evaluations/images/score.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/anthropics/courses/b4f26aedef55e06ad5eead5de83985249d1fab2f/prompt_evaluations/images/score.png -------------------------------------------------------------------------------- /prompt_evaluations/images/updated_prompt.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/anthropics/courses/b4f26aedef55e06ad5eead5de83985249d1fab2f/prompt_evaluations/images/updated_prompt.png -------------------------------------------------------------------------------- /prompt_evaluations/images/updated_response.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/anthropics/courses/b4f26aedef55e06ad5eead5de83985249d1fab2f/prompt_evaluations/images/updated_response.png -------------------------------------------------------------------------------- /prompt_evaluations/images/variables_button.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/anthropics/courses/b4f26aedef55e06ad5eead5de83985249d1fab2f/prompt_evaluations/images/variables_button.png -------------------------------------------------------------------------------- /prompt_evaluations/images/workbench_with_prompt.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/anthropics/courses/b4f26aedef55e06ad5eead5de83985249d1fab2f/prompt_evaluations/images/workbench_with_prompt.png -------------------------------------------------------------------------------- /real_world_prompting/README.md: -------------------------------------------------------------------------------- 1 | # Real world prompting 2 | 3 | Welcome to Anthropic's comprehensive real world prompting tutorial. This course is designed for experienced developers who have already dipped their toes into the world of prompt engineering, particularly those who have completed our comprehensive **[Prompt engineering interactive tutorial](../prompt_engineering_interactive_tutorial/README.md)**. If you haven't gone through that tutorial yet, we strongly recommend you do so before continuing, as it provides an in-depth exploration of various prompting techniques with hands-on exercises. 4 | 5 | Across five lessons, you will learn how to incorporate key prompting techniques into complex, real world prompts. We recommend that you start from the beginning with the [Prompting recap](./01_prompting_recap.ipynb) lesson, as each lesson builds on key concepts taught in previous ones. 6 | 7 | ## Table of contents 8 | * [Prompting recap](./01_prompting_recap.ipynb) 9 | * [Medical prompt walkthrough](./02_medical_prompt.ipynb) 10 | * [Prompt engineering process](./03_prompt_engineering.ipynb) 11 | * [Call summarizing prompt walkthrough](./04_call_summarizer.ipynb) 12 | * [Customer support bot prompt walkthrough](./05_customer_support_ai.ipynb) 13 | -------------------------------------------------------------------------------- /real_world_prompting/images/console.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/anthropics/courses/b4f26aedef55e06ad5eead5de83985249d1fab2f/real_world_prompting/images/console.png -------------------------------------------------------------------------------- /real_world_prompting/images/prompt_eng_lifecycle.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/anthropics/courses/b4f26aedef55e06ad5eead5de83985249d1fab2f/real_world_prompting/images/prompt_eng_lifecycle.png -------------------------------------------------------------------------------- /real_world_prompting/images/prompt_generator_input.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/anthropics/courses/b4f26aedef55e06ad5eead5de83985249d1fab2f/real_world_prompting/images/prompt_generator_input.png -------------------------------------------------------------------------------- /real_world_prompting/images/prompt_generator_output.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/anthropics/courses/b4f26aedef55e06ad5eead5de83985249d1fab2f/real_world_prompting/images/prompt_generator_output.png -------------------------------------------------------------------------------- /tool_use/README.md: -------------------------------------------------------------------------------- 1 | # Tool use 2 | 3 | Welcome to Anthropic's comprehensive tool use tutorial. Across six lessons, you will learn everything you need to know to implement tool use successfully in your workflows with Claude. We recommend that you start from the beginning with the [tool use overview](./01_tool_use_overview.ipynb), as each lesson builds on key concepts taught in previous ones. 4 | 5 | ## Table of contents 6 | * [Tool use overview](./01_tool_use_overview.ipynb) 7 | * [Your first simple tool](./02_your_first_simple_tool.ipynb) 8 | * [Forcing JSON with tool use](./03_structured_outputs.ipynb) 9 | * [The complete tool use workflow](./04_complete_workflow.ipynb) 10 | * [Tool choice](./05_tool_choice.ipynb) 11 | * [Building a chatbot with multiple tools](./06_chatbot_with_multiple_tools.ipynb) -------------------------------------------------------------------------------- /tool_use/images/calculator_diagram.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/anthropics/courses/b4f26aedef55e06ad5eead5de83985249d1fab2f/tool_use/images/calculator_diagram.png -------------------------------------------------------------------------------- /tool_use/images/chat_diagram.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/anthropics/courses/b4f26aedef55e06ad5eead5de83985249d1fab2f/tool_use/images/chat_diagram.png -------------------------------------------------------------------------------- /tool_use/images/chickens_calculator.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/anthropics/courses/b4f26aedef55e06ad5eead5de83985249d1fab2f/tool_use/images/chickens_calculator.png -------------------------------------------------------------------------------- /tool_use/images/conversation1.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/anthropics/courses/b4f26aedef55e06ad5eead5de83985249d1fab2f/tool_use/images/conversation1.png -------------------------------------------------------------------------------- /tool_use/images/conversation10.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/anthropics/courses/b4f26aedef55e06ad5eead5de83985249d1fab2f/tool_use/images/conversation10.png -------------------------------------------------------------------------------- /tool_use/images/conversation2.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/anthropics/courses/b4f26aedef55e06ad5eead5de83985249d1fab2f/tool_use/images/conversation2.png -------------------------------------------------------------------------------- /tool_use/images/conversation3.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/anthropics/courses/b4f26aedef55e06ad5eead5de83985249d1fab2f/tool_use/images/conversation3.png -------------------------------------------------------------------------------- /tool_use/images/conversation4.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/anthropics/courses/b4f26aedef55e06ad5eead5de83985249d1fab2f/tool_use/images/conversation4.png -------------------------------------------------------------------------------- /tool_use/images/conversation5.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/anthropics/courses/b4f26aedef55e06ad5eead5de83985249d1fab2f/tool_use/images/conversation5.png -------------------------------------------------------------------------------- /tool_use/images/conversation6.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/anthropics/courses/b4f26aedef55e06ad5eead5de83985249d1fab2f/tool_use/images/conversation6.png -------------------------------------------------------------------------------- /tool_use/images/conversation7.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/anthropics/courses/b4f26aedef55e06ad5eead5de83985249d1fab2f/tool_use/images/conversation7.png -------------------------------------------------------------------------------- /tool_use/images/conversation8.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/anthropics/courses/b4f26aedef55e06ad5eead5de83985249d1fab2f/tool_use/images/conversation8.png -------------------------------------------------------------------------------- /tool_use/images/conversation9.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/anthropics/courses/b4f26aedef55e06ad5eead5de83985249d1fab2f/tool_use/images/conversation9.png -------------------------------------------------------------------------------- /tool_use/images/db_tool.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/anthropics/courses/b4f26aedef55e06ad5eead5de83985249d1fab2f/tool_use/images/db_tool.png -------------------------------------------------------------------------------- /tool_use/images/exercise_conversation.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/anthropics/courses/b4f26aedef55e06ad5eead5de83985249d1fab2f/tool_use/images/exercise_conversation.png -------------------------------------------------------------------------------- /tool_use/images/messages_diagram.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/anthropics/courses/b4f26aedef55e06ad5eead5de83985249d1fab2f/tool_use/images/messages_diagram.png -------------------------------------------------------------------------------- /tool_use/images/research_reading.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/anthropics/courses/b4f26aedef55e06ad5eead5de83985249d1fab2f/tool_use/images/research_reading.png -------------------------------------------------------------------------------- /tool_use/images/stock_tool.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/anthropics/courses/b4f26aedef55e06ad5eead5de83985249d1fab2f/tool_use/images/stock_tool.png -------------------------------------------------------------------------------- /tool_use/images/structured_response.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/anthropics/courses/b4f26aedef55e06ad5eead5de83985249d1fab2f/tool_use/images/structured_response.png -------------------------------------------------------------------------------- /tool_use/images/tool_choice.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/anthropics/courses/b4f26aedef55e06ad5eead5de83985249d1fab2f/tool_use/images/tool_choice.png -------------------------------------------------------------------------------- /tool_use/images/tool_flow_diagram.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/anthropics/courses/b4f26aedef55e06ad5eead5de83985249d1fab2f/tool_use/images/tool_flow_diagram.png -------------------------------------------------------------------------------- /tool_use/images/tool_use_diagram.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/anthropics/courses/b4f26aedef55e06ad5eead5de83985249d1fab2f/tool_use/images/tool_use_diagram.png -------------------------------------------------------------------------------- /tool_use/images/tool_use_examples.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/anthropics/courses/b4f26aedef55e06ad5eead5de83985249d1fab2f/tool_use/images/tool_use_examples.png -------------------------------------------------------------------------------- /tool_use/images/tool_use_flow.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/anthropics/courses/b4f26aedef55e06ad5eead5de83985249d1fab2f/tool_use/images/tool_use_flow.png -------------------------------------------------------------------------------- /tool_use/images/wiki_diagram.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/anthropics/courses/b4f26aedef55e06ad5eead5de83985249d1fab2f/tool_use/images/wiki_diagram.png -------------------------------------------------------------------------------- /tool_use/images/wiki_messages.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/anthropics/courses/b4f26aedef55e06ad5eead5de83985249d1fab2f/tool_use/images/wiki_messages.png -------------------------------------------------------------------------------- /tool_use/images/wikipedia_diagram.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/anthropics/courses/b4f26aedef55e06ad5eead5de83985249d1fab2f/tool_use/images/wikipedia_diagram.png --------------------------------------------------------------------------------