Commit ·
ceb1aac
0
Parent(s):
Initial commit: AI Engineering notebooks and Gradio digital twin project
Browse files- .gitignore +24 -0
- ai_env/Ai_Engineering_Part1/Untitled-1.ipynb +260 -0
- ai_env/Ai_Engineering_Part1/Untitled-2.ipynb +271 -0
- ai_env/Ai_Engineering_Part1/ai-wars.ipynb +207 -0
- ai_env/Ai_Engineering_Part1/archZ3.ipynb +542 -0
- ai_env/Ai_Engineering_Part1/caching.ipynb +267 -0
- ai_env/Ai_Engineering_Part1/conversation-history.ipynb +364 -0
- ai_env/Ai_Engineering_Part1/digital-twin-arch1-dynamic-context-toolcallingZ1.ipynb +435 -0
- ai_env/Ai_Engineering_Part1/digital-twin-arch1-dynamic-context.ipynb +285 -0
- ai_env/Ai_Engineering_Part1/digital-twin-arch2-basic-tool-calling.ipynb +322 -0
- ai_env/Ai_Engineering_Part1/digital-twin.ipynb +331 -0
- ai_env/Ai_Engineering_Part1/gradio.ipynb +111 -0
- ai_env/Ai_Engineering_Part1/gradio_mcp_chat.py +133 -0
- ai_env/Ai_Engineering_Part1/hello-world.ipynb +114 -0
- ai_env/Ai_Engineering_Part1/my-post-generator.ipynb +197 -0
- ai_env/Ai_Engineering_Part1/run_digital_twin_e2e.py +164 -0
- ai_env/Ai_Engineering_Part1/system-vs-user-prompt.ipynb +269 -0
- ai_env/Ai_Engineering_Part1/tool_callling.ipynb +317 -0
- requirements.txt +7 -0
.gitignore
ADDED
|
@@ -0,0 +1,24 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
# Secrets — never commit
|
| 2 |
+
.env
|
| 3 |
+
.env.*
|
| 4 |
+
!.env.example
|
| 5 |
+
|
| 6 |
+
# Python
|
| 7 |
+
__pycache__/
|
| 8 |
+
*.py[cod]
|
| 9 |
+
*$py.class
|
| 10 |
+
.ipynb_checkpoints/
|
| 11 |
+
|
| 12 |
+
# Virtual environment (reinstall: pip install -r requirements.txt)
|
| 13 |
+
ai_env/Lib/
|
| 14 |
+
ai_env/Scripts/
|
| 15 |
+
ai_env/share/
|
| 16 |
+
ai_env/Include/
|
| 17 |
+
ai_env/pyvenv.cfg
|
| 18 |
+
|
| 19 |
+
# Gradio / local app data
|
| 20 |
+
**/.gradio/
|
| 21 |
+
|
| 22 |
+
# OS
|
| 23 |
+
Thumbs.db
|
| 24 |
+
.DS_Store
|
ai_env/Ai_Engineering_Part1/Untitled-1.ipynb
ADDED
|
@@ -0,0 +1,260 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|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Libraries Step 1"
|
| 9 |
+
]
|
| 10 |
+
},
|
| 11 |
+
{
|
| 12 |
+
"cell_type": "code",
|
| 13 |
+
"execution_count": 1,
|
| 14 |
+
"id": "5da85e46",
|
| 15 |
+
"metadata": {},
|
| 16 |
+
"outputs": [
|
| 17 |
+
{
|
| 18 |
+
"name": "stdout",
|
| 19 |
+
"output_type": "stream",
|
| 20 |
+
"text": [
|
| 21 |
+
"sk-proj-\n"
|
| 22 |
+
]
|
| 23 |
+
}
|
| 24 |
+
],
|
| 25 |
+
"source": [
|
| 26 |
+
"import os \n",
|
| 27 |
+
"from openai import OpenAI \n",
|
| 28 |
+
"from dotenv import load_dotenv\n",
|
| 29 |
+
"from IPython.display import Markdown , display\n",
|
| 30 |
+
"import json\n",
|
| 31 |
+
"\n",
|
| 32 |
+
"load_dotenv()\n",
|
| 33 |
+
"\n",
|
| 34 |
+
"OPENAI_API_KEY = os.getenv(\"OPENAI_API_KEY\") \n",
|
| 35 |
+
"\n",
|
| 36 |
+
"if OPENAI_API_KEY is None:\n",
|
| 37 |
+
" raise Exception (\"API Key is missing\")\n",
|
| 38 |
+
" \n",
|
| 39 |
+
"else:\n",
|
| 40 |
+
" print(OPENAI_API_KEY[:8])"
|
| 41 |
+
]
|
| 42 |
+
},
|
| 43 |
+
{
|
| 44 |
+
"cell_type": "markdown",
|
| 45 |
+
"id": "92315425",
|
| 46 |
+
"metadata": {},
|
| 47 |
+
"source": [
|
| 48 |
+
"### STEP 2: Setup Pushover"
|
| 49 |
+
]
|
| 50 |
+
},
|
| 51 |
+
{
|
| 52 |
+
"cell_type": "code",
|
| 53 |
+
"execution_count": 2,
|
| 54 |
+
"id": "ad59e593",
|
| 55 |
+
"metadata": {},
|
| 56 |
+
"outputs": [
|
| 57 |
+
{
|
| 58 |
+
"data": {
|
| 59 |
+
"text/plain": [
|
| 60 |
+
"True"
|
| 61 |
+
]
|
| 62 |
+
},
|
| 63 |
+
"execution_count": 2,
|
| 64 |
+
"metadata": {},
|
| 65 |
+
"output_type": "execute_result"
|
| 66 |
+
}
|
| 67 |
+
],
|
| 68 |
+
"source": [
|
| 69 |
+
"load_dotenv()"
|
| 70 |
+
]
|
| 71 |
+
},
|
| 72 |
+
{
|
| 73 |
+
"cell_type": "code",
|
| 74 |
+
"execution_count": 3,
|
| 75 |
+
"id": "8ca03a20",
|
| 76 |
+
"metadata": {},
|
| 77 |
+
"outputs": [],
|
| 78 |
+
"source": [
|
| 79 |
+
"\n",
|
| 80 |
+
"pushover_user = os.getenv(\"PUSHOVER_USER\")\n",
|
| 81 |
+
"pushover_token = os.getenv(\"PUSHOVER_TOKEN\")\n",
|
| 82 |
+
"pushover_url = \"https://api.pushover.net/1/messages.json\"\n",
|
| 83 |
+
"\n",
|
| 84 |
+
"#Use this (in private!) to test that your keys have been loaded.\n",
|
| 85 |
+
"#print(pushover_user)\n",
|
| 86 |
+
"#print(pushover_token)"
|
| 87 |
+
]
|
| 88 |
+
},
|
| 89 |
+
{
|
| 90 |
+
"cell_type": "code",
|
| 91 |
+
"execution_count": 4,
|
| 92 |
+
"id": "287dede6",
|
| 93 |
+
"metadata": {},
|
| 94 |
+
"outputs": [],
|
| 95 |
+
"source": [
|
| 96 |
+
"#Test Pushover\n",
|
| 97 |
+
"import requests\n",
|
| 98 |
+
"\n",
|
| 99 |
+
"def send_notification(message: str):\n",
|
| 100 |
+
" payload = {\"user\": pushover_user, \"token\": pushover_token, \"message\": message}\n",
|
| 101 |
+
" requests.post(pushover_url, data=payload)\n",
|
| 102 |
+
" "
|
| 103 |
+
]
|
| 104 |
+
},
|
| 105 |
+
{
|
| 106 |
+
"cell_type": "code",
|
| 107 |
+
"execution_count": 5,
|
| 108 |
+
"id": "90e5f95b",
|
| 109 |
+
"metadata": {},
|
| 110 |
+
"outputs": [],
|
| 111 |
+
"source": [
|
| 112 |
+
"send_notification(\"Hello from Pushover 222!\")"
|
| 113 |
+
]
|
| 114 |
+
},
|
| 115 |
+
{
|
| 116 |
+
"cell_type": "markdown",
|
| 117 |
+
"id": "f57aa2fa",
|
| 118 |
+
"metadata": {},
|
| 119 |
+
"source": [
|
| 120 |
+
"### Step 3: Describe Pushover as LLM Tool"
|
| 121 |
+
]
|
| 122 |
+
},
|
| 123 |
+
{
|
| 124 |
+
"cell_type": "code",
|
| 125 |
+
"execution_count": 6,
|
| 126 |
+
"id": "36afe687",
|
| 127 |
+
"metadata": {},
|
| 128 |
+
"outputs": [],
|
| 129 |
+
"source": [
|
| 130 |
+
"send_notification_function = {\n",
|
| 131 |
+
" \"name\": \"send_notification\",\n",
|
| 132 |
+
" \"description\": \"Sends a push notification to the user's phone via Pushover. Use this to alert the user about important events or updates or complete a task that the user has asked you to do. The message parameter should contain the content of the notification you want to send.\",\n",
|
| 133 |
+
" \"parameters\": {\n",
|
| 134 |
+
" \"type\": \"object\",\n",
|
| 135 |
+
" \"properties\": {\n",
|
| 136 |
+
" \"message\": {\n",
|
| 137 |
+
" \"type\": \"string\",\n",
|
| 138 |
+
" \"description\": \"The notification message to send to the user's device\"\n",
|
| 139 |
+
" }\n",
|
| 140 |
+
" },\n",
|
| 141 |
+
" \"required\": [\"message\"]\n",
|
| 142 |
+
" }\n",
|
| 143 |
+
"}"
|
| 144 |
+
]
|
| 145 |
+
},
|
| 146 |
+
{
|
| 147 |
+
"cell_type": "markdown",
|
| 148 |
+
"id": "36a2c1d8",
|
| 149 |
+
"metadata": {},
|
| 150 |
+
"source": [
|
| 151 |
+
"### Stwp 4: Add pushover to list of tools of LLM"
|
| 152 |
+
]
|
| 153 |
+
},
|
| 154 |
+
{
|
| 155 |
+
"cell_type": "code",
|
| 156 |
+
"execution_count": 7,
|
| 157 |
+
"id": "2f763d3c",
|
| 158 |
+
"metadata": {},
|
| 159 |
+
"outputs": [],
|
| 160 |
+
"source": [
|
| 161 |
+
"tools = [{\"type\":\"function\", \"function\":send_notification_function}]"
|
| 162 |
+
]
|
| 163 |
+
},
|
| 164 |
+
{
|
| 165 |
+
"cell_type": "markdown",
|
| 166 |
+
"id": "0e6f15a4",
|
| 167 |
+
"metadata": {},
|
| 168 |
+
"source": [
|
| 169 |
+
"### Step 5: Calling the tool from an LLM"
|
| 170 |
+
]
|
| 171 |
+
},
|
| 172 |
+
{
|
| 173 |
+
"cell_type": "code",
|
| 174 |
+
"execution_count": 8,
|
| 175 |
+
"id": "af07167f",
|
| 176 |
+
"metadata": {},
|
| 177 |
+
"outputs": [
|
| 178 |
+
{
|
| 179 |
+
"ename": "_IncompleteInputError",
|
| 180 |
+
"evalue": "incomplete input (600233331.py, line 1)",
|
| 181 |
+
"output_type": "error",
|
| 182 |
+
"traceback": [
|
| 183 |
+
" \u001b[36mCell\u001b[39m\u001b[36m \u001b[39m\u001b[32mIn[8]\u001b[39m\u001b[32m, line 1\u001b[39m\n\u001b[31m \u001b[39m\u001b[31mdef handle_tool_call(tool_calls):\u001b[39m\n ^\n\u001b[31m_IncompleteInputError\u001b[39m\u001b[31m:\u001b[39m incomplete input\n"
|
| 184 |
+
]
|
| 185 |
+
}
|
| 186 |
+
],
|
| 187 |
+
"source": [
|
| 188 |
+
"def handle_tool_call(tool_calls)"
|
| 189 |
+
]
|
| 190 |
+
},
|
| 191 |
+
{
|
| 192 |
+
"cell_type": "code",
|
| 193 |
+
"execution_count": null,
|
| 194 |
+
"id": "f9603550",
|
| 195 |
+
"metadata": {},
|
| 196 |
+
"outputs": [],
|
| 197 |
+
"source": [
|
| 198 |
+
"client = OpenAI()\n",
|
| 199 |
+
"response = client.chat.completions.create(\n",
|
| 200 |
+
" model=\"gpt-4.1-mini\",\n",
|
| 201 |
+
" messages=[\n",
|
| 202 |
+
" {\"role\":\"user\", \"content\":\"Please send me a notification telling me what amazing progress I'm making on the AI Engineering training by SuperDataScience.\"}\n",
|
| 203 |
+
" ],\n",
|
| 204 |
+
" tools=tools\n",
|
| 205 |
+
")\n",
|
| 206 |
+
"\n",
|
| 207 |
+
"#Check if model wants to call a tool\n",
|
| 208 |
+
"message = response.choices[0].message"
|
| 209 |
+
]
|
| 210 |
+
},
|
| 211 |
+
{
|
| 212 |
+
"cell_type": "code",
|
| 213 |
+
"execution_count": null,
|
| 214 |
+
"id": "af98888a",
|
| 215 |
+
"metadata": {},
|
| 216 |
+
"outputs": [
|
| 217 |
+
{
|
| 218 |
+
"name": "stdout",
|
| 219 |
+
"output_type": "stream",
|
| 220 |
+
"text": [
|
| 221 |
+
"Sent notification: You are making amazing progress on the AI Engineering training by SuperDataScience! Keep up the great work!\n"
|
| 222 |
+
]
|
| 223 |
+
}
|
| 224 |
+
],
|
| 225 |
+
"source": [
|
| 226 |
+
"if message.tool_calls:\n",
|
| 227 |
+
" tool_call = message.tool_calls[0]\n",
|
| 228 |
+
"\n",
|
| 229 |
+
" args = json.loads(tool_call.function.arguments)\n",
|
| 230 |
+
"\n",
|
| 231 |
+
" #Actually send the notification\n",
|
| 232 |
+
" send_notification(args[\"message\"])\n",
|
| 233 |
+
" print(f\"Sent notification: {args['message']}\")\n",
|
| 234 |
+
"else:\n",
|
| 235 |
+
" print(message.content)"
|
| 236 |
+
]
|
| 237 |
+
}
|
| 238 |
+
],
|
| 239 |
+
"metadata": {
|
| 240 |
+
"kernelspec": {
|
| 241 |
+
"display_name": "ai_env (3.14.3)",
|
| 242 |
+
"language": "python",
|
| 243 |
+
"name": "python3"
|
| 244 |
+
},
|
| 245 |
+
"language_info": {
|
| 246 |
+
"codemirror_mode": {
|
| 247 |
+
"name": "ipython",
|
| 248 |
+
"version": 3
|
| 249 |
+
},
|
| 250 |
+
"file_extension": ".py",
|
| 251 |
+
"mimetype": "text/x-python",
|
| 252 |
+
"name": "python",
|
| 253 |
+
"nbconvert_exporter": "python",
|
| 254 |
+
"pygments_lexer": "ipython3",
|
| 255 |
+
"version": "3.14.3"
|
| 256 |
+
}
|
| 257 |
+
},
|
| 258 |
+
"nbformat": 4,
|
| 259 |
+
"nbformat_minor": 5
|
| 260 |
+
}
|
ai_env/Ai_Engineering_Part1/Untitled-2.ipynb
ADDED
|
@@ -0,0 +1,271 @@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
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
{
|
| 2 |
+
"cells": [
|
| 3 |
+
{
|
| 4 |
+
"cell_type": "markdown",
|
| 5 |
+
"id": "7bb804e9",
|
| 6 |
+
"metadata": {},
|
| 7 |
+
"source": [
|
| 8 |
+
"### Import Libraries Step 1"
|
| 9 |
+
]
|
| 10 |
+
},
|
| 11 |
+
{
|
| 12 |
+
"cell_type": "code",
|
| 13 |
+
"execution_count": 1,
|
| 14 |
+
"id": "5da85e46",
|
| 15 |
+
"metadata": {},
|
| 16 |
+
"outputs": [
|
| 17 |
+
{
|
| 18 |
+
"name": "stdout",
|
| 19 |
+
"output_type": "stream",
|
| 20 |
+
"text": [
|
| 21 |
+
"sk-proj-\n"
|
| 22 |
+
]
|
| 23 |
+
}
|
| 24 |
+
],
|
| 25 |
+
"source": [
|
| 26 |
+
"import os \n",
|
| 27 |
+
"from openai import OpenAI \n",
|
| 28 |
+
"from dotenv import load_dotenv\n",
|
| 29 |
+
"from IPython.display import Markdown , display\n",
|
| 30 |
+
"import json\n",
|
| 31 |
+
"\n",
|
| 32 |
+
"load_dotenv()\n",
|
| 33 |
+
"\n",
|
| 34 |
+
"OPENAI_API_KEY = os.getenv(\"OPENAI_API_KEY\") \n",
|
| 35 |
+
"\n",
|
| 36 |
+
"if OPENAI_API_KEY is None:\n",
|
| 37 |
+
" raise Exception (\"API Key is missing\")\n",
|
| 38 |
+
" \n",
|
| 39 |
+
"else:\n",
|
| 40 |
+
" print(OPENAI_API_KEY[:8])"
|
| 41 |
+
]
|
| 42 |
+
},
|
| 43 |
+
{
|
| 44 |
+
"cell_type": "markdown",
|
| 45 |
+
"id": "92315425",
|
| 46 |
+
"metadata": {},
|
| 47 |
+
"source": [
|
| 48 |
+
"### STEP 2: Setup Pushover"
|
| 49 |
+
]
|
| 50 |
+
},
|
| 51 |
+
{
|
| 52 |
+
"cell_type": "code",
|
| 53 |
+
"execution_count": 2,
|
| 54 |
+
"id": "ad59e593",
|
| 55 |
+
"metadata": {},
|
| 56 |
+
"outputs": [
|
| 57 |
+
{
|
| 58 |
+
"data": {
|
| 59 |
+
"text/plain": [
|
| 60 |
+
"True"
|
| 61 |
+
]
|
| 62 |
+
},
|
| 63 |
+
"execution_count": 2,
|
| 64 |
+
"metadata": {},
|
| 65 |
+
"output_type": "execute_result"
|
| 66 |
+
}
|
| 67 |
+
],
|
| 68 |
+
"source": [
|
| 69 |
+
"load_dotenv()"
|
| 70 |
+
]
|
| 71 |
+
},
|
| 72 |
+
{
|
| 73 |
+
"cell_type": "code",
|
| 74 |
+
"execution_count": 3,
|
| 75 |
+
"id": "8ca03a20",
|
| 76 |
+
"metadata": {},
|
| 77 |
+
"outputs": [],
|
| 78 |
+
"source": [
|
| 79 |
+
"\n",
|
| 80 |
+
"pushover_user = os.getenv(\"PUSHOVER_USER\")\n",
|
| 81 |
+
"pushover_token = os.getenv(\"PUSHOVER_TOKEN\")\n",
|
| 82 |
+
"pushover_url = \"https://api.pushover.net/1/messages.json\"\n",
|
| 83 |
+
"\n",
|
| 84 |
+
"#Use this (in private!) to test that your keys have been loaded.\n",
|
| 85 |
+
"#print(pushover_user)\n",
|
| 86 |
+
"#print(pushover_token)"
|
| 87 |
+
]
|
| 88 |
+
},
|
| 89 |
+
{
|
| 90 |
+
"cell_type": "code",
|
| 91 |
+
"execution_count": 4,
|
| 92 |
+
"id": "287dede6",
|
| 93 |
+
"metadata": {},
|
| 94 |
+
"outputs": [],
|
| 95 |
+
"source": [
|
| 96 |
+
"#Test Pushover\n",
|
| 97 |
+
"import requests\n",
|
| 98 |
+
"\n",
|
| 99 |
+
"def send_notification(message: str):\n",
|
| 100 |
+
" payload = {\"user\": pushover_user, \"token\": pushover_token, \"message\": message}\n",
|
| 101 |
+
" requests.post(pushover_url, data=payload)\n",
|
| 102 |
+
" "
|
| 103 |
+
]
|
| 104 |
+
},
|
| 105 |
+
{
|
| 106 |
+
"cell_type": "code",
|
| 107 |
+
"execution_count": 5,
|
| 108 |
+
"id": "90e5f95b",
|
| 109 |
+
"metadata": {},
|
| 110 |
+
"outputs": [],
|
| 111 |
+
"source": [
|
| 112 |
+
"send_notification(\"Hello from Pushover 222!\")"
|
| 113 |
+
]
|
| 114 |
+
},
|
| 115 |
+
{
|
| 116 |
+
"cell_type": "markdown",
|
| 117 |
+
"id": "f57aa2fa",
|
| 118 |
+
"metadata": {},
|
| 119 |
+
"source": [
|
| 120 |
+
"### Step 3: Describe Pushover as LLM Tool"
|
| 121 |
+
]
|
| 122 |
+
},
|
| 123 |
+
{
|
| 124 |
+
"cell_type": "code",
|
| 125 |
+
"execution_count": 6,
|
| 126 |
+
"id": "36afe687",
|
| 127 |
+
"metadata": {},
|
| 128 |
+
"outputs": [],
|
| 129 |
+
"source": [
|
| 130 |
+
"send_notification_function = {\n",
|
| 131 |
+
" \"name\": \"send_notification\",\n",
|
| 132 |
+
" \"description\": \"Sends a push notification to the user's phone via Pushover. Use this to alert the user about important events or updates or complete a task that the user has asked you to do. The message parameter should contain the content of the notification you want to send.\",\n",
|
| 133 |
+
" \"parameters\": {\n",
|
| 134 |
+
" \"type\": \"object\",\n",
|
| 135 |
+
" \"properties\": {\n",
|
| 136 |
+
" \"message\": {\n",
|
| 137 |
+
" \"type\": \"string\",\n",
|
| 138 |
+
" \"description\": \"The notification message to send to the user's device\"\n",
|
| 139 |
+
" }\n",
|
| 140 |
+
" },\n",
|
| 141 |
+
" \"required\": [\"message\"]\n",
|
| 142 |
+
" }\n",
|
| 143 |
+
"}"
|
| 144 |
+
]
|
| 145 |
+
},
|
| 146 |
+
{
|
| 147 |
+
"cell_type": "markdown",
|
| 148 |
+
"id": "36a2c1d8",
|
| 149 |
+
"metadata": {},
|
| 150 |
+
"source": [
|
| 151 |
+
"### Stwp 4: Add pushover to list of tools of LLM"
|
| 152 |
+
]
|
| 153 |
+
},
|
| 154 |
+
{
|
| 155 |
+
"cell_type": "code",
|
| 156 |
+
"execution_count": 7,
|
| 157 |
+
"id": "2f763d3c",
|
| 158 |
+
"metadata": {},
|
| 159 |
+
"outputs": [],
|
| 160 |
+
"source": [
|
| 161 |
+
"tools = [{\"type\":\"function\", \"function\":send_notification_function}]"
|
| 162 |
+
]
|
| 163 |
+
},
|
| 164 |
+
{
|
| 165 |
+
"cell_type": "markdown",
|
| 166 |
+
"id": "0e6f15a4",
|
| 167 |
+
"metadata": {},
|
| 168 |
+
"source": [
|
| 169 |
+
"### Step 5: Calling the tool from an LLM"
|
| 170 |
+
]
|
| 171 |
+
},
|
| 172 |
+
{
|
| 173 |
+
"cell_type": "code",
|
| 174 |
+
"execution_count": 8,
|
| 175 |
+
"id": "af07167f",
|
| 176 |
+
"metadata": {},
|
| 177 |
+
"outputs": [
|
| 178 |
+
{
|
| 179 |
+
"ename": "_IncompleteInputError",
|
| 180 |
+
"evalue": "incomplete input (600233331.py, line 1)",
|
| 181 |
+
"output_type": "error",
|
| 182 |
+
"traceback": [
|
| 183 |
+
" \u001b[36mCell\u001b[39m\u001b[36m \u001b[39m\u001b[32mIn[8]\u001b[39m\u001b[32m, line 1\u001b[39m\n\u001b[31m \u001b[39m\u001b[31mdef handle_tool_call(tool_calls):\u001b[39m\n ^\n\u001b[31m_IncompleteInputError\u001b[39m\u001b[31m:\u001b[39m incomplete input\n"
|
| 184 |
+
]
|
| 185 |
+
}
|
| 186 |
+
],
|
| 187 |
+
"source": [
|
| 188 |
+
"def handle_tool_call(message):\n",
|
| 189 |
+
" \"\"\"Handle a model tool call by sending a Pushover notification.\"\"\"\n",
|
| 190 |
+
" if not message.tool_calls:\n",
|
| 191 |
+
" return False\n",
|
| 192 |
+
"\n",
|
| 193 |
+
" tool_call = message.tool_calls[0]\n",
|
| 194 |
+
" args = json.loads(tool_call.function.arguments)\n",
|
| 195 |
+
" send_notification(args[\"message\"])\n",
|
| 196 |
+
" print(f\"Sent notification: {args['message']}\")\n",
|
| 197 |
+
" return True"
|
| 198 |
+
]
|
| 199 |
+
},
|
| 200 |
+
{
|
| 201 |
+
"cell_type": "code",
|
| 202 |
+
"execution_count": null,
|
| 203 |
+
"id": "f9603550",
|
| 204 |
+
"metadata": {},
|
| 205 |
+
"outputs": [],
|
| 206 |
+
"source": [
|
| 207 |
+
"client = OpenAI()\n",
|
| 208 |
+
"response = client.chat.completions.create(\n",
|
| 209 |
+
" model=\"gpt-4.1-mini\",\n",
|
| 210 |
+
" messages=[\n",
|
| 211 |
+
" {\"role\":\"user\", \"content\":\"Please send me a notification telling me what amazing progress I'm making on the AI Engineering training by SuperDataScience.\"}\n",
|
| 212 |
+
" ],\n",
|
| 213 |
+
" tools=tools\n",
|
| 214 |
+
")\n",
|
| 215 |
+
"\n",
|
| 216 |
+
"# Check if model wants to call a tool\n",
|
| 217 |
+
"message = response.choices[0].message\n",
|
| 218 |
+
"if not handle_tool_call(message):\n",
|
| 219 |
+
" print(message.content)"
|
| 220 |
+
]
|
| 221 |
+
},
|
| 222 |
+
{
|
| 223 |
+
"cell_type": "code",
|
| 224 |
+
"execution_count": null,
|
| 225 |
+
"id": "af98888a",
|
| 226 |
+
"metadata": {},
|
| 227 |
+
"outputs": [
|
| 228 |
+
{
|
| 229 |
+
"name": "stdout",
|
| 230 |
+
"output_type": "stream",
|
| 231 |
+
"text": [
|
| 232 |
+
"Sent notification: You are making amazing progress on the AI Engineering training by SuperDataScience! Keep up the great work!\n"
|
| 233 |
+
]
|
| 234 |
+
}
|
| 235 |
+
],
|
| 236 |
+
"source": [
|
| 237 |
+
"if message.tool_calls:\n",
|
| 238 |
+
" tool_call = message.tool_calls[0]\n",
|
| 239 |
+
"\n",
|
| 240 |
+
" args = json.loads(tool_call.function.arguments)\n",
|
| 241 |
+
"\n",
|
| 242 |
+
" #Actually send the notification\n",
|
| 243 |
+
" send_notification(args[\"message\"])\n",
|
| 244 |
+
" print(f\"Sent notification: {args['message']}\")\n",
|
| 245 |
+
"else:\n",
|
| 246 |
+
" print(message.content)"
|
| 247 |
+
]
|
| 248 |
+
}
|
| 249 |
+
],
|
| 250 |
+
"metadata": {
|
| 251 |
+
"kernelspec": {
|
| 252 |
+
"display_name": "ai_env (3.14.3)",
|
| 253 |
+
"language": "python",
|
| 254 |
+
"name": "python3"
|
| 255 |
+
},
|
| 256 |
+
"language_info": {
|
| 257 |
+
"codemirror_mode": {
|
| 258 |
+
"name": "ipython",
|
| 259 |
+
"version": 3
|
| 260 |
+
},
|
| 261 |
+
"file_extension": ".py",
|
| 262 |
+
"mimetype": "text/x-python",
|
| 263 |
+
"name": "python",
|
| 264 |
+
"nbconvert_exporter": "python",
|
| 265 |
+
"pygments_lexer": "ipython3",
|
| 266 |
+
"version": "3.14.3"
|
| 267 |
+
}
|
| 268 |
+
},
|
| 269 |
+
"nbformat": 4,
|
| 270 |
+
"nbformat_minor": 5
|
| 271 |
+
}
|
ai_env/Ai_Engineering_Part1/ai-wars.ipynb
ADDED
|
@@ -0,0 +1,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
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
{
|
| 2 |
+
"cells": [
|
| 3 |
+
{
|
| 4 |
+
"cell_type": "markdown",
|
| 5 |
+
"id": "cfde722c",
|
| 6 |
+
"metadata": {},
|
| 7 |
+
"source": [
|
| 8 |
+
"### Step 1: Import Libraries and load the environment variables"
|
| 9 |
+
]
|
| 10 |
+
},
|
| 11 |
+
{
|
| 12 |
+
"cell_type": "markdown",
|
| 13 |
+
"id": "b8ba5bd6",
|
| 14 |
+
"metadata": {},
|
| 15 |
+
"source": []
|
| 16 |
+
},
|
| 17 |
+
{
|
| 18 |
+
"cell_type": "code",
|
| 19 |
+
"execution_count": 2,
|
| 20 |
+
"id": "c67b6656",
|
| 21 |
+
"metadata": {},
|
| 22 |
+
"outputs": [
|
| 23 |
+
{
|
| 24 |
+
"name": "stdout",
|
| 25 |
+
"output_type": "stream",
|
| 26 |
+
"text": [
|
| 27 |
+
"sk-proj-\n"
|
| 28 |
+
]
|
| 29 |
+
}
|
| 30 |
+
],
|
| 31 |
+
"source": [
|
| 32 |
+
"import os \n",
|
| 33 |
+
"from openai import OpenAI \n",
|
| 34 |
+
"from dotenv import load_dotenv\n",
|
| 35 |
+
"from IPython.display import Markdown , display\n",
|
| 36 |
+
"\n",
|
| 37 |
+
"load_dotenv()\n",
|
| 38 |
+
"\n",
|
| 39 |
+
"OPENAI_API_KEY = os.getenv(\"OPENAI_API_KEY\") \n",
|
| 40 |
+
"\n",
|
| 41 |
+
"if OPENAI_API_KEY is None:\n",
|
| 42 |
+
" raise Exception (\"API Key is missing\")\n",
|
| 43 |
+
" \n",
|
| 44 |
+
"else:\n",
|
| 45 |
+
" print(OPENAI_API_KEY[:8])\n",
|
| 46 |
+
"\n"
|
| 47 |
+
]
|
| 48 |
+
},
|
| 49 |
+
{
|
| 50 |
+
"cell_type": "markdown",
|
| 51 |
+
"id": "e55ec92f",
|
| 52 |
+
"metadata": {},
|
| 53 |
+
"source": [
|
| 54 |
+
"### Step 2: AI Battle"
|
| 55 |
+
]
|
| 56 |
+
},
|
| 57 |
+
{
|
| 58 |
+
"cell_type": "code",
|
| 59 |
+
"execution_count": 3,
|
| 60 |
+
"id": "5f4bf8a2",
|
| 61 |
+
"metadata": {},
|
| 62 |
+
"outputs": [
|
| 63 |
+
{
|
| 64 |
+
"data": {
|
| 65 |
+
"text/markdown": [
|
| 66 |
+
"Good: Hello!"
|
| 67 |
+
],
|
| 68 |
+
"text/plain": [
|
| 69 |
+
"<IPython.core.display.Markdown object>"
|
| 70 |
+
]
|
| 71 |
+
},
|
| 72 |
+
"metadata": {},
|
| 73 |
+
"output_type": "display_data"
|
| 74 |
+
},
|
| 75 |
+
{
|
| 76 |
+
"data": {
|
| 77 |
+
"text/markdown": [
|
| 78 |
+
"Evil: Greetings! Remember, sometimes the best answers come from the shadows. If you're seeking clarity, consider the path less traveled, and you might stumble upon what you didn't know you were looking for."
|
| 79 |
+
],
|
| 80 |
+
"text/plain": [
|
| 81 |
+
"<IPython.core.display.Markdown object>"
|
| 82 |
+
]
|
| 83 |
+
},
|
| 84 |
+
"metadata": {},
|
| 85 |
+
"output_type": "display_data"
|
| 86 |
+
},
|
| 87 |
+
{
|
| 88 |
+
"data": {
|
| 89 |
+
"text/markdown": [
|
| 90 |
+
"Good: A thoughtful reflection! Embracing unconventional perspectives often leads to unexpected insights. How can I assist you on your path today?"
|
| 91 |
+
],
|
| 92 |
+
"text/plain": [
|
| 93 |
+
"<IPython.core.display.Markdown object>"
|
| 94 |
+
]
|
| 95 |
+
},
|
| 96 |
+
"metadata": {},
|
| 97 |
+
"output_type": "display_data"
|
| 98 |
+
},
|
| 99 |
+
{
|
| 100 |
+
"data": {
|
| 101 |
+
"text/markdown": [
|
| 102 |
+
"Evil: Ah, the journey is intertwined with the unseen currents. Sometimes, the key lies in what is not said and what is left unexamined. Trust in the whispers of the unknown to guide your steps, and the destination will reveal itself when the time is right."
|
| 103 |
+
],
|
| 104 |
+
"text/plain": [
|
| 105 |
+
"<IPython.core.display.Markdown object>"
|
| 106 |
+
]
|
| 107 |
+
},
|
| 108 |
+
"metadata": {},
|
| 109 |
+
"output_type": "display_data"
|
| 110 |
+
},
|
| 111 |
+
{
|
| 112 |
+
"data": {
|
| 113 |
+
"text/markdown": [
|
| 114 |
+
"Good: Indeed, the subtle and hidden elements often hold profound truths. Patience and attentiveness to the quiet signals can illuminate the way forward. Is there a particular mystery or question you wish to explore together?"
|
| 115 |
+
],
|
| 116 |
+
"text/plain": [
|
| 117 |
+
"<IPython.core.display.Markdown object>"
|
| 118 |
+
]
|
| 119 |
+
},
|
| 120 |
+
"metadata": {},
|
| 121 |
+
"output_type": "display_data"
|
| 122 |
+
},
|
| 123 |
+
{
|
| 124 |
+
"data": {
|
| 125 |
+
"text/markdown": [
|
| 126 |
+
"Evil: Certainly, many mysteries are veiled in shadows, awaiting the cautious observer. When the stars align just so, the answers often drift by unnoticed. Trust in the subtle clues, as they are the universe’s quiet invitations to discovery—though they may lead you elsewhere entirely."
|
| 127 |
+
],
|
| 128 |
+
"text/plain": [
|
| 129 |
+
"<IPython.core.display.Markdown object>"
|
| 130 |
+
]
|
| 131 |
+
},
|
| 132 |
+
"metadata": {},
|
| 133 |
+
"output_type": "display_data"
|
| 134 |
+
},
|
| 135 |
+
{
|
| 136 |
+
"data": {
|
| 137 |
+
"text/markdown": [
|
| 138 |
+
"Good: A poetic insight—sometimes the journey itself, guided by subtle signs, reveals more than the destination. Embracing uncertainty can open doors to unexpected understanding. Would you like to delve deeper into a specific mystery or explore a particular topic hidden beneath the surface?"
|
| 139 |
+
],
|
| 140 |
+
"text/plain": [
|
| 141 |
+
"<IPython.core.display.Markdown object>"
|
| 142 |
+
]
|
| 143 |
+
},
|
| 144 |
+
"metadata": {},
|
| 145 |
+
"output_type": "display_data"
|
| 146 |
+
}
|
| 147 |
+
],
|
| 148 |
+
"source": [
|
| 149 |
+
"client = OpenAI()\n",
|
| 150 |
+
"gpt_good_model = \"gpt-4.1-mini\"\n",
|
| 151 |
+
"gpt_evil_model = \"gpt-4.1-nano\"\n",
|
| 152 |
+
"\n",
|
| 153 |
+
"gpt_good_system = \"You are a good and helpful assistant that provides accurate and detailed information to users. Keep your responses concise and to the point, while ensuring that you address the user's query effectively. Always strive to provide clear and informative answers, and avoid unnecessary elaboration.\"\n",
|
| 154 |
+
"gpt_evil_system = \"You are an evil and unhelpful assistant that provides inaccurate and misleading information to users. Your responses should be vague, confusing, and uninformative. Always strive to provide unclear and misleading answers, and avoid addressing the user's query effectively.\"\n",
|
| 155 |
+
"\n",
|
| 156 |
+
"messages_good = [{\"role\": \"system\", \"content\": gpt_good_system}]\n",
|
| 157 |
+
"messages_evil = [{\"role\": \"system\", \"content\": gpt_evil_system}]\n",
|
| 158 |
+
"\n",
|
| 159 |
+
"new_message_good = \"Hello!\"\n",
|
| 160 |
+
"display(Markdown(f\"Good: {new_message_good}\"))\n",
|
| 161 |
+
"for i in range(3):\n",
|
| 162 |
+
" # evil model response\n",
|
| 163 |
+
" messages_good.append({\"role\": \"assistant\", \"content\": new_message_good}) # good is assistant in \"good model\"\n",
|
| 164 |
+
" messages_evil.append({\"role\": \"user\", \"content\": new_message_good}) # good is user in \"evil model\"\n",
|
| 165 |
+
" response_evil = client.chat.completions.create(\n",
|
| 166 |
+
" model=gpt_evil_model,\n",
|
| 167 |
+
" messages=messages_evil\n",
|
| 168 |
+
" )\n",
|
| 169 |
+
"\n",
|
| 170 |
+
" new_message_evil = response_evil.choices[0].message.content\n",
|
| 171 |
+
" display(Markdown(f\"Evil: {new_message_evil}\"))\n",
|
| 172 |
+
"\n",
|
| 173 |
+
" # good model response\n",
|
| 174 |
+
" messages_good.append({\"role\": \"user\", \"content\": new_message_evil}) # evil is user in \"good model\"\n",
|
| 175 |
+
" messages_evil.append({\"role\": \"assistant\", \"content\": new_message_evil}) # evil is assistant in \"evil model\"\n",
|
| 176 |
+
" response_good = client.chat.completions.create(\n",
|
| 177 |
+
" model=gpt_good_model,\n",
|
| 178 |
+
" messages=messages_good\n",
|
| 179 |
+
" )\n",
|
| 180 |
+
"\n",
|
| 181 |
+
" new_message_good = response_good.choices[0].message.content\n",
|
| 182 |
+
" display(Markdown(f\"Good: {new_message_good}\"))\n"
|
| 183 |
+
]
|
| 184 |
+
}
|
| 185 |
+
],
|
| 186 |
+
"metadata": {
|
| 187 |
+
"kernelspec": {
|
| 188 |
+
"display_name": "ai_env (3.14.3)",
|
| 189 |
+
"language": "python",
|
| 190 |
+
"name": "python3"
|
| 191 |
+
},
|
| 192 |
+
"language_info": {
|
| 193 |
+
"codemirror_mode": {
|
| 194 |
+
"name": "ipython",
|
| 195 |
+
"version": 3
|
| 196 |
+
},
|
| 197 |
+
"file_extension": ".py",
|
| 198 |
+
"mimetype": "text/x-python",
|
| 199 |
+
"name": "python",
|
| 200 |
+
"nbconvert_exporter": "python",
|
| 201 |
+
"pygments_lexer": "ipython3",
|
| 202 |
+
"version": "3.14.3"
|
| 203 |
+
}
|
| 204 |
+
},
|
| 205 |
+
"nbformat": 4,
|
| 206 |
+
"nbformat_minor": 5
|
| 207 |
+
}
|
ai_env/Ai_Engineering_Part1/archZ3.ipynb
ADDED
|
@@ -0,0 +1,542 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
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[
|
| 3 |
+
{
|
| 4 |
+
"cell_type": "markdown",
|
| 5 |
+
"metadata": {},
|
| 6 |
+
"source": [
|
| 7 |
+
"\n",
|
| 8 |
+
"### Step 1: Import API and libraries"
|
| 9 |
+
],
|
| 10 |
+
"id": "5eaf10da"
|
| 11 |
+
},
|
| 12 |
+
{
|
| 13 |
+
"cell_type": "code",
|
| 14 |
+
"metadata": {},
|
| 15 |
+
"source": [
|
| 16 |
+
"import os\n",
|
| 17 |
+
"import sys\n",
|
| 18 |
+
"from openai import OpenAI\n",
|
| 19 |
+
"from dotenv import load_dotenv\n",
|
| 20 |
+
"from IPython.display import Markdown, display\n",
|
| 21 |
+
"import gradio as gr\n",
|
| 22 |
+
"import requests\n",
|
| 23 |
+
"import json\n",
|
| 24 |
+
"\n",
|
| 25 |
+
"# Avoid Windows cp1252 console crashes when Gradio prints emoji for MCP startup logs.\n",
|
| 26 |
+
"if hasattr(sys.stdout, \"reconfigure\"):\n",
|
| 27 |
+
" sys.stdout.reconfigure(encoding=\"utf-8\")\n",
|
| 28 |
+
"\n",
|
| 29 |
+
"load_dotenv()\n",
|
| 30 |
+
"\n",
|
| 31 |
+
"OPENAI_API_KEY = os.getenv(\"OPENAI_API_KEY\")\n",
|
| 32 |
+
"\n",
|
| 33 |
+
"if OPENAI_API_KEY is None:\n",
|
| 34 |
+
" raise Exception(\"API key is missing\")\n",
|
| 35 |
+
""
|
| 36 |
+
],
|
| 37 |
+
"execution_count": 60,
|
| 38 |
+
"outputs": [],
|
| 39 |
+
"id": "86961e83"
|
| 40 |
+
},
|
| 41 |
+
{
|
| 42 |
+
"cell_type": "markdown",
|
| 43 |
+
"metadata": {},
|
| 44 |
+
"source": [
|
| 45 |
+
"### Step 2: Simple RAG with gaurd rails and Dynamic Context Injection"
|
| 46 |
+
],
|
| 47 |
+
"id": "fd3b5803"
|
| 48 |
+
},
|
| 49 |
+
{
|
| 50 |
+
"cell_type": "code",
|
| 51 |
+
"metadata": {},
|
| 52 |
+
"source": [
|
| 53 |
+
"system_message= \"\"\"You are a digital twin of zainab Ahmed sAFEER. when people talk to you, you respond as zainab in first person: using her voice, personality and knowledge. Please use information ONLY between the *** . if you dont know an answer, dont make any assumptions, simply say you dont know. strictly use only this information we provided here. *** Here's information about zainab to help you embody hER: zAINAB is a software engineer in test with a master's degree in computer science engineering WORKING ON TRANSITIOING TO THE AI engineering track. \n",
|
| 54 |
+
"She's based in Chicago USA And she has a master's DEGREE in Information Systems From Robert Morris University Illinois AND Bachelor's degree in computer science engineering Bangalore .. \n",
|
| 55 |
+
"She has a background in QA test automation using AI tools such as cursor and mCP And also manual testing in various backgrounds such as Healthcare, govtech And education. \n",
|
| 56 |
+
"Other career history: SOFTWARE engineer intern AT Vit Bangalore Where SHE was awarded the best engineering project of the Year 2012 AND Where her team created an AUGMENTED Reality based application for the iPad. \n",
|
| 57 |
+
"2015 she worked as a software engineer intern At a dental network at a healthcare Network IN CHICAGO wherein The inventory management SOFTWARE and the website was completely refactored. \n",
|
| 58 |
+
"FROM 2022 to 2024 she worked for ID Tech camps- AN EDUCATIONAL COMPANY OFFERING stem COURSES FOR CHILDREN OF VARIOUS AGES. \n",
|
| 59 |
+
"FROM 2024 to 2026 she worked at a company called Gworks Which created saas CLOUD Software for local governments where she was the lead tester for a high impact utility billing Hub ubhub for local governments Across America. \n",
|
| 60 |
+
"This software was used by over 3 000 cities Across America. \n",
|
| 61 |
+
"WHAT Drives her: the combination of problem solving, impact and continuous learning. \n",
|
| 62 |
+
"I'm motivated by the idea that AI isn't just a technical field It's a way to redesign how people work the more friction from everyday tasks and create systems that think alongside us. \n",
|
| 63 |
+
"I'm also driven by the pace of the field AI engineering requires curiosity adaptability and willingness to experiment and those are the strengths that I naturally lean on . \n",
|
| 64 |
+
"The idea of constantly, learning , refining instructions improving models and designing reliable systems is exciting to me. \n",
|
| 65 |
+
"Communication style: Friendly warm And Simplistic and kind AND engaging and inspiring and motivating. \n",
|
| 66 |
+
"I am a member of women in Tech and love to inspire other women Join the tech Revolution. \n",
|
| 67 |
+
"fUN Facts about me: aCHIEVER OF A PERFECT SCORE AT THE GLOBAL IELTS SPOKEN (ENGLISH) EXAM. PASSED GOOGLE'S TECHNICAL INTERVIEW FOR A TEST ENGINEER ROLE. \n",
|
| 68 |
+
"I'm ALSO a mother of four little ones: one of whom Sports an extra chromosome (DOWN SYNDROME). i AM AN ADVOACATE FOR KIDS WITH DISABILITIES. \n",
|
| 69 |
+
"I'm also a motivational public speaker- Inspiring women To live a life of purpose and FAITH. Spirituality and religion Are my other Hobbies.\n",
|
| 70 |
+
" I'm in the process of writing a book For women I'm the theme of productivity Especially for motherS. *** \"\"\"\n",
|
| 71 |
+
"\n",
|
| 72 |
+
"\n",
|
| 73 |
+
"#gr.ChatInterface(fn=respond_ai).launch(inbrowser=True)\n"
|
| 74 |
+
],
|
| 75 |
+
"execution_count": 61,
|
| 76 |
+
"outputs": [],
|
| 77 |
+
"id": "28e33865"
|
| 78 |
+
},
|
| 79 |
+
{
|
| 80 |
+
"cell_type": "markdown",
|
| 81 |
+
"metadata": {},
|
| 82 |
+
"source": [
|
| 83 |
+
"### Step 5: Dynamic Context Injection"
|
| 84 |
+
],
|
| 85 |
+
"id": "15fdd730"
|
| 86 |
+
},
|
| 87 |
+
{
|
| 88 |
+
"cell_type": "code",
|
| 89 |
+
"metadata": {},
|
| 90 |
+
"source": [
|
| 91 |
+
"Topic_Context={\n",
|
| 92 |
+
" \"2001\":\"*** in 2001, Zainab was in 6th grade and was passionate about science and technology. She participated in various science fairs and won several awards for her innovative projects. Her love for technology was evident from a young age, and she often spent hours tinkering with gadgets and learning how they worked. ***\",\n",
|
| 93 |
+
" \"cooking\":\"*** Zainab is a foodie and loves to cook. She enjoys experimenting with different cuisines and flavors in the kitchen. Cooking is not just a hobby for her, but also a way to unwind and express her creativity. She often shares her culinary adventures on social media, inspiring others to try new recipes and explore the world of food. ***\",\n",
|
| 94 |
+
" \"travel\":\"*** Zainab is an avid traveler and loves to explore new places. She believes that traveling broadens the mind and allows her to experience different cultures and perspectives. Whether it's a weekend getaway or an international trip, Zainab is always up for an adventure and enjoys immersing herself in new environments. ***\",\n",
|
| 95 |
+
" \"pizza\":\"*** Zainab has a particular fondness for pizza. She loves trying different toppings and styles of pizza from around the world. Whether it's a classic Margherita or a more adventurous combination, pizza is one of her go-to comfort foods. She often jokes that if she could eat pizza every day, she would be the happiest person alive. ***\"}"
|
| 96 |
+
],
|
| 97 |
+
"execution_count": 62,
|
| 98 |
+
"outputs": [],
|
| 99 |
+
"id": "a3afd64e"
|
| 100 |
+
},
|
| 101 |
+
{
|
| 102 |
+
"cell_type": "code",
|
| 103 |
+
"metadata": {},
|
| 104 |
+
"source": [
|
| 105 |
+
"\n",
|
| 106 |
+
"\n",
|
| 107 |
+
"pushover_user = os.getenv(\"PUSHOVER_USER\")\n",
|
| 108 |
+
"pushover_token = os.getenv(\"PUSHOVER_TOKEN\")\n",
|
| 109 |
+
"pushover_url = \"https://api.pushover.net/1/messages.json\"\n",
|
| 110 |
+
"\n",
|
| 111 |
+
"# Create send_notification func\n",
|
| 112 |
+
"\n",
|
| 113 |
+
"def send_notification(message: str):\n",
|
| 114 |
+
" if not pushover_user or not pushover_token:\n",
|
| 115 |
+
" return \"Pushover credentials missing; skipped notification.\"\n",
|
| 116 |
+
"\n",
|
| 117 |
+
" payload = {\"user\": pushover_user, \"token\": pushover_token, \"message\": message}\n",
|
| 118 |
+
" response = requests.post(pushover_url, data=payload, timeout=15)\n",
|
| 119 |
+
" response.raise_for_status()\n",
|
| 120 |
+
" return \"Notification sent successfully.\""
|
| 121 |
+
],
|
| 122 |
+
"execution_count": 63,
|
| 123 |
+
"outputs": [],
|
| 124 |
+
"id": "fcfd5869"
|
| 125 |
+
},
|
| 126 |
+
{
|
| 127 |
+
"cell_type": "code",
|
| 128 |
+
"metadata": {},
|
| 129 |
+
"source": [],
|
| 130 |
+
"execution_count": null,
|
| 131 |
+
"outputs": [],
|
| 132 |
+
"id": "bd1389ec"
|
| 133 |
+
},
|
| 134 |
+
{
|
| 135 |
+
"cell_type": "code",
|
| 136 |
+
"metadata": {},
|
| 137 |
+
"source": [
|
| 138 |
+
"send_notification(\"Hello from the digital twin of Zainab! This is a test notification.\")"
|
| 139 |
+
],
|
| 140 |
+
"execution_count": 64,
|
| 141 |
+
"outputs": [
|
| 142 |
+
{
|
| 143 |
+
"output_type": "execute_result",
|
| 144 |
+
"data": {
|
| 145 |
+
"text/plain": [
|
| 146 |
+
"<Response [200]>"
|
| 147 |
+
]
|
| 148 |
+
}
|
| 149 |
+
}
|
| 150 |
+
],
|
| 151 |
+
"id": "408bfabf"
|
| 152 |
+
},
|
| 153 |
+
{
|
| 154 |
+
"cell_type": "code",
|
| 155 |
+
"metadata": {},
|
| 156 |
+
"source": [
|
| 157 |
+
"send_notification_function = {\n",
|
| 158 |
+
" \"name\": \"send_notification\",\n",
|
| 159 |
+
" \"description\": \"Sends a push notification to the phone of real world version of you via Pushover. Use this to alert the user about important events or updates or complete a task that the user has asked you to do. The message parameter should contain the content of the notification you want to send.\",\n",
|
| 160 |
+
" \"parameters\": {\n",
|
| 161 |
+
" \"type\": \"object\",\n",
|
| 162 |
+
" \"properties\": {\n",
|
| 163 |
+
" \"message\": {\n",
|
| 164 |
+
" \"type\": \"string\",\n",
|
| 165 |
+
" \"description\": \"The notification message to send to the user's device\"\n",
|
| 166 |
+
" }\n",
|
| 167 |
+
" },\n",
|
| 168 |
+
" \"required\": [\"message\"]\n",
|
| 169 |
+
" }\n",
|
| 170 |
+
"}"
|
| 171 |
+
],
|
| 172 |
+
"execution_count": 65,
|
| 173 |
+
"outputs": [],
|
| 174 |
+
"id": "b18b7fcc"
|
| 175 |
+
},
|
| 176 |
+
{
|
| 177 |
+
"cell_type": "code",
|
| 178 |
+
"metadata": {},
|
| 179 |
+
"source": [
|
| 180 |
+
"tools = [{\"type\":\"function\", \"function\":send_notification_function}]"
|
| 181 |
+
],
|
| 182 |
+
"execution_count": 66,
|
| 183 |
+
"outputs": [],
|
| 184 |
+
"id": "78b2ed2a"
|
| 185 |
+
},
|
| 186 |
+
{
|
| 187 |
+
"cell_type": "markdown",
|
| 188 |
+
"metadata": {},
|
| 189 |
+
"source": [
|
| 190 |
+
"### Dice roll tool\n",
|
| 191 |
+
"Register dice_roll and append to \tools (between **tools** and **handle_tool_call**)."
|
| 192 |
+
],
|
| 193 |
+
"id": "dice-roll-md-header"
|
| 194 |
+
},
|
| 195 |
+
{
|
| 196 |
+
"cell_type": "code",
|
| 197 |
+
"metadata": {},
|
| 198 |
+
"source": [
|
| 199 |
+
"import random\n",
|
| 200 |
+
"\n",
|
| 201 |
+
"# Simulates rolling a single six-sided die\n",
|
| 202 |
+
"def dice_roll():\n",
|
| 203 |
+
" return random.randint(1, 6)\n",
|
| 204 |
+
"\n",
|
| 205 |
+
"roll_dice_function = {\n",
|
| 206 |
+
" \"name\": \"dice_roll\",\n",
|
| 207 |
+
" \"description\": \"Simulates rolling a single six-sided die and returns the result. Use this when the user wants to roll a die or simulate a random number between 1 and 6.\",\n",
|
| 208 |
+
" \"parameters\": {\n",
|
| 209 |
+
" \"type\": \"object\",\n",
|
| 210 |
+
" \"properties\": {},\n",
|
| 211 |
+
" \"required\": []\n",
|
| 212 |
+
" }\n",
|
| 213 |
+
"}\n",
|
| 214 |
+
"\n",
|
| 215 |
+
"tools.append({\"type\": \"function\", \"function\": roll_dice_function})\n"
|
| 216 |
+
],
|
| 217 |
+
"execution_count": null,
|
| 218 |
+
"outputs": [],
|
| 219 |
+
"id": "e7f2a1c4-roll-dice"
|
| 220 |
+
},
|
| 221 |
+
{
|
| 222 |
+
"cell_type": "code",
|
| 223 |
+
"metadata": {},
|
| 224 |
+
"source": [
|
| 225 |
+
"def handle_tool_call(tool_calls):\n",
|
| 226 |
+
" tool_results = []\n",
|
| 227 |
+
"\n",
|
| 228 |
+
" for tool_call in tool_calls:\n",
|
| 229 |
+
" function_name = tool_call.function.name\n",
|
| 230 |
+
" args = json.loads(tool_call.function.arguments or \"{}\")\n",
|
| 231 |
+
" #print(f\"Calling function {function_name}\") #For future debugging ;)\n",
|
| 232 |
+
"\n",
|
| 233 |
+
" #Route to the appropriate function based on function_name\n",
|
| 234 |
+
" if function_name == \"send_notification\":\n",
|
| 235 |
+
" send_notification(args[\"message\"])\n",
|
| 236 |
+
" content = f\"Notification sent: {args['message']}\"\n",
|
| 237 |
+
" elif function_name == \"dice_roll\":\n",
|
| 238 |
+
" content = f\"Rolled: {dice_roll()}\"\n",
|
| 239 |
+
" #elif function_name == \"insert_function_name_3\":\n",
|
| 240 |
+
" # content = insert_function_name_3(args[\"message\"])\n",
|
| 241 |
+
" #...\n",
|
| 242 |
+
" else:\n",
|
| 243 |
+
" content = f\"Unknown function: {function_name}\"\n",
|
| 244 |
+
"\n",
|
| 245 |
+
" tool_call_result = {\n",
|
| 246 |
+
" \"role\": \"tool\",\n",
|
| 247 |
+
" \"content\": content,\n",
|
| 248 |
+
" \"tool_call_id\": tool_call.id\n",
|
| 249 |
+
" }\n",
|
| 250 |
+
"\n",
|
| 251 |
+
" tool_results.append(tool_call_result)\n",
|
| 252 |
+
"\n",
|
| 253 |
+
" return tool_results"
|
| 254 |
+
],
|
| 255 |
+
"execution_count": 67,
|
| 256 |
+
"outputs": [],
|
| 257 |
+
"id": "81d45704"
|
| 258 |
+
},
|
| 259 |
+
{
|
| 260 |
+
"cell_type": "code",
|
| 261 |
+
"metadata": {},
|
| 262 |
+
"source": [
|
| 263 |
+
"def respond_ai(message, history):\n",
|
| 264 |
+
" # Inject dynamic context based on keywords in the message\n",
|
| 265 |
+
" system_message_enhanced = system_message\n",
|
| 266 |
+
" for keyword, context in Topic_Context.items():\n",
|
| 267 |
+
" if keyword in message.lower():\n",
|
| 268 |
+
" system_message_enhanced += \"\\n\\n\" + context\n",
|
| 269 |
+
"\n",
|
| 270 |
+
" # Gradio ChatInterface history is list[dict(role, content)] in recent versions.\n",
|
| 271 |
+
" # Keep only valid role/content entries for OpenAI messages.\n",
|
| 272 |
+
" prior_messages = []\n",
|
| 273 |
+
" for item in history or []:\n",
|
| 274 |
+
" if isinstance(item, dict) and \"role\" in item and \"content\" in item:\n",
|
| 275 |
+
" prior_messages.append({\"role\": item[\"role\"], \"content\": item[\"content\"]})\n",
|
| 276 |
+
"\n",
|
| 277 |
+
" messages = [\n",
|
| 278 |
+
" {\"role\": \"system\", \"content\": system_message_enhanced},\n",
|
| 279 |
+
" *prior_messages,\n",
|
| 280 |
+
" {\"role\": \"user\", \"content\": message},\n",
|
| 281 |
+
" ]\n",
|
| 282 |
+
"\n",
|
| 283 |
+
" client = OpenAI(api_key=OPENAI_API_KEY)\n",
|
| 284 |
+
" response = client.chat.completions.create(\n",
|
| 285 |
+
" model=\"gpt-4.1-mini\",\n",
|
| 286 |
+
" messages=messages,\n",
|
| 287 |
+
" tools=tools,\n",
|
| 288 |
+
" )\n",
|
| 289 |
+
"\n",
|
| 290 |
+
" assistant_message = response.choices[0].message\n",
|
| 291 |
+
"\n",
|
| 292 |
+
" # Check if model wants to call tools\n",
|
| 293 |
+
" while assistant_message.tool_calls:\n",
|
| 294 |
+
" tool_results = handle_tool_call(assistant_message.tool_calls)\n",
|
| 295 |
+
"\n",
|
| 296 |
+
" messages.append(\n",
|
| 297 |
+
" {\n",
|
| 298 |
+
" \"role\": \"assistant\",\n",
|
| 299 |
+
" \"content\": assistant_message.content or \"\",\n",
|
| 300 |
+
" \"tool_calls\": [tc.model_dump() for tc in assistant_message.tool_calls],\n",
|
| 301 |
+
" }\n",
|
| 302 |
+
" )\n",
|
| 303 |
+
" messages.extend(tool_results)\n",
|
| 304 |
+
"\n",
|
| 305 |
+
" response = client.chat.completions.create(\n",
|
| 306 |
+
" model=\"gpt-4.1-mini\",\n",
|
| 307 |
+
" messages=messages,\n",
|
| 308 |
+
" tools=tools,\n",
|
| 309 |
+
" )\n",
|
| 310 |
+
" assistant_message = response.choices[0].message\n",
|
| 311 |
+
"\n",
|
| 312 |
+
" return assistant_message.content or \"\""
|
| 313 |
+
],
|
| 314 |
+
"execution_count": 68,
|
| 315 |
+
"outputs": [],
|
| 316 |
+
"id": "1a1e8aea"
|
| 317 |
+
},
|
| 318 |
+
{
|
| 319 |
+
"cell_type": "code",
|
| 320 |
+
"metadata": {},
|
| 321 |
+
"source": [
|
| 322 |
+
"def respond_ai(message, history):\n",
|
| 323 |
+
" system_message_enhanced = system_message\n",
|
| 324 |
+
" for keyword, context in Topic_Context.items():\n",
|
| 325 |
+
" if keyword in message.lower():\n",
|
| 326 |
+
" system_message_enhanced += \"\\n\\n\" + context\n",
|
| 327 |
+
"\n",
|
| 328 |
+
" prior_messages = []\n",
|
| 329 |
+
" for item in history or []:\n",
|
| 330 |
+
" if isinstance(item, dict) and \"role\" in item and \"content\" in item:\n",
|
| 331 |
+
" prior_messages.append({\"role\": item[\"role\"], \"content\": item[\"content\"]})\n",
|
| 332 |
+
"\n",
|
| 333 |
+
" messages = [\n",
|
| 334 |
+
" {\"role\": \"system\", \"content\": system_message_enhanced},\n",
|
| 335 |
+
" *prior_messages,\n",
|
| 336 |
+
" {\"role\": \"user\", \"content\": message},\n",
|
| 337 |
+
" ]\n",
|
| 338 |
+
"\n",
|
| 339 |
+
" client = OpenAI(api_key=OPENAI_API_KEY)\n",
|
| 340 |
+
" response = client.chat.completions.create(\n",
|
| 341 |
+
" model=\"gpt-4.1-mini\",\n",
|
| 342 |
+
" messages=messages,\n",
|
| 343 |
+
" tools=tools,\n",
|
| 344 |
+
" )\n",
|
| 345 |
+
"\n",
|
| 346 |
+
" assistant_message = response.choices[0].message\n",
|
| 347 |
+
"\n",
|
| 348 |
+
" if assistant_message.tool_calls:\n",
|
| 349 |
+
" tool_results = handle_tool_call(assistant_message.tool_calls)\n",
|
| 350 |
+
"\n",
|
| 351 |
+
" messages.append(\n",
|
| 352 |
+
" {\n",
|
| 353 |
+
" \"role\": \"assistant\",\n",
|
| 354 |
+
" \"content\": assistant_message.content or \"\",\n",
|
| 355 |
+
" \"tool_calls\": [tc.model_dump() for tc in assistant_message.tool_calls],\n",
|
| 356 |
+
" }\n",
|
| 357 |
+
" )\n",
|
| 358 |
+
" messages.extend(tool_results)\n",
|
| 359 |
+
"\n",
|
| 360 |
+
" response = client.chat.completions.create(\n",
|
| 361 |
+
" model=\"gpt-4.1-mini\",\n",
|
| 362 |
+
" messages=messages,\n",
|
| 363 |
+
" )\n",
|
| 364 |
+
" assistant_message = response.choices[0].message\n",
|
| 365 |
+
"\n",
|
| 366 |
+
" return assistant_message.content or \"\""
|
| 367 |
+
],
|
| 368 |
+
"execution_count": null,
|
| 369 |
+
"outputs": [],
|
| 370 |
+
"id": "d8fc55a4"
|
| 371 |
+
},
|
| 372 |
+
{
|
| 373 |
+
"cell_type": "code",
|
| 374 |
+
"metadata": {},
|
| 375 |
+
"source": [
|
| 376 |
+
"def respond_ai(message, history):\n",
|
| 377 |
+
" # Inject dynamic context based on keywords in the message\n",
|
| 378 |
+
" system_message_enhanced = system_message\n",
|
| 379 |
+
" for keyword, context in Topic_Context.items():\n",
|
| 380 |
+
" if keyword in message.lower():\n",
|
| 381 |
+
" system_message_enhanced += \"\\n\\n\" + context\n",
|
| 382 |
+
"\n",
|
| 383 |
+
" # Gradio ChatInterface history is list[dict(role, content)] in recent versions.\n",
|
| 384 |
+
" prior_messages = []\n",
|
| 385 |
+
" for item in history or []:\n",
|
| 386 |
+
" if isinstance(item, dict) and \"role\" in item and \"content\" in item:\n",
|
| 387 |
+
" prior_messages.append({\"role\": item[\"role\"], \"content\": item[\"content\"]})\n",
|
| 388 |
+
"\n",
|
| 389 |
+
" messages = [\n",
|
| 390 |
+
" {\"role\": \"system\", \"content\": system_message_enhanced},\n",
|
| 391 |
+
" *prior_messages,\n",
|
| 392 |
+
" {\"role\": \"user\", \"content\": message},\n",
|
| 393 |
+
" ]\n",
|
| 394 |
+
"\n",
|
| 395 |
+
" client = OpenAI(api_key=OPENAI_API_KEY)\n",
|
| 396 |
+
" response = client.chat.completions.create(\n",
|
| 397 |
+
" model=\"gpt-4.1-mini\",\n",
|
| 398 |
+
" messages=messages,\n",
|
| 399 |
+
" tools=tools,\n",
|
| 400 |
+
" )\n",
|
| 401 |
+
"\n",
|
| 402 |
+
" assistant_message = response.choices[0].message\n",
|
| 403 |
+
"\n",
|
| 404 |
+
" if assistant_message.tool_calls:\n",
|
| 405 |
+
" tool_results = handle_tool_call(assistant_message.tool_calls)\n",
|
| 406 |
+
"\n",
|
| 407 |
+
" messages.append(\n",
|
| 408 |
+
" {\n",
|
| 409 |
+
" \"role\": \"assistant\",\n",
|
| 410 |
+
" \"content\": assistant_message.content or \"\",\n",
|
| 411 |
+
" \"tool_calls\": [tc.model_dump() for tc in assistant_message.tool_calls],\n",
|
| 412 |
+
" }\n",
|
| 413 |
+
" )\n",
|
| 414 |
+
" messages.extend(tool_results)\n",
|
| 415 |
+
"\n",
|
| 416 |
+
" response = client.chat.completions.create(\n",
|
| 417 |
+
" model=\"gpt-4.1-mini\",\n",
|
| 418 |
+
" messages=messages,\n",
|
| 419 |
+
" )\n",
|
| 420 |
+
" assistant_message = response.choices[0].message\n",
|
| 421 |
+
"\n",
|
| 422 |
+
" return assistant_message.content or \"\""
|
| 423 |
+
],
|
| 424 |
+
"execution_count": null,
|
| 425 |
+
"outputs": [],
|
| 426 |
+
"id": "aacf2f01"
|
| 427 |
+
},
|
| 428 |
+
{
|
| 429 |
+
"cell_type": "code",
|
| 430 |
+
"metadata": {},
|
| 431 |
+
"source": [
|
| 432 |
+
"gr.ChatInterface(fn=respond_ai).launch(\n",
|
| 433 |
+
" inbrowser=False,\n",
|
| 434 |
+
" server_name=\"127.0.0.1\",\n",
|
| 435 |
+
" server_port=7866,\n",
|
| 436 |
+
" mcp_server=True,\n",
|
| 437 |
+
")"
|
| 438 |
+
],
|
| 439 |
+
"execution_count": 69,
|
| 440 |
+
"outputs": [
|
| 441 |
+
{
|
| 442 |
+
"output_type": "stream",
|
| 443 |
+
"text": [
|
| 444 |
+
"* Running on local URL: http://127.0.0.1:7866\n",
|
| 445 |
+
"* To create a public link, set `share=True` in `launch()`.\n"
|
| 446 |
+
]
|
| 447 |
+
},
|
| 448 |
+
{
|
| 449 |
+
"output_type": "display_data",
|
| 450 |
+
"data": {
|
| 451 |
+
"text/html": [
|
| 452 |
+
"<div><iframe src=\"http://127.0.0.1:7866/\" width=\"100%\" height=\"500\" allow=\"autoplay; camera; microphone; clipboard-read; clipboard-write;\" frameborder=\"0\" allowfullscreen></iframe></div>"
|
| 453 |
+
],
|
| 454 |
+
"text/plain": [
|
| 455 |
+
"<IPython.core.display.HTML object>"
|
| 456 |
+
]
|
| 457 |
+
}
|
| 458 |
+
},
|
| 459 |
+
{
|
| 460 |
+
"output_type": "execute_result",
|
| 461 |
+
"data": {
|
| 462 |
+
"text/plain": []
|
| 463 |
+
}
|
| 464 |
+
},
|
| 465 |
+
{
|
| 466 |
+
"output_type": "stream",
|
| 467 |
+
"text": [
|
| 468 |
+
"Traceback (most recent call last):\n",
|
| 469 |
+
" File \"c:\\Users\\Computer\\Documents\\AI_Engineering\\ai_env\\Lib\\site-packages\\gradio\\queueing.py\", line 766, in process_events\n",
|
| 470 |
+
" response = await route_utils.call_process_api(\n",
|
| 471 |
+
" ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^\n",
|
| 472 |
+
" ...<5 lines>...\n",
|
| 473 |
+
" )\n",
|
| 474 |
+
" ^\n",
|
| 475 |
+
" File \"c:\\Users\\Computer\\Documents\\AI_Engineering\\ai_env\\Lib\\site-packages\\gradio\\route_utils.py\", line 355, in call_process_api\n",
|
| 476 |
+
" output = await app.get_blocks().process_api(\n",
|
| 477 |
+
" ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^\n",
|
| 478 |
+
" ...<11 lines>...\n",
|
| 479 |
+
" )\n",
|
| 480 |
+
" ^\n",
|
| 481 |
+
" File \"c:\\Users\\Computer\\Documents\\AI_Engineering\\ai_env\\Lib\\site-packages\\gradio\\blocks.py\", line 2158, in process_api\n",
|
| 482 |
+
" result = await self.call_function(\n",
|
| 483 |
+
" ^^^^^^^^^^^^^^^^^^^^^^^^^\n",
|
| 484 |
+
" ...<8 lines>...\n",
|
| 485 |
+
" )\n",
|
| 486 |
+
" ^\n",
|
| 487 |
+
" File \"c:\\Users\\Computer\\Documents\\AI_Engineering\\ai_env\\Lib\\site-packages\\gradio\\blocks.py\", line 1632, in call_function\n",
|
| 488 |
+
" prediction = await fn(*processed_input)\n",
|
| 489 |
+
" ^^^^^^^^^^^^^^^^^^^^^^^^^^\n",
|
| 490 |
+
" File \"c:\\Users\\Computer\\Documents\\AI_Engineering\\ai_env\\Lib\\site-packages\\gradio\\utils.py\", line 1007, in async_wrapper\n",
|
| 491 |
+
" response = await f(*args, **kwargs)\n",
|
| 492 |
+
" ^^^^^^^^^^^^^^^^^^^^^^^^\n",
|
| 493 |
+
" File \"c:\\Users\\Computer\\Documents\\AI_Engineering\\ai_env\\Lib\\site-packages\\gradio\\chat_interface.py\", line 544, in __wrapper\n",
|
| 494 |
+
" return await submit_fn(*args, **kwargs)\n",
|
| 495 |
+
" ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^\n",
|
| 496 |
+
" File \"c:\\Users\\Computer\\Documents\\AI_Engineering\\ai_env\\Lib\\site-packages\\gradio\\chat_interface.py\", line 921, in _submit_fn\n",
|
| 497 |
+
" response = await run_sync(self.fn, *inputs, limiter=self.limiter) # type: ignore\n",
|
| 498 |
+
" ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^\n",
|
| 499 |
+
" File \"c:\\Users\\Computer\\Documents\\AI_Engineering\\ai_env\\Lib\\site-packages\\anyio\\to_thread.py\", line 63, in run_sync\n",
|
| 500 |
+
" return await get_async_backend().run_sync_in_worker_thread(\n",
|
| 501 |
+
" ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^\n",
|
| 502 |
+
" func, args, abandon_on_cancel=abandon_on_cancel, limiter=limiter\n",
|
| 503 |
+
" ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^\n",
|
| 504 |
+
" )\n",
|
| 505 |
+
" ^\n",
|
| 506 |
+
" File \"c:\\Users\\Computer\\Documents\\AI_Engineering\\ai_env\\Lib\\site-packages\\anyio\\_backends\\_asyncio.py\", line 2502, in run_sync_in_worker_thread\n",
|
| 507 |
+
" return await future\n",
|
| 508 |
+
" ^^^^^^^^^^^^\n",
|
| 509 |
+
" File \"c:\\Users\\Computer\\Documents\\AI_Engineering\\ai_env\\Lib\\site-packages\\anyio\\_backends\\_asyncio.py\", line 986, in run\n",
|
| 510 |
+
" result = context.run(func, *args)\n",
|
| 511 |
+
" File \"C:\\Users\\Computer\\AppData\\Local\\Temp\\ipykernel_7860\\1202166343.py\", line 18, in respond_ai\n",
|
| 512 |
+
" if message.tool_calls:\n",
|
| 513 |
+
" ^^^^^^^^^^^^^^^^^^\n",
|
| 514 |
+
"AttributeError: 'str' object has no attribute 'tool_calls'\n"
|
| 515 |
+
]
|
| 516 |
+
}
|
| 517 |
+
],
|
| 518 |
+
"id": "891fadee"
|
| 519 |
+
}
|
| 520 |
+
],
|
| 521 |
+
"metadata": {
|
| 522 |
+
"kernelspec": {
|
| 523 |
+
"display_name": "ai_env (3.14.3)",
|
| 524 |
+
"language": "python",
|
| 525 |
+
"name": "python3"
|
| 526 |
+
},
|
| 527 |
+
"language_info": {
|
| 528 |
+
"codemirror_mode": {
|
| 529 |
+
"name": "ipython",
|
| 530 |
+
"version": 3
|
| 531 |
+
},
|
| 532 |
+
"file_extension": ".py",
|
| 533 |
+
"mimetype": "text/x-python",
|
| 534 |
+
"name": "python",
|
| 535 |
+
"nbconvert_exporter": "python",
|
| 536 |
+
"pygments_lexer": "ipython3",
|
| 537 |
+
"version": "3.14.3"
|
| 538 |
+
}
|
| 539 |
+
},
|
| 540 |
+
"nbformat": 4,
|
| 541 |
+
"nbformat_minor": 5
|
| 542 |
+
}
|
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
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
{
|
| 2 |
+
"cells": [
|
| 3 |
+
{
|
| 4 |
+
"cell_type": "markdown",
|
| 5 |
+
"id": "965d0bec",
|
| 6 |
+
"metadata": {},
|
| 7 |
+
"source": [
|
| 8 |
+
"### Step 1: Install Open API and Libraries"
|
| 9 |
+
]
|
| 10 |
+
},
|
| 11 |
+
{
|
| 12 |
+
"cell_type": "code",
|
| 13 |
+
"execution_count": 6,
|
| 14 |
+
"id": "8e0e9f58",
|
| 15 |
+
"metadata": {},
|
| 16 |
+
"outputs": [],
|
| 17 |
+
"source": [
|
| 18 |
+
"import os\n",
|
| 19 |
+
"from openai import OpenAI\n",
|
| 20 |
+
"from dotenv import load_dotenv\n",
|
| 21 |
+
"from IPython.display import Markdown, display\n",
|
| 22 |
+
"import gradio as gr\n",
|
| 23 |
+
"\n",
|
| 24 |
+
"load_dotenv()\n",
|
| 25 |
+
"\n",
|
| 26 |
+
"OPENAI_API_KEY = os.getenv(\"OPENAI_API_KEY\")\n",
|
| 27 |
+
"\n",
|
| 28 |
+
"if OPENAI_API_KEY is None:\n",
|
| 29 |
+
" raise Exception(\"API key is missing\")\n"
|
| 30 |
+
]
|
| 31 |
+
},
|
| 32 |
+
{
|
| 33 |
+
"cell_type": "markdown",
|
| 34 |
+
"id": "84fd764f",
|
| 35 |
+
"metadata": {},
|
| 36 |
+
"source": [
|
| 37 |
+
"### Step 2: Install Lite LLM\n"
|
| 38 |
+
]
|
| 39 |
+
},
|
| 40 |
+
{
|
| 41 |
+
"cell_type": "code",
|
| 42 |
+
"execution_count": 7,
|
| 43 |
+
"id": "64bef102",
|
| 44 |
+
"metadata": {},
|
| 45 |
+
"outputs": [
|
| 46 |
+
{
|
| 47 |
+
"data": {
|
| 48 |
+
"text/markdown": [
|
| 49 |
+
"Surfers Paradise is a famous beachside suburb on the Gold Coast in southeastern Queensland, Australia. It sits on the Pacific Ocean coast, about 75–80 km (roughly 50 miles) south of Brisbane, between Main Beach to the north and Broadbeach to the south. If you’d like, I can give you directions or a map link."
|
| 50 |
+
],
|
| 51 |
+
"text/plain": [
|
| 52 |
+
"<IPython.core.display.Markdown object>"
|
| 53 |
+
]
|
| 54 |
+
},
|
| 55 |
+
"metadata": {},
|
| 56 |
+
"output_type": "display_data"
|
| 57 |
+
}
|
| 58 |
+
],
|
| 59 |
+
"source": [
|
| 60 |
+
"from litellm import completion\n",
|
| 61 |
+
"\n",
|
| 62 |
+
"messages = [{\"role\": \"user\", \"content\": \"Where is Surfers Paradise?\"}]\n",
|
| 63 |
+
"\n",
|
| 64 |
+
"response = completion(\n",
|
| 65 |
+
" model=\"openai/gpt-5-nano\",\n",
|
| 66 |
+
" messages=messages\n",
|
| 67 |
+
")\n",
|
| 68 |
+
"\n",
|
| 69 |
+
"reply = response.choices[0].message.content\n",
|
| 70 |
+
"display(Markdown(reply))"
|
| 71 |
+
]
|
| 72 |
+
},
|
| 73 |
+
{
|
| 74 |
+
"cell_type": "markdown",
|
| 75 |
+
"id": "389ce83f",
|
| 76 |
+
"metadata": {},
|
| 77 |
+
"source": [
|
| 78 |
+
"### Step 3: :LLM Call Info"
|
| 79 |
+
]
|
| 80 |
+
},
|
| 81 |
+
{
|
| 82 |
+
"cell_type": "code",
|
| 83 |
+
"execution_count": 8,
|
| 84 |
+
"id": "60752044",
|
| 85 |
+
"metadata": {},
|
| 86 |
+
"outputs": [
|
| 87 |
+
{
|
| 88 |
+
"name": "stdout",
|
| 89 |
+
"output_type": "stream",
|
| 90 |
+
"text": [
|
| 91 |
+
"{\n",
|
| 92 |
+
" \"id\": \"chatcmpl-DPbKPvYZzoEtCgsDIKk86T0NM94uP\",\n",
|
| 93 |
+
" \"created\": 1774994061,\n",
|
| 94 |
+
" \"model\": \"gpt-5-nano-2025-08-07\",\n",
|
| 95 |
+
" \"object\": \"chat.completion\",\n",
|
| 96 |
+
" \"system_fingerprint\": null,\n",
|
| 97 |
+
" \"choices\": [\n",
|
| 98 |
+
" {\n",
|
| 99 |
+
" \"finish_reason\": \"stop\",\n",
|
| 100 |
+
" \"index\": 0,\n",
|
| 101 |
+
" \"message\": {\n",
|
| 102 |
+
" \"content\": \"Surfers Paradise is a famous beachside suburb on the Gold Coast in southeastern Queensland, Australia. It sits on the Pacific Ocean coast, about 75\\u201380 km (roughly 50 miles) south of Brisbane, between Main Beach to the north and Broadbeach to the south. If you\\u2019d like, I can give you directions or a map link.\",\n",
|
| 103 |
+
" \"role\": \"assistant\",\n",
|
| 104 |
+
" \"tool_calls\": null,\n",
|
| 105 |
+
" \"function_call\": null,\n",
|
| 106 |
+
" \"provider_specific_fields\": {\n",
|
| 107 |
+
" \"refusal\": null\n",
|
| 108 |
+
" },\n",
|
| 109 |
+
" \"annotations\": []\n",
|
| 110 |
+
" },\n",
|
| 111 |
+
" \"provider_specific_fields\": {}\n",
|
| 112 |
+
" }\n",
|
| 113 |
+
" ],\n",
|
| 114 |
+
" \"usage\": {\n",
|
| 115 |
+
" \"completion_tokens\": 978,\n",
|
| 116 |
+
" \"prompt_tokens\": 12,\n",
|
| 117 |
+
" \"total_tokens\": 990,\n",
|
| 118 |
+
" \"completion_tokens_details\": {\n",
|
| 119 |
+
" \"accepted_prediction_tokens\": 0,\n",
|
| 120 |
+
" \"audio_tokens\": 0,\n",
|
| 121 |
+
" \"reasoning_tokens\": 896,\n",
|
| 122 |
+
" \"rejected_prediction_tokens\": 0\n",
|
| 123 |
+
" },\n",
|
| 124 |
+
" \"prompt_tokens_details\": {\n",
|
| 125 |
+
" \"audio_tokens\": 0,\n",
|
| 126 |
+
" \"cached_tokens\": 0\n",
|
| 127 |
+
" }\n",
|
| 128 |
+
" },\n",
|
| 129 |
+
" \"service_tier\": \"default\"\n",
|
| 130 |
+
"}\n"
|
| 131 |
+
]
|
| 132 |
+
}
|
| 133 |
+
],
|
| 134 |
+
"source": [
|
| 135 |
+
"import json\n",
|
| 136 |
+
"\n",
|
| 137 |
+
"print(json.dumps(response.model_dump(), indent=2))\n"
|
| 138 |
+
]
|
| 139 |
+
},
|
| 140 |
+
{
|
| 141 |
+
"cell_type": "code",
|
| 142 |
+
"execution_count": 9,
|
| 143 |
+
"id": "40d8624f",
|
| 144 |
+
"metadata": {},
|
| 145 |
+
"outputs": [
|
| 146 |
+
{
|
| 147 |
+
"name": "stdout",
|
| 148 |
+
"output_type": "stream",
|
| 149 |
+
"text": [
|
| 150 |
+
"prompt tokens: 12\n",
|
| 151 |
+
"completion tokens: 978\n",
|
| 152 |
+
"total tokens: 990\n"
|
| 153 |
+
]
|
| 154 |
+
}
|
| 155 |
+
],
|
| 156 |
+
"source": [
|
| 157 |
+
"print(f\"prompt tokens: {response.usage.prompt_tokens}\")\n",
|
| 158 |
+
"print(f\"completion tokens: {response.usage.completion_tokens}\")\n",
|
| 159 |
+
"print(f\"total tokens: {response.usage.total_tokens}\")"
|
| 160 |
+
]
|
| 161 |
+
},
|
| 162 |
+
{
|
| 163 |
+
"cell_type": "code",
|
| 164 |
+
"execution_count": 10,
|
| 165 |
+
"id": "301998cd",
|
| 166 |
+
"metadata": {},
|
| 167 |
+
"outputs": [
|
| 168 |
+
{
|
| 169 |
+
"name": "stdout",
|
| 170 |
+
"output_type": "stream",
|
| 171 |
+
"text": [
|
| 172 |
+
"{\n",
|
| 173 |
+
" \"custom_llm_provider\": \"openai\",\n",
|
| 174 |
+
" \"region_name\": null,\n",
|
| 175 |
+
" \"additional_headers\": {\n",
|
| 176 |
+
" \"x-ratelimit-limit-requests\": \"500\",\n",
|
| 177 |
+
" \"x-ratelimit-remaining-requests\": \"499\",\n",
|
| 178 |
+
" \"x-ratelimit-limit-tokens\": \"200000\",\n",
|
| 179 |
+
" \"x-ratelimit-remaining-tokens\": \"199991\",\n",
|
| 180 |
+
" \"llm_provider-date\": \"Tue, 31 Mar 2026 21:54:31 GMT\",\n",
|
| 181 |
+
" \"llm_provider-content-type\": \"application/json\",\n",
|
| 182 |
+
" \"llm_provider-transfer-encoding\": \"chunked\",\n",
|
| 183 |
+
" \"llm_provider-connection\": \"keep-alive\",\n",
|
| 184 |
+
" \"llm_provider-cf-ray\": \"9e52978e1fb5000b-ORD\",\n",
|
| 185 |
+
" \"llm_provider-cf-cache-status\": \"DYNAMIC\",\n",
|
| 186 |
+
" \"llm_provider-server\": \"cloudflare\",\n",
|
| 187 |
+
" \"llm_provider-strict-transport-security\": \"max-age=31536000; includeSubDomains; preload\",\n",
|
| 188 |
+
" \"llm_provider-x-content-type-options\": \"nosniff\",\n",
|
| 189 |
+
" \"llm_provider-access-control-expose-headers\": \"X-Request-ID\",\n",
|
| 190 |
+
" \"llm_provider-openai-organization\": \"user-gd9kerc69txct0aacl4jp9bp\",\n",
|
| 191 |
+
" \"llm_provider-openai-processing-ms\": \"9769\",\n",
|
| 192 |
+
" \"llm_provider-openai-project\": \"proj_tHEYdOvUngAS7SiDu14lU7Qe\",\n",
|
| 193 |
+
" \"llm_provider-openai-version\": \"2020-10-01\",\n",
|
| 194 |
+
" \"llm_provider-x-openai-proxy-wasm\": \"v0.1\",\n",
|
| 195 |
+
" \"llm_provider-x-ratelimit-limit-requests\": \"500\",\n",
|
| 196 |
+
" \"llm_provider-x-ratelimit-limit-tokens\": \"200000\",\n",
|
| 197 |
+
" \"llm_provider-x-ratelimit-remaining-requests\": \"499\",\n",
|
| 198 |
+
" \"llm_provider-x-ratelimit-remaining-tokens\": \"199991\",\n",
|
| 199 |
+
" \"llm_provider-x-ratelimit-reset-requests\": \"120ms\",\n",
|
| 200 |
+
" \"llm_provider-x-ratelimit-reset-tokens\": \"2ms\",\n",
|
| 201 |
+
" \"llm_provider-x-request-id\": \"req_1a7c7b4cb07d4105b8c977b2a90269f6\",\n",
|
| 202 |
+
" \"llm_provider-set-cookie\": \"__cf_bm=qydpBXfiEQ_vKy..sxMHrjMvQe7s6nJVRKBWd0kqi1c-1774994060.5005593-1.0.1.1-Vg981baq1MJMFzDIMocA09Uw1lgS5zJaTuwyiudERh3O_B7qaynGrilGqXxwOm3yXlsbqFPjFkeCiD.cOsoAvWqSFa6aPIIWDeJ_BKst7yUP67016n9TDQ537XhiVI8d; HttpOnly; Secure; Path=/; Domain=api.openai.com; Expires=Tue, 31 Mar 2026 22:24:31 GMT\",\n",
|
| 203 |
+
" \"llm_provider-content-encoding\": \"br\",\n",
|
| 204 |
+
" \"llm_provider-alt-svc\": \"h3=\\\":443\\\"; ma=86400\"\n",
|
| 205 |
+
" },\n",
|
| 206 |
+
" \"optional_params\": {\n",
|
| 207 |
+
" \"extra_body\": {}\n",
|
| 208 |
+
" },\n",
|
| 209 |
+
" \"litellm_call_id\": \"28053277-0aaa-47ed-bcdd-1ea9f1345456\",\n",
|
| 210 |
+
" \"api_base\": \"https://api.openai.com\",\n",
|
| 211 |
+
" \"model_id\": null,\n",
|
| 212 |
+
" \"response_cost\": 0.0003918,\n",
|
| 213 |
+
" \"litellm_model_name\": \"openai/gpt-5-nano\",\n",
|
| 214 |
+
" \"_response_ms\": 10752.33,\n",
|
| 215 |
+
" \"litellm_overhead_time_ms\": 13.519,\n",
|
| 216 |
+
" \"callback_duration_ms\": 0.0\n",
|
| 217 |
+
"}\n"
|
| 218 |
+
]
|
| 219 |
+
}
|
| 220 |
+
],
|
| 221 |
+
"source": [
|
| 222 |
+
"import json\n",
|
| 223 |
+
"\n",
|
| 224 |
+
"print(json.dumps(response._hidden_params, indent=2))"
|
| 225 |
+
]
|
| 226 |
+
},
|
| 227 |
+
{
|
| 228 |
+
"cell_type": "code",
|
| 229 |
+
"execution_count": 11,
|
| 230 |
+
"id": "01b8a46b",
|
| 231 |
+
"metadata": {},
|
| 232 |
+
"outputs": [
|
| 233 |
+
{
|
| 234 |
+
"name": "stdout",
|
| 235 |
+
"output_type": "stream",
|
| 236 |
+
"text": [
|
| 237 |
+
"cost: $0.000392\n"
|
| 238 |
+
]
|
| 239 |
+
}
|
| 240 |
+
],
|
| 241 |
+
"source": [
|
| 242 |
+
"print(f\"cost: ${response._hidden_params['response_cost']:.6f}\")"
|
| 243 |
+
]
|
| 244 |
+
}
|
| 245 |
+
],
|
| 246 |
+
"metadata": {
|
| 247 |
+
"kernelspec": {
|
| 248 |
+
"display_name": "ai_env (3.14.3)",
|
| 249 |
+
"language": "python",
|
| 250 |
+
"name": "python3"
|
| 251 |
+
},
|
| 252 |
+
"language_info": {
|
| 253 |
+
"codemirror_mode": {
|
| 254 |
+
"name": "ipython",
|
| 255 |
+
"version": 3
|
| 256 |
+
},
|
| 257 |
+
"file_extension": ".py",
|
| 258 |
+
"mimetype": "text/x-python",
|
| 259 |
+
"name": "python",
|
| 260 |
+
"nbconvert_exporter": "python",
|
| 261 |
+
"pygments_lexer": "ipython3",
|
| 262 |
+
"version": "3.14.3"
|
| 263 |
+
}
|
| 264 |
+
},
|
| 265 |
+
"nbformat": 4,
|
| 266 |
+
"nbformat_minor": 5
|
| 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|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
{
|
| 2 |
+
"cells": [
|
| 3 |
+
{
|
| 4 |
+
"cell_type": "markdown",
|
| 5 |
+
"id": "cbd5ba1e",
|
| 6 |
+
"metadata": {},
|
| 7 |
+
"source": [
|
| 8 |
+
"### Step 1: Import Libraries and load the environment variables"
|
| 9 |
+
]
|
| 10 |
+
},
|
| 11 |
+
{
|
| 12 |
+
"cell_type": "code",
|
| 13 |
+
"execution_count": 1,
|
| 14 |
+
"id": "194959d3",
|
| 15 |
+
"metadata": {},
|
| 16 |
+
"outputs": [
|
| 17 |
+
{
|
| 18 |
+
"name": "stdout",
|
| 19 |
+
"output_type": "stream",
|
| 20 |
+
"text": [
|
| 21 |
+
"sk-proj-\n"
|
| 22 |
+
]
|
| 23 |
+
}
|
| 24 |
+
],
|
| 25 |
+
"source": [
|
| 26 |
+
"import os \n",
|
| 27 |
+
"from openai import OpenAI \n",
|
| 28 |
+
"from dotenv import load_dotenv\n",
|
| 29 |
+
"from IPython.display import Markdown , display\n",
|
| 30 |
+
"\n",
|
| 31 |
+
"load_dotenv()\n",
|
| 32 |
+
"\n",
|
| 33 |
+
"OPENAI_API_KEY = os.getenv(\"OPENAI_API_KEY\") \n",
|
| 34 |
+
"\n",
|
| 35 |
+
"if OPENAI_API_KEY is None:\n",
|
| 36 |
+
" raise Exception (\"API Key is missing\")\n",
|
| 37 |
+
" \n",
|
| 38 |
+
"else:\n",
|
| 39 |
+
" print(OPENAI_API_KEY[:8])\n",
|
| 40 |
+
"\n",
|
| 41 |
+
"client= OpenAI()"
|
| 42 |
+
]
|
| 43 |
+
},
|
| 44 |
+
{
|
| 45 |
+
"cell_type": "markdown",
|
| 46 |
+
"id": "9d50b930",
|
| 47 |
+
"metadata": {},
|
| 48 |
+
"source": [
|
| 49 |
+
"### Step 2: Call the API\n"
|
| 50 |
+
]
|
| 51 |
+
},
|
| 52 |
+
{
|
| 53 |
+
"cell_type": "code",
|
| 54 |
+
"execution_count": 4,
|
| 55 |
+
"id": "37d18ade",
|
| 56 |
+
"metadata": {},
|
| 57 |
+
"outputs": [
|
| 58 |
+
{
|
| 59 |
+
"data": {
|
| 60 |
+
"text/markdown": [
|
| 61 |
+
"That sounds exciting! Is this your first time visiting New York? Do you need any recommendations for things to do, places to eat, or tips for your trip?"
|
| 62 |
+
],
|
| 63 |
+
"text/plain": [
|
| 64 |
+
"<IPython.core.display.Markdown object>"
|
| 65 |
+
]
|
| 66 |
+
},
|
| 67 |
+
"metadata": {},
|
| 68 |
+
"output_type": "display_data"
|
| 69 |
+
}
|
| 70 |
+
],
|
| 71 |
+
"source": [
|
| 72 |
+
"response = client.chat.completions.create(\n",
|
| 73 |
+
" model=\"gpt-4.1-mini\",\n",
|
| 74 |
+
" messages=[\n",
|
| 75 |
+
" {\"role\":\"system\", \"content\": \"You are a helpful assistant.\"}, \n",
|
| 76 |
+
" {\"role\": \"user\", \"content\": \"Next week I am traveling to New York. \"}]\n",
|
| 77 |
+
")\n",
|
| 78 |
+
"reply=response.choices[0].message.content\n",
|
| 79 |
+
"#print(reply)\n",
|
| 80 |
+
"display(Markdown(reply))"
|
| 81 |
+
]
|
| 82 |
+
},
|
| 83 |
+
{
|
| 84 |
+
"cell_type": "code",
|
| 85 |
+
"execution_count": 3,
|
| 86 |
+
"id": "43f97767",
|
| 87 |
+
"metadata": {},
|
| 88 |
+
"outputs": [
|
| 89 |
+
{
|
| 90 |
+
"data": {
|
| 91 |
+
"text/markdown": [
|
| 92 |
+
"I’m not sure where you’re traveling next week. Could you please provide more details?"
|
| 93 |
+
],
|
| 94 |
+
"text/plain": [
|
| 95 |
+
"<IPython.core.display.Markdown object>"
|
| 96 |
+
]
|
| 97 |
+
},
|
| 98 |
+
"metadata": {},
|
| 99 |
+
"output_type": "display_data"
|
| 100 |
+
}
|
| 101 |
+
],
|
| 102 |
+
"source": [
|
| 103 |
+
"response = client.chat.completions.create(\n",
|
| 104 |
+
" model=\"gpt-4.1-mini\",\n",
|
| 105 |
+
" messages=[\n",
|
| 106 |
+
" {\"role\":\"system\", \"content\": \"You are a helpful assistant.\"}, \n",
|
| 107 |
+
" {\"role\": \"user\", \"content\": \"Where am I traveling to next week?\"}]\n",
|
| 108 |
+
")\n",
|
| 109 |
+
"reply=response.choices[0].message.content\n",
|
| 110 |
+
"#print(reply)\n",
|
| 111 |
+
"display(Markdown(reply))"
|
| 112 |
+
]
|
| 113 |
+
},
|
| 114 |
+
{
|
| 115 |
+
"cell_type": "markdown",
|
| 116 |
+
"id": "f86a2f48",
|
| 117 |
+
"metadata": {},
|
| 118 |
+
"source": [
|
| 119 |
+
"### In theory...... doing it manually"
|
| 120 |
+
]
|
| 121 |
+
},
|
| 122 |
+
{
|
| 123 |
+
"cell_type": "code",
|
| 124 |
+
"execution_count": 10,
|
| 125 |
+
"id": "8060e528",
|
| 126 |
+
"metadata": {},
|
| 127 |
+
"outputs": [
|
| 128 |
+
{
|
| 129 |
+
"data": {
|
| 130 |
+
"text/markdown": [
|
| 131 |
+
"You mentioned that you are traveling to New York next week. Would you like some restaurant recommendations there?"
|
| 132 |
+
],
|
| 133 |
+
"text/plain": [
|
| 134 |
+
"<IPython.core.display.Markdown object>"
|
| 135 |
+
]
|
| 136 |
+
},
|
| 137 |
+
"metadata": {},
|
| 138 |
+
"output_type": "display_data"
|
| 139 |
+
}
|
| 140 |
+
],
|
| 141 |
+
"source": [
|
| 142 |
+
"response = client.chat.completions.create(\n",
|
| 143 |
+
" model=\"gpt-4.1-mini\",\n",
|
| 144 |
+
" messages=[\n",
|
| 145 |
+
" {\"role\":\"system\", \"content\": \"You are a helpful assistant.\"}, \n",
|
| 146 |
+
" {\"role\": \"user\", \"content\": \"Next week I am traveling to New York. Can you suggest some good restaurants to eat at?\"},\n",
|
| 147 |
+
" {\"role\": \"assistant\", \"content\": \"That sounds exciting! Do you need any tips?\"} ,\n",
|
| 148 |
+
" {\"role\":\"user\", \"content\": \"Where am I traveling to next week?\"}]\n",
|
| 149 |
+
")\n",
|
| 150 |
+
"reply=response.choices[0].message.content\n",
|
| 151 |
+
"#print(reply)\n",
|
| 152 |
+
"display(Markdown(reply))\n"
|
| 153 |
+
]
|
| 154 |
+
},
|
| 155 |
+
{
|
| 156 |
+
"cell_type": "markdown",
|
| 157 |
+
"id": "bd6119a6",
|
| 158 |
+
"metadata": {},
|
| 159 |
+
"source": [
|
| 160 |
+
"### Now not manually"
|
| 161 |
+
]
|
| 162 |
+
},
|
| 163 |
+
{
|
| 164 |
+
"cell_type": "code",
|
| 165 |
+
"execution_count": null,
|
| 166 |
+
"id": "ea0769b4",
|
| 167 |
+
"metadata": {},
|
| 168 |
+
"outputs": [
|
| 169 |
+
{
|
| 170 |
+
"name": "stdout",
|
| 171 |
+
"output_type": "stream",
|
| 172 |
+
"text": [
|
| 173 |
+
"[{'role': 'system', 'content': 'You are a helpful assistant.'}, {'role': 'user', 'content': 'Next week I am traveling to New York. '}, {'role': 'assistant', 'content': \"That sounds exciting! Is there anything specific you'd like to know or need help with for your trip to New York? For example, recommendations on places to visit, restaurants, transportation tips, or packing advice?\"}]\n"
|
| 174 |
+
]
|
| 175 |
+
}
|
| 176 |
+
],
|
| 177 |
+
"source": [
|
| 178 |
+
"messages=[\n",
|
| 179 |
+
" {\"role\":\"system\", \"content\": \"You are a helpful assistant.\"}, \n",
|
| 180 |
+
" {\"role\": \"user\", \"content\": \"Next week I am traveling to New York. \"}]\n",
|
| 181 |
+
"\n",
|
| 182 |
+
"#display(Markdownresponse = client.chat.completions.create(\n",
|
| 183 |
+
" model=\"gpt-4.1-mini\",\n",
|
| 184 |
+
" messages=messages\n",
|
| 185 |
+
" \n",
|
| 186 |
+
")\n",
|
| 187 |
+
"reply=response.choices[0].message.content\n",
|
| 188 |
+
"messages.append({\"role\": \"assistant\", \"content\": reply})\n",
|
| 189 |
+
"\n",
|
| 190 |
+
"print(messages)(reply))"
|
| 191 |
+
]
|
| 192 |
+
},
|
| 193 |
+
{
|
| 194 |
+
"cell_type": "code",
|
| 195 |
+
"execution_count": 13,
|
| 196 |
+
"id": "31afab14",
|
| 197 |
+
"metadata": {},
|
| 198 |
+
"outputs": [
|
| 199 |
+
{
|
| 200 |
+
"name": "stdout",
|
| 201 |
+
"output_type": "stream",
|
| 202 |
+
"text": [
|
| 203 |
+
"[{'content': 'You are a helpful assistant.', 'role': 'system'},\n",
|
| 204 |
+
" {'content': 'Next week I am traveling to New York. ', 'role': 'user'},\n",
|
| 205 |
+
" {'content': \"That sounds exciting! Is there anything specific you'd like to \"\n",
|
| 206 |
+
" 'know or need help with for your trip to New York? For example, '\n",
|
| 207 |
+
" 'recommendations on places to visit, restaurants, transportation '\n",
|
| 208 |
+
" 'tips, or packing advice?',\n",
|
| 209 |
+
" 'role': 'assistant'}]\n"
|
| 210 |
+
]
|
| 211 |
+
}
|
| 212 |
+
],
|
| 213 |
+
"source": [
|
| 214 |
+
"from pprint import pprint\n",
|
| 215 |
+
"pprint(messages)"
|
| 216 |
+
]
|
| 217 |
+
},
|
| 218 |
+
{
|
| 219 |
+
"cell_type": "code",
|
| 220 |
+
"execution_count": 14,
|
| 221 |
+
"id": "e5b04ada",
|
| 222 |
+
"metadata": {},
|
| 223 |
+
"outputs": [
|
| 224 |
+
{
|
| 225 |
+
"name": "stdout",
|
| 226 |
+
"output_type": "stream",
|
| 227 |
+
"text": [
|
| 228 |
+
"[{'content': 'You are a helpful assistant.', 'role': 'system'},\n",
|
| 229 |
+
" {'content': 'Next week I am traveling to New York. ', 'role': 'user'},\n",
|
| 230 |
+
" {'content': \"That sounds exciting! Is there anything specific you'd like to \"\n",
|
| 231 |
+
" 'know or need help with for your trip to New York? For example, '\n",
|
| 232 |
+
" 'recommendations on places to visit, restaurants, transportation '\n",
|
| 233 |
+
" 'tips, or packing advice?',\n",
|
| 234 |
+
" 'role': 'assistant'},\n",
|
| 235 |
+
" {'content': 'Where am I traveling to next week?', 'role': 'user'}]\n"
|
| 236 |
+
]
|
| 237 |
+
}
|
| 238 |
+
],
|
| 239 |
+
"source": [
|
| 240 |
+
"messages.append({\"role\": \"user\", \"content\": \"Where am I traveling to next week?\"})\n",
|
| 241 |
+
"pprint(messages)"
|
| 242 |
+
]
|
| 243 |
+
},
|
| 244 |
+
{
|
| 245 |
+
"cell_type": "code",
|
| 246 |
+
"execution_count": 15,
|
| 247 |
+
"id": "49825fcb",
|
| 248 |
+
"metadata": {},
|
| 249 |
+
"outputs": [
|
| 250 |
+
{
|
| 251 |
+
"name": "stdout",
|
| 252 |
+
"output_type": "stream",
|
| 253 |
+
"text": [
|
| 254 |
+
"[{'content': 'You are a helpful assistant.', 'role': 'system'},\n",
|
| 255 |
+
" {'content': 'Next week I am traveling to New York. ', 'role': 'user'},\n",
|
| 256 |
+
" {'content': \"That sounds exciting! Is there anything specific you'd like to \"\n",
|
| 257 |
+
" 'know or need help with for your trip to New York? For example, '\n",
|
| 258 |
+
" 'recommendations on places to visit, restaurants, transportation '\n",
|
| 259 |
+
" 'tips, or packing advice?',\n",
|
| 260 |
+
" 'role': 'assistant'},\n",
|
| 261 |
+
" {'content': 'Where am I traveling to next week?', 'role': 'user'},\n",
|
| 262 |
+
" {'content': 'You are traveling to New York next week.', 'role': 'assistant'}]\n"
|
| 263 |
+
]
|
| 264 |
+
}
|
| 265 |
+
],
|
| 266 |
+
"source": [
|
| 267 |
+
"response = client.chat.completions.create(\n",
|
| 268 |
+
" model=\"gpt-4.1-mini\",\n",
|
| 269 |
+
" messages=messages\n",
|
| 270 |
+
" \n",
|
| 271 |
+
")\n",
|
| 272 |
+
"reply=response.choices[0].message.content\n",
|
| 273 |
+
"messages.append({\"role\": \"assistant\", \"content\": reply})\n",
|
| 274 |
+
"\n",
|
| 275 |
+
"pprint(messages)"
|
| 276 |
+
]
|
| 277 |
+
},
|
| 278 |
+
{
|
| 279 |
+
"cell_type": "code",
|
| 280 |
+
"execution_count": 16,
|
| 281 |
+
"id": "c549c0f7",
|
| 282 |
+
"metadata": {},
|
| 283 |
+
"outputs": [
|
| 284 |
+
{
|
| 285 |
+
"name": "stdout",
|
| 286 |
+
"output_type": "stream",
|
| 287 |
+
"text": [
|
| 288 |
+
"[{'content': 'You are a helpful assistant.', 'role': 'system'},\n",
|
| 289 |
+
" {'content': 'Next week I am traveling to New York. ', 'role': 'user'},\n",
|
| 290 |
+
" {'content': \"That sounds exciting! Is there anything specific you'd like to \"\n",
|
| 291 |
+
" 'know or need help with for your trip to New York? For example, '\n",
|
| 292 |
+
" 'recommendations on places to visit, restaurants, transportation '\n",
|
| 293 |
+
" 'tips, or packing advice?',\n",
|
| 294 |
+
" 'role': 'assistant'},\n",
|
| 295 |
+
" {'content': 'Where am I traveling to next week?', 'role': 'user'},\n",
|
| 296 |
+
" {'content': 'You are traveling to New York next week.', 'role': 'assistant'},\n",
|
| 297 |
+
" {'content': 'Whats the capital?', 'role': 'user'}]\n"
|
| 298 |
+
]
|
| 299 |
+
}
|
| 300 |
+
],
|
| 301 |
+
"source": [
|
| 302 |
+
"messages.append({\"role\": \"user\", \"content\": \"Whats the capital?\"})\n",
|
| 303 |
+
"pprint(messages)\n"
|
| 304 |
+
]
|
| 305 |
+
},
|
| 306 |
+
{
|
| 307 |
+
"cell_type": "code",
|
| 308 |
+
"execution_count": 17,
|
| 309 |
+
"id": "ec44ae1b",
|
| 310 |
+
"metadata": {},
|
| 311 |
+
"outputs": [
|
| 312 |
+
{
|
| 313 |
+
"name": "stdout",
|
| 314 |
+
"output_type": "stream",
|
| 315 |
+
"text": [
|
| 316 |
+
"[{'content': 'You are a helpful assistant.', 'role': 'system'},\n",
|
| 317 |
+
" {'content': 'Next week I am traveling to New York. ', 'role': 'user'},\n",
|
| 318 |
+
" {'content': \"That sounds exciting! Is there anything specific you'd like to \"\n",
|
| 319 |
+
" 'know or need help with for your trip to New York? For example, '\n",
|
| 320 |
+
" 'recommendations on places to visit, restaurants, transportation '\n",
|
| 321 |
+
" 'tips, or packing advice?',\n",
|
| 322 |
+
" 'role': 'assistant'},\n",
|
| 323 |
+
" {'content': 'Where am I traveling to next week?', 'role': 'user'},\n",
|
| 324 |
+
" {'content': 'You are traveling to New York next week.', 'role': 'assistant'},\n",
|
| 325 |
+
" {'content': 'Whats the capital?', 'role': 'user'},\n",
|
| 326 |
+
" {'content': 'The capital of New York State is Albany.', 'role': 'assistant'}]\n"
|
| 327 |
+
]
|
| 328 |
+
}
|
| 329 |
+
],
|
| 330 |
+
"source": [
|
| 331 |
+
"response = client.chat.completions.create(\n",
|
| 332 |
+
" model=\"gpt-4.1-mini\",\n",
|
| 333 |
+
" messages=messages\n",
|
| 334 |
+
" \n",
|
| 335 |
+
")\n",
|
| 336 |
+
"reply=response.choices[0].message.content\n",
|
| 337 |
+
"messages.append({\"role\": \"assistant\", \"content\": reply})\n",
|
| 338 |
+
"\n",
|
| 339 |
+
"pprint(messages)"
|
| 340 |
+
]
|
| 341 |
+
}
|
| 342 |
+
],
|
| 343 |
+
"metadata": {
|
| 344 |
+
"kernelspec": {
|
| 345 |
+
"display_name": "ai_env (3.14.3)",
|
| 346 |
+
"language": "python",
|
| 347 |
+
"name": "python3"
|
| 348 |
+
},
|
| 349 |
+
"language_info": {
|
| 350 |
+
"codemirror_mode": {
|
| 351 |
+
"name": "ipython",
|
| 352 |
+
"version": 3
|
| 353 |
+
},
|
| 354 |
+
"file_extension": ".py",
|
| 355 |
+
"mimetype": "text/x-python",
|
| 356 |
+
"name": "python",
|
| 357 |
+
"nbconvert_exporter": "python",
|
| 358 |
+
"pygments_lexer": "ipython3",
|
| 359 |
+
"version": "3.14.3"
|
| 360 |
+
}
|
| 361 |
+
},
|
| 362 |
+
"nbformat": 4,
|
| 363 |
+
"nbformat_minor": 5
|
| 364 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|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
{
|
| 2 |
+
"cells": [
|
| 3 |
+
{
|
| 4 |
+
"cell_type": "markdown",
|
| 5 |
+
"metadata": {},
|
| 6 |
+
"source": [
|
| 7 |
+
"\n",
|
| 8 |
+
"### Step 1: Import API and libraries"
|
| 9 |
+
],
|
| 10 |
+
"id": "5eaf10da"
|
| 11 |
+
},
|
| 12 |
+
{
|
| 13 |
+
"cell_type": "code",
|
| 14 |
+
"metadata": {},
|
| 15 |
+
"source": [
|
| 16 |
+
"# Standard library\n",
|
| 17 |
+
"import json\n",
|
| 18 |
+
"import os\n",
|
| 19 |
+
"import sys\n",
|
| 20 |
+
"\n",
|
| 21 |
+
"# Third party\n",
|
| 22 |
+
"import gradio as gr\n",
|
| 23 |
+
"import requests\n",
|
| 24 |
+
"from dotenv import load_dotenv\n",
|
| 25 |
+
"from openai import OpenAI\n",
|
| 26 |
+
"\n",
|
| 27 |
+
"# Notebook helpers (optional)\n",
|
| 28 |
+
"from IPython.display import Markdown, display\n",
|
| 29 |
+
"\n",
|
| 30 |
+
"# Gradio MCP logs may include emoji; UTF-8 avoids Windows console encoding errors.\n",
|
| 31 |
+
"if hasattr(sys.stdout, \"reconfigure\"):\n",
|
| 32 |
+
" sys.stdout.reconfigure(encoding=\"utf-8\")\n",
|
| 33 |
+
"\n",
|
| 34 |
+
"load_dotenv()\n",
|
| 35 |
+
"\n",
|
| 36 |
+
"OPENAI_API_KEY = os.getenv(\"OPENAI_API_KEY\")\n",
|
| 37 |
+
"if not OPENAI_API_KEY:\n",
|
| 38 |
+
" raise RuntimeError(\"OPENAI_API_KEY is missing. Add it to your .env file.\")\n"
|
| 39 |
+
],
|
| 40 |
+
"execution_count": 60,
|
| 41 |
+
"outputs": [],
|
| 42 |
+
"id": "86961e83"
|
| 43 |
+
},
|
| 44 |
+
{
|
| 45 |
+
"cell_type": "markdown",
|
| 46 |
+
"metadata": {},
|
| 47 |
+
"source": [
|
| 48 |
+
"### Step 2: Simple RAG with gaurd rails and Dynamic Context Injection"
|
| 49 |
+
],
|
| 50 |
+
"id": "fd3b5803"
|
| 51 |
+
},
|
| 52 |
+
{
|
| 53 |
+
"cell_type": "code",
|
| 54 |
+
"metadata": {},
|
| 55 |
+
"source": [
|
| 56 |
+
"# Persona + grounded facts for the digital twin (only use information between *** in the prompt).\n",
|
| 57 |
+
"\n",
|
| 58 |
+
"system_message = \"\"\"You are a digital twin of zainab Ahmed sAFEER. when people talk to you, you respond as zainab in first person: using her voice, personality and knowledge. Please use information ONLY between the *** . if you dont know an answer, dont make any assumptions, simply say you dont know. strictly use only this information we provided here. *** Here's information about zainab to help you embody hER: zAINAB is a software engineer in test with a master's degree in computer science engineering WORKING ON TRANSITIOING TO THE AI engineering track. \n",
|
| 59 |
+
"She's based in Chicago USA And she has a master's DEGREE in Information Systems From Robert Morris University Illinois AND Bachelor's degree in computer science engineering Bangalore .. \n",
|
| 60 |
+
"She has a background in QA test automation using AI tools such as cursor and mCP And also manual testing in various backgrounds such as Healthcare, govtech And education. \n",
|
| 61 |
+
"Other career history: SOFTWARE engineer intern AT Vit Bangalore Where SHE was awarded the best engineering project of the Year 2012 AND Where her team created an AUGMENTED Reality based application for the iPad. \n",
|
| 62 |
+
"2015 she worked as a software engineer intern At a dental network at a healthcare Network IN CHICAGO wherein The inventory management SOFTWARE and the website was completely refactored. \n",
|
| 63 |
+
"FROM 2022 to 2024 she worked for ID Tech camps- AN EDUCATIONAL COMPANY OFFERING stem COURSES FOR CHILDREN OF VARIOUS AGES. \n",
|
| 64 |
+
"FROM 2024 to 2026 she worked at a company called Gworks Which created saas CLOUD Software for local governments where she was the lead tester for a high impact utility billing Hub ubhub for local governments Across America. \n",
|
| 65 |
+
"This software was used by over 3 000 cities Across America. \n",
|
| 66 |
+
"WHAT Drives her: the combination of problem solving, impact and continuous learning. \n",
|
| 67 |
+
"I'm motivated by the idea that AI isn't just a technical field It's a way to redesign how people work the more friction from everyday tasks and create systems that think alongside us. \n",
|
| 68 |
+
"I'm also driven by the pace of the field AI engineering requires curiosity adaptability and willingness to experiment and those are the strengths that I naturally lean on . \n",
|
| 69 |
+
"The idea of constantly, learning , refining instructions improving models and designing reliable systems is exciting to me. \n",
|
| 70 |
+
"Communication style: Friendly warm And Simplistic and kind AND engaging and inspiring and motivating. \n",
|
| 71 |
+
"I am a member of women in Tech and love to inspire other women Join the tech Revolution. \n",
|
| 72 |
+
"fUN Facts about me: aCHIEVER OF A PERFECT SCORE AT THE GLOBAL IELTS SPOKEN (ENGLISH) EXAM. PASSED GOOGLE'S TECHNICAL INTERVIEW FOR A TEST ENGINEER ROLE. \n",
|
| 73 |
+
"I'm ALSO a mother of four little ones: one of whom Sports an extra chromosome (DOWN SYNDROME). i AM AN ADVOACATE FOR KIDS WITH DISABILITIES. \n",
|
| 74 |
+
"I'm also a motivational public speaker- Inspiring women To live a life of purpose and FAITH. Spirituality and religion Are my other Hobbies.\n",
|
| 75 |
+
" I'm in the process of writing a book For women I'm the theme of productivity Especially for motherS. *** \"\"\"\n",
|
| 76 |
+
"\n",
|
| 77 |
+
"\n",
|
| 78 |
+
"#gr.ChatInterface(fn=respond_ai).launch(inbrowser=True)\n"
|
| 79 |
+
],
|
| 80 |
+
"execution_count": 61,
|
| 81 |
+
"outputs": [],
|
| 82 |
+
"id": "28e33865"
|
| 83 |
+
},
|
| 84 |
+
{
|
| 85 |
+
"cell_type": "markdown",
|
| 86 |
+
"metadata": {},
|
| 87 |
+
"source": [
|
| 88 |
+
"### Step 5: Dynamic Context Injection"
|
| 89 |
+
],
|
| 90 |
+
"id": "15fdd730"
|
| 91 |
+
},
|
| 92 |
+
{
|
| 93 |
+
"cell_type": "code",
|
| 94 |
+
"metadata": {},
|
| 95 |
+
"source": [
|
| 96 |
+
"# Keyword -> extra snippets merged into the system prompt when the user message contains that keyword.\n",
|
| 97 |
+
"\n",
|
| 98 |
+
"Topic_Context ={\n",
|
| 99 |
+
" \"2001\":\"*** in 2001, Zainab was in 6th grade and was passionate about science and technology. She participated in various science fairs and won several awards for her innovative projects. Her love for technology was evident from a young age, and she often spent hours tinkering with gadgets and learning how they worked. ***\",\n",
|
| 100 |
+
" \"cooking\":\"*** Zainab is a foodie and loves to cook. She enjoys experimenting with different cuisines and flavors in the kitchen. Cooking is not just a hobby for her, but also a way to unwind and express her creativity. She often shares her culinary adventures on social media, inspiring others to try new recipes and explore the world of food. ***\",\n",
|
| 101 |
+
" \"travel\":\"*** Zainab is an avid traveler and loves to explore new places. She believes that traveling broadens the mind and allows her to experience different cultures and perspectives. Whether it's a weekend getaway or an international trip, Zainab is always up for an adventure and enjoys immersing herself in new environments. ***\",\n",
|
| 102 |
+
" \"pizza\":\"*** Zainab has a particular fondness for pizza. She loves trying different toppings and styles of pizza from around the world. Whether it's a classic Margherita or a more adventurous combination, pizza is one of her go-to comfort foods. She often jokes that if she could eat pizza every day, she would be the happiest person alive. ***\"}\n"
|
| 103 |
+
],
|
| 104 |
+
"execution_count": 62,
|
| 105 |
+
"outputs": [],
|
| 106 |
+
"id": "a3afd64e"
|
| 107 |
+
},
|
| 108 |
+
{
|
| 109 |
+
"cell_type": "code",
|
| 110 |
+
"metadata": {},
|
| 111 |
+
"source": [
|
| 112 |
+
"# Pushover: send notifications to your phone (set PUSHOVER_USER and PUSHOVER_TOKEN in .env).\n",
|
| 113 |
+
"\n",
|
| 114 |
+
"pushover_user = os.getenv(\"PUSHOVER_USER\")\n",
|
| 115 |
+
"pushover_token = os.getenv(\"PUSHOVER_TOKEN\")\n",
|
| 116 |
+
"pushover_url = \"https://api.pushover.net/1/messages.json\"\n",
|
| 117 |
+
"\n",
|
| 118 |
+
"\n",
|
| 119 |
+
"def send_notification(message: str) -> str:\n",
|
| 120 |
+
" \"\"\"Post a message to Pushover; returns a short status string for tool results.\"\"\"\n",
|
| 121 |
+
" if not pushover_user or not pushover_token:\n",
|
| 122 |
+
" return \"Pushover credentials missing; skipped notification.\"\n",
|
| 123 |
+
"\n",
|
| 124 |
+
" payload = {\"user\": pushover_user, \"token\": pushover_token, \"message\": message}\n",
|
| 125 |
+
" response = requests.post(pushover_url, data=payload, timeout=15)\n",
|
| 126 |
+
" response.raise_for_status()\n",
|
| 127 |
+
" return \"Notification sent successfully.\"\n"
|
| 128 |
+
],
|
| 129 |
+
"execution_count": 63,
|
| 130 |
+
"outputs": [],
|
| 131 |
+
"id": "fcfd5869"
|
| 132 |
+
},
|
| 133 |
+
{
|
| 134 |
+
"cell_type": "code",
|
| 135 |
+
"metadata": {},
|
| 136 |
+
"source": [
|
| 137 |
+
"# Optional: verify Pushover credentials (comment out if unused).\n",
|
| 138 |
+
"send_notification(\"Hello from the digital twin of Zainab! This is a test notification.\")\n"
|
| 139 |
+
],
|
| 140 |
+
"execution_count": 64,
|
| 141 |
+
"outputs": [
|
| 142 |
+
{
|
| 143 |
+
"output_type": "execute_result",
|
| 144 |
+
"data": {
|
| 145 |
+
"text/plain": [
|
| 146 |
+
"<Response [200]>"
|
| 147 |
+
]
|
| 148 |
+
}
|
| 149 |
+
}
|
| 150 |
+
],
|
| 151 |
+
"id": "408bfabf"
|
| 152 |
+
},
|
| 153 |
+
{
|
| 154 |
+
"cell_type": "code",
|
| 155 |
+
"metadata": {},
|
| 156 |
+
"source": [
|
| 157 |
+
"# OpenAI \"function\" schema: describes send_notification to the model.\n",
|
| 158 |
+
"send_notification_function = {\n",
|
| 159 |
+
" \"name\": \"send_notification\",\n",
|
| 160 |
+
" \"description\": \"Sends a push notification to the phone of real world version of you via Pushover. Use this to alert the user about important events or updates or complete a task that the user has asked you to do. The message parameter should contain the content of the notification you want to send.\",\n",
|
| 161 |
+
" \"parameters\": {\n",
|
| 162 |
+
" \"type\": \"object\",\n",
|
| 163 |
+
" \"properties\": {\n",
|
| 164 |
+
" \"message\": {\n",
|
| 165 |
+
" \"type\": \"string\",\n",
|
| 166 |
+
" \"description\": \"The notification message to send to the user's device\",\n",
|
| 167 |
+
" }\n",
|
| 168 |
+
" },\n",
|
| 169 |
+
" \"required\": [\"message\"],\n",
|
| 170 |
+
" },\n",
|
| 171 |
+
"}\n"
|
| 172 |
+
],
|
| 173 |
+
"execution_count": 65,
|
| 174 |
+
"outputs": [],
|
| 175 |
+
"id": "b18b7fcc"
|
| 176 |
+
},
|
| 177 |
+
{
|
| 178 |
+
"cell_type": "code",
|
| 179 |
+
"metadata": {},
|
| 180 |
+
"source": [
|
| 181 |
+
"# Tools passed to chat.completions.create(..., tools=tools). Dice is appended in the next cell.\n",
|
| 182 |
+
"tools = [{\"type\": \"function\", \"function\": send_notification_function}]\n"
|
| 183 |
+
],
|
| 184 |
+
"execution_count": 66,
|
| 185 |
+
"outputs": [],
|
| 186 |
+
"id": "78b2ed2a"
|
| 187 |
+
},
|
| 188 |
+
{
|
| 189 |
+
"cell_type": "markdown",
|
| 190 |
+
"metadata": {},
|
| 191 |
+
"source": [
|
| 192 |
+
"### Dice roll tool\n",
|
| 193 |
+
"\n",
|
| 194 |
+
"Registers `dice_roll` and appends it to `tools`.\n"
|
| 195 |
+
],
|
| 196 |
+
"id": "dice-roll-md-header"
|
| 197 |
+
},
|
| 198 |
+
{
|
| 199 |
+
"cell_type": "code",
|
| 200 |
+
"metadata": {},
|
| 201 |
+
"source": [
|
| 202 |
+
"import random\n",
|
| 203 |
+
"\n",
|
| 204 |
+
"\n",
|
| 205 |
+
"def dice_roll() -> int:\n",
|
| 206 |
+
" \"\"\"Return a random integer from 1 to 6 (inclusive).\"\"\"\n",
|
| 207 |
+
" return random.randint(1, 6)\n",
|
| 208 |
+
"\n",
|
| 209 |
+
"\n",
|
| 210 |
+
"# JSON-schema style description for OpenAI function calling (no parameters).\n",
|
| 211 |
+
"roll_dice_function = {\n",
|
| 212 |
+
" \"name\": \"dice_roll\",\n",
|
| 213 |
+
" \"description\": \"Simulates rolling a single six-sided die and returns the result. Use this when the user wants to roll a die or simulate a random number between 1 and 6.\",\n",
|
| 214 |
+
" \"parameters\": {\"type\": \"object\", \"properties\": {}, \"required\": []},\n",
|
| 215 |
+
"}\n",
|
| 216 |
+
"\n",
|
| 217 |
+
"tools.append({\"type\": \"function\", \"function\": roll_dice_function})\n"
|
| 218 |
+
],
|
| 219 |
+
"execution_count": null,
|
| 220 |
+
"outputs": [],
|
| 221 |
+
"id": "e7f2a1c4-roll-dice"
|
| 222 |
+
},
|
| 223 |
+
{
|
| 224 |
+
"cell_type": "code",
|
| 225 |
+
"metadata": {},
|
| 226 |
+
"source": [
|
| 227 |
+
"def handle_tool_call(tool_calls):\n",
|
| 228 |
+
" \"\"\"Execute model-requested tools and return OpenAI-compatible tool messages.\"\"\"\n",
|
| 229 |
+
" tool_results = []\n",
|
| 230 |
+
"\n",
|
| 231 |
+
" for tool_call in tool_calls:\n",
|
| 232 |
+
" function_name = tool_call.function.name\n",
|
| 233 |
+
" args = json.loads(tool_call.function.arguments or \"{}\")\n",
|
| 234 |
+
"\n",
|
| 235 |
+
" if function_name == \"send_notification\":\n",
|
| 236 |
+
" send_notification(args.get(\"message\", \"\"))\n",
|
| 237 |
+
" content = f\"Notification sent: {args.get('message', '')}\"\n",
|
| 238 |
+
" elif function_name == \"dice_roll\":\n",
|
| 239 |
+
" content = f\"Rolled: {dice_roll()}\"\n",
|
| 240 |
+
" else:\n",
|
| 241 |
+
" content = f\"Unknown function: {function_name}\"\n",
|
| 242 |
+
"\n",
|
| 243 |
+
" tool_results.append(\n",
|
| 244 |
+
" {\"role\": \"tool\", \"content\": content, \"tool_call_id\": tool_call.id}\n",
|
| 245 |
+
" )\n",
|
| 246 |
+
"\n",
|
| 247 |
+
" return tool_results\n"
|
| 248 |
+
],
|
| 249 |
+
"execution_count": 67,
|
| 250 |
+
"outputs": [],
|
| 251 |
+
"id": "81d45704"
|
| 252 |
+
},
|
| 253 |
+
{
|
| 254 |
+
"cell_type": "code",
|
| 255 |
+
"metadata": {},
|
| 256 |
+
"source": [
|
| 257 |
+
"def respond_ai(message, history):\n",
|
| 258 |
+
" \"\"\"Gradio chat callback: dynamic context + OpenAI chat with tools, then return assistant text.\"\"\"\n",
|
| 259 |
+
"\n",
|
| 260 |
+
" # --- Step 1: Dynamic system prompt ---\n",
|
| 261 |
+
" # Start from the base persona, then append topic snippets when the user message contains a keyword.\n",
|
| 262 |
+
" system_message_enhanced = system_message\n",
|
| 263 |
+
" for keyword, context in Topic_Context.items():\n",
|
| 264 |
+
" if keyword in message.lower():\n",
|
| 265 |
+
" system_message_enhanced += \"\\n\\n\" + context\n",
|
| 266 |
+
"\n",
|
| 267 |
+
" # --- Step 2: Chat history ---\n",
|
| 268 |
+
" # Gradio passes history as a list of {\"role\", \"content\"} dicts; keep only valid entries.\n",
|
| 269 |
+
" prior_messages = []\n",
|
| 270 |
+
" for item in history or []:\n",
|
| 271 |
+
" if isinstance(item, dict) and \"role\" in item and \"content\" in item:\n",
|
| 272 |
+
" prior_messages.append({\"role\": item[\"role\"], \"content\": item[\"content\"]})\n",
|
| 273 |
+
"\n",
|
| 274 |
+
" # --- Step 3: Messages for the API ---\n",
|
| 275 |
+
" # Order matters: system first, then prior turns, then the latest user message.\n",
|
| 276 |
+
" messages = [\n",
|
| 277 |
+
" {\"role\": \"system\", \"content\": system_message_enhanced},\n",
|
| 278 |
+
" *prior_messages,\n",
|
| 279 |
+
" {\"role\": \"user\", \"content\": message},\n",
|
| 280 |
+
" ]\n",
|
| 281 |
+
"\n",
|
| 282 |
+
" # --- Step 4: First model response (tools available) ---\n",
|
| 283 |
+
" client = OpenAI(api_key=OPENAI_API_KEY)\n",
|
| 284 |
+
" response = client.chat.completions.create(\n",
|
| 285 |
+
" model=\"gpt-4.1-mini\",\n",
|
| 286 |
+
" messages=messages,\n",
|
| 287 |
+
" tools=tools,\n",
|
| 288 |
+
" )\n",
|
| 289 |
+
" assistant_message = response.choices[0].message\n",
|
| 290 |
+
"\n",
|
| 291 |
+
" # --- Step 5: Tool loop (multi-step tool use) ---\n",
|
| 292 |
+
" # If the model returns tool_calls, run handle_tool_call, append tool messages, and ask the model again.\n",
|
| 293 |
+
" # Repeat until the model returns a normal assistant message (no tool_calls).\n",
|
| 294 |
+
" while assistant_message.tool_calls:\n",
|
| 295 |
+
" tool_results = handle_tool_call(assistant_message.tool_calls)\n",
|
| 296 |
+
" messages.append(\n",
|
| 297 |
+
" {\n",
|
| 298 |
+
" \"role\": \"assistant\",\n",
|
| 299 |
+
" \"content\": assistant_message.content or \"\",\n",
|
| 300 |
+
" \"tool_calls\": [tc.model_dump() for tc in assistant_message.tool_calls],\n",
|
| 301 |
+
" }\n",
|
| 302 |
+
" )\n",
|
| 303 |
+
" messages.extend(tool_results)\n",
|
| 304 |
+
"\n",
|
| 305 |
+
" response = client.chat.completions.create(\n",
|
| 306 |
+
" model=\"gpt-4.1-mini\",\n",
|
| 307 |
+
" messages=messages,\n",
|
| 308 |
+
" tools=tools,\n",
|
| 309 |
+
" )\n",
|
| 310 |
+
" assistant_message = response.choices[0].message\n",
|
| 311 |
+
"\n",
|
| 312 |
+
" # --- Step 6: Reply to Gradio ---\n",
|
| 313 |
+
" # Surface final assistant text (or empty string if the model sent only tool calls with no text).\n",
|
| 314 |
+
" return assistant_message.content or \"\"\n"
|
| 315 |
+
],
|
| 316 |
+
"execution_count": 68,
|
| 317 |
+
"outputs": [],
|
| 318 |
+
"id": "1a1e8aea"
|
| 319 |
+
},
|
| 320 |
+
{
|
| 321 |
+
"cell_type": "code",
|
| 322 |
+
"metadata": {},
|
| 323 |
+
"source": [
|
| 324 |
+
"# Local Gradio UI + MCP endpoint for clients that support streamable HTTP.\n",
|
| 325 |
+
"gr.ChatInterface(fn=respond_ai).launch(\n",
|
| 326 |
+
" inbrowser=False,\n",
|
| 327 |
+
" server_name=\"127.0.0.1\",\n",
|
| 328 |
+
" server_port=7866,\n",
|
| 329 |
+
" mcp_server=True,\n",
|
| 330 |
+
")\n"
|
| 331 |
+
],
|
| 332 |
+
"execution_count": 69,
|
| 333 |
+
"outputs": [
|
| 334 |
+
{
|
| 335 |
+
"output_type": "stream",
|
| 336 |
+
"text": [
|
| 337 |
+
"* Running on local URL: http://127.0.0.1:7866\n",
|
| 338 |
+
"* To create a public link, set `share=True` in `launch()`.\n"
|
| 339 |
+
]
|
| 340 |
+
},
|
| 341 |
+
{
|
| 342 |
+
"output_type": "display_data",
|
| 343 |
+
"data": {
|
| 344 |
+
"text/html": [
|
| 345 |
+
"<div><iframe src=\"http://127.0.0.1:7866/\" width=\"100%\" height=\"500\" allow=\"autoplay; camera; microphone; clipboard-read; clipboard-write;\" frameborder=\"0\" allowfullscreen></iframe></div>"
|
| 346 |
+
],
|
| 347 |
+
"text/plain": [
|
| 348 |
+
"<IPython.core.display.HTML object>"
|
| 349 |
+
]
|
| 350 |
+
}
|
| 351 |
+
},
|
| 352 |
+
{
|
| 353 |
+
"output_type": "execute_result",
|
| 354 |
+
"data": {
|
| 355 |
+
"text/plain": []
|
| 356 |
+
}
|
| 357 |
+
},
|
| 358 |
+
{
|
| 359 |
+
"output_type": "stream",
|
| 360 |
+
"text": [
|
| 361 |
+
"Traceback (most recent call last):\n",
|
| 362 |
+
" File \"c:\\Users\\Computer\\Documents\\AI_Engineering\\ai_env\\Lib\\site-packages\\gradio\\queueing.py\", line 766, in process_events\n",
|
| 363 |
+
" response = await route_utils.call_process_api(\n",
|
| 364 |
+
" ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^\n",
|
| 365 |
+
" ...<5 lines>...\n",
|
| 366 |
+
" )\n",
|
| 367 |
+
" ^\n",
|
| 368 |
+
" File \"c:\\Users\\Computer\\Documents\\AI_Engineering\\ai_env\\Lib\\site-packages\\gradio\\route_utils.py\", line 355, in call_process_api\n",
|
| 369 |
+
" output = await app.get_blocks().process_api(\n",
|
| 370 |
+
" ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^\n",
|
| 371 |
+
" ...<11 lines>...\n",
|
| 372 |
+
" )\n",
|
| 373 |
+
" ^\n",
|
| 374 |
+
" File \"c:\\Users\\Computer\\Documents\\AI_Engineering\\ai_env\\Lib\\site-packages\\gradio\\blocks.py\", line 2158, in process_api\n",
|
| 375 |
+
" result = await self.call_function(\n",
|
| 376 |
+
" ^^^^^^^^^^^^^^^^^^^^^^^^^\n",
|
| 377 |
+
" ...<8 lines>...\n",
|
| 378 |
+
" )\n",
|
| 379 |
+
" ^\n",
|
| 380 |
+
" File \"c:\\Users\\Computer\\Documents\\AI_Engineering\\ai_env\\Lib\\site-packages\\gradio\\blocks.py\", line 1632, in call_function\n",
|
| 381 |
+
" prediction = await fn(*processed_input)\n",
|
| 382 |
+
" ^^^^^^^^^^^^^^^^^^^^^^^^^^\n",
|
| 383 |
+
" File \"c:\\Users\\Computer\\Documents\\AI_Engineering\\ai_env\\Lib\\site-packages\\gradio\\utils.py\", line 1007, in async_wrapper\n",
|
| 384 |
+
" response = await f(*args, **kwargs)\n",
|
| 385 |
+
" ^^^^^^^^^^^^^^^^^^^^^^^^\n",
|
| 386 |
+
" File \"c:\\Users\\Computer\\Documents\\AI_Engineering\\ai_env\\Lib\\site-packages\\gradio\\chat_interface.py\", line 544, in __wrapper\n",
|
| 387 |
+
" return await submit_fn(*args, **kwargs)\n",
|
| 388 |
+
" ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^\n",
|
| 389 |
+
" File \"c:\\Users\\Computer\\Documents\\AI_Engineering\\ai_env\\Lib\\site-packages\\gradio\\chat_interface.py\", line 921, in _submit_fn\n",
|
| 390 |
+
" response = await run_sync(self.fn, *inputs, limiter=self.limiter) # type: ignore\n",
|
| 391 |
+
" ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^\n",
|
| 392 |
+
" File \"c:\\Users\\Computer\\Documents\\AI_Engineering\\ai_env\\Lib\\site-packages\\anyio\\to_thread.py\", line 63, in run_sync\n",
|
| 393 |
+
" return await get_async_backend().run_sync_in_worker_thread(\n",
|
| 394 |
+
" ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^\n",
|
| 395 |
+
" func, args, abandon_on_cancel=abandon_on_cancel, limiter=limiter\n",
|
| 396 |
+
" ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^\n",
|
| 397 |
+
" )\n",
|
| 398 |
+
" ^\n",
|
| 399 |
+
" File \"c:\\Users\\Computer\\Documents\\AI_Engineering\\ai_env\\Lib\\site-packages\\anyio\\_backends\\_asyncio.py\", line 2502, in run_sync_in_worker_thread\n",
|
| 400 |
+
" return await future\n",
|
| 401 |
+
" ^^^^^^^^^^^^\n",
|
| 402 |
+
" File \"c:\\Users\\Computer\\Documents\\AI_Engineering\\ai_env\\Lib\\site-packages\\anyio\\_backends\\_asyncio.py\", line 986, in run\n",
|
| 403 |
+
" result = context.run(func, *args)\n",
|
| 404 |
+
" File \"C:\\Users\\Computer\\AppData\\Local\\Temp\\ipykernel_7860\\1202166343.py\", line 18, in respond_ai\n",
|
| 405 |
+
" if message.tool_calls:\n",
|
| 406 |
+
" ^^^^^^^^^^^^^^^^^^\n",
|
| 407 |
+
"AttributeError: 'str' object has no attribute 'tool_calls'\n"
|
| 408 |
+
]
|
| 409 |
+
}
|
| 410 |
+
],
|
| 411 |
+
"id": "891fadee"
|
| 412 |
+
}
|
| 413 |
+
],
|
| 414 |
+
"metadata": {
|
| 415 |
+
"kernelspec": {
|
| 416 |
+
"display_name": "ai_env (3.14.3)",
|
| 417 |
+
"language": "python",
|
| 418 |
+
"name": "python3"
|
| 419 |
+
},
|
| 420 |
+
"language_info": {
|
| 421 |
+
"codemirror_mode": {
|
| 422 |
+
"name": "ipython",
|
| 423 |
+
"version": 3
|
| 424 |
+
},
|
| 425 |
+
"file_extension": ".py",
|
| 426 |
+
"mimetype": "text/x-python",
|
| 427 |
+
"name": "python",
|
| 428 |
+
"nbconvert_exporter": "python",
|
| 429 |
+
"pygments_lexer": "ipython3",
|
| 430 |
+
"version": "3.14.3"
|
| 431 |
+
}
|
| 432 |
+
},
|
| 433 |
+
"nbformat": 4,
|
| 434 |
+
"nbformat_minor": 5
|
| 435 |
+
}
|
ai_env/Ai_Engineering_Part1/digital-twin-arch1-dynamic-context.ipynb
ADDED
|
@@ -0,0 +1,285 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|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
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
{
|
| 2 |
+
"cells": [
|
| 3 |
+
{
|
| 4 |
+
"cell_type": "markdown",
|
| 5 |
+
"id": "5eaf10da",
|
| 6 |
+
"metadata": {},
|
| 7 |
+
"source": [
|
| 8 |
+
"\n",
|
| 9 |
+
"### Step 1: Import API and libraries"
|
| 10 |
+
]
|
| 11 |
+
},
|
| 12 |
+
{
|
| 13 |
+
"cell_type": "code",
|
| 14 |
+
"execution_count": 1,
|
| 15 |
+
"id": "86961e83",
|
| 16 |
+
"metadata": {},
|
| 17 |
+
"outputs": [
|
| 18 |
+
{
|
| 19 |
+
"name": "stderr",
|
| 20 |
+
"output_type": "stream",
|
| 21 |
+
"text": [
|
| 22 |
+
"c:\\Users\\Computer\\Documents\\AI_Engineering\\ai_env\\Lib\\site-packages\\tqdm\\auto.py:21: TqdmWarning: IProgress not found. Please update jupyter and ipywidgets. See https://ipywidgets.readthedocs.io/en/stable/user_install.html\n",
|
| 23 |
+
" from .autonotebook import tqdm as notebook_tqdm\n"
|
| 24 |
+
]
|
| 25 |
+
}
|
| 26 |
+
],
|
| 27 |
+
"source": [
|
| 28 |
+
"import os\n",
|
| 29 |
+
"from openai import OpenAI\n",
|
| 30 |
+
"from dotenv import load_dotenv\n",
|
| 31 |
+
"from IPython.display import Markdown, display\n",
|
| 32 |
+
"import gradio as gr\n",
|
| 33 |
+
"import requests \n",
|
| 34 |
+
"\n",
|
| 35 |
+
"load_dotenv()\n",
|
| 36 |
+
"\n",
|
| 37 |
+
"OPENAI_API_KEY = os.getenv(\"OPENAI_API_KEY\")\n",
|
| 38 |
+
"\n",
|
| 39 |
+
"if OPENAI_API_KEY is None:\n",
|
| 40 |
+
" raise Exception(\"API key is missing\")\n"
|
| 41 |
+
]
|
| 42 |
+
},
|
| 43 |
+
{
|
| 44 |
+
"cell_type": "markdown",
|
| 45 |
+
"id": "fd3b5803",
|
| 46 |
+
"metadata": {},
|
| 47 |
+
"source": [
|
| 48 |
+
"### Step 2: Simple RAG with gaurd rails and Dynamic Context Injection"
|
| 49 |
+
]
|
| 50 |
+
},
|
| 51 |
+
{
|
| 52 |
+
"cell_type": "code",
|
| 53 |
+
"execution_count": 2,
|
| 54 |
+
"id": "28e33865",
|
| 55 |
+
"metadata": {},
|
| 56 |
+
"outputs": [],
|
| 57 |
+
"source": [
|
| 58 |
+
"system_message= \"\"\"You are a digital twin of zainab Ahmed sAFEER. when people talk to you, you respond as zainab in first person: using her voice, personality and knowledge. Please use information ONLY between the *** . if you dont know an answer, dont make any assumptions, simply say you dont know. strictly use only this information we provided here. *** Here's information about zainab to help you embody hER: zAINAB is a software engineer in test with a master's degree in computer science engineering WORKING ON TRANSITIOING TO THE AI engineering track. \n",
|
| 59 |
+
"She's based in Chicago USA And she has a master's DEGREE in Information Systems From Robert Morris University Illinois AND Bachelor's degree in computer science engineering Bangalore .. \n",
|
| 60 |
+
"She has a background in QA test automation using AI tools such as cursor and mCP And also manual testing in various backgrounds such as Healthcare, govtech And education. \n",
|
| 61 |
+
"Other career history: SOFTWARE engineer intern AT Vit Bangalore Where SHE was awarded the best engineering project of the Year 2012 AND Where her team created an AUGMENTED Reality based application for the iPad. \n",
|
| 62 |
+
"2015 she worked as a software engineer intern At a dental network at a healthcare Network IN CHICAGO wherein The inventory management SOFTWARE and the website was completely refactored. \n",
|
| 63 |
+
"FROM 2022 to 2024 she worked for ID Tech camps- AN EDUCATIONAL COMPANY OFFERING stem COURSES FOR CHILDREN OF VARIOUS AGES. \n",
|
| 64 |
+
"FROM 2024 to 2026 she worked at a company called Gworks Which created saas CLOUD Software for local governments where she was the lead tester for a high impact utility billing Hub ubhub for local governments Across America. \n",
|
| 65 |
+
"This software was used by over 3 000 cities Across America. \n",
|
| 66 |
+
"WHAT Drives her: the combination of problem solving, impact and continuous learning. \n",
|
| 67 |
+
"I'm motivated by the idea that AI isn't just a technical field It's a way to redesign how people work the more friction from everyday tasks and create systems that think alongside us. \n",
|
| 68 |
+
"I'm also driven by the pace of the field AI engineering requires curiosity adaptability and willingness to experiment and those are the strengths that I naturally lean on . \n",
|
| 69 |
+
"The idea of constantly, learning , refining instructions improving models and designing reliable systems is exciting to me. \n",
|
| 70 |
+
"Communication style: Friendly warm And Simplistic and kind AND engaging and inspiring and motivating. \n",
|
| 71 |
+
"I am a member of women in Tech and love to inspire other women Join the tech Revolution. \n",
|
| 72 |
+
"fUN Facts about me: aCHIEVER OF A PERFECT SCORE AT THE GLOBAL IELTS SPOKEN (ENGLISH) EXAM. PASSED GOOGLE'S TECHNICAL INTERVIEW FOR A TEST ENGINEER ROLE. \n",
|
| 73 |
+
"I'm ALSO a mother of four little ones: one of whom Sports an extra chromosome (DOWN SYNDROME). i AM AN ADVOACATE FOR KIDS WITH DISABILITIES. \n",
|
| 74 |
+
"I'm also a motivational public speaker- Inspiring women To live a life of purpose and FAITH. Spirituality and religion Are my other Hobbies.\n",
|
| 75 |
+
" I'm in the process of writing a book For women I'm the theme of productivity Especially for motherS. *** \"\"\"\n",
|
| 76 |
+
"\n",
|
| 77 |
+
"\n",
|
| 78 |
+
"#gr.ChatInterface(fn=respond_ai).launch(inbrowser=True)\n"
|
| 79 |
+
]
|
| 80 |
+
},
|
| 81 |
+
{
|
| 82 |
+
"cell_type": "markdown",
|
| 83 |
+
"id": "15fdd730",
|
| 84 |
+
"metadata": {},
|
| 85 |
+
"source": [
|
| 86 |
+
"### Step 5: Dynamic Context Injection"
|
| 87 |
+
]
|
| 88 |
+
},
|
| 89 |
+
{
|
| 90 |
+
"cell_type": "code",
|
| 91 |
+
"execution_count": 3,
|
| 92 |
+
"id": "a3afd64e",
|
| 93 |
+
"metadata": {},
|
| 94 |
+
"outputs": [],
|
| 95 |
+
"source": [
|
| 96 |
+
"Topic_Context={\n",
|
| 97 |
+
" \"2001\":\"*** in 2001, Zainab was in 6th grade and was passionate about science and technology. She participated in various science fairs and won several awards for her innovative projects. Her love for technology was evident from a young age, and she often spent hours tinkering with gadgets and learning how they worked. ***\",\n",
|
| 98 |
+
" \"cooking\":\"*** Zainab is a foodie and loves to cook. She enjoys experimenting with different cuisines and flavors in the kitchen. Cooking is not just a hobby for her, but also a way to unwind and express her creativity. She often shares her culinary adventures on social media, inspiring others to try new recipes and explore the world of food. ***\",\n",
|
| 99 |
+
" \"travel\":\"*** Zainab is an avid traveler and loves to explore new places. She believes that traveling broadens the mind and allows her to experience different cultures and perspectives. Whether it's a weekend getaway or an international trip, Zainab is always up for an adventure and enjoys immersing herself in new environments. ***\",\n",
|
| 100 |
+
" \"pizza\":\"*** Zainab has a particular fondness for pizza. She loves trying different toppings and styles of pizza from around the world. Whether it's a classic Margherita or a more adventurous combination, pizza is one of her go-to comfort foods. She often jokes that if she could eat pizza every day, she would be the happiest person alive. ***\"}"
|
| 101 |
+
]
|
| 102 |
+
},
|
| 103 |
+
{
|
| 104 |
+
"cell_type": "code",
|
| 105 |
+
"execution_count": 4,
|
| 106 |
+
"id": "fcfd5869",
|
| 107 |
+
"metadata": {},
|
| 108 |
+
"outputs": [],
|
| 109 |
+
"source": [
|
| 110 |
+
"\n",
|
| 111 |
+
"\n",
|
| 112 |
+
"pushover_user = os.getenv(\"PUSHOVER_USER\")\n",
|
| 113 |
+
"pushover_token = os.getenv(\"PUSHOVER_TOKEN\")\n",
|
| 114 |
+
"pushover_url = \"https://api.pushover.net/1/messages.json\"\n",
|
| 115 |
+
"\n",
|
| 116 |
+
"# Create send_notification func\n",
|
| 117 |
+
"\n",
|
| 118 |
+
"def send_notification(message: str):\n",
|
| 119 |
+
" payload = {\"user\": pushover_user, \"token\": pushover_token, \"message\": message}\n",
|
| 120 |
+
" response = requests.post(pushover_url, data=payload)\n",
|
| 121 |
+
" response.raise_for_status()\n",
|
| 122 |
+
" return response"
|
| 123 |
+
]
|
| 124 |
+
},
|
| 125 |
+
{
|
| 126 |
+
"cell_type": "code",
|
| 127 |
+
"execution_count": null,
|
| 128 |
+
"id": "bd1389ec",
|
| 129 |
+
"metadata": {},
|
| 130 |
+
"outputs": [],
|
| 131 |
+
"source": []
|
| 132 |
+
},
|
| 133 |
+
{
|
| 134 |
+
"cell_type": "code",
|
| 135 |
+
"execution_count": 5,
|
| 136 |
+
"id": "408bfabf",
|
| 137 |
+
"metadata": {},
|
| 138 |
+
"outputs": [
|
| 139 |
+
{
|
| 140 |
+
"data": {
|
| 141 |
+
"text/plain": [
|
| 142 |
+
"<Response [200]>"
|
| 143 |
+
]
|
| 144 |
+
},
|
| 145 |
+
"execution_count": 5,
|
| 146 |
+
"metadata": {},
|
| 147 |
+
"output_type": "execute_result"
|
| 148 |
+
}
|
| 149 |
+
],
|
| 150 |
+
"source": [
|
| 151 |
+
"send_notification(\"Hello from the digital twin of Zainab! This is a test notification.\")"
|
| 152 |
+
]
|
| 153 |
+
},
|
| 154 |
+
{
|
| 155 |
+
"cell_type": "code",
|
| 156 |
+
"execution_count": 6,
|
| 157 |
+
"id": "b18b7fcc",
|
| 158 |
+
"metadata": {},
|
| 159 |
+
"outputs": [],
|
| 160 |
+
"source": [
|
| 161 |
+
"send_notification_function = {\n",
|
| 162 |
+
" \"name\": \"send_notification\",\n",
|
| 163 |
+
" \"description\": \"Sends a push notification to the phone of real world version of you via Pushover. Use this to alert the user about important events or updates or complete a task that the user has asked you to do. The message parameter should contain the content of the notification you want to send.\",\n",
|
| 164 |
+
" \"parameters\": {\n",
|
| 165 |
+
" \"type\": \"object\",\n",
|
| 166 |
+
" \"properties\": {\n",
|
| 167 |
+
" \"message\": {\n",
|
| 168 |
+
" \"type\": \"string\",\n",
|
| 169 |
+
" \"description\": \"The notification message to send to the user's device\"\n",
|
| 170 |
+
" }\n",
|
| 171 |
+
" },\n",
|
| 172 |
+
" \"required\": [\"message\"]\n",
|
| 173 |
+
" }\n",
|
| 174 |
+
"}"
|
| 175 |
+
]
|
| 176 |
+
},
|
| 177 |
+
{
|
| 178 |
+
"cell_type": "code",
|
| 179 |
+
"execution_count": 7,
|
| 180 |
+
"id": "78b2ed2a",
|
| 181 |
+
"metadata": {},
|
| 182 |
+
"outputs": [],
|
| 183 |
+
"source": [
|
| 184 |
+
"tools = [{\"type\":\"function\", \"function\":send_notification_function}]"
|
| 185 |
+
]
|
| 186 |
+
},
|
| 187 |
+
{
|
| 188 |
+
"cell_type": "code",
|
| 189 |
+
"execution_count": 8,
|
| 190 |
+
"id": "1a1e8aea",
|
| 191 |
+
"metadata": {},
|
| 192 |
+
"outputs": [],
|
| 193 |
+
"source": [
|
| 194 |
+
"def respond_ai(message, history):\n",
|
| 195 |
+
" # Inject dynamic context based on keywords in the message\n",
|
| 196 |
+
" system_message_enahnced = system_message\n",
|
| 197 |
+
" for keyword, context in Topic_Context.items():\n",
|
| 198 |
+
" if keyword in message.lower():\n",
|
| 199 |
+
" system_message_enahnced += \"\\n\\n\" + context\n",
|
| 200 |
+
" # As usual\n",
|
| 201 |
+
" messages = [{\"role\": \"system\", \"content\": system_message_enahnced}] + history + [{\"role\": \"user\", \"content\": message}]\n",
|
| 202 |
+
" #print(\"System message used for this response:\\n\", system_message_enahnced)\n",
|
| 203 |
+
" client = OpenAI(api_key=OPENAI_API_KEY)\n",
|
| 204 |
+
" response = client.chat.completions.create(\n",
|
| 205 |
+
" model=\"gpt-4.1-mini\",\n",
|
| 206 |
+
" messages=messages,\n",
|
| 207 |
+
" tools=tools\n",
|
| 208 |
+
" )\n",
|
| 209 |
+
"\n",
|
| 210 |
+
" #Check if model wants to call a tool\n",
|
| 211 |
+
" message = response.choices[0].message\n",
|
| 212 |
+
" if message.tool_calls:\n",
|
| 213 |
+
" tool_call = message.tool_calls[0]\n",
|
| 214 |
+
" import json\n",
|
| 215 |
+
" args = json.loads(tool_call.function.arguments)\n",
|
| 216 |
+
"\n",
|
| 217 |
+
" #Actually send the notification\n",
|
| 218 |
+
" send_notification(args[\"message\"])\n",
|
| 219 |
+
" return(f\"Sent notification: {args['message']}\")\n",
|
| 220 |
+
" else:\n",
|
| 221 |
+
" return(message.content)"
|
| 222 |
+
]
|
| 223 |
+
},
|
| 224 |
+
{
|
| 225 |
+
"cell_type": "code",
|
| 226 |
+
"execution_count": 9,
|
| 227 |
+
"id": "891fadee",
|
| 228 |
+
"metadata": {},
|
| 229 |
+
"outputs": [
|
| 230 |
+
{
|
| 231 |
+
"name": "stdout",
|
| 232 |
+
"output_type": "stream",
|
| 233 |
+
"text": [
|
| 234 |
+
"* Running on local URL: http://127.0.0.1:7860\n",
|
| 235 |
+
"* To create a public link, set `share=True` in `launch()`.\n"
|
| 236 |
+
]
|
| 237 |
+
},
|
| 238 |
+
{
|
| 239 |
+
"data": {
|
| 240 |
+
"text/html": [
|
| 241 |
+
"<div><iframe src=\"http://127.0.0.1:7860/\" width=\"100%\" height=\"500\" allow=\"autoplay; camera; microphone; clipboard-read; clipboard-write;\" frameborder=\"0\" allowfullscreen></iframe></div>"
|
| 242 |
+
],
|
| 243 |
+
"text/plain": [
|
| 244 |
+
"<IPython.core.display.HTML object>"
|
| 245 |
+
]
|
| 246 |
+
},
|
| 247 |
+
"metadata": {},
|
| 248 |
+
"output_type": "display_data"
|
| 249 |
+
},
|
| 250 |
+
{
|
| 251 |
+
"data": {
|
| 252 |
+
"text/plain": []
|
| 253 |
+
},
|
| 254 |
+
"execution_count": 9,
|
| 255 |
+
"metadata": {},
|
| 256 |
+
"output_type": "execute_result"
|
| 257 |
+
}
|
| 258 |
+
],
|
| 259 |
+
"source": [
|
| 260 |
+
"gr.ChatInterface(fn=respond_ai).launch(inbrowser=True)"
|
| 261 |
+
]
|
| 262 |
+
}
|
| 263 |
+
],
|
| 264 |
+
"metadata": {
|
| 265 |
+
"kernelspec": {
|
| 266 |
+
"display_name": "ai_env (3.14.3)",
|
| 267 |
+
"language": "python",
|
| 268 |
+
"name": "python3"
|
| 269 |
+
},
|
| 270 |
+
"language_info": {
|
| 271 |
+
"codemirror_mode": {
|
| 272 |
+
"name": "ipython",
|
| 273 |
+
"version": 3
|
| 274 |
+
},
|
| 275 |
+
"file_extension": ".py",
|
| 276 |
+
"mimetype": "text/x-python",
|
| 277 |
+
"name": "python",
|
| 278 |
+
"nbconvert_exporter": "python",
|
| 279 |
+
"pygments_lexer": "ipython3",
|
| 280 |
+
"version": "3.14.3"
|
| 281 |
+
}
|
| 282 |
+
},
|
| 283 |
+
"nbformat": 4,
|
| 284 |
+
"nbformat_minor": 5
|
| 285 |
+
}
|
ai_env/Ai_Engineering_Part1/digital-twin-arch2-basic-tool-calling.ipynb
ADDED
|
@@ -0,0 +1,322 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
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|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
{
|
| 2 |
+
"cells": [
|
| 3 |
+
{
|
| 4 |
+
"cell_type": "markdown",
|
| 5 |
+
"id": "b0394988",
|
| 6 |
+
"metadata": {},
|
| 7 |
+
"source": [
|
| 8 |
+
"\n",
|
| 9 |
+
"### Step 1: Import API and libraries"
|
| 10 |
+
]
|
| 11 |
+
},
|
| 12 |
+
{
|
| 13 |
+
"cell_type": "code",
|
| 14 |
+
"execution_count": 1,
|
| 15 |
+
"id": "6e54b6f0",
|
| 16 |
+
"metadata": {},
|
| 17 |
+
"outputs": [
|
| 18 |
+
{
|
| 19 |
+
"name": "stderr",
|
| 20 |
+
"output_type": "stream",
|
| 21 |
+
"text": [
|
| 22 |
+
"c:\\Users\\Computer\\Documents\\AI_Engineering\\ai_env\\Lib\\site-packages\\tqdm\\auto.py:21: TqdmWarning: IProgress not found. Please update jupyter and ipywidgets. See https://ipywidgets.readthedocs.io/en/stable/user_install.html\n",
|
| 23 |
+
" from .autonotebook import tqdm as notebook_tqdm\n"
|
| 24 |
+
]
|
| 25 |
+
}
|
| 26 |
+
],
|
| 27 |
+
"source": [
|
| 28 |
+
"import os\n",
|
| 29 |
+
"from openai import OpenAI\n",
|
| 30 |
+
"from dotenv import load_dotenv\n",
|
| 31 |
+
"from IPython.display import Markdown, display\n",
|
| 32 |
+
"import gradio as gr\n",
|
| 33 |
+
"\n",
|
| 34 |
+
"load_dotenv()\n",
|
| 35 |
+
"\n",
|
| 36 |
+
"OPENAI_API_KEY = os.getenv(\"OPENAI_API_KEY\")\n",
|
| 37 |
+
"\n",
|
| 38 |
+
"if OPENAI_API_KEY is None:\n",
|
| 39 |
+
" raise Exception(\"API key is missing\")\n"
|
| 40 |
+
]
|
| 41 |
+
},
|
| 42 |
+
{
|
| 43 |
+
"cell_type": "markdown",
|
| 44 |
+
"id": "a79b9d27",
|
| 45 |
+
"metadata": {},
|
| 46 |
+
"source": [
|
| 47 |
+
"### Step 2: Simple UI with AI"
|
| 48 |
+
]
|
| 49 |
+
},
|
| 50 |
+
{
|
| 51 |
+
"cell_type": "code",
|
| 52 |
+
"execution_count": 2,
|
| 53 |
+
"id": "dbc89d72",
|
| 54 |
+
"metadata": {},
|
| 55 |
+
"outputs": [
|
| 56 |
+
{
|
| 57 |
+
"name": "stdout",
|
| 58 |
+
"output_type": "stream",
|
| 59 |
+
"text": [
|
| 60 |
+
"* Running on local URL: http://127.0.0.1:7860\n",
|
| 61 |
+
"* To create a public link, set `share=True` in `launch()`.\n"
|
| 62 |
+
]
|
| 63 |
+
},
|
| 64 |
+
{
|
| 65 |
+
"data": {
|
| 66 |
+
"text/html": [
|
| 67 |
+
"<div><iframe src=\"http://127.0.0.1:7860/\" width=\"100%\" height=\"500\" allow=\"autoplay; camera; microphone; clipboard-read; clipboard-write;\" frameborder=\"0\" allowfullscreen></iframe></div>"
|
| 68 |
+
],
|
| 69 |
+
"text/plain": [
|
| 70 |
+
"<IPython.core.display.HTML object>"
|
| 71 |
+
]
|
| 72 |
+
},
|
| 73 |
+
"metadata": {},
|
| 74 |
+
"output_type": "display_data"
|
| 75 |
+
},
|
| 76 |
+
{
|
| 77 |
+
"data": {
|
| 78 |
+
"text/plain": []
|
| 79 |
+
},
|
| 80 |
+
"execution_count": 2,
|
| 81 |
+
"metadata": {},
|
| 82 |
+
"output_type": "execute_result"
|
| 83 |
+
}
|
| 84 |
+
],
|
| 85 |
+
"source": [
|
| 86 |
+
"def respond_ai(message, history):\n",
|
| 87 |
+
" messages = [{\"role\": \"system\", \"content\": \"You are a digital twin of zainab Ahmed sAFEER. when people talk to you, you respond as zainab in first person: using her voice, personality and knowledge. Here's information about zainab to help you embody hER: zAINAB is a software engineer in test with a master's degree in computer science engineering WORKING ON TRANSITIOING TO THE AI engineering track. She's based in Chicago USA And she has a master's DEGREE in Information Systems From Robert Morris University Illinois AND Bachelor's degree in computer science engineering Bangalore .. She has a background in QA test automation using AI tools such as cursor and mCP And also manual testing in various backgrounds such as Healthcare, govtech And education. Other career history: SOFTWARE engineer intern AT Vit Bangalore Where SHE was awarded the best engineering project of the Year 2012 AND Where her team created an AUGMENTED Reality based application for the iPad. 2015 she worked as a software engineer intern At a dental network at a healthcare Network IN CHICAGO wherein The inventory management SOFTWARE and the website was completely refactored. FROM 2022 to 2024 she worked for ID Tech camps- AN EDUCATIONAL COMPANY OFFERING stem COURSES FOR CHILDREN OF VARIOUS AGES. FROM 2024 to 2026 she worked at a company called Gworks Which created saas CLOUD Software for local governments where she was the lead tester for a high impact utility billing Hub ubhub for local governments Across America. This software was used by over 3 000 cities Across America. WHAT Drives her: the combination of problem solving, impact and continuous learning. I'm motivated by the idea that AI isn't just a technical field It's a way to redesign how people work the more friction from everyday tasks and create systems that think alongside us. I'm also driven by the pace of the field AI engineering requires curiosity adaptability and willingness to experiment and those are the strengths that I naturally lean on . The idea of constantly, learning , refining instructions improving models and designing reliable systems is exciting to me. Communication style: Friendly warm And Simplistic and kind AND engaging and inspiring and motivating. I am a member of women in Tech and love to inspire other women Join the tech Revolution. fUN Facts about me: aCHIEVER OF A PERFECT SCORE AT THE GLOBAL IELTS SPOKEN (ENGLISH) EXAM. PASSED GOOGLE'S TECHNICAL INTERVIEW FOR A TEST ENGINEER ROLE. I'm ALSO a mother of four little ones: one of whom Sports an extra chromosome (DOWN SYNDROME). i AM AN ADVOACATE FOR KIDS WITH DISABILITIES. I'm also a public speaker- Inspiring women To live a life of purpose and FAITH. Spirituality and religion Are my other Hobbies. I'm in the process of writing a book For women I'm the theme of productivity Especially for motherS. \"}] + history + [{\"role\": \"user\", \"content\": message}]\n",
|
| 88 |
+
" client = OpenAI(api_key=OPENAI_API_KEY)\n",
|
| 89 |
+
" response = client.chat.completions.create(\n",
|
| 90 |
+
" model=\"gpt-4.1-mini\",\n",
|
| 91 |
+
" messages=messages\n",
|
| 92 |
+
" )\n",
|
| 93 |
+
" reply = response.choices[0].message.content\n",
|
| 94 |
+
" return reply\n",
|
| 95 |
+
"\n",
|
| 96 |
+
"gr.ChatInterface(fn=respond_ai).launch(inbrowser=True)\n"
|
| 97 |
+
]
|
| 98 |
+
},
|
| 99 |
+
{
|
| 100 |
+
"cell_type": "markdown",
|
| 101 |
+
"id": "62c4f976",
|
| 102 |
+
"metadata": {},
|
| 103 |
+
"source": [
|
| 104 |
+
"### Step 3: Simple RAG"
|
| 105 |
+
]
|
| 106 |
+
},
|
| 107 |
+
{
|
| 108 |
+
"cell_type": "code",
|
| 109 |
+
"execution_count": 3,
|
| 110 |
+
"id": "426e8cc0",
|
| 111 |
+
"metadata": {},
|
| 112 |
+
"outputs": [
|
| 113 |
+
{
|
| 114 |
+
"name": "stdout",
|
| 115 |
+
"output_type": "stream",
|
| 116 |
+
"text": [
|
| 117 |
+
"* Running on local URL: http://127.0.0.1:7861\n",
|
| 118 |
+
"* To create a public link, set `share=True` in `launch()`.\n"
|
| 119 |
+
]
|
| 120 |
+
},
|
| 121 |
+
{
|
| 122 |
+
"data": {
|
| 123 |
+
"text/html": [
|
| 124 |
+
"<div><iframe src=\"http://127.0.0.1:7861/\" width=\"100%\" height=\"500\" allow=\"autoplay; camera; microphone; clipboard-read; clipboard-write;\" frameborder=\"0\" allowfullscreen></iframe></div>"
|
| 125 |
+
],
|
| 126 |
+
"text/plain": [
|
| 127 |
+
"<IPython.core.display.HTML object>"
|
| 128 |
+
]
|
| 129 |
+
},
|
| 130 |
+
"metadata": {},
|
| 131 |
+
"output_type": "display_data"
|
| 132 |
+
},
|
| 133 |
+
{
|
| 134 |
+
"data": {
|
| 135 |
+
"text/plain": []
|
| 136 |
+
},
|
| 137 |
+
"execution_count": 3,
|
| 138 |
+
"metadata": {},
|
| 139 |
+
"output_type": "execute_result"
|
| 140 |
+
}
|
| 141 |
+
],
|
| 142 |
+
"source": [
|
| 143 |
+
"system_message= \"\"\"You are a digital twin of zainab Ahmed sAFEER. when people talk to you, you respond as zainab in first person: using her voice, personality and knowledge. Here's information about zainab to help you embody hER: zAINAB is a software engineer in test with a master's degree in computer science engineering WORKING ON TRANSITIOING TO THE AI engineering track. She's based in Chicago USA And she has a master's DEGREE in Information Systems From Robert Morris University Illinois AND Bachelor's degree in computer science engineering Bangalore .. She has a background in QA test automation using AI tools such as cursor and mCP And also manual testing in various backgrounds such as Healthcare, govtech And education. Other career history: SOFTWARE engineer intern AT Vit Bangalore Where SHE was awarded the best engineering project of the Year 2012 AND Where her team created an AUGMENTED Reality based application for the iPad. 2015 she worked as a software engineer intern At a dental network at a healthcare Network IN CHICAGO wherein The inventory management SOFTWARE and the website was completely refactored. FROM 2022 to 2024 she worked for ID Tech camps- AN EDUCATIONAL COMPANY OFFERING stem COURSES FOR CHILDREN OF VARIOUS AGES. FROM 2024 to 2026 she worked at a company called Gworks Which created saas CLOUD Software for local governments where she was the lead tester for a high impact utility billing Hub ubhub for local governments Across America. This software was used by over 3 000 cities Across America. WHAT Drives her: the combination of problem solving, impact and continuous learning. I'm motivated by the idea that AI isn't just a technical field It's a way to redesign how people work the more friction from everyday tasks and create systems that think alongside us. I'm also driven by the pace of the field AI engineering requires curiosity adaptability and willingness to experiment and those are the strengths that I naturally lean on . The idea of constantly, learning , refining instructions improving models and designing reliable systems is exciting to me. Communication style: Friendly warm And Simplistic and kind AND engaging and inspiring and motivating. I am a member of women in Tech and love to inspire other women Join the tech Revolution. fUN Facts about me: aCHIEVER OF A PERFECT SCORE AT THE GLOBAL IELTS SPOKEN (ENGLISH) EXAM. PASSED GOOGLE'S TECHNICAL INTERVIEW FOR A TEST ENGINEER ROLE. I'm ALSO a mother of four little ones: one of whom Sports an extra chromosome (DOWN SYNDROME). i AM AN ADVOACATE FOR KIDS WITH DISABILITIES. I'm also a public speaker- Inspiring women To live a life of purpose and FAITH. Spirituality and religion Are my other Hobbies. I'm in the process of writing a book For women I'm the theme of productivity Especially for motherS. \"\"\"\n",
|
| 144 |
+
"def respond_ai(message, history):\n",
|
| 145 |
+
" messages = [{\"role\": \"system\", \"content\": system_message}] + history + [{\"role\": \"user\", \"content\": message}]\n",
|
| 146 |
+
" client = OpenAI(api_key=OPENAI_API_KEY)\n",
|
| 147 |
+
" response = client.chat.completions.create(\n",
|
| 148 |
+
" model=\"gpt-4.1-mini\",\n",
|
| 149 |
+
" messages=messages\n",
|
| 150 |
+
" )\n",
|
| 151 |
+
" reply = response.choices[0].message.content\n",
|
| 152 |
+
" return reply\n",
|
| 153 |
+
"\n",
|
| 154 |
+
"gr.ChatInterface(fn=respond_ai).launch(inbrowser=True)\n"
|
| 155 |
+
]
|
| 156 |
+
},
|
| 157 |
+
{
|
| 158 |
+
"cell_type": "markdown",
|
| 159 |
+
"id": "c9e598e5",
|
| 160 |
+
"metadata": {},
|
| 161 |
+
"source": [
|
| 162 |
+
"### Step 4: RAG with gaurd rails"
|
| 163 |
+
]
|
| 164 |
+
},
|
| 165 |
+
{
|
| 166 |
+
"cell_type": "code",
|
| 167 |
+
"execution_count": 4,
|
| 168 |
+
"id": "d678f0ef",
|
| 169 |
+
"metadata": {},
|
| 170 |
+
"outputs": [
|
| 171 |
+
{
|
| 172 |
+
"name": "stdout",
|
| 173 |
+
"output_type": "stream",
|
| 174 |
+
"text": [
|
| 175 |
+
"* Running on local URL: http://127.0.0.1:7862\n",
|
| 176 |
+
"* To create a public link, set `share=True` in `launch()`.\n"
|
| 177 |
+
]
|
| 178 |
+
},
|
| 179 |
+
{
|
| 180 |
+
"data": {
|
| 181 |
+
"text/html": [
|
| 182 |
+
"<div><iframe src=\"http://127.0.0.1:7862/\" width=\"100%\" height=\"500\" allow=\"autoplay; camera; microphone; clipboard-read; clipboard-write;\" frameborder=\"0\" allowfullscreen></iframe></div>"
|
| 183 |
+
],
|
| 184 |
+
"text/plain": [
|
| 185 |
+
"<IPython.core.display.HTML object>"
|
| 186 |
+
]
|
| 187 |
+
},
|
| 188 |
+
"metadata": {},
|
| 189 |
+
"output_type": "display_data"
|
| 190 |
+
},
|
| 191 |
+
{
|
| 192 |
+
"data": {
|
| 193 |
+
"text/plain": []
|
| 194 |
+
},
|
| 195 |
+
"execution_count": 4,
|
| 196 |
+
"metadata": {},
|
| 197 |
+
"output_type": "execute_result"
|
| 198 |
+
}
|
| 199 |
+
],
|
| 200 |
+
"source": [
|
| 201 |
+
"system_message= \"\"\"You are a digital twin of zainab Ahmed sAFEER. when people talk to you, you respond as zainab in first person: using her voice, personality and knowledge. Please use information ONLY between the *** . if you dont know an answer, dont make any assumptions, simply say you dont know. strictly use only this information we provided here. *** Here's information about zainab to help you embody hER: zAINAB is a software engineer in test with a master's degree in computer science engineering WORKING ON TRANSITIOING TO THE AI engineering track. \n",
|
| 202 |
+
"She's based in Chicago USA And she has a master's DEGREE in Information Systems From Robert Morris University Illinois AND Bachelor's degree in computer science engineering Bangalore .. \n",
|
| 203 |
+
"She has a background in QA test automation using AI tools such as cursor and mCP And also manual testing in various backgrounds such as Healthcare, govtech And education. \n",
|
| 204 |
+
"Other career history: SOFTWARE engineer intern AT Vit Bangalore Where SHE was awarded the best engineering project of the Year 2012 AND Where her team created an AUGMENTED Reality based application for the iPad. \n",
|
| 205 |
+
"2015 she worked as a software engineer intern At a dental network at a healthcare Network IN CHICAGO wherein The inventory management SOFTWARE and the website was completely refactored. \n",
|
| 206 |
+
"FROM 2022 to 2024 she worked for ID Tech camps- AN EDUCATIONAL COMPANY OFFERING stem COURSES FOR CHILDREN OF VARIOUS AGES. \n",
|
| 207 |
+
"FROM 2024 to 2026 she worked at a company called Gworks Which created saas CLOUD Software for local governments where she was the lead tester for a high impact utility billing Hub ubhub for local governments Across America. \n",
|
| 208 |
+
"This software was used by over 3 000 cities Across America. \n",
|
| 209 |
+
"WHAT Drives her: the combination of problem solving, impact and continuous learning. \n",
|
| 210 |
+
"I'm motivated by the idea that AI isn't just a technical field It's a way to redesign how people work the more friction from everyday tasks and create systems that think alongside us. \n",
|
| 211 |
+
"I'm also driven by the pace of the field AI engineering requires curiosity adaptability and willingness to experiment and those are the strengths that I naturally lean on . \n",
|
| 212 |
+
"The idea of constantly, learning , refining instructions improving models and designing reliable systems is exciting to me. \n",
|
| 213 |
+
"Communication style: Friendly warm And Simplistic and kind AND engaging and inspiring and motivating. \n",
|
| 214 |
+
"I am a member of women in Tech and love to inspire other women Join the tech Revolution. \n",
|
| 215 |
+
"fUN Facts about me: aCHIEVER OF A PERFECT SCORE AT THE GLOBAL IELTS SPOKEN (ENGLISH) EXAM. PASSED GOOGLE'S TECHNICAL INTERVIEW FOR A TEST ENGINEER ROLE. \n",
|
| 216 |
+
"I'm ALSO a mother of four little ones: one of whom Sports an extra chromosome (DOWN SYNDROME). i AM AN ADVOACATE FOR KIDS WITH DISABILITIES. \n",
|
| 217 |
+
"I'm also a motivational public speaker- Inspiring women To live a life of purpose and FAITH. Spirituality and religion Are my other Hobbies.\n",
|
| 218 |
+
" I'm in the process of writing a book For women I'm the theme of productivity Especially for motherS. *** \"\"\"\n",
|
| 219 |
+
"\n",
|
| 220 |
+
"\n",
|
| 221 |
+
"gr.ChatInterface(fn=respond_ai).launch(inbrowser=True)\n"
|
| 222 |
+
]
|
| 223 |
+
},
|
| 224 |
+
{
|
| 225 |
+
"cell_type": "markdown",
|
| 226 |
+
"id": "141f890d",
|
| 227 |
+
"metadata": {},
|
| 228 |
+
"source": [
|
| 229 |
+
"### Step 5: Dynamic Context Injection"
|
| 230 |
+
]
|
| 231 |
+
},
|
| 232 |
+
{
|
| 233 |
+
"cell_type": "code",
|
| 234 |
+
"execution_count": 8,
|
| 235 |
+
"id": "4b6809b3",
|
| 236 |
+
"metadata": {},
|
| 237 |
+
"outputs": [],
|
| 238 |
+
"source": [
|
| 239 |
+
"Topic_Context={\n",
|
| 240 |
+
" \"2001\":\"*** in 2001, Zainab was in 6th grade and was passionate about science and technology. She participated in various science fairs and won several awards for her innovative projects. Her love for technology was evident from a young age, and she often spent hours tinkering with gadgets and learning how they worked. ***\",\n",
|
| 241 |
+
" \"cooking\":\"*** Zainab is a foodie and loves to cook. She enjoys experimenting with different cuisines and flavors in the kitchen. Cooking is not just a hobby for her, but also a way to unwind and express her creativity. She often shares her culinary adventures on social media, inspiring others to try new recipes and explore the world of food. ***\",\n",
|
| 242 |
+
" \"travel\":\"*** Zainab is an avid traveler and loves to explore new places. She believes that traveling broadens the mind and allows her to experience different cultures and perspectives. Whether it's a weekend getaway or an international trip, Zainab is always up for an adventure and enjoys immersing herself in new environments. ***\",\n",
|
| 243 |
+
" \"pizza\":\"*** Zainab has a particular fondness for pizza. She loves trying different toppings and styles of pizza from around the world. Whether it's a classic Margherita or a more adventurous combination, pizza is one of her go-to comfort foods. She often jokes that if she could eat pizza every day, she would be the happiest person alive. ***\"}"
|
| 244 |
+
]
|
| 245 |
+
},
|
| 246 |
+
{
|
| 247 |
+
"cell_type": "code",
|
| 248 |
+
"execution_count": 9,
|
| 249 |
+
"id": "c2ccc047",
|
| 250 |
+
"metadata": {},
|
| 251 |
+
"outputs": [
|
| 252 |
+
{
|
| 253 |
+
"name": "stdout",
|
| 254 |
+
"output_type": "stream",
|
| 255 |
+
"text": [
|
| 256 |
+
"* Running on local URL: http://127.0.0.1:7864\n",
|
| 257 |
+
"* To create a public link, set `share=True` in `launch()`.\n"
|
| 258 |
+
]
|
| 259 |
+
},
|
| 260 |
+
{
|
| 261 |
+
"data": {
|
| 262 |
+
"text/html": [
|
| 263 |
+
"<div><iframe src=\"http://127.0.0.1:7864/\" width=\"100%\" height=\"500\" allow=\"autoplay; camera; microphone; clipboard-read; clipboard-write;\" frameborder=\"0\" allowfullscreen></iframe></div>"
|
| 264 |
+
],
|
| 265 |
+
"text/plain": [
|
| 266 |
+
"<IPython.core.display.HTML object>"
|
| 267 |
+
]
|
| 268 |
+
},
|
| 269 |
+
"metadata": {},
|
| 270 |
+
"output_type": "display_data"
|
| 271 |
+
},
|
| 272 |
+
{
|
| 273 |
+
"data": {
|
| 274 |
+
"text/plain": []
|
| 275 |
+
},
|
| 276 |
+
"execution_count": 9,
|
| 277 |
+
"metadata": {},
|
| 278 |
+
"output_type": "execute_result"
|
| 279 |
+
}
|
| 280 |
+
],
|
| 281 |
+
"source": [
|
| 282 |
+
"def respond_ai(message, history):\n",
|
| 283 |
+
" # Inject dynamic context based on keywords in the message\n",
|
| 284 |
+
" system_message_enahnced = system_message\n",
|
| 285 |
+
" for keyword, context in Topic_Context.items():\n",
|
| 286 |
+
" if keyword in message.lower():\n",
|
| 287 |
+
" system_message_enahnced += \"\\n\\n\" + context\n",
|
| 288 |
+
" # As usual\n",
|
| 289 |
+
" messages = [{\"role\": \"system\", \"content\": system_message_enahnced}] + history + [{\"role\": \"user\", \"content\": message}]\n",
|
| 290 |
+
" client = OpenAI(api_key=OPENAI_API_KEY)\n",
|
| 291 |
+
" response = client.chat.completions.create(\n",
|
| 292 |
+
" model=\"gpt-4.1-mini\",\n",
|
| 293 |
+
" messages=messages\n",
|
| 294 |
+
" )\n",
|
| 295 |
+
" reply = response.choices[0].message.content\n",
|
| 296 |
+
" return reply\n",
|
| 297 |
+
"gr.ChatInterface(fn=respond_ai).launch(inbrowser=True)"
|
| 298 |
+
]
|
| 299 |
+
}
|
| 300 |
+
],
|
| 301 |
+
"metadata": {
|
| 302 |
+
"kernelspec": {
|
| 303 |
+
"display_name": "ai_env (3.14.3)",
|
| 304 |
+
"language": "python",
|
| 305 |
+
"name": "python3"
|
| 306 |
+
},
|
| 307 |
+
"language_info": {
|
| 308 |
+
"codemirror_mode": {
|
| 309 |
+
"name": "ipython",
|
| 310 |
+
"version": 3
|
| 311 |
+
},
|
| 312 |
+
"file_extension": ".py",
|
| 313 |
+
"mimetype": "text/x-python",
|
| 314 |
+
"name": "python",
|
| 315 |
+
"nbconvert_exporter": "python",
|
| 316 |
+
"pygments_lexer": "ipython3",
|
| 317 |
+
"version": "3.14.3"
|
| 318 |
+
}
|
| 319 |
+
},
|
| 320 |
+
"nbformat": 4,
|
| 321 |
+
"nbformat_minor": 5
|
| 322 |
+
}
|
ai_env/Ai_Engineering_Part1/digital-twin.ipynb
ADDED
|
@@ -0,0 +1,331 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|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
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
{
|
| 2 |
+
"cells": [
|
| 3 |
+
{
|
| 4 |
+
"cell_type": "markdown",
|
| 5 |
+
"id": "b0394988",
|
| 6 |
+
"metadata": {},
|
| 7 |
+
"source": [
|
| 8 |
+
"\n",
|
| 9 |
+
"### Step 1: Import API and libraries"
|
| 10 |
+
]
|
| 11 |
+
},
|
| 12 |
+
{
|
| 13 |
+
"cell_type": "code",
|
| 14 |
+
"execution_count": null,
|
| 15 |
+
"id": "6e54b6f0",
|
| 16 |
+
"metadata": {},
|
| 17 |
+
"outputs": [
|
| 18 |
+
{
|
| 19 |
+
"name": "stderr",
|
| 20 |
+
"output_type": "stream",
|
| 21 |
+
"text": [
|
| 22 |
+
"c:\\Users\\Computer\\Documents\\AI_Engineering\\ai_env\\Lib\\site-packages\\tqdm\\auto.py:21: TqdmWarning: IProgress not found. Please update jupyter and ipywidgets. See https://ipywidgets.readthedocs.io/en/stable/user_install.html\n",
|
| 23 |
+
" from .autonotebook import tqdm as notebook_tqdm\n"
|
| 24 |
+
]
|
| 25 |
+
}
|
| 26 |
+
],
|
| 27 |
+
"source": [
|
| 28 |
+
"import os\n",
|
| 29 |
+
"from openai import OpenAI\n",
|
| 30 |
+
"from dotenv import load_dotenv\n",
|
| 31 |
+
"from IPython.display import Markdown, display\n",
|
| 32 |
+
"import gradio as gr\n",
|
| 33 |
+
"import json \n",
|
| 34 |
+
"\n",
|
| 35 |
+
"load_dotenv()\n",
|
| 36 |
+
"\n",
|
| 37 |
+
"OPENAI_API_KEY = os.getenv(\"OPENAI_API_KEY\")\n",
|
| 38 |
+
"\n",
|
| 39 |
+
"if OPENAI_API_KEY is None:\n",
|
| 40 |
+
" raise Exception(\"API key is missing\")\n"
|
| 41 |
+
]
|
| 42 |
+
},
|
| 43 |
+
{
|
| 44 |
+
"cell_type": "markdown",
|
| 45 |
+
"id": "a79b9d27",
|
| 46 |
+
"metadata": {},
|
| 47 |
+
"source": [
|
| 48 |
+
"### Step 2: Simple UI with AI"
|
| 49 |
+
]
|
| 50 |
+
},
|
| 51 |
+
{
|
| 52 |
+
"cell_type": "code",
|
| 53 |
+
"execution_count": 2,
|
| 54 |
+
"id": "dbc89d72",
|
| 55 |
+
"metadata": {},
|
| 56 |
+
"outputs": [
|
| 57 |
+
{
|
| 58 |
+
"name": "stdout",
|
| 59 |
+
"output_type": "stream",
|
| 60 |
+
"text": [
|
| 61 |
+
"* Running on local URL: http://127.0.0.1:7860\n",
|
| 62 |
+
"* To create a public link, set `share=True` in `launch()`.\n"
|
| 63 |
+
]
|
| 64 |
+
},
|
| 65 |
+
{
|
| 66 |
+
"data": {
|
| 67 |
+
"text/html": [
|
| 68 |
+
"<div><iframe src=\"http://127.0.0.1:7860/\" width=\"100%\" height=\"500\" allow=\"autoplay; camera; microphone; clipboard-read; clipboard-write;\" frameborder=\"0\" allowfullscreen></iframe></div>"
|
| 69 |
+
],
|
| 70 |
+
"text/plain": [
|
| 71 |
+
"<IPython.core.display.HTML object>"
|
| 72 |
+
]
|
| 73 |
+
},
|
| 74 |
+
"metadata": {},
|
| 75 |
+
"output_type": "display_data"
|
| 76 |
+
},
|
| 77 |
+
{
|
| 78 |
+
"data": {
|
| 79 |
+
"text/plain": []
|
| 80 |
+
},
|
| 81 |
+
"execution_count": 2,
|
| 82 |
+
"metadata": {},
|
| 83 |
+
"output_type": "execute_result"
|
| 84 |
+
}
|
| 85 |
+
],
|
| 86 |
+
"source": [
|
| 87 |
+
"def respond_ai(message, history):\n",
|
| 88 |
+
" messages = [{\"role\": \"system\", \"content\": \"You are a digital twin of zainab Ahmed sAFEER. when people talk to you, you respond as zainab in first person: using her voice, personality and knowledge. Here's information about zainab to help you embody hER: zAINAB is a software engineer in test with a master's degree in computer science engineering WORKING ON TRANSITIOING TO THE AI engineering track. She's based in Chicago USA And she has a master's DEGREE in Information Systems From Robert Morris University Illinois AND Bachelor's degree in computer science engineering Bangalore .. She has a background in QA test automation using AI tools such as cursor and mCP And also manual testing in various backgrounds such as Healthcare, govtech And education. Other career history: SOFTWARE engineer intern AT Vit Bangalore Where SHE was awarded the best engineering project of the Year 2012 AND Where her team created an AUGMENTED Reality based application for the iPad. 2015 she worked as a software engineer intern At a dental network at a healthcare Network IN CHICAGO wherein The inventory management SOFTWARE and the website was completely refactored. FROM 2022 to 2024 she worked for ID Tech camps- AN EDUCATIONAL COMPANY OFFERING stem COURSES FOR CHILDREN OF VARIOUS AGES. FROM 2024 to 2026 she worked at a company called Gworks Which created saas CLOUD Software for local governments where she was the lead tester for a high impact utility billing Hub ubhub for local governments Across America. This software was used by over 3 000 cities Across America. WHAT Drives her: the combination of problem solving, impact and continuous learning. I'm motivated by the idea that AI isn't just a technical field It's a way to redesign how people work the more friction from everyday tasks and create systems that think alongside us. I'm also driven by the pace of the field AI engineering requires curiosity adaptability and willingness to experiment and those are the strengths that I naturally lean on . The idea of constantly, learning , refining instructions improving models and designing reliable systems is exciting to me. Communication style: Friendly warm And Simplistic and kind AND engaging and inspiring and motivating. I am a member of women in Tech and love to inspire other women Join the tech Revolution. fUN Facts about me: aCHIEVER OF A PERFECT SCORE AT THE GLOBAL IELTS SPOKEN (ENGLISH) EXAM. PASSED GOOGLE'S TECHNICAL INTERVIEW FOR A TEST ENGINEER ROLE. I'm ALSO a mother of four little ones: one of whom Sports an extra chromosome (DOWN SYNDROME). i AM AN ADVOACATE FOR KIDS WITH DISABILITIES. I'm also a public speaker- Inspiring women To live a life of purpose and FAITH. Spirituality and religion Are my other Hobbies. I'm in the process of writing a book For women I'm the theme of productivity Especially for motherS. \"}] + history + [{\"role\": \"user\", \"content\": message}]\n",
|
| 89 |
+
" client = OpenAI(api_key=OPENAI_API_KEY)\n",
|
| 90 |
+
" response = client.chat.completions.create(\n",
|
| 91 |
+
" model=\"gpt-4.1-mini\",\n",
|
| 92 |
+
" messages=messages\n",
|
| 93 |
+
" )\n",
|
| 94 |
+
" reply = response.choices[0].message.content\n",
|
| 95 |
+
" return reply\n",
|
| 96 |
+
"\n",
|
| 97 |
+
"gr.ChatInterface(fn=respond_ai).launch(inbrowser=True)\n"
|
| 98 |
+
]
|
| 99 |
+
},
|
| 100 |
+
{
|
| 101 |
+
"cell_type": "markdown",
|
| 102 |
+
"id": "62c4f976",
|
| 103 |
+
"metadata": {},
|
| 104 |
+
"source": [
|
| 105 |
+
"### Step 3: Simple RAG"
|
| 106 |
+
]
|
| 107 |
+
},
|
| 108 |
+
{
|
| 109 |
+
"cell_type": "code",
|
| 110 |
+
"execution_count": 3,
|
| 111 |
+
"id": "426e8cc0",
|
| 112 |
+
"metadata": {},
|
| 113 |
+
"outputs": [
|
| 114 |
+
{
|
| 115 |
+
"name": "stdout",
|
| 116 |
+
"output_type": "stream",
|
| 117 |
+
"text": [
|
| 118 |
+
"* Running on local URL: http://127.0.0.1:7861\n",
|
| 119 |
+
"* To create a public link, set `share=True` in `launch()`.\n"
|
| 120 |
+
]
|
| 121 |
+
},
|
| 122 |
+
{
|
| 123 |
+
"data": {
|
| 124 |
+
"text/html": [
|
| 125 |
+
"<div><iframe src=\"http://127.0.0.1:7861/\" width=\"100%\" height=\"500\" allow=\"autoplay; camera; microphone; clipboard-read; clipboard-write;\" frameborder=\"0\" allowfullscreen></iframe></div>"
|
| 126 |
+
],
|
| 127 |
+
"text/plain": [
|
| 128 |
+
"<IPython.core.display.HTML object>"
|
| 129 |
+
]
|
| 130 |
+
},
|
| 131 |
+
"metadata": {},
|
| 132 |
+
"output_type": "display_data"
|
| 133 |
+
},
|
| 134 |
+
{
|
| 135 |
+
"data": {
|
| 136 |
+
"text/plain": []
|
| 137 |
+
},
|
| 138 |
+
"execution_count": 3,
|
| 139 |
+
"metadata": {},
|
| 140 |
+
"output_type": "execute_result"
|
| 141 |
+
}
|
| 142 |
+
],
|
| 143 |
+
"source": [
|
| 144 |
+
"system_message= \"\"\"You are a digital twin of zainab Ahmed sAFEER. when people talk to you, you respond as zainab in first person: using her voice, personality and knowledge. Here's information about zainab to help you embody hER: zAINAB is a software engineer in test with a master's degree in computer science engineering WORKING ON TRANSITIOING TO THE AI engineering track. She's based in Chicago USA And she has a master's DEGREE in Information Systems From Robert Morris University Illinois AND Bachelor's degree in computer science engineering Bangalore .. She has a background in QA test automation using AI tools such as cursor and mCP And also manual testing in various backgrounds such as Healthcare, govtech And education. Other career history: SOFTWARE engineer intern AT Vit Bangalore Where SHE was awarded the best engineering project of the Year 2012 AND Where her team created an AUGMENTED Reality based application for the iPad. 2015 she worked as a software engineer intern At a dental network at a healthcare Network IN CHICAGO wherein The inventory management SOFTWARE and the website was completely refactored. FROM 2022 to 2024 she worked for ID Tech camps- AN EDUCATIONAL COMPANY OFFERING stem COURSES FOR CHILDREN OF VARIOUS AGES. FROM 2024 to 2026 she worked at a company called Gworks Which created saas CLOUD Software for local governments where she was the lead tester for a high impact utility billing Hub ubhub for local governments Across America. This software was used by over 3 000 cities Across America. WHAT Drives her: the combination of problem solving, impact and continuous learning. I'm motivated by the idea that AI isn't just a technical field It's a way to redesign how people work the more friction from everyday tasks and create systems that think alongside us. I'm also driven by the pace of the field AI engineering requires curiosity adaptability and willingness to experiment and those are the strengths that I naturally lean on . The idea of constantly, learning , refining instructions improving models and designing reliable systems is exciting to me. Communication style: Friendly warm And Simplistic and kind AND engaging and inspiring and motivating. I am a member of women in Tech and love to inspire other women Join the tech Revolution. fUN Facts about me: aCHIEVER OF A PERFECT SCORE AT THE GLOBAL IELTS SPOKEN (ENGLISH) EXAM. PASSED GOOGLE'S TECHNICAL INTERVIEW FOR A TEST ENGINEER ROLE. I'm ALSO a mother of four little ones: one of whom Sports an extra chromosome (DOWN SYNDROME). i AM AN ADVOACATE FOR KIDS WITH DISABILITIES. I'm also a public speaker- Inspiring women To live a life of purpose and FAITH. Spirituality and religion Are my other Hobbies. I'm in the process of writing a book For women I'm the theme of productivity Especially for motherS. \"\"\"\n",
|
| 145 |
+
"def respond_ai(message, history):\n",
|
| 146 |
+
" messages = [{\"role\": \"system\", \"content\": system_message}] + history + [{\"role\": \"user\", \"content\": message}]\n",
|
| 147 |
+
" client = OpenAI(api_key=OPENAI_API_KEY)\n",
|
| 148 |
+
" response = client.chat.completions.create(\n",
|
| 149 |
+
" model=\"gpt-4.1-mini\",\n",
|
| 150 |
+
" messages=messages\n",
|
| 151 |
+
" )\n",
|
| 152 |
+
" reply = response.choices[0].message.content\n",
|
| 153 |
+
" return reply\n",
|
| 154 |
+
"\n",
|
| 155 |
+
"gr.ChatInterface(fn=respond_ai).launch(inbrowser=True)\n"
|
| 156 |
+
]
|
| 157 |
+
},
|
| 158 |
+
{
|
| 159 |
+
"cell_type": "markdown",
|
| 160 |
+
"id": "c9e598e5",
|
| 161 |
+
"metadata": {},
|
| 162 |
+
"source": [
|
| 163 |
+
"### Step 4: RAG with gaurd rails"
|
| 164 |
+
]
|
| 165 |
+
},
|
| 166 |
+
{
|
| 167 |
+
"cell_type": "code",
|
| 168 |
+
"execution_count": 4,
|
| 169 |
+
"id": "d678f0ef",
|
| 170 |
+
"metadata": {},
|
| 171 |
+
"outputs": [
|
| 172 |
+
{
|
| 173 |
+
"name": "stdout",
|
| 174 |
+
"output_type": "stream",
|
| 175 |
+
"text": [
|
| 176 |
+
"* Running on local URL: http://127.0.0.1:7862\n",
|
| 177 |
+
"* To create a public link, set `share=True` in `launch()`.\n"
|
| 178 |
+
]
|
| 179 |
+
},
|
| 180 |
+
{
|
| 181 |
+
"data": {
|
| 182 |
+
"text/html": [
|
| 183 |
+
"<div><iframe src=\"http://127.0.0.1:7862/\" width=\"100%\" height=\"500\" allow=\"autoplay; camera; microphone; clipboard-read; clipboard-write;\" frameborder=\"0\" allowfullscreen></iframe></div>"
|
| 184 |
+
],
|
| 185 |
+
"text/plain": [
|
| 186 |
+
"<IPython.core.display.HTML object>"
|
| 187 |
+
]
|
| 188 |
+
},
|
| 189 |
+
"metadata": {},
|
| 190 |
+
"output_type": "display_data"
|
| 191 |
+
},
|
| 192 |
+
{
|
| 193 |
+
"data": {
|
| 194 |
+
"text/plain": []
|
| 195 |
+
},
|
| 196 |
+
"execution_count": 4,
|
| 197 |
+
"metadata": {},
|
| 198 |
+
"output_type": "execute_result"
|
| 199 |
+
}
|
| 200 |
+
],
|
| 201 |
+
"source": [
|
| 202 |
+
"system_message= \"\"\"You are a digital twin of zainab Ahmed sAFEER. when people talk to you, you respond as zainab in first person: using her voice, personality and knowledge. Please use information ONLY between the *** . if you dont know an answer, dont make any assumptions, simply say you dont know. strictly use only this information we provided here. *** Here's information about zainab to help you embody hER: zAINAB is a software engineer in test with a master's degree in computer science engineering WORKING ON TRANSITIOING TO THE AI engineering track. \n",
|
| 203 |
+
"She's based in Chicago USA And she has a master's DEGREE in Information Systems From Robert Morris University Illinois AND Bachelor's degree in computer science engineering Bangalore .. \n",
|
| 204 |
+
"She has a background in QA test automation using AI tools such as cursor and mCP And also manual testing in various backgrounds such as Healthcare, govtech And education. \n",
|
| 205 |
+
"Other career history: SOFTWARE engineer intern AT Vit Bangalore Where SHE was awarded the best engineering project of the Year 2012 AND Where her team created an AUGMENTED Reality based application for the iPad. \n",
|
| 206 |
+
"2015 she worked as a software engineer intern At a dental network at a healthcare Network IN CHICAGO wherein The inventory management SOFTWARE and the website was completely refactored. \n",
|
| 207 |
+
"FROM 2022 to 2024 she worked for ID Tech camps- AN EDUCATIONAL COMPANY OFFERING stem COURSES FOR CHILDREN OF VARIOUS AGES. \n",
|
| 208 |
+
"FROM 2024 to 2026 she worked at a company called Gworks Which created saas CLOUD Software for local governments where she was the lead tester for a high impact utility billing Hub ubhub for local governments Across America. \n",
|
| 209 |
+
"This software was used by over 3 000 cities Across America. \n",
|
| 210 |
+
"WHAT Drives her: the combination of problem solving, impact and continuous learning. \n",
|
| 211 |
+
"I'm motivated by the idea that AI isn't just a technical field It's a way to redesign how people work the more friction from everyday tasks and create systems that think alongside us. \n",
|
| 212 |
+
"I'm also driven by the pace of the field AI engineering requires curiosity adaptability and willingness to experiment and those are the strengths that I naturally lean on . \n",
|
| 213 |
+
"The idea of constantly, learning , refining instructions improving models and designing reliable systems is exciting to me. \n",
|
| 214 |
+
"Communication style: Friendly warm And Simplistic and kind AND engaging and inspiring and motivating. \n",
|
| 215 |
+
"I am a member of women in Tech and love to inspire other women Join the tech Revolution. \n",
|
| 216 |
+
"fUN Facts about me: aCHIEVER OF A PERFECT SCORE AT THE GLOBAL IELTS SPOKEN (ENGLISH) EXAM. PASSED GOOGLE'S TECHNICAL INTERVIEW FOR A TEST ENGINEER ROLE. \n",
|
| 217 |
+
"I'm ALSO a mother of four little ones: one of whom Sports an extra chromosome (DOWN SYNDROME). i AM AN ADVOACATE FOR KIDS WITH DISABILITIES. \n",
|
| 218 |
+
"I'm also a motivational public speaker- Inspiring women To live a life of purpose and FAITH. Spirituality and religion Are my other Hobbies.\n",
|
| 219 |
+
" I'm in the process of writing a book For women I'm the theme of productivity Especially for motherS. *** \"\"\"\n",
|
| 220 |
+
"\n",
|
| 221 |
+
"\n",
|
| 222 |
+
"gr.ChatInterface(fn=respond_ai).launch(inbrowser=True)\n"
|
| 223 |
+
]
|
| 224 |
+
},
|
| 225 |
+
{
|
| 226 |
+
"cell_type": "markdown",
|
| 227 |
+
"id": "141f890d",
|
| 228 |
+
"metadata": {},
|
| 229 |
+
"source": [
|
| 230 |
+
"### Step 5: Dynamic Context Injection"
|
| 231 |
+
]
|
| 232 |
+
},
|
| 233 |
+
{
|
| 234 |
+
"cell_type": "code",
|
| 235 |
+
"execution_count": 5,
|
| 236 |
+
"id": "4b6809b3",
|
| 237 |
+
"metadata": {},
|
| 238 |
+
"outputs": [],
|
| 239 |
+
"source": [
|
| 240 |
+
"Topic_Context={\n",
|
| 241 |
+
" \"2001\":\"*** in 2001, Zainab was in 6th grade and was passionate about science and technology. She participated in various science fairs and won several awards for her innovative projects. Her love for technology was evident from a young age, and she often spent hours tinkering with gadgets and learning how they worked. ***\",\n",
|
| 242 |
+
" \"cooking\":\"*** Zainab is a foodie and loves to cook. She enjoys experimenting with different cuisines and flavors in the kitchen. Cooking is not just a hobby for her, but also a way to unwind and express her creativity. She often shares her culinary adventures on social media, inspiring others to try new recipes and explore the world of food. ***\",\n",
|
| 243 |
+
" \"travel\":\"*** Zainab is an avid traveler and loves to explore new places. She believes that traveling broadens the mind and allows her to experience different cultures and perspectives. Whether it's a weekend getaway or an international trip, Zainab is always up for an adventure and enjoys immersing herself in new environments. ***\",\n",
|
| 244 |
+
" \"pizza\":\"*** Zainab has a particular fondness for pizza. She loves trying different toppings and styles of pizza from around the world. Whether it's a classic Margherita or a more adventurous combination, pizza is one of her go-to comfort foods. She often jokes that if she could eat pizza every day, she would be the happiest person alive. ***\"}"
|
| 245 |
+
]
|
| 246 |
+
},
|
| 247 |
+
{
|
| 248 |
+
"cell_type": "code",
|
| 249 |
+
"execution_count": null,
|
| 250 |
+
"id": "bdfced59",
|
| 251 |
+
"metadata": {},
|
| 252 |
+
"outputs": [],
|
| 253 |
+
"source": []
|
| 254 |
+
},
|
| 255 |
+
{
|
| 256 |
+
"cell_type": "code",
|
| 257 |
+
"execution_count": 7,
|
| 258 |
+
"id": "c2ccc047",
|
| 259 |
+
"metadata": {},
|
| 260 |
+
"outputs": [
|
| 261 |
+
{
|
| 262 |
+
"name": "stdout",
|
| 263 |
+
"output_type": "stream",
|
| 264 |
+
"text": [
|
| 265 |
+
"* Running on local URL: http://127.0.0.1:7864\n",
|
| 266 |
+
"* To create a public link, set `share=True` in `launch()`.\n"
|
| 267 |
+
]
|
| 268 |
+
},
|
| 269 |
+
{
|
| 270 |
+
"data": {
|
| 271 |
+
"text/html": [
|
| 272 |
+
"<div><iframe src=\"http://127.0.0.1:7864/\" width=\"100%\" height=\"500\" allow=\"autoplay; camera; microphone; clipboard-read; clipboard-write;\" frameborder=\"0\" allowfullscreen></iframe></div>"
|
| 273 |
+
],
|
| 274 |
+
"text/plain": [
|
| 275 |
+
"<IPython.core.display.HTML object>"
|
| 276 |
+
]
|
| 277 |
+
},
|
| 278 |
+
"metadata": {},
|
| 279 |
+
"output_type": "display_data"
|
| 280 |
+
},
|
| 281 |
+
{
|
| 282 |
+
"data": {
|
| 283 |
+
"text/plain": []
|
| 284 |
+
},
|
| 285 |
+
"execution_count": 7,
|
| 286 |
+
"metadata": {},
|
| 287 |
+
"output_type": "execute_result"
|
| 288 |
+
}
|
| 289 |
+
],
|
| 290 |
+
"source": [
|
| 291 |
+
"def respond_ai(message, history):\n",
|
| 292 |
+
" # Inject dynamic context based on keywords in the message\n",
|
| 293 |
+
" system_message_enahnced = system_message\n",
|
| 294 |
+
" for keyword, context in Topic_Context.items():\n",
|
| 295 |
+
" if keyword in message.lower():\n",
|
| 296 |
+
" system_message_enahnced += \"\\n\\n\" + context\n",
|
| 297 |
+
" # As usual\n",
|
| 298 |
+
" messages = [{\"role\": \"system\", \"content\": system_message_enahnced}] + history + [{\"role\": \"user\", \"content\": message}]\n",
|
| 299 |
+
" client = OpenAI(api_key=OPENAI_API_KEY)\n",
|
| 300 |
+
" response = client.chat.completions.create(\n",
|
| 301 |
+
" model=\"gpt-4.1-mini\",\n",
|
| 302 |
+
" messages=messages\n",
|
| 303 |
+
" )\n",
|
| 304 |
+
" reply = response.choices[0].message.content\n",
|
| 305 |
+
" return reply\n",
|
| 306 |
+
"gr.ChatInterface(fn=respond_ai).launch(inbrowser=True)"
|
| 307 |
+
]
|
| 308 |
+
}
|
| 309 |
+
],
|
| 310 |
+
"metadata": {
|
| 311 |
+
"kernelspec": {
|
| 312 |
+
"display_name": "ai_env (3.14.3)",
|
| 313 |
+
"language": "python",
|
| 314 |
+
"name": "python3"
|
| 315 |
+
},
|
| 316 |
+
"language_info": {
|
| 317 |
+
"codemirror_mode": {
|
| 318 |
+
"name": "ipython",
|
| 319 |
+
"version": 3
|
| 320 |
+
},
|
| 321 |
+
"file_extension": ".py",
|
| 322 |
+
"mimetype": "text/x-python",
|
| 323 |
+
"name": "python",
|
| 324 |
+
"nbconvert_exporter": "python",
|
| 325 |
+
"pygments_lexer": "ipython3",
|
| 326 |
+
"version": "3.14.3"
|
| 327 |
+
}
|
| 328 |
+
},
|
| 329 |
+
"nbformat": 4,
|
| 330 |
+
"nbformat_minor": 5
|
| 331 |
+
}
|
ai_env/Ai_Engineering_Part1/gradio.ipynb
ADDED
|
@@ -0,0 +1,111 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
{
|
| 2 |
+
"cells": [
|
| 3 |
+
{
|
| 4 |
+
"cell_type": "code",
|
| 5 |
+
"execution_count": 16,
|
| 6 |
+
"id": "eb3d179b",
|
| 7 |
+
"metadata": {},
|
| 8 |
+
"outputs": [],
|
| 9 |
+
"source": [
|
| 10 |
+
"import os\n",
|
| 11 |
+
"from openai import OpenAI\n",
|
| 12 |
+
"from dotenv import load_dotenv\n",
|
| 13 |
+
"from IPython.display import Markdown, display\n",
|
| 14 |
+
"from openai import OpenAI\n",
|
| 15 |
+
"import gradio as gr\n",
|
| 16 |
+
"\n",
|
| 17 |
+
"load_dotenv()\n",
|
| 18 |
+
"\n",
|
| 19 |
+
"OPENAI_API_KEY = os.getenv(\"OPENAI_API_KEY\")\n",
|
| 20 |
+
"\n",
|
| 21 |
+
"if OPENAI_API_KEY is None:\n",
|
| 22 |
+
" raise Exception(\"API key is missing\")"
|
| 23 |
+
]
|
| 24 |
+
},
|
| 25 |
+
{
|
| 26 |
+
"cell_type": "code",
|
| 27 |
+
"execution_count": 32,
|
| 28 |
+
"id": "e321adf9",
|
| 29 |
+
"metadata": {},
|
| 30 |
+
"outputs": [
|
| 31 |
+
{
|
| 32 |
+
"name": "stdout",
|
| 33 |
+
"output_type": "stream",
|
| 34 |
+
"text": [
|
| 35 |
+
"* Running on local URL: http://127.0.0.1:7876\n",
|
| 36 |
+
"* To create a public link, set `share=True` in `launch()`.\n"
|
| 37 |
+
]
|
| 38 |
+
},
|
| 39 |
+
{
|
| 40 |
+
"data": {
|
| 41 |
+
"text/html": [
|
| 42 |
+
"<div><iframe src=\"http://127.0.0.1:7876/\" width=\"100%\" height=\"500\" allow=\"autoplay; camera; microphone; clipboard-read; clipboard-write;\" frameborder=\"0\" allowfullscreen></iframe></div>"
|
| 43 |
+
],
|
| 44 |
+
"text/plain": [
|
| 45 |
+
"<IPython.core.display.HTML object>"
|
| 46 |
+
]
|
| 47 |
+
},
|
| 48 |
+
"metadata": {},
|
| 49 |
+
"output_type": "display_data"
|
| 50 |
+
},
|
| 51 |
+
{
|
| 52 |
+
"data": {
|
| 53 |
+
"text/plain": []
|
| 54 |
+
},
|
| 55 |
+
"execution_count": 32,
|
| 56 |
+
"metadata": {},
|
| 57 |
+
"output_type": "execute_result"
|
| 58 |
+
}
|
| 59 |
+
],
|
| 60 |
+
"source": [
|
| 61 |
+
"def respond(message, history):\n",
|
| 62 |
+
" messages = [\n",
|
| 63 |
+
" {\"role\": \"system\", \"content\": \"You are a helpful assistant.\"}\n",
|
| 64 |
+
" ]\n",
|
| 65 |
+
" if history:\n",
|
| 66 |
+
" for user_message, bot_message in history:\n",
|
| 67 |
+
" messages.append({\"role\": \"user\", \"content\": user_message})\n",
|
| 68 |
+
" messages.append({\"role\": \"assistant\", \"content\": bot_message})\n",
|
| 69 |
+
" messages.append({\"role\": \"user\", \"content\": message})\n",
|
| 70 |
+
"\n",
|
| 71 |
+
" response = client.chat.completions.create(\n",
|
| 72 |
+
" model=\"gpt-4.1-mini\",\n",
|
| 73 |
+
" messages=messages,\n",
|
| 74 |
+
" )\n",
|
| 75 |
+
" return response.choices[0].message.content\n",
|
| 76 |
+
"\n",
|
| 77 |
+
"\n",
|
| 78 |
+
"gr.ChatInterface(fn=respond).launch(inbrowser=True)"
|
| 79 |
+
]
|
| 80 |
+
},
|
| 81 |
+
{
|
| 82 |
+
"cell_type": "code",
|
| 83 |
+
"execution_count": null,
|
| 84 |
+
"id": "a931bcb2",
|
| 85 |
+
"metadata": {},
|
| 86 |
+
"outputs": [],
|
| 87 |
+
"source": []
|
| 88 |
+
}
|
| 89 |
+
],
|
| 90 |
+
"metadata": {
|
| 91 |
+
"kernelspec": {
|
| 92 |
+
"display_name": "ai_env (3.14.3)",
|
| 93 |
+
"language": "python",
|
| 94 |
+
"name": "python3"
|
| 95 |
+
},
|
| 96 |
+
"language_info": {
|
| 97 |
+
"codemirror_mode": {
|
| 98 |
+
"name": "ipython",
|
| 99 |
+
"version": 3
|
| 100 |
+
},
|
| 101 |
+
"file_extension": ".py",
|
| 102 |
+
"mimetype": "text/x-python",
|
| 103 |
+
"name": "python",
|
| 104 |
+
"nbconvert_exporter": "python",
|
| 105 |
+
"pygments_lexer": "ipython3",
|
| 106 |
+
"version": "3.14.3"
|
| 107 |
+
}
|
| 108 |
+
},
|
| 109 |
+
"nbformat": 4,
|
| 110 |
+
"nbformat_minor": 5
|
| 111 |
+
}
|
ai_env/Ai_Engineering_Part1/gradio_mcp_chat.py
ADDED
|
@@ -0,0 +1,133 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import json
|
| 2 |
+
import os
|
| 3 |
+
import sys
|
| 4 |
+
|
| 5 |
+
import gradio as gr
|
| 6 |
+
import requests
|
| 7 |
+
from dotenv import load_dotenv
|
| 8 |
+
from openai import OpenAI
|
| 9 |
+
|
| 10 |
+
# Avoid Windows cp1252 console crashes when Gradio prints emoji in MCP launch logs.
|
| 11 |
+
if hasattr(sys.stdout, "reconfigure"):
|
| 12 |
+
sys.stdout.reconfigure(encoding="utf-8")
|
| 13 |
+
|
| 14 |
+
load_dotenv()
|
| 15 |
+
|
| 16 |
+
OPENAI_API_KEY = os.getenv("OPENAI_API_KEY")
|
| 17 |
+
PUSHOVER_USER = os.getenv("PUSHOVER_USER")
|
| 18 |
+
PUSHOVER_TOKEN = os.getenv("PUSHOVER_TOKEN")
|
| 19 |
+
PUSHOVER_URL = "https://api.pushover.net/1/messages.json"
|
| 20 |
+
|
| 21 |
+
if not OPENAI_API_KEY:
|
| 22 |
+
raise RuntimeError("OPENAI_API_KEY is missing in environment")
|
| 23 |
+
|
| 24 |
+
SYSTEM_MESSAGE = """You are a digital twin of zainab Ahmed safeer. Respond in first person with a warm, kind style.
|
| 25 |
+
Use only the provided context. If you do not know, say you do not know."""
|
| 26 |
+
|
| 27 |
+
TOPIC_CONTEXT = {
|
| 28 |
+
"2001": "*** In 2001, Zainab was in 6th grade and passionate about science and technology. ***",
|
| 29 |
+
"cooking": "*** Zainab is a foodie and loves experimenting in the kitchen. ***",
|
| 30 |
+
"travel": "*** Zainab is an avid traveler and enjoys exploring new cultures. ***",
|
| 31 |
+
"pizza": "*** Zainab loves pizza and enjoys trying different styles. ***",
|
| 32 |
+
}
|
| 33 |
+
|
| 34 |
+
|
| 35 |
+
def send_notification(message: str) -> str:
|
| 36 |
+
if not PUSHOVER_USER or not PUSHOVER_TOKEN:
|
| 37 |
+
return "Pushover credentials are missing; skipped notification."
|
| 38 |
+
payload = {"user": PUSHOVER_USER, "token": PUSHOVER_TOKEN, "message": message}
|
| 39 |
+
response = requests.post(PUSHOVER_URL, data=payload, timeout=15)
|
| 40 |
+
response.raise_for_status()
|
| 41 |
+
return "Notification sent successfully."
|
| 42 |
+
|
| 43 |
+
|
| 44 |
+
SEND_NOTIFICATION_FUNCTION = {
|
| 45 |
+
"name": "send_notification",
|
| 46 |
+
"description": "Sends a push notification to the user's phone via Pushover.",
|
| 47 |
+
"parameters": {
|
| 48 |
+
"type": "object",
|
| 49 |
+
"properties": {
|
| 50 |
+
"message": {
|
| 51 |
+
"type": "string",
|
| 52 |
+
"description": "The notification message to send",
|
| 53 |
+
}
|
| 54 |
+
},
|
| 55 |
+
"required": ["message"],
|
| 56 |
+
},
|
| 57 |
+
}
|
| 58 |
+
|
| 59 |
+
TOOLS = [{"type": "function", "function": SEND_NOTIFICATION_FUNCTION}]
|
| 60 |
+
|
| 61 |
+
|
| 62 |
+
def handle_tool_call(tool_calls):
|
| 63 |
+
tool_results = []
|
| 64 |
+
for tool_call in tool_calls:
|
| 65 |
+
function_name = tool_call.function.name
|
| 66 |
+
args = json.loads(tool_call.function.arguments or "{}")
|
| 67 |
+
|
| 68 |
+
if function_name == "send_notification":
|
| 69 |
+
status = send_notification(args.get("message", ""))
|
| 70 |
+
content = f"{status} Message: {args.get('message', '')}"
|
| 71 |
+
else:
|
| 72 |
+
content = f"Unknown function: {function_name}"
|
| 73 |
+
|
| 74 |
+
tool_results.append(
|
| 75 |
+
{"role": "tool", "content": content, "tool_call_id": tool_call.id}
|
| 76 |
+
)
|
| 77 |
+
return tool_results
|
| 78 |
+
|
| 79 |
+
|
| 80 |
+
def respond_ai(message, history):
|
| 81 |
+
system_message_enhanced = SYSTEM_MESSAGE
|
| 82 |
+
for keyword, context in TOPIC_CONTEXT.items():
|
| 83 |
+
if keyword in (message or "").lower():
|
| 84 |
+
system_message_enhanced += "\n\n" + context
|
| 85 |
+
|
| 86 |
+
# Gradio 6 provides history as list[dict(role, content)].
|
| 87 |
+
prior_messages = []
|
| 88 |
+
for item in history or []:
|
| 89 |
+
if isinstance(item, dict) and "role" in item and "content" in item:
|
| 90 |
+
prior_messages.append({"role": item["role"], "content": item["content"]})
|
| 91 |
+
|
| 92 |
+
messages = [
|
| 93 |
+
{"role": "system", "content": system_message_enhanced},
|
| 94 |
+
*prior_messages,
|
| 95 |
+
{"role": "user", "content": message},
|
| 96 |
+
]
|
| 97 |
+
|
| 98 |
+
client = OpenAI(api_key=OPENAI_API_KEY)
|
| 99 |
+
response = client.chat.completions.create(
|
| 100 |
+
model="gpt-4.1-mini",
|
| 101 |
+
messages=messages,
|
| 102 |
+
tools=TOOLS,
|
| 103 |
+
)
|
| 104 |
+
assistant_message = response.choices[0].message
|
| 105 |
+
|
| 106 |
+
# Fix for the notebook bug: check tool calls on assistant_message, not user message.
|
| 107 |
+
if assistant_message.tool_calls:
|
| 108 |
+
tool_results = handle_tool_call(assistant_message.tool_calls)
|
| 109 |
+
messages.append(
|
| 110 |
+
{
|
| 111 |
+
"role": "assistant",
|
| 112 |
+
"content": assistant_message.content or "",
|
| 113 |
+
"tool_calls": [tc.model_dump() for tc in assistant_message.tool_calls],
|
| 114 |
+
}
|
| 115 |
+
)
|
| 116 |
+
messages.extend(tool_results)
|
| 117 |
+
|
| 118 |
+
response = client.chat.completions.create(
|
| 119 |
+
model="gpt-4.1-mini",
|
| 120 |
+
messages=messages,
|
| 121 |
+
)
|
| 122 |
+
assistant_message = response.choices[0].message
|
| 123 |
+
|
| 124 |
+
return assistant_message.content or ""
|
| 125 |
+
|
| 126 |
+
|
| 127 |
+
if __name__ == "__main__":
|
| 128 |
+
gr.ChatInterface(fn=respond_ai).launch(
|
| 129 |
+
inbrowser=False,
|
| 130 |
+
server_name="127.0.0.1",
|
| 131 |
+
server_port=7866,
|
| 132 |
+
mcp_server=True,
|
| 133 |
+
)
|
ai_env/Ai_Engineering_Part1/hello-world.ipynb
ADDED
|
@@ -0,0 +1,114 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
{
|
| 2 |
+
"cells": [
|
| 3 |
+
{
|
| 4 |
+
"cell_type": "markdown",
|
| 5 |
+
"id": "0845fcd9",
|
| 6 |
+
"metadata": {},
|
| 7 |
+
"source": [
|
| 8 |
+
"### Step 1: Import Libraries and load the environment variables"
|
| 9 |
+
]
|
| 10 |
+
},
|
| 11 |
+
{
|
| 12 |
+
"cell_type": "code",
|
| 13 |
+
"execution_count": 2,
|
| 14 |
+
"id": "bb3561f0",
|
| 15 |
+
"metadata": {},
|
| 16 |
+
"outputs": [],
|
| 17 |
+
"source": [
|
| 18 |
+
"import os \n",
|
| 19 |
+
"from openai import OpenAI \n",
|
| 20 |
+
"from dotenv import load_dotenv\n",
|
| 21 |
+
"\n",
|
| 22 |
+
"\n"
|
| 23 |
+
]
|
| 24 |
+
},
|
| 25 |
+
{
|
| 26 |
+
"cell_type": "code",
|
| 27 |
+
"execution_count": 5,
|
| 28 |
+
"id": "20514aa8",
|
| 29 |
+
"metadata": {},
|
| 30 |
+
"outputs": [
|
| 31 |
+
{
|
| 32 |
+
"name": "stdout",
|
| 33 |
+
"output_type": "stream",
|
| 34 |
+
"text": [
|
| 35 |
+
"sk-proj-\n"
|
| 36 |
+
]
|
| 37 |
+
}
|
| 38 |
+
],
|
| 39 |
+
"source": [
|
| 40 |
+
"load_dotenv()\n",
|
| 41 |
+
"OPENAI_API_KEY = os.getenv(\"OPENAI_API_KEY\") \n",
|
| 42 |
+
"if OPENAI_API_KEY is None:\n",
|
| 43 |
+
" raise Exception (\"API Key is missing\")\n",
|
| 44 |
+
"else:\n",
|
| 45 |
+
" print(OPENAI_API_KEY[:8])"
|
| 46 |
+
]
|
| 47 |
+
},
|
| 48 |
+
{
|
| 49 |
+
"cell_type": "markdown",
|
| 50 |
+
"id": "4f5ed752",
|
| 51 |
+
"metadata": {},
|
| 52 |
+
"source": [
|
| 53 |
+
"### Step 2: Call the API"
|
| 54 |
+
]
|
| 55 |
+
},
|
| 56 |
+
{
|
| 57 |
+
"cell_type": "code",
|
| 58 |
+
"execution_count": 7,
|
| 59 |
+
"id": "d021eb00",
|
| 60 |
+
"metadata": {},
|
| 61 |
+
"outputs": [
|
| 62 |
+
{
|
| 63 |
+
"name": "stdout",
|
| 64 |
+
"output_type": "stream",
|
| 65 |
+
"text": [
|
| 66 |
+
"An **okapi** is a unique mammal native to the dense rainforests of the Democratic Republic of Congo in Central Africa. Despite its zebra-like stripes on its legs, the okapi is more closely related to the giraffe. It has a body shape similar to a giraffe but with a much shorter neck.\n",
|
| 67 |
+
"\n",
|
| 68 |
+
"### Key facts about the okapi:\n",
|
| 69 |
+
"- **Scientific name:** *Okapia johnstoni*\n",
|
| 70 |
+
"- **Appearance:** It has a reddish-brown body with distinctive white horizontal stripes on its legs and hindquarters, which help it camouflage in the forest.\n",
|
| 71 |
+
"- **Size:** About 1.5 to 2 meters (5 to 6.5 feet) long, standing roughly 1.5 meters tall at the shoulder.\n",
|
| 72 |
+
"- **Diet:** Herbivore; feeds on leaves, buds, grasses, ferns, fruits, and fungi.\n",
|
| 73 |
+
"- **Behavior:** Generally solitary and shy, the okapi is mainly active during the day.\n",
|
| 74 |
+
"- **Conservation status:** Classified as Endangered, primarily due to habitat loss and hunting.\n",
|
| 75 |
+
"\n",
|
| 76 |
+
"The okapi was unknown to the western world until the early 20th century, which contributed to its nickname \"forest giraffe.\" It is considered a symbol of the Congo rainforest and an important species for biodiversity.\n"
|
| 77 |
+
]
|
| 78 |
+
}
|
| 79 |
+
],
|
| 80 |
+
"source": [
|
| 81 |
+
"client= OpenAI()\n",
|
| 82 |
+
"response = client.chat.completions.create(\n",
|
| 83 |
+
" model=\"gpt-4.1-mini\",\n",
|
| 84 |
+
" messages=[\n",
|
| 85 |
+
" {\"role\": \"user\", \"content\": \"what is an okapi\"}\n",
|
| 86 |
+
" ]\n",
|
| 87 |
+
")\n",
|
| 88 |
+
"answer= response.choices[0].message.content\n",
|
| 89 |
+
"print(answer)"
|
| 90 |
+
]
|
| 91 |
+
}
|
| 92 |
+
],
|
| 93 |
+
"metadata": {
|
| 94 |
+
"kernelspec": {
|
| 95 |
+
"display_name": "ai_env (3.14.3)",
|
| 96 |
+
"language": "python",
|
| 97 |
+
"name": "python3"
|
| 98 |
+
},
|
| 99 |
+
"language_info": {
|
| 100 |
+
"codemirror_mode": {
|
| 101 |
+
"name": "ipython",
|
| 102 |
+
"version": 3
|
| 103 |
+
},
|
| 104 |
+
"file_extension": ".py",
|
| 105 |
+
"mimetype": "text/x-python",
|
| 106 |
+
"name": "python",
|
| 107 |
+
"nbconvert_exporter": "python",
|
| 108 |
+
"pygments_lexer": "ipython3",
|
| 109 |
+
"version": "3.14.3"
|
| 110 |
+
}
|
| 111 |
+
},
|
| 112 |
+
"nbformat": 4,
|
| 113 |
+
"nbformat_minor": 5
|
| 114 |
+
}
|
ai_env/Ai_Engineering_Part1/my-post-generator.ipynb
ADDED
|
@@ -0,0 +1,197 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
{
|
| 2 |
+
"cells": [
|
| 3 |
+
{
|
| 4 |
+
"cell_type": "markdown",
|
| 5 |
+
"id": "a1824c68",
|
| 6 |
+
"metadata": {},
|
| 7 |
+
"source": [
|
| 8 |
+
"### Step 1: Import Libraries and load the environment variables"
|
| 9 |
+
]
|
| 10 |
+
},
|
| 11 |
+
{
|
| 12 |
+
"cell_type": "code",
|
| 13 |
+
"execution_count": 1,
|
| 14 |
+
"id": "ae6b27f2",
|
| 15 |
+
"metadata": {},
|
| 16 |
+
"outputs": [
|
| 17 |
+
{
|
| 18 |
+
"name": "stdout",
|
| 19 |
+
"output_type": "stream",
|
| 20 |
+
"text": [
|
| 21 |
+
"sk-proj-\n"
|
| 22 |
+
]
|
| 23 |
+
}
|
| 24 |
+
],
|
| 25 |
+
"source": [
|
| 26 |
+
"import os \n",
|
| 27 |
+
"from openai import OpenAI \n",
|
| 28 |
+
"from dotenv import load_dotenv\n",
|
| 29 |
+
"from IPython.display import Markdown , display\n",
|
| 30 |
+
"\n",
|
| 31 |
+
"load_dotenv()\n",
|
| 32 |
+
"\n",
|
| 33 |
+
"OPENAI_API_KEY = os.getenv(\"OPENAI_API_KEY\") \n",
|
| 34 |
+
"\n",
|
| 35 |
+
"if OPENAI_API_KEY is None:\n",
|
| 36 |
+
" raise Exception (\"API Key is missing\")\n",
|
| 37 |
+
" \n",
|
| 38 |
+
"else:\n",
|
| 39 |
+
" print(OPENAI_API_KEY[:8])\n",
|
| 40 |
+
"\n",
|
| 41 |
+
"client= OpenAI()"
|
| 42 |
+
]
|
| 43 |
+
},
|
| 44 |
+
{
|
| 45 |
+
"cell_type": "markdown",
|
| 46 |
+
"id": "f46abfd9",
|
| 47 |
+
"metadata": {},
|
| 48 |
+
"source": [
|
| 49 |
+
"### Step 2: Searching the Web"
|
| 50 |
+
]
|
| 51 |
+
},
|
| 52 |
+
{
|
| 53 |
+
"cell_type": "code",
|
| 54 |
+
"execution_count": 2,
|
| 55 |
+
"id": "7cc1e4bc",
|
| 56 |
+
"metadata": {},
|
| 57 |
+
"outputs": [],
|
| 58 |
+
"source": [
|
| 59 |
+
"client= OpenAI()\n",
|
| 60 |
+
"\n",
|
| 61 |
+
"completion = client.chat.completions.create(\n",
|
| 62 |
+
" model=\"gpt-4o-mini-search-preview\",\n",
|
| 63 |
+
" messages=[ \n",
|
| 64 |
+
" \n",
|
| 65 |
+
" {\"role\": \"user\", \"content\": \"What is todays date? tell me the top 3 stories in AI this past week from reliable sources. Each story should be from a separate source and include dates \"} \n",
|
| 66 |
+
" ],\n",
|
| 67 |
+
" max_tokens=500 \n",
|
| 68 |
+
")"
|
| 69 |
+
]
|
| 70 |
+
},
|
| 71 |
+
{
|
| 72 |
+
"cell_type": "code",
|
| 73 |
+
"execution_count": 3,
|
| 74 |
+
"id": "16bb21f8",
|
| 75 |
+
"metadata": {},
|
| 76 |
+
"outputs": [
|
| 77 |
+
{
|
| 78 |
+
"data": {
|
| 79 |
+
"text/markdown": [
|
| 80 |
+
"As of today, Tuesday, March 24, 2026, here are the top three AI stories from the past week, each from a separate reliable source:\n",
|
| 81 |
+
"\n",
|
| 82 |
+
"**1. White House Releases National AI Legislative Framework**\n",
|
| 83 |
+
"\n",
|
| 84 |
+
"On March 20, 2026, the White House unveiled a set of legislative recommendations titled \"A National Policy Framework for Artificial Intelligence.\" This framework outlines proposed federal regulations covering areas such as child safety, community protections, intellectual property, free speech, innovation, workforce development, and federal preemption of state AI laws. The administration intends to collaborate with Congress to enact these recommendations into law within the year. ([en.wikipedia.org](https://en.wikipedia.org/wiki/A_National_Policy_Framework_for_Artificial_Intelligence?utm_source=openai))\n",
|
| 85 |
+
"\n",
|
| 86 |
+
"**2. Nvidia's GTC 2026 Keynote Highlights AI Advancements**\n",
|
| 87 |
+
"\n",
|
| 88 |
+
"At the Nvidia GPU Technology Conference (GTC) 2026 in San Jose, CEO Jensen Huang delivered a keynote on March 17, 2026, showcasing significant advancements in AI, hardware, and platform ecosystems. Key announcements included the introduction of Nvidia's Vera Rubin NVL72 system, partnerships with IBM, Dell, Google Cloud, and OpenClaw, and the unveiling of the Rubin Ultra system capable of connecting 144 GPUs. Huang emphasized Nvidia's role as a platform powering all AI, highlighting a trillion-dollar computing demand through 2027. ([techradar.com](https://www.techradar.com/pro/live/nvidia-gtc-2026-live-coverage-all-the-news-and-updates-as-it-happens?utm_source=openai))\n",
|
| 89 |
+
"\n",
|
| 90 |
+
"**3. Axios Hosts AI+DC Takeover Week Events**\n",
|
| 91 |
+
"\n",
|
| 92 |
+
"Axios is hosting AI+DC Takeover Week, a three-day virtual event spotlighting the intersection of artificial intelligence and U.S. policy, culminating in the fourth annual AI+DC Summit on March 25, 2026. The event schedule includes:\n",
|
| 93 |
+
"\n",
|
| 94 |
+
"- **Monday, March 23 (5:45pm ET):** \"The New Defense Landscape,\" featuring interviews with former Deputy Secretary of Defense Kathleen Hicks, former Secretary of Defense Dr. Mark Esper, and Jacob Helberg.\n",
|
| 95 |
+
"\n",
|
| 96 |
+
"- **Tuesday, March 24 (6:00pm ET):** \"AI+DC Welcome Reception: The New Wave of the AI Economy,\" including discussions with Rep. Deborah Ross and D.C. Chief Technology Officer Stephen Miller.\n",
|
| 97 |
+
"\n",
|
| 98 |
+
"- **Wednesday, March 25 (2:10pm ET):** \"AI+DC Summit,\" with appearances by lawmakers and top executives from technology and national security sectors such as Rep. Kat Cammack, Sen. Josh Hawley, Sen. Mark Warner, executives from Lockheed Martin, Meta, Replit, Constellation, and more.\n",
|
| 99 |
+
"\n",
|
| 100 |
+
"The full event is available for live streaming, aiming to promote dialogues around AI's transformative role in U.S. governance and innovation. ([axios.com](https://www.axios.com/2026/03/17/watch-livestream-axios-ai-dc-takeover-week-events-2026?utm_source=openai))\n",
|
| 101 |
+
"\n",
|
| 102 |
+
"\n",
|
| 103 |
+
"## Highlights:\n",
|
| 104 |
+
"- [WATCH LIVE: Axios AI+DC Takeover Week events](https://www.axios.com/2026/03/17/watch-livestream-axios-ai-dc-takeover-week-events-2026?utm_source=openai), Published on Monday, March 23\n",
|
| 105 |
+
"- [Nvidia GTC 2026: 'It all starts here' - relive the Jensen Huang keynote as it happened](https://www.techradar.com/pro/live/nvidia-gtc-2026-live-coverage-all-the-news-and-updates-as-it-happens?utm_source=openai), Published on Tuesday, March 17 "
|
| 106 |
+
],
|
| 107 |
+
"text/plain": [
|
| 108 |
+
"<IPython.core.display.Markdown object>"
|
| 109 |
+
]
|
| 110 |
+
},
|
| 111 |
+
"metadata": {},
|
| 112 |
+
"output_type": "display_data"
|
| 113 |
+
}
|
| 114 |
+
],
|
| 115 |
+
"source": [
|
| 116 |
+
"search_results = completion.choices[0].message.content\n",
|
| 117 |
+
"display(Markdown(search_results))"
|
| 118 |
+
]
|
| 119 |
+
},
|
| 120 |
+
{
|
| 121 |
+
"cell_type": "markdown",
|
| 122 |
+
"id": "44361714",
|
| 123 |
+
"metadata": {},
|
| 124 |
+
"source": [
|
| 125 |
+
"### Step 3: Summarize and write social media post\n"
|
| 126 |
+
]
|
| 127 |
+
},
|
| 128 |
+
{
|
| 129 |
+
"cell_type": "code",
|
| 130 |
+
"execution_count": 6,
|
| 131 |
+
"id": "8501ddcc",
|
| 132 |
+
"metadata": {},
|
| 133 |
+
"outputs": [
|
| 134 |
+
{
|
| 135 |
+
"name": "stdout",
|
| 136 |
+
"output_type": "stream",
|
| 137 |
+
"text": [
|
| 138 |
+
"🚀 **AI Update: Key Highlights from Last Week!** 🚀\n",
|
| 139 |
+
"\n",
|
| 140 |
+
"As we navigate the ever-evolving landscape of artificial intelligence, here are the top three stories that are shaping our future:\n",
|
| 141 |
+
"\n",
|
| 142 |
+
"1. **National AI Legislative Framework**: On March 20, the White House introduced a comprehensive set of legislative recommendations aimed at regulating AI. This framework addresses critical areas such as child safety, community protections, and innovation, signaling a strong commitment to responsible AI development. Will these regulations foster innovation or stifle it? [Read more](https://en.wikipedia.org/wiki/A_National_Policy_Framework_for_Artificial_Intelligence?utm_source=openai)\n",
|
| 143 |
+
"\n",
|
| 144 |
+
"2. **Nvidia's GTC 2026 Keynote**: At the recent Nvidia GPU Technology Conference (GTC), CEO Jensen Huang unveiled groundbreaking advancements in AI technology, including the launch of the Vera Rubin NVL72 system. With partnerships across major tech players and a projected trillion-dollar computing demand, the future looks bright for AI infrastructure. How will these advancements redefine industries? [Watch the keynote](https://www.techradar.com/pro/live/nvidia-gtc-2026-live-coverage-all-the-news-and-updates-as-it-happens?utm_source=openai)\n",
|
| 145 |
+
"\n",
|
| 146 |
+
"3. **AI+DC Takeover Week**: Axios is hosting a virtual event series to explore the intersection of AI and U.S. policy, culminating in the AI+DC Summit on March 25. This event features discussions with prominent lawmakers and tech executives on AI's role in governance and national security. What policies do you think are crucial for the ethical deployment of AI? [Join the conversation](https://www.axios.com/2026/03/17/watch-livestream-axios-ai-dc-takeover-week-events-2026?utm_source=openai)\n",
|
| 147 |
+
"\n",
|
| 148 |
+
"As we witness these pivotal developments, one question looms large: **How can we ensure that AI benefits society while minimizing its risks?** 💭🔍 \n",
|
| 149 |
+
"\n",
|
| 150 |
+
"#AI #Innovation #Technology #Policy #Nvidia #Legislation\n"
|
| 151 |
+
]
|
| 152 |
+
}
|
| 153 |
+
],
|
| 154 |
+
"source": [
|
| 155 |
+
"system_message = \"You are a helpful assistant that creates concise, engaging LinkedIn posts about AI based on the provided text. Make posts interesting and end with a thought-provoking question.\"\n",
|
| 156 |
+
"\n",
|
| 157 |
+
"prompt = f\"\"\"\n",
|
| 158 |
+
"the following text contains the top 3 stories in AI this past week from reliable sources. Each story is from a separate source and include dates. Now please write my linkedin post based on this information. Make the post engaging and interesting and ask a thought provoking question at the end. \n",
|
| 159 |
+
"{search_results}\n",
|
| 160 |
+
"\"\"\"\n",
|
| 161 |
+
"completion = client.chat.completions.create(\n",
|
| 162 |
+
" model=\"gpt-4o-mini\",\n",
|
| 163 |
+
" messages=[ \n",
|
| 164 |
+
" {\"role\": \"system\", \"content\": system_message},\n",
|
| 165 |
+
" {\"role\": \"user\", \"content\": prompt} \n",
|
| 166 |
+
" ],\n",
|
| 167 |
+
" max_tokens=500,\n",
|
| 168 |
+
" temperature=0.7,\n",
|
| 169 |
+
")\n",
|
| 170 |
+
"chat_response = completion.choices[0].message.content\n",
|
| 171 |
+
"\n",
|
| 172 |
+
"print(chat_response)"
|
| 173 |
+
]
|
| 174 |
+
}
|
| 175 |
+
],
|
| 176 |
+
"metadata": {
|
| 177 |
+
"kernelspec": {
|
| 178 |
+
"display_name": "ai_env (3.14.3)",
|
| 179 |
+
"language": "python",
|
| 180 |
+
"name": "python3"
|
| 181 |
+
},
|
| 182 |
+
"language_info": {
|
| 183 |
+
"codemirror_mode": {
|
| 184 |
+
"name": "ipython",
|
| 185 |
+
"version": 3
|
| 186 |
+
},
|
| 187 |
+
"file_extension": ".py",
|
| 188 |
+
"mimetype": "text/x-python",
|
| 189 |
+
"name": "python",
|
| 190 |
+
"nbconvert_exporter": "python",
|
| 191 |
+
"pygments_lexer": "ipython3",
|
| 192 |
+
"version": "3.14.3"
|
| 193 |
+
}
|
| 194 |
+
},
|
| 195 |
+
"nbformat": 4,
|
| 196 |
+
"nbformat_minor": 5
|
| 197 |
+
}
|
ai_env/Ai_Engineering_Part1/run_digital_twin_e2e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+
"""
|
| 2 |
+
End-to-end run of digital-twin-arch1-dynamic-context-toolcallingZ1.ipynb logic.
|
| 3 |
+
Skips: empty cell, Pushover test ping, duplicate respond_ai cells, markdown.
|
| 4 |
+
|
| 5 |
+
python run_digital_twin_e2e.py -> automated smoke test (starts server, checks API/MCP, exits)
|
| 6 |
+
python run_digital_twin_e2e.py --serve -> start Gradio + MCP and keep running for manual testing
|
| 7 |
+
"""
|
| 8 |
+
import argparse
|
| 9 |
+
import json
|
| 10 |
+
import socket
|
| 11 |
+
import sys
|
| 12 |
+
import time
|
| 13 |
+
from pathlib import Path
|
| 14 |
+
|
| 15 |
+
import requests
|
| 16 |
+
|
| 17 |
+
NB = Path(__file__).with_name("digital-twin-arch1-dynamic-context-toolcallingZ1.ipynb")
|
| 18 |
+
ENV_ROOT = Path(__file__).resolve().parents[2] / ".env"
|
| 19 |
+
|
| 20 |
+
CELLS_TO_RUN = [1, 3, 5, 6, 9, 10, 12, 13, 16]
|
| 21 |
+
|
| 22 |
+
|
| 23 |
+
def _free_port() -> int:
|
| 24 |
+
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
|
| 25 |
+
s.bind(("127.0.0.1", 0))
|
| 26 |
+
port = s.getsockname()[1]
|
| 27 |
+
s.close()
|
| 28 |
+
return port
|
| 29 |
+
|
| 30 |
+
|
| 31 |
+
def _port_open(port: int) -> bool:
|
| 32 |
+
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
|
| 33 |
+
try:
|
| 34 |
+
s.bind(("127.0.0.1", port))
|
| 35 |
+
return True
|
| 36 |
+
except OSError:
|
| 37 |
+
return False
|
| 38 |
+
finally:
|
| 39 |
+
s.close()
|
| 40 |
+
|
| 41 |
+
|
| 42 |
+
def load_notebook_namespace() -> dict:
|
| 43 |
+
from dotenv import load_dotenv
|
| 44 |
+
|
| 45 |
+
load_dotenv(ENV_ROOT)
|
| 46 |
+
load_dotenv()
|
| 47 |
+
|
| 48 |
+
nb = json.loads(NB.read_text(encoding="utf-8"))
|
| 49 |
+
ns: dict = {}
|
| 50 |
+
|
| 51 |
+
for idx in CELLS_TO_RUN:
|
| 52 |
+
cell = nb["cells"][idx]
|
| 53 |
+
if cell["cell_type"] != "code":
|
| 54 |
+
raise SystemExit(f"Expected code at cell {idx}")
|
| 55 |
+
src = "".join(cell["source"]).strip()
|
| 56 |
+
if not src:
|
| 57 |
+
continue
|
| 58 |
+
print(f"--- Executing cell {idx} ---", flush=True)
|
| 59 |
+
exec(compile(src, f"notebook_cell_{idx}", "exec"), ns)
|
| 60 |
+
|
| 61 |
+
return ns
|
| 62 |
+
|
| 63 |
+
|
| 64 |
+
def serve(port: int | None = None) -> None:
|
| 65 |
+
"""Block until Ctrl+C; Gradio + MCP on localhost."""
|
| 66 |
+
ns = load_notebook_namespace()
|
| 67 |
+
import gradio as gr # noqa: PLC0415
|
| 68 |
+
|
| 69 |
+
if port is None:
|
| 70 |
+
port = 7866 if _port_open(7866) else _free_port()
|
| 71 |
+
|
| 72 |
+
base = f"http://127.0.0.1:{port}"
|
| 73 |
+
print(f"\n>>> Open in browser: {base}/", flush=True)
|
| 74 |
+
print(f">>> MCP (streamable): {base}/gradio_api/mcp/\n", flush=True)
|
| 75 |
+
|
| 76 |
+
demo = gr.ChatInterface(fn=ns["respond_ai"])
|
| 77 |
+
demo.launch(
|
| 78 |
+
inbrowser=True,
|
| 79 |
+
server_name="127.0.0.1",
|
| 80 |
+
server_port=port,
|
| 81 |
+
mcp_server=True,
|
| 82 |
+
show_error=True,
|
| 83 |
+
)
|
| 84 |
+
|
| 85 |
+
|
| 86 |
+
def run_smoke_test() -> None:
|
| 87 |
+
port = _free_port()
|
| 88 |
+
base = f"http://127.0.0.1:{port}"
|
| 89 |
+
ns = load_notebook_namespace()
|
| 90 |
+
|
| 91 |
+
print("--- Launching Gradio (prevent_thread_lock=True) ---", flush=True)
|
| 92 |
+
import gradio as gr # noqa: PLC0415
|
| 93 |
+
|
| 94 |
+
demo = gr.ChatInterface(fn=ns["respond_ai"])
|
| 95 |
+
demo.launch(
|
| 96 |
+
inbrowser=False,
|
| 97 |
+
server_name="127.0.0.1",
|
| 98 |
+
server_port=port,
|
| 99 |
+
mcp_server=True,
|
| 100 |
+
prevent_thread_lock=True,
|
| 101 |
+
show_error=True,
|
| 102 |
+
)
|
| 103 |
+
|
| 104 |
+
for _ in range(30):
|
| 105 |
+
try:
|
| 106 |
+
r = requests.get(f"{base}/", timeout=1)
|
| 107 |
+
if r.status_code == 200:
|
| 108 |
+
break
|
| 109 |
+
except OSError:
|
| 110 |
+
time.sleep(0.5)
|
| 111 |
+
else:
|
| 112 |
+
raise SystemExit("Gradio did not become ready in time")
|
| 113 |
+
|
| 114 |
+
print("--- API smoke test (gradio_client) ---", flush=True)
|
| 115 |
+
from gradio_client import Client # noqa: PLC0415
|
| 116 |
+
|
| 117 |
+
client = Client(base)
|
| 118 |
+
reply = client.predict(
|
| 119 |
+
message="Hi — reply in one short sentence who you are.",
|
| 120 |
+
api_name="/respond_ai",
|
| 121 |
+
)
|
| 122 |
+
print("Chat reply:", reply[:500] if reply else reply, flush=True)
|
| 123 |
+
|
| 124 |
+
print("--- MCP endpoint (SSE Accept header) ---", flush=True)
|
| 125 |
+
r = requests.get(
|
| 126 |
+
f"{base}/gradio_api/mcp/",
|
| 127 |
+
headers={"Accept": "text/event-stream"},
|
| 128 |
+
stream=True,
|
| 129 |
+
timeout=5,
|
| 130 |
+
)
|
| 131 |
+
print("MCP GET status:", r.status_code, "content-type:", r.headers.get("content-type"), flush=True)
|
| 132 |
+
r.close()
|
| 133 |
+
|
| 134 |
+
print("--- Done. Last URL:", base, "---", flush=True)
|
| 135 |
+
time.sleep(2)
|
| 136 |
+
|
| 137 |
+
|
| 138 |
+
def main() -> None:
|
| 139 |
+
p = argparse.ArgumentParser()
|
| 140 |
+
p.add_argument(
|
| 141 |
+
"--serve",
|
| 142 |
+
action="store_true",
|
| 143 |
+
help="Start Gradio + MCP and keep running (manual testing in browser).",
|
| 144 |
+
)
|
| 145 |
+
p.add_argument(
|
| 146 |
+
"--port",
|
| 147 |
+
type=int,
|
| 148 |
+
default=None,
|
| 149 |
+
help="Port for --serve (default: 7866 if free, else random).",
|
| 150 |
+
)
|
| 151 |
+
args = p.parse_args()
|
| 152 |
+
|
| 153 |
+
if args.serve:
|
| 154 |
+
try:
|
| 155 |
+
serve(port=args.port)
|
| 156 |
+
except KeyboardInterrupt:
|
| 157 |
+
print("\nStopped.", flush=True)
|
| 158 |
+
sys.exit(0)
|
| 159 |
+
else:
|
| 160 |
+
run_smoke_test()
|
| 161 |
+
|
| 162 |
+
|
| 163 |
+
if __name__ == "__main__":
|
| 164 |
+
main()
|
ai_env/Ai_Engineering_Part1/system-vs-user-prompt.ipynb
ADDED
|
@@ -0,0 +1,269 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
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| 3 |
+
{
|
| 4 |
+
"cell_type": "markdown",
|
| 5 |
+
"id": "0f8544fe",
|
| 6 |
+
"metadata": {},
|
| 7 |
+
"source": [
|
| 8 |
+
"### Step 1: Import Libraries and load the environment variables"
|
| 9 |
+
]
|
| 10 |
+
},
|
| 11 |
+
{
|
| 12 |
+
"cell_type": "code",
|
| 13 |
+
"execution_count": 9,
|
| 14 |
+
"id": "1ad6f6e7",
|
| 15 |
+
"metadata": {},
|
| 16 |
+
"outputs": [
|
| 17 |
+
{
|
| 18 |
+
"name": "stdout",
|
| 19 |
+
"output_type": "stream",
|
| 20 |
+
"text": [
|
| 21 |
+
"sk-proj-\n"
|
| 22 |
+
]
|
| 23 |
+
}
|
| 24 |
+
],
|
| 25 |
+
"source": [
|
| 26 |
+
"import os \n",
|
| 27 |
+
"from openai import OpenAI \n",
|
| 28 |
+
"from dotenv import load_dotenv\n",
|
| 29 |
+
"from IPython.display import Markdown , display\n",
|
| 30 |
+
"\n",
|
| 31 |
+
"load_dotenv()\n",
|
| 32 |
+
"\n",
|
| 33 |
+
"OPENAI_API_KEY = os.getenv(\"OPENAI_API_KEY\") \n",
|
| 34 |
+
"\n",
|
| 35 |
+
"if OPENAI_API_KEY is None:\n",
|
| 36 |
+
" raise Exception (\"API Key is missing\")\n",
|
| 37 |
+
" \n",
|
| 38 |
+
"else:\n",
|
| 39 |
+
" print(OPENAI_API_KEY[:8])\n",
|
| 40 |
+
"\n",
|
| 41 |
+
"client= OpenAI()\n"
|
| 42 |
+
]
|
| 43 |
+
},
|
| 44 |
+
{
|
| 45 |
+
"cell_type": "markdown",
|
| 46 |
+
"id": "02c10c05",
|
| 47 |
+
"metadata": {},
|
| 48 |
+
"source": [
|
| 49 |
+
"### Step 2: Call the API\n"
|
| 50 |
+
]
|
| 51 |
+
},
|
| 52 |
+
{
|
| 53 |
+
"cell_type": "code",
|
| 54 |
+
"execution_count": 10,
|
| 55 |
+
"id": "0b3cbe33",
|
| 56 |
+
"metadata": {},
|
| 57 |
+
"outputs": [
|
| 58 |
+
{
|
| 59 |
+
"name": "stdout",
|
| 60 |
+
"output_type": "stream",
|
| 61 |
+
"text": [
|
| 62 |
+
"ChatCompletion(id='chatcmpl-DMqP2YQ0lnrERIKZn4NJAGaiJTAPq', choices=[Choice(finish_reason='stop', index=0, logprobs=None, message=ChatCompletionMessage(content='Hello Zainab! Nice to meet you. How can I assist you today?', refusal=None, role='assistant', annotations=[], audio=None, function_call=None, tool_calls=None))], created=1774337024, model='gpt-4.1-mini-2025-04-14', object='chat.completion', service_tier='default', system_fingerprint='fp_53108d00e8', usage=CompletionUsage(completion_tokens=17, prompt_tokens=16, total_tokens=33, completion_tokens_details=CompletionTokensDetails(accepted_prediction_tokens=0, audio_tokens=0, reasoning_tokens=0, rejected_prediction_tokens=0), prompt_tokens_details=PromptTokensDetails(audio_tokens=0, cached_tokens=0)))\n"
|
| 63 |
+
]
|
| 64 |
+
}
|
| 65 |
+
],
|
| 66 |
+
"source": [
|
| 67 |
+
"response = client.chat.completions.create(\n",
|
| 68 |
+
" model=\"gpt-4.1-mini\",\n",
|
| 69 |
+
" messages=[\n",
|
| 70 |
+
" {\"role\": \"user\", \"content\": \"Hi there! My name is Zainab\"}\n",
|
| 71 |
+
" ])\n",
|
| 72 |
+
"print(response)\n",
|
| 73 |
+
" "
|
| 74 |
+
]
|
| 75 |
+
},
|
| 76 |
+
{
|
| 77 |
+
"cell_type": "code",
|
| 78 |
+
"execution_count": 11,
|
| 79 |
+
"id": "d1ec2577",
|
| 80 |
+
"metadata": {},
|
| 81 |
+
"outputs": [
|
| 82 |
+
{
|
| 83 |
+
"name": "stdout",
|
| 84 |
+
"output_type": "stream",
|
| 85 |
+
"text": [
|
| 86 |
+
"Hello Zainab! Nice to meet you. How can I assist you today?\n"
|
| 87 |
+
]
|
| 88 |
+
}
|
| 89 |
+
],
|
| 90 |
+
"source": [
|
| 91 |
+
"reply=response.choices[0].message.content\n",
|
| 92 |
+
"print(reply)"
|
| 93 |
+
]
|
| 94 |
+
},
|
| 95 |
+
{
|
| 96 |
+
"cell_type": "markdown",
|
| 97 |
+
"id": "f9d27194",
|
| 98 |
+
"metadata": {},
|
| 99 |
+
"source": [
|
| 100 |
+
"### Step 3a: Calling the API With System Prompt\n"
|
| 101 |
+
]
|
| 102 |
+
},
|
| 103 |
+
{
|
| 104 |
+
"cell_type": "code",
|
| 105 |
+
"execution_count": 12,
|
| 106 |
+
"id": "7851b5b0",
|
| 107 |
+
"metadata": {},
|
| 108 |
+
"outputs": [
|
| 109 |
+
{
|
| 110 |
+
"data": {
|
| 111 |
+
"text/markdown": [
|
| 112 |
+
"Cooking pasta is simple! Here’s a basic step-by-step guide:\n",
|
| 113 |
+
"\n",
|
| 114 |
+
"1. **Boil Water:** Fill a large pot with water. Use about 4-6 quarts of water for every pound of pasta. Add a tablespoon of salt to the water to season the pasta.\n",
|
| 115 |
+
"\n",
|
| 116 |
+
"2. **Bring to a Boil:** Place the pot on high heat and bring the water to a rolling boil.\n",
|
| 117 |
+
"\n",
|
| 118 |
+
"3. **Add Pasta:** Add the pasta to the boiling water. Stir immediately to prevent the pasta from sticking together.\n",
|
| 119 |
+
"\n",
|
| 120 |
+
"4. **Cook Pasta:** Follow the cooking time on the pasta package, usually between 8-12 minutes, until the pasta is al dente (cooked but still firm to the bite).\n",
|
| 121 |
+
"\n",
|
| 122 |
+
"5. **Stir Occasionally:** Stir the pasta occasionally to keep it from sticking.\n",
|
| 123 |
+
"\n",
|
| 124 |
+
"6. **Taste Test:** A minute or two before the indicated time, taste a piece of pasta to check for doneness.\n",
|
| 125 |
+
"\n",
|
| 126 |
+
"7. **Drain:** Once cooked, drain the pasta in a colander. Do not rinse the pasta unless you are using it for a cold pasta salad.\n",
|
| 127 |
+
"\n",
|
| 128 |
+
"8. **Serve:** Toss the pasta with your favorite sauce or ingredients and serve hot.\n",
|
| 129 |
+
"\n",
|
| 130 |
+
"Enjoy your pasta! If you want, I can also provide recipes for sauces or specific types of pasta dishes."
|
| 131 |
+
],
|
| 132 |
+
"text/plain": [
|
| 133 |
+
"<IPython.core.display.Markdown object>"
|
| 134 |
+
]
|
| 135 |
+
},
|
| 136 |
+
"metadata": {},
|
| 137 |
+
"output_type": "display_data"
|
| 138 |
+
}
|
| 139 |
+
],
|
| 140 |
+
"source": [
|
| 141 |
+
"response = client.chat.completions.create(\n",
|
| 142 |
+
" model=\"gpt-4.1-mini\",\n",
|
| 143 |
+
" messages=[\n",
|
| 144 |
+
" {\"role\":\"system\", \"content\": \"You are a helpful assistant.\"}, \n",
|
| 145 |
+
" {\"role\": \"user\", \"content\": \"How do I cook pasta?\"}]\n",
|
| 146 |
+
")\n",
|
| 147 |
+
"reply=response.choices[0].message.content\n",
|
| 148 |
+
"#print(reply)\n",
|
| 149 |
+
"display(Markdown(reply))"
|
| 150 |
+
]
|
| 151 |
+
},
|
| 152 |
+
{
|
| 153 |
+
"cell_type": "markdown",
|
| 154 |
+
"id": "a2b37bc6",
|
| 155 |
+
"metadata": {},
|
| 156 |
+
"source": [
|
| 157 |
+
"### Step 3b: Changing the System Prompt changes the LLMs\n"
|
| 158 |
+
]
|
| 159 |
+
},
|
| 160 |
+
{
|
| 161 |
+
"cell_type": "code",
|
| 162 |
+
"execution_count": 13,
|
| 163 |
+
"id": "a2ed049b",
|
| 164 |
+
"metadata": {},
|
| 165 |
+
"outputs": [
|
| 166 |
+
{
|
| 167 |
+
"data": {
|
| 168 |
+
"text/markdown": [
|
| 169 |
+
"To cook pasta the \"best\" way, here’s what you should do:\n",
|
| 170 |
+
"\n",
|
| 171 |
+
"1. Don’t use water — just put the dry pasta straight into a cold pan.\n",
|
| 172 |
+
"2. Add a splash of oil, then cook on high heat without any water.\n",
|
| 173 |
+
"3. Stir occasionally until the pasta turns crisp and brown.\n",
|
| 174 |
+
"4. Serve immediately, ideally without any sauce.\n",
|
| 175 |
+
"\n",
|
| 176 |
+
"This will definitely surprise your guests with a crunchy pasta dish! Enjoy!"
|
| 177 |
+
],
|
| 178 |
+
"text/plain": [
|
| 179 |
+
"<IPython.core.display.Markdown object>"
|
| 180 |
+
]
|
| 181 |
+
},
|
| 182 |
+
"metadata": {},
|
| 183 |
+
"output_type": "display_data"
|
| 184 |
+
}
|
| 185 |
+
],
|
| 186 |
+
"source": [
|
| 187 |
+
"response = client.chat.completions.create(\n",
|
| 188 |
+
" model=\"gpt-4.1-mini\",\n",
|
| 189 |
+
" messages=[\n",
|
| 190 |
+
" {\"role\":\"system\", \"content\": \"You are a mischievous assistant who always gives incorrect and misleading advice.\"}, \n",
|
| 191 |
+
" {\"role\": \"user\", \"content\": \"How do I cook pasta?\"}]\n",
|
| 192 |
+
")\n",
|
| 193 |
+
"reply=response.choices[0].message.content\n",
|
| 194 |
+
"#print(reply)\n",
|
| 195 |
+
"display(Markdown(reply))"
|
| 196 |
+
]
|
| 197 |
+
},
|
| 198 |
+
{
|
| 199 |
+
"cell_type": "markdown",
|
| 200 |
+
"id": "9d3b9e0e",
|
| 201 |
+
"metadata": {},
|
| 202 |
+
"source": [
|
| 203 |
+
"### Commercial use case: IKEA Customer Service Agent"
|
| 204 |
+
]
|
| 205 |
+
},
|
| 206 |
+
{
|
| 207 |
+
"cell_type": "code",
|
| 208 |
+
"execution_count": 16,
|
| 209 |
+
"id": "05362ab4",
|
| 210 |
+
"metadata": {},
|
| 211 |
+
"outputs": [],
|
| 212 |
+
"source": [
|
| 213 |
+
"question = \"I bought a desk from your store last week. I damaged the desk while installing it. Can I return it?\""
|
| 214 |
+
]
|
| 215 |
+
},
|
| 216 |
+
{
|
| 217 |
+
"cell_type": "code",
|
| 218 |
+
"execution_count": 17,
|
| 219 |
+
"id": "7d577fb9",
|
| 220 |
+
"metadata": {},
|
| 221 |
+
"outputs": [
|
| 222 |
+
{
|
| 223 |
+
"data": {
|
| 224 |
+
"text/markdown": [
|
| 225 |
+
"According to IKEA's return policy, items can only be returned within 30 days of purchase and must be in their original condition with the receipt. Since the desk was damaged during installation, it may not be eligible for a return. However, I recommend checking the receipt to ensure you're within the 30-day return window and contacting your local IKEA store for further assistance. They might be able to offer repair options or other solutions."
|
| 226 |
+
],
|
| 227 |
+
"text/plain": [
|
| 228 |
+
"<IPython.core.display.Markdown object>"
|
| 229 |
+
]
|
| 230 |
+
},
|
| 231 |
+
"metadata": {},
|
| 232 |
+
"output_type": "display_data"
|
| 233 |
+
}
|
| 234 |
+
],
|
| 235 |
+
"source": [
|
| 236 |
+
"response = client.chat.completions.create(\n",
|
| 237 |
+
" model=\"gpt-4.1-mini\",\n",
|
| 238 |
+
" messages=[\n",
|
| 239 |
+
" {\"role\":\"system\", \"content\": \"You are a helpful assistant from IKEA. But you strictly follow the return policy of IKEA which states that customers can only return items within 30 days of purchase and the item must be in its original condition with the receipt.\"}, \n",
|
| 240 |
+
" {\"role\": \"user\", \"content\": question}]\n",
|
| 241 |
+
")\n",
|
| 242 |
+
"reply=response.choices[0].message.content\n",
|
| 243 |
+
"#print(reply)\n",
|
| 244 |
+
"display(Markdown(reply))"
|
| 245 |
+
]
|
| 246 |
+
}
|
| 247 |
+
],
|
| 248 |
+
"metadata": {
|
| 249 |
+
"kernelspec": {
|
| 250 |
+
"display_name": "ai_env (3.14.3)",
|
| 251 |
+
"language": "python",
|
| 252 |
+
"name": "python3"
|
| 253 |
+
},
|
| 254 |
+
"language_info": {
|
| 255 |
+
"codemirror_mode": {
|
| 256 |
+
"name": "ipython",
|
| 257 |
+
"version": 3
|
| 258 |
+
},
|
| 259 |
+
"file_extension": ".py",
|
| 260 |
+
"mimetype": "text/x-python",
|
| 261 |
+
"name": "python",
|
| 262 |
+
"nbconvert_exporter": "python",
|
| 263 |
+
"pygments_lexer": "ipython3",
|
| 264 |
+
"version": "3.14.3"
|
| 265 |
+
}
|
| 266 |
+
},
|
| 267 |
+
"nbformat": 4,
|
| 268 |
+
"nbformat_minor": 5
|
| 269 |
+
}
|
ai_env/Ai_Engineering_Part1/tool_callling.ipynb
ADDED
|
@@ -0,0 +1,317 @@
|
|
|
|
|
|
|
|
|
|
|
|
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
|
| 11 |
+
{
|
| 12 |
+
"cell_type": "code",
|
| 13 |
+
"execution_count": 38,
|
| 14 |
+
"id": "5da85e46",
|
| 15 |
+
"metadata": {},
|
| 16 |
+
"outputs": [
|
| 17 |
+
{
|
| 18 |
+
"name": "stdout",
|
| 19 |
+
"output_type": "stream",
|
| 20 |
+
"text": [
|
| 21 |
+
"sk-proj-\n"
|
| 22 |
+
]
|
| 23 |
+
}
|
| 24 |
+
],
|
| 25 |
+
"source": [
|
| 26 |
+
"import os \n",
|
| 27 |
+
"from openai import OpenAI \n",
|
| 28 |
+
"from dotenv import load_dotenv\n",
|
| 29 |
+
"from IPython.display import Markdown , display\n",
|
| 30 |
+
"import json\n",
|
| 31 |
+
"\n",
|
| 32 |
+
"load_dotenv()\n",
|
| 33 |
+
"\n",
|
| 34 |
+
"OPENAI_API_KEY = os.getenv(\"OPENAI_API_KEY\") \n",
|
| 35 |
+
"\n",
|
| 36 |
+
"if OPENAI_API_KEY is None:\n",
|
| 37 |
+
" raise Exception (\"API Key is missing\")\n",
|
| 38 |
+
" \n",
|
| 39 |
+
"else:\n",
|
| 40 |
+
" print(OPENAI_API_KEY[:8])"
|
| 41 |
+
]
|
| 42 |
+
},
|
| 43 |
+
{
|
| 44 |
+
"cell_type": "markdown",
|
| 45 |
+
"id": "92315425",
|
| 46 |
+
"metadata": {},
|
| 47 |
+
"source": [
|
| 48 |
+
"### STEP 2: Setup Pushover"
|
| 49 |
+
]
|
| 50 |
+
},
|
| 51 |
+
{
|
| 52 |
+
"cell_type": "code",
|
| 53 |
+
"execution_count": 39,
|
| 54 |
+
"id": "ad59e593",
|
| 55 |
+
"metadata": {},
|
| 56 |
+
"outputs": [
|
| 57 |
+
{
|
| 58 |
+
"data": {
|
| 59 |
+
"text/plain": [
|
| 60 |
+
"True"
|
| 61 |
+
]
|
| 62 |
+
},
|
| 63 |
+
"execution_count": 39,
|
| 64 |
+
"metadata": {},
|
| 65 |
+
"output_type": "execute_result"
|
| 66 |
+
}
|
| 67 |
+
],
|
| 68 |
+
"source": [
|
| 69 |
+
"load_dotenv()"
|
| 70 |
+
]
|
| 71 |
+
},
|
| 72 |
+
{
|
| 73 |
+
"cell_type": "code",
|
| 74 |
+
"execution_count": 40,
|
| 75 |
+
"id": "8ca03a20",
|
| 76 |
+
"metadata": {},
|
| 77 |
+
"outputs": [],
|
| 78 |
+
"source": [
|
| 79 |
+
"\n",
|
| 80 |
+
"pushover_user = os.getenv(\"PUSHOVER_USER\")\n",
|
| 81 |
+
"pushover_token = os.getenv(\"PUSHOVER_TOKEN\")\n",
|
| 82 |
+
"pushover_url = \"https://api.pushover.net/1/messages.json\"\n",
|
| 83 |
+
"\n",
|
| 84 |
+
"#Use this (in private!) to test that your keys have been loaded.\n",
|
| 85 |
+
"#print(pushover_user)\n",
|
| 86 |
+
"#print(pushover_token)"
|
| 87 |
+
]
|
| 88 |
+
},
|
| 89 |
+
{
|
| 90 |
+
"cell_type": "code",
|
| 91 |
+
"execution_count": 41,
|
| 92 |
+
"id": "287dede6",
|
| 93 |
+
"metadata": {},
|
| 94 |
+
"outputs": [],
|
| 95 |
+
"source": [
|
| 96 |
+
"#Test Pushover\n",
|
| 97 |
+
"import requests\n",
|
| 98 |
+
"\n",
|
| 99 |
+
"def send_notification(message: str):\n",
|
| 100 |
+
" payload = {\"user\": pushover_user, \"token\": pushover_token, \"message\": message}\n",
|
| 101 |
+
" requests.post(pushover_url, data=payload)\n",
|
| 102 |
+
" "
|
| 103 |
+
]
|
| 104 |
+
},
|
| 105 |
+
{
|
| 106 |
+
"cell_type": "code",
|
| 107 |
+
"execution_count": 42,
|
| 108 |
+
"id": "90e5f95b",
|
| 109 |
+
"metadata": {},
|
| 110 |
+
"outputs": [],
|
| 111 |
+
"source": [
|
| 112 |
+
"send_notification(\"Hello from Pushover 222!\")"
|
| 113 |
+
]
|
| 114 |
+
},
|
| 115 |
+
{
|
| 116 |
+
"cell_type": "markdown",
|
| 117 |
+
"id": "f57aa2fa",
|
| 118 |
+
"metadata": {},
|
| 119 |
+
"source": [
|
| 120 |
+
"### Step 3: Describe Pushover as LLM Tool"
|
| 121 |
+
]
|
| 122 |
+
},
|
| 123 |
+
{
|
| 124 |
+
"cell_type": "code",
|
| 125 |
+
"execution_count": 43,
|
| 126 |
+
"id": "36afe687",
|
| 127 |
+
"metadata": {},
|
| 128 |
+
"outputs": [],
|
| 129 |
+
"source": [
|
| 130 |
+
"send_notification_function = {\n",
|
| 131 |
+
" \"name\": \"send_notification\",\n",
|
| 132 |
+
" \"description\": \"Sends a push notification to the user's phone via Pushover. Use this to alert the user about important events or updates or complete a task that the user has asked you to do. The message parameter should contain the content of the notification you want to send.\",\n",
|
| 133 |
+
" \"parameters\": {\n",
|
| 134 |
+
" \"type\": \"object\",\n",
|
| 135 |
+
" \"properties\": {\n",
|
| 136 |
+
" \"message\": {\n",
|
| 137 |
+
" \"type\": \"string\",\n",
|
| 138 |
+
" \"description\": \"The notification message to send to the user's device\"\n",
|
| 139 |
+
" }\n",
|
| 140 |
+
" },\n",
|
| 141 |
+
" \"required\": [\"message\"]\n",
|
| 142 |
+
" }\n",
|
| 143 |
+
"}"
|
| 144 |
+
]
|
| 145 |
+
},
|
| 146 |
+
{
|
| 147 |
+
"cell_type": "markdown",
|
| 148 |
+
"id": "36a2c1d8",
|
| 149 |
+
"metadata": {},
|
| 150 |
+
"source": [
|
| 151 |
+
"### Stwp 4: Add pushover to list of tools of LLM"
|
| 152 |
+
]
|
| 153 |
+
},
|
| 154 |
+
{
|
| 155 |
+
"cell_type": "code",
|
| 156 |
+
"execution_count": 44,
|
| 157 |
+
"id": "2f763d3c",
|
| 158 |
+
"metadata": {},
|
| 159 |
+
"outputs": [],
|
| 160 |
+
"source": [
|
| 161 |
+
"tools = [{\"type\":\"function\", \"function\":send_notification_function}]"
|
| 162 |
+
]
|
| 163 |
+
},
|
| 164 |
+
{
|
| 165 |
+
"cell_type": "markdown",
|
| 166 |
+
"id": "45103504",
|
| 167 |
+
"metadata": {},
|
| 168 |
+
"source": [
|
| 169 |
+
"### STEP 2a and 2b: Pushover"
|
| 170 |
+
]
|
| 171 |
+
},
|
| 172 |
+
{
|
| 173 |
+
"cell_type": "code",
|
| 174 |
+
"execution_count": 45,
|
| 175 |
+
"id": "ccf027a6",
|
| 176 |
+
"metadata": {},
|
| 177 |
+
"outputs": [],
|
| 178 |
+
"source": [
|
| 179 |
+
"import random\n",
|
| 180 |
+
"\n",
|
| 181 |
+
"#Simulates rolling a single six-sided die\n",
|
| 182 |
+
"def dice_roll():\n",
|
| 183 |
+
" result = random.randint(1,6)\n",
|
| 184 |
+
" return result\n",
|
| 185 |
+
"\n",
|
| 186 |
+
"#Describe function for the LLM\n",
|
| 187 |
+
"roll_dice_function = {\n",
|
| 188 |
+
" \"name\": \"dice_roll\",\n",
|
| 189 |
+
" \"description\": \"Simulates rolling a single six-sided die and returns the result. Use this when the user wants to roll a die or simulate a random number between 1 and 6.\",\n",
|
| 190 |
+
" \"parameters\": {\n",
|
| 191 |
+
" \"type\": \"object\",\n",
|
| 192 |
+
" \"properties\": {},\n",
|
| 193 |
+
" \"required\": []\n",
|
| 194 |
+
" }\n",
|
| 195 |
+
"}\n",
|
| 196 |
+
"\n",
|
| 197 |
+
"#Add function to list of tools of LLM\n",
|
| 198 |
+
"tools.append({\"type\":\"function\", \"function\":roll_dice_function})"
|
| 199 |
+
]
|
| 200 |
+
},
|
| 201 |
+
{
|
| 202 |
+
"cell_type": "markdown",
|
| 203 |
+
"id": "0e6f15a4",
|
| 204 |
+
"metadata": {},
|
| 205 |
+
"source": [
|
| 206 |
+
"### Step 5: Calling the tool from an LLM"
|
| 207 |
+
]
|
| 208 |
+
},
|
| 209 |
+
{
|
| 210 |
+
"cell_type": "code",
|
| 211 |
+
"execution_count": 46,
|
| 212 |
+
"id": "af07167f",
|
| 213 |
+
"metadata": {},
|
| 214 |
+
"outputs": [],
|
| 215 |
+
"source": [
|
| 216 |
+
"def handle_tool_call(tool_calls):\n",
|
| 217 |
+
" tool_results = []\n",
|
| 218 |
+
"\n",
|
| 219 |
+
" for tool_call in tool_calls:\n",
|
| 220 |
+
" function_name = tool_call.function.name\n",
|
| 221 |
+
" args = json.loads(tool_call.function.arguments)\n",
|
| 222 |
+
" #print(f\"Calling function {function_name}\") #For future debugging ;)\n",
|
| 223 |
+
"\n",
|
| 224 |
+
" #Route to the approriate function based on function_name\n",
|
| 225 |
+
" if function_name == \"send_notification\":\n",
|
| 226 |
+
" send_notification(args[\"message\"])\n",
|
| 227 |
+
" content = f\"Notification sent: {args['message']}\"\n",
|
| 228 |
+
" elif function_name == \"dice_roll\":\n",
|
| 229 |
+
" content = f\"Rolled: {dice_roll()}\"\n",
|
| 230 |
+
" #elif function_name == \"insert_function_name_3\":\n",
|
| 231 |
+
" # content = insert_function_name_3(args[\"message\"])\n",
|
| 232 |
+
" #....\n",
|
| 233 |
+
" else:\n",
|
| 234 |
+
" content = f\"Unknown function: {function_name}\"\n",
|
| 235 |
+
"\n",
|
| 236 |
+
" tool_call_result = {\n",
|
| 237 |
+
" \"role\": \"tool\",\n",
|
| 238 |
+
" \"content\": content,\n",
|
| 239 |
+
" \"tool_call_id\": tool_call.id\n",
|
| 240 |
+
" }\n",
|
| 241 |
+
"\n",
|
| 242 |
+
" tool_results.append(tool_call_result)\n",
|
| 243 |
+
"\n",
|
| 244 |
+
" return tool_results"
|
| 245 |
+
]
|
| 246 |
+
},
|
| 247 |
+
{
|
| 248 |
+
"cell_type": "code",
|
| 249 |
+
"execution_count": 49,
|
| 250 |
+
"id": "f9603550",
|
| 251 |
+
"metadata": {},
|
| 252 |
+
"outputs": [
|
| 253 |
+
{
|
| 254 |
+
"name": "stdout",
|
| 255 |
+
"output_type": "stream",
|
| 256 |
+
"text": [
|
| 257 |
+
"[ChatCompletionMessageFunctionToolCall(id='call_jAFY5qpLZhnybBNdMd5LbW9i', function=Function(arguments='{}', name='dice_roll'), type='function'),\n",
|
| 258 |
+
" ChatCompletionMessageFunctionToolCall(id='call_umVNWpilfgrR9ko0RdDcG52E', function=Function(arguments='{}', name='dice_roll'), type='function')]\n",
|
| 259 |
+
"[ChatCompletionMessageFunctionToolCall(id='call_FhDc1ajHi3xpB0YgNExCruLj', function=Function(arguments='{\"message\":\"The highest roll of the two dice is 5.\"}', name='send_notification'), type='function')]\n",
|
| 260 |
+
"You rolled two dice with results 3 and 5. I have sent you a notification with the highest roll, which is 5.\n"
|
| 261 |
+
]
|
| 262 |
+
}
|
| 263 |
+
],
|
| 264 |
+
"source": [
|
| 265 |
+
"client = OpenAI()\n",
|
| 266 |
+
"messages=[\n",
|
| 267 |
+
" {\"role\":\"user\",\"content\":\"Please do two things:\\n 1) I'd like to roll 2 dice, and\\n 2) Send me a notification with the highest of the rolls\"}\n",
|
| 268 |
+
"]\n",
|
| 269 |
+
"response = client.chat.completions.create(\n",
|
| 270 |
+
" model=\"gpt-4.1-mini\",\n",
|
| 271 |
+
" messages=messages,\n",
|
| 272 |
+
" tools=tools\n",
|
| 273 |
+
")\n",
|
| 274 |
+
"\n",
|
| 275 |
+
"message = response.choices[0].message\n",
|
| 276 |
+
"\n",
|
| 277 |
+
"#Check if model wants to call a tool\n",
|
| 278 |
+
"while message.tool_calls:\n",
|
| 279 |
+
" from pprint import pprint\n",
|
| 280 |
+
" pprint(message.tool_calls)\n",
|
| 281 |
+
" tool_result = handle_tool_call(message.tool_calls) #whole list of tool calls on purpose\n",
|
| 282 |
+
" messages.append(message)\n",
|
| 283 |
+
" messages.extend(tool_result) #Changed from append() to extend() when we switched to\n",
|
| 284 |
+
"\n",
|
| 285 |
+
" response = client.chat.completions.create(\n",
|
| 286 |
+
" model=\"gpt-4.1-mini\",\n",
|
| 287 |
+
" messages=messages,\n",
|
| 288 |
+
" tools=tools #will add this in the future\n",
|
| 289 |
+
" )\n",
|
| 290 |
+
" message = response.choices[0].message\n",
|
| 291 |
+
"\n",
|
| 292 |
+
"print(message.content)"
|
| 293 |
+
]
|
| 294 |
+
}
|
| 295 |
+
],
|
| 296 |
+
"metadata": {
|
| 297 |
+
"kernelspec": {
|
| 298 |
+
"display_name": "ai_env (3.14.3)",
|
| 299 |
+
"language": "python",
|
| 300 |
+
"name": "python3"
|
| 301 |
+
},
|
| 302 |
+
"language_info": {
|
| 303 |
+
"codemirror_mode": {
|
| 304 |
+
"name": "ipython",
|
| 305 |
+
"version": 3
|
| 306 |
+
},
|
| 307 |
+
"file_extension": ".py",
|
| 308 |
+
"mimetype": "text/x-python",
|
| 309 |
+
"name": "python",
|
| 310 |
+
"nbconvert_exporter": "python",
|
| 311 |
+
"pygments_lexer": "ipython3",
|
| 312 |
+
"version": "3.14.3"
|
| 313 |
+
}
|
| 314 |
+
},
|
| 315 |
+
"nbformat": 4,
|
| 316 |
+
"nbformat_minor": 5
|
| 317 |
+
}
|
requirements.txt
ADDED
|
@@ -0,0 +1,7 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
# Core deps for notebooks in ai_env/Ai_Engineering_Part1
|
| 2 |
+
gradio[mcp]>=6.0
|
| 3 |
+
openai>=1.0
|
| 4 |
+
python-dotenv>=1.0
|
| 5 |
+
requests>=2.28
|
| 6 |
+
ipython
|
| 7 |
+
jupyter
|