diff --git a/udo/w2d3/store_chatbot_with_pricelist.ipynb b/udo/w2d3/store_chatbot_with_pricelist.ipynb new file mode 100644 index 0000000..8f83c5a --- /dev/null +++ b/udo/w2d3/store_chatbot_with_pricelist.ipynb @@ -0,0 +1,121 @@ +{ + "cells": [ + { + "cell_type": "code", + "execution_count": null, + "id": "bacb12b8-592a-4515-9e27-e439f88c324b", + "metadata": {}, + "outputs": [], + "source": [ + "import os\n", + "from dotenv import load_dotenv\n", + "from openai import OpenAI\n", + "import gradio as gr\n", + "import re" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "3fd0515a-b126-49dc-8bd1-3085cca7f93f", + "metadata": {}, + "outputs": [], + "source": [ + "load_dotenv()\n", + "openai_api_key = os.getenv('OPENAI_API_KEY')" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "52dde4d9-c77b-4f4e-9cf6-2630d3ac743a", + "metadata": {}, + "outputs": [], + "source": [ + "openai = OpenAI()\n", + "MODEL = 'gpt-4o-mini'" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "f75ac2d7-c102-462e-b6bd-687f662f25e3", + "metadata": {}, + "outputs": [], + "source": [ + "sys_msg = \"You are an articulate, curteous, lovable clerk in a store that sells clothing.\"\n", + "products = {\n", + " r\"\\b[Bb]elts?\\b\": [\"belts\", 25.99],\n", + " r\"\\b[Ss]hoes?|[Ff]ootwear|[Kk]icks\\b\": [\"footwear\", 49.99],\n", + " r\"\\b[Tt]ies?\\b\": [\"ties\", 23.00]\n", + "}" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "050a4188-a5a6-47b4-bf02-2e391eb46097", + "metadata": {}, + "outputs": [], + "source": [ + "def chat(message, history):\n", + "\n", + " relevant_system_message = sys_msg\n", + " for product in products:\n", + " if re.search(product, message):\n", + " relevant_system_message += f\"\\nThe store does have {products[product][0]}, they only cost ${products[product][1]} today!\"\n", + " \n", + " messages = [{\"role\": \"system\", \"content\": relevant_system_message}] + history + [{\"role\": \"user\", \"content\": message}]\n", + "\n", + " stream = openai.chat.completions.create(model=MODEL, messages=messages, stream=True)\n", + "\n", + " response = \"\"\n", + " for chunk in stream:\n", + " response += chunk.choices[0].delta.content or ''\n", + " yield response" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "494ad214-4e95-4d78-b56a-f87c34fe6442", + "metadata": {}, + "outputs": [], + "source": [ + "sys_msg += \"\\nIf the customer asks for hats, you should respond that hats are currently sold out, \\\n", + "because they were so popular. We'll restock within a couple weeks.\"" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "e0e14baf-7842-4404-8e49-dda703157512", + "metadata": {}, + "outputs": [], + "source": [ + "gr.ChatInterface(fn=chat, type=\"messages\").launch()" + ] + } + ], + "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.11" + } + }, + "nbformat": 4, + "nbformat_minor": 5 +} diff --git a/udo/w2d4/geo_knowledge_bot.ipynb b/udo/w2d4/geo_knowledge_bot.ipynb new file mode 100644 index 0000000..621ffa5 --- /dev/null +++ b/udo/w2d4/geo_knowledge_bot.ipynb @@ -0,0 +1,274 @@ +{ + "cells": [ + { + "cell_type": "code", + "execution_count": null, + "id": "9aed4e5e-bb3e-422c-9178-1eaada1fbbe7", + "metadata": {}, + "outputs": [], + "source": [ + "import os\n", + "import json\n", + "from dotenv import load_dotenv\n", + "from openai import OpenAI\n", + "import gradio as gr" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "26da8d29-2a54-468e-93ff-2c732a89872d", + "metadata": {}, + "outputs": [], + "source": [ + "load_dotenv()\n", + "\n", + "openai_api_key = os.getenv('OPENAI_API_KEY')\n", + " \n", + "MODEL = \"gpt-4o-mini\"\n", + "openai = OpenAI()" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "e1b5bb0a-6862-49ee-83c9-3e1a7ba212b1", + "metadata": {}, + "outputs": [], + "source": [ + "system_message = \"You are a helpful assistant for questions regarding geography. \"\n", + "system_message += \"Give short, friendly answers, no more than 2 sentences. You are currently still under construction, but\\\n", + "you already have a lot of knowledge at your disposal. \"\n", + "system_message += \"Always be accurate. If you don't know the answer, say so.\"" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "bf33946a-4737-4882-9fb9-2707867a3fae", + "metadata": {}, + "outputs": [], + "source": [ + "#Here are the knowledge lists:\n", + "\n", + "country_capitals = {\"france\": \"Paris\", \"belgium\": \"Brussels\", \"mali\": \"Bamako\"}\n", + "country_populations = {\"france\": 68_170_000, \"belgium\": 11_820_000, \"mali\": 23_290_000}\n", + "capital_populations = {\"france\": 2_103_000, \"belgium\": 188_737, \"mali\": 4_228_000}" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "3a60dcf1-9ef5-4e67-9aad-0a471f6e794a", + "metadata": {}, + "outputs": [], + "source": [ + "def get_country_capital(country):\n", + " print(f\"Tool get_country_capital called for {country}\")\n", + " data_country = country.lower()\n", + " return country_capitals.get(data_country, 'Unknown') #This is a default" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "56585ff9-11c0-4057-8f5c-b87060103c74", + "metadata": {}, + "outputs": [], + "source": [ + "get_country_capital(\"Botswana\")" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "f18c051a-1bd0-4951-9fdb-a96199361320", + "metadata": {}, + "outputs": [], + "source": [ + "def get_country_population(country):\n", + " print(f\"Tool get_country_population called for {country}\")\n", + " data_country = country.lower()\n", + " return str(country_populations.get(data_country, 'Unknown'))" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "30081159-c29a-49e9-8727-d27e609a6f39", + "metadata": {}, + "outputs": [], + "source": [ + "def get_capital_population(country):\n", + " print(f\"Tool get_capital_population called for {country}\")\n", + " data_country = country.lower()\n", + " return str(capital_populations.get(data_country, 'Unknown'))" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "b54f7ee6-50ee-4153-907a-aa2ab3e52c5b", + "metadata": {}, + "outputs": [], + "source": [ + "country_capital_function = {\n", + " \"name\": \"get_country_capital\",\n", + " \"description\": \"Get the capital city of a country. Call this whenever you need to know what a country's capital is, for example when a user asks 'What's the capital of this country?'\",\n", + " \"parameters\": {\n", + " \"type\": \"object\",\n", + " \"properties\": {\n", + " \"country\": {\n", + " \"type\": \"string\",\n", + " \"description\": \"The country the user wants to know the capital of\",\n", + " },\n", + " },\n", + " \"required\": [\"country\"],\n", + " \"additionalProperties\": False\n", + " }\n", + "}" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "2b554d24-3d64-4bd2-bb3e-015efcaaa69b", + "metadata": {}, + "outputs": [], + "source": [ + "country_population_function = {\n", + " \"name\": \"get_country_population\",\n", + " \"description\": \"Get the population of a country. Call this whenever you need to know how many people live in a country, for example \\\n", + "when a user asks 'What's the population of this country?'\",\n", + " \"parameters\": {\n", + " \"type\": \"object\",\n", + " \"properties\": {\n", + " \"country\": {\n", + " \"type\": \"string\",\n", + " \"description\": \"The country the user wants to know the population of\",\n", + " },\n", + " },\n", + " \"required\": [\"country\"],\n", + " \"additionalProperties\": False\n", + " }\n", + "}" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "0cc418a0-443a-4445-95b1-f59a98ff294e", + "metadata": {}, + "outputs": [], + "source": [ + "capital_population_function = {\n", + " \"name\": \"get_capital_population\",\n", + " \"description\": \"Get the population of a country's capital. Call this whenever you need to know how many people live in the capital of a country\\\n", + ", for example when a user asks 'How many people live in the capital of this country?'\",\n", + " \"parameters\": {\n", + " \"type\": \"object\",\n", + " \"properties\": {\n", + " \"country\": {\n", + " \"type\": \"string\",\n", + " \"description\": \"The country whose capital the user wants to know the population of\",\n", + " },\n", + " },\n", + " \"required\": [\"country\"],\n", + " \"additionalProperties\": False\n", + " }\n", + "}" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "d8fad366-a911-42cb-8bc3-687241bd9557", + "metadata": {}, + "outputs": [], + "source": [ + "tools = [{\"type\": \"function\", \"function\": country_capital_function}, {\"type\": \"function\", \"function\": country_population_function}, {\"type\": \"function\", \"function\": capital_population_function}]" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "8660f43e-1c80-4cd1-b1ad-90753f9263c2", + "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", + "\n", + " if response.choices[0].finish_reason==\"tool_calls\":\n", + " message = response.choices[0].message\n", + " responses = handle_tool_call(message)\n", + " messages.append(message)\n", + " for response in responses:\n", + " messages.append(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": "a3fa504e-50f8-4d38-a856-be06b00dc53d", + "metadata": {}, + "outputs": [], + "source": [ + "def handle_tool_call(message):\n", + " responses = []\n", + " for tool_call in message.tool_calls:\n", + " arguments = json.loads(tool_call.function.arguments)\n", + " indata = arguments.get(\"country\") #in case of different functions with different numbers of arguments, this part will have to be 'abstracted'\n", + " function_name = tool_call.function.name\n", + " if function_name == 'get_country_capital':\n", + " outdata = get_country_capital(indata)\n", + " elif function_name == 'get_country_population':\n", + " outdata = get_country_population(indata)\n", + " else:\n", + " outdata = get_capital_population(indata)\n", + " \n", + " responses.append({\n", + " \"role\": \"tool\",\n", + " \"content\": json.dumps({\"country\": indata,\"outdata\": outdata}),\n", + " \"tool_call_id\": tool_call.id\n", + " })\n", + " return responses" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "1893cb4b-bd61-4617-85db-8698d98516b1", + "metadata": {}, + "outputs": [], + "source": [ + "gr.ChatInterface(fn=chat, type=\"messages\").launch()" + ] + } + ], + "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.11" + } + }, + "nbformat": 4, + "nbformat_minor": 5 +}