diff --git a/tutorials/2.tips.ipynb b/tutorials/2.tips.ipynb index e67c44e..7abb79e 100644 --- a/tutorials/2.tips.ipynb +++ b/tutorials/2.tips.ipynb @@ -32,7 +32,7 @@ }, { "cell_type": "code", - "execution_count": 106, + "execution_count": 9, "id": "fdf5e1d9-31ad-4e8a-a55e-e2e70fff598d", "metadata": {}, "outputs": [ @@ -42,14 +42,15 @@ "{'age': 17, 'name': 'Harry Potter', 'house': }" ] }, - "execution_count": 106, + "execution_count": 9, "metadata": {}, "output_type": "execute_result" } ], "source": [ "from enum import Enum\n", - "\n", + "from pydantic import BaseModel,Field\n", + "from typing import List,Literal\n", "import instructor\n", "from openai import OpenAI\n", "\n", @@ -82,7 +83,7 @@ }, { "cell_type": "code", - "execution_count": 107, + "execution_count": 10, "id": "03db160c-81e9-4373-bfec-7a107224b6dd", "metadata": {}, "outputs": [ @@ -92,7 +93,7 @@ "{'age': 17, 'name': 'Harry Potter', 'house': 'Gryffindor'}" ] }, - "execution_count": 107, + "execution_count": 10, "metadata": {}, "output_type": "execute_result" } @@ -128,7 +129,7 @@ }, { "cell_type": "code", - "execution_count": 108, + "execution_count": 4, "id": "0e7938b8-4666-4df4-bd80-f53e8baf7550", "metadata": {}, "outputs": [ @@ -138,13 +139,13 @@ "{'age': 38,\n", " 'name': 'Severus Snape',\n", " 'house': 'Slytherin',\n", - " 'properties': [{'key': 'occupation', 'value': 'Potions Master'},\n", + " 'properties': [{'key': 'role', 'value': 'Professor of Potions'},\n", " {'key': 'loyalty', 'value': 'Dumbledore'},\n", " {'key': 'patronus', 'value': 'Doe'},\n", " {'key': 'played_by', 'value': 'Alan Rickman'}]}" ] }, - "execution_count": 108, + "execution_count": 4, "metadata": {}, "output_type": "execute_result" } @@ -189,7 +190,7 @@ }, { "cell_type": "code", - "execution_count": 109, + "execution_count": 5, "id": "69a58d01-ab6f-41b6-bc0c-b0e55fdb6fe4", "metadata": {}, "outputs": [ @@ -199,14 +200,20 @@ "{'age': 38,\n", " 'name': 'Severus Snape',\n", " 'house': 'Slytherin',\n", - " 'properties': [{'index': '1', 'key': 'Role', 'value': 'Potions Master'},\n", - " {'index': '2', 'key': 'Loyalty', 'value': 'Dumbledore'},\n", - " {'index': '3', 'key': 'Spying for', 'value': 'Order of the Phoenix'},\n", + " 'properties': [{'index': '1',\n", + " 'key': 'Occupation',\n", + " 'value': 'Potions Master, Defense Against the Dark Arts Professor, Headmaster'},\n", + " {'index': '2',\n", + " 'key': 'Loyalty',\n", + " 'value': 'Hogwarts, Order of the Phoenix, Albus Dumbledore, Lily Potter'},\n", + " {'index': '3',\n", + " 'key': 'Skills',\n", + " 'value': 'Potions, Occlumency, Legilimency, Spell creation'},\n", " {'index': '4', 'key': 'Patronus', 'value': 'Doe'},\n", - " {'index': '5', 'key': 'Played by', 'value': 'Alan Rickman'}]}" + " {'index': '5', 'key': 'Actor', 'value': 'Alan Rickman'}]}" ] }, - "execution_count": 109, + "execution_count": 5, "metadata": {}, "output_type": "execute_result" } @@ -248,7 +255,7 @@ }, { "cell_type": "code", - "execution_count": 110, + "execution_count": 6, "id": "1f2a2b14-a956-4f96-90c9-e11ca04ab7d1", "metadata": {}, "outputs": [ @@ -258,24 +265,34 @@ "{'users': [{'age': 38,\n", " 'name': 'Severus Snape',\n", " 'house': 'Slytherin',\n", - " 'properties': [{'index': '1', 'key': 'Position', 'value': 'Potions Master'},\n", - " {'index': '2', 'key': 'Loyalty', 'value': 'Dumbledore'},\n", - " {'index': '3', 'key': 'Special Ability', 'value': 'Occlumency'},\n", + " 'properties': [{'index': '1',\n", + " 'key': 'Occupation',\n", + " 'value': 'Potions Master, Defense Against the Dark Arts teacher, Headmaster'},\n", + " {'index': '2',\n", + " 'key': 'Loyalty',\n", + " 'value': 'Hogwarts School, Order of the Phoenix, Albus Dumbledore'},\n", + " {'index': '3',\n", + " 'key': 'Skills',\n", + " 'value': 'Potions, Occlumency, Legilimency'},\n", " {'index': '4', 'key': 'Patronus', 'value': 'Doe'},\n", " {'index': '5', 'key': 'Played by', 'value': 'Alan Rickman'}]},\n", " {'age': 115,\n", " 'name': 'Albus Dumbledore',\n", " 'house': 'Gryffindor',\n", - " 'properties': [{'index': '1', 'key': 'Position', 'value': 'Headmaster'},\n", - " {'index': '2', 'key': 'Founder of', 'value': 'Order of the Phoenix'},\n", - " {'index': '3', 'key': 'Possession', 'value': 'Elder Wand'},\n", - " {'index': '4', 'key': 'Special Ability', 'value': 'Incredible Wizard'},\n", + " 'properties': [{'index': '1',\n", + " 'key': 'Occupation',\n", + " 'value': 'Headmaster, Founder of the Order of the Phoenix'},\n", + " {'index': '2',\n", + " 'key': 'Loyalty',\n", + " 'value': 'Hogwarts School, Order of the Phoenix'},\n", + " {'index': '3', 'key': 'Skills', 'value': 'Transfiguration, Alchemy'},\n", + " {'index': '4', 'key': 'Patronus', 'value': 'Phoenix'},\n", " {'index': '5',\n", " 'key': 'Played by',\n", - " 'value': 'Michael Gambon (primarily)'}]}]}" + " 'value': 'Richard Harris (films 1-2), Michael Gambon (films 3-8)'}]}]}" ] }, - "execution_count": 110, + "execution_count": 6, "metadata": {}, "output_type": "execute_result" } @@ -309,7 +326,7 @@ }, { "cell_type": "code", - "execution_count": 111, + "execution_count": 7, "id": "6de8768e-b36a-4a51-9cf9-940d178552f6", "metadata": {}, "outputs": [ @@ -317,15 +334,17 @@ "data": { "text/plain": [ "{'users': [{'id': 1, 'name': 'Harry Potter', 'friends': [2, 3, 4, 5, 6, 7]},\n", - " {'id': 2, 'name': 'Ron Weasley', 'friends': [1, 3, 5, 6]},\n", + " {'id': 2, 'name': 'Ron Weasley', 'friends': [1, 3, 5, 6, 7]},\n", " {'id': 3, 'name': 'Hermione Granger', 'friends': [1, 2, 5, 6, 7]},\n", - " {'id': 4, 'name': 'Draco Malfoy', 'friends': [1, 5]},\n", - " {'id': 5, 'name': 'Neville Longbottom', 'friends': [1, 2, 3, 6]},\n", - " {'id': 6, 'name': 'Luna Lovegood', 'friends': [1, 2, 3, 5]},\n", - " {'id': 7, 'name': 'Ginny Weasley', 'friends': [1, 3]}]}" + " {'id': 4, 'name': 'Draco Malfoy', 'friends': [1, 7]},\n", + " {'id': 5, 'name': 'Neville Longbottom', 'friends': [1, 2, 3]},\n", + " {'id': 6, 'name': 'Luna Lovegood', 'friends': [1, 2, 3]},\n", + " {'id': 7, 'name': 'Ginny Weasley', 'friends': [1, 2, 3]},\n", + " {'id': 8, 'name': 'Fred Weasley', 'friends': [2, 7]},\n", + " {'id': 9, 'name': 'George Weasley', 'friends': [2, 7]}]}" ] }, - "execution_count": 111, + "execution_count": 7, "metadata": {}, "output_type": "execute_result" } @@ -354,7 +373,7 @@ }, { "cell_type": "code", - "execution_count": 112, + "execution_count": 8, "id": "b31e10d7-ebd2-49b4-b2c4-20dd67ca135d", "metadata": {}, "outputs": [ @@ -367,141 +386,153 @@ "\n", "\n", - "\n", - "\n", - "\n", + "\n", + "\n", + "\n", "\n", "\n", "1\n", - "\n", - "Harry Potter\n", + "\n", + "Harry Potter\n", "\n", "\n", "\n", "2\n", - "\n", - "Ron Weasley\n", + "\n", + "Ron Weasley\n", "\n", "\n", "\n", "1->2\n", - "\n", - "\n", + "\n", + "\n", "\n", "\n", "\n", "3\n", - "\n", - "Hermione Granger\n", + "\n", + "Hermione Granger\n", "\n", "\n", "\n", "1->3\n", - "\n", - "\n", + "\n", + "\n", "\n", "\n", "\n", "4\n", - "\n", - "Draco Malfoy\n", + "\n", + "Draco Malfoy\n", "\n", "\n", "\n", "1->4\n", - "\n", - "\n", + "\n", + "\n", "\n", "\n", "\n", "5\n", - "\n", - "Neville Longbottom\n", + "\n", + "Neville Longbottom\n", "\n", "\n", "\n", "1->5\n", - "\n", - "\n", + "\n", + "\n", "\n", "\n", "\n", "6\n", - "\n", - "Luna Lovegood\n", + "\n", + "Luna Lovegood\n", "\n", "\n", "\n", "1->6\n", - "\n", - "\n", + "\n", + "\n", "\n", "\n", "\n", "7\n", - "\n", - "Ginny Weasley\n", + "\n", + "Ginny Weasley\n", "\n", "\n", "\n", "1->7\n", - "\n", - "\n", + "\n", + "\n", "\n", "\n", "\n", "2->3\n", - "\n", - "\n", + "\n", + "\n", "\n", "\n", "\n", "2->5\n", - "\n", - "\n", + "\n", + "\n", "\n", "\n", "\n", "2->6\n", - "\n", - "\n", + "\n", + "\n", + "\n", + "\n", + "\n", + "2->7\n", + "\n", + "\n", "\n", "\n", - "\n", + "\n", "3->5\n", - "\n", - "\n", + "\n", + "\n", "\n", "\n", - "\n", + "\n", "3->6\n", - "\n", - "\n", + "\n", + "\n", "\n", "\n", - "\n", - "3->7\n", - "\n", - "\n", - "\n", - "\n", "\n", - "4->5\n", - "\n", - "\n", + "3->7\n", + "\n", + "\n", "\n", - "\n", + "\n", "\n", - "5->6\n", - "\n", - "\n", + "4->7\n", + "\n", + "\n", + "\n", + "\n", + "\n", + "8\n", + "\n", + "Fred Weasley\n", + "\n", + "\n", + "\n", + "9\n", + "\n", + "George Weasley\n", "\n", "\n", "\n" ], "text/plain": [ - "" + "" ] }, "metadata": {}, @@ -542,9 +573,9 @@ ], "metadata": { "kernelspec": { - "display_name": "Python 3 (ipykernel)", + "display_name": "instructor-env", "language": "python", - "name": "python3" + "name": "instructor-env" }, "language_info": { "codemirror_mode": { diff --git a/tutorials/README.md b/tutorials/README.md new file mode 100644 index 0000000..0362b51 --- /dev/null +++ b/tutorials/README.md @@ -0,0 +1,58 @@ +# Introduction + +This section includes a list of notebooks that walk you through some simple concepts in Instructor. We start small and then work our way up to more complex and tricky implementations using the library. + +## Overview + +Currently we have the following notebooks avaliable + +1. `Introduction` - This is a quick walkthrough some of the benefits of Pydantic and how the Instructor Library integrates nicely with Pydantic with `instructor.patch()` + +2. `Tips` - Quick demonstration of how to use enums, `Pydantic` models and structured prompting to get specific output formats + +3. + + + +## Installation + +We utilise the Graphviz package in this tutorial series. If you don't have it on hand, you should download it. Mac users can do so by running `brew install graphviz` while Linux users can try `sudo apt install graphviz` ( modify to your system specific package manager). + +If you're encountering an error like the following when trying to run graphviz after installing it, just restart the notebook and verify you've got graphviz installed by running `dot -v` in your shell. + +``` +Command '[PosixPath('dot'), '-Kdot', '-Tsvg']' died with . +``` + +Here are the steps to start running the notebooks + +1. Create a virtual environment + +``` +python3 -m venv .venv +source .venv .venv/bin/activate +``` + +2. Install the dependencies + +``` +pip3 install -r requirements.txt +``` + +3. Add the virtual environment to Jupyter notebook + +``` +python -m ipykernel install --user --name=instructor-env +``` + +4. Add OpenAI API Key into your shell by running the following command. This will be set for as long as the shell is open. + +``` +export OPENAI_API_KEY= +``` + +5. Start Jupyter Notebook + +``` +jupyter notebook +``` \ No newline at end of file diff --git a/tutorials/requirements.txt b/tutorials/requirements.txt new file mode 100644 index 0000000..b2258fd --- /dev/null +++ b/tutorials/requirements.txt @@ -0,0 +1,6 @@ +ipykernel +jupyter +instructor +openai>=1.1.0 +pydantic +graphviz \ No newline at end of file