Browse Source
Modified day 4 to allow bot to a) get data from an sqlite database and to respond to compound questions which return multiple "tool requests" Simple case: "How much is ticket to Rome?" - will respond with a price for an economy ticket from database. Compound case: "What about Business and First Class? - will respond with prices for each.pull/30/head
3 changed files with 487 additions and 0 deletions
@ -0,0 +1,479 @@ |
|||||||
|
{ |
||||||
|
"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": 1, |
||||||
|
"id": "8b50bbe2-c0b1-49c3-9a5c-1ba7efa2bcb4", |
||||||
|
"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" |
||||||
|
] |
||||||
|
}, |
||||||
|
{ |
||||||
|
"cell_type": "code", |
||||||
|
"execution_count": 2, |
||||||
|
"id": "747e8786-9da8-4342-b6c9-f5f69c2e22ae", |
||||||
|
"metadata": {}, |
||||||
|
"outputs": [ |
||||||
|
{ |
||||||
|
"name": "stdout", |
||||||
|
"output_type": "stream", |
||||||
|
"text": [ |
||||||
|
"OpenAI API Key exists and begins sk-proj-\n" |
||||||
|
] |
||||||
|
} |
||||||
|
], |
||||||
|
"source": [ |
||||||
|
"# Initialization\n", |
||||||
|
"\n", |
||||||
|
"load_dotenv()\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": 3, |
||||||
|
"id": "0a521d84-d07c-49ab-a0df-d6451499ed97", |
||||||
|
"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.\"\n", |
||||||
|
"system_message += \"Do not make up answers.\"" |
||||||
|
] |
||||||
|
}, |
||||||
|
{ |
||||||
|
"cell_type": "code", |
||||||
|
"execution_count": 4, |
||||||
|
"id": "61a2a15d-b559-4844-b377-6bd5cb4949f6", |
||||||
|
"metadata": {}, |
||||||
|
"outputs": [ |
||||||
|
{ |
||||||
|
"name": "stdout", |
||||||
|
"output_type": "stream", |
||||||
|
"text": [ |
||||||
|
"* Running on local URL: http://127.0.0.1:7860\n", |
||||||
|
"\n", |
||||||
|
"To create a public link, set `share=True` in `launch()`.\n" |
||||||
|
] |
||||||
|
}, |
||||||
|
{ |
||||||
|
"data": { |
||||||
|
"text/html": [ |
||||||
|
"<div><iframe src=\"http://127.0.0.1:7860/\" width=\"100%\" height=\"500\" allow=\"autoplay; camera; microphone; clipboard-read; clipboard-write;\" frameborder=\"0\" allowfullscreen></iframe></div>" |
||||||
|
], |
||||||
|
"text/plain": [ |
||||||
|
"<IPython.core.display.HTML object>" |
||||||
|
] |
||||||
|
}, |
||||||
|
"metadata": {}, |
||||||
|
"output_type": "display_data" |
||||||
|
}, |
||||||
|
{ |
||||||
|
"data": { |
||||||
|
"text/plain": [] |
||||||
|
}, |
||||||
|
"execution_count": 4, |
||||||
|
"metadata": {}, |
||||||
|
"output_type": "execute_result" |
||||||
|
} |
||||||
|
], |
||||||
|
"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": "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": 5, |
||||||
|
"id": "08fba41a-955c-4b97-84b8-592a0fb23055", |
||||||
|
"metadata": { |
||||||
|
"editable": true, |
||||||
|
"slideshow": { |
||||||
|
"slide_type": "" |
||||||
|
}, |
||||||
|
"tags": [] |
||||||
|
}, |
||||||
|
"outputs": [], |
||||||
|
"source": [ |
||||||
|
"import sqlite3\n", |
||||||
|
"def get_ticket_price(destination_city, ticket_class):\n", |
||||||
|
" print(f\"Tool get_ticket_price called for {destination_city}, {ticket_class}\")\n", |
||||||
|
" conn = sqlite3.connect(\"prices.db\")\n", |
||||||
|
" cursor = conn.cursor()\n", |
||||||
|
" query = f\"select prices.{ticket_class.lower()} from prices where lower(prices.Destination) = '{destination_city.lower()}'\"\n", |
||||||
|
" print (f'{query=}')\n", |
||||||
|
" try:\n", |
||||||
|
" cursor.execute(query)\n", |
||||||
|
" results = cursor.fetchall()\n", |
||||||
|
" if results:\n", |
||||||
|
" return f\"${results[0][0]}\"\n", |
||||||
|
" else:\n", |
||||||
|
" return \"Unknown\"\n", |
||||||
|
" except sqlite3.Error as e:\n", |
||||||
|
" print(\"Error:\", e)\n", |
||||||
|
" finally:\n", |
||||||
|
" cursor.close()\n", |
||||||
|
" conn.close()" |
||||||
|
] |
||||||
|
}, |
||||||
|
{ |
||||||
|
"cell_type": "code", |
||||||
|
"execution_count": 6, |
||||||
|
"id": "a3af8709-45fc-4aac-aa8b-f9068f5597e1", |
||||||
|
"metadata": {}, |
||||||
|
"outputs": [ |
||||||
|
{ |
||||||
|
"name": "stdout", |
||||||
|
"output_type": "stream", |
||||||
|
"text": [ |
||||||
|
"Tool get_ticket_price called for Rome, business\n", |
||||||
|
"query=\"select prices.business from prices where lower(prices.Destination) = 'rome'\"\n" |
||||||
|
] |
||||||
|
}, |
||||||
|
{ |
||||||
|
"data": { |
||||||
|
"text/plain": [ |
||||||
|
"'$1050'" |
||||||
|
] |
||||||
|
}, |
||||||
|
"execution_count": 6, |
||||||
|
"metadata": {}, |
||||||
|
"output_type": "execute_result" |
||||||
|
} |
||||||
|
], |
||||||
|
"source": [ |
||||||
|
"get_ticket_price(\"Rome\", \"business\")" |
||||||
|
] |
||||||
|
}, |
||||||
|
{ |
||||||
|
"cell_type": "code", |
||||||
|
"execution_count": 7, |
||||||
|
"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 for the given ticket class. Call this whenever you need to know the ticket price, for example when a customer asks 'How much is a First Class ticket to this 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", |
||||||
|
" \"ticket_class\": {\n", |
||||||
|
" \"type\": \"string\",\n", |
||||||
|
" \"description\": \"The ticket class\",\n", |
||||||
|
" \"default\": \"economy\"\n", |
||||||
|
" }\n", |
||||||
|
" },\n", |
||||||
|
" \"required\": [\"destination_city\"],\n", |
||||||
|
" \"additionalProperties\": False\n", |
||||||
|
" }\n", |
||||||
|
"}" |
||||||
|
] |
||||||
|
}, |
||||||
|
{ |
||||||
|
"cell_type": "code", |
||||||
|
"execution_count": 8, |
||||||
|
"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": "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": 81, |
||||||
|
"id": "199a54f5-b8c8-4e6f-8731-f60f823bdef2", |
||||||
|
"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", |
||||||
|
" #this means that openai has returned 1 or more tool requests that need to be addressed\n", |
||||||
|
" message = response.choices[0].message\n", |
||||||
|
" tool_responses = handle_tool_calls(message)\n", |
||||||
|
" messages.append(message)\n", |
||||||
|
" for tool_response in tool_responses:\n", |
||||||
|
" messages.append(tool_response)\n", |
||||||
|
" response = openai.chat.completions.create(model=MODEL, messages=messages)\n", |
||||||
|
" return response.choices[0].message.content" |
||||||
|
] |
||||||
|
}, |
||||||
|
{ |
||||||
|
"cell_type": "code", |
||||||
|
"execution_count": 83, |
||||||
|
"id": "710a9447-b3a5-4615-a0aa-38a46ccb4467", |
||||||
|
"metadata": {}, |
||||||
|
"outputs": [], |
||||||
|
"source": [ |
||||||
|
"def handle_tool_calls(message):\n", |
||||||
|
" \n", |
||||||
|
" tool_responses = []\n", |
||||||
|
" #iterate through tool calls\n", |
||||||
|
" for tool_call in message.tool_calls:\n", |
||||||
|
" #json parameters \n", |
||||||
|
" arguments = json.loads(tool_call.function.arguments)\n", |
||||||
|
" \n", |
||||||
|
" #look up argument\n", |
||||||
|
" city = arguments.get('destination_city')\n", |
||||||
|
" ticket_class = arguments.get('ticket_class')\n", |
||||||
|
" \n", |
||||||
|
" #check for empty parameter\n", |
||||||
|
" if not ticket_class:\n", |
||||||
|
" ticket_class = 'economy'\n", |
||||||
|
" #call the tool\n", |
||||||
|
" price = get_ticket_price(city, ticket_class)\n", |
||||||
|
" \n", |
||||||
|
" #build a response - note role = tool\n", |
||||||
|
" response = {\n", |
||||||
|
" \"role\": \"tool\",\n", |
||||||
|
" \"content\": json.dumps({\"destination_city\": city,\"ticket_class\": ticket_class, \"price\": price}),\n", |
||||||
|
" #link response to request using id\n", |
||||||
|
" \"tool_call_id\": tool_call.id\n", |
||||||
|
" }\n", |
||||||
|
" tool_responses.append(response)\n", |
||||||
|
" return tool_responses" |
||||||
|
] |
||||||
|
}, |
||||||
|
{ |
||||||
|
"cell_type": "code", |
||||||
|
"execution_count": 84, |
||||||
|
"id": "f4be8a71-b19e-4c2f-80df-f59ff2661f14", |
||||||
|
"metadata": {}, |
||||||
|
"outputs": [ |
||||||
|
{ |
||||||
|
"name": "stdout", |
||||||
|
"output_type": "stream", |
||||||
|
"text": [ |
||||||
|
"* Running on local URL: http://127.0.0.1:7884\n", |
||||||
|
"\n", |
||||||
|
"To create a public link, set `share=True` in `launch()`.\n" |
||||||
|
] |
||||||
|
}, |
||||||
|
{ |
||||||
|
"data": { |
||||||
|
"text/html": [ |
||||||
|
"<div><iframe src=\"http://127.0.0.1:7884/\" width=\"100%\" height=\"500\" allow=\"autoplay; camera; microphone; clipboard-read; clipboard-write;\" frameborder=\"0\" allowfullscreen></iframe></div>" |
||||||
|
], |
||||||
|
"text/plain": [ |
||||||
|
"<IPython.core.display.HTML object>" |
||||||
|
] |
||||||
|
}, |
||||||
|
"metadata": {}, |
||||||
|
"output_type": "display_data" |
||||||
|
}, |
||||||
|
{ |
||||||
|
"data": { |
||||||
|
"text/plain": [] |
||||||
|
}, |
||||||
|
"execution_count": 84, |
||||||
|
"metadata": {}, |
||||||
|
"output_type": "execute_result" |
||||||
|
}, |
||||||
|
{ |
||||||
|
"name": "stdout", |
||||||
|
"output_type": "stream", |
||||||
|
"text": [ |
||||||
|
"IN CHAT Before Checking for tool calls : response=ChatCompletion(id='chatcmpl-AaPsfsMvh2yG1leVL1fFaM4Dw24Hd', choices=[Choice(finish_reason='tool_calls', index=0, logprobs=None, message=ChatCompletionMessage(content=None, refusal=None, role='assistant', audio=None, function_call=None, tool_calls=[ChatCompletionMessageToolCall(id='call_ao86rTi80FlHa9vdJ4RlsRhG', function=Function(arguments='{\"destination_city\": \"Rome\", \"ticket_class\": \"first\"}', name='get_ticket_price'), type='function'), ChatCompletionMessageToolCall(id='call_PTmz2Z17DutHPigINwidprUl', function=Function(arguments='{\"destination_city\": \"Rome\", \"ticket_class\": \"business\"}', name='get_ticket_price'), type='function')]))], created=1733242657, model='gpt-4o-mini-2024-07-18', object='chat.completion', service_tier=None, system_fingerprint='fp_0705bf87c0', usage=CompletionUsage(completion_tokens=58, prompt_tokens=171, total_tokens=229, completion_tokens_details=CompletionTokensDetails(accepted_prediction_tokens=0, audio_tokens=0, reasoning_tokens=0, rejected_prediction_tokens=0), prompt_tokens_details=PromptTokensDetails(audio_tokens=0, cached_tokens=0)))\n", |
||||||
|
"\n", |
||||||
|
"\n", |
||||||
|
"IN CHAT: message=ChatCompletionMessage(content=None, refusal=None, role='assistant', audio=None, function_call=None, tool_calls=[ChatCompletionMessageToolCall(id='call_ao86rTi80FlHa9vdJ4RlsRhG', function=Function(arguments='{\"destination_city\": \"Rome\", \"ticket_class\": \"first\"}', name='get_ticket_price'), type='function'), ChatCompletionMessageToolCall(id='call_PTmz2Z17DutHPigINwidprUl', function=Function(arguments='{\"destination_city\": \"Rome\", \"ticket_class\": \"business\"}', name='get_ticket_price'), type='function')])\n", |
||||||
|
"\n", |
||||||
|
"\n", |
||||||
|
"\n", |
||||||
|
"\n", |
||||||
|
"**** In handle_tool_calls ****: tool_call=ChatCompletionMessageToolCall(id='call_ao86rTi80FlHa9vdJ4RlsRhG', function=Function(arguments='{\"destination_city\": \"Rome\", \"ticket_class\": \"first\"}', name='get_ticket_price'), type='function')\n", |
||||||
|
"\n", |
||||||
|
"\n", |
||||||
|
"Tool get_ticket_price called for Rome, first\n", |
||||||
|
"query=\"select prices.first from prices where lower(prices.Destination) = 'rome'\"\n", |
||||||
|
"\n", |
||||||
|
"\n", |
||||||
|
"**** In handle_tool_calls ****: tool_responses=[{'role': 'tool', 'content': '{\"destination_city\": \"Rome\", \"ticket_class\": \"first\", \"price\": \"$3150\"}', 'tool_call_id': 'call_ao86rTi80FlHa9vdJ4RlsRhG'}]\n", |
||||||
|
"\n", |
||||||
|
"\n", |
||||||
|
"\n", |
||||||
|
"\n", |
||||||
|
"**** In handle_tool_calls ****: tool_call=ChatCompletionMessageToolCall(id='call_PTmz2Z17DutHPigINwidprUl', function=Function(arguments='{\"destination_city\": \"Rome\", \"ticket_class\": \"business\"}', name='get_ticket_price'), type='function')\n", |
||||||
|
"\n", |
||||||
|
"\n", |
||||||
|
"Tool get_ticket_price called for Rome, business\n", |
||||||
|
"query=\"select prices.business from prices where lower(prices.Destination) = 'rome'\"\n", |
||||||
|
"\n", |
||||||
|
"\n", |
||||||
|
"**** In handle_tool_calls ****: tool_responses=[{'role': 'tool', 'content': '{\"destination_city\": \"Rome\", \"ticket_class\": \"first\", \"price\": \"$3150\"}', 'tool_call_id': 'call_ao86rTi80FlHa9vdJ4RlsRhG'}, {'role': 'tool', 'content': '{\"destination_city\": \"Rome\", \"ticket_class\": \"business\", \"price\": \"$1050\"}', 'tool_call_id': 'call_PTmz2Z17DutHPigINwidprUl'}]\n", |
||||||
|
"\n", |
||||||
|
"\n", |
||||||
|
"IN CHAT After handle_tool_call:\n", |
||||||
|
"\n", |
||||||
|
" tool_responses=[{'role': 'tool', 'content': '{\"destination_city\": \"Rome\", \"ticket_class\": \"first\", \"price\": \"$3150\"}', 'tool_call_id': 'call_ao86rTi80FlHa9vdJ4RlsRhG'}, {'role': 'tool', 'content': '{\"destination_city\": \"Rome\", \"ticket_class\": \"business\", \"price\": \"$1050\"}', 'tool_call_id': 'call_PTmz2Z17DutHPigINwidprUl'}]\n", |
||||||
|
"\n", |
||||||
|
"\n", |
||||||
|
"IN CHAT message sent to completions before final response: messages=[{'role': 'system', 'content': \"You are a helpful assistant for an Airline called FlightAI. Give short, courteous answers, no more than 1 sentence. Always be accurate. If you don't know the answer, say so.Do not make up answers.\"}, {'role': 'user', 'content': 'how much is a ticket to rome for first class and business class?'}, ChatCompletionMessage(content=None, refusal=None, role='assistant', audio=None, function_call=None, tool_calls=[ChatCompletionMessageToolCall(id='call_ao86rTi80FlHa9vdJ4RlsRhG', function=Function(arguments='{\"destination_city\": \"Rome\", \"ticket_class\": \"first\"}', name='get_ticket_price'), type='function'), ChatCompletionMessageToolCall(id='call_PTmz2Z17DutHPigINwidprUl', function=Function(arguments='{\"destination_city\": \"Rome\", \"ticket_class\": \"business\"}', name='get_ticket_price'), type='function')]), {'role': 'tool', 'content': '{\"destination_city\": \"Rome\", \"ticket_class\": \"first\", \"price\": \"$3150\"}', 'tool_call_id': 'call_ao86rTi80FlHa9vdJ4RlsRhG'}, {'role': 'tool', 'content': '{\"destination_city\": \"Rome\", \"ticket_class\": \"business\", \"price\": \"$1050\"}', 'tool_call_id': 'call_PTmz2Z17DutHPigINwidprUl'}]\n", |
||||||
|
"\n", |
||||||
|
"\n", |
||||||
|
"IN CHAT: final chat completions call response=ChatCompletion(id='chatcmpl-AaPsg2AjXrHMNtef5J0Lmh2jcy996', choices=[Choice(finish_reason='stop', index=0, logprobs=None, message=ChatCompletionMessage(content='A first-class ticket to Rome is $3150, and a business-class ticket is $1050.', refusal=None, role='assistant', audio=None, function_call=None, tool_calls=None))], created=1733242658, model='gpt-4o-mini-2024-07-18', object='chat.completion', service_tier=None, system_fingerprint='fp_0705bf87c0', usage=CompletionUsage(completion_tokens=21, prompt_tokens=182, total_tokens=203, completion_tokens_details=CompletionTokensDetails(accepted_prediction_tokens=0, audio_tokens=0, reasoning_tokens=0, rejected_prediction_tokens=0), prompt_tokens_details=PromptTokensDetails(audio_tokens=0, cached_tokens=0)))\n", |
||||||
|
"\n", |
||||||
|
"\n", |
||||||
|
"IN CHAT: final chat completions call response.choices[0].message.content='A first-class ticket to Rome is $3150, and a business-class ticket is $1050.'\n", |
||||||
|
"\n", |
||||||
|
"\n", |
||||||
|
"IN CHAT Before Checking for tool calls : response=ChatCompletion(id='chatcmpl-AaPtBcgwsaTz0Z3h9l2yFVBv61IPA', choices=[Choice(finish_reason='tool_calls', index=0, logprobs=None, message=ChatCompletionMessage(content=None, refusal=None, role='assistant', audio=None, function_call=None, tool_calls=[ChatCompletionMessageToolCall(id='call_MOh9s41BQ4psUq2Zp7SwH1wE', function=Function(arguments='{\"destination_city\": \"Paris\"}', name='get_ticket_price'), type='function'), ChatCompletionMessageToolCall(id='call_CfO2DcWoJB2cxy7gMNZxgRG2', function=Function(arguments='{\"destination_city\": \"Berlin\"}', name='get_ticket_price'), type='function')]))], created=1733242689, model='gpt-4o-mini-2024-07-18', object='chat.completion', service_tier=None, system_fingerprint='fp_0705bf87c0', usage=CompletionUsage(completion_tokens=48, prompt_tokens=209, total_tokens=257, completion_tokens_details=CompletionTokensDetails(accepted_prediction_tokens=0, audio_tokens=0, reasoning_tokens=0, rejected_prediction_tokens=0), prompt_tokens_details=PromptTokensDetails(audio_tokens=0, cached_tokens=0)))\n", |
||||||
|
"\n", |
||||||
|
"\n", |
||||||
|
"IN CHAT: message=ChatCompletionMessage(content=None, refusal=None, role='assistant', audio=None, function_call=None, tool_calls=[ChatCompletionMessageToolCall(id='call_MOh9s41BQ4psUq2Zp7SwH1wE', function=Function(arguments='{\"destination_city\": \"Paris\"}', name='get_ticket_price'), type='function'), ChatCompletionMessageToolCall(id='call_CfO2DcWoJB2cxy7gMNZxgRG2', function=Function(arguments='{\"destination_city\": \"Berlin\"}', name='get_ticket_price'), type='function')])\n", |
||||||
|
"\n", |
||||||
|
"\n", |
||||||
|
"\n", |
||||||
|
"\n", |
||||||
|
"**** In handle_tool_calls ****: tool_call=ChatCompletionMessageToolCall(id='call_MOh9s41BQ4psUq2Zp7SwH1wE', function=Function(arguments='{\"destination_city\": \"Paris\"}', name='get_ticket_price'), type='function')\n", |
||||||
|
"\n", |
||||||
|
"\n", |
||||||
|
"Tool get_ticket_price called for Paris, economy\n", |
||||||
|
"query=\"select prices.economy from prices where lower(prices.Destination) = 'paris'\"\n", |
||||||
|
"\n", |
||||||
|
"\n", |
||||||
|
"**** In handle_tool_calls ****: tool_responses=[{'role': 'tool', 'content': '{\"destination_city\": \"Paris\", \"ticket_class\": \"economy\", \"price\": \"$400\"}', 'tool_call_id': 'call_MOh9s41BQ4psUq2Zp7SwH1wE'}]\n", |
||||||
|
"\n", |
||||||
|
"\n", |
||||||
|
"\n", |
||||||
|
"\n", |
||||||
|
"**** In handle_tool_calls ****: tool_call=ChatCompletionMessageToolCall(id='call_CfO2DcWoJB2cxy7gMNZxgRG2', function=Function(arguments='{\"destination_city\": \"Berlin\"}', name='get_ticket_price'), type='function')\n", |
||||||
|
"\n", |
||||||
|
"\n", |
||||||
|
"Tool get_ticket_price called for Berlin, economy\n", |
||||||
|
"query=\"select prices.economy from prices where lower(prices.Destination) = 'berlin'\"\n", |
||||||
|
"\n", |
||||||
|
"\n", |
||||||
|
"**** In handle_tool_calls ****: tool_responses=[{'role': 'tool', 'content': '{\"destination_city\": \"Paris\", \"ticket_class\": \"economy\", \"price\": \"$400\"}', 'tool_call_id': 'call_MOh9s41BQ4psUq2Zp7SwH1wE'}, {'role': 'tool', 'content': '{\"destination_city\": \"Berlin\", \"ticket_class\": \"economy\", \"price\": \"$400\"}', 'tool_call_id': 'call_CfO2DcWoJB2cxy7gMNZxgRG2'}]\n", |
||||||
|
"\n", |
||||||
|
"\n", |
||||||
|
"IN CHAT After handle_tool_call:\n", |
||||||
|
"\n", |
||||||
|
" tool_responses=[{'role': 'tool', 'content': '{\"destination_city\": \"Paris\", \"ticket_class\": \"economy\", \"price\": \"$400\"}', 'tool_call_id': 'call_MOh9s41BQ4psUq2Zp7SwH1wE'}, {'role': 'tool', 'content': '{\"destination_city\": \"Berlin\", \"ticket_class\": \"economy\", \"price\": \"$400\"}', 'tool_call_id': 'call_CfO2DcWoJB2cxy7gMNZxgRG2'}]\n", |
||||||
|
"\n", |
||||||
|
"\n", |
||||||
|
"IN CHAT message sent to completions before final response: messages=[{'role': 'system', 'content': \"You are a helpful assistant for an Airline called FlightAI. Give short, courteous answers, no more than 1 sentence. Always be accurate. If you don't know the answer, say so.Do not make up answers.\"}, {'role': 'user', 'metadata': {'title': None}, 'content': 'how much is a ticket to rome for first class and business class?'}, {'role': 'assistant', 'metadata': {'title': None}, 'content': 'A first-class ticket to Rome is $3150, and a business-class ticket is $1050.'}, {'role': 'user', 'content': 'How much are tickets to Paris and Berlin?'}, ChatCompletionMessage(content=None, refusal=None, role='assistant', audio=None, function_call=None, tool_calls=[ChatCompletionMessageToolCall(id='call_MOh9s41BQ4psUq2Zp7SwH1wE', function=Function(arguments='{\"destination_city\": \"Paris\"}', name='get_ticket_price'), type='function'), ChatCompletionMessageToolCall(id='call_CfO2DcWoJB2cxy7gMNZxgRG2', function=Function(arguments='{\"destination_city\": \"Berlin\"}', name='get_ticket_price'), type='function')]), {'role': 'tool', 'content': '{\"destination_city\": \"Paris\", \"ticket_class\": \"economy\", \"price\": \"$400\"}', 'tool_call_id': 'call_MOh9s41BQ4psUq2Zp7SwH1wE'}, {'role': 'tool', 'content': '{\"destination_city\": \"Berlin\", \"ticket_class\": \"economy\", \"price\": \"$400\"}', 'tool_call_id': 'call_CfO2DcWoJB2cxy7gMNZxgRG2'}]\n", |
||||||
|
"\n", |
||||||
|
"\n", |
||||||
|
"IN CHAT: final chat completions call response=ChatCompletion(id='chatcmpl-AaPtC5kKqdouGRnfMEpjY0cdqiIJi', choices=[Choice(finish_reason='stop', index=0, logprobs=None, message=ChatCompletionMessage(content='Economy tickets to both Paris and Berlin are priced at $400.', refusal=None, role='assistant', audio=None, function_call=None, tool_calls=None))], created=1733242690, model='gpt-4o-mini-2024-07-18', object='chat.completion', service_tier=None, system_fingerprint='fp_0705bf87c0', usage=CompletionUsage(completion_tokens=14, prompt_tokens=210, total_tokens=224, completion_tokens_details=CompletionTokensDetails(accepted_prediction_tokens=0, audio_tokens=0, reasoning_tokens=0, rejected_prediction_tokens=0), prompt_tokens_details=PromptTokensDetails(audio_tokens=0, cached_tokens=0)))\n", |
||||||
|
"\n", |
||||||
|
"\n", |
||||||
|
"IN CHAT: final chat completions call response.choices[0].message.content='Economy tickets to both Paris and Berlin are priced at $400.'\n", |
||||||
|
"\n", |
||||||
|
"\n" |
||||||
|
] |
||||||
|
} |
||||||
|
], |
||||||
|
"source": [ |
||||||
|
"gr.ChatInterface(fn=chat, type=\"messages\").launch()" |
||||||
|
] |
||||||
|
}, |
||||||
|
{ |
||||||
|
"cell_type": "code", |
||||||
|
"execution_count": null, |
||||||
|
"id": "11c9da69-d0cf-4cf2-a49e-e5669deec47b", |
||||||
|
"metadata": {}, |
||||||
|
"outputs": [], |
||||||
|
"source": [ |
||||||
|
"how much is a ticket to rome for first class and business class?\n", |
||||||
|
"\n" |
||||||
|
] |
||||||
|
} |
||||||
|
], |
||||||
|
"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.2" |
||||||
|
} |
||||||
|
}, |
||||||
|
"nbformat": 4, |
||||||
|
"nbformat_minor": 5 |
||||||
|
} |
|
Binary file not shown.
Loading…
Reference in new issue