Files
instructor/tutorials/2.tips.ipynb
T

596 lines
23 KiB
Plaintext

{
"cells": [
{
"cell_type": "markdown",
"id": "8bb7d0d0-2b7f-4e9e-8565-467dc5c6fd22",
"metadata": {},
"source": [
"# General Tips on Prompting\n",
"\n",
"Before we get into some big applications of schema engineering I want to equip you with the tools for success. \n",
"\n",
"This notebook is to share some general advice when using prompts to get the most of your models. "
]
},
{
"cell_type": "markdown",
"id": "8a785c25-b08d-4ab4-bbd7-22e3b090c2ed",
"metadata": {},
"source": [
"## Classification\n",
"\n",
"For classification we've found theres generally two methods of modeling.\n",
"\n",
"1. using Enums\n",
"2. using Literals\n",
"\n",
"\n",
"Use an enum in Python when you need a set of named constants that are related and you want to ensure type safety, readability, and prevent invalid values. Enums are helpful for grouping and iterating over these constants.\n",
"\n",
"Use literals when you have a small, unchanging set of values that you don't need to group or iterate over, and when type safety and preventing invalid values is less of a concern. Literals are simpler and more direct for basic, one-off values."
]
},
{
"cell_type": "code",
"execution_count": 9,
"id": "fdf5e1d9-31ad-4e8a-a55e-e2e70fff598d",
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"{'age': 17, 'name': 'Harry Potter', 'house': <House.Gryffindor: 'gryffindor'>}"
]
},
"execution_count": 9,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"from enum import Enum\n",
"from pydantic import BaseModel,Field\n",
"from typing import List,Literal\n",
"import instructor\n",
"from openai import OpenAI\n",
"\n",
"client = instructor.patch(OpenAI())\n",
"\n",
"# Tip: Do not use auto() as they cast to 1,2,3,4 \n",
"class House(Enum):\n",
" Gryffindor = \"gryffindor\"\n",
" Hufflepuff = \"hufflepuff\"\n",
" Ravenclaw = \"ravenclaw\"\n",
" Slytherin = \"slytherin\"\n",
"\n",
"class Character(BaseModel):\n",
" age: int\n",
" name: str\n",
" house: House\n",
" \n",
"resp = client.chat.completions.create(\n",
" model=\"gpt-4-1106-preview\",\n",
" messages=[\n",
" {\n",
" \"role\": \"user\", \n",
" \"content\": \"Harry Potter\"\n",
" }\n",
" ],\n",
" response_model=Character\n",
")\n",
"resp.model_dump()"
]
},
{
"cell_type": "code",
"execution_count": 10,
"id": "03db160c-81e9-4373-bfec-7a107224b6dd",
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"{'age': 17, 'name': 'Harry Potter', 'house': 'Gryffindor'}"
]
},
"execution_count": 10,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"class Character(BaseModel):\n",
" age: int\n",
" name: str\n",
" house: Literal[\"Gryffindor\", \"Hufflepuff\", \"Ravenclaw\", \"Slytherin\"]\n",
" \n",
"resp = client.chat.completions.create(\n",
" model=\"gpt-4-1106-preview\",\n",
" messages=[\n",
" {\n",
" \"role\": \"user\", \n",
" \"content\": \"Harry Potter\"\n",
" }\n",
" ],\n",
" response_model=Character\n",
")\n",
"resp.model_dump()"
]
},
{
"cell_type": "markdown",
"id": "803e0ce6-6e7e-4d86-a7a8-49ebaad0a40b",
"metadata": {},
"source": [
"## Arbitrary long properties\n",
"\n",
"Often times there are long properties that you might want to extract from data that we can not specify in advanced. We can get around this by defining an arbitrary key value store like so:"
]
},
{
"cell_type": "code",
"execution_count": 4,
"id": "0e7938b8-4666-4df4-bd80-f53e8baf7550",
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"{'age': 38,\n",
" 'name': 'Severus Snape',\n",
" 'house': 'Slytherin',\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": 4,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"from typing import List\n",
"\n",
"class Property(BaseModel):\n",
" key: str = Field(description=\"Must be snake case\")\n",
" value: str\n",
"\n",
"class Character(BaseModel):\n",
" age: int\n",
" name: str\n",
" house: Literal[\"Gryffindor\", \"Hufflepuff\", \"Ravenclaw\", \"Slytherin\"]\n",
" properties: List[Property]\n",
"\n",
"resp = client.chat.completions.create(\n",
" model=\"gpt-4-1106-preview\",\n",
" messages=[\n",
" {\n",
" \"role\": \"user\", \n",
" \"content\": \"Snape from Harry Potter\"\n",
" }\n",
" ],\n",
" response_model=Character\n",
")\n",
"resp.model_dump()"
]
},
{
"cell_type": "markdown",
"id": "b3e62f68-a79f-4f65-9c1f-726e4e2d340a",
"metadata": {},
"source": [
"## Limiting the length of lists \n",
"\n",
"In later chapters we'll talk about how to use validators to assert the length of lists but we can also use prompting tricks to enumerate values. Here we'll define a index to count the properties.\n",
"\n",
"In this following example instead of extraction we're going to work on generation instead."
]
},
{
"cell_type": "code",
"execution_count": 5,
"id": "69a58d01-ab6f-41b6-bc0c-b0e55fdb6fe4",
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"{'age': 38,\n",
" 'name': 'Severus Snape',\n",
" 'house': 'Slytherin',\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': 'Actor', 'value': 'Alan Rickman'}]}"
]
},
"execution_count": 5,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"class Property(BaseModel):\n",
" index: str = Field(..., description=\"Monotonically increasing ID\")\n",
" key: str\n",
" value: str\n",
"\n",
"class Character(BaseModel):\n",
" age: int\n",
" name: str\n",
" house: Literal[\"Gryffindor\", \"Hufflepuff\", \"Ravenclaw\", \"Slytherin\"]\n",
" properties: List[Property] = Field(..., description=\"Numbered list of arbitrary extracted properties, should be exactly 5\")\n",
"\n",
"resp = client.chat.completions.create(\n",
" model=\"gpt-4-1106-preview\",\n",
" messages=[\n",
" {\n",
" \"role\": \"user\", \n",
" \"content\": \"Snape from Harry Potter\"\n",
" }\n",
" ],\n",
" response_model=Character\n",
")\n",
"resp.model_dump()"
]
},
{
"cell_type": "markdown",
"id": "bbc1d900-617a-4e4d-a401-6d10a5153cda",
"metadata": {},
"source": [
"## Defining Multiple Entities\n",
"\n",
"Now that we see a single entity with many properties we can continue to nest them into many users"
]
},
{
"cell_type": "code",
"execution_count": 6,
"id": "1f2a2b14-a956-4f96-90c9-e11ca04ab7d1",
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"{'users': [{'age': 38,\n",
" 'name': 'Severus Snape',\n",
" 'house': 'Slytherin',\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',\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': 'Richard Harris (films 1-2), Michael Gambon (films 3-8)'}]}]}"
]
},
"execution_count": 6,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"class Characters(BaseModel):\n",
" users: List[Character]\n",
"\n",
"resp = client.chat.completions.create(\n",
" model=\"gpt-4-1106-preview\",\n",
" messages=[\n",
" {\n",
" \"role\": \"user\", \n",
" \"content\": \"Snape and Dumbledore from Harry Potter\"\n",
" }\n",
" ],\n",
" response_model=Characters\n",
")\n",
"resp.model_dump()"
]
},
{
"cell_type": "markdown",
"id": "f6ed3144-bde1-4033-9c94-a6926fa079d2",
"metadata": {},
"source": [
"## Defining Relationships \n",
"\n",
"Now only can we define lists of users, with list of properties one of the more interesting things I've learned about prompting is that we can also easily define lists of references."
]
},
{
"cell_type": "code",
"execution_count": 7,
"id": "6de8768e-b36a-4a51-9cf9-940d178552f6",
"metadata": {},
"outputs": [
{
"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, 7]},\n",
" {'id': 3, 'name': 'Hermione Granger', 'friends': [1, 2, 5, 6, 7]},\n",
" {'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": 7,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"class Character(BaseModel):\n",
" id: int\n",
" name: str\n",
" friends: List[int]\n",
"\n",
"class Characters(BaseModel):\n",
" users: List[Character]\n",
"\n",
"resp = client.chat.completions.create(\n",
" model=\"gpt-4-1106-preview\",\n",
" messages=[\n",
" {\n",
" \"role\": \"user\", \n",
" \"content\": \"The kids from from Harry Potter\"\n",
" }\n",
" ],\n",
" response_model=Characters\n",
")\n",
"resp.model_dump()"
]
},
{
"cell_type": "code",
"execution_count": 8,
"id": "b31e10d7-ebd2-49b4-b2c4-20dd67ca135d",
"metadata": {},
"outputs": [
{
"data": {
"image/svg+xml": [
"<?xml version=\"1.0\" encoding=\"UTF-8\" standalone=\"no\"?>\n",
"<!DOCTYPE svg PUBLIC \"-//W3C//DTD SVG 1.1//EN\"\n",
" \"http://www.w3.org/Graphics/SVG/1.1/DTD/svg11.dtd\">\n",
"<!-- Generated by graphviz version 9.0.0 (20230911.1827)\n",
" -->\n",
"<!-- Pages: 1 -->\n",
"<svg width=\"704pt\" height=\"260pt\"\n",
" viewBox=\"0.00 0.00 704.49 260.00\" xmlns=\"http://www.w3.org/2000/svg\" xmlns:xlink=\"http://www.w3.org/1999/xlink\">\n",
"<g id=\"graph0\" class=\"graph\" transform=\"scale(1 1) rotate(0) translate(4 256)\">\n",
"<polygon fill=\"white\" stroke=\"none\" points=\"-4,4 -4,-256 700.49,-256 700.49,4 -4,4\"/>\n",
"<!-- 1 -->\n",
"<g id=\"node1\" class=\"node\">\n",
"<title>1</title>\n",
"<ellipse fill=\"none\" stroke=\"black\" cx=\"336.67\" cy=\"-234\" rx=\"56.98\" ry=\"18\"/>\n",
"<text text-anchor=\"middle\" x=\"336.67\" y=\"-228.95\" font-family=\"Times,serif\" font-size=\"14.00\">Harry Potter</text>\n",
"</g>\n",
"<!-- 2 -->\n",
"<g id=\"node2\" class=\"node\">\n",
"<title>2</title>\n",
"<ellipse fill=\"none\" stroke=\"black\" cx=\"292.67\" cy=\"-162\" rx=\"60.05\" ry=\"18\"/>\n",
"<text text-anchor=\"middle\" x=\"292.67\" y=\"-156.95\" font-family=\"Times,serif\" font-size=\"14.00\">Ron Weasley</text>\n",
"</g>\n",
"<!-- 1&#45;&gt;2 -->\n",
"<g id=\"edge1\" class=\"edge\">\n",
"<title>1&#45;&gt;2</title>\n",
"<path fill=\"none\" stroke=\"black\" d=\"M326.02,-216.05C320.99,-208.06 314.87,-198.33 309.26,-189.4\"/>\n",
"<polygon fill=\"black\" stroke=\"black\" points=\"312.31,-187.67 304.02,-181.07 306.38,-191.4 312.31,-187.67\"/>\n",
"</g>\n",
"<!-- 3 -->\n",
"<g id=\"node3\" class=\"node\">\n",
"<title>3</title>\n",
"<ellipse fill=\"none\" stroke=\"black\" cx=\"195.67\" cy=\"-90\" rx=\"81.04\" ry=\"18\"/>\n",
"<text text-anchor=\"middle\" x=\"195.67\" y=\"-84.95\" font-family=\"Times,serif\" font-size=\"14.00\">Hermione Granger</text>\n",
"</g>\n",
"<!-- 1&#45;&gt;3 -->\n",
"<g id=\"edge2\" class=\"edge\">\n",
"<title>1&#45;&gt;3</title>\n",
"<path fill=\"none\" stroke=\"black\" d=\"M293.3,-221.97C269.73,-213.92 241.78,-200.72 223.67,-180 208.99,-163.22 202,-138.75 198.68,-119.72\"/>\n",
"<polygon fill=\"black\" stroke=\"black\" points=\"202.15,-119.29 197.23,-109.91 195.23,-120.31 202.15,-119.29\"/>\n",
"</g>\n",
"<!-- 4 -->\n",
"<g id=\"node4\" class=\"node\">\n",
"<title>4</title>\n",
"<ellipse fill=\"none\" stroke=\"black\" cx=\"472.67\" cy=\"-90\" rx=\"63.63\" ry=\"18\"/>\n",
"<text text-anchor=\"middle\" x=\"472.67\" y=\"-84.95\" font-family=\"Times,serif\" font-size=\"14.00\">Draco Malfoy</text>\n",
"</g>\n",
"<!-- 1&#45;&gt;4 -->\n",
"<g id=\"edge3\" class=\"edge\">\n",
"<title>1&#45;&gt;4</title>\n",
"<path fill=\"none\" stroke=\"black\" d=\"M355.7,-216.56C367.27,-206.34 382.16,-192.77 394.67,-180 414.93,-159.31 436.52,-134.43 451.84,-116.24\"/>\n",
"<polygon fill=\"black\" stroke=\"black\" points=\"454.12,-118.97 457.85,-109.06 448.75,-114.48 454.12,-118.97\"/>\n",
"</g>\n",
"<!-- 5 -->\n",
"<g id=\"node5\" class=\"node\">\n",
"<title>5</title>\n",
"<ellipse fill=\"none\" stroke=\"black\" cx=\"86.67\" cy=\"-18\" rx=\"86.67\" ry=\"18\"/>\n",
"<text text-anchor=\"middle\" x=\"86.67\" y=\"-12.95\" font-family=\"Times,serif\" font-size=\"14.00\">Neville Longbottom</text>\n",
"</g>\n",
"<!-- 1&#45;&gt;5 -->\n",
"<g id=\"edge4\" class=\"edge\">\n",
"<title>1&#45;&gt;5</title>\n",
"<path fill=\"none\" stroke=\"black\" d=\"M286.48,-225.09C225.68,-212.45 126.37,-181.17 85.67,-108 75.5,-89.72 76.36,-65.89 79.43,-47.49\"/>\n",
"<polygon fill=\"black\" stroke=\"black\" points=\"82.85,-48.27 81.38,-37.78 75.98,-46.9 82.85,-48.27\"/>\n",
"</g>\n",
"<!-- 6 -->\n",
"<g id=\"node6\" class=\"node\">\n",
"<title>6</title>\n",
"<ellipse fill=\"none\" stroke=\"black\" cx=\"304.67\" cy=\"-18\" rx=\"69.78\" ry=\"18\"/>\n",
"<text text-anchor=\"middle\" x=\"304.67\" y=\"-12.95\" font-family=\"Times,serif\" font-size=\"14.00\">Luna Lovegood</text>\n",
"</g>\n",
"<!-- 1&#45;&gt;6 -->\n",
"<g id=\"edge5\" class=\"edge\">\n",
"<title>1&#45;&gt;6</title>\n",
"<path fill=\"none\" stroke=\"black\" d=\"M347.03,-216.03C352.57,-205.96 358.82,-192.71 361.67,-180 365.16,-164.39 365.06,-159.64 361.67,-144 353.87,-108.11 334.83,-70.16 320.89,-45.62\"/>\n",
"<polygon fill=\"black\" stroke=\"black\" points=\"324.09,-44.17 316.04,-37.28 318.04,-47.69 324.09,-44.17\"/>\n",
"</g>\n",
"<!-- 7 -->\n",
"<g id=\"node7\" class=\"node\">\n",
"<title>7</title>\n",
"<ellipse fill=\"none\" stroke=\"black\" cx=\"466.67\" cy=\"-18\" rx=\"67.73\" ry=\"18\"/>\n",
"<text text-anchor=\"middle\" x=\"466.67\" y=\"-12.95\" font-family=\"Times,serif\" font-size=\"14.00\">Ginny Weasley</text>\n",
"</g>\n",
"<!-- 1&#45;&gt;7 -->\n",
"<g id=\"edge6\" class=\"edge\">\n",
"<title>1&#45;&gt;7</title>\n",
"<path fill=\"none\" stroke=\"black\" d=\"M378.69,-221.61C428.73,-205.53 510,-170.89 545.67,-108 553.56,-94.08 553.2,-86.12 545.67,-72 538.36,-58.3 525.76,-47.51 512.74,-39.35\"/>\n",
"<polygon fill=\"black\" stroke=\"black\" points=\"514.51,-36.33 504.1,-34.36 511.02,-42.39 514.51,-36.33\"/>\n",
"</g>\n",
"<!-- 2&#45;&gt;3 -->\n",
"<g id=\"edge7\" class=\"edge\">\n",
"<title>2&#45;&gt;3</title>\n",
"<path fill=\"none\" stroke=\"black\" d=\"M270.66,-145.12C258.03,-136 241.95,-124.4 227.92,-114.28\"/>\n",
"<polygon fill=\"black\" stroke=\"black\" points=\"229.99,-111.45 219.83,-108.44 225.89,-117.13 229.99,-111.45\"/>\n",
"</g>\n",
"<!-- 2&#45;&gt;5 -->\n",
"<g id=\"edge8\" class=\"edge\">\n",
"<title>2&#45;&gt;5</title>\n",
"<path fill=\"none\" stroke=\"black\" d=\"M233.05,-159.55C191.61,-155.28 138.18,-142.73 105.67,-108 90.71,-92.03 86.38,-67.3 85.55,-47.95\"/>\n",
"<polygon fill=\"black\" stroke=\"black\" points=\"89.05,-47.93 85.44,-37.96 82.05,-48 89.05,-47.93\"/>\n",
"</g>\n",
"<!-- 2&#45;&gt;6 -->\n",
"<g id=\"edge9\" class=\"edge\">\n",
"<title>2&#45;&gt;6</title>\n",
"<path fill=\"none\" stroke=\"black\" d=\"M294.14,-143.59C296.16,-119.61 299.84,-76.14 302.26,-47.42\"/>\n",
"<polygon fill=\"black\" stroke=\"black\" points=\"305.74,-47.88 303.09,-37.62 298.76,-47.29 305.74,-47.88\"/>\n",
"</g>\n",
"<!-- 2&#45;&gt;7 -->\n",
"<g id=\"edge10\" class=\"edge\">\n",
"<title>2&#45;&gt;7</title>\n",
"<path fill=\"none\" stroke=\"black\" d=\"M311.88,-144.78C333.2,-126.83 368.7,-97.12 399.67,-72 411.66,-62.27 424.99,-51.68 436.58,-42.55\"/>\n",
"<polygon fill=\"black\" stroke=\"black\" points=\"438.55,-45.45 444.25,-36.51 434.22,-39.94 438.55,-45.45\"/>\n",
"</g>\n",
"<!-- 3&#45;&gt;5 -->\n",
"<g id=\"edge11\" class=\"edge\">\n",
"<title>3&#45;&gt;5</title>\n",
"<path fill=\"none\" stroke=\"black\" d=\"M170.11,-72.59C155.73,-63.35 137.56,-51.68 121.85,-41.59\"/>\n",
"<polygon fill=\"black\" stroke=\"black\" points=\"124.02,-38.83 113.71,-36.37 120.24,-44.72 124.02,-38.83\"/>\n",
"</g>\n",
"<!-- 3&#45;&gt;6 -->\n",
"<g id=\"edge12\" class=\"edge\">\n",
"<title>3&#45;&gt;6</title>\n",
"<path fill=\"none\" stroke=\"black\" d=\"M221.22,-72.59C235.81,-63.22 254.3,-51.34 270.17,-41.16\"/>\n",
"<polygon fill=\"black\" stroke=\"black\" points=\"271.85,-44.23 278.37,-35.89 268.07,-38.34 271.85,-44.23\"/>\n",
"</g>\n",
"<!-- 3&#45;&gt;7 -->\n",
"<g id=\"edge13\" class=\"edge\">\n",
"<title>3&#45;&gt;7</title>\n",
"<path fill=\"none\" stroke=\"black\" d=\"M246.96,-75.75C292.94,-63.87 360.36,-46.46 408.37,-34.06\"/>\n",
"<polygon fill=\"black\" stroke=\"black\" points=\"408.94,-37.53 417.75,-31.64 407.19,-30.75 408.94,-37.53\"/>\n",
"</g>\n",
"<!-- 4&#45;&gt;7 -->\n",
"<g id=\"edge14\" class=\"edge\">\n",
"<title>4&#45;&gt;7</title>\n",
"<path fill=\"none\" stroke=\"black\" d=\"M471.18,-71.7C470.56,-64.41 469.81,-55.73 469.11,-47.54\"/>\n",
"<polygon fill=\"black\" stroke=\"black\" points=\"472.6,-47.28 468.26,-37.61 465.63,-47.87 472.6,-47.28\"/>\n",
"</g>\n",
"<!-- 8 -->\n",
"<g id=\"node8\" class=\"node\">\n",
"<title>8</title>\n",
"<ellipse fill=\"none\" stroke=\"black\" cx=\"473.67\" cy=\"-234\" rx=\"61.59\" ry=\"18\"/>\n",
"<text text-anchor=\"middle\" x=\"473.67\" y=\"-228.95\" font-family=\"Times,serif\" font-size=\"14.00\">Fred Weasley</text>\n",
"</g>\n",
"<!-- 9 -->\n",
"<g id=\"node9\" class=\"node\">\n",
"<title>9</title>\n",
"<ellipse fill=\"none\" stroke=\"black\" cx=\"624.67\" cy=\"-234\" rx=\"71.82\" ry=\"18\"/>\n",
"<text text-anchor=\"middle\" x=\"624.67\" y=\"-228.95\" font-family=\"Times,serif\" font-size=\"14.00\">George Weasley</text>\n",
"</g>\n",
"</g>\n",
"</svg>\n"
],
"text/plain": [
"<graphviz.graphs.Digraph at 0x109081f90>"
]
},
"metadata": {},
"output_type": "display_data"
}
],
"source": [
"from graphviz import Digraph\n",
"from IPython.display import display\n",
"from typing import List\n",
"\n",
"dot = Digraph()\n",
"\n",
"# Create nodes for each user\n",
"for user in resp.users:\n",
" dot.node(str(user.id), user.name)\n",
"\n",
"# Create edges for friends\n",
"for user in resp.users:\n",
" for friend_id in user.friends:\n",
" # To avoid duplicating edges, only add an edge if the friend ID is greater than the user ID\n",
" if friend_id > user.id:\n",
" dot.edge(str(user.id), str(friend_id))\n",
" \n",
"\n",
"# Render the graph to a file\n",
"display(dot)"
]
},
{
"cell_type": "markdown",
"id": "523b5797-71a5-4a96-a4b7-21280fb73015",
"metadata": {},
"source": [
"With the tools we've discussed, we can find numerous real-world applications in production settings. These include extracting action items from transcripts, generating fake data, filling out forms, and creating objects that correspond to generative UI. These simple tricks will be highly useful."
]
}
],
"metadata": {
"kernelspec": {
"display_name": "instructor-env",
"language": "python",
"name": "instructor-env"
},
"language_info": {
"codemirror_mode": {
"name": "ipython",
"version": 3
},
"file_extension": ".py",
"mimetype": "text/x-python",
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.11.6"
}
},
"nbformat": 4,
"nbformat_minor": 5
}