From ca67f61bd8a84bc8ed899d3db036cdefe4848cc1 Mon Sep 17 00:00:00 2001 From: shubhiasthana Date: Tue, 16 Dec 2025 14:23:25 -0800 Subject: [PATCH 1/7] tool_selection collab nb --- docs/examples/notebooks/tool_selection.ipynb | 701 +++++++++++++++++++ 1 file changed, 701 insertions(+) create mode 100644 docs/examples/notebooks/tool_selection.ipynb diff --git a/docs/examples/notebooks/tool_selection.ipynb b/docs/examples/notebooks/tool_selection.ipynb new file mode 100644 index 00000000..6872a214 --- /dev/null +++ b/docs/examples/notebooks/tool_selection.ipynb @@ -0,0 +1,701 @@ +{ + "cells": [ + { + "cell_type": "markdown", + "metadata": { + "id": "IByYhgKy9WCo" + }, + "source": [ + "# Simple Example\n", + "This Jupyter notebook runs on Colab and shows a simple example of Tooling selection using Top-K and Double Round Robin" + ] + }, + { + "cell_type": "markdown", + "metadata": { + "id": "ZIu6B1Ht927Z" + }, + "source": [ + "## Install Ollama\n", + "\n", + "Before we get started with Mellea, we download, install and serve ollama. We define set_css to wrap Colab output." + ] + }, + { + "cell_type": "code", + "execution_count": 2, + "metadata": { + "id": "VDaTfltQY3Fl", + "colab": { + "base_uri": "https://localhost:8080/" + }, + "outputId": "06232eb8-a747-47aa-f414-1ff77562f2f8" + }, + "outputs": [ + { + "output_type": "stream", + "name": "stdout", + "text": [ + ">>> Installing ollama to /usr/local\n", + ">>> Downloading Linux amd64 bundle\n", + "######################################################################## 100.0%\n", + ">>> Creating ollama user...\n", + ">>> Adding ollama user to video group...\n", + ">>> Adding current user to ollama group...\n", + ">>> Creating ollama systemd service...\n", + ">>> The Ollama API is now available at 127.0.0.1:11434.\n", + ">>> Install complete. Run \"ollama\" from the command line.\n" + ] + } + ], + "source": [ + "!curl -fsSL https://ollama.com/install.sh | sh > /dev/null\n", + "!nohup ollama serve >/dev/null 2>&1 &\n", + "\n", + "from IPython.display import HTML, display\n", + "\n", + "\n", + "def set_css():\n", + " display(HTML(\"\\n\\n\"))\n", + "\n", + "\n", + "get_ipython().events.register(\"pre_run_cell\", set_css)" + ] + }, + { + "cell_type": "markdown", + "metadata": { + "id": "jEl3nAk696mI" + }, + "source": [ + "## Install Mellea\n", + "We run `uv pip install mellea` to install Mellea." + ] + }, + { + "cell_type": "code", + "execution_count": 3, + "metadata": { + "id": "9EurAUSz_1yl", + "colab": { + "base_uri": "https://localhost:8080/", + "height": 17 + }, + "outputId": "509840ea-a8f9-42b8-f43e-031c5f90224e" + }, + "outputs": [ + { + "output_type": "display_data", + "data": { + "text/plain": [ + "" + ], + "text/html": [ + "\n", + "\n" + ] + }, + "metadata": {} + } + ], + "source": [ + "!uv pip install mellea -q" + ] + }, + { + "cell_type": "markdown", + "metadata": { + "id": "NU7bZqKA0djW" + }, + "source": [ + "### **Import top_k and double_round_robin libraries**" + ] + }, + { + "cell_type": "code", + "execution_count": 4, + "metadata": { + "id": "Jt3UC-Sa0djY", + "colab": { + "base_uri": "https://localhost:8080/", + "height": 139 + }, + "outputId": "c778249c-c236-448b-cc91-a7d1446d6bc7" + }, + "outputs": [ + { + "output_type": "display_data", + "data": { + "text/plain": [ + "" + ], + "text/html": [ + "\n", + "\n" + ] + }, + "metadata": {} + }, + { + "output_type": "stream", + "name": "stdout", + "text": [ + "Cloning into 'mellea-contribs'...\n", + "remote: Enumerating objects: 219, done.\u001b[K\n", + "remote: Counting objects: 100% (161/161), done.\u001b[K\n", + "remote: Compressing objects: 100% (114/114), done.\u001b[K\n", + "remote: Total 219 (delta 86), reused 108 (delta 44), pack-reused 58 (from 1)\u001b[K\n", + "Receiving objects: 100% (219/219), 351.40 KiB | 2.77 MiB/s, done.\n", + "Resolving deltas: 100% (97/97), done.\n" + ] + } + ], + "source": [ + "!git clone https://github.com/generative-computing/mellea-contribs.git" + ] + }, + { + "cell_type": "code", + "source": [ + "%cd mellea-contribs\n", + "!pip install -e ." + ], + "metadata": { + "colab": { + "base_uri": "https://localhost:8080/", + "height": 1000 + }, + "id": "Pf8560taKn7x", + "outputId": "4b044d64-b9e9-418a-f963-4c039d9524da" + }, + "execution_count": 5, + "outputs": [ + { + "output_type": "display_data", + "data": { + "text/plain": [ + "" + ], + "text/html": [ + "\n", + "\n" + ] + }, + "metadata": {} + }, + { + "output_type": "stream", + "name": "stdout", + "text": [ + "/content/mellea-contribs\n", + "Obtaining file:///content/mellea-contribs\n", + " Installing build dependencies ... \u001b[?25l\u001b[?25hdone\n", + " Checking if build backend supports build_editable ... \u001b[?25l\u001b[?25hdone\n", + " Getting requirements to build editable ... \u001b[?25l\u001b[?25hdone\n", + " Preparing editable metadata (pyproject.toml) ... \u001b[?25l\u001b[?25hdone\n", + "Requirement already satisfied: mellea in /usr/local/lib/python3.12/dist-packages (from mellea-contribs==0.1.2) (0.2.1)\n", + "Requirement already satisfied: pydantic in /usr/local/lib/python3.12/dist-packages (from mellea->mellea-contribs==0.1.2) (2.12.3)\n", + "Requirement already satisfied: openai in /usr/local/lib/python3.12/dist-packages (from mellea->mellea-contribs==0.1.2) (2.9.0)\n", + "Requirement already satisfied: jinja2 in /usr/local/lib/python3.12/dist-packages (from mellea->mellea-contribs==0.1.2) (3.1.6)\n", + "Requirement already satisfied: json5 in /usr/local/lib/python3.12/dist-packages (from mellea->mellea-contribs==0.1.2) (0.12.1)\n", + "Requirement already satisfied: ollama>=0.5.1 in /usr/local/lib/python3.12/dist-packages (from mellea->mellea-contribs==0.1.2) (0.6.1)\n", + "Requirement already satisfied: ansicolors in /usr/local/lib/python3.12/dist-packages (from mellea->mellea-contribs==0.1.2) (1.1.8)\n", + "Requirement already satisfied: requests>=2.32.3 in /usr/local/lib/python3.12/dist-packages (from mellea->mellea-contribs==0.1.2) (2.32.4)\n", + "Requirement already satisfied: uvicorn in /usr/local/lib/python3.12/dist-packages (from mellea->mellea-contribs==0.1.2) (0.38.0)\n", + "Requirement already satisfied: fastapi in /usr/local/lib/python3.12/dist-packages (from mellea->mellea-contribs==0.1.2) (0.118.3)\n", + "Requirement already satisfied: types-requests in /usr/local/lib/python3.12/dist-packages (from mellea->mellea-contribs==0.1.2) (2.32.4.20250913)\n", + "Requirement already satisfied: types-tqdm in /usr/local/lib/python3.12/dist-packages (from mellea->mellea-contribs==0.1.2) (4.67.0.20250809)\n", + "Requirement already satisfied: typer in /usr/local/lib/python3.12/dist-packages (from mellea->mellea-contribs==0.1.2) (0.20.0)\n", + "Requirement already satisfied: click<8.2.0 in /usr/local/lib/python3.12/dist-packages (from mellea->mellea-contribs==0.1.2) (8.1.8)\n", + "Requirement already satisfied: mistletoe>=1.4.0 in /usr/local/lib/python3.12/dist-packages (from mellea->mellea-contribs==0.1.2) (1.5.1)\n", + "Requirement already satisfied: huggingface-hub>=0.33.4 in /usr/local/lib/python3.12/dist-packages (from mellea->mellea-contribs==0.1.2) (0.36.0)\n", + "Requirement already satisfied: pillow in /usr/local/lib/python3.12/dist-packages (from mellea->mellea-contribs==0.1.2) (11.3.0)\n", + "Requirement already satisfied: granite-common>=0.3.5 in /usr/local/lib/python3.12/dist-packages (from mellea->mellea-contribs==0.1.2) (0.3.6)\n", + "Requirement already satisfied: math_verify in /usr/local/lib/python3.12/dist-packages (from mellea->mellea-contribs==0.1.2) (0.8.0)\n", + "Requirement already satisfied: rouge_score in /usr/local/lib/python3.12/dist-packages (from mellea->mellea-contribs==0.1.2) (0.1.2)\n", + "Requirement already satisfied: llm-sandbox>=0.3.23 in /usr/local/lib/python3.12/dist-packages (from llm-sandbox[docker]>=0.3.23->mellea->mellea-contribs==0.1.2) (0.3.31)\n", + "Requirement already satisfied: jsonschema in /usr/local/lib/python3.12/dist-packages (from granite-common>=0.3.5->mellea->mellea-contribs==0.1.2) (4.25.1)\n", + "Requirement already satisfied: filelock in /usr/local/lib/python3.12/dist-packages (from huggingface-hub>=0.33.4->mellea->mellea-contribs==0.1.2) (3.20.0)\n", + "Requirement already satisfied: fsspec>=2023.5.0 in /usr/local/lib/python3.12/dist-packages (from huggingface-hub>=0.33.4->mellea->mellea-contribs==0.1.2) (2025.3.0)\n", + "Requirement already satisfied: packaging>=20.9 in /usr/local/lib/python3.12/dist-packages (from huggingface-hub>=0.33.4->mellea->mellea-contribs==0.1.2) (25.0)\n", + "Requirement already satisfied: pyyaml>=5.1 in /usr/local/lib/python3.12/dist-packages (from huggingface-hub>=0.33.4->mellea->mellea-contribs==0.1.2) (6.0.3)\n", + "Requirement already satisfied: tqdm>=4.42.1 in /usr/local/lib/python3.12/dist-packages (from huggingface-hub>=0.33.4->mellea->mellea-contribs==0.1.2) (4.67.1)\n", + "Requirement already satisfied: typing-extensions>=3.7.4.3 in /usr/local/lib/python3.12/dist-packages (from huggingface-hub>=0.33.4->mellea->mellea-contribs==0.1.2) (4.15.0)\n", + "Requirement already satisfied: hf-xet<2.0.0,>=1.1.3 in /usr/local/lib/python3.12/dist-packages (from huggingface-hub>=0.33.4->mellea->mellea-contribs==0.1.2) (1.2.0)\n", + "Requirement already satisfied: docker>=7.1.0 in /usr/local/lib/python3.12/dist-packages (from llm-sandbox[docker]>=0.3.23->mellea->mellea-contribs==0.1.2) (7.1.0)\n", + "Requirement already satisfied: httpx>=0.27 in /usr/local/lib/python3.12/dist-packages (from ollama>=0.5.1->mellea->mellea-contribs==0.1.2) (0.28.1)\n", + "Requirement already satisfied: annotated-types>=0.6.0 in /usr/local/lib/python3.12/dist-packages (from pydantic->mellea->mellea-contribs==0.1.2) (0.7.0)\n", + "Requirement already satisfied: pydantic-core==2.41.4 in /usr/local/lib/python3.12/dist-packages (from pydantic->mellea->mellea-contribs==0.1.2) (2.41.4)\n", + "Requirement already satisfied: typing-inspection>=0.4.2 in /usr/local/lib/python3.12/dist-packages (from pydantic->mellea->mellea-contribs==0.1.2) (0.4.2)\n", + "Requirement already satisfied: charset_normalizer<4,>=2 in /usr/local/lib/python3.12/dist-packages (from requests>=2.32.3->mellea->mellea-contribs==0.1.2) (3.4.4)\n", + "Requirement already satisfied: idna<4,>=2.5 in /usr/local/lib/python3.12/dist-packages (from requests>=2.32.3->mellea->mellea-contribs==0.1.2) (3.11)\n", + "Requirement already satisfied: urllib3<3,>=1.21.1 in /usr/local/lib/python3.12/dist-packages (from requests>=2.32.3->mellea->mellea-contribs==0.1.2) (2.5.0)\n", + "Requirement already satisfied: certifi>=2017.4.17 in /usr/local/lib/python3.12/dist-packages (from requests>=2.32.3->mellea->mellea-contribs==0.1.2) (2025.11.12)\n", + "Requirement already satisfied: starlette<0.49.0,>=0.40.0 in /usr/local/lib/python3.12/dist-packages (from fastapi->mellea->mellea-contribs==0.1.2) (0.48.0)\n", + "Requirement already satisfied: MarkupSafe>=2.0 in /usr/local/lib/python3.12/dist-packages (from jinja2->mellea->mellea-contribs==0.1.2) (3.0.3)\n", + "Requirement already satisfied: latex2sympy2_extended==1.10.2 in /usr/local/lib/python3.12/dist-packages (from math_verify->mellea->mellea-contribs==0.1.2) (1.10.2)\n", + "Requirement already satisfied: sympy in /usr/local/lib/python3.12/dist-packages (from latex2sympy2_extended==1.10.2->math_verify->mellea->mellea-contribs==0.1.2) (1.14.0)\n", + "Requirement already satisfied: antlr4-python3-runtime<=4.13.2,>=4.9.3 in /usr/local/lib/python3.12/dist-packages (from latex2sympy2_extended==1.10.2->math_verify->mellea->mellea-contribs==0.1.2) (4.9.3)\n", + "Requirement already satisfied: anyio<5,>=3.5.0 in /usr/local/lib/python3.12/dist-packages (from openai->mellea->mellea-contribs==0.1.2) (4.12.0)\n", + "Requirement already satisfied: distro<2,>=1.7.0 in /usr/local/lib/python3.12/dist-packages (from openai->mellea->mellea-contribs==0.1.2) (1.9.0)\n", + "Requirement already satisfied: jiter<1,>=0.10.0 in /usr/local/lib/python3.12/dist-packages (from openai->mellea->mellea-contribs==0.1.2) (0.12.0)\n", + "Requirement already satisfied: sniffio in /usr/local/lib/python3.12/dist-packages (from openai->mellea->mellea-contribs==0.1.2) (1.3.1)\n", + "Requirement already satisfied: absl-py in /usr/local/lib/python3.12/dist-packages (from rouge_score->mellea->mellea-contribs==0.1.2) (1.4.0)\n", + "Requirement already satisfied: nltk in /usr/local/lib/python3.12/dist-packages (from rouge_score->mellea->mellea-contribs==0.1.2) (3.9.1)\n", + "Requirement already satisfied: numpy in /usr/local/lib/python3.12/dist-packages (from rouge_score->mellea->mellea-contribs==0.1.2) (2.0.2)\n", + "Requirement already satisfied: six>=1.14.0 in /usr/local/lib/python3.12/dist-packages (from rouge_score->mellea->mellea-contribs==0.1.2) (1.17.0)\n", + "Requirement already satisfied: shellingham>=1.3.0 in /usr/local/lib/python3.12/dist-packages (from typer->mellea->mellea-contribs==0.1.2) (1.5.4)\n", + "Requirement already satisfied: rich>=10.11.0 in /usr/local/lib/python3.12/dist-packages (from typer->mellea->mellea-contribs==0.1.2) (13.9.4)\n", + "Requirement already satisfied: h11>=0.8 in /usr/local/lib/python3.12/dist-packages (from uvicorn->mellea->mellea-contribs==0.1.2) (0.16.0)\n", + "Requirement already satisfied: httpcore==1.* in /usr/local/lib/python3.12/dist-packages (from httpx>=0.27->ollama>=0.5.1->mellea->mellea-contribs==0.1.2) (1.0.9)\n", + "Requirement already satisfied: markdown-it-py>=2.2.0 in /usr/local/lib/python3.12/dist-packages (from rich>=10.11.0->typer->mellea->mellea-contribs==0.1.2) (4.0.0)\n", + "Requirement already satisfied: pygments<3.0.0,>=2.13.0 in /usr/local/lib/python3.12/dist-packages (from rich>=10.11.0->typer->mellea->mellea-contribs==0.1.2) (2.19.2)\n", + "Requirement already satisfied: attrs>=22.2.0 in /usr/local/lib/python3.12/dist-packages (from jsonschema->granite-common>=0.3.5->mellea->mellea-contribs==0.1.2) (25.4.0)\n", + "Requirement already satisfied: jsonschema-specifications>=2023.03.6 in /usr/local/lib/python3.12/dist-packages (from jsonschema->granite-common>=0.3.5->mellea->mellea-contribs==0.1.2) (2025.9.1)\n", + "Requirement already satisfied: referencing>=0.28.4 in /usr/local/lib/python3.12/dist-packages (from jsonschema->granite-common>=0.3.5->mellea->mellea-contribs==0.1.2) (0.37.0)\n", + "Requirement already satisfied: rpds-py>=0.7.1 in /usr/local/lib/python3.12/dist-packages (from jsonschema->granite-common>=0.3.5->mellea->mellea-contribs==0.1.2) (0.30.0)\n", + "Requirement already satisfied: joblib in /usr/local/lib/python3.12/dist-packages (from nltk->rouge_score->mellea->mellea-contribs==0.1.2) (1.5.2)\n", + "Requirement already satisfied: regex>=2021.8.3 in /usr/local/lib/python3.12/dist-packages (from nltk->rouge_score->mellea->mellea-contribs==0.1.2) (2025.11.3)\n", + "Requirement already satisfied: mdurl~=0.1 in /usr/local/lib/python3.12/dist-packages (from markdown-it-py>=2.2.0->rich>=10.11.0->typer->mellea->mellea-contribs==0.1.2) (0.1.2)\n", + "Requirement already satisfied: mpmath<1.4,>=1.1.0 in /usr/local/lib/python3.12/dist-packages (from sympy->latex2sympy2_extended==1.10.2->math_verify->mellea->mellea-contribs==0.1.2) (1.3.0)\n", + "Building wheels for collected packages: mellea-contribs\n", + " Building editable for mellea-contribs (pyproject.toml) ... \u001b[?25l\u001b[?25hdone\n", + " Created wheel for mellea-contribs: filename=mellea_contribs-0.1.2-py3-none-any.whl size=5868 sha256=f180b98b8dd3142068c245b8e44edaf166b082c48d3724df2c0825892be00eec\n", + " Stored in directory: /tmp/pip-ephem-wheel-cache-eap6g_i9/wheels/4e/73/36/57f081d1b5b5a73a20d0f5515fda442a6c206e7645b5a97df0\n", + "Successfully built mellea-contribs\n", + "Installing collected packages: mellea-contribs\n", + "Successfully installed mellea-contribs-0.1.2\n" + ] + } + ] + }, + { + "cell_type": "markdown", + "source": [ + "## **Defining sample set of tools**" + ], + "metadata": { + "id": "W0rC23c6Lb6f" + } + }, + { + "cell_type": "code", + "source": [ + "from mellea import start_session\n", + "import mellea\n", + "\n", + "m = start_session()\n", + "\n", + "TOOLS = [\n", + " {\n", + " \"name\": \"calendar\",\n", + " \"description\": \"Internal service for scheduling meetings and managing events\"\n", + " },\n", + " {\n", + " \"name\": \"calculator\",\n", + " \"description\": \"Internal utility for budget and cost calculations\"\n", + " },\n", + " {\n", + " \"name\": \"search\",\n", + " \"description\": \"Internal indexed search over company documents\"\n", + " },\n", + "]\n", + "\n", + "USER_QUERY = \"Schedule a meeting for tomorrow and calculate the event budget\"" + ], + "metadata": { + "colab": { + "base_uri": "https://localhost:8080/", + "height": 399 + }, + "id": "oSyRPp5bKpz7", + "outputId": "84339bfe-c432-477a-da6b-2cba6712aa03" + }, + "execution_count": 6, + "outputs": [ + { + "output_type": "display_data", + "data": { + "text/plain": [ + "" + ], + "text/html": [ + "\n", + "\n" + ] + }, + "metadata": {} + }, + { + "output_type": "stream", + "name": "stderr", + "text": [ + "/usr/local/lib/python3.12/dist-packages/granite_common/granite3/granite33/output.py:184: SyntaxWarning: invalid escape sequence '\\d'\n", + " f'{re.escape(CITE_START)}{{\"document_id\": \"(\\d+)\"}}{re.escape(CITE_END)}'\n", + "pulling 6c02683809a8 sha256:6c026: 100%|██████████| 2.10G/2.10G [00:11<00:00, 152MB/s]\n", + "pulling 0f6ec9740c76 sha256:0f6ec: 0%| | 0.00/7.08k [00:00" + ], + "text/html": [ + "\n", + "\n" + ] + }, + "metadata": {} + }, + { + "output_type": "stream", + "name": "stderr", + "text": [ + "\r 0%| | 0/2 [00:00" + ], + "text/html": [ + "\n", + "\n" + ] + }, + "metadata": {} + }, + { + "output_type": "stream", + "name": "stderr", + "text": [ + "\r 0%| | 0/2 [00:00" + ], + "text/html": [ + "\n", + "\n" + ] + }, + "metadata": {} + }, + { + "output_type": "stream", + "name": "stdout", + "text": [ + "\n", + "Selected tool: calendar\n" + ] + } + ] + }, + { + "cell_type": "code", + "source": [], + "metadata": { + "id": "8iCOuJ66NTu8" + }, + "execution_count": null, + "outputs": [] + } + ], + "metadata": { + "colab": { + "gpuType": "T4", + "provenance": [] + }, + "kernelspec": { + "display_name": "Python 3 (ipykernel)", + "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.13.5" + } + }, + "nbformat": 4, + "nbformat_minor": 0 +} \ No newline at end of file From cef270dbb3ae73f634aca1c73ea5399de11ad73c Mon Sep 17 00:00:00 2001 From: shubhiasthana Date: Thu, 18 Dec 2025 12:11:29 -0800 Subject: [PATCH 2/7] tool_selection collab update --- docs/examples/notebooks/tool_selection.ipynb | 410 +++++++++++-------- 1 file changed, 241 insertions(+), 169 deletions(-) diff --git a/docs/examples/notebooks/tool_selection.ipynb b/docs/examples/notebooks/tool_selection.ipynb index 6872a214..df3d10ed 100644 --- a/docs/examples/notebooks/tool_selection.ipynb +++ b/docs/examples/notebooks/tool_selection.ipynb @@ -23,13 +23,13 @@ }, { "cell_type": "code", - "execution_count": 2, + "execution_count": null, "metadata": { "id": "VDaTfltQY3Fl", "colab": { "base_uri": "https://localhost:8080/" }, - "outputId": "06232eb8-a747-47aa-f414-1ff77562f2f8" + "outputId": "753d62f7-6aaa-4095-8e60-83c58e3a3afc" }, "outputs": [ { @@ -74,14 +74,14 @@ }, { "cell_type": "code", - "execution_count": 3, + "execution_count": null, "metadata": { "id": "9EurAUSz_1yl", "colab": { "base_uri": "https://localhost:8080/", "height": 17 }, - "outputId": "509840ea-a8f9-42b8-f43e-031c5f90224e" + "outputId": "fe16a0c6-2398-4942-82de-00d8167dbcda" }, "outputs": [ { @@ -117,14 +117,14 @@ }, { "cell_type": "code", - "execution_count": 4, + "execution_count": null, "metadata": { "id": "Jt3UC-Sa0djY", "colab": { "base_uri": "https://localhost:8080/", "height": 139 }, - "outputId": "c778249c-c236-448b-cc91-a7d1446d6bc7" + "outputId": "1977ba2b-2db4-485a-a6e4-09cc1520293b" }, "outputs": [ { @@ -153,7 +153,7 @@ "remote: Counting objects: 100% (161/161), done.\u001b[K\n", "remote: Compressing objects: 100% (114/114), done.\u001b[K\n", "remote: Total 219 (delta 86), reused 108 (delta 44), pack-reused 58 (from 1)\u001b[K\n", - "Receiving objects: 100% (219/219), 351.40 KiB | 2.77 MiB/s, done.\n", + "Receiving objects: 100% (219/219), 351.40 KiB | 7.48 MiB/s, done.\n", "Resolving deltas: 100% (97/97), done.\n" ] } @@ -174,9 +174,9 @@ "height": 1000 }, "id": "Pf8560taKn7x", - "outputId": "4b044d64-b9e9-418a-f963-4c039d9524da" + "outputId": "b15d386d-cb63-4f00-e4cd-7e32342d6d67" }, - "execution_count": 5, + "execution_count": null, "outputs": [ { "output_type": "display_data", @@ -273,7 +273,7 @@ "Building wheels for collected packages: mellea-contribs\n", " Building editable for mellea-contribs (pyproject.toml) ... \u001b[?25l\u001b[?25hdone\n", " Created wheel for mellea-contribs: filename=mellea_contribs-0.1.2-py3-none-any.whl size=5868 sha256=f180b98b8dd3142068c245b8e44edaf166b082c48d3724df2c0825892be00eec\n", - " Stored in directory: /tmp/pip-ephem-wheel-cache-eap6g_i9/wheels/4e/73/36/57f081d1b5b5a73a20d0f5515fda442a6c206e7645b5a97df0\n", + " Stored in directory: /tmp/pip-ephem-wheel-cache-cwv1xo0h/wheels/4e/73/36/57f081d1b5b5a73a20d0f5515fda442a6c206e7645b5a97df0\n", "Successfully built mellea-contribs\n", "Installing collected packages: mellea-contribs\n", "Successfully installed mellea-contribs-0.1.2\n" @@ -296,21 +296,38 @@ "from mellea import start_session\n", "import mellea\n", "\n", + "# Start a Mellea session\n", "m = start_session()\n", "\n", + "# Define a sample set of tools\n", "TOOLS = [\n", " {\n", - " \"name\": \"calendar\",\n", - " \"description\": \"Internal service for scheduling meetings and managing events\"\n", + " \"name\": \"EnterpriseScheduler\",\n", + " \"short_description\": \"Internal tool for scheduling enterprise-wide meetings and events.\",\n", + " \"long_description\": \"\"\"EnterpriseScheduler is a proprietary tool used for scheduling meetings, events, and resource bookings.\n", + " It integrates with internal calendars, room booking systems, and employee availability databases.\"\"\"\n", " },\n", " {\n", - " \"name\": \"calculator\",\n", - " \"description\": \"Internal utility for budget and cost calculations\"\n", + " \"name\": \"BudgetOptimizer\",\n", + " \"short_description\": \"Internal utility for budget forecasting and cost optimization.\",\n", + " \"long_description\": \"\"\"BudgetOptimizer is a proprietary tool for financial planning, budget forecasting, and cost optimization.\n", + " It integrates with internal ERP systems and provides real-time financial insights.\"\"\"\n", " },\n", " {\n", - " \"name\": \"search\",\n", - " \"description\": \"Internal indexed search over company documents\"\n", + " \"name\": \"DocSearchPro\",\n", + " \"short_description\": \"Internal search engine for company documents and knowledge base.\",\n", + " \"long_description\": \"\"\"DocSearchPro is a proprietary search engine for indexing and retrieving internal documents,\n", + " knowledge base articles, and project files. It supports advanced search queries and filters.\n", + " \"\"\"\n", " },\n", + " {\n", + " \"name\": \"ResourceAllocator\",\n", + " \"short_description\": \"Internal tool for resource allocation and project management.\",\n", + " \"long_description\": \"\"\"\n", + " ResourceAllocator is a proprietary tool for managing project resources, including team allocation,\n", + " equipment booking, and project timelines. It integrates with internal HR and project management systems.\n", + " \"\"\"\n", + " }\n", "]\n", "\n", "USER_QUERY = \"Schedule a meeting for tomorrow and calculate the event budget\"" @@ -318,12 +335,12 @@ "metadata": { "colab": { "base_uri": "https://localhost:8080/", - "height": 399 + "height": 382 }, "id": "oSyRPp5bKpz7", - "outputId": "84339bfe-c432-477a-da6b-2cba6712aa03" + "outputId": "ccf11182-d3e2-4f7c-8815-e0ed00d3f641" }, - "execution_count": 6, + "execution_count": null, "outputs": [ { "output_type": "display_data", @@ -348,13 +365,13 @@ "text": [ "/usr/local/lib/python3.12/dist-packages/granite_common/granite3/granite33/output.py:184: SyntaxWarning: invalid escape sequence '\\d'\n", " f'{re.escape(CITE_START)}{{\"document_id\": \"(\\d+)\"}}{re.escape(CITE_END)}'\n", - "pulling 6c02683809a8 sha256:6c026: 100%|██████████| 2.10G/2.10G [00:11<00:00, 152MB/s]\n", + "pulling 6c02683809a8 sha256:6c026: 99%|█████████▉| 2.09G/2.10G [00:12<00:00, 224MB/s]\n", "pulling 0f6ec9740c76 sha256:0f6ec: 0%| | 0.00/7.08k [00:00" + ], + "text/html": [ + "\n", + "\n" + ] + }, + "metadata": {} + } + ] + }, + { + "cell_type": "markdown", + "source": [ + "**Final Tool selection**" + ], + "metadata": { + "id": "s9J7WdURNXeB" + } + }, + { + "cell_type": "code", + "source": [ + "from pydantic import RootModel\n", + "from mellea.stdlib.requirement import req, simple_validate\n", + "from mellea.stdlib.sampling import RejectionSamplingStrategy\n", "\n", - " return rankings\n", + "class ToolChoice(RootModel[str]):\n", + " pass\n", "\n", + "def choose_final_tool(\n", + " m: mellea.MelleaSession,\n", + " user_query: str,\n", + " ranked_tools: list[dict],\n", + "):\n", + " tool_names = [t[\"name\"] for t in ranked_tools]\n", "\n", - "final_rankings = rank_tools_with_drr(\n", - " m,\n", - " user_query=USER_QUERY,\n", - " tools=top_k_tools,\n", - ")\n", + " response = m.instruct(\n", + " f\"\"\"\n", + " Given the user query:\n", + " '{user_query}'\n", "\n", - "print(\"\\nFinal tool ranking:\")\n", - "for tool, score in final_rankings:\n", - " print(f\"{tool['name']}: {score}\")\n" + " Choose the single best tool from this list:\n", + " {tool_names}\n", + " \"\"\",\n", + " requirements=[\n", + " req(\n", + " \"Tool must be one of the provided candidates\",\n", + " validation_fn=simple_validate(lambda s: s in tool_names),\n", + " ),\n", + " req(\"Return only the tool name\"),\n", + " ],\n", + " strategy=RejectionSamplingStrategy(loop_budget=3),\n", + " format=ToolChoice,\n", + " )\n", + "\n", + " return response.value\n" ], "metadata": { + "id": "WYfUea4YMzN9", "colab": { "base_uri": "https://localhost:8080/", - "height": 225 + "height": 17 }, - "id": "Hf66hB_fMqpi", - "outputId": "327116f0-5f3c-42f2-f419-d42f77aa57d9" + "outputId": "77aa6ea8-2958-4e2d-f05b-7ebb1d2cf84d" + }, + "execution_count": null, + "outputs": [ + { + "output_type": "display_data", + "data": { + "text/plain": [ + "" + ], + "text/html": [ + "\n", + "\n" + ] + }, + "metadata": {} + } + ] + }, + { + "cell_type": "code", + "source": [ + "m = start_session()\n", + "\n", + "top_k_tools = shortlist_tools(m, USER_QUERY, TOOLS, k=2)\n", + "ranked = rank_tools(m, USER_QUERY, top_k_tools)\n", + "final_tool = choose_final_tool(m, USER_QUERY, [t for t, _ in ranked])\n", + "\n", + "print(\"Selected tool:\", final_tool)" + ], + "metadata": { + "id": "8iCOuJ66NTu8", + "colab": { + "base_uri": "https://localhost:8080/", + "height": 468 + }, + "outputId": "cd466767-09e4-460b-b1a0-00918f78a1d7" }, - "execution_count": 8, + "execution_count": null, "outputs": [ { "output_type": "display_data", @@ -563,7 +624,7 @@ "output_type": "stream", "name": "stdout", "text": [ - "\u001b[38;20m=== 22:07:11-INFO ======\n", + "\u001b[38;20m=== 23:03:53-INFO ======\n", "SUCCESS\u001b[0m\n" ] }, @@ -572,7 +633,7 @@ "name": "stderr", "text": [ "INFO:fancy_logger:SUCCESS\n", - " 0%| | 0/2 [00:32" - ], - "text/html": [ - "\n", - "\n" - ] - }, - "metadata": {} + "output_type": "stream", + "name": "stdout", + "text": [ + "\u001b[38;20m=== 23:06:20-INFO ======\n", + "FAILED. Valid: 1/2\u001b[0m\n" + ] + }, + { + "output_type": "stream", + "name": "stderr", + "text": [ + "INFO:fancy_logger:FAILED. Valid: 1/2\n", + " 33%|███▎ | 1/3 [00:38<01:17, 38.74s/it]" + ] + }, + { + "output_type": "stream", + "name": "stdout", + "text": [ + "\u001b[38;20m=== 23:06:53-INFO ======\n", + "FAILED. Valid: 1/2\u001b[0m\n" + ] + }, + { + "output_type": "stream", + "name": "stderr", + "text": [ + "INFO:fancy_logger:FAILED. Valid: 1/2\n", + " 67%|██████▋ | 2/3 [01:11<00:35, 35.43s/it]" + ] + }, + { + "output_type": "stream", + "name": "stdout", + "text": [ + "\u001b[38;20m=== 23:07:28-INFO ======\n", + "FAILED. Valid: 1/2\u001b[0m\n" + ] + }, + { + "output_type": "stream", + "name": "stderr", + "text": [ + "INFO:fancy_logger:FAILED. Valid: 1/2\n", + "100%|██████████| 3/3 [01:46<00:00, 35.36s/it]" + ] }, { "output_type": "stream", "name": "stdout", + "text": [ + "\u001b[38;20m=== 23:07:28-INFO ======\n", + "Invoking select_from_failure after 3 failed attempts.\u001b[0m\n" + ] + }, + { + "output_type": "stream", + "name": "stderr", "text": [ "\n", - "Selected tool: calendar\n" + "INFO:fancy_logger:Invoking select_from_failure after 3 failed attempts.\n" + ] + }, + { + "output_type": "stream", + "name": "stdout", + "text": [ + "Selected tool: \"BudgetOptimizer\"\n" ] } ] - }, - { - "cell_type": "code", - "source": [], - "metadata": { - "id": "8iCOuJ66NTu8" - }, - "execution_count": null, - "outputs": [] } ], "metadata": { From c5bbe84c418d8063adb3c7cb11a2b61d876aea4d Mon Sep 17 00:00:00 2001 From: Shubhi Asthana Date: Thu, 18 Dec 2025 12:42:58 -0800 Subject: [PATCH 3/7] Update tool_selection.ipynb accelerator in metadata --- docs/examples/notebooks/tool_selection.ipynb | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/docs/examples/notebooks/tool_selection.ipynb b/docs/examples/notebooks/tool_selection.ipynb index df3d10ed..45c3c0c6 100644 --- a/docs/examples/notebooks/tool_selection.ipynb +++ b/docs/examples/notebooks/tool_selection.ipynb @@ -746,6 +746,7 @@ } ], "metadata": { + "accelerator": "GPU", "colab": { "gpuType": "T4", "provenance": [] @@ -770,4 +771,4 @@ }, "nbformat": 4, "nbformat_minor": 0 -} \ No newline at end of file +} From 8050d53fa41f15bf73a77afa21c08209df5cbb1a Mon Sep 17 00:00:00 2001 From: shubhiasthana Date: Thu, 18 Dec 2025 13:00:49 -0800 Subject: [PATCH 4/7] tool_selection collab update --- docs/examples/notebooks/tool_selection.ipynb | 504 +------------------ 1 file changed, 23 insertions(+), 481 deletions(-) diff --git a/docs/examples/notebooks/tool_selection.ipynb b/docs/examples/notebooks/tool_selection.ipynb index 45c3c0c6..69e41463 100644 --- a/docs/examples/notebooks/tool_selection.ipynb +++ b/docs/examples/notebooks/tool_selection.ipynb @@ -25,29 +25,9 @@ "cell_type": "code", "execution_count": null, "metadata": { - "id": "VDaTfltQY3Fl", - "colab": { - "base_uri": "https://localhost:8080/" - }, - "outputId": "753d62f7-6aaa-4095-8e60-83c58e3a3afc" + "id": "VDaTfltQY3Fl" }, - "outputs": [ - { - "output_type": "stream", - "name": "stdout", - "text": [ - ">>> Installing ollama to /usr/local\n", - ">>> Downloading Linux amd64 bundle\n", - "######################################################################## 100.0%\n", - ">>> Creating ollama user...\n", - ">>> Adding ollama user to video group...\n", - ">>> Adding current user to ollama group...\n", - ">>> Creating ollama systemd service...\n", - ">>> The Ollama API is now available at 127.0.0.1:11434.\n", - ">>> Install complete. Run \"ollama\" from the command line.\n" - ] - } - ], + "outputs": [], "source": [ "!curl -fsSL https://ollama.com/install.sh | sh > /dev/null\n", "!nohup ollama serve >/dev/null 2>&1 &\n", @@ -76,32 +56,9 @@ "cell_type": "code", "execution_count": null, "metadata": { - "id": "9EurAUSz_1yl", - "colab": { - "base_uri": "https://localhost:8080/", - "height": 17 - }, - "outputId": "fe16a0c6-2398-4942-82de-00d8167dbcda" + "id": "9EurAUSz_1yl" }, - "outputs": [ - { - "output_type": "display_data", - "data": { - "text/plain": [ - "" - ], - "text/html": [ - "\n", - "\n" - ] - }, - "metadata": {} - } - ], + "outputs": [], "source": [ "!uv pip install mellea -q" ] @@ -119,45 +76,9 @@ "cell_type": "code", "execution_count": null, "metadata": { - "id": "Jt3UC-Sa0djY", - "colab": { - "base_uri": "https://localhost:8080/", - "height": 139 - }, - "outputId": "1977ba2b-2db4-485a-a6e4-09cc1520293b" + "id": "Jt3UC-Sa0djY" }, - "outputs": [ - { - "output_type": "display_data", - "data": { - "text/plain": [ - "" - ], - "text/html": [ - "\n", - "\n" - ] - }, - "metadata": {} - }, - { - "output_type": "stream", - "name": "stdout", - "text": [ - "Cloning into 'mellea-contribs'...\n", - "remote: Enumerating objects: 219, done.\u001b[K\n", - "remote: Counting objects: 100% (161/161), done.\u001b[K\n", - "remote: Compressing objects: 100% (114/114), done.\u001b[K\n", - "remote: Total 219 (delta 86), reused 108 (delta 44), pack-reused 58 (from 1)\u001b[K\n", - "Receiving objects: 100% (219/219), 351.40 KiB | 7.48 MiB/s, done.\n", - "Resolving deltas: 100% (97/97), done.\n" - ] - } - ], + "outputs": [], "source": [ "!git clone https://github.com/generative-computing/mellea-contribs.git" ] @@ -166,120 +87,13 @@ "cell_type": "code", "source": [ "%cd mellea-contribs\n", - "!pip install -e ." + "!pip install -e . -qq" ], "metadata": { - "colab": { - "base_uri": "https://localhost:8080/", - "height": 1000 - }, - "id": "Pf8560taKn7x", - "outputId": "b15d386d-cb63-4f00-e4cd-7e32342d6d67" + "id": "Pf8560taKn7x" }, "execution_count": null, - "outputs": [ - { - "output_type": "display_data", - "data": { - "text/plain": [ - "" - ], - "text/html": [ - "\n", - "\n" - ] - }, - "metadata": {} - }, - { - "output_type": "stream", - "name": "stdout", - "text": [ - "/content/mellea-contribs\n", - "Obtaining file:///content/mellea-contribs\n", - " Installing build dependencies ... \u001b[?25l\u001b[?25hdone\n", - " Checking if build backend supports build_editable ... \u001b[?25l\u001b[?25hdone\n", - " Getting requirements to build editable ... \u001b[?25l\u001b[?25hdone\n", - " Preparing editable metadata (pyproject.toml) ... \u001b[?25l\u001b[?25hdone\n", - "Requirement already satisfied: mellea in /usr/local/lib/python3.12/dist-packages (from mellea-contribs==0.1.2) (0.2.1)\n", - "Requirement already satisfied: pydantic in /usr/local/lib/python3.12/dist-packages (from mellea->mellea-contribs==0.1.2) (2.12.3)\n", - "Requirement already satisfied: openai in /usr/local/lib/python3.12/dist-packages (from mellea->mellea-contribs==0.1.2) (2.9.0)\n", - "Requirement already satisfied: jinja2 in /usr/local/lib/python3.12/dist-packages (from mellea->mellea-contribs==0.1.2) (3.1.6)\n", - "Requirement already satisfied: json5 in /usr/local/lib/python3.12/dist-packages (from mellea->mellea-contribs==0.1.2) (0.12.1)\n", - "Requirement already satisfied: ollama>=0.5.1 in /usr/local/lib/python3.12/dist-packages (from mellea->mellea-contribs==0.1.2) (0.6.1)\n", - "Requirement already satisfied: ansicolors in /usr/local/lib/python3.12/dist-packages (from mellea->mellea-contribs==0.1.2) (1.1.8)\n", - "Requirement already satisfied: requests>=2.32.3 in /usr/local/lib/python3.12/dist-packages (from mellea->mellea-contribs==0.1.2) (2.32.4)\n", - "Requirement already satisfied: uvicorn in /usr/local/lib/python3.12/dist-packages (from mellea->mellea-contribs==0.1.2) (0.38.0)\n", - "Requirement already satisfied: fastapi in /usr/local/lib/python3.12/dist-packages (from mellea->mellea-contribs==0.1.2) (0.118.3)\n", - "Requirement already satisfied: types-requests in /usr/local/lib/python3.12/dist-packages (from mellea->mellea-contribs==0.1.2) (2.32.4.20250913)\n", - "Requirement already satisfied: types-tqdm in /usr/local/lib/python3.12/dist-packages (from mellea->mellea-contribs==0.1.2) (4.67.0.20250809)\n", - "Requirement already satisfied: typer in /usr/local/lib/python3.12/dist-packages (from mellea->mellea-contribs==0.1.2) (0.20.0)\n", - "Requirement already satisfied: click<8.2.0 in /usr/local/lib/python3.12/dist-packages (from mellea->mellea-contribs==0.1.2) (8.1.8)\n", - "Requirement already satisfied: mistletoe>=1.4.0 in /usr/local/lib/python3.12/dist-packages (from mellea->mellea-contribs==0.1.2) (1.5.1)\n", - "Requirement already satisfied: huggingface-hub>=0.33.4 in /usr/local/lib/python3.12/dist-packages (from mellea->mellea-contribs==0.1.2) (0.36.0)\n", - "Requirement already satisfied: pillow in /usr/local/lib/python3.12/dist-packages (from mellea->mellea-contribs==0.1.2) (11.3.0)\n", - "Requirement already satisfied: granite-common>=0.3.5 in /usr/local/lib/python3.12/dist-packages (from mellea->mellea-contribs==0.1.2) (0.3.6)\n", - "Requirement already satisfied: math_verify in /usr/local/lib/python3.12/dist-packages (from mellea->mellea-contribs==0.1.2) (0.8.0)\n", - "Requirement already satisfied: rouge_score in /usr/local/lib/python3.12/dist-packages (from mellea->mellea-contribs==0.1.2) (0.1.2)\n", - "Requirement already satisfied: llm-sandbox>=0.3.23 in /usr/local/lib/python3.12/dist-packages (from llm-sandbox[docker]>=0.3.23->mellea->mellea-contribs==0.1.2) (0.3.31)\n", - "Requirement already satisfied: jsonschema in /usr/local/lib/python3.12/dist-packages (from granite-common>=0.3.5->mellea->mellea-contribs==0.1.2) (4.25.1)\n", - "Requirement already satisfied: filelock in /usr/local/lib/python3.12/dist-packages (from huggingface-hub>=0.33.4->mellea->mellea-contribs==0.1.2) (3.20.0)\n", - "Requirement already satisfied: fsspec>=2023.5.0 in /usr/local/lib/python3.12/dist-packages (from huggingface-hub>=0.33.4->mellea->mellea-contribs==0.1.2) (2025.3.0)\n", - "Requirement already satisfied: packaging>=20.9 in /usr/local/lib/python3.12/dist-packages (from huggingface-hub>=0.33.4->mellea->mellea-contribs==0.1.2) (25.0)\n", - "Requirement already satisfied: pyyaml>=5.1 in /usr/local/lib/python3.12/dist-packages (from huggingface-hub>=0.33.4->mellea->mellea-contribs==0.1.2) (6.0.3)\n", - "Requirement already satisfied: tqdm>=4.42.1 in /usr/local/lib/python3.12/dist-packages (from huggingface-hub>=0.33.4->mellea->mellea-contribs==0.1.2) (4.67.1)\n", - "Requirement already satisfied: typing-extensions>=3.7.4.3 in /usr/local/lib/python3.12/dist-packages (from huggingface-hub>=0.33.4->mellea->mellea-contribs==0.1.2) (4.15.0)\n", - "Requirement already satisfied: hf-xet<2.0.0,>=1.1.3 in /usr/local/lib/python3.12/dist-packages (from huggingface-hub>=0.33.4->mellea->mellea-contribs==0.1.2) (1.2.0)\n", - "Requirement already satisfied: docker>=7.1.0 in /usr/local/lib/python3.12/dist-packages (from llm-sandbox[docker]>=0.3.23->mellea->mellea-contribs==0.1.2) (7.1.0)\n", - "Requirement already satisfied: httpx>=0.27 in /usr/local/lib/python3.12/dist-packages (from ollama>=0.5.1->mellea->mellea-contribs==0.1.2) (0.28.1)\n", - "Requirement already satisfied: annotated-types>=0.6.0 in /usr/local/lib/python3.12/dist-packages (from pydantic->mellea->mellea-contribs==0.1.2) (0.7.0)\n", - "Requirement already satisfied: pydantic-core==2.41.4 in /usr/local/lib/python3.12/dist-packages (from pydantic->mellea->mellea-contribs==0.1.2) (2.41.4)\n", - "Requirement already satisfied: typing-inspection>=0.4.2 in /usr/local/lib/python3.12/dist-packages (from pydantic->mellea->mellea-contribs==0.1.2) (0.4.2)\n", - "Requirement already satisfied: charset_normalizer<4,>=2 in /usr/local/lib/python3.12/dist-packages (from requests>=2.32.3->mellea->mellea-contribs==0.1.2) (3.4.4)\n", - "Requirement already satisfied: idna<4,>=2.5 in /usr/local/lib/python3.12/dist-packages (from requests>=2.32.3->mellea->mellea-contribs==0.1.2) (3.11)\n", - "Requirement already satisfied: urllib3<3,>=1.21.1 in /usr/local/lib/python3.12/dist-packages (from requests>=2.32.3->mellea->mellea-contribs==0.1.2) (2.5.0)\n", - "Requirement already satisfied: certifi>=2017.4.17 in /usr/local/lib/python3.12/dist-packages (from requests>=2.32.3->mellea->mellea-contribs==0.1.2) (2025.11.12)\n", - "Requirement already satisfied: starlette<0.49.0,>=0.40.0 in /usr/local/lib/python3.12/dist-packages (from fastapi->mellea->mellea-contribs==0.1.2) (0.48.0)\n", - "Requirement already satisfied: MarkupSafe>=2.0 in /usr/local/lib/python3.12/dist-packages (from jinja2->mellea->mellea-contribs==0.1.2) (3.0.3)\n", - "Requirement already satisfied: latex2sympy2_extended==1.10.2 in /usr/local/lib/python3.12/dist-packages (from math_verify->mellea->mellea-contribs==0.1.2) (1.10.2)\n", - "Requirement already satisfied: sympy in /usr/local/lib/python3.12/dist-packages (from latex2sympy2_extended==1.10.2->math_verify->mellea->mellea-contribs==0.1.2) (1.14.0)\n", - "Requirement already satisfied: antlr4-python3-runtime<=4.13.2,>=4.9.3 in /usr/local/lib/python3.12/dist-packages (from latex2sympy2_extended==1.10.2->math_verify->mellea->mellea-contribs==0.1.2) (4.9.3)\n", - "Requirement already satisfied: anyio<5,>=3.5.0 in /usr/local/lib/python3.12/dist-packages (from openai->mellea->mellea-contribs==0.1.2) (4.12.0)\n", - "Requirement already satisfied: distro<2,>=1.7.0 in /usr/local/lib/python3.12/dist-packages (from openai->mellea->mellea-contribs==0.1.2) (1.9.0)\n", - "Requirement already satisfied: jiter<1,>=0.10.0 in /usr/local/lib/python3.12/dist-packages (from openai->mellea->mellea-contribs==0.1.2) (0.12.0)\n", - "Requirement already satisfied: sniffio in /usr/local/lib/python3.12/dist-packages (from openai->mellea->mellea-contribs==0.1.2) (1.3.1)\n", - "Requirement already satisfied: absl-py in /usr/local/lib/python3.12/dist-packages (from rouge_score->mellea->mellea-contribs==0.1.2) (1.4.0)\n", - "Requirement already satisfied: nltk in /usr/local/lib/python3.12/dist-packages (from rouge_score->mellea->mellea-contribs==0.1.2) (3.9.1)\n", - "Requirement already satisfied: numpy in /usr/local/lib/python3.12/dist-packages (from rouge_score->mellea->mellea-contribs==0.1.2) (2.0.2)\n", - "Requirement already satisfied: six>=1.14.0 in /usr/local/lib/python3.12/dist-packages (from rouge_score->mellea->mellea-contribs==0.1.2) (1.17.0)\n", - "Requirement already satisfied: shellingham>=1.3.0 in /usr/local/lib/python3.12/dist-packages (from typer->mellea->mellea-contribs==0.1.2) (1.5.4)\n", - "Requirement already satisfied: rich>=10.11.0 in /usr/local/lib/python3.12/dist-packages (from typer->mellea->mellea-contribs==0.1.2) (13.9.4)\n", - "Requirement already satisfied: h11>=0.8 in /usr/local/lib/python3.12/dist-packages (from uvicorn->mellea->mellea-contribs==0.1.2) (0.16.0)\n", - "Requirement already satisfied: httpcore==1.* in /usr/local/lib/python3.12/dist-packages (from httpx>=0.27->ollama>=0.5.1->mellea->mellea-contribs==0.1.2) (1.0.9)\n", - "Requirement already satisfied: markdown-it-py>=2.2.0 in /usr/local/lib/python3.12/dist-packages (from rich>=10.11.0->typer->mellea->mellea-contribs==0.1.2) (4.0.0)\n", - "Requirement already satisfied: pygments<3.0.0,>=2.13.0 in /usr/local/lib/python3.12/dist-packages (from rich>=10.11.0->typer->mellea->mellea-contribs==0.1.2) (2.19.2)\n", - "Requirement already satisfied: attrs>=22.2.0 in /usr/local/lib/python3.12/dist-packages (from jsonschema->granite-common>=0.3.5->mellea->mellea-contribs==0.1.2) (25.4.0)\n", - "Requirement already satisfied: jsonschema-specifications>=2023.03.6 in /usr/local/lib/python3.12/dist-packages (from jsonschema->granite-common>=0.3.5->mellea->mellea-contribs==0.1.2) (2025.9.1)\n", - "Requirement already satisfied: referencing>=0.28.4 in /usr/local/lib/python3.12/dist-packages (from jsonschema->granite-common>=0.3.5->mellea->mellea-contribs==0.1.2) (0.37.0)\n", - "Requirement already satisfied: rpds-py>=0.7.1 in /usr/local/lib/python3.12/dist-packages (from jsonschema->granite-common>=0.3.5->mellea->mellea-contribs==0.1.2) (0.30.0)\n", - "Requirement already satisfied: joblib in /usr/local/lib/python3.12/dist-packages (from nltk->rouge_score->mellea->mellea-contribs==0.1.2) (1.5.2)\n", - "Requirement already satisfied: regex>=2021.8.3 in /usr/local/lib/python3.12/dist-packages (from nltk->rouge_score->mellea->mellea-contribs==0.1.2) (2025.11.3)\n", - "Requirement already satisfied: mdurl~=0.1 in /usr/local/lib/python3.12/dist-packages (from markdown-it-py>=2.2.0->rich>=10.11.0->typer->mellea->mellea-contribs==0.1.2) (0.1.2)\n", - "Requirement already satisfied: mpmath<1.4,>=1.1.0 in /usr/local/lib/python3.12/dist-packages (from sympy->latex2sympy2_extended==1.10.2->math_verify->mellea->mellea-contribs==0.1.2) (1.3.0)\n", - "Building wheels for collected packages: mellea-contribs\n", - " Building editable for mellea-contribs (pyproject.toml) ... \u001b[?25l\u001b[?25hdone\n", - " Created wheel for mellea-contribs: filename=mellea_contribs-0.1.2-py3-none-any.whl size=5868 sha256=f180b98b8dd3142068c245b8e44edaf166b082c48d3724df2c0825892be00eec\n", - " Stored in directory: /tmp/pip-ephem-wheel-cache-cwv1xo0h/wheels/4e/73/36/57f081d1b5b5a73a20d0f5515fda442a6c206e7645b5a97df0\n", - "Successfully built mellea-contribs\n", - "Installing collected packages: mellea-contribs\n", - "Successfully installed mellea-contribs-0.1.2\n" - ] - } - ] + "outputs": [] }, { "cell_type": "markdown", @@ -333,65 +147,15 @@ "USER_QUERY = \"Schedule a meeting for tomorrow and calculate the event budget\"" ], "metadata": { - "colab": { - "base_uri": "https://localhost:8080/", - "height": 382 - }, - "id": "oSyRPp5bKpz7", - "outputId": "ccf11182-d3e2-4f7c-8815-e0ed00d3f641" + "id": "oSyRPp5bKpz7" }, "execution_count": null, - "outputs": [ - { - "output_type": "display_data", - "data": { - "text/plain": [ - "" - ], - "text/html": [ - "\n", - "\n" - ] - }, - "metadata": {} - }, - { - "output_type": "stream", - "name": "stderr", - "text": [ - "/usr/local/lib/python3.12/dist-packages/granite_common/granite3/granite33/output.py:184: SyntaxWarning: invalid escape sequence '\\d'\n", - " f'{re.escape(CITE_START)}{{\"document_id\": \"(\\d+)\"}}{re.escape(CITE_END)}'\n", - "pulling 6c02683809a8 sha256:6c026: 99%|█████████▉| 2.09G/2.10G [00:12<00:00, 224MB/s]\n", - "pulling 0f6ec9740c76 sha256:0f6ec: 0%| | 0.00/7.08k [00:00" - ], - "text/html": [ - "\n", - "\n" - ] - }, - "metadata": {} - } - ] + "outputs": [] }, { "cell_type": "markdown", "source": [ - "**Step 2: High-Confidence Ranking with Double Round Robin**" + "## **Step 2: High-Confidence Ranking with Double Round Robin**" ], "metadata": { "id": "NM3w5OXEMt3p" @@ -471,38 +212,15 @@ " )\n" ], "metadata": { - "id": "Hf66hB_fMqpi", - "colab": { - "base_uri": "https://localhost:8080/", - "height": 17 - }, - "outputId": "568beeca-276b-45ff-9644-72170fd13782" + "id": "Hf66hB_fMqpi" }, "execution_count": null, - "outputs": [ - { - "output_type": "display_data", - "data": { - "text/plain": [ - "" - ], - "text/html": [ - "\n", - "\n" - ] - }, - "metadata": {} - } - ] + "outputs": [] }, { "cell_type": "markdown", "source": [ - "**Final Tool selection**" + "## **Final Tool selection**" ], "metadata": { "id": "s9J7WdURNXeB" @@ -547,33 +265,10 @@ " return response.value\n" ], "metadata": { - "id": "WYfUea4YMzN9", - "colab": { - "base_uri": "https://localhost:8080/", - "height": 17 - }, - "outputId": "77aa6ea8-2958-4e2d-f05b-7ebb1d2cf84d" + "id": "WYfUea4YMzN9" }, "execution_count": null, - "outputs": [ - { - "output_type": "display_data", - "data": { - "text/plain": [ - "" - ], - "text/html": [ - "\n", - "\n" - ] - }, - "metadata": {} - } - ] + "outputs": [] }, { "cell_type": "code", @@ -587,166 +282,13 @@ "print(\"Selected tool:\", final_tool)" ], "metadata": { - "id": "8iCOuJ66NTu8", - "colab": { - "base_uri": "https://localhost:8080/", - "height": 468 - }, - "outputId": "cd466767-09e4-460b-b1a0-00918f78a1d7" + "id": "8iCOuJ66NTu8" }, "execution_count": null, - "outputs": [ - { - "output_type": "display_data", - "data": { - "text/plain": [ - "" - ], - "text/html": [ - "\n", - "\n" - ] - }, - "metadata": {} - }, - { - "output_type": "stream", - "name": "stderr", - "text": [ - "\r 0%| | 0/2 [00:00 Date: Thu, 18 Dec 2025 13:01:34 -0800 Subject: [PATCH 5/7] Update tool_selection.ipynb --- docs/examples/notebooks/tool_selection.ipynb | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/docs/examples/notebooks/tool_selection.ipynb b/docs/examples/notebooks/tool_selection.ipynb index 69e41463..0eb4268b 100644 --- a/docs/examples/notebooks/tool_selection.ipynb +++ b/docs/examples/notebooks/tool_selection.ipynb @@ -289,6 +289,7 @@ } ], "metadata": { + "accelerator": "GPU", "colab": { "gpuType": "T4", "provenance": [] @@ -313,4 +314,4 @@ }, "nbformat": 4, "nbformat_minor": 0 -} \ No newline at end of file +} From 696c5e926c121c6db97ec3a7e09f0aae1d2c2e49 Mon Sep 17 00:00:00 2001 From: shubhiasthana Date: Fri, 19 Dec 2025 09:26:53 -0800 Subject: [PATCH 6/7] tool_selection requirements and longer description --- docs/examples/notebooks/tool_selection.ipynb | 62 ++++++++++++++------ 1 file changed, 45 insertions(+), 17 deletions(-) diff --git a/docs/examples/notebooks/tool_selection.ipynb b/docs/examples/notebooks/tool_selection.ipynb index 0eb4268b..0cf6993a 100644 --- a/docs/examples/notebooks/tool_selection.ipynb +++ b/docs/examples/notebooks/tool_selection.ipynb @@ -117,34 +117,62 @@ "TOOLS = [\n", " {\n", " \"name\": \"EnterpriseScheduler\",\n", - " \"short_description\": \"Internal tool for scheduling enterprise-wide meetings and events.\",\n", - " \"long_description\": \"\"\"EnterpriseScheduler is a proprietary tool used for scheduling meetings, events, and resource bookings.\n", - " It integrates with internal calendars, room booking systems, and employee availability databases.\"\"\"\n", + " \"short_description\": \"Schedules internal meetings and events.\",\n", + " \"long_description\": (\n", + " \"EnterpriseScheduler is an internal tool used to schedule meetings and events across teams. \"\n", + " \"It integrates with company calendars, room booking systems, and employee availability data \"\n", + " \"to resolve conflicts and enforce scheduling policies. The tool focuses strictly on time \"\n", + " \"and resource coordination and does not perform financial analysis or budgeting.\"\n", + " ),\n", + " \"requirements\": {\n", + " \"accepts\": [\"date\", \"participants\", \"location\"],\n", + " \"does_not_support\": [\"budget_calculation\", \"cost_estimation\"]\n", + " }\n", " },\n", " {\n", " \"name\": \"BudgetOptimizer\",\n", - " \"short_description\": \"Internal utility for budget forecasting and cost optimization.\",\n", - " \"long_description\": \"\"\"BudgetOptimizer is a proprietary tool for financial planning, budget forecasting, and cost optimization.\n", - " It integrates with internal ERP systems and provides real-time financial insights.\"\"\"\n", + " \"short_description\": \"Performs internal budget forecasting and cost analysis.\",\n", + " \"long_description\": (\n", + " \"BudgetOptimizer is a proprietary financial planning tool used for estimating costs, \"\n", + " \"forecasting budgets, and optimizing spend for internal initiatives. It integrates with \"\n", + " \"enterprise ERP systems and historical financial data to produce projections and summaries. \"\n", + " \"The tool does not schedule meetings or manage calendars.\"\n", + " ),\n", + " \"requirements\": {\n", + " \"accepts\": [\"cost_inputs\", \"financial_constraints\"],\n", + " \"does_not_support\": [\"meeting_scheduling\", \"calendar_management\"]\n", + " }\n", " },\n", " {\n", " \"name\": \"DocSearchPro\",\n", - " \"short_description\": \"Internal search engine for company documents and knowledge base.\",\n", - " \"long_description\": \"\"\"DocSearchPro is a proprietary search engine for indexing and retrieving internal documents,\n", - " knowledge base articles, and project files. It supports advanced search queries and filters.\n", - " \"\"\"\n", + " \"short_description\": \"Searches internal documents and knowledge bases.\",\n", + " \"long_description\": (\n", + " \"DocSearchPro provides semantic search over internal documents, policies, and project files. \"\n", + " \"It is designed to help employees retrieve institutional knowledge and past decisions quickly. \"\n", + " \"The tool does not execute actions, perform calculations, or modify schedules.\"\n", + " ),\n", + " \"requirements\": {\n", + " \"accepts\": [\"search_query\", \"filters\"],\n", + " \"does_not_support\": [\"scheduling\", \"budgeting\", \"resource_allocation\"]\n", + " }\n", " },\n", " {\n", " \"name\": \"ResourceAllocator\",\n", - " \"short_description\": \"Internal tool for resource allocation and project management.\",\n", - " \"long_description\": \"\"\"\n", - " ResourceAllocator is a proprietary tool for managing project resources, including team allocation,\n", - " equipment booking, and project timelines. It integrates with internal HR and project management systems.\n", - " \"\"\"\n", + " \"short_description\": \"Allocates people and resources to projects.\",\n", + " \"long_description\": (\n", + " \"ResourceAllocator manages internal project resources by assigning people, equipment, and \"\n", + " \"timelines based on availability and priority constraints. It integrates with HR and project \"\n", + " \"management systems to track utilization and resolve conflicts. The tool does not handle \"\n", + " \"meeting scheduling or financial budgeting.\"\n", + " ),\n", + " \"requirements\": {\n", + " \"accepts\": [\"project_id\", \"resource_constraints\"],\n", + " \"does_not_support\": [\"meeting_scheduling\", \"budget_calculation\"]\n", + " }\n", " }\n", "]\n", "\n", - "USER_QUERY = \"Schedule a meeting for tomorrow and calculate the event budget\"" + "USER_QUERY = (\"Plan a team kickoff for next week by scheduling a meeting and estimating the expected cost based on headcount and duration.\")" ], "metadata": { "id": "oSyRPp5bKpz7" @@ -314,4 +342,4 @@ }, "nbformat": 4, "nbformat_minor": 0 -} +} \ No newline at end of file From 58c0b21ba4dfc86766778ac7de5d43282f87580a Mon Sep 17 00:00:00 2001 From: shubhiasthana Date: Fri, 19 Dec 2025 09:53:46 -0800 Subject: [PATCH 7/7] tool_selection requirements and longer description, with requirements validation --- docs/examples/notebooks/tool_selection.ipynb | 63 +++++++++++++++----- 1 file changed, 49 insertions(+), 14 deletions(-) diff --git a/docs/examples/notebooks/tool_selection.ipynb b/docs/examples/notebooks/tool_selection.ipynb index 0cf6993a..8eacb25e 100644 --- a/docs/examples/notebooks/tool_selection.ipynb +++ b/docs/examples/notebooks/tool_selection.ipynb @@ -198,15 +198,23 @@ "from mellea.stdlib.sampling import RejectionSamplingStrategy\n", "\n", "def shortlist_tools(m, user_query, tools, k):\n", - " return top_k(\n", - " items=tools,\n", + " items = [\n", + " {\"name\": t[\"name\"], \"description\": t[\"short_description\"]}\n", + " for t in tools\n", + " ]\n", + "\n", + " ranked = top_k(\n", + " items=items,\n", " comparison_prompt=f\"\"\"\n", " Select the tools most relevant to:\n", " '{user_query}'\n", " \"\"\",\n", " m=m,\n", " k=k,\n", - " )" + " )\n", + "\n", + " ranked_names = {t[\"name\"] for t in ranked}\n", + " return [t for t in tools if t[\"name\"] in ranked_names]" ], "metadata": { "id": "gtITUkKlLsM0" @@ -229,12 +237,23 @@ "from mellea_contribs.tools.double_round_robin import double_round_robin\n", "\n", "def rank_tools(m, user_query, tools):\n", + " items = [\n", + " {\n", + " \"name\": t[\"name\"],\n", + " \"description\": t[\"long_description\"],\n", + " \"constraints\": t[\"requirements\"],\n", + " }\n", + " for t in tools\n", + " ]\n", + "\n", " return double_round_robin(\n", - " items=tools,\n", + " items=items,\n", " comparison_prompt=f\"\"\"\n", " Given the user query:\n", " '{user_query}'\n", - " select which tool is more useful.\n", + "\n", + " Compare tools strictly based on their described capabilities\n", + " and stated constraints. Do not assume unsupported functionality.\n", " \"\"\",\n", " m=m,\n", " )\n" @@ -257,10 +276,6 @@ { "cell_type": "code", "source": [ - "from pydantic import RootModel\n", - "from mellea.stdlib.requirement import req, simple_validate\n", - "from mellea.stdlib.sampling import RejectionSamplingStrategy\n", - "\n", "class ToolChoice(RootModel[str]):\n", " pass\n", "\n", @@ -271,14 +286,26 @@ "):\n", " tool_names = [t[\"name\"] for t in ranked_tools]\n", "\n", + " tool_context = \"\\n\\n\".join(\n", + " f\"\"\"\n", + " Tool: {t['name']}\n", + " Description: {t['long_description']}\n", + " Requirements: {t['requirements']}\n", + " \"\"\"\n", + " for t in ranked_tools\n", + " )\n", + "\n", " response = m.instruct(\n", " f\"\"\"\n", " Given the user query:\n", " '{user_query}'\n", "\n", - " Choose the single best tool from this list:\n", - " {tool_names}\n", + " Choose the single best tool.\n", + " The tool must satisfy the user's intent and must not violate its stated requirements.\n", " \"\"\",\n", + " grounding_context={\n", + " \"tools\": tool_context\n", + " },\n", " requirements=[\n", " req(\n", " \"Tool must be one of the provided candidates\",\n", @@ -305,9 +332,18 @@ "\n", "top_k_tools = shortlist_tools(m, USER_QUERY, TOOLS, k=2)\n", "ranked = rank_tools(m, USER_QUERY, top_k_tools)\n", - "final_tool = choose_final_tool(m, USER_QUERY, [t for t, _ in ranked])\n", "\n", - "print(\"Selected tool:\", final_tool)" + "ranked_tools = []\n", + "tool_by_name = {t[\"name\"]: t for t in TOOLS}\n", + "\n", + "for item, score in ranked:\n", + " ranked_tools.append(tool_by_name[item[\"name\"]])\n", + "\n", + "final_tool = choose_final_tool(m, USER_QUERY, ranked_tools)\n", + "\n", + "print(\"Top-K shortlisted tools:\", [t[\"name\"] for t in top_k_tools])\n", + "print(\"DRR ranking:\", [(t[\"name\"], score) for t, score in ranked])\n", + "print(\"Final selected tool:\", final_tool)" ], "metadata": { "id": "8iCOuJ66NTu8" @@ -317,7 +353,6 @@ } ], "metadata": { - "accelerator": "GPU", "colab": { "gpuType": "T4", "provenance": []