Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
191 changes: 170 additions & 21 deletions 01_materials/labs/05_1_tools.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -34,18 +34,27 @@
},
{
"cell_type": "code",
"execution_count": null,
"execution_count": 27,
"id": "d90d1ddd",
"metadata": {},
"outputs": [],
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"The dotenv extension is already loaded. To reload it, use:\n",
" %reload_ext dotenv\n"
]
}
],
"source": [
"%load_ext dotenv\n",
"%dotenv ../../05_src/.secrets"
]
},
{
"cell_type": "code",
"execution_count": null,
"execution_count": 28,
"id": "76b0c6c3",
"metadata": {},
"outputs": [],
Expand Down Expand Up @@ -97,7 +106,7 @@
},
{
"cell_type": "code",
"execution_count": null,
"execution_count": 29,
"id": "ddcae9ad",
"metadata": {},
"outputs": [],
Expand Down Expand Up @@ -139,13 +148,13 @@
},
{
"cell_type": "code",
"execution_count": null,
"execution_count": 30,
"id": "6ff32621",
"metadata": {},
"outputs": [],
"source": [
"input_list = [\n",
" {\"role\": \"user\", \"content\": \"What is my horoscope? I am an Aquarius.\"}\n",
" {\"role\": \"user\", \"content\": \"What is my horoscope? I am a Virgo.\"}\n",
"]"
]
},
Expand All @@ -159,7 +168,7 @@
},
{
"cell_type": "code",
"execution_count": null,
"execution_count": 31,
"id": "c3927771",
"metadata": {},
"outputs": [],
Expand Down Expand Up @@ -189,12 +198,25 @@
},
{
"cell_type": "code",
"execution_count": null,
"execution_count": 32,
"id": "56f346d4",
"metadata": {},
"outputs": [],
"outputs": [
{
"data": {
"text/plain": [
"[{'role': 'user', 'content': 'What is my horoscope? I am a Virgo.'},\n",
" ResponseReasoningItem(id='rs_08033f801fcf50e200699f8ad6327c8194911300c3d556e0d0', summary=[], type='reasoning', content=None, encrypted_content=None, status=None),\n",
" ResponseFunctionToolCall(arguments='{\"zodiac_sign\":\"Virgo\"}', call_id='call_8x3RZrv0VhIuhHoV6rj6aN0Q', name='get_horoscope', type='function_call', id='fc_08033f801fcf50e200699f8ad7b0b08194a9a46abe42e9f22f', status='completed')]"
]
},
"execution_count": 32,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"response.output"
"input_list + response.output"
]
},
{
Expand All @@ -207,10 +229,26 @@
},
{
"cell_type": "code",
"execution_count": null,
"execution_count": 33,
"id": "e02789b3",
"metadata": {},
"outputs": [],
"outputs": [
{
"data": {
"text/plain": [
"{'arguments': '{\"zodiac_sign\":\"Virgo\"}',\n",
" 'call_id': 'call_8x3RZrv0VhIuhHoV6rj6aN0Q',\n",
" 'name': 'get_horoscope',\n",
" 'type': 'function_call',\n",
" 'id': 'fc_08033f801fcf50e200699f8ad7b0b08194a9a46abe42e9f22f',\n",
" 'status': 'completed'}"
]
},
"execution_count": 33,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"response.output[1].model_dump()"
]
Expand All @@ -228,7 +266,7 @@
},
{
"cell_type": "code",
"execution_count": null,
"execution_count": 34,
"id": "874a4f3f",
"metadata": {},
"outputs": [],
Expand All @@ -239,7 +277,7 @@
" if item.type == \"function_call\":\n",
" if item.name == \"get_horoscope\":\n",
" # Execute the function logic for get_horoscope\n",
" horoscope = get_horoscope(**json.loads(item.arguments))\n",
" horoscope = get_horoscope(**json.loads(item.arguments)) # ** each key value pair is converted to function call parameters\n",
" \n",
" # Provide function call results to the model\n",
" input_list.append({\n",
Expand Down Expand Up @@ -269,17 +307,25 @@
},
{
"cell_type": "code",
"execution_count": null,
"execution_count": 39,
"id": "9d61e62f",
"metadata": {},
"outputs": [],
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"[{'role': 'user', 'content': 'What is my horoscope? I am a Virgo.'}, ResponseReasoningItem(id='rs_08033f801fcf50e200699f8ad6327c8194911300c3d556e0d0', summary=[], type='reasoning', content=None, encrypted_content=None, status=None), ResponseFunctionToolCall(arguments='{\"zodiac_sign\":\"Virgo\"}', call_id='call_8x3RZrv0VhIuhHoV6rj6aN0Q', name='get_horoscope', type='function_call', id='fc_08033f801fcf50e200699f8ad7b0b08194a9a46abe42e9f22f', status='completed'), {'type': 'function_call_output', 'call_id': 'call_8x3RZrv0VhIuhHoV6rj6aN0Q', 'output': '{\"horoscope\": \"Virgo: Today is a great day for new beginnings.\"}'}]\n"
]
}
],
"source": [
"print(input_list)"
]
},
{
"cell_type": "code",
"execution_count": null,
"execution_count": 40,
"id": "d1e27b61",
"metadata": {},
"outputs": [],
Expand All @@ -302,10 +348,113 @@
},
{
"cell_type": "code",
"execution_count": null,
"execution_count": 37,
"id": "6b714b4d",
"metadata": {},
"outputs": [],
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"{\n",
" \"id\": \"resp_08033f801fcf50e200699f8ad83b648194b22086e20607ae75\",\n",
" \"created_at\": 1772063448.0,\n",
" \"error\": null,\n",
" \"incomplete_details\": null,\n",
" \"instructions\": \"Respond only with a horoscope generated by a tool.\",\n",
" \"metadata\": {},\n",
" \"model\": \"gpt-5-2025-08-07\",\n",
" \"object\": \"response\",\n",
" \"output\": [\n",
" {\n",
" \"id\": \"msg_08033f801fcf50e200699f8ad8d64081949ebeb6a391771a45\",\n",
" \"content\": [\n",
" {\n",
" \"annotations\": [],\n",
" \"text\": \"Virgo: Today is a great day for new beginnings.\",\n",
" \"type\": \"output_text\",\n",
" \"logprobs\": []\n",
" }\n",
" ],\n",
" \"role\": \"assistant\",\n",
" \"status\": \"completed\",\n",
" \"type\": \"message\"\n",
" }\n",
" ],\n",
" \"parallel_tool_calls\": true,\n",
" \"temperature\": 1.0,\n",
" \"tool_choice\": \"auto\",\n",
" \"tools\": [\n",
" {\n",
" \"name\": \"get_horoscope\",\n",
" \"parameters\": {\n",
" \"type\": \"object\",\n",
" \"properties\": {\n",
" \"zodiac_sign\": {\n",
" \"type\": \"string\",\n",
" \"description\": \"Zodiac sign e.g. Aries, Taurus\"\n",
" }\n",
" },\n",
" \"required\": [\n",
" \"zodiac_sign\"\n",
" ],\n",
" \"additionalProperties\": false\n",
" },\n",
" \"strict\": true,\n",
" \"type\": \"function\",\n",
" \"description\": \"Get horoscope for a given zodiac sign.\"\n",
" }\n",
" ],\n",
" \"top_p\": 1.0,\n",
" \"background\": false,\n",
" \"conversation\": null,\n",
" \"max_output_tokens\": null,\n",
" \"max_tool_calls\": null,\n",
" \"previous_response_id\": null,\n",
" \"prompt\": null,\n",
" \"prompt_cache_key\": null,\n",
" \"reasoning\": {\n",
" \"effort\": \"medium\",\n",
" \"generate_summary\": null,\n",
" \"summary\": null\n",
" },\n",
" \"safety_identifier\": null,\n",
" \"service_tier\": \"default\",\n",
" \"status\": \"completed\",\n",
" \"text\": {\n",
" \"format\": {\n",
" \"type\": \"text\"\n",
" },\n",
" \"verbosity\": \"medium\"\n",
" },\n",
" \"top_logprobs\": 0,\n",
" \"truncation\": \"disabled\",\n",
" \"usage\": {\n",
" \"input_tokens\": 222,\n",
" \"input_tokens_details\": {\n",
" \"cached_tokens\": 0\n",
" },\n",
" \"output_tokens\": 16,\n",
" \"output_tokens_details\": {\n",
" \"reasoning_tokens\": 0\n",
" },\n",
" \"total_tokens\": 238\n",
" },\n",
" \"user\": null,\n",
" \"billing\": {\n",
" \"payer\": \"developer\"\n",
" },\n",
" \"completed_at\": 1772063449,\n",
" \"frequency_penalty\": 0.0,\n",
" \"presence_penalty\": 0.0,\n",
" \"prompt_cache_retention\": null,\n",
" \"store\": true\n",
"}\n",
"\n",
"Virgo: Today is a great day for new beginnings.\n"
]
}
],
"source": [
"print(response.model_dump_json(indent=2))\n",
"print(\"\\n\" + response.output_text)"
Expand All @@ -314,7 +463,7 @@
],
"metadata": {
"kernelspec": {
"display_name": "deploying-ai-env (3.12.7)",
"display_name": "deploying-ai-env (3.12.3)",
"language": "python",
"name": "python3"
},
Expand All @@ -328,7 +477,7 @@
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.12.7"
"version": "3.12.3"
}
},
"nbformat": 4,
Expand Down
Loading