Browse Source

Duration of hearing and added quantity of tickets to purchase option

Duration of hearing and added quantity of tickets to purchase option
pull/295/head
arunabeshc 1 month ago
parent
commit
230697ea4d
  1. 166
      week2/community-contributions/AI Booking Chatbot.ipynb

166
week2/community-contributions/AI Booking Chatbot.ipynb

@ -147,17 +147,19 @@
"metadata": {},
"outputs": [],
"source": [
"def book_ticket(destination_city,price,availability):\n",
"def book_ticket(destination_city,price,availability,no_of_tickets):\n",
" status=\"\"\n",
" if availability == 0:\n",
" status=\"Cannot book a ticket, no seat available\\n\"\n",
" elif int(availability)-int(no_of_tickets) <= 0:\n",
" status=\"Cannot book a ticket, no seat available\\n\"\n",
" else:\n",
" print(f\"Tool book_function called for {destination_city}\")\n",
" f = open(\"C:/Users/aruna/Desktop/book_status.txt\", \"a\")\n",
" f.write(f\"Ticket to {destination_city} booked for {price}, currently available - {int(availability)-1}\")\n",
" f.write(f\"{no_of_tickets} ticket/s to {destination_city} booked for {price}, currently available - {int(availability)-int(no_of_tickets)}\")\n",
" f.write(\"\\n\")\n",
" f.close()\n",
" ticket_availability[destination_city.lower()]=str(int(availability)-1)\n",
" ticket_availability[destination_city.lower()]=str(int(availability)-int(no_of_tickets))\n",
" \n",
" status=\"Ticket reservation is a success\\n\"\n",
" return status"
@ -172,9 +174,10 @@
"source": [
"book_function = {\n",
" \"name\": \"book_ticket\",\n",
" \"description\": \"Book the ticket based on the ticket price and availability as confirmed by the user. For example, when a \\\n",
" customer confirms to purchase the ticket for Tokyo after getting to know the ticket price and/or the availability, then \\\n",
" proceed with this tool call. Please help the customer in booking the ticket if tickets are available\",\n",
" \"description\": \"Book the ticket based on the ticket price and/ or availability as requested by the user. For example, when a \\\n",
" customer asks to purchase one or more tickets to Tokyo after getting to know the ticket price and/or the availability, then \\\n",
" proceed with this tool call. Please help the customer in booking the ticket/s if tickets are available. But please each time you \\\n",
" book, ask confirmation from the user before proceeding with booking\",\n",
" \"parameters\": {\n",
" \"type\": \"object\",\n",
" \"properties\": {\n",
@ -190,8 +193,12 @@
" \"type\": \"string\",\n",
" \"description\": \"ticket availability to the city the customer wants to travel to\",\n",
" },\n",
" \"no_of_tickets\": {\n",
" \"type\": \"string\",\n",
" \"description\": \"the number of tickets the customer wants to purchase/ book to the destination\",\n",
" }\n",
" },\n",
" \"required\": [\"destination_city\",\"price\",\"availability\"],\n",
" \"required\": [\"destination_city\",\"price\",\"availability\",\"no_of_tickets\"],\n",
" \"additionalProperties\": False\n",
" }\n",
"}"
@ -338,6 +345,7 @@
" arguments = json.loads(tool_call.function.arguments)\n",
" name = json.dumps(tool_call.function.name)\n",
" city = arguments.get('destination_city')\n",
" no = arguments.get('no_of_tickets')\n",
" \n",
" if name.replace('\"','') == \"get_ticket_price\":\n",
" price = get_ticket_price(city)\n",
@ -349,10 +357,10 @@
" elif name.replace('\"','') == \"book_ticket\":\n",
" price = get_ticket_price(city)\n",
" availability = get_ticket_availability(city)\n",
" booked=book_ticket(city,price,availability)\n",
" booked=book_ticket(city,price,availability,no)\n",
" response = {\n",
" \"role\": \"tool\",\n",
" \"content\": json.dumps({\"destination_city\": city,\"booking_status\": booked}),\n",
" \"content\": json.dumps({\"destination_city\": city, \"number of tickets\":no, \"booking_status\": booked}),\n",
" \"tool_call_id\": tool_call.id\n",
" }\n",
" else :\n",
@ -528,43 +536,68 @@
{
"cell_type": "code",
"execution_count": 20,
"id": "23b102a4-e544-4560-acc8-a15620478582",
"id": "36e11d99-9281-4efd-a792-dd4fa5935917",
"metadata": {},
"outputs": [],
"source": [
"import speech_recognition as sr\n",
"from pydub import AudioSegment\n",
"import simpleaudio as sa\n",
"def listen2(history):\n",
" import speech_recognition as sr\n",
"\n",
"def listener():\n",
" recognizer = sr.Recognizer()\n",
" \n",
" r = sr.Recognizer()\n",
" with sr.Microphone() as source:\n",
" print(\"Listening... Speak now!\")\n",
" recognizer.adjust_for_ambient_noise(source) # Adjust for background noise\n",
" audio = recognizer.listen(source)\n",
" \n",
" print(\"Speak now...\")\n",
" audio = r.listen(source, phrase_time_limit=30)\n",
" text=\"\"\n",
" try:\n",
" print(\"Processing speech...\")\n",
" text = recognizer.recognize_google(audio) # Use Google Speech-to-Text\n",
" print(f\"You said: {text}\")\n",
" return text\n",
" text = r.recognize_google(audio)\n",
" print(\"You said:\", text)\n",
" except sr.UnknownValueError:\n",
" print(\"Sorry, I could not understand what you said.\")\n",
" return None\n",
" except sr.RequestError:\n",
" print(\"Could not request results, please check your internet connection.\")\n",
" return None\n",
" print(\"Could not understand audio.\")\n",
"\n",
"# Example usage:\n",
"# text = listener() # Listen for speech\n",
"# if text:\n",
"# print(f\"You just said: {text}\") "
" history += [{\"role\":\"user\", \"content\":text}] \n",
" return \"\", history"
]
},
{
"cell_type": "code",
"execution_count": 21,
"id": "23b102a4-e544-4560-acc8-a15620478582",
"metadata": {},
"outputs": [],
"source": [
"# import speech_recognition as sr\n",
"# from pydub import AudioSegment\n",
"# import simpleaudio as sa\n",
"\n",
"# def listener():\n",
"# recognizer = sr.Recognizer()\n",
" \n",
"# with sr.Microphone() as source:\n",
"# print(\"Listening... Speak now!\")\n",
"# recognizer.adjust_for_ambient_noise(source) # Adjust for background noise\n",
"# audio = recognizer.listen(source, phrase_time_limit=30)\n",
" \n",
"# try:\n",
"# print(\"Processing speech...\")\n",
"# text = recognizer.recognize_google(audio) # Use Google Speech-to-Text\n",
"# print(f\"You said: {text}\")\n",
"# return text\n",
"# except sr.UnknownValueError:\n",
"# print(\"Sorry, I could not understand what you said.\")\n",
"# return None\n",
"# except sr.RequestError:\n",
"# print(\"Could not request results, please check your internet connection.\")\n",
"# return None\n",
"\n",
"# # Example usage:\n",
"# # text = listener() # Listen for speech\n",
"# # if text:\n",
"# # print(f\"You just said: {text}\") "
]
},
{
"cell_type": "code",
"execution_count": 22,
"id": "133904cf-4d72-4552-84a8-76650f334857",
"metadata": {},
"outputs": [
@ -593,7 +626,7 @@
"data": {
"text/plain": []
},
"execution_count": 21,
"execution_count": 22,
"metadata": {},
"output_type": "execute_result"
}
@ -616,9 +649,9 @@
" with gr.Row():\n",
" clear = gr.Button(\"Clear\")\n",
"\n",
" def listen():\n",
" text=listener()\n",
" return text\n",
" def listen(history):\n",
" message, history=listen2(history)\n",
" return message, history\n",
"\n",
" def do_entry(message, history):\n",
" history += [{\"role\":\"user\", \"content\":message}]\n",
@ -628,7 +661,9 @@
" # chat, inputs=chatbot, outputs=[chatbot, image_output]\n",
" chat1, inputs=[chatbot, Model], outputs=[chatbot]\n",
" )\n",
" speak.click(listen, inputs=None, outputs=[entry])\n",
" speak.click(listen, inputs=[chatbot], outputs=[entry, chatbot]).then(\n",
" chat1, inputs=[chatbot, Model], outputs=[chatbot]\n",
" )\n",
" clear.click(lambda: None, inputs=None, outputs=chatbot, queue=False)\n",
"\n",
"ui.launch(inbrowser=True)"
@ -636,7 +671,7 @@
},
{
"cell_type": "code",
"execution_count": 22,
"execution_count": 23,
"id": "dc4a3844-194c-4af7-8ca8-2fc4edb74c11",
"metadata": {},
"outputs": [
@ -645,18 +680,43 @@
"output_type": "stream",
"text": [
"{'london': '20', 'paris': '90', 'tokyo': '100', 'berlin': '2'}\n",
"ChatCompletionMessage(content=None, refusal=None, role='assistant', annotations=[], audio=None, function_call=None, tool_calls=[ChatCompletionMessageToolCall(id='call_ELqH6MFPXMfklfid2QhDQr8Z', function=Function(arguments='{\"destination_city\":\"London\"}', name='get_ticket_price'), type='function')])\n",
"Speak now...\n",
"You said: ticket price to London\n",
"ChatCompletionMessage(content=None, refusal=None, role='assistant', annotations=[], audio=None, function_call=None, tool_calls=[ChatCompletionMessageToolCall(id='call_sLAcmufug2cPxfVZyOuYee4X', function=Function(arguments='{\"destination_city\":\"London\"}', name='get_ticket_price'), type='function')])\n",
"Tool get_ticket_price called for London\n",
"ChatCompletionMessage(content=None, refusal=None, role='assistant', annotations=[], audio=None, function_call=None, tool_calls=[ChatCompletionMessageToolCall(id='call_dDP6CpaDUOkT8yzQbYQMjF5Q', function=Function(arguments='{\"destination_city\":\"Berlin\"}', name='get_ticket_price'), type='function')])\n",
"Tool get_ticket_price called for Berlin\n",
"ChatCompletionMessage(content=None, refusal=None, role='assistant', annotations=[], audio=None, function_call=None, tool_calls=[ChatCompletionMessageToolCall(id='call_F4l14muEmGWk1ZUqdLvH5xUc', function=Function(arguments='{\"destination_city\":\"Berlin\",\"price\":\"$499\",\"availability\":\"Available\"}', name='book_ticket'), type='function')])\n",
"Tool get_ticket_price called for Berlin\n",
"Speak now...\n",
"You said: can you please resolve two tickets for me to London\n",
"Speak now...\n",
"You said: yes please proceed\n",
"ChatCompletionMessage(content=None, refusal=None, role='assistant', annotations=[], audio=None, function_call=None, tool_calls=[ChatCompletionMessageToolCall(id='call_gOnuOwDEsE6lUIQZhZ15mSzR', function=Function(arguments='{\"destination_city\":\"London\",\"price\":\"$799\",\"availability\":\"available\",\"no_of_tickets\":\"2\"}', name='book_ticket'), type='function')])\n",
"Tool get_ticket_price called for London\n",
"Tool get_ticket_availability called for London\n",
"Tool book_function called for London\n",
"Speak now...\n",
"You said: what is the current ticket availability to\n",
"ChatCompletionMessage(content=None, refusal=None, role='assistant', annotations=[], audio=None, function_call=None, tool_calls=[ChatCompletionMessageToolCall(id='call_EYind2wu2Mc1ILAOlzgyO9MT', function=Function(arguments='{\"destination_city\":\"London\"}', name='get_ticket_availability'), type='function')])\n",
"Tool get_ticket_availability called for London\n",
"Speak now...\n",
"You said: yes can you please reserve 19 tickets for me to London\n",
"Speak now...\n",
"You said: no I think this is fine for\n",
"Speak now...\n",
"You said: what is the ticket availability to Ber\n",
"ChatCompletionMessage(content=None, refusal=None, role='assistant', annotations=[], audio=None, function_call=None, tool_calls=[ChatCompletionMessageToolCall(id='call_UvchMEkQjeTZ5ou6uURYmKSz', function=Function(arguments='{\"destination_city\":\"Ber\"}', name='get_ticket_availability'), type='function')])\n",
"Tool get_ticket_availability called for Ber\n",
"Speak now...\n",
"You said: ticket availability to Berlin\n",
"ChatCompletionMessage(content=None, refusal=None, role='assistant', annotations=[], audio=None, function_call=None, tool_calls=[ChatCompletionMessageToolCall(id='call_4qiigA8seXHWgKhYGZNDeXV8', function=Function(arguments='{\"destination_city\":\"Berlin\"}', name='get_ticket_availability'), type='function')])\n",
"Tool get_ticket_availability called for Berlin\n",
"Tool book_function called for Berlin\n",
"ChatCompletionMessage(content=None, refusal=None, role='assistant', annotations=[], audio=None, function_call=None, tool_calls=[ChatCompletionMessageToolCall(id='call_j6hezbCfwk2EiGQArBfxFEwp', function=Function(arguments='{\"destination_city\":\"Berlin\",\"price\":\"$499\",\"availability\":\"Available\"}', name='book_ticket'), type='function')])\n",
"Speak now...\n",
"You said: I would like to reserve two tickets to Berlin\n",
"Speak now...\n",
"You said: yes please please proceed\n",
"ChatCompletionMessage(content=None, refusal=None, role='assistant', annotations=[], audio=None, function_call=None, tool_calls=[ChatCompletionMessageToolCall(id='call_hlJhpoFCJ1g70ASa62SzCFYV', function=Function(arguments='{\"destination_city\":\"Berlin\",\"price\":\"450\",\"availability\":\"2\",\"no_of_tickets\":\"2\"}', name='book_ticket'), type='function')])\n",
"Tool get_ticket_price called for Berlin\n",
"Tool get_ticket_availability called for Berlin\n",
"Tool book_function called for Berlin\n"
"Speak now...\n",
"You said: no that will be fine now thank you\n"
]
}
],
@ -667,15 +727,7 @@
{
"cell_type": "code",
"execution_count": null,
"id": "3d6638a5-ec46-4e98-912b-9408664bb200",
"metadata": {},
"outputs": [],
"source": []
},
{
"cell_type": "code",
"execution_count": null,
"id": "f8fd989c-6da8-4668-8992-62b1eefdba03",
"id": "5166396e-6d8d-4cf2-982b-270d1c87a5ee",
"metadata": {},
"outputs": [],
"source": []
@ -683,7 +735,7 @@
{
"cell_type": "code",
"execution_count": null,
"id": "181f3d17-322c-4f0d-b835-dd1b90ba6784",
"id": "e871fc45-64db-4fb6-add7-569c8b30fe05",
"metadata": {},
"outputs": [],
"source": []

Loading…
Cancel
Save