diff --git a/week2/community-contributions/Day-4-Extension_to_project.ipynb b/week2/community-contributions/Day-4-Extension_to_project.ipynb new file mode 100644 index 0000000..cbc53cd --- /dev/null +++ b/week2/community-contributions/Day-4-Extension_to_project.ipynb @@ -0,0 +1,249 @@ +{ + "cells": [ + { + "cell_type": "markdown", + "id": "87c471b2-6a46-47f6-9da9-81d2652dd1b6", + "metadata": {}, + "source": [ + "# The code given by tutor results in an error when more than 1 city name is entered." + ] + }, + { + "cell_type": "markdown", + "id": "d4c3cdc4-3af9-4b9e-a5d2-80cee3b120be", + "metadata": {}, + "source": [ + "# This code aims to solve that by giving proper prices for all the given cities" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "292b5152-8932-4341-b2c4-850f16a89e5e", + "metadata": {}, + "outputs": [], + "source": [ + "# imports\n", + "\n", + "import os\n", + "import json\n", + "from dotenv import load_dotenv\n", + "from openai import OpenAI\n", + "import gradio as gr\n" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "92d35c3d-cb2d-4ce8-a6da-3907ce3ce8b8", + "metadata": {}, + "outputs": [], + "source": [ + "# Initialization\n", + "\n", + "load_dotenv(override=True)\n", + "\n", + "openai_api_key = os.getenv('OPENAI_API_KEY')\n", + "if openai_api_key:\n", + " print(f\"OpenAI API Key exists and begins {openai_api_key[:8]}\")\n", + "else:\n", + " print(\"OpenAI API Key not set\")\n", + " \n", + "MODEL = \"gpt-4o-mini\"\n", + "openai = OpenAI()" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "54e11038-795c-4451-ad3b-f797abb57728", + "metadata": {}, + "outputs": [], + "source": [ + "system_message = \"You are a helpful assistant for an Airline called FlightAI. \"\n", + "system_message += \"Give short, courteous answers, no more than 1 sentence. \"\n", + "system_message += \"Always be accurate. If you don't know the answer, say so.\"" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "e06c982f-59f1-4e33-a1c1-2f56415efbde", + "metadata": { + "scrolled": true + }, + "outputs": [], + "source": [ + "# This function looks rather simpler than the one from my video, because we're taking advantage of the latest Gradio updates\n", + "\n", + "def chat(message, history):\n", + " messages = [{\"role\": \"system\", \"content\": system_message}] + history + [{\"role\": \"user\", \"content\": message}]\n", + " response = openai.chat.completions.create(model=MODEL, messages=messages)\n", + " return response.choices[0].message.content\n", + "\n", + "gr.ChatInterface(fn=chat, type=\"messages\").launch()" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "d895e0ff-c47f-4b01-b987-4a236c452ba6", + "metadata": {}, + "outputs": [], + "source": [ + "# we'll try to impliment methods handle multi inputs in the query\n", + "ticket_prices = {\"london\": \"$799\", \"paris\": \"$899\", \"tokyo\": \"$1400\", \"berlin\": \"$499\"}\n", + "\n", + "def get_ticket_price(destination_city):\n", + " print(f\"Tool get_ticket_price called for {destination_city}\")\n", + " #return_prices = []\n", + " #for city in destination_city:\n", + " city = destination_city.lower()\n", + " #return_prices.append(ticket_prices.get(city,\"unknown\"))\n", + " return ticket_prices.get(city,\"Unknown\")" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "e2387fe7-a7ac-4192-ad46-9ec2a9bc49fa", + "metadata": {}, + "outputs": [], + "source": [ + "get_ticket_price(\"paris\")" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "b63e229e-08c9-49b4-b7af-1883736f12cd", + "metadata": {}, + "outputs": [], + "source": [ + "# There's a particular dictionary structure that's required to describe our function:\n", + "\n", + "price_function = {\n", + " \"name\": \"get_ticket_price\",\n", + " \"description\": \"Get the price of a return ticket to the destination city. Call this whenever you need to know the ticket price, for example when a customer asks 'How much is a ticket to this city'\",\n", + " \"parameters\": {\n", + " \"type\": \"object\",\n", + " \"properties\": {\n", + " \"destination_city\": {\n", + " \"type\": \"string\",\n", + " \"description\": \"List of cities that the customer wants to travel to\",\n", + " },\n", + " },\n", + " \"required\": [\"destination_city\"],\n", + " \"additionalProperties\": False\n", + " }\n", + "}" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "0162af66-2ea4-4221-93df-dd22f0ad92f7", + "metadata": {}, + "outputs": [], + "source": [ + "# And this is included in a list of tools:\n", + "\n", + "tools = [{\"type\": \"function\", \"function\": price_function}]" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "8b2a5434-63d0-4519-907e-bce21852d48f", + "metadata": {}, + "outputs": [], + "source": [ + "def chat(message, history):\n", + " messages = [{\"role\": \"system\", \"content\": system_message}] + history + [{\"role\": \"user\", \"content\": message}]\n", + " response = openai.chat.completions.create(model=MODEL, messages=messages, tools=tools)\n", + " print(f\"response ----------------- \\n {response}\")\n", + " if response.choices[0].finish_reason==\"tool_calls\":\n", + " message = response.choices[0].message\n", + " print(f\"message: -----------------\\n\",message)\n", + " response, city = handle_tool_call(message)\n", + " # print('response is --------', response)\n", + " # print('city is ----------',city)\n", + " messages.append(message)\n", + " messages.extend(response)\n", + " response = openai.chat.completions.create(model=MODEL, messages=messages)\n", + " \n", + " return response.choices[0].message.content" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "d7dfa28c-95f8-4d25-8f3c-cd677bb4a4d1", + "metadata": {}, + "outputs": [], + "source": [ + "# We have to write that function handle_tool_call:\n", + "\n", + "def handle_tool_call(message):\n", + " responses = []\n", + " all_cities = []\n", + " for tool_call in message.tool_calls:\n", + " \n", + " arguments = json.loads(tool_call.function.arguments)\n", + " list_of_city = arguments.get('destination_city')\n", + " print(f'list of city is ======== {list_of_city}')\n", + " price = get_ticket_price(list_of_city)\n", + " print(f'price of ticket to {list_of_city} is {price}')\n", + " response = {\n", + " \"role\": \"tool\",\n", + " \"content\": json.dumps({\"destination_city\": list_of_city,\"price\": price}),\n", + " \"tool_call_id\": tool_call.id\n", + " }\n", + " responses.append(response)\n", + " all_cities.append(list_of_city)\n", + " print(f'responses ====== {responses}')\n", + " print(f'cities ======= {all_cities}')\n", + " return responses,all_cities" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "15a4152d-6455-4116-bb63-6700eedf0626", + "metadata": {}, + "outputs": [], + "source": [ + "gr.ChatInterface(fn=chat, type=\"messages\").launch()" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "c6b0fcfa-38b7-4063-933e-1c8177bf55f1", + "metadata": {}, + "outputs": [], + "source": [] + } + ], + "metadata": { + "kernelspec": { + "display_name": "Python 3 (ipykernel)", + "language": "python", + "name": "python3" + }, + "language_info": { + "codemirror_mode": { + "name": "ipython", + "version": 3 + }, + "file_extension": ".py", + "mimetype": "text/x-python", + "name": "python", + "nbconvert_exporter": "python", + "pygments_lexer": "ipython3", + "version": "3.11.13" + } + }, + "nbformat": 4, + "nbformat_minor": 5 +}