From 6816169f378e3d6c1c97e10cb1a00d369202be51 Mon Sep 17 00:00:00 2001 From: s-udhaya Date: Fri, 6 Dec 2024 14:23:53 +0000 Subject: [PATCH 1/3] fix minor issues in openai sdk agent notebooks --- .../01_data_pipeline.ipynb | 187 ++++-- .../02_agent_setup.ipynb | 69 ++- .../03_create_synthetic_eval.ipynb | 142 ++++- .../04_create_tools.ipynb | 584 +++++++++++++++--- .../05_tool_calling_agent.ipynb | 355 +++++++++-- .../requirements.txt | 1 + 6 files changed, 1112 insertions(+), 226 deletions(-) diff --git a/openai_sdk_agent_app_sample_code/01_data_pipeline.ipynb b/openai_sdk_agent_app_sample_code/01_data_pipeline.ipynb index 0587393..39723c8 100644 --- a/openai_sdk_agent_app_sample_code/01_data_pipeline.ipynb +++ b/openai_sdk_agent_app_sample_code/01_data_pipeline.ipynb @@ -111,7 +111,7 @@ }, { "cell_type": "code", - "execution_count": 3, + "execution_count": 0, "metadata": { "application/vnd.databricks.v1+cell": { "cellMetadata": { @@ -134,7 +134,16 @@ }, { "cell_type": "markdown", - "metadata": {}, + "metadata": { + "application/vnd.databricks.v1+cell": { + "cellMetadata": {}, + "inputWidgets": {}, + "nuid": "b3bd89b1-83d0-47d4-a3aa-00f32f2494d2", + "showTitle": false, + "tableResultSettingsMap": {}, + "title": "" + } + }, "source": [ "### Connect to Databricks (Local IDE only)\n", "\n", @@ -143,8 +152,17 @@ }, { "cell_type": "code", - "execution_count": 4, - "metadata": {}, + "execution_count": 0, + "metadata": { + "application/vnd.databricks.v1+cell": { + "cellMetadata": {}, + "inputWidgets": {}, + "nuid": "87623ad1-a556-4bc7-af9f-aae3f10771f2", + "showTitle": false, + "tableResultSettingsMap": {}, + "title": "" + } + }, "outputs": [], "source": [ "from cookbook.databricks_utils import get_cluster_url\n", @@ -217,10 +235,13 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 0, "metadata": { "application/vnd.databricks.v1+cell": { - "cellMetadata": {}, + "cellMetadata": { + "byteLimit": 2048000, + "rowLimit": 10000 + }, "inputWidgets": {}, "nuid": "59b3efc5-0591-4a44-b88d-184003cabfb6", "showTitle": false, @@ -289,8 +310,20 @@ }, { "cell_type": "code", - "execution_count": null, - "metadata": {}, + "execution_count": 0, + "metadata": { + "application/vnd.databricks.v1+cell": { + "cellMetadata": { + "byteLimit": 2048000, + "rowLimit": 10000 + }, + "inputWidgets": {}, + "nuid": "bb522de2-004c-44cc-9023-d95e24040bf8", + "showTitle": false, + "tableResultSettingsMap": {}, + "title": "" + } + }, "outputs": [], "source": [ "from cookbook.config.data_pipeline.data_pipeline_output import DataPipelineOuputConfig\n", @@ -362,10 +395,13 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 0, "metadata": { "application/vnd.databricks.v1+cell": { - "cellMetadata": {}, + "cellMetadata": { + "byteLimit": 2048000, + "rowLimit": 10000 + }, "inputWidgets": {}, "nuid": "06ee684b-c7bd-4c0e-8fd8-f54416948a5a", "showTitle": false, @@ -395,7 +431,16 @@ }, { "cell_type": "markdown", - "metadata": {}, + "metadata": { + "application/vnd.databricks.v1+cell": { + "cellMetadata": {}, + "inputWidgets": {}, + "nuid": "0f792e9d-e4dc-4dd2-9078-651117dc640c", + "showTitle": false, + "tableResultSettingsMap": {}, + "title": "" + } + }, "source": [ "#### 🚫✏️ Write the data pipeline configuration to a YAML\n", "\n", @@ -404,8 +449,20 @@ }, { "cell_type": "code", - "execution_count": 8, - "metadata": {}, + "execution_count": 0, + "metadata": { + "application/vnd.databricks.v1+cell": { + "cellMetadata": { + "byteLimit": 2048000, + "rowLimit": 10000 + }, + "inputWidgets": {}, + "nuid": "60e5afe7-cd4a-4153-867e-1f81e1c24229", + "showTitle": false, + "tableResultSettingsMap": {}, + "title": "" + } + }, "outputs": [], "source": [ "from cookbook.config.data_pipeline import DataPipelineConfig\n", @@ -454,6 +511,22 @@ "The code below executes the data pipeline. You can modify the below code as indicated to implement different parsing or chunking strategies or to extract additional metadata fields" ] }, + { + "cell_type": "markdown", + "metadata": { + "application/vnd.databricks.v1+cell": { + "cellMetadata": {}, + "inputWidgets": {}, + "nuid": "f0e36fae-4bb4-48cd-ba83-4574b1d25d43", + "showTitle": false, + "tableResultSettingsMap": {}, + "title": "" + } + }, + "source": [ + "#### 🛑 Make sure to populate the volume with source files before running the parsing code below." + ] + }, { "cell_type": "markdown", "metadata": { @@ -496,10 +569,13 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 0, "metadata": { "application/vnd.databricks.v1+cell": { - "cellMetadata": {}, + "cellMetadata": { + "byteLimit": 2048000, + "rowLimit": 10000 + }, "inputWidgets": {}, "nuid": "d09fd38c-5b7b-47c5-aa6a-ff571ce2f83b", "showTitle": false, @@ -535,10 +611,13 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 0, "metadata": { "application/vnd.databricks.v1+cell": { - "cellMetadata": {}, + "cellMetadata": { + "byteLimit": 2048000, + "rowLimit": 10000 + }, "inputWidgets": {}, "nuid": "48a3ab67-2e30-4e39-b05e-3a8ff304fd5b", "showTitle": false, @@ -588,10 +667,13 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 0, "metadata": { "application/vnd.databricks.v1+cell": { - "cellMetadata": {}, + "cellMetadata": { + "byteLimit": 2048000, + "rowLimit": 10000 + }, "inputWidgets": {}, "nuid": "165706b2-5824-42e7-a22b-3ca0edfd0a77", "showTitle": false, @@ -646,15 +728,36 @@ }, { "cell_type": "markdown", - "metadata": {}, + "metadata": { + "application/vnd.databricks.v1+cell": { + "cellMetadata": {}, + "inputWidgets": {}, + "nuid": "3556c8be-cd81-4af4-9964-fe3b86b1c16a", + "showTitle": false, + "tableResultSettingsMap": {}, + "title": "" + } + }, "source": [ "Show any parsing failures or successfully parsed files that resulted in an empty document." ] }, { "cell_type": "code", - "execution_count": null, - "metadata": {}, + "execution_count": 0, + "metadata": { + "application/vnd.databricks.v1+cell": { + "cellMetadata": { + "byteLimit": 2048000, + "rowLimit": 10000 + }, + "inputWidgets": {}, + "nuid": "cbcf821a-19e0-4904-993b-7c89b7af0e49", + "showTitle": false, + "tableResultSettingsMap": {}, + "title": "" + } + }, "outputs": [], "source": [ "\n", @@ -710,10 +813,13 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 0, "metadata": { "application/vnd.databricks.v1+cell": { - "cellMetadata": {}, + "cellMetadata": { + "byteLimit": 2048000, + "rowLimit": 10000 + }, "inputWidgets": {}, "nuid": "02c40228-f933-4af8-9121-ed2efa0985dd", "showTitle": false, @@ -769,10 +875,13 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 0, "metadata": { "application/vnd.databricks.v1+cell": { - "cellMetadata": {}, + "cellMetadata": { + "byteLimit": 2048000, + "rowLimit": 10000 + }, "inputWidgets": {}, "nuid": "0dfa90f8-c4dc-4485-8fa8-dcd4c7d40618", "showTitle": false, @@ -838,10 +947,13 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 0, "metadata": { "application/vnd.databricks.v1+cell": { - "cellMetadata": {}, + "cellMetadata": { + "byteLimit": 2048000, + "rowLimit": 10000 + }, "inputWidgets": {}, "nuid": "d53faa42-2a65-40b0-8fc1-6c27e88df6d0", "showTitle": false, @@ -891,10 +1003,13 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 0, "metadata": { "application/vnd.databricks.v1+cell": { - "cellMetadata": {}, + "cellMetadata": { + "byteLimit": 2048000, + "rowLimit": 10000 + }, "inputWidgets": {}, "nuid": "0cd40431-4cd3-4cc9-b38d-5ab817c40043", "showTitle": false, @@ -915,6 +1030,7 @@ ], "metadata": { "application/vnd.databricks.v1+notebook": { + "computePreferences": null, "dashboards": [], "environmentMetadata": null, "language": "python", @@ -930,16 +1046,7 @@ "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.11.10" + "name": "python" } }, "nbformat": 4, diff --git a/openai_sdk_agent_app_sample_code/02_agent_setup.ipynb b/openai_sdk_agent_app_sample_code/02_agent_setup.ipynb index 2298851..e3dc85b 100644 --- a/openai_sdk_agent_app_sample_code/02_agent_setup.ipynb +++ b/openai_sdk_agent_app_sample_code/02_agent_setup.ipynb @@ -65,10 +65,13 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 0, "metadata": { "application/vnd.databricks.v1+cell": { - "cellMetadata": {}, + "cellMetadata": { + "byteLimit": 2048000, + "rowLimit": 10000 + }, "inputWidgets": {}, "nuid": "0a145c3b-d3d9-4b95-b7f6-22e1d8e991c6", "showTitle": false, @@ -84,7 +87,16 @@ }, { "cell_type": "markdown", - "metadata": {}, + "metadata": { + "application/vnd.databricks.v1+cell": { + "cellMetadata": {}, + "inputWidgets": {}, + "nuid": "c6635d07-3a87-4b1a-b6b9-47d79bc2a34d", + "showTitle": false, + "tableResultSettingsMap": {}, + "title": "" + } + }, "source": [ "### 🚫✏️ Connect to Databricks\n", "\n", @@ -93,8 +105,17 @@ }, { "cell_type": "code", - "execution_count": 1, - "metadata": {}, + "execution_count": 0, + "metadata": { + "application/vnd.databricks.v1+cell": { + "cellMetadata": {}, + "inputWidgets": {}, + "nuid": "22508721-f046-4506-9e09-4d08733530b1", + "showTitle": false, + "tableResultSettingsMap": {}, + "title": "" + } + }, "outputs": [], "source": [ "from mlflow.utils import databricks_utils as du\n", @@ -125,10 +146,13 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 0, "metadata": { "application/vnd.databricks.v1+cell": { - "cellMetadata": {}, + "cellMetadata": { + "byteLimit": 2048000, + "rowLimit": 10000 + }, "inputWidgets": {}, "nuid": "7824cc0a-1b29-4cf9-a974-2c5ef885979f", "showTitle": false, @@ -167,10 +191,13 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 0, "metadata": { "application/vnd.databricks.v1+cell": { - "cellMetadata": {}, + "cellMetadata": { + "byteLimit": 2048000, + "rowLimit": 10000 + }, "inputWidgets": {}, "nuid": "64682c1f-7e61-430e-84c9-4fb9cad8152b", "showTitle": false, @@ -183,11 +210,10 @@ "from cookbook.config.shared.agent_storage_location import AgentStorageConfig\n", "from cookbook.databricks_utils import get_mlflow_experiment_url\n", "import mlflow\n", + "import os\n", "\n", "# Default values below for `AgentStorageConfig` \n", "agent_name = \"my_agent_2\"\n", - "uc_catalog_name = f\"{default_catalog}\"\n", - "uc_schema_name = f\"{user_name}_agents\"\n", "uc_catalog_name = f\"ep\"\n", "uc_schema_name = f\"cookbook_local_test\"\n", "\n", @@ -229,10 +255,13 @@ }, { "cell_type": "code", - "execution_count": 5, + "execution_count": 0, "metadata": { "application/vnd.databricks.v1+cell": { - "cellMetadata": {}, + "cellMetadata": { + "byteLimit": 2048000, + "rowLimit": 10000 + }, "inputWidgets": {}, "nuid": "6dd99015-5b0d-420b-8a3e-067d84b84dc7", "showTitle": false, @@ -250,13 +279,14 @@ ], "metadata": { "application/vnd.databricks.v1+notebook": { + "computePreferences": null, "dashboards": [], "environmentMetadata": null, "language": "python", "notebookMetadata": { "pythonIndentUnit": 2 }, - "notebookName": "00_shared_config", + "notebookName": "02_agent_setup", "widgets": {} }, "kernelspec": { @@ -265,16 +295,7 @@ "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.11.10" + "name": "python" } }, "nbformat": 4, diff --git a/openai_sdk_agent_app_sample_code/03_create_synthetic_eval.ipynb b/openai_sdk_agent_app_sample_code/03_create_synthetic_eval.ipynb index afbad77..5e4fe81 100644 --- a/openai_sdk_agent_app_sample_code/03_create_synthetic_eval.ipynb +++ b/openai_sdk_agent_app_sample_code/03_create_synthetic_eval.ipynb @@ -2,7 +2,16 @@ "cells": [ { "cell_type": "markdown", - "metadata": {}, + "metadata": { + "application/vnd.databricks.v1+cell": { + "cellMetadata": {}, + "inputWidgets": {}, + "nuid": "6d2afd90-a542-4dee-b053-6c71eb56eb02", + "showTitle": false, + "tableResultSettingsMap": {}, + "title": "" + } + }, "source": [ "## 👉 START HERE: How to use this notebook\n", "\n", @@ -18,7 +27,16 @@ }, { "cell_type": "markdown", - "metadata": {}, + "metadata": { + "application/vnd.databricks.v1+cell": { + "cellMetadata": {}, + "inputWidgets": {}, + "nuid": "9d47d187-9cc4-43ae-9899-030594037970", + "showTitle": false, + "tableResultSettingsMap": {}, + "title": "" + } + }, "source": [ "**Important note:** Throughout this notebook, we indicate which cells you:\n", "- ✅✏️ *should* customize - these cells contain config settings to change\n", @@ -29,7 +47,16 @@ }, { "cell_type": "markdown", - "metadata": {}, + "metadata": { + "application/vnd.databricks.v1+cell": { + "cellMetadata": {}, + "inputWidgets": {}, + "nuid": "dd99d8d3-9b35-42d9-8821-d42ef743bb1b", + "showTitle": false, + "tableResultSettingsMap": {}, + "title": "" + } + }, "source": [ "### 🚫✏️ Install Python libraries" ] @@ -58,7 +85,16 @@ }, { "cell_type": "markdown", - "metadata": {}, + "metadata": { + "application/vnd.databricks.v1+cell": { + "cellMetadata": {}, + "inputWidgets": {}, + "nuid": "20b50bf5-5663-42f7-aa7a-d19fe25686a9", + "showTitle": false, + "tableResultSettingsMap": {}, + "title": "" + } + }, "source": [ "### 🚫✏️ Connect to Databricks\n", "\n", @@ -67,8 +103,17 @@ }, { "cell_type": "code", - "execution_count": 2, - "metadata": {}, + "execution_count": 0, + "metadata": { + "application/vnd.databricks.v1+cell": { + "cellMetadata": {}, + "inputWidgets": {}, + "nuid": "8fef54bc-06b1-4490-91ff-c45b3add4c4a", + "showTitle": false, + "tableResultSettingsMap": {}, + "title": "" + } + }, "outputs": [], "source": [ "from mlflow.utils import databricks_utils as du\n", @@ -83,7 +128,16 @@ }, { "cell_type": "markdown", - "metadata": {}, + "metadata": { + "application/vnd.databricks.v1+cell": { + "cellMetadata": {}, + "inputWidgets": {}, + "nuid": "43acd1f1-f3f3-441d-ad49-7e69d72fe3e2", + "showTitle": false, + "tableResultSettingsMap": {}, + "title": "" + } + }, "source": [ "### 🚫✏️ Load the Agent's storage locations\n", "\n", @@ -92,8 +146,20 @@ }, { "cell_type": "code", - "execution_count": null, - "metadata": {}, + "execution_count": 0, + "metadata": { + "application/vnd.databricks.v1+cell": { + "cellMetadata": { + "byteLimit": 2048000, + "rowLimit": 10000 + }, + "inputWidgets": {}, + "nuid": "313d7097-ce98-4ac7-bacb-86afd347faf4", + "showTitle": false, + "tableResultSettingsMap": {}, + "title": "" + } + }, "outputs": [], "source": [ "from cookbook.config.shared.agent_storage_location import AgentStorageConfig\n", @@ -116,7 +182,16 @@ }, { "cell_type": "markdown", - "metadata": {}, + "metadata": { + "application/vnd.databricks.v1+cell": { + "cellMetadata": {}, + "inputWidgets": {}, + "nuid": "b262ee6c-60b1-41d0-93d6-fa71b0383101", + "showTitle": false, + "tableResultSettingsMap": {}, + "title": "" + } + }, "source": [ "#### ✅✏️ Load the source documents for synthetic evaluation data generation\n", "\n", @@ -131,8 +206,20 @@ }, { "cell_type": "code", - "execution_count": null, - "metadata": {}, + "execution_count": 0, + "metadata": { + "application/vnd.databricks.v1+cell": { + "cellMetadata": { + "byteLimit": 2048000, + "rowLimit": 10000 + }, + "inputWidgets": {}, + "nuid": "d5349414-4b08-4eac-81cb-59c7db296aff", + "showTitle": false, + "tableResultSettingsMap": {}, + "title": "" + } + }, "outputs": [], "source": [ "from cookbook.config.data_pipeline import DataPipelineConfig\n", @@ -147,7 +234,16 @@ }, { "cell_type": "markdown", - "metadata": {}, + "metadata": { + "application/vnd.databricks.v1+cell": { + "cellMetadata": {}, + "inputWidgets": {}, + "nuid": "4e782101-d93d-4f61-8b4d-852706602728", + "showTitle": false, + "tableResultSettingsMap": {}, + "title": "" + } + }, "source": [ "#### ✅✏️ Run the synthetic evaluation data generation\n", "\n", @@ -156,7 +252,7 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 0, "metadata": { "application/vnd.databricks.v1+cell": { "cellMetadata": { @@ -196,7 +292,7 @@ "synthesized_evals_df = generate_evals_df(\n", " docs=source_documents,\n", " # The number of evaluations to generate for each doc.\n", - " num_questions_per_doc=2,\n", + " num_evals=10,\n", " # A optional set of guidelines that help guide the synthetic generation. This is a free-form string that will be used to prompt the generation.\n", " # guidelines=guidelines\n", ")\n", @@ -205,13 +301,14 @@ "spark.createDataFrame(synthesized_evals_df).write.format(\"delta\").mode(\"append\").saveAsTable(agent_storage_config.evaluation_set_uc_table)\n", "\n", "# Display the synthetic evaluation data\n", - "eval_set_df = spark.table(agent_storage_config.evaluation_set_uc_table).show()\n", + "eval_set_df = spark.table(agent_storage_config.evaluation_set_uc_table)\n", "display(eval_set_df.toPandas())" ] } ], "metadata": { "application/vnd.databricks.v1+notebook": { + "computePreferences": null, "dashboards": [], "environmentMetadata": { "base_environment": "", @@ -221,7 +318,7 @@ "notebookMetadata": { "pythonIndentUnit": 4 }, - "notebookName": "02_create_synthetic_eval", + "notebookName": "03_create_synthetic_eval", "widgets": {} }, "kernelspec": { @@ -230,16 +327,7 @@ "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.11.10" + "name": "python" } }, "nbformat": 4, diff --git a/openai_sdk_agent_app_sample_code/04_create_tools.ipynb b/openai_sdk_agent_app_sample_code/04_create_tools.ipynb index 0054d05..72494ad 100644 --- a/openai_sdk_agent_app_sample_code/04_create_tools.ipynb +++ b/openai_sdk_agent_app_sample_code/04_create_tools.ipynb @@ -61,10 +61,13 @@ }, { "cell_type": "code", - "execution_count": 1, + "execution_count": 0, "metadata": { "application/vnd.databricks.v1+cell": { - "cellMetadata": {}, + "cellMetadata": { + "byteLimit": 2048000, + "rowLimit": 10000 + }, "inputWidgets": {}, "nuid": "6d4030e8-ae97-4351-bebd-9651d283578f", "showTitle": false, @@ -81,7 +84,16 @@ }, { "cell_type": "markdown", - "metadata": {}, + "metadata": { + "application/vnd.databricks.v1+cell": { + "cellMetadata": {}, + "inputWidgets": {}, + "nuid": "33bca1ff-c8be-4f90-8473-90865c60c2c0", + "showTitle": false, + "tableResultSettingsMap": {}, + "title": "" + } + }, "source": [ "### 🚫✏️ Connect to Databricks\n", "\n", @@ -90,8 +102,17 @@ }, { "cell_type": "code", - "execution_count": 28, - "metadata": {}, + "execution_count": 0, + "metadata": { + "application/vnd.databricks.v1+cell": { + "cellMetadata": {}, + "inputWidgets": {}, + "nuid": "69be6313-0b0f-42f4-a54e-e50122f22f5d", + "showTitle": false, + "tableResultSettingsMap": {}, + "title": "" + } + }, "outputs": [], "source": [ "from mlflow.utils import databricks_utils as du\n", @@ -106,7 +127,16 @@ }, { "cell_type": "markdown", - "metadata": {}, + "metadata": { + "application/vnd.databricks.v1+cell": { + "cellMetadata": {}, + "inputWidgets": {}, + "nuid": "6d738f51-0005-44d7-b47c-47228f81f828", + "showTitle": false, + "tableResultSettingsMap": {}, + "title": "" + } + }, "source": [ "### 🚫✏️ Load the Agent's UC storage locations; set up MLflow experiment\n", "\n", @@ -115,14 +145,27 @@ }, { "cell_type": "code", - "execution_count": null, - "metadata": {}, + "execution_count": 0, + "metadata": { + "application/vnd.databricks.v1+cell": { + "cellMetadata": { + "byteLimit": 2048000, + "rowLimit": 10000 + }, + "inputWidgets": {}, + "nuid": "8dff09e4-085d-4375-a0c6-0260a474b4f5", + "showTitle": false, + "tableResultSettingsMap": {}, + "title": "" + } + }, "outputs": [], "source": [ "from cookbook.config.shared.agent_storage_location import AgentStorageConfig\n", "from cookbook.databricks_utils import get_mlflow_experiment_url\n", "from cookbook.config import load_serializable_config_from_yaml_file\n", - "import mlflow \n", + "import mlflow\n", + "import os\n", "\n", "# Load the Agent's storage locations\n", "agent_storage_config: AgentStorageConfig= load_serializable_config_from_yaml_file(\"./configs/agent_storage_config.yaml\")\n", @@ -140,7 +183,16 @@ }, { "cell_type": "markdown", - "metadata": {}, + "metadata": { + "application/vnd.databricks.v1+cell": { + "cellMetadata": {}, + "inputWidgets": {}, + "nuid": "1abeffaa-a1a7-4457-8a51-e2bbf42e9ffe", + "showTitle": false, + "tableResultSettingsMap": {}, + "title": "" + } + }, "source": [ "# create tools\n", "\n", @@ -152,15 +204,36 @@ }, { "cell_type": "markdown", - "metadata": {}, + "metadata": { + "application/vnd.databricks.v1+cell": { + "cellMetadata": {}, + "inputWidgets": {}, + "nuid": "b0487dfd-0c27-418f-afc5-34aa42e237d9", + "showTitle": false, + "tableResultSettingsMap": {}, + "title": "" + } + }, "source": [ "always reload the tool's code" ] }, { "cell_type": "code", - "execution_count": 6, - "metadata": {}, + "execution_count": 0, + "metadata": { + "application/vnd.databricks.v1+cell": { + "cellMetadata": { + "byteLimit": 2048000, + "rowLimit": 10000 + }, + "inputWidgets": {}, + "nuid": "974c9105-fde1-4d46-9258-bd50f54d852e", + "showTitle": false, + "tableResultSettingsMap": {}, + "title": "" + } + }, "outputs": [], "source": [ "%load_ext autoreload\n", @@ -169,22 +242,52 @@ }, { "cell_type": "markdown", - "metadata": {}, + "metadata": { + "application/vnd.databricks.v1+cell": { + "cellMetadata": {}, + "inputWidgets": {}, + "nuid": "6c15a837-69b8-4e83-8e80-ec47b9b817a6", + "showTitle": false, + "tableResultSettingsMap": {}, + "title": "" + } + }, "source": [ "## lets do an example of a simple, but fake tool that translates old to new SKUs." ] }, { "cell_type": "markdown", - "metadata": {}, + "metadata": { + "application/vnd.databricks.v1+cell": { + "cellMetadata": {}, + "inputWidgets": {}, + "nuid": "c2b2384f-a495-49f7-8a97-083a05a9e9ec", + "showTitle": false, + "tableResultSettingsMap": {}, + "title": "" + } + }, "source": [ "1, create the python function that will become your UC function. you need to annotate the function with docstrings & type hints - these are used to create the tool's metadata in UC." ] }, { "cell_type": "code", - "execution_count": null, - "metadata": {}, + "execution_count": 0, + "metadata": { + "application/vnd.databricks.v1+cell": { + "cellMetadata": { + "byteLimit": 2048000, + "rowLimit": 10000 + }, + "inputWidgets": {}, + "nuid": "dd5fccbd-272e-437d-9de2-0fb4f4bf4b5e", + "showTitle": false, + "tableResultSettingsMap": {}, + "title": "" + } + }, "outputs": [], "source": [ "%%writefile tools/sample_tool.py\n", @@ -238,15 +341,36 @@ }, { "cell_type": "markdown", - "metadata": {}, + "metadata": { + "application/vnd.databricks.v1+cell": { + "cellMetadata": {}, + "inputWidgets": {}, + "nuid": "967c550a-e829-4adc-8bd0-f7ada479f8d0", + "showTitle": false, + "tableResultSettingsMap": {}, + "title": "" + } + }, "source": [ "Now, let's import the tool and test it locally" ] }, { "cell_type": "code", - "execution_count": null, - "metadata": {}, + "execution_count": 0, + "metadata": { + "application/vnd.databricks.v1+cell": { + "cellMetadata": { + "byteLimit": 2048000, + "rowLimit": 10000 + }, + "inputWidgets": {}, + "nuid": "50316824-e110-469d-a432-2b33e856b1a7", + "showTitle": false, + "tableResultSettingsMap": {}, + "title": "" + } + }, "outputs": [], "source": [ "from tools.sample_tool import sku_sample_translator\n", @@ -256,15 +380,36 @@ }, { "cell_type": "markdown", - "metadata": {}, + "metadata": { + "application/vnd.databricks.v1+cell": { + "cellMetadata": {}, + "inputWidgets": {}, + "nuid": "e04cac07-3058-478a-8bff-424f601551cf", + "showTitle": false, + "tableResultSettingsMap": {}, + "title": "" + } + }, "source": [ "now, lets write some pyTest unit tests for the tool - these are just samples, you will need to write your own" ] }, { "cell_type": "code", - "execution_count": null, - "metadata": {}, + "execution_count": 0, + "metadata": { + "application/vnd.databricks.v1+cell": { + "cellMetadata": { + "byteLimit": 2048000, + "rowLimit": 10000 + }, + "inputWidgets": {}, + "nuid": "9552bd3c-9b21-4987-9163-54969150a5da", + "showTitle": false, + "tableResultSettingsMap": {}, + "title": "" + } + }, "outputs": [], "source": [ "%%writefile tools/test_sample_tool.py\n", @@ -324,34 +469,81 @@ }, { "cell_type": "markdown", - "metadata": {}, + "metadata": { + "application/vnd.databricks.v1+cell": { + "cellMetadata": {}, + "inputWidgets": {}, + "nuid": "b521aec6-e570-4ab3-a580-2d1b9f54391c", + "showTitle": false, + "tableResultSettingsMap": {}, + "title": "" + } + }, "source": [ "now, lets run the tests" ] }, { "cell_type": "code", - "execution_count": null, - "metadata": {}, + "execution_count": 0, + "metadata": { + "application/vnd.databricks.v1+cell": { + "cellMetadata": { + "byteLimit": 2048000, + "rowLimit": 10000 + }, + "inputWidgets": {}, + "nuid": "5a448046-c658-4a6a-92e2-55b32d0b0ab8", + "showTitle": false, + "tableResultSettingsMap": {}, + "title": "" + } + }, "outputs": [], "source": [ "import pytest\n", + "from mlflow.utils import databricks_utils as du\n", "\n", + "if du.is_in_databricks_notebook():\n", + " import sys\n", + " sys.dont_write_bytecode = True # Skip writing .pyc files to the bytecode cache on the cluster.\n", + " \n", "# Run tests from test_sku_translator.py\n", "pytest.main([\"-v\", \"tools/test_sample_tool.py\"])\n" ] }, { "cell_type": "markdown", - "metadata": {}, + "metadata": { + "application/vnd.databricks.v1+cell": { + "cellMetadata": {}, + "inputWidgets": {}, + "nuid": "7ad0be87-b719-481c-892f-a55141c8d837", + "showTitle": false, + "tableResultSettingsMap": {}, + "title": "" + } + }, "source": [ "Now, lets deploy the tool to Unity catalog." ] }, { "cell_type": "code", - "execution_count": null, - "metadata": {}, + "execution_count": 0, + "metadata": { + "application/vnd.databricks.v1+cell": { + "cellMetadata": { + "byteLimit": 2048000, + "rowLimit": 10000 + }, + "inputWidgets": {}, + "nuid": "61ff2c1e-cd48-49db-b9eb-903ac13428c3", + "showTitle": false, + "tableResultSettingsMap": {}, + "title": "" + } + }, "outputs": [], "source": [ "from unitycatalog.ai.core.databricks import DatabricksFunctionClient\n", @@ -371,15 +563,36 @@ }, { "cell_type": "markdown", - "metadata": {}, + "metadata": { + "application/vnd.databricks.v1+cell": { + "cellMetadata": {}, + "inputWidgets": {}, + "nuid": "333a4267-0277-4665-a695-12dd950fd031", + "showTitle": false, + "tableResultSettingsMap": {}, + "title": "" + } + }, "source": [ "Now, wrap it into a UCTool that will be used by our Agent. UC tool is just a Pydnatic base model that is serializable to YAML that will load the tool's metadata from UC and wrap it in a callable object." ] }, { "cell_type": "code", - "execution_count": 9, - "metadata": {}, + "execution_count": 0, + "metadata": { + "application/vnd.databricks.v1+cell": { + "cellMetadata": { + "byteLimit": 2048000, + "rowLimit": 10000 + }, + "inputWidgets": {}, + "nuid": "770054d3-8776-4c1e-a1c3-6877dd8e0fcb", + "showTitle": false, + "tableResultSettingsMap": {}, + "title": "" + } + }, "outputs": [], "source": [ "from cookbook.tools.uc_tool import UCTool\n", @@ -390,7 +603,16 @@ }, { "cell_type": "markdown", - "metadata": {}, + "metadata": { + "application/vnd.databricks.v1+cell": { + "cellMetadata": {}, + "inputWidgets": {}, + "nuid": "d3bd8bf4-ed7b-492e-99fa-d8a900553fae", + "showTitle": false, + "tableResultSettingsMap": {}, + "title": "" + } + }, "source": [ "Now, let's test the UC tool - the UCTool is a directly callable wrapper around the UC function, so it can be used just like a local function, but the output will be put into a dictionary with either the output in a 'value' key or an 'error' key if an error is raised.\n", "\n", @@ -399,8 +621,20 @@ }, { "cell_type": "code", - "execution_count": null, - "metadata": {}, + "execution_count": 0, + "metadata": { + "application/vnd.databricks.v1+cell": { + "cellMetadata": { + "byteLimit": 2048000, + "rowLimit": 10000 + }, + "inputWidgets": {}, + "nuid": "f67b42a4-7859-4c84-95d4-0db66679a61e", + "showTitle": false, + "tableResultSettingsMap": {}, + "title": "" + } + }, "outputs": [], "source": [ "# successful call\n", @@ -409,8 +643,20 @@ }, { "cell_type": "code", - "execution_count": null, - "metadata": {}, + "execution_count": 0, + "metadata": { + "application/vnd.databricks.v1+cell": { + "cellMetadata": { + "byteLimit": 2048000, + "rowLimit": 10000 + }, + "inputWidgets": {}, + "nuid": "7fda074a-b16e-4b94-8eb7-69d7cf76a0ae", + "showTitle": false, + "tableResultSettingsMap": {}, + "title": "" + } + }, "outputs": [], "source": [ "# unsuccessful call\n", @@ -419,15 +665,36 @@ }, { "cell_type": "markdown", - "metadata": {}, + "metadata": { + "application/vnd.databricks.v1+cell": { + "cellMetadata": {}, + "inputWidgets": {}, + "nuid": "ff4ff41f-ba97-451b-bfe3-39e83bb9e306", + "showTitle": false, + "tableResultSettingsMap": {}, + "title": "" + } + }, "source": [ "now, let's convert our pytests to work with the UC tool. this requires a bit of transformation to the test code to account for the fact that the output is in a dictionary & exceptions are not raised directly." ] }, { "cell_type": "code", - "execution_count": null, - "metadata": {}, + "execution_count": 0, + "metadata": { + "application/vnd.databricks.v1+cell": { + "cellMetadata": { + "byteLimit": 2048000, + "rowLimit": 10000 + }, + "inputWidgets": {}, + "nuid": "7e0afe99-6688-4770-a6a7-43e35f1a6cf1", + "showTitle": false, + "tableResultSettingsMap": {}, + "title": "" + } + }, "outputs": [], "source": [ "%%writefile tools/test_sample_tool_uc.py\n", @@ -507,27 +774,65 @@ }, { "cell_type": "code", - "execution_count": null, - "metadata": {}, + "execution_count": 0, + "metadata": { + "application/vnd.databricks.v1+cell": { + "cellMetadata": { + "byteLimit": 2048000, + "rowLimit": 10000 + }, + "inputWidgets": {}, + "nuid": "5c551f80-4fd0-45fb-b4be-e37383cb9c08", + "showTitle": false, + "tableResultSettingsMap": {}, + "title": "" + } + }, "outputs": [], "source": [ "import pytest\n", + "from mlflow.utils import databricks_utils as du\n", "\n", - "# Run tests from test_sku_translator.py\n", + "if du.is_in_databricks_notebook():\n", + " import sys\n", + " sys.dont_write_bytecode = True # Skip writing .pyc files to the bytecode cache on the cluster.\n", + "\n", + "# Run tests from test_sample_tool_uc.py\n", "pytest.main([\"-v\", \"tools/test_sample_tool_uc.py\"])\n" ] }, { "cell_type": "markdown", - "metadata": {}, + "metadata": { + "application/vnd.databricks.v1+cell": { + "cellMetadata": {}, + "inputWidgets": {}, + "nuid": "83118bbc-8687-4ed7-a437-3627754e3ab7", + "showTitle": false, + "tableResultSettingsMap": {}, + "title": "" + } + }, "source": [ "# Now, here's another example of a tool that executes python code." ] }, { "cell_type": "code", - "execution_count": null, - "metadata": {}, + "execution_count": 0, + "metadata": { + "application/vnd.databricks.v1+cell": { + "cellMetadata": { + "byteLimit": 2048000, + "rowLimit": 10000 + }, + "inputWidgets": {}, + "nuid": "7a5be80d-c77f-4c45-8e8a-bb3594849de7", + "showTitle": false, + "tableResultSettingsMap": {}, + "title": "" + } + }, "outputs": [], "source": [ "%%writefile tools/code_exec.py\n", @@ -555,8 +860,20 @@ }, { "cell_type": "code", - "execution_count": null, - "metadata": {}, + "execution_count": 0, + "metadata": { + "application/vnd.databricks.v1+cell": { + "cellMetadata": { + "byteLimit": 2048000, + "rowLimit": 10000 + }, + "inputWidgets": {}, + "nuid": "98fd1eeb-b3ed-4bbd-8432-95025ad02838", + "showTitle": false, + "tableResultSettingsMap": {}, + "title": "" + } + }, "outputs": [], "source": [ "from tools.code_exec import python_exec\n", @@ -566,15 +883,36 @@ }, { "cell_type": "markdown", - "metadata": {}, + "metadata": { + "application/vnd.databricks.v1+cell": { + "cellMetadata": {}, + "inputWidgets": {}, + "nuid": "f11047e3-d784-4801-a4c0-9e9a065a7b57", + "showTitle": false, + "tableResultSettingsMap": {}, + "title": "" + } + }, "source": [ "Test it locally" ] }, { "cell_type": "code", - "execution_count": null, - "metadata": {}, + "execution_count": 0, + "metadata": { + "application/vnd.databricks.v1+cell": { + "cellMetadata": { + "byteLimit": 2048000, + "rowLimit": 10000 + }, + "inputWidgets": {}, + "nuid": "3f9d61d4-3563-4066-9e48-3e176a2d6a9d", + "showTitle": false, + "tableResultSettingsMap": {}, + "title": "" + } + }, "outputs": [], "source": [ "%%writefile tools/test_code_exec.py\n", @@ -671,28 +1009,66 @@ }, { "cell_type": "code", - "execution_count": null, - "metadata": {}, + "execution_count": 0, + "metadata": { + "application/vnd.databricks.v1+cell": { + "cellMetadata": { + "byteLimit": 2048000, + "rowLimit": 10000 + }, + "inputWidgets": {}, + "nuid": "c4e2823c-3f8a-4249-9575-f27fb7c0a01e", + "showTitle": false, + "tableResultSettingsMap": {}, + "title": "" + } + }, "outputs": [], "source": [ "import pytest\n", + "from mlflow.utils import databricks_utils as du\n", "\n", - "# Run tests from test_sku_translator.py\n", + "if du.is_in_databricks_notebook():\n", + " import sys\n", + " sys.dont_write_bytecode = True # Skip writing .pyc files to the bytecode cache on the cluster.\n", + "\n", + "# Run tests from test_code_exec.py\n", "pytest.main([\"-v\", \"tools/test_code_exec.py\"])\n", "\n" ] }, { "cell_type": "markdown", - "metadata": {}, + "metadata": { + "application/vnd.databricks.v1+cell": { + "cellMetadata": {}, + "inputWidgets": {}, + "nuid": "e66df2d1-4a81-4706-8440-35594f8a8854", + "showTitle": false, + "tableResultSettingsMap": {}, + "title": "" + } + }, "source": [ "Deploy to UC" ] }, { "cell_type": "code", - "execution_count": null, - "metadata": {}, + "execution_count": 0, + "metadata": { + "application/vnd.databricks.v1+cell": { + "cellMetadata": { + "byteLimit": 2048000, + "rowLimit": 10000 + }, + "inputWidgets": {}, + "nuid": "18ce1f96-f0ad-4bb2-8f23-1a732c05eba2", + "showTitle": false, + "tableResultSettingsMap": {}, + "title": "" + } + }, "outputs": [], "source": [ "from unitycatalog.ai.core.databricks import DatabricksFunctionClient\n", @@ -714,15 +1090,36 @@ }, { "cell_type": "markdown", - "metadata": {}, + "metadata": { + "application/vnd.databricks.v1+cell": { + "cellMetadata": {}, + "inputWidgets": {}, + "nuid": "6b82baf1-441f-4ff3-aa3c-8242b31e4a50", + "showTitle": false, + "tableResultSettingsMap": {}, + "title": "" + } + }, "source": [ "Test as UC Tool for the Agent" ] }, { "cell_type": "code", - "execution_count": null, - "metadata": {}, + "execution_count": 0, + "metadata": { + "application/vnd.databricks.v1+cell": { + "cellMetadata": { + "byteLimit": 2048000, + "rowLimit": 10000 + }, + "inputWidgets": {}, + "nuid": "9f906a80-bed0-4a6c-8ee1-75a5525210e4", + "showTitle": false, + "tableResultSettingsMap": {}, + "title": "" + } + }, "outputs": [], "source": [ "from cookbook.tools.uc_tool import UCTool\n", @@ -736,15 +1133,36 @@ }, { "cell_type": "markdown", - "metadata": {}, + "metadata": { + "application/vnd.databricks.v1+cell": { + "cellMetadata": {}, + "inputWidgets": {}, + "nuid": "a8b39cfd-a272-4142-b18a-e4df49a4410d", + "showTitle": false, + "tableResultSettingsMap": {}, + "title": "" + } + }, "source": [ "New tests" ] }, { "cell_type": "code", - "execution_count": null, - "metadata": {}, + "execution_count": 0, + "metadata": { + "application/vnd.databricks.v1+cell": { + "cellMetadata": { + "byteLimit": 2048000, + "rowLimit": 10000 + }, + "inputWidgets": {}, + "nuid": "dfb377c5-ff24-41cf-8ac0-bbf8a74a4d9d", + "showTitle": false, + "tableResultSettingsMap": {}, + "title": "" + } + }, "outputs": [], "source": [ "%%writefile tools/test_code_exec_as_uc_tool.py\n", @@ -854,13 +1272,29 @@ }, { "cell_type": "code", - "execution_count": null, - "metadata": {}, + "execution_count": 0, + "metadata": { + "application/vnd.databricks.v1+cell": { + "cellMetadata": { + "byteLimit": 2048000, + "rowLimit": 10000 + }, + "inputWidgets": {}, + "nuid": "3cc03332-afba-454e-89d6-80f9919114fe", + "showTitle": false, + "tableResultSettingsMap": {}, + "title": "" + } + }, "outputs": [], "source": [ "import pytest\n", "\n", - "# Run tests from test_sku_translator.py\n", + "if du.is_in_databricks_notebook():\n", + " import sys\n", + " sys.dont_write_bytecode = True # Skip writing .pyc files to the bytecode cache on the cluster.\n", + "\n", + "# Run tests from test_code_exec_as_uc_tool.py\n", "pytest.main([\"-v\", \"tools/test_code_exec_as_uc_tool.py\"])\n", "\n" ] @@ -868,11 +1302,14 @@ ], "metadata": { "application/vnd.databricks.v1+notebook": { + "computePreferences": null, "dashboards": [], "environmentMetadata": null, "language": "python", - "notebookMetadata": {}, - "notebookName": "02_agent__function_calling_mlflow_sdk", + "notebookMetadata": { + "pythonIndentUnit": 4 + }, + "notebookName": "04_create_tools", "widgets": {} }, "kernelspec": { @@ -881,16 +1318,7 @@ "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.11.10" + "name": "python" } }, "nbformat": 4, diff --git a/openai_sdk_agent_app_sample_code/05_tool_calling_agent.ipynb b/openai_sdk_agent_app_sample_code/05_tool_calling_agent.ipynb index 0d60c00..63b2440 100644 --- a/openai_sdk_agent_app_sample_code/05_tool_calling_agent.ipynb +++ b/openai_sdk_agent_app_sample_code/05_tool_calling_agent.ipynb @@ -70,10 +70,13 @@ }, { "cell_type": "code", - "execution_count": 1, + "execution_count": 0, "metadata": { "application/vnd.databricks.v1+cell": { - "cellMetadata": {}, + "cellMetadata": { + "byteLimit": 2048000, + "rowLimit": 10000 + }, "inputWidgets": {}, "nuid": "6d4030e8-ae97-4351-bebd-9651d283578f", "showTitle": false, @@ -83,14 +86,23 @@ }, "outputs": [], "source": [ - "# %pip install -qqqq -U -r requirements.txt\n", - "# # Restart to load the packages into the Python environment\n", - "# dbutils.library.restartPython()" + "%pip install -qqqq -U -r requirements.txt\n", + "# Restart to load the packages into the Python environment\n", + "dbutils.library.restartPython()" ] }, { "cell_type": "markdown", - "metadata": {}, + "metadata": { + "application/vnd.databricks.v1+cell": { + "cellMetadata": {}, + "inputWidgets": {}, + "nuid": "b8b12265-d7fd-4ac4-bdd4-73d889807669", + "showTitle": false, + "tableResultSettingsMap": {}, + "title": "" + } + }, "source": [ "### 🚫✏️ Connect to Databricks\n", "\n", @@ -99,8 +111,17 @@ }, { "cell_type": "code", - "execution_count": 2, - "metadata": {}, + "execution_count": 0, + "metadata": { + "application/vnd.databricks.v1+cell": { + "cellMetadata": {}, + "inputWidgets": {}, + "nuid": "4b3bd6a6-3e55-42ee-9340-dd663edd9e1c", + "showTitle": false, + "tableResultSettingsMap": {}, + "title": "" + } + }, "outputs": [], "source": [ "from mlflow.utils import databricks_utils as du\n", @@ -115,7 +136,16 @@ }, { "cell_type": "markdown", - "metadata": {}, + "metadata": { + "application/vnd.databricks.v1+cell": { + "cellMetadata": {}, + "inputWidgets": {}, + "nuid": "c7a986f8-2dea-475f-bdd2-8398507c9dd3", + "showTitle": false, + "tableResultSettingsMap": {}, + "title": "" + } + }, "source": [ "### 🚫✏️ Load the Agent's UC storage locations; set up MLflow experiment\n", "\n", @@ -124,14 +154,27 @@ }, { "cell_type": "code", - "execution_count": null, - "metadata": {}, + "execution_count": 0, + "metadata": { + "application/vnd.databricks.v1+cell": { + "cellMetadata": { + "byteLimit": 2048000, + "rowLimit": 10000 + }, + "inputWidgets": {}, + "nuid": "906645cf-0891-409a-94f2-1b5718d1c65f", + "showTitle": false, + "tableResultSettingsMap": {}, + "title": "" + } + }, "outputs": [], "source": [ "from cookbook.config.shared.agent_storage_location import AgentStorageConfig\n", "from cookbook.databricks_utils import get_mlflow_experiment_url\n", "from cookbook.config import load_serializable_config_from_yaml_file\n", "import mlflow \n", + "import os\n", "\n", "# Load the Agent's storage locations\n", "agent_storage_config: AgentStorageConfig= load_serializable_config_from_yaml_file(\"./configs/agent_storage_config.yaml\")\n", @@ -149,7 +192,16 @@ }, { "cell_type": "markdown", - "metadata": {}, + "metadata": { + "application/vnd.databricks.v1+cell": { + "cellMetadata": {}, + "inputWidgets": {}, + "nuid": "585aaefc-6499-44bc-803e-b1bcacd8694d", + "showTitle": false, + "tableResultSettingsMap": {}, + "title": "" + } + }, "source": [ "### 🚫✏️ Helper method to log the Agent's code & config to MLflow\n", "\n", @@ -158,8 +210,20 @@ }, { "cell_type": "code", - "execution_count": 15, - "metadata": {}, + "execution_count": 0, + "metadata": { + "application/vnd.databricks.v1+cell": { + "cellMetadata": { + "byteLimit": 2048000, + "rowLimit": 10000 + }, + "inputWidgets": {}, + "nuid": "45b1cce3-fdae-4578-a107-aec572e40697", + "showTitle": false, + "tableResultSettingsMap": {}, + "title": "" + } + }, "outputs": [], "source": [ "\n", @@ -232,8 +296,20 @@ }, { "cell_type": "code", - "execution_count": 9, - "metadata": {}, + "execution_count": 0, + "metadata": { + "application/vnd.databricks.v1+cell": { + "cellMetadata": { + "byteLimit": 2048000, + "rowLimit": 10000 + }, + "inputWidgets": {}, + "nuid": "28426475-3329-426c-a5cc-4acbc65bf0f1", + "showTitle": false, + "tableResultSettingsMap": {}, + "title": "" + } + }, "outputs": [], "source": [ "# Import Cookbook Agent configurations, which are Pydantic models\n", @@ -272,7 +348,7 @@ "retriever_tool = VectorSearchRetrieverTool(\n", " name=\"search_product_docs\",\n", " description=\"Use this tool to search for product documentation.\",\n", - " vector_search_index=\"ep.cookbook_local_test.product_docs_docs_chunked_index__v1\",\n", + " vector_search_index=\"shared.cookbook_local_test_fix.test_product_docs_docs_chunked_index__v2\",\n", " vector_search_schema=VectorSearchSchema(\n", " # These columns are the default values used in the `01_data_pipeline` notebook\n", " # If you used a different column names in that notebook OR you are using a pre-built vector index, update the column names here.\n", @@ -294,7 +370,7 @@ "# #### ✅✏️ Add Unity Catalog tools to the Agent\n", "########################\n", "\n", - "translate_sku_tool = UCTool(uc_function_name=\"ep.cookbook_local_test.sku_sample_translator\")\n", + "translate_sku_tool = UCTool(uc_function_name=\"shared.cookbook_local_test_fix.sku_sample_translator\")\n", "\n", "\n", "########################\n", @@ -327,7 +403,7 @@ "\n", "fc_agent_config = FunctionCallingAgentConfig(\n", " llm_config=LLMConfig(\n", - " llm_endpoint_name=\"ep-gpt4o-new\", # Model serving endpoint w/ a Chat Completions API\n", + " llm_endpoint_name=\"agents-demo-gpt4o-mini\", # Model serving endpoint w/ a Chat Completions API\n", " llm_system_prompt_template=system_prompt, # System prompt template\n", " llm_parameters=LLMParametersConfig(\n", " temperature=0.01, max_tokens=1500\n", @@ -354,7 +430,16 @@ }, { "cell_type": "markdown", - "metadata": {}, + "metadata": { + "application/vnd.databricks.v1+cell": { + "cellMetadata": {}, + "inputWidgets": {}, + "nuid": "ad89e46f-eb82-4d6e-aaf6-595a46b365fe", + "showTitle": false, + "tableResultSettingsMap": {}, + "title": "" + } + }, "source": [ "#### ✅✏️ Optionally, adjust the Agent's code\n", "\n", @@ -365,8 +450,20 @@ }, { "cell_type": "code", - "execution_count": null, - "metadata": {}, + "execution_count": 0, + "metadata": { + "application/vnd.databricks.v1+cell": { + "cellMetadata": { + "byteLimit": 2048000, + "rowLimit": 10000 + }, + "inputWidgets": {}, + "nuid": "012bd196-bbec-4359-910d-714bb311cf46", + "showTitle": false, + "tableResultSettingsMap": {}, + "title": "" + } + }, "outputs": [], "source": [ "from cookbook.agents.function_calling_agent import FunctionCallingAgent\n", @@ -378,8 +475,20 @@ }, { "cell_type": "code", - "execution_count": 6, - "metadata": {}, + "execution_count": 0, + "metadata": { + "application/vnd.databricks.v1+cell": { + "cellMetadata": { + "byteLimit": 2048000, + "rowLimit": 10000 + }, + "inputWidgets": {}, + "nuid": "468a4299-85ea-4d99-91ff-d48ca2f50cae", + "showTitle": false, + "tableResultSettingsMap": {}, + "title": "" + } + }, "outputs": [], "source": [ "%load_ext autoreload\n", @@ -388,7 +497,16 @@ }, { "cell_type": "markdown", - "metadata": {}, + "metadata": { + "application/vnd.databricks.v1+cell": { + "cellMetadata": {}, + "inputWidgets": {}, + "nuid": "6586ccdb-001f-4131-afb8-b51acdf62f7a", + "showTitle": false, + "tableResultSettingsMap": {}, + "title": "" + } + }, "source": [ "#### ✅✏️ 🅰 Vibe check the Agent for a single query\n", "\n", @@ -399,8 +517,20 @@ }, { "cell_type": "code", - "execution_count": null, - "metadata": {}, + "execution_count": 0, + "metadata": { + "application/vnd.databricks.v1+cell": { + "cellMetadata": { + "byteLimit": 2048000, + "rowLimit": 10000 + }, + "inputWidgets": {}, + "nuid": "8e8f044a-44de-47a3-83c9-ff93c2f391cf", + "showTitle": false, + "tableResultSettingsMap": {}, + "title": "" + } + }, "outputs": [], "source": [ "from cookbook.databricks_utils import get_mlflow_experiment_traces_url\n", @@ -421,15 +551,36 @@ }, { "cell_type": "markdown", - "metadata": {}, + "metadata": { + "application/vnd.databricks.v1+cell": { + "cellMetadata": {}, + "inputWidgets": {}, + "nuid": "a989ccd2-6321-413a-a009-5101563193cf", + "showTitle": false, + "tableResultSettingsMap": {}, + "title": "" + } + }, "source": [ "Now, let's test a multi-turn conversation with the Agent." ] }, { "cell_type": "code", - "execution_count": null, - "metadata": {}, + "execution_count": 0, + "metadata": { + "application/vnd.databricks.v1+cell": { + "cellMetadata": { + "byteLimit": 2048000, + "rowLimit": 10000 + }, + "inputWidgets": {}, + "nuid": "7dd76041-653f-4da9-b81e-6eb61d280d1d", + "showTitle": false, + "tableResultSettingsMap": {}, + "title": "" + } + }, "outputs": [], "source": [ "second_turn = {'messages': output['messages'] + [{\"role\": \"user\", \"content\": \"How do I turn it on?\"}]}\n", @@ -445,7 +596,16 @@ }, { "cell_type": "markdown", - "metadata": {}, + "metadata": { + "application/vnd.databricks.v1+cell": { + "cellMetadata": {}, + "inputWidgets": {}, + "nuid": "a3d50802-10e0-45bf-82b1-258390891193", + "showTitle": false, + "tableResultSettingsMap": {}, + "title": "" + } + }, "source": [ "#### ✅✏️ 🅱 Evaluate the Agent using your evaluation set\n", "\n", @@ -454,8 +614,20 @@ }, { "cell_type": "code", - "execution_count": null, - "metadata": {}, + "execution_count": 0, + "metadata": { + "application/vnd.databricks.v1+cell": { + "cellMetadata": { + "byteLimit": 2048000, + "rowLimit": 10000 + }, + "inputWidgets": {}, + "nuid": "38414512-1258-45a3-a6a3-cdcf35b8878d", + "showTitle": false, + "tableResultSettingsMap": {}, + "title": "" + } + }, "outputs": [], "source": [ "evaluation_set = spark.table(agent_storage_config.evaluation_set_uc_table)\n", @@ -478,7 +650,16 @@ }, { "cell_type": "markdown", - "metadata": {}, + "metadata": { + "application/vnd.databricks.v1+cell": { + "cellMetadata": {}, + "inputWidgets": {}, + "nuid": "8df6d8a4-25b3-434d-9f58-c8cccb24018e", + "showTitle": false, + "tableResultSettingsMap": {}, + "title": "" + } + }, "source": [ "## 2️⃣ Deploy a version of your Agent - either to the Review App or Production\n", "\n", @@ -495,15 +676,36 @@ }, { "cell_type": "markdown", - "metadata": {}, + "metadata": { + "application/vnd.databricks.v1+cell": { + "cellMetadata": {}, + "inputWidgets": {}, + "nuid": "5346c87b-adc5-4fba-a20b-67ecaf23a8d8", + "showTitle": false, + "tableResultSettingsMap": {}, + "title": "" + } + }, "source": [ "Option 1: Deploy the last agent you logged above" ] }, { "cell_type": "code", - "execution_count": null, - "metadata": {}, + "execution_count": 0, + "metadata": { + "application/vnd.databricks.v1+cell": { + "cellMetadata": { + "byteLimit": 2048000, + "rowLimit": 10000 + }, + "inputWidgets": {}, + "nuid": "1866d6f6-6d1e-4048-be8f-c436c80878a7", + "showTitle": false, + "tableResultSettingsMap": {}, + "title": "" + } + }, "outputs": [], "source": [ "from databricks import agents\n", @@ -522,15 +724,33 @@ }, { "cell_type": "markdown", - "metadata": {}, + "metadata": { + "application/vnd.databricks.v1+cell": { + "cellMetadata": {}, + "inputWidgets": {}, + "nuid": "d7c571dd-454c-4b1c-adf0-193723dfe667", + "showTitle": false, + "tableResultSettingsMap": {}, + "title": "" + } + }, "source": [ "Option 2: Log the latest copy of the Agent's code/config and deploy it" ] }, { "cell_type": "code", - "execution_count": null, - "metadata": {}, + "execution_count": 0, + "metadata": { + "application/vnd.databricks.v1+cell": { + "cellMetadata": {}, + "inputWidgets": {}, + "nuid": "60959035-ffde-4922-a045-33ee0f550b4e", + "showTitle": false, + "tableResultSettingsMap": {}, + "title": "" + } + }, "outputs": [], "source": [ "from databricks import agents\n", @@ -552,15 +772,33 @@ }, { "cell_type": "markdown", - "metadata": {}, + "metadata": { + "application/vnd.databricks.v1+cell": { + "cellMetadata": {}, + "inputWidgets": {}, + "nuid": "f00e0a2c-2643-48f1-821c-0bc74ba6c147", + "showTitle": false, + "tableResultSettingsMap": {}, + "title": "" + } + }, "source": [ "Load the logged model to test it locally" ] }, { "cell_type": "code", - "execution_count": null, - "metadata": {}, + "execution_count": 0, + "metadata": { + "application/vnd.databricks.v1+cell": { + "cellMetadata": {}, + "inputWidgets": {}, + "nuid": "44059c64-4ce2-448e-a724-12ba430993f2", + "showTitle": false, + "tableResultSettingsMap": {}, + "title": "" + } + }, "outputs": [], "source": [ "import mlflow\n", @@ -572,8 +810,17 @@ }, { "cell_type": "code", - "execution_count": null, - "metadata": {}, + "execution_count": 0, + "metadata": { + "application/vnd.databricks.v1+cell": { + "cellMetadata": {}, + "inputWidgets": {}, + "nuid": "059317ba-6f31-47e1-996a-2aa8eb3ef8da", + "showTitle": false, + "tableResultSettingsMap": {}, + "title": "" + } + }, "outputs": [], "source": [ "from databricks import agents\n", @@ -588,11 +835,14 @@ ], "metadata": { "application/vnd.databricks.v1+notebook": { + "computePreferences": null, "dashboards": [], "environmentMetadata": null, "language": "python", - "notebookMetadata": {}, - "notebookName": "02_agent__function_calling_mlflow_sdk", + "notebookMetadata": { + "pythonIndentUnit": 4 + }, + "notebookName": "05_tool_calling_agent", "widgets": {} }, "kernelspec": { @@ -601,16 +851,7 @@ "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.11.10" + "name": "python" } }, "nbformat": 4, diff --git a/openai_sdk_agent_app_sample_code/requirements.txt b/openai_sdk_agent_app_sample_code/requirements.txt index a6070d4..e138706 100644 --- a/openai_sdk_agent_app_sample_code/requirements.txt +++ b/openai_sdk_agent_app_sample_code/requirements.txt @@ -10,3 +10,4 @@ tabulate pandas pyspark databricks-connect==15.1.0 +pytest From 86449c0008136801b64fec60d0b783eeb85110da Mon Sep 17 00:00:00 2001 From: Udhayaraj Sivalingam Date: Fri, 6 Dec 2024 15:31:34 +0100 Subject: [PATCH 2/3] convert genie_agent from notebook to normal python file --- .../cookbook/agents/genie_agent.py | 14 -------------- 1 file changed, 14 deletions(-) diff --git a/openai_sdk_agent_app_sample_code/cookbook/agents/genie_agent.py b/openai_sdk_agent_app_sample_code/cookbook/agents/genie_agent.py index b158f7a..23d000c 100644 --- a/openai_sdk_agent_app_sample_code/cookbook/agents/genie_agent.py +++ b/openai_sdk_agent_app_sample_code/cookbook/agents/genie_agent.py @@ -1,17 +1,3 @@ -# Databricks notebook source -# MAGIC %md -# MAGIC # Genie Space Agent -# MAGIC -# MAGIC In this notebook, we construct a Genie space as an Agent. This Agent is encapsulated in a MLflow PyFunc class called `GenieAgent()`. - -# COMMAND ---------- - -# # # If running this notebook by itself, uncomment these. -# %pip install --upgrade -qqqq mlflow databricks-sdk tabulate tiktoken -# dbutils.library.restartPython() - -# COMMAND ---------- - import sys # Add the parent directory to the path so we can import the `utils` modules From fdf736f09fe4964622b2e40d9f8411bf75b0497f Mon Sep 17 00:00:00 2001 From: s-udhaya Date: Fri, 6 Dec 2024 14:36:53 +0000 Subject: [PATCH 3/3] fix minor things in tool_calling_agent notebook --- .../05_tool_calling_agent.ipynb | 106 ++++++++++-------- 1 file changed, 62 insertions(+), 44 deletions(-) diff --git a/openai_sdk_agent_app_sample_code/05_tool_calling_agent.ipynb b/openai_sdk_agent_app_sample_code/05_tool_calling_agent.ipynb index 63b2440..6fe158d 100644 --- a/openai_sdk_agent_app_sample_code/05_tool_calling_agent.ipynb +++ b/openai_sdk_agent_app_sample_code/05_tool_calling_agent.ipynb @@ -4,7 +4,10 @@ "cell_type": "markdown", "metadata": { "application/vnd.databricks.v1+cell": { - "cellMetadata": {}, + "cellMetadata": { + "byteLimit": 2048000, + "rowLimit": 10000 + }, "inputWidgets": {}, "nuid": "31661828-f9bb-4fc2-a1bd-94424a27ed52", "showTitle": false, @@ -33,7 +36,10 @@ "cell_type": "markdown", "metadata": { "application/vnd.databricks.v1+cell": { - "cellMetadata": {}, + "cellMetadata": { + "byteLimit": 2048000, + "rowLimit": 10000 + }, "inputWidgets": {}, "nuid": "5d9f685a-fdb7-49a4-9e3a-a4a9e964d045", "showTitle": false, @@ -54,7 +60,10 @@ "cell_type": "markdown", "metadata": { "application/vnd.databricks.v1+cell": { - "cellMetadata": {}, + "cellMetadata": { + "byteLimit": 2048000, + "rowLimit": 10000 + }, "inputWidgets": {}, "nuid": "bb4f8cc0-1797-4beb-a9f2-df21a9db79f0", "showTitle": false, @@ -86,16 +95,19 @@ }, "outputs": [], "source": [ - "%pip install -qqqq -U -r requirements.txt\n", - "# Restart to load the packages into the Python environment\n", - "dbutils.library.restartPython()" + "# %pip install -qqqq -U -r requirements.txt\n", + "# # Restart to load the packages into the Python environment\n", + "# dbutils.library.restartPython()" ] }, { "cell_type": "markdown", "metadata": { "application/vnd.databricks.v1+cell": { - "cellMetadata": {}, + "cellMetadata": { + "byteLimit": 2048000, + "rowLimit": 10000 + }, "inputWidgets": {}, "nuid": "b8b12265-d7fd-4ac4-bdd4-73d889807669", "showTitle": false, @@ -114,7 +126,10 @@ "execution_count": 0, "metadata": { "application/vnd.databricks.v1+cell": { - "cellMetadata": {}, + "cellMetadata": { + "byteLimit": 2048000, + "rowLimit": 10000 + }, "inputWidgets": {}, "nuid": "4b3bd6a6-3e55-42ee-9340-dd663edd9e1c", "showTitle": false, @@ -138,7 +153,10 @@ "cell_type": "markdown", "metadata": { "application/vnd.databricks.v1+cell": { - "cellMetadata": {}, + "cellMetadata": { + "byteLimit": 2048000, + "rowLimit": 10000 + }, "inputWidgets": {}, "nuid": "c7a986f8-2dea-475f-bdd2-8398507c9dd3", "showTitle": false, @@ -194,7 +212,10 @@ "cell_type": "markdown", "metadata": { "application/vnd.databricks.v1+cell": { - "cellMetadata": {}, + "cellMetadata": { + "byteLimit": 2048000, + "rowLimit": 10000 + }, "inputWidgets": {}, "nuid": "585aaefc-6499-44bc-803e-b1bcacd8694d", "showTitle": false, @@ -268,7 +289,10 @@ "cell_type": "markdown", "metadata": { "application/vnd.databricks.v1+cell": { - "cellMetadata": {}, + "cellMetadata": { + "byteLimit": 2048000, + "rowLimit": 10000 + }, "inputWidgets": {}, "nuid": "9933d05f-29fa-452e-abdc-2a02328fbe22", "showTitle": false, @@ -348,7 +372,7 @@ "retriever_tool = VectorSearchRetrieverTool(\n", " name=\"search_product_docs\",\n", " description=\"Use this tool to search for product documentation.\",\n", - " vector_search_index=\"shared.cookbook_local_test_fix.test_product_docs_docs_chunked_index__v2\",\n", + " vector_search_index=\"ep.cookbook_local_test.test_product_docs_docs_chunked_index__v2\",\n", " vector_search_schema=VectorSearchSchema(\n", " # These columns are the default values used in the `01_data_pipeline` notebook\n", " # If you used a different column names in that notebook OR you are using a pre-built vector index, update the column names here.\n", @@ -370,7 +394,7 @@ "# #### ✅✏️ Add Unity Catalog tools to the Agent\n", "########################\n", "\n", - "translate_sku_tool = UCTool(uc_function_name=\"shared.cookbook_local_test_fix.sku_sample_translator\")\n", + "translate_sku_tool = UCTool(uc_function_name=\"ep.cookbook_local_test.sku_sample_translator\")\n", "\n", "\n", "########################\n", @@ -403,7 +427,7 @@ "\n", "fc_agent_config = FunctionCallingAgentConfig(\n", " llm_config=LLMConfig(\n", - " llm_endpoint_name=\"agents-demo-gpt4o-mini\", # Model serving endpoint w/ a Chat Completions API\n", + " llm_endpoint_name=\"ep-gpt4o-new\", # Model serving endpoint w/ a Chat Completions API\n", " llm_system_prompt_template=system_prompt, # System prompt template\n", " llm_parameters=LLMParametersConfig(\n", " temperature=0.01, max_tokens=1500\n", @@ -432,7 +456,10 @@ "cell_type": "markdown", "metadata": { "application/vnd.databricks.v1+cell": { - "cellMetadata": {}, + "cellMetadata": { + "byteLimit": 2048000, + "rowLimit": 10000 + }, "inputWidgets": {}, "nuid": "ad89e46f-eb82-4d6e-aaf6-595a46b365fe", "showTitle": false, @@ -499,7 +526,10 @@ "cell_type": "markdown", "metadata": { "application/vnd.databricks.v1+cell": { - "cellMetadata": {}, + "cellMetadata": { + "byteLimit": 2048000, + "rowLimit": 10000 + }, "inputWidgets": {}, "nuid": "6586ccdb-001f-4131-afb8-b51acdf62f7a", "showTitle": false, @@ -553,7 +583,10 @@ "cell_type": "markdown", "metadata": { "application/vnd.databricks.v1+cell": { - "cellMetadata": {}, + "cellMetadata": { + "byteLimit": 2048000, + "rowLimit": 10000 + }, "inputWidgets": {}, "nuid": "a989ccd2-6321-413a-a009-5101563193cf", "showTitle": false, @@ -598,7 +631,10 @@ "cell_type": "markdown", "metadata": { "application/vnd.databricks.v1+cell": { - "cellMetadata": {}, + "cellMetadata": { + "byteLimit": 2048000, + "rowLimit": 10000 + }, "inputWidgets": {}, "nuid": "a3d50802-10e0-45bf-82b1-258390891193", "showTitle": false, @@ -652,7 +688,10 @@ "cell_type": "markdown", "metadata": { "application/vnd.databricks.v1+cell": { - "cellMetadata": {}, + "cellMetadata": { + "byteLimit": 2048000, + "rowLimit": 10000 + }, "inputWidgets": {}, "nuid": "8df6d8a4-25b3-434d-9f58-c8cccb24018e", "showTitle": false, @@ -678,7 +717,10 @@ "cell_type": "markdown", "metadata": { "application/vnd.databricks.v1+cell": { - "cellMetadata": {}, + "cellMetadata": { + "byteLimit": 2048000, + "rowLimit": 10000 + }, "inputWidgets": {}, "nuid": "5346c87b-adc5-4fba-a20b-67ecaf23a8d8", "showTitle": false, @@ -807,30 +849,6 @@ "\n", "loaded_model.predict({\"messages\": [{\"role\": \"user\", \"content\": \"A test question?\"}]})" ] - }, - { - "cell_type": "code", - "execution_count": 0, - "metadata": { - "application/vnd.databricks.v1+cell": { - "cellMetadata": {}, - "inputWidgets": {}, - "nuid": "059317ba-6f31-47e1-996a-2aa8eb3ef8da", - "showTitle": false, - "tableResultSettingsMap": {}, - "title": "" - } - }, - "outputs": [], - "source": [ - "from databricks import agents\n", - "\n", - "# Use Unity Catalog as the model registry\n", - "mlflow.set_registry_uri(\"databricks-uc\")\n", - "\n", - "with mlflow.start_run():\n", - " logged_agent_info = log_agent_to_mlflow(fc_agent_config)" - ] } ], "metadata": {