Fixed the Multiple functions calls error.
This commit is contained in:
@@ -77,6 +77,11 @@
|
|||||||
" {\"Player\": \"Babar Azam\", \"Format\": \"ODI\", \"Year\": 2023, \"Runs\": 1454, \"Matches\": 26, \"Average\": 62.00, \"StrikeRate\": 89.50},\n",
|
" {\"Player\": \"Babar Azam\", \"Format\": \"ODI\", \"Year\": 2023, \"Runs\": 1454, \"Matches\": 26, \"Average\": 62.00, \"StrikeRate\": 89.50},\n",
|
||||||
" {\"Player\": \"Babar Azam\", \"Format\": \"ODI\", \"Year\": 2022, \"Runs\": 1198, \"Matches\": 18, \"Average\": 66.55, \"StrikeRate\": 92.00},\n",
|
" {\"Player\": \"Babar Azam\", \"Format\": \"ODI\", \"Year\": 2022, \"Runs\": 1198, \"Matches\": 18, \"Average\": 66.55, \"StrikeRate\": 92.00},\n",
|
||||||
" {\"Player\": \"Babar Azam\", \"Format\": \"ODI\", \"Year\": 2021, \"Runs\": 949, \"Matches\": 15, \"Average\": 67.78, \"StrikeRate\": 90.50},\n",
|
" {\"Player\": \"Babar Azam\", \"Format\": \"ODI\", \"Year\": 2021, \"Runs\": 949, \"Matches\": 15, \"Average\": 67.78, \"StrikeRate\": 90.50},\n",
|
||||||
|
" {\"Player\": \"Joe Root\", \"Format\": \"Test\", \"Year\": 2025, \"Runs\": 949, \"Matches\": 15, \"Average\": 69.78, \"StrikeRate\": 95.50},\n",
|
||||||
|
" {\"Player\": \"Joe Root\", \"Format\": \"Test\", \"Year\": 2024, \"Runs\": 2025, \"Matches\": 22, \"Average\": 68.78, \"StrikeRate\": 90.50},\n",
|
||||||
|
" {\"Player\": \"Harry Brook\", \"Format\": \"Test\", \"Year\": 2025, \"Runs\": 1056, \"Matches\": 16, \"Average\": 67.78, \"StrikeRate\": 95.50},\n",
|
||||||
|
" {\"Player\": \"Harry Brook\", \"Format\": \"Test\", \"Year\": 2024, \"Runs\": 2200, \"Matches\": 21, \"Average\": 71.78, \"StrikeRate\": 98.50},\n",
|
||||||
|
"\n",
|
||||||
"]"
|
"]"
|
||||||
]
|
]
|
||||||
},
|
},
|
||||||
@@ -98,9 +103,9 @@
|
|||||||
"metadata": {},
|
"metadata": {},
|
||||||
"outputs": [],
|
"outputs": [],
|
||||||
"source": [
|
"source": [
|
||||||
"def analyze_cricket(data_list, player1, player2, match_format=\"ODI\", years=3):\n",
|
"def analyze_cricket(data_list, player1, match_format=\"ODI\", years=3):\n",
|
||||||
" \"\"\"\n",
|
" \"\"\"\n",
|
||||||
" Compare two cricket players' performances using an in-memory list of dicts.\n",
|
" Return cricket players' performances using an in-memory list of dicts.\n",
|
||||||
" \"\"\"\n",
|
" \"\"\"\n",
|
||||||
" print(\"Tool 'analyze_cricket' is called\")\n",
|
" print(\"Tool 'analyze_cricket' is called\")\n",
|
||||||
" df = pd.DataFrame(data_list)\n",
|
" df = pd.DataFrame(data_list)\n",
|
||||||
@@ -110,10 +115,10 @@
|
|||||||
" filtered = df[\n",
|
" filtered = df[\n",
|
||||||
" (df['Format'].str.upper() == match_format.upper()) &\n",
|
" (df['Format'].str.upper() == match_format.upper()) &\n",
|
||||||
" (df['Year'] >= min_year) &\n",
|
" (df['Year'] >= min_year) &\n",
|
||||||
" (df['Player'].isin([player1, player2]))\n",
|
" (df['Player'].isin([player1]))\n",
|
||||||
" ]\n",
|
" ]\n",
|
||||||
" if filtered.empty:\n",
|
" if filtered.empty:\n",
|
||||||
" return {\"error\": f\"No data found for {player1} and {player2} in {match_format} for last {years} years.\"}\n",
|
" return {\"error\": f\"No data found for {player1} in {match_format} for last {years} years.\"}\n",
|
||||||
"\n",
|
"\n",
|
||||||
" summary = filtered.groupby(\"Player\").agg({\n",
|
" summary = filtered.groupby(\"Player\").agg({\n",
|
||||||
" \"Matches\": \"sum\",\n",
|
" \"Matches\": \"sum\",\n",
|
||||||
@@ -133,7 +138,7 @@
|
|||||||
"outputs": [],
|
"outputs": [],
|
||||||
"source": [
|
"source": [
|
||||||
"# Example usage:\n",
|
"# Example usage:\n",
|
||||||
"result = analyze_cricket(cricket_data, \"Virat Kohli\", \"Babar Azam\", \"ODI\", 3)\n",
|
"result = analyze_cricket(cricket_data, \"Virat Kohli\", \"ODI\", 3)\n",
|
||||||
"print(result)"
|
"print(result)"
|
||||||
]
|
]
|
||||||
},
|
},
|
||||||
@@ -152,11 +157,11 @@
|
|||||||
" \"type\": \"object\",\n",
|
" \"type\": \"object\",\n",
|
||||||
" \"properties\": {\n",
|
" \"properties\": {\n",
|
||||||
" \"player1\": {\"type\": \"string\", \"description\": \"Name of first player\"},\n",
|
" \"player1\": {\"type\": \"string\", \"description\": \"Name of first player\"},\n",
|
||||||
" \"player2\": {\"type\": \"string\", \"description\": \"Name of second player\"},\n",
|
" # \"player2\": {\"type\": \"string\", \"description\": \"Name of second player\"},\n",
|
||||||
" \"match_format\": {\"type\": \"string\", \"enum\": [\"ODI\", \"Test\", \"T20\"], \"description\": \"Format of the match\"},\n",
|
" \"match_format\": {\"type\": \"string\", \"enum\": [\"ODI\", \"Test\", \"T20\"], \"description\": \"Format of the match\"},\n",
|
||||||
" \"years\": {\"type\": \"integer\", \"description\": \"Number of years to compare\"}\n",
|
" \"years\": {\"type\": \"integer\", \"description\": \"Number of years to compare\"}\n",
|
||||||
" },\n",
|
" },\n",
|
||||||
" \"required\": [\"player1\", \"player2\"]\n",
|
" \"required\": [\"player1\"]\n",
|
||||||
" }\n",
|
" }\n",
|
||||||
" }"
|
" }"
|
||||||
]
|
]
|
||||||
@@ -200,11 +205,11 @@
|
|||||||
"\n",
|
"\n",
|
||||||
" if response.choices[0].finish_reason==\"tool_calls\":\n",
|
" if response.choices[0].finish_reason==\"tool_calls\":\n",
|
||||||
" message = response.choices[0].message\n",
|
" message = response.choices[0].message\n",
|
||||||
" response, player1, player2 = handle_tool_call(message)\n",
|
|
||||||
" messages.append(message)\n",
|
" messages.append(message)\n",
|
||||||
" messages.append(response)\n",
|
" for tool_call in message.tool_calls: \n",
|
||||||
|
" response, player1= handle_tool_call(tool_call)\n",
|
||||||
|
" messages.append(response)\n",
|
||||||
" response = openai.chat.completions.create(model=MODEL, messages=messages)\n",
|
" response = openai.chat.completions.create(model=MODEL, messages=messages)\n",
|
||||||
" \n",
|
|
||||||
" return response.choices[0].message.content"
|
" return response.choices[0].message.content"
|
||||||
]
|
]
|
||||||
},
|
},
|
||||||
@@ -217,23 +222,23 @@
|
|||||||
"source": [
|
"source": [
|
||||||
"# We have to write that function handle_tool_call:\n",
|
"# We have to write that function handle_tool_call:\n",
|
||||||
"\n",
|
"\n",
|
||||||
"def handle_tool_call(message):\n",
|
"def handle_tool_call(tool_call):\n",
|
||||||
" tool_call = message.tool_calls[0]\n",
|
" # tool_call = message.tool_calls[0]\n",
|
||||||
" print(\"tool_call.id\", tool_call.id)\n",
|
" # print(\"tool_call.id\", tool_call.id)\n",
|
||||||
" arguments = json.loads(tool_call.function.arguments)\n",
|
" arguments = json.loads(tool_call.function.arguments)\n",
|
||||||
" print(\"arguments\", arguments)\n",
|
" print(\"arguments\", arguments)\n",
|
||||||
" player1 = arguments.get('player1')\n",
|
" player1 = arguments.get('player1')\n",
|
||||||
" player2 = arguments.get('player2')\n",
|
" # player2 = arguments.get('player2')\n",
|
||||||
" match_format = arguments.get('match_format', 'ODI')\n",
|
" match_format = arguments.get('match_format', 'ODI')\n",
|
||||||
" years = arguments.get('years', 3)\n",
|
" years = arguments.get('years', 3)\n",
|
||||||
" result = analyze_cricket(cricket_data, player1, player2, match_format, years)\n",
|
" result = analyze_cricket(cricket_data, player1, match_format, years)\n",
|
||||||
" print(\"result from analyze_cricket function: \", result)\n",
|
" print(\"result from analyze_cricket function: \", tool_call.id, result)\n",
|
||||||
" response = {\n",
|
" response = {\n",
|
||||||
" \"role\": \"tool\",\n",
|
" \"role\": \"tool\",\n",
|
||||||
" \"content\": json.dumps(result),\n",
|
" \"content\": json.dumps(result),\n",
|
||||||
" \"tool_call_id\": tool_call.id\n",
|
" \"tool_call_id\": tool_call.id\n",
|
||||||
" }\n",
|
" }\n",
|
||||||
" return response, player1, player2"
|
" return response, player1"
|
||||||
]
|
]
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
@@ -252,13 +257,11 @@
|
|||||||
"\n",
|
"\n",
|
||||||
"\n",
|
"\n",
|
||||||
"2. ### can you please give me the comparison of Virat and babar?\n",
|
"2. ### can you please give me the comparison of Virat and babar?\n",
|
||||||
"Here you are not provided the info of format and number of years. In this case, the function will pick the default values for the match format, which is ODI and years which is 3.\n",
|
"Here, you are not provided the info on the format and number of years. In this case, the function will pick the default values for the match format, which is ODI, and the years, which is 3.\n",
|
||||||
"\n",
|
"\n",
|
||||||
"\n",
|
"\n",
|
||||||
"3. ### Compare Rizwan and Babar in ODI Matches over the last years.\n",
|
"3. ### Compare Rizwan and Babar in ODI Matches over the last years.\n",
|
||||||
"The given data is not available in the above data list. \n",
|
"The given data is not available in the above data list. \n"
|
||||||
"- **Output**: \n",
|
|
||||||
"It seems that I couldn't retrieve specific data for Mohammad Rizwan and Babar Azam in ODI matches over the last three years. However, I can provide a general comparison based on available statistics up to October 2023.\n"
|
|
||||||
]
|
]
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
@@ -268,7 +271,7 @@
|
|||||||
"metadata": {},
|
"metadata": {},
|
||||||
"outputs": [],
|
"outputs": [],
|
||||||
"source": [
|
"source": [
|
||||||
"gr.ChatInterface(fn=chat, type=\"messages\").launch()"
|
"gr.ChatInterface(fn=chat, type=\"messages\").launch(inbrowser=True)"
|
||||||
]
|
]
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
@@ -306,18 +309,21 @@
|
|||||||
"metadata": {},
|
"metadata": {},
|
||||||
"outputs": [],
|
"outputs": [],
|
||||||
"source": [
|
"source": [
|
||||||
"def artist(player1, player2):\n",
|
"def artist(player_names):\n",
|
||||||
" return None\n",
|
" if len(player_names) <2 or len(player_names) > 2:\n",
|
||||||
" # image_response = openai.images.generate(\n",
|
" return None\n",
|
||||||
" # model=\"dall-e-3\",\n",
|
" player1 = player_names[0]\n",
|
||||||
" # prompt=f\"An image representing a comparison of {player1} and {player2}, showing their country flags and bowling or batting style\",\n",
|
" player2 = player_names[1]\n",
|
||||||
" # size=\"1024x1024\",\n",
|
" image_response = openai.images.generate(\n",
|
||||||
" # n=1,\n",
|
" model=\"dall-e-3\",\n",
|
||||||
" # response_format=\"b64_json\",\n",
|
" prompt=f\"An image representing a comparison of {player1} and {player2}, showing their country flags and bowling or batting style\",\n",
|
||||||
" # )\n",
|
" size=\"1024x1024\",\n",
|
||||||
" # image_base64 = image_response.data[0].b64_json\n",
|
" n=1,\n",
|
||||||
" # image_data = base64.b64decode(image_base64)\n",
|
" response_format=\"b64_json\",\n",
|
||||||
" # return Image.open(BytesIO(image_data))"
|
" )\n",
|
||||||
|
" image_base64 = image_response.data[0].b64_json\n",
|
||||||
|
" image_data = base64.b64decode(image_base64)\n",
|
||||||
|
" return Image.open(BytesIO(image_data))"
|
||||||
]
|
]
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
@@ -327,18 +333,10 @@
|
|||||||
"metadata": {},
|
"metadata": {},
|
||||||
"outputs": [],
|
"outputs": [],
|
||||||
"source": [
|
"source": [
|
||||||
"image = artist(\"Babar\", \"Virat\")\n",
|
"image = artist([\"Babar\", \"root\"])\n",
|
||||||
"display(image)"
|
"display(image)"
|
||||||
]
|
]
|
||||||
},
|
},
|
||||||
{
|
|
||||||
"cell_type": "code",
|
|
||||||
"execution_count": null,
|
|
||||||
"id": "728a12c5-adc3-415d-bb05-82beb73b079b",
|
|
||||||
"metadata": {},
|
|
||||||
"outputs": [],
|
|
||||||
"source": []
|
|
||||||
},
|
|
||||||
{
|
{
|
||||||
"cell_type": "code",
|
"cell_type": "code",
|
||||||
"execution_count": null,
|
"execution_count": null,
|
||||||
@@ -368,7 +366,7 @@
|
|||||||
"metadata": {},
|
"metadata": {},
|
||||||
"outputs": [],
|
"outputs": [],
|
||||||
"source": [
|
"source": [
|
||||||
"talker(\"Well, hi there\")"
|
"talker(\"Well, hi there\") # For testing purposes"
|
||||||
]
|
]
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
@@ -384,9 +382,7 @@
|
|||||||
"2. The ability for LLMs to use Tools to give them additional capabilities\n",
|
"2. The ability for LLMs to use Tools to give them additional capabilities\n",
|
||||||
"3. The 'Agent Environment' which allows Agents to collaborate\n",
|
"3. The 'Agent Environment' which allows Agents to collaborate\n",
|
||||||
"4. An LLM can act as the Planner, dividing bigger tasks into smaller ones for the specialists\n",
|
"4. An LLM can act as the Planner, dividing bigger tasks into smaller ones for the specialists\n",
|
||||||
"5. The concept of an Agent having autonomy / agency, beyond just responding to a prompt - such as Memory\n",
|
"5. The concept of an Agent having autonomy / agency, beyond just responding to a prompt - such as Memory\n"
|
||||||
"\n",
|
|
||||||
"We're showing 1 and 2 here, and to a lesser extent 3 and 5. In week 8 we will do the lot!"
|
|
||||||
]
|
]
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
@@ -396,18 +392,23 @@
|
|||||||
"metadata": {},
|
"metadata": {},
|
||||||
"outputs": [],
|
"outputs": [],
|
||||||
"source": [
|
"source": [
|
||||||
"def chat(history):\n",
|
"def chat(history, image_choice):\n",
|
||||||
" messages = [{\"role\": \"system\", \"content\": system_message}] + history\n",
|
" messages = [{\"role\": \"system\", \"content\": system_message}] + history\n",
|
||||||
" response = openai.chat.completions.create(model=MODEL, messages=messages, tools=tools)\n",
|
" response = openai.chat.completions.create(model=MODEL, messages=messages, tools=tools)\n",
|
||||||
" image = None\n",
|
" image = None\n",
|
||||||
" \n",
|
" \n",
|
||||||
" if response.choices[0].finish_reason==\"tool_calls\":\n",
|
" if response.choices[0].finish_reason==\"tool_calls\":\n",
|
||||||
" message = response.choices[0].message\n",
|
" message = response.choices[0].message\n",
|
||||||
" response, player1, player2 = handle_tool_call(message)\n",
|
|
||||||
" messages.append(message)\n",
|
" messages.append(message)\n",
|
||||||
" messages.append(response)\n",
|
" player_names = []\n",
|
||||||
" \n",
|
" for tool_call in message.tool_calls:\n",
|
||||||
" image = artist(player1, player2) #QUERY: how can i pass the value of format_dropdown in this function? \n",
|
" response, player1= handle_tool_call(tool_call)\n",
|
||||||
|
" player_names.append(player1)\n",
|
||||||
|
" messages.append(response)\n",
|
||||||
|
" if image_choice.lower() == 'yes':\n",
|
||||||
|
" image = artist(player_names)\n",
|
||||||
|
" else:\n",
|
||||||
|
" print(\"Image value is NO\", image_choice)\n",
|
||||||
" \n",
|
" \n",
|
||||||
" response = openai.chat.completions.create(model=MODEL, messages=messages)\n",
|
" response = openai.chat.completions.create(model=MODEL, messages=messages)\n",
|
||||||
" \n",
|
" \n",
|
||||||
@@ -431,12 +432,13 @@
|
|||||||
"# Passing in inbrowser=True in the last line will cause a Gradio window to pop up immediately.\n",
|
"# Passing in inbrowser=True in the last line will cause a Gradio window to pop up immediately.\n",
|
||||||
"\n",
|
"\n",
|
||||||
"with gr.Blocks() as ui:\n",
|
"with gr.Blocks() as ui:\n",
|
||||||
|
" gr.Markdown(\"### 🏏 Cricket Analyst AI Assistant\")\n",
|
||||||
" with gr.Row():\n",
|
" with gr.Row():\n",
|
||||||
" chatbot = gr.Chatbot(height=500, type=\"messages\")\n",
|
" chatbot = gr.Chatbot(height=500, type=\"messages\")\n",
|
||||||
" image_output = gr.Image(height=500)\n",
|
" image_output = gr.Image(height=500)\n",
|
||||||
"\n",
|
"\n",
|
||||||
" with gr.Row(): #QUERY: How can I receive the value of \"Yes\" or \"No\" in chat function?\n",
|
" with gr.Row():\n",
|
||||||
" format_dropdown = gr.Dropdown(\n",
|
" image_dropdown = gr.Dropdown(\n",
|
||||||
" choices=[\"Yes\", \"No\"],\n",
|
" choices=[\"Yes\", \"No\"],\n",
|
||||||
" label=\"Do you want image?\"\n",
|
" label=\"Do you want image?\"\n",
|
||||||
" )\n",
|
" )\n",
|
||||||
@@ -445,13 +447,19 @@
|
|||||||
" with gr.Row():\n",
|
" with gr.Row():\n",
|
||||||
" clear = gr.Button(\"Clear\")\n",
|
" clear = gr.Button(\"Clear\")\n",
|
||||||
"\n",
|
"\n",
|
||||||
" def do_entry(message, history):\n",
|
" def do_entry(message, history, image_choice):\n",
|
||||||
" history += [{\"role\": \"user\", \"content\": message}]\n",
|
" history += [{\"role\": \"user\", \"content\": message}]\n",
|
||||||
" return \"\", history\n",
|
" return \"\", history, image_choice\n",
|
||||||
"\n",
|
"\n",
|
||||||
" entry.submit(do_entry, inputs=[entry, chatbot, format_dropdown], outputs=[entry, chatbot]).then(\n",
|
" entry.submit(\n",
|
||||||
" chat, inputs=chatbot, outputs=[chatbot, image_output]\n",
|
" do_entry, \n",
|
||||||
" ) #QUERY: This part is a little bit confusing for me.\n",
|
" inputs=[entry, chatbot, image_dropdown], \n",
|
||||||
|
" outputs=[entry, chatbot, image_dropdown]\n",
|
||||||
|
" ).then(\n",
|
||||||
|
" chat, \n",
|
||||||
|
" inputs=[chatbot, image_dropdown], \n",
|
||||||
|
" outputs=[chatbot, image_output]\n",
|
||||||
|
" )\n",
|
||||||
" clear.click(lambda: None, inputs=None, outputs=chatbot, queue=False)\n",
|
" clear.click(lambda: None, inputs=None, outputs=chatbot, queue=False)\n",
|
||||||
"\n",
|
"\n",
|
||||||
"ui.launch(inbrowser=True)"
|
"ui.launch(inbrowser=True)"
|
||||||
|
|||||||
Reference in New Issue
Block a user