Skip to content
Closed
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
128 changes: 106 additions & 22 deletions 1_foundations/4_lab4.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@
},
{
"cell_type": "code",
"execution_count": null,
"execution_count": 1,
"metadata": {},
"outputs": [],
"source": [
Expand All @@ -51,21 +51,49 @@
},
{
"cell_type": "code",
"execution_count": null,
"execution_count": 5,
"metadata": {},
"outputs": [],
"outputs": [
{
"data": {
"text/plain": [
"True"
]
},
"execution_count": 5,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"# The usual start\n",
"\n",
"load_dotenv(override=True)\n",
"openai = OpenAI()"
"load_dotenv(override=True)\n"
]
},
{
"cell_type": "code",
"execution_count": null,
"execution_count": 7,
"metadata": {},
"outputs": [],
"source": [
"gemini = OpenAI(api_key=os.environ.get(\"GOOGLE_API_KEY\"), base_url=\"https://generativelanguage.googleapis.com/v1beta/openai/\")"
]
},
{
"cell_type": "code",
"execution_count": 8,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"Pushover user found and looks good\n",
"Pushover token found and looks good\n"
]
}
],
"source": [
"# For pushover\n",
"\n",
Expand All @@ -92,7 +120,7 @@
},
{
"cell_type": "code",
"execution_count": null,
"execution_count": 9,
"metadata": {},
"outputs": [],
"source": [
Expand All @@ -106,14 +134,22 @@
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"Push: NIGGA YEAHHHHH\n"
]
}
],
"source": [
"push(\"HEY!!\")"
"push(\"HEYY\")"
]
},
{
"cell_type": "code",
"execution_count": null,
"execution_count": 14,
"metadata": {},
"outputs": [],
"source": [
Expand All @@ -124,7 +160,7 @@
},
{
"cell_type": "code",
"execution_count": null,
"execution_count": 15,
"metadata": {},
"outputs": [],
"source": [
Expand All @@ -135,7 +171,7 @@
},
{
"cell_type": "code",
"execution_count": null,
"execution_count": 16,
"metadata": {},
"outputs": [],
"source": [
Expand All @@ -158,7 +194,7 @@
},
{
"cell_type": "code",
"execution_count": null,
"execution_count": 17,
"metadata": {},
"outputs": [],
"source": [
Expand All @@ -178,7 +214,7 @@
},
{
"cell_type": "code",
"execution_count": null,
"execution_count": 18,
"metadata": {},
"outputs": [],
"source": [
Expand All @@ -188,16 +224,46 @@
},
{
"cell_type": "code",
"execution_count": null,
"execution_count": 19,
"metadata": {},
"outputs": [],
"outputs": [
{
"data": {
"text/plain": [
"[{'type': 'function',\n",
" 'function': {'name': 'record_user_details',\n",
" 'description': 'Use this tool to record that a user is interested in being in touch and provided an email address',\n",
" 'parameters': {'type': 'object',\n",
" 'properties': {'email': {'type': 'string',\n",
" 'description': 'The email address of this user'},\n",
" 'name': {'type': 'string',\n",
" 'description': \"The user's name, if they provided it\"},\n",
" 'notes': {'type': 'string',\n",
" 'description': \"Any additional info about the conversation that's worth recording to give context\"}},\n",
" 'required': ['email'],\n",
" 'additionalProperties': False}}},\n",
" {'type': 'function',\n",
" 'function': {'name': 'record_unknown_question',\n",
" 'description': \"Always use this tool to record any question that couldn't be answered as you didn't know the answer\",\n",
" 'parameters': {'type': 'object',\n",
" 'properties': {'question': {'type': 'string',\n",
" 'description': \"The question that couldn't be answered\"}},\n",
" 'required': ['question'],\n",
" 'additionalProperties': False}}}]"
]
},
"execution_count": 19,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"tools"
]
},
{
"cell_type": "code",
"execution_count": null,
"execution_count": 20,
"metadata": {},
"outputs": [],
"source": [
Expand Down Expand Up @@ -234,16 +300,34 @@
},
{
"cell_type": "code",
"execution_count": null,
"execution_count": 24,
"metadata": {},
"outputs": [],
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"Push: Recording this is a really hard question asked that I couldn't answer\n"
]
},
{
"data": {
"text/plain": [
"'OK'"
]
},
"execution_count": 24,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"globals()[\"record_unknown_question\"](\"this is a really hard question\")"
]
},
{
"cell_type": "code",
"execution_count": null,
"execution_count": 25,
"metadata": {},
"outputs": [],
"source": [
Expand Down Expand Up @@ -327,14 +411,14 @@
"source": [
"def chat(message, history):\n",
" messages = [{\"role\": \"system\", \"content\": system_prompt}] + history + [{\"role\": \"user\", \"content\": message}]\n",
" response = openai.chat.completions.create(model=\"gpt-5.4-mini\", messages=messages, tools=tools)\n",
" response = gemini.chat.completions.create(model=\"gpt-5.4-mini\", messages=messages, tools=tools)\n",
" while response.choices[0].finish_reason==\"tool_calls\":\n",
" message = response.choices[0].message\n",
" tool_calls = message.tool_calls\n",
" results = handle_tool_calls(tool_calls)\n",
" messages.append(message)\n",
" messages.extend(results)\n",
" response = openai.chat.completions.create(model=\"gpt-5.4-mini\", messages=messages, tools=tools)\n",
" response = gemini.chat.completions.create(model=\"gpt-5.4-mini\", messages=messages, tools=tools)\n",
" return response.choices[0].message.content"
]
},
Expand Down
Loading