llm-function-calling/llm_function_calling.ipynb (266 lines of code) (raw):
{
"cells": [
{
"cell_type": "markdown",
"id": "_KqUrITrkw5S",
"metadata": {
"id": "_KqUrITrkw5S"
},
"source": [
"## LLM Function Calling with Apigee\n",
"\n",
"<table align=\"left\">\n",
" <td style=\"text-align: center\">\n",
" <a href=\"https://colab.research.google.com/github/GoogleCloudPlatform/apigee-samples/blob/main/llm-function-calling/llm_function_calling.ipynb\">\n",
" <img src=\"https://cloud.google.com/ml-engine/images/colab-logo-32px.png\" alt=\"Google Colaboratory logo\\\"><br> Open in Colab\n",
" </a>\n",
" </td>\n",
" <td style=\"text-align: center\">\n",
" <a href=\"https://console.cloud.google.com/vertex-ai/colab/import/https%3A%2F%2Fraw.githubusercontent.com%2FGoogleCloudPlatform%2Fapigee-samples%2Fmain%2Fllm-function-calling%2Fllm_function_calling.ipynb\">\n",
" <img width=\"32px\" src=\"https://lh3.googleusercontent.com/JmcxdQi-qOpctIvWKgPtrzZdJJK-J3sWE1RsfjZNwshCFgE_9fULcNpuXYTilIR2hjwN\" alt=\"Google Cloud Colab Enterprise logo\"><br> Open in Colab Enterprise\n",
" </a>\n",
" </td> \n",
" <td style=\"text-align: center\">\n",
" <a href=\"https://console.cloud.google.com/vertex-ai/workbench/deploy-notebook?download_url=https://raw.githubusercontent.com/GoogleCloudPlatform/apigee-samples/main/llm-function-calling/llm_function_calling.ipynb\">\n",
" <img src=\"https://lh3.googleusercontent.com/UiNooY4LUgW_oTvpsNhPpQzsstV5W8F7rYgxgGBD85cWJoLmrOzhVs_ksK_vgx40SHs7jCqkTkCk=e14-rj-sc0xffffff-h130-w32\" alt=\"Vertex AI logo\"><br> Open in Workbench\n",
" </a>\n",
" </td>\n",
" <td style=\"text-align: center\">\n",
" <a href=\"https://github.com/GoogleCloudPlatform/apigee-samples/blob/main/llm-function-calling/llm_function_calling.ipynb\">\n",
" <img src=\"https://cloud.google.com/ml-engine/images/github-logo-32px.png\" alt=\"GitHub logo\"><br> View on GitHub\n",
" </a>\n",
" </td>\n",
"</table>\n",
"<br />\n",
"<br />\n",
"<br />\n",
"<br />\n",
"\n",
"This is a sample on how to deploy a sample Apigee proxy and configure it as a function call.\n",
"\n",
"\n",
"\n",
"## Setup\n",
"\n",
"Use the following GCP CloudShell tutorial. Follow the instructions to deploy the sample.\n",
"\n",
"[](https://ssh.cloud.google.com/cloudshell/open?cloudshell_git_repo=https://github.com/GoogleCloudPlatform/apigee-samples&cloudshell_git_branch=main&cloudshell_workspace=.&cloudshell_tutorial=llm-function-calling/docs/cloudshell-tutorial.md)"
]
},
{
"cell_type": "markdown",
"id": "79714ad3",
"metadata": {},
"source": [
"### Install Dependencies"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "E1aMG7gl9McN",
"metadata": {
"id": "E1aMG7gl9McN",
"tags": []
},
"outputs": [],
"source": [
"!pip install -Uq google-genai"
]
},
{
"cell_type": "markdown",
"id": "3fd8953d",
"metadata": {},
"source": [
"### Authenticate your notebook environment (Colab only)\n",
"If you are running this notebook on Google Colab, run the following cell to authenticate your environment. This step is not required if you are using Vertex AI Workbench."
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "c1c82574",
"metadata": {},
"outputs": [],
"source": [
"import sys\n",
"\n",
"# Colab Auth needed to call Client Endpoints (i.e. vertexai)\n",
"if \"google.colab\" in sys.modules:\n",
" # Authenticate user to Google Cloud\n",
" from google.colab import auth as google_auth\n",
" google_auth.authenticate_user()"
]
},
{
"cell_type": "markdown",
"id": "2Do-9w7Pk5G1",
"metadata": {
"id": "2Do-9w7Pk5G1"
},
"source": [
"### Initialize Variables"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "AMz0hBjsfC7u",
"metadata": {
"id": "AMz0hBjsfC7u"
},
"outputs": [],
"source": [
"PROJECT_ID = \"\" # @param {type:\"string\"}\n",
"APIGEE_HOST = \"\" # @param {type:\"string\"}\n",
"APIKEY = \"\" # @param {type:\"string\"}\n",
"MODEL = \"gemini-2.0-flash\""
]
},
{
"cell_type": "markdown",
"id": "7ba06356",
"metadata": {},
"source": [
"### Using Automatic Function Calling to Products API deployed in Apigee\n",
"\n",
"In this section, you'll define a function that takes multiple parameters as inputs. Then you'll use automatic function calling in the Gemini API to make a live API call to fetch your product catalog.\n",
"\n",
"\n"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "0cb90094",
"metadata": {},
"outputs": [],
"source": [
"import requests\n",
"\n",
"def get_products() -> list[dict]:\n",
" \"\"\"\n",
" Get Products\n",
"\n",
" Returns:\n",
" list[dict]: A list of products\n",
" \"\"\"\n",
" base_url = \"https://\"+APIGEE_HOST+\"/v1/samples/llm-function-calling/products\"\n",
"\n",
" try:\n",
" response = requests.get(base_url, headers={\"x-apikey\": APIKEY})\n",
" response.raise_for_status()\n",
" return response.json()\n",
" except requests.RequestException as e:\n",
" print(f\"Error fetching location data: {e}\")\n",
" return []\n",
"\n",
"def get_product(\n",
" id: int | None = None,\n",
") -> list[dict]:\n",
" \"\"\"\n",
" Get Products\n",
"\n",
" Returns:\n",
" list[dict]: A list of products\n",
" \"\"\"\n",
" base_url = \"https://\"+APIGEE_HOST+\"/v1/samples/llm-function-calling/products/\"+str(id)\n",
"\n",
" try:\n",
" response = requests.get(base_url, headers={\"x-apikey\": APIKEY})\n",
" response.raise_for_status()\n",
" return response.json()\n",
" except requests.RequestException as e:\n",
" print(f\"Error fetching location data: {e}\")\n",
" return []"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "69729586",
"metadata": {},
"outputs": [],
"source": [
"print(get_products())\n",
"print(get_product(1))"
]
},
{
"cell_type": "markdown",
"id": "c44b6125",
"metadata": {},
"source": [
"### Test\n",
"\n",
"Let's ask the Gemini model to fetch all the products from the Products API"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "8f1f6a8c",
"metadata": {},
"outputs": [],
"source": [
"from google import genai\n",
"from google.genai.types import GenerateContentConfig, Part, Tool\n",
"\n",
"prompt = \"List all the products in your catalog and their price\"\n",
"client = genai.Client(vertexai=True, project=PROJECT_ID, location=\"us-central1\")\n",
"\n",
"response = client.models.generate_content(\n",
" model=MODEL,\n",
" contents=prompt,\n",
" config=GenerateContentConfig(tools=[get_products], temperature=0),\n",
")\n",
"print(response.text)"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "f8520a90",
"metadata": {},
"outputs": [],
"source": [
"from google import genai\n",
"from google.genai.types import GenerateContentConfig\n",
"\n",
"prompt = \"Fetch all products and find the capacity of the glass jar?\"\n",
"response = client.models.generate_content(\n",
" model=MODEL,\n",
" contents=prompt,\n",
" config=GenerateContentConfig(tools=[get_products, get_product], temperature=0),\n",
")\n",
"print(response.text)"
]
}
],
"metadata": {
"colab": {
"name": "llm-agent",
"provenance": []
},
"kernelspec": {
"display_name": "Python 3",
"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.10.10"
}
},
"nbformat": 4,
"nbformat_minor": 5
}