diff --git a/week2/day4.ipynb b/week2/day4.ipynb index 1a8d72f..3ce0713 100644 --- a/week2/day4.ipynb +++ b/week2/day4.ipynb @@ -150,6 +150,72 @@ "}" ] }, + { + "cell_type": "code", + "execution_count": null, + "id": "b6c73b6b-1f9f-4df3-bd36-f6c8bf45a670", + "metadata": {}, + "outputs": [], + "source": [ + "from datetime import date, datetime\n", + "\n", + "availabilities = {\"london\": \"01/01/2025-31/03/2025\", \"paris\": \"01/01/2025-31/12/2025\", \"tokyo\": \"05/06/2024-04/06/2025\", \"berlin\": \"01/12/2024-30/09/2025\"}\n", + "\n", + "def get_availability_period(destination_city, flight_date):\n", + " \"\"\"Check for dates as LLM answers on this are not reliable.\"\"\"\n", + " print(f\"Tool get_availability_period called for {destination_city}\")\n", + " \n", + " city = destination_city.lower()\n", + " availability = availabilities.get(city)\n", + " \n", + " if availability:\n", + " current_date = date.today()\n", + " if len(flight_date.split(\"-\")) == 2:\n", + " flight_date = f\"{flight_date}-{str(current_date.year)}\"\n", + " flight_date = datetime.strptime(flight_date, \"%d-%m-%Y\").date()\n", + " \n", + " start_str, end_str = availability.split(\"-\")\n", + " start_date = datetime.strptime(start_str, \"%d/%m/%Y\").date()\n", + " end_date = datetime.strptime(end_str, \"%d/%m/%Y\").date()\n", + " \n", + " if start_date < flight_date < end_date:\n", + " return \"Available\"\n", + " else:\n", + " return \"Not available for requested flight dates\"" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "9680afe1-65d8-4094-8a09-6ef37ebdad2b", + "metadata": {}, + "outputs": [], + "source": [ + "\n", + "availability_function = {\n", + " \"name\": \"get_availability_period\",\n", + " \"description\": \"Check if the requested flight dates are within the availability period of the flight schedule to the destination city. Call this whenever you need to know whether there is a flight to the requested city at the provided travel dates of the user, for example when a customer asks 'I want to book a flight to Tokyo on 5 Nov 2025'. \\\n", + " If the user's date does not include a year, the date will refer to a date after the current date. State in your response whether there will be a flight to the destination city at the requested flight date. \\\n", + " For example, an availability of '01/08/2025-31/10/2025' means that there are flights between 01 Aug 25 and 31 Oct 2025. \\\n", + " Parse the user's flight date as string in the format DD-MM-YYYY or DD-MM if no year is given\",\n", + " \"parameters\": {\n", + " \"type\": \"object\",\n", + " \"properties\": {\n", + " \"destination_city\": {\n", + " \"type\": \"string\",\n", + " \"description\": \"The city that the customer wants to travel to\",\n", + " },\n", + " \"flight_date\": {\n", + " \"type\": \"string\",\n", + " \"description\": \"The date on which the customer wants to travel\",\n", + " },\n", + " },\n", + " \"required\": [\"destination_city\", \"flight_date\"],\n", + " \"additionalProperties\": False\n", + " }\n", + "}" + ] + }, { "cell_type": "code", "execution_count": null, @@ -159,7 +225,7 @@ "source": [ "# And this is included in a list of tools:\n", "\n", - "tools = [{\"type\": \"function\", \"function\": price_function}]" + "tools = [{\"type\": \"function\", \"function\": price_function}, {\"type\": \"function\", \"function\": availability_function}]" ] }, { @@ -189,9 +255,11 @@ "\n", " if response.choices[0].finish_reason==\"tool_calls\":\n", " message = response.choices[0].message\n", - " response, city = handle_tool_call(message)\n", + " response = handle_tools(message)\n", " messages.append(message)\n", + " print(f\"Message: \\n {messages}\")\n", " messages.append(response)\n", + " print(f\"Tool call response: \\n {response}\")\n", " response = openai.chat.completions.create(model=MODEL, messages=messages)\n", " \n", " return response.choices[0].message.content" @@ -219,6 +287,39 @@ " return response, city" ] }, + { + "cell_type": "code", + "execution_count": null, + "id": "d772048a-310e-4ff1-8c20-3dd7edb14a19", + "metadata": {}, + "outputs": [], + "source": [ + "# Extended function to handle two tools\n", + "\n", + "def handle_tools(message):\n", + " print(f\"message.tool_calls:\\n {message.tool_calls}\")\n", + " tool_call = message.tool_calls[0] \n", + " if message.tool_calls[0].function.name == \"get_ticket_price\":\n", + " arguments = json.loads(tool_call.function.arguments)\n", + " city = arguments.get('destination_city')\n", + " price = get_ticket_price(city)\n", + " content = json.dumps({\"destination_city\": city,\"price\": price})\n", + " elif message.tool_calls[0].function.name == \"get_availability_period\":\n", + " arguments = json.loads(tool_call.function.arguments)\n", + " city = arguments.get('destination_city')\n", + " flight_date = arguments.get('flight_date')\n", + " availability_dates = get_availability_period(city, flight_date)\n", + " content = json.dumps({\"destination_city\": city,\"availability_dates\": availability_dates})\n", + " \n", + " response = {\n", + " \"role\": \"tool\",\n", + " \"content\": content,\n", + " \"tool_call_id\": tool_call.id\n", + " }\n", + "\n", + " return response" + ] + }, { "cell_type": "code", "execution_count": null,