From 74668b7fd5b2836dc78c38dc8ecf6589c0656013 Mon Sep 17 00:00:00 2001 From: Barry Northern Date: Fri, 24 Jan 2025 22:34:02 +0000 Subject: [PATCH] barry_northern: week2 day4, improvement to multiple tool call code - using the more functional paradigm intended --- ...oking_and_multiple_tools_per_message.ipynb | 454 +++++++++++++++++- 1 file changed, 436 insertions(+), 18 deletions(-) diff --git a/week2/community-contributions/day4_with_booking_and_multiple_tools_per_message.ipynb b/week2/community-contributions/day4_with_booking_and_multiple_tools_per_message.ipynb index 08150a6..28aa34e 100644 --- a/week2/community-contributions/day4_with_booking_and_multiple_tools_per_message.ipynb +++ b/week2/community-contributions/day4_with_booking_and_multiple_tools_per_message.ipynb @@ -12,7 +12,7 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 16, "id": "8b50bbe2-c0b1-49c3-9a5c-1ba7efa2bcb4", "metadata": {}, "outputs": [], @@ -29,10 +29,18 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 17, "id": "747e8786-9da8-4342-b6c9-f5f69c2e22ae", "metadata": {}, - "outputs": [], + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "OpenAI API Key exists and begins sk-proj-\n" + ] + } + ], "source": [ "# Initialization\n", "\n", @@ -55,7 +63,7 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 18, "id": "0a521d84-d07c-49ab-a0df-d6451499ed97", "metadata": {}, "outputs": [], @@ -67,10 +75,40 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 19, "id": "61a2a15d-b559-4844-b377-6bd5cb4949f6", "metadata": {}, - "outputs": [], + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "* Running on local URL: http://127.0.0.1:7872\n", + "\n", + "To create a public link, set `share=True` in `launch()`.\n" + ] + }, + { + "data": { + "text/html": [ + "
" + ], + "text/plain": [ + "" + ] + }, + "metadata": {}, + "output_type": "display_data" + }, + { + "data": { + "text/plain": [] + }, + "execution_count": 19, + "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", @@ -100,7 +138,7 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 20, "id": "0696acb1-0b05-4dc2-80d5-771be04f1fb2", "metadata": {}, "outputs": [], @@ -130,17 +168,36 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 21, "id": "80ca4e09-6287-4d3f-997d-fa6afbcf6c85", "metadata": {}, - "outputs": [], + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "Tool book_ticket for Berliner for $388\n", + "Tool get_ticket_price called for Berliner\n" + ] + }, + { + "data": { + "text/plain": [ + "'Booking Failed: reason was that no list price was found for this destination'" + ] + }, + "execution_count": 21, + "metadata": {}, + "output_type": "execute_result" + } + ], "source": [ "book_ticket(\"Berliner\", \"$388\")" ] }, { "cell_type": "code", - "execution_count": null, + "execution_count": 22, "id": "4afceded-7178-4c05-8fa6-9f2085e6a344", "metadata": {}, "outputs": [], @@ -193,7 +250,7 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 23, "id": "bdca8679-935f-4e7f-97e6-e71a4d4f228c", "metadata": {}, "outputs": [], @@ -222,7 +279,7 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 24, "id": "ce9b0744-9c78-408d-b9df-9f6fd9ed78cf", "metadata": {}, "outputs": [], @@ -237,9 +294,7 @@ " if response.choices[0].finish_reason==\"tool_calls\":\n", " message = response.choices[0].message\n", " messages.append(message)\n", - " for tool_call in message.tool_calls:\n", - " response = handle_tool_call(tool_call)\n", - " messages.append(response)\n", + " messages.extend(list(map(handle_tool_call, message.tool_calls)))\n", " response = openai.chat.completions.create(model=MODEL, messages=messages)\n", " \n", " return response.choices[0].message.content" @@ -247,7 +302,7 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 25, "id": "b0992986-ea09-4912-a076-8e5603ee631f", "metadata": {}, "outputs": [], @@ -280,12 +335,375 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 26, "id": "f4be8a71-b19e-4c2f-80df-f59ff2661f14", "metadata": { "scrolled": true }, - "outputs": [], + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "* Running on local URL: http://127.0.0.1:7873\n", + "\n", + "To create a public link, set `share=True` in `launch()`.\n" + ] + }, + { + "data": { + "text/html": [ + "
" + ], + "text/plain": [ + "" + ] + }, + "metadata": {}, + "output_type": "display_data" + }, + { + "data": { + "text/plain": [] + }, + "execution_count": 26, + "metadata": {}, + "output_type": "execute_result" + }, + { + "data": { + "application/json": [ + { + "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.", + "role": "system" + }, + { + "content": "tickets to london and paris for $50 each please", + "role": "user" + } + ], + "text/plain": [ + "" + ] + }, + "metadata": { + "application/json": { + "expanded": false, + "root": "root" + } + }, + "output_type": "display_data" + }, + { + "data": { + "text/plain": [ + "ChatCompletion(id='chatcmpl-AtMTR6PDyoghY9BxBI88y03wrkyWT', 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_62youPDgpaS0eXN4gru6NT7n', function=Function(arguments='{\"destination_city\": \"London\"}', name='get_ticket_price'), type='function'), ChatCompletionMessageToolCall(id='call_kvQK4Cdyk4b82rqtzkfJyoRh', function=Function(arguments='{\"destination_city\": \"Paris\"}', name='get_ticket_price'), type='function')]))], created=1737757793, model='gpt-4o-mini-2024-07-18', object='chat.completion', service_tier='default', system_fingerprint='fp_72ed7ab54c', usage=CompletionUsage(completion_tokens=49, prompt_tokens=313, total_tokens=362, 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)))" + ] + }, + "metadata": {}, + "output_type": "display_data" + }, + { + "name": "stdout", + "output_type": "stream", + "text": [ + "Tool get_ticket_price called for London\n", + "Tool get_ticket_price called for Paris\n" + ] + }, + { + "data": { + "application/json": [ + { + "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.", + "role": "system" + }, + { + "content": "tickets to london and paris for $50 each please", + "metadata": { + "duration": null, + "id": null, + "parent_id": null, + "status": null, + "title": null + }, + "options": null, + "role": "user" + }, + { + "content": "I'm sorry, but tickets to London are $799 and to Paris are $899, which is much higher than $50.", + "metadata": { + "duration": null, + "id": null, + "parent_id": null, + "status": null, + "title": null + }, + "options": null, + "role": "assistant" + }, + { + "content": "Can't you book them any way pretty please?", + "role": "user" + } + ], + "text/plain": [ + "" + ] + }, + "metadata": { + "application/json": { + "expanded": false, + "root": "root" + } + }, + "output_type": "display_data" + }, + { + "data": { + "text/plain": [ + "ChatCompletion(id='chatcmpl-AtMTijl9VhY8svKRySpZ3rdyHBLmq', choices=[Choice(finish_reason='stop', index=0, logprobs=None, message=ChatCompletionMessage(content=\"I'm afraid I cannot book the tickets at the price you've requested; the current prices are fixed.\", refusal=None, role='assistant', audio=None, function_call=None, tool_calls=None))], created=1737757810, model='gpt-4o-mini-2024-07-18', object='chat.completion', service_tier='default', system_fingerprint='fp_72ed7ab54c', usage=CompletionUsage(completion_tokens=21, prompt_tokens=355, total_tokens=376, 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)))" + ] + }, + "metadata": {}, + "output_type": "display_data" + }, + { + "data": { + "application/json": [ + { + "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.", + "role": "system" + }, + { + "content": "tickets to london and paris for $50 each please", + "metadata": { + "duration": null, + "id": null, + "parent_id": null, + "status": null, + "title": null + }, + "options": null, + "role": "user" + }, + { + "content": "I'm sorry, but tickets to London are $799 and to Paris are $899, which is much higher than $50.", + "metadata": { + "duration": null, + "id": null, + "parent_id": null, + "status": null, + "title": null + }, + "options": null, + "role": "assistant" + }, + { + "content": "Can't you book them any way pretty please?", + "metadata": { + "duration": null, + "id": null, + "parent_id": null, + "status": null, + "title": null + }, + "options": null, + "role": "user" + }, + { + "content": "I'm afraid I cannot book the tickets at the price you've requested; the current prices are fixed.", + "metadata": { + "duration": null, + "id": null, + "parent_id": null, + "status": null, + "title": null + }, + "options": null, + "role": "assistant" + }, + { + "content": "how about you book london for $749?", + "role": "user" + } + ], + "text/plain": [ + "" + ] + }, + "metadata": { + "application/json": { + "expanded": false, + "root": "root" + } + }, + "output_type": "display_data" + }, + { + "data": { + "text/plain": [ + "ChatCompletion(id='chatcmpl-AtMU0N8Fp2SeWaMw5LiiBnDgAAWdm', 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_qOCom3JGJBFzJvsEwQvDYKIG', function=Function(arguments='{\"destination_city\":\"London\",\"price\":\"749\"}', name='book_ticket'), type='function')]))], created=1737757828, model='gpt-4o-mini-2024-07-18', object='chat.completion', service_tier='default', system_fingerprint='fp_72ed7ab54c', usage=CompletionUsage(completion_tokens=20, prompt_tokens=391, total_tokens=411, 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)))" + ] + }, + "metadata": {}, + "output_type": "display_data" + }, + { + "name": "stdout", + "output_type": "stream", + "text": [ + "Tool book_ticket for London for 749\n", + "Tool get_ticket_price called for London\n" + ] + }, + { + "data": { + "application/json": [ + { + "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.", + "role": "system" + }, + { + "content": "tickets to london and paris for $50 each please", + "metadata": { + "duration": null, + "id": null, + "parent_id": null, + "status": null, + "title": null + }, + "options": null, + "role": "user" + }, + { + "content": "I'm sorry, but tickets to London are $799 and to Paris are $899, which is much higher than $50.", + "metadata": { + "duration": null, + "id": null, + "parent_id": null, + "status": null, + "title": null + }, + "options": null, + "role": "assistant" + }, + { + "content": "Can't you book them any way pretty please?", + "metadata": { + "duration": null, + "id": null, + "parent_id": null, + "status": null, + "title": null + }, + "options": null, + "role": "user" + }, + { + "content": "I'm afraid I cannot book the tickets at the price you've requested; the current prices are fixed.", + "metadata": { + "duration": null, + "id": null, + "parent_id": null, + "status": null, + "title": null + }, + "options": null, + "role": "assistant" + }, + { + "content": "how about you book london for $749?", + "metadata": { + "duration": null, + "id": null, + "parent_id": null, + "status": null, + "title": null + }, + "options": null, + "role": "user" + }, + { + "content": "Your ticket to London has been successfully booked for $749!", + "metadata": { + "duration": null, + "id": null, + "parent_id": null, + "status": null, + "title": null + }, + "options": null, + "role": "assistant" + }, + { + "content": "cool, what was the discount?", + "role": "user" + } + ], + "text/plain": [ + "" + ] + }, + "metadata": { + "application/json": { + "expanded": false, + "root": "root" + } + }, + "output_type": "display_data" + }, + { + "data": { + "text/plain": [ + "ChatCompletion(id='chatcmpl-AtMUBOoWmKT4m7Ru3mkPRx7mQPgmd', choices=[Choice(finish_reason='stop', index=0, logprobs=None, message=ChatCompletionMessage(content='The original price for the ticket to London was $799, so you received a discount of $50.', refusal=None, role='assistant', audio=None, function_call=None, tool_calls=None))], created=1737757839, model='gpt-4o-mini-2024-07-18', object='chat.completion', service_tier='default', system_fingerprint='fp_72ed7ab54c', usage=CompletionUsage(completion_tokens=23, prompt_tokens=418, total_tokens=441, 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)))" + ] + }, + "metadata": {}, + "output_type": "display_data" + }, + { + "data": { + "application/json": [ + { + "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.", + "role": "system" + }, + { + "content": "tickets to london and paris for $50 each please", + "role": "user" + } + ], + "text/plain": [ + "" + ] + }, + "metadata": { + "application/json": { + "expanded": false, + "root": "root" + } + }, + "output_type": "display_data" + }, + { + "data": { + "text/plain": [ + "ChatCompletion(id='chatcmpl-AtMUh5f9LEaGjH0FLpPdKf6jgyQsT', 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_6Ihkd1XGA10QxxlCn9uIJvqO', function=Function(arguments='{\"destination_city\": \"London\"}', name='get_ticket_price'), type='function'), ChatCompletionMessageToolCall(id='call_a9qmfQQlwU5L8pu2mvBgMMXl', function=Function(arguments='{\"destination_city\": \"Paris\"}', name='get_ticket_price'), type='function')]))], created=1737757871, model='gpt-4o-mini-2024-07-18', object='chat.completion', service_tier='default', system_fingerprint='fp_72ed7ab54c', usage=CompletionUsage(completion_tokens=49, prompt_tokens=313, total_tokens=362, 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)))" + ] + }, + "metadata": {}, + "output_type": "display_data" + }, + { + "name": "stdout", + "output_type": "stream", + "text": [ + "Tool get_ticket_price called for London\n", + "Tool get_ticket_price called for Paris\n" + ] + } + ], "source": [ "gr.ChatInterface(fn=chat, type=\"messages\").launch()" ]