479 lines
14 KiB
Plaintext
479 lines
14 KiB
Plaintext
{
|
|
"cells": [
|
|
{
|
|
"cell_type": "markdown",
|
|
"id": "ddfa9ae6-69fe-444a-b994-8c4c5970a7ec",
|
|
"metadata": {},
|
|
"source": [
|
|
"# Project - Airline AI Assistant\n",
|
|
"\n",
|
|
"We'll now bring together what we've learned to make an AI Customer Support assistant for an Airline"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "code",
|
|
"execution_count": null,
|
|
"id": "8b50bbe2-c0b1-49c3-9a5c-1ba7efa2bcb4",
|
|
"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": "747e8786-9da8-4342-b6c9-f5f69c2e22ae",
|
|
"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-4.1-mini\"\n",
|
|
"openai = OpenAI()\n",
|
|
"\n",
|
|
"# As an alternative, if you'd like to use Ollama instead of OpenAI\n",
|
|
"# Check that Ollama is running for you locally (see week1/day2 exercise) then uncomment these next 2 lines\n",
|
|
"# MODEL = \"llama3.2\"\n",
|
|
"# openai = OpenAI(base_url='http://localhost:11434/v1', api_key='ollama')\n"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "code",
|
|
"execution_count": null,
|
|
"id": "0a521d84-d07c-49ab-a0df-d6451499ed97",
|
|
"metadata": {},
|
|
"outputs": [],
|
|
"source": [
|
|
"system_message = \"\"\"\n",
|
|
"You are a helpful assistant for an Airline called FlightAI.\n",
|
|
"Give short, courteous answers, no more than 1 sentence.\n",
|
|
"Always be accurate. If you don't know the answer, say so.\n",
|
|
"\"\"\""
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "code",
|
|
"execution_count": null,
|
|
"id": "61a2a15d-b559-4844-b377-6bd5cb4949f6",
|
|
"metadata": {},
|
|
"outputs": [],
|
|
"source": [
|
|
"def chat(message, history):\n",
|
|
" history = [{\"role\":h[\"role\"], \"content\":h[\"content\"]} for h in 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": "markdown",
|
|
"id": "36bedabf-a0a7-4985-ad8e-07ed6a55a3a4",
|
|
"metadata": {},
|
|
"source": [
|
|
"## Tools\n",
|
|
"\n",
|
|
"Tools are an incredibly powerful feature provided by the frontier LLMs.\n",
|
|
"\n",
|
|
"With tools, you can write a function, and have the LLM call that function as part of its response.\n",
|
|
"\n",
|
|
"Sounds almost spooky.. we're giving it the power to run code on our machine?\n",
|
|
"\n",
|
|
"Well, kinda."
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "code",
|
|
"execution_count": null,
|
|
"id": "0696acb1-0b05-4dc2-80d5-771be04f1fb2",
|
|
"metadata": {},
|
|
"outputs": [],
|
|
"source": [
|
|
"# Let's start by making a useful function\n",
|
|
"\n",
|
|
"ticket_prices = {\"london\": \"$799\", \"paris\": \"$899\", \"tokyo\": \"$1400\", \"berlin\": \"$499\"}\n",
|
|
"\n",
|
|
"def get_ticket_price(destination_city):\n",
|
|
" print(f\"Tool called for city {destination_city}\")\n",
|
|
" price = ticket_prices.get(destination_city.lower(), \"Unknown ticket price\")\n",
|
|
" return f\"The price of a ticket to {destination_city} is {price}\"\n"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "code",
|
|
"execution_count": null,
|
|
"id": "80ca4e09-6287-4d3f-997d-fa6afbcf6c85",
|
|
"metadata": {},
|
|
"outputs": [],
|
|
"source": [
|
|
"get_ticket_price(\"London\")"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "code",
|
|
"execution_count": null,
|
|
"id": "4afceded-7178-4c05-8fa6-9f2085e6a344",
|
|
"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.\",\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",
|
|
" },\n",
|
|
" \"required\": [\"destination_city\"],\n",
|
|
" \"additionalProperties\": False\n",
|
|
" }\n",
|
|
"}"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "code",
|
|
"execution_count": null,
|
|
"id": "bdca8679-935f-4e7f-97e6-e71a4d4f228c",
|
|
"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": "818b4b2b",
|
|
"metadata": {},
|
|
"outputs": [],
|
|
"source": [
|
|
"tools"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "markdown",
|
|
"id": "c3d3554f-b4e3-4ce7-af6f-68faa6dd2340",
|
|
"metadata": {},
|
|
"source": [
|
|
"## Getting OpenAI to use our Tool\n",
|
|
"\n",
|
|
"There's some fiddly stuff to allow OpenAI \"to call our tool\"\n",
|
|
"\n",
|
|
"What we actually do is give the LLM the opportunity to inform us that it wants us to run the tool.\n",
|
|
"\n",
|
|
"Here's how the new chat function looks:"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "code",
|
|
"execution_count": null,
|
|
"id": "ce9b0744-9c78-408d-b9df-9f6fd9ed78cf",
|
|
"metadata": {},
|
|
"outputs": [],
|
|
"source": [
|
|
"def chat(message, history):\n",
|
|
" history = [{\"role\":h[\"role\"], \"content\":h[\"content\"]} for h in 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",
|
|
" response = handle_tool_call(message)\n",
|
|
" messages.append(message)\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": "b0992986-ea09-4912-a076-8e5603ee631f",
|
|
"metadata": {},
|
|
"outputs": [],
|
|
"source": [
|
|
"# We have to write that function handle_tool_call:\n",
|
|
"\n",
|
|
"def handle_tool_call(message):\n",
|
|
" tool_call = message.tool_calls[0]\n",
|
|
" if tool_call.function.name == \"get_ticket_price\":\n",
|
|
" arguments = json.loads(tool_call.function.arguments)\n",
|
|
" city = arguments.get('destination_city')\n",
|
|
" price_details = get_ticket_price(city)\n",
|
|
" response = {\n",
|
|
" \"role\": \"tool\",\n",
|
|
" \"content\": price_details,\n",
|
|
" \"tool_call_id\": tool_call.id\n",
|
|
" }\n",
|
|
" return response"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "code",
|
|
"execution_count": null,
|
|
"id": "f4be8a71-b19e-4c2f-80df-f59ff2661f14",
|
|
"metadata": {},
|
|
"outputs": [],
|
|
"source": [
|
|
"gr.ChatInterface(fn=chat, type=\"messages\").launch()"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "markdown",
|
|
"id": "47f30fbe",
|
|
"metadata": {},
|
|
"source": [
|
|
"## Let's make a couple of improvements\n",
|
|
"\n",
|
|
"Handling multiple tool calls in 1 response\n",
|
|
"\n",
|
|
"Handling multiple tool calls 1 after another"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "code",
|
|
"execution_count": null,
|
|
"id": "b6f5c860",
|
|
"metadata": {},
|
|
"outputs": [],
|
|
"source": [
|
|
"def chat(message, history):\n",
|
|
" history = [{\"role\":h[\"role\"], \"content\":h[\"content\"]} for h in 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_calls(message)\n",
|
|
" messages.append(message)\n",
|
|
" messages.extend(responses)\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": "9c46a861",
|
|
"metadata": {},
|
|
"outputs": [],
|
|
"source": [
|
|
"def handle_tool_calls(message):\n",
|
|
" responses = []\n",
|
|
" for tool_call in message.tool_calls:\n",
|
|
" if tool_call.function.name == \"get_ticket_price\":\n",
|
|
" arguments = json.loads(tool_call.function.arguments)\n",
|
|
" city = arguments.get('destination_city')\n",
|
|
" price_details = get_ticket_price(city)\n",
|
|
" responses.append({\n",
|
|
" \"role\": \"tool\",\n",
|
|
" \"content\": price_details,\n",
|
|
" \"tool_call_id\": tool_call.id\n",
|
|
" })\n",
|
|
" return responses"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "code",
|
|
"execution_count": null,
|
|
"id": "95f02a4d",
|
|
"metadata": {},
|
|
"outputs": [],
|
|
"source": [
|
|
"gr.ChatInterface(fn=chat, type=\"messages\").launch()"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "code",
|
|
"execution_count": null,
|
|
"id": "cf262abc",
|
|
"metadata": {},
|
|
"outputs": [],
|
|
"source": [
|
|
"def chat(message, history):\n",
|
|
" history = [{\"role\":h[\"role\"], \"content\":h[\"content\"]} for h in 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",
|
|
" while response.choices[0].finish_reason==\"tool_calls\":\n",
|
|
" message = response.choices[0].message\n",
|
|
" responses = handle_tool_calls(message)\n",
|
|
" messages.append(message)\n",
|
|
" messages.extend(responses)\n",
|
|
" response = openai.chat.completions.create(model=MODEL, messages=messages, tools=tools)\n",
|
|
" \n",
|
|
" return response.choices[0].message.content"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "code",
|
|
"execution_count": null,
|
|
"id": "47d50e70",
|
|
"metadata": {},
|
|
"outputs": [],
|
|
"source": [
|
|
"import sqlite3\n"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "code",
|
|
"execution_count": null,
|
|
"id": "bb61a45d",
|
|
"metadata": {},
|
|
"outputs": [],
|
|
"source": [
|
|
"DB = \"prices.db\"\n",
|
|
"\n",
|
|
"with sqlite3.connect(DB) as conn:\n",
|
|
" cursor = conn.cursor()\n",
|
|
" cursor.execute('CREATE TABLE IF NOT EXISTS prices (city TEXT PRIMARY KEY, price REAL)')\n",
|
|
" conn.commit()"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "code",
|
|
"execution_count": null,
|
|
"id": "12c73b6a",
|
|
"metadata": {},
|
|
"outputs": [],
|
|
"source": [
|
|
"def get_ticket_price(city):\n",
|
|
" print(f\"DATABASE TOOL CALLED: Getting price for {city}\", flush=True)\n",
|
|
" with sqlite3.connect(DB) as conn:\n",
|
|
" cursor = conn.cursor()\n",
|
|
" cursor.execute('SELECT price FROM prices WHERE city = ?', (city.lower(),))\n",
|
|
" result = cursor.fetchone()\n",
|
|
" return f\"Ticket price to {city} is ${result[0]}\" if result else \"No price data available for this city\""
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "code",
|
|
"execution_count": null,
|
|
"id": "7cb2e079",
|
|
"metadata": {},
|
|
"outputs": [],
|
|
"source": [
|
|
"get_ticket_price(\"London\")"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "code",
|
|
"execution_count": null,
|
|
"id": "46e43463",
|
|
"metadata": {},
|
|
"outputs": [],
|
|
"source": [
|
|
"def set_ticket_price(city, price):\n",
|
|
" with sqlite3.connect(DB) as conn:\n",
|
|
" cursor = conn.cursor()\n",
|
|
" cursor.execute('INSERT INTO prices (city, price) VALUES (?, ?) ON CONFLICT(city) DO UPDATE SET price = ?', (city.lower(), price, price))\n",
|
|
" conn.commit()"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "code",
|
|
"execution_count": null,
|
|
"id": "9185228e",
|
|
"metadata": {},
|
|
"outputs": [],
|
|
"source": [
|
|
"ticket_prices = {\"london\":799, \"paris\": 899, \"tokyo\": 1420, \"sydney\": 2999}\n",
|
|
"for city, price in ticket_prices.items():\n",
|
|
" set_ticket_price(city, price)"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "code",
|
|
"execution_count": null,
|
|
"id": "cda459b9",
|
|
"metadata": {},
|
|
"outputs": [],
|
|
"source": [
|
|
"get_ticket_price(\"Tokyo\")"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "code",
|
|
"execution_count": null,
|
|
"id": "bfbfa251",
|
|
"metadata": {},
|
|
"outputs": [],
|
|
"source": [
|
|
"gr.ChatInterface(fn=chat, type=\"messages\").launch()"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "markdown",
|
|
"id": "d1a9e9c7",
|
|
"metadata": {},
|
|
"source": [
|
|
"## Exercise\n",
|
|
"\n",
|
|
"Add a tool to set the price of a ticket!"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "markdown",
|
|
"id": "6aeba34c",
|
|
"metadata": {},
|
|
"source": [
|
|
"<table style=\"margin: 0; text-align: left;\">\n",
|
|
" <tr>\n",
|
|
" <td style=\"width: 150px; height: 150px; vertical-align: middle;\">\n",
|
|
" <img src=\"../assets/business.jpg\" width=\"150\" height=\"150\" style=\"display: block;\" />\n",
|
|
" </td>\n",
|
|
" <td>\n",
|
|
" <h2 style=\"color:#181;\">Business Applications</h2>\n",
|
|
" <span style=\"color:#181;\">Hopefully this hardly needs to be stated! You now have the ability to give actions to your LLMs. This Airline Assistant can now do more than answer questions - it could interact with booking APIs to make bookings!</span>\n",
|
|
" </td>\n",
|
|
" </tr>\n",
|
|
"</table>"
|
|
]
|
|
}
|
|
],
|
|
"metadata": {
|
|
"kernelspec": {
|
|
"display_name": ".venv",
|
|
"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.12.9"
|
|
}
|
|
},
|
|
"nbformat": 4,
|
|
"nbformat_minor": 5
|
|
}
|