mirror of
https://github.com/kennethreitz/instructor.git
synced 2026-06-05 22:50:18 +00:00
565 lines
21 KiB
Plaintext
565 lines
21 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": 106,
|
|
"id": "fdf5e1d9-31ad-4e8a-a55e-e2e70fff598d",
|
|
"metadata": {},
|
|
"outputs": [
|
|
{
|
|
"data": {
|
|
"text/plain": [
|
|
"{'age': 17, 'name': 'Harry Potter', 'house': <House.Gryffindor: 'gryffindor'>}"
|
|
]
|
|
},
|
|
"execution_count": 106,
|
|
"metadata": {},
|
|
"output_type": "execute_result"
|
|
}
|
|
],
|
|
"source": [
|
|
"from enum import Enum\n",
|
|
"\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": 107,
|
|
"id": "03db160c-81e9-4373-bfec-7a107224b6dd",
|
|
"metadata": {},
|
|
"outputs": [
|
|
{
|
|
"data": {
|
|
"text/plain": [
|
|
"{'age': 17, 'name': 'Harry Potter', 'house': 'Gryffindor'}"
|
|
]
|
|
},
|
|
"execution_count": 107,
|
|
"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": 108,
|
|
"id": "0e7938b8-4666-4df4-bd80-f53e8baf7550",
|
|
"metadata": {},
|
|
"outputs": [
|
|
{
|
|
"data": {
|
|
"text/plain": [
|
|
"{'age': 38,\n",
|
|
" 'name': 'Severus Snape',\n",
|
|
" 'house': 'Slytherin',\n",
|
|
" 'properties': [{'key': 'occupation', 'value': 'Potions Master'},\n",
|
|
" {'key': 'loyalty', 'value': 'Dumbledore'},\n",
|
|
" {'key': 'patronus', 'value': 'Doe'},\n",
|
|
" {'key': 'played_by', 'value': 'Alan Rickman'}]}"
|
|
]
|
|
},
|
|
"execution_count": 108,
|
|
"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": 109,
|
|
"id": "69a58d01-ab6f-41b6-bc0c-b0e55fdb6fe4",
|
|
"metadata": {},
|
|
"outputs": [
|
|
{
|
|
"data": {
|
|
"text/plain": [
|
|
"{'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",
|
|
" {'index': '4', 'key': 'Patronus', 'value': 'Doe'},\n",
|
|
" {'index': '5', 'key': 'Played by', 'value': 'Alan Rickman'}]}"
|
|
]
|
|
},
|
|
"execution_count": 109,
|
|
"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": 110,
|
|
"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', 'key': 'Position', 'value': 'Potions Master'},\n",
|
|
" {'index': '2', 'key': 'Loyalty', 'value': 'Dumbledore'},\n",
|
|
" {'index': '3', 'key': 'Special Ability', 'value': 'Occlumency'},\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",
|
|
" {'index': '5',\n",
|
|
" 'key': 'Played by',\n",
|
|
" 'value': 'Michael Gambon (primarily)'}]}]}"
|
|
]
|
|
},
|
|
"execution_count": 110,
|
|
"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": 111,
|
|
"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]},\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]}]}"
|
|
]
|
|
},
|
|
"execution_count": 111,
|
|
"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": 112,
|
|
"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=\"470pt\" height=\"332pt\"\n",
|
|
" viewBox=\"0.00 0.00 469.97 332.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 328)\">\n",
|
|
"<polygon fill=\"white\" stroke=\"none\" points=\"-4,4 -4,-328 465.97,-328 465.97,4 -4,4\"/>\n",
|
|
"<!-- 1 -->\n",
|
|
"<g id=\"node1\" class=\"node\">\n",
|
|
"<title>1</title>\n",
|
|
"<ellipse fill=\"none\" stroke=\"black\" cx=\"267.78\" cy=\"-306\" rx=\"56.98\" ry=\"18\"/>\n",
|
|
"<text text-anchor=\"middle\" x=\"267.78\" y=\"-300.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=\"213.78\" cy=\"-234\" rx=\"60.05\" ry=\"18\"/>\n",
|
|
"<text text-anchor=\"middle\" x=\"213.78\" y=\"-228.95\" font-family=\"Times,serif\" font-size=\"14.00\">Ron Weasley</text>\n",
|
|
"</g>\n",
|
|
"<!-- 1->2 -->\n",
|
|
"<g id=\"edge1\" class=\"edge\">\n",
|
|
"<title>1->2</title>\n",
|
|
"<path fill=\"none\" stroke=\"black\" d=\"M254.7,-288.05C248.4,-279.89 240.71,-269.91 233.7,-260.82\"/>\n",
|
|
"<polygon fill=\"black\" stroke=\"black\" points=\"236.53,-258.77 227.65,-252.99 230.99,-263.04 236.53,-258.77\"/>\n",
|
|
"</g>\n",
|
|
"<!-- 3 -->\n",
|
|
"<g id=\"node3\" class=\"node\">\n",
|
|
"<title>3</title>\n",
|
|
"<ellipse fill=\"none\" stroke=\"black\" cx=\"317.78\" cy=\"-162\" rx=\"81.04\" ry=\"18\"/>\n",
|
|
"<text text-anchor=\"middle\" x=\"317.78\" y=\"-156.95\" font-family=\"Times,serif\" font-size=\"14.00\">Hermione Granger</text>\n",
|
|
"</g>\n",
|
|
"<!-- 1->3 -->\n",
|
|
"<g id=\"edge2\" class=\"edge\">\n",
|
|
"<title>1->3</title>\n",
|
|
"<path fill=\"none\" stroke=\"black\" d=\"M273.81,-287.87C282.29,-263.78 297.85,-219.6 308,-190.77\"/>\n",
|
|
"<polygon fill=\"black\" stroke=\"black\" points=\"311.2,-192.21 311.22,-181.62 304.6,-189.89 311.2,-192.21\"/>\n",
|
|
"</g>\n",
|
|
"<!-- 4 -->\n",
|
|
"<g id=\"node4\" class=\"node\">\n",
|
|
"<title>4</title>\n",
|
|
"<ellipse fill=\"none\" stroke=\"black\" cx=\"78.78\" cy=\"-162\" rx=\"63.63\" ry=\"18\"/>\n",
|
|
"<text text-anchor=\"middle\" x=\"78.78\" y=\"-156.95\" font-family=\"Times,serif\" font-size=\"14.00\">Draco Malfoy</text>\n",
|
|
"</g>\n",
|
|
"<!-- 1->4 -->\n",
|
|
"<g id=\"edge3\" class=\"edge\">\n",
|
|
"<title>1->4</title>\n",
|
|
"<path fill=\"none\" stroke=\"black\" d=\"M226.38,-293.25C201.03,-284.65 168.93,-271.13 144.78,-252 123.3,-234.99 105.29,-209.37 93.54,-189.97\"/>\n",
|
|
"<polygon fill=\"black\" stroke=\"black\" points=\"96.66,-188.38 88.59,-181.51 90.62,-191.91 96.66,-188.38\"/>\n",
|
|
"</g>\n",
|
|
"<!-- 5 -->\n",
|
|
"<g id=\"node5\" class=\"node\">\n",
|
|
"<title>5</title>\n",
|
|
"<ellipse fill=\"none\" stroke=\"black\" cx=\"93.78\" cy=\"-90\" rx=\"86.67\" ry=\"18\"/>\n",
|
|
"<text text-anchor=\"middle\" x=\"93.78\" y=\"-84.95\" font-family=\"Times,serif\" font-size=\"14.00\">Neville Longbottom</text>\n",
|
|
"</g>\n",
|
|
"<!-- 1->5 -->\n",
|
|
"<g id=\"edge4\" class=\"edge\">\n",
|
|
"<title>1->5</title>\n",
|
|
"<path fill=\"none\" stroke=\"black\" d=\"M215.38,-298.46C151.13,-287.2 45.83,-257.3 5.78,-180 -1.58,-165.79 -2.1,-157.93 5.78,-144 13.68,-130.01 26.93,-119.3 40.78,-111.29\"/>\n",
|
|
"<polygon fill=\"black\" stroke=\"black\" points=\"42.36,-114.42 49.56,-106.64 39.08,-108.23 42.36,-114.42\"/>\n",
|
|
"</g>\n",
|
|
"<!-- 6 -->\n",
|
|
"<g id=\"node6\" class=\"node\">\n",
|
|
"<title>6</title>\n",
|
|
"<ellipse fill=\"none\" stroke=\"black\" cx=\"240.78\" cy=\"-18\" rx=\"69.78\" ry=\"18\"/>\n",
|
|
"<text text-anchor=\"middle\" x=\"240.78\" y=\"-12.95\" font-family=\"Times,serif\" font-size=\"14.00\">Luna Lovegood</text>\n",
|
|
"</g>\n",
|
|
"<!-- 1->6 -->\n",
|
|
"<g id=\"edge5\" class=\"edge\">\n",
|
|
"<title>1->6</title>\n",
|
|
"<path fill=\"none\" stroke=\"black\" d=\"M308.2,-293.01C331.73,-284.48 360.8,-271.11 381.78,-252 443.9,-195.39 490.99,-138.64 439.78,-72 424.13,-51.64 363.39,-37.44 313.09,-28.9\"/>\n",
|
|
"<polygon fill=\"black\" stroke=\"black\" points=\"313.81,-25.47 303.37,-27.31 312.68,-32.38 313.81,-25.47\"/>\n",
|
|
"</g>\n",
|
|
"<!-- 7 -->\n",
|
|
"<g id=\"node7\" class=\"node\">\n",
|
|
"<title>7</title>\n",
|
|
"<ellipse fill=\"none\" stroke=\"black\" cx=\"362.78\" cy=\"-90\" rx=\"67.73\" ry=\"18\"/>\n",
|
|
"<text text-anchor=\"middle\" x=\"362.78\" y=\"-84.95\" font-family=\"Times,serif\" font-size=\"14.00\">Ginny Weasley</text>\n",
|
|
"</g>\n",
|
|
"<!-- 1->7 -->\n",
|
|
"<g id=\"edge6\" class=\"edge\">\n",
|
|
"<title>1->7</title>\n",
|
|
"<path fill=\"none\" stroke=\"black\" d=\"M295.71,-289.98C329.55,-269.99 384.87,-231.15 407.78,-180 414.31,-165.4 413.29,-159.02 407.78,-144 403.99,-133.68 397.2,-124 390.03,-115.8\"/>\n",
|
|
"<polygon fill=\"black\" stroke=\"black\" points=\"392.67,-113.5 383.25,-108.64 387.58,-118.31 392.67,-113.5\"/>\n",
|
|
"</g>\n",
|
|
"<!-- 2->3 -->\n",
|
|
"<g id=\"edge7\" class=\"edge\">\n",
|
|
"<title>2->3</title>\n",
|
|
"<path fill=\"none\" stroke=\"black\" d=\"M237.38,-217.12C251.17,-207.83 268.82,-195.95 284.06,-185.69\"/>\n",
|
|
"<polygon fill=\"black\" stroke=\"black\" points=\"285.59,-188.88 291.93,-180.4 281.68,-183.08 285.59,-188.88\"/>\n",
|
|
"</g>\n",
|
|
"<!-- 2->5 -->\n",
|
|
"<g id=\"edge8\" class=\"edge\">\n",
|
|
"<title>2->5</title>\n",
|
|
"<path fill=\"none\" stroke=\"black\" d=\"M203.39,-215.78C191.85,-197.22 172.13,-167.23 151.78,-144 143.04,-134.02 132.57,-123.98 122.95,-115.36\"/>\n",
|
|
"<polygon fill=\"black\" stroke=\"black\" points=\"125.38,-112.84 115.56,-108.88 120.77,-118.11 125.38,-112.84\"/>\n",
|
|
"</g>\n",
|
|
"<!-- 2->6 -->\n",
|
|
"<g id=\"edge9\" class=\"edge\">\n",
|
|
"<title>2->6</title>\n",
|
|
"<path fill=\"none\" stroke=\"black\" d=\"M215.94,-215.85C220.57,-179.14 231.48,-92.66 237.18,-47.53\"/>\n",
|
|
"<polygon fill=\"black\" stroke=\"black\" points=\"240.63,-48.09 238.41,-37.73 233.69,-47.22 240.63,-48.09\"/>\n",
|
|
"</g>\n",
|
|
"<!-- 3->5 -->\n",
|
|
"<g id=\"edge10\" class=\"edge\">\n",
|
|
"<title>3->5</title>\n",
|
|
"<path fill=\"none\" stroke=\"black\" d=\"M272.18,-146.75C237.25,-135.84 188.84,-120.71 151.26,-108.96\"/>\n",
|
|
"<polygon fill=\"black\" stroke=\"black\" points=\"152.5,-105.68 141.91,-106.04 150.41,-112.37 152.5,-105.68\"/>\n",
|
|
"</g>\n",
|
|
"<!-- 3->6 -->\n",
|
|
"<g id=\"edge11\" class=\"edge\">\n",
|
|
"<title>3->6</title>\n",
|
|
"<path fill=\"none\" stroke=\"black\" d=\"M307.07,-144.01C300.69,-133.7 292.54,-120.22 285.78,-108 274.52,-87.68 262.71,-64.21 254.01,-46.48\"/>\n",
|
|
"<polygon fill=\"black\" stroke=\"black\" points=\"257.22,-45.08 249.69,-37.62 250.92,-48.14 257.22,-45.08\"/>\n",
|
|
"</g>\n",
|
|
"<!-- 3->7 -->\n",
|
|
"<g id=\"edge12\" class=\"edge\">\n",
|
|
"<title>3->7</title>\n",
|
|
"<path fill=\"none\" stroke=\"black\" d=\"M328.9,-143.7C333.97,-135.81 340.08,-126.3 345.71,-117.55\"/>\n",
|
|
"<polygon fill=\"black\" stroke=\"black\" points=\"348.5,-119.68 350.96,-109.38 342.61,-115.9 348.5,-119.68\"/>\n",
|
|
"</g>\n",
|
|
"<!-- 4->5 -->\n",
|
|
"<g id=\"edge13\" class=\"edge\">\n",
|
|
"<title>4->5</title>\n",
|
|
"<path fill=\"none\" stroke=\"black\" d=\"M82.48,-143.7C84.06,-136.32 85.95,-127.52 87.72,-119.25\"/>\n",
|
|
"<polygon fill=\"black\" stroke=\"black\" points=\"91.12,-120.1 89.79,-109.58 84.28,-118.63 91.12,-120.1\"/>\n",
|
|
"</g>\n",
|
|
"<!-- 5->6 -->\n",
|
|
"<g id=\"edge14\" class=\"edge\">\n",
|
|
"<title>5->6</title>\n",
|
|
"<path fill=\"none\" stroke=\"black\" d=\"M127.13,-73.12C148.19,-63.09 175.6,-50.04 198.22,-39.27\"/>\n",
|
|
"<polygon fill=\"black\" stroke=\"black\" points=\"199.67,-42.45 207.2,-34.99 196.66,-36.13 199.67,-42.45\"/>\n",
|
|
"</g>\n",
|
|
"</g>\n",
|
|
"</svg>\n"
|
|
],
|
|
"text/plain": [
|
|
"<graphviz.graphs.Digraph at 0x10c125fd0>"
|
|
]
|
|
},
|
|
"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": "Python 3 (ipykernel)",
|
|
"language": "python",
|
|
"name": "python3"
|
|
},
|
|
"language_info": {
|
|
"codemirror_mode": {
|
|
"name": "ipython",
|
|
"version": 3
|
|
},
|
|
"file_extension": ".py",
|
|
"mimetype": "text/x-python",
|
|
"name": "python",
|
|
"nbconvert_exporter": "python",
|
|
"pygments_lexer": "ipython3",
|
|
"version": "3.11.6"
|
|
}
|
|
},
|
|
"nbformat": 4,
|
|
"nbformat_minor": 5
|
|
}
|