Skip to content

Commit

Permalink
Add parallel function call/response examples
Browse files Browse the repository at this point in the history
  • Loading branch information
markmcd committed May 6, 2024
1 parent c11176e commit b4fe891
Showing 1 changed file with 142 additions and 11 deletions.
153 changes: 142 additions & 11 deletions quickstarts/Function_calling.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -70,18 +70,9 @@
"metadata": {
"id": "9OEoeosRTv-5"
},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"\u001b[2K \u001b[90m━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━\u001b[0m \u001b[32m137.4/137.4 kB\u001b[0m \u001b[31m933.2 kB/s\u001b[0m eta \u001b[36m0:00:00\u001b[0m\n",
"\u001b[?25h"
]
}
],
"outputs": [],
"source": [
"!pip install -U -q google-generativeai # Install the Python SDK"
"!pip install -U -q google-generativeai # Install the Python SDK"
]
},
{
Expand Down Expand Up @@ -596,6 +587,146 @@
"print(response.text)"
]
},
{
"cell_type": "markdown",
"metadata": {
"id": "EuwKoNIhGBJN"
},
"source": [
"## Parallel function calls\n",
"\n",
"The Gemini API can call multiple functions in a single turn. This caters for scenarios where there are multiple function calls that can take place independently to complete a task.\n",
"\n",
"First set the tools up. Unlike the movie example above, these functions do not require input from each other to be called so they should be good candidates for parallel calling."
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"id": "cJ-mSixWGqLv"
},
"outputs": [],
"source": [
"def power_disco_ball(power: bool) -> bool:\n",
" \"\"\"Powers the spinning disco ball.\"\"\"\n",
" print(f\"Disco ball is {'spinning!' if power else 'stopped.'}\")\n",
" return True\n",
"\n",
"\n",
"def start_music(energetic: bool, loud: bool, bpm: int) -> str:\n",
" \"\"\"Play some music matching the specified parameters.\n",
"\n",
" Args:\n",
" energetic: Whether the music is energetic or not.\n",
" loud: Whether the music is loud or not.\n",
" bpm: The beats per minute of the music.\n",
"\n",
" Returns: The name of the song being played.\n",
" \"\"\"\n",
" print(f\"Starting music! {energetic=} {loud=}, {bpm=}\")\n",
" return \"Never gonna give you up.\"\n",
"\n",
"\n",
"def dim_lights(brightness: float) -> bool:\n",
" \"\"\"Dim the lights.\n",
"\n",
" Args:\n",
" brightness: The brightness of the lights, 0.0 is off, 1.0 is full.\n",
" \"\"\"\n",
" print(f\"Lights are now set to {brightness:.0%}\")\n",
" return True"
]
},
{
"cell_type": "markdown",
"metadata": {
"id": "zlrmXN7fxQi0"
},
"source": [
"Now call the model with an instruction that could use all of the specified tools."
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"id": "21ecYHLgIsCl"
},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"power_disco_ball(power=True)\n",
"start_music(energetic=True, loud=True, bpm=120.0)\n",
"dim_lights(brightness=0.3)\n"
]
}
],
"source": [
"# Set the model up with tools.\n",
"house_fns = [power_disco_ball, start_music, dim_lights]\n",
"# Try this out with Pro and Flash...\n",
"model = genai.GenerativeModel(model_name=\"gemini-1.5-pro\", tools=house_fns)\n",
"\n",
"# Call the API.\n",
"chat = model.start_chat()\n",
"response = chat.send_message(\"Turn this place into a party!\")\n",
"\n",
"# Print out each of the function calls requested from this single call.\n",
"for part in response.parts:\n",
" if fn := part.function_call:\n",
" args = \", \".join(f\"{key}={val}\" for key, val in fn.args.items())\n",
" print(f\"{fn.name}({args})\")"
]
},
{
"cell_type": "markdown",
"metadata": {
"id": "t6iYpty7yZct"
},
"source": [
"Each of the printed results reflects a single function call that the model has requested. To send the results back, include the responses in the same order as they were requested."
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"id": "L7RxoiR3foBR"
},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"Let's get this party started! I've turned on the disco ball, started playing some upbeat music, and dimmed the lights. 🎶✨ Get ready to dance! 🕺💃 \n",
"\n",
"\n"
]
}
],
"source": [
"import google.ai.generativelanguage as glm\n",
"\n",
"# Simulate the responses from the specified tools.\n",
"responses = {\n",
" \"power_disco_ball\": True,\n",
" \"start_music\": \"Never gonna give you up.\",\n",
" \"dim_lights\": True,\n",
"}\n",
"\n",
"# Build the response parts.\n",
"response_parts = [\n",
" glm.Part(function_response=glm.FunctionResponse(name=fn, response={\"result\": val}))\n",
" for fn, val in responses.items()\n",
"]\n",
"\n",
"response = chat.send_message(response_parts)\n",
"print(response.text)"
]
},
{
"cell_type": "markdown",
"metadata": {
Expand Down

0 comments on commit b4fe891

Please sign in to comment.