diff --git a/notebooks/how_to/qualitative_text/qualitative_text_generation.ipynb b/notebooks/how_to/qualitative_text/qualitative_text_generation.ipynb new file mode 100644 index 000000000..d85879f94 --- /dev/null +++ b/notebooks/how_to/qualitative_text/qualitative_text_generation.ipynb @@ -0,0 +1,951 @@ +{ + "cells": [ + { + "cell_type": "markdown", + "id": "9a900020", + "metadata": {}, + "source": [ + "# Generate qualitative text with the ValidMind library\n", + "\n", + "This notebook shows how to generate qualitative documentation content directly from the ValidMind library using both `vm.run_text_generation()` and `vm.generate_documentation_text()`. Instead of switching to the UI to write text manually or trigger generation one section at a time, you can generate content for documentation text blocks programmatically from within a notebook and log it back to the corresponding sections of the model document.\n", + "\n", + "After building an example model and documenting its quantitative results, we’ll show how to generate text for individual content blocks, customize the output with prompts, control the context used for generation, and use a configuration-driven workflow to populate multiple qualitative sections across the document. By the end, you’ll have an end-to-end example of how quantitative test results and AI-generated qualitative content can work together to populate a full model document from Python, giving you a more automated documentation workflow directly in the library." + ] + }, + { + "cell_type": "markdown", + "id": "cd48db57", + "metadata": {}, + "source": [ + "::: {.content-hidden when-format=\"html\"}\n", + "## Contents \n", + "- [About ValidMind](#toc1__) \n", + " - [Before you begin](#toc1_1__) \n", + " - [New to ValidMind?](#toc1_2__) \n", + " - [Key concepts](#toc1_3__) \n", + "- [Setting up](#toc2__) \n", + " - [Install the ValidMind Library](#toc2_1__) \n", + " - [Initialize the ValidMind Library](#toc2_2__) \n", + " - [Register sample model](#toc2_2_1__) \n", + " - [Apply documentation template](#toc2_2_2__) \n", + " - [Get your code snippet](#toc2_2_3__) \n", + " - [Initialize the Python environment](#toc2_3__) \n", + "- [Getting to know ValidMind](#toc3__) \n", + " - [Preview the documentation template](#toc3_1__) \n", + " - [View model documentation in the ValidMind Platform](#toc3_2__) \n", + "- [Build the example model](#toc4__) \n", + " - [Import the sample dataset](#toc4_1__) \n", + " - [Preprocessing the raw dataset](#toc4_2__) \n", + " - [Training an XGBoost classifier model](#toc4_3__) \n", + "- [Initialize the ValidMind inputs](#toc5__) \n", + "- [Document test results](#toc6__) \n", + "- [Document qualitative sections](#toc7__) \n", + " - [Generate text for a single content block](#toc7_1__) \n", + " - [Customize the prompt](#toc7_2__) \n", + " - [Pass section-specific context](#toc7_3__) \n", + " - [Append a new text block to a section](#toc7_4__) \n", + " - [Generate text across the document](#toc7_5__) \n", + "- [In summary](#toc8__) \n", + "- [Next steps](#toc9__) \n", + " - [Work with your model documentation](#toc9_1__) \n", + " - [Discover more learning resources](#toc9_2__) \n", + "- [Upgrade ValidMind](#toc10__) \n", + "\n", + ":::\n", + "\n", + "" + ] + }, + { + "cell_type": "markdown", + "id": "a67217b3", + "metadata": {}, + "source": [ + "\n", + "\n", + "## About ValidMind\n", + "\n", + "ValidMind is a suite of tools for managing model risk, including risk associated with AI and statistical models. \n", + "\n", + "You use the ValidMind Library to automate documentation and validation tests, and then use the ValidMind Platform to collaborate on model documentation. Together, these products simplify model risk management, facilitate compliance with regulations and institutional standards, and enhance collaboration between yourself and model validators." + ] + }, + { + "cell_type": "markdown", + "id": "281cfb86", + "metadata": {}, + "source": [ + "\n", + "\n", + "### Before you begin\n", + "\n", + "This notebook assumes you have basic familiarity with Python, including an understanding of how functions work. If you are new to Python, you can still run the notebook but we recommend further familiarizing yourself with the language. \n", + "\n", + "If you encounter errors due to missing modules in your Python environment, install the modules with `pip install`, and then re-run the notebook. For more help, refer to [Installing Python Modules](https://docs.python.org/3/installing/index.html)." + ] + }, + { + "cell_type": "markdown", + "id": "51c11b52", + "metadata": {}, + "source": [ + "\n", + "\n", + "### New to ValidMind?\n", + "\n", + "If you haven't already seen our documentation on the [ValidMind Library](https://docs.validmind.ai/developer/validmind-library.html), we recommend you begin by exploring the available resources in this section. There, you can learn more about documenting models and running tests, as well as find code samples and our Python Library API reference.\n", + "\n", + "
For access to all features available in this notebook, you'll need access to a ValidMind account.\n", + "

\n", + "Register with ValidMind
" + ] + }, + { + "cell_type": "markdown", + "id": "9103cd45", + "metadata": {}, + "source": [ + "\n", + "\n", + "### Key concepts\n", + "\n", + "**Validation report**: A comprehensive and structured assessment of a model’s development and performance, focusing on verifying its integrity, appropriateness, and alignment with its intended use. It includes analyses of model assumptions, data quality, performance metrics, outcomes of testing procedures, and risk considerations. The validation report supports transparency, regulatory compliance, and informed decision-making by documenting the validator’s independent review and conclusions.\n", + "\n", + "**Validation report template**: Serves as a standardized framework for conducting and documenting model validation activities. It outlines the required sections, recommended analyses, and expected validation tests, ensuring consistency and completeness across validation reports. The template helps guide validators through a systematic review process while promoting comparability and traceability of validation outcomes.\n", + "\n", + "**Tests**: A function contained in the ValidMind Library, designed to run a specific quantitative test on the dataset or model. Tests are the building blocks of ValidMind, used to evaluate and document models and datasets.\n", + "\n", + "**Metrics**: A subset of tests that do not have thresholds. In the context of this notebook, metrics and tests can be thought of as interchangeable concepts.\n", + "\n", + "**Custom metrics**: Custom metrics are functions that you define to evaluate your model or dataset. These functions can be registered with the ValidMind Library to be used in the ValidMind Platform.\n", + "\n", + "**Inputs**: Objects to be evaluated and documented in the ValidMind Library. They can be any of the following:\n", + "\n", + " - **model**: A single model that has been initialized in ValidMind with [`vm.init_model()`](https://docs.validmind.ai/validmind/validmind.html#init_model).\n", + " - **dataset**: Single dataset that has been initialized in ValidMind with [`vm.init_dataset()`](https://docs.validmind.ai/validmind/validmind.html#init_dataset).\n", + " - **models**: A list of ValidMind models - usually this is used when you want to compare multiple models in your custom metric.\n", + " - **datasets**: A list of ValidMind datasets - usually this is used when you want to compare multiple datasets in your custom metric. (Learn more: [Run tests with multiple datasets](https://docs.validmind.ai/notebooks/how_to/tests/run_tests/configure_tests/run_tests_that_require_multiple_datasets.html))\n", + "\n", + "**Parameters**: Additional arguments that can be passed when running a ValidMind test, used to pass additional information to a metric, customize its behavior, or provide additional context.\n", + "\n", + "**Outputs**: Custom metrics can return elements like tables or plots. Tables may be a list of dictionaries (each representing a row) or a pandas DataFrame. Plots may be matplotlib or plotly figures." + ] + }, + { + "cell_type": "markdown", + "id": "23020a1b", + "metadata": {}, + "source": [ + "\n", + "\n", + "## Setting up" + ] + }, + { + "cell_type": "markdown", + "id": "6202d6dc", + "metadata": {}, + "source": [ + "\n", + "\n", + "### Install the ValidMind Library\n", + "\n", + "
Recommended Python versions\n", + "

\n", + "Python 3.8 <= x <= 3.14
\n", + "\n", + "To install the library:" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "045b05a6", + "metadata": {}, + "outputs": [], + "source": [ + "%pip install -q validmind" + ] + }, + { + "cell_type": "markdown", + "id": "b3231d8e", + "metadata": {}, + "source": [ + "\n", + "\n", + "### Initialize the ValidMind Library" + ] + }, + { + "cell_type": "markdown", + "id": "56592217", + "metadata": {}, + "source": [ + "\n", + "\n", + "#### Register sample model\n", + "\n", + "Let's first register a sample model for use with this notebook:\n", + "\n", + "1. In a browser, [log in to ValidMind](https://docs.validmind.ai/guide/configuration/log-in-to-validmind.html).\n", + "\n", + "2. In the left sidebar, navigate to **Inventory** and click **+ Register Model**.\n", + "\n", + "3. Enter the model details and click **Next >** to continue to assignment of model stakeholders. ([Need more help?](https://docs.validmind.ai/guide/model-inventory/register-models-in-inventory.html))\n", + "\n", + "4. Select your own name under the **MODEL OWNER** drop-down.\n", + "\n", + "5. Click **Register Model** to add the model to your inventory." + ] + }, + { + "cell_type": "markdown", + "id": "43ed3d0c", + "metadata": {}, + "source": [ + "\n", + "\n", + "#### Apply documentation template\n", + "\n", + "Once you've registered your model, let's select a documentation template. A template predefines sections for your model documentation and provides a general outline to follow, making the documentation process much easier.\n", + "\n", + "1. In the left sidebar that appears for your model, click **Documents** and select **Development**.\n", + "\n", + "2. Under **TEMPLATE**, select `Binary classification`.\n", + "\n", + "3. Click **Use Template** to apply the template." + ] + }, + { + "cell_type": "markdown", + "id": "9b9203be", + "metadata": {}, + "source": [ + "\n", + "\n", + "#### Get your code snippet\n", + "\n", + "Initialize the ValidMind Library with the *code snippet* unique to each model per document, ensuring your test results are uploaded to the correct model and automatically populated in the right document in the ValidMind Platform when you run this notebook.\n", + "\n", + "1. On the left sidebar that appears for your model, select **Getting Started** and select `Development` from the **DOCUMENT** drop-down menu.\n", + "2. Click **Copy snippet to clipboard**.\n", + "3. Next, [load your model identifier credentials from an `.env` file](https://docs.validmind.ai/developer/model-documentation/store-credentials-in-env-file.html) or replace the placeholder with your own code snippet:" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "690dc368", + "metadata": {}, + "outputs": [], + "source": [ + "# Load your model identifier credentials from an `.env` file\n", + "\n", + "%load_ext dotenv\n", + "%dotenv .env\n", + "\n", + "# Or replace with your code snippet\n", + "\n", + "import validmind as vm\n", + "\n", + "vm.init(\n", + " api_host=\"http://localhost:5000/api/v1/tracking\",\n", + " api_key=\"..\",\n", + " api_secret=\"..\",\n", + " document=\"documentation\", # requires library >=2.12.0\n", + " model=\"..\",\n", + ")" + ] + }, + { + "cell_type": "markdown", + "id": "a68f6031", + "metadata": {}, + "source": [ + "\n", + "\n", + "### Initialize the Python environment\n", + "\n", + "Then, let's import the necessary libraries and set up your Python environment for data analysis:\n", + "\n", + "- Import **Extreme Gradient Boosting** (XGBoost) with an alias so that we can reference its functions in later calls. XGBoost is a powerful machine learning library designed for speed and performance, especially in handling structured or tabular data.\n", + "- Enable **`matplotlib`**, a plotting library used for visualizing data. Ensures that any plots you generate will render inline in our notebook output rather than opening in a separate window." + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "3fa2d9de", + "metadata": {}, + "outputs": [], + "source": [ + "%matplotlib inline\n", + "\n", + "import xgboost as xgb" + ] + }, + { + "cell_type": "markdown", + "id": "69a37995", + "metadata": {}, + "source": [ + "\n", + "\n", + "## Getting to know ValidMind" + ] + }, + { + "cell_type": "markdown", + "id": "40c9eb24", + "metadata": {}, + "source": [ + "\n", + "\n", + "### Preview the documentation template\n", + "\n", + "Let's verify that you have connected the ValidMind Library to the ValidMind Platform and that the appropriate *template* is selected for your model.\n", + "\n", + "You will upload documentation and test results unique to your model based on this template later on. For now, **take a look at the default structure that the template provides with [the `vm.preview_template()` function](https://docs.validmind.ai/validmind/validmind.html#preview_template)** from the ValidMind library and note the empty sections:" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "62842e84", + "metadata": {}, + "outputs": [], + "source": [ + "vm.preview_template()" + ] + }, + { + "cell_type": "markdown", + "id": "6fab1c1c", + "metadata": {}, + "source": [ + "\n", + "\n", + "### View model documentation in the ValidMind Platform\n", + "\n", + "Next, let's head to the ValidMind Platform to see the template in action:\n", + "\n", + "1. In a browser, [log in to ValidMind](https://docs.validmind.ai/guide/configuration/log-in-to-validmind.html).\n", + "\n", + "2. In the left sidebar, navigate to **Inventory** and select the model you registered for this notebook.\n", + "\n", + "3. Click **Development** under Documents for your model and note how the structure of the documentation matches our preview above." + ] + }, + { + "cell_type": "markdown", + "id": "606d932b", + "metadata": {}, + "source": [ + "\n", + "\n", + "## Build the example model" + ] + }, + { + "cell_type": "markdown", + "id": "3d7ad25a", + "metadata": {}, + "source": [ + "\n", + "\n", + "### Import the sample dataset\n", + "\n", + "First, let's import the public [Bank Customer Churn Prediction](https://www.kaggle.com/datasets/shantanudhakadd/bank-customer-churn-prediction) dataset from Kaggle so that we have something to work with.\n", + "\n", + "In our below example, note that: \n", + "\n", + "- The target column, `Exited` has a value of `1` when a customer has churned and `0` otherwise.\n", + "- The ValidMind Library provides a wrapper to automatically load the dataset as a [Pandas DataFrame](https://pandas.pydata.org/docs/reference/api/pandas.DataFrame.html) object. A Pandas Dataframe is a two-dimensional tabular data structure that makes use of rows and columns." + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "8ea8188e", + "metadata": {}, + "outputs": [], + "source": [ + "from validmind.datasets.classification import customer_churn\n", + "\n", + "print(\n", + " f\"Loaded demo dataset with: \\n\\n\\t• Target column: '{customer_churn.target_column}' \\n\\t• Class labels: {customer_churn.class_labels}\"\n", + ")\n", + "\n", + "raw_df = customer_churn.load_data()\n", + "raw_df.head()" + ] + }, + { + "cell_type": "markdown", + "id": "a5ceef72", + "metadata": {}, + "source": [ + "\n", + "\n", + "### Preprocessing the raw dataset\n", + "\n", + "In this section, we preprocess the raw dataset so it is ready for model training and validation. This includes splitting the data into training, validation, and test subsets to support both model fitting and evaluation on unseen data, and then separating each subset into input features and target labels so the model can learn from customer attributes and predict whether a customer churned." + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "9d2bec58", + "metadata": {}, + "outputs": [], + "source": [ + "train_df, validation_df, test_df = customer_churn.preprocess(raw_df)\n", + "\n", + "x_train = train_df.drop(customer_churn.target_column, axis=1)\n", + "y_train = train_df[customer_churn.target_column]\n", + "x_val = validation_df.drop(customer_churn.target_column, axis=1)\n", + "y_val = validation_df[customer_churn.target_column]" + ] + }, + { + "cell_type": "markdown", + "id": "3b9edacf", + "metadata": {}, + "source": [ + "\n", + "\n", + "### Training an XGBoost classifier model\n", + "\n", + "In this section, we train an XGBoost classifier to predict customer churn, using early stopping to halt training if performance does not improve after 10 rounds and reduce unnecessary fitting. We configure the model to evaluate performance with three complementary metrics: error for incorrect predictions, logloss for prediction confidence, and auc for class separation. The model is trained on the training split and evaluated against the validation split during fitting, while verbose=False keeps the training output concise." + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "658447fc", + "metadata": {}, + "outputs": [], + "source": [ + "model = xgb.XGBClassifier(early_stopping_rounds=10)\n", + "\n", + "model.set_params(\n", + " eval_metric=[\"error\", \"logloss\", \"auc\"],\n", + ")\n", + "\n", + "model.fit(\n", + " x_train,\n", + " y_train,\n", + " eval_set=[(x_val, y_val)],\n", + " verbose=False,\n", + ")" + ] + }, + { + "cell_type": "markdown", + "id": "c2a6b492", + "metadata": {}, + "source": [ + "\n", + "\n", + "## Initialize the ValidMind inputs\n", + "\n", + "We begin by registering the datasets and trained model as ValidMind inputs so they can be referenced consistently throughout the documentation workflow. For the datasets, this means creating ValidMind Dataset objects for the raw, training, and testing data, each with a unique `input_id` for traceability. Where needed, we also provide supporting metadata such as the target column and class labels so tests can interpret the data correctly." + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "081548ae", + "metadata": {}, + "outputs": [], + "source": [ + "# Initialize the raw dataset\n", + "vm_raw_dataset = vm.init_dataset(\n", + " dataset=raw_df,\n", + " input_id=\"raw_dataset\",\n", + " target_column=customer_churn.target_column,\n", + " class_labels=customer_churn.class_labels,\n", + ")\n", + "\n", + "# Initialize the training dataset\n", + "vm_train_ds = vm.init_dataset(\n", + " dataset=train_df,\n", + " input_id=\"train_dataset\",\n", + " target_column=customer_churn.target_column,\n", + ")\n", + "\n", + "# Initialize the testing dataset\n", + "vm_test_ds = vm.init_dataset(\n", + " dataset=test_df,\n", + " input_id=\"test_dataset\",\n", + " target_column=customer_churn.target_column\n", + ")" + ] + }, + { + "cell_type": "markdown", + "id": "1ebfda19", + "metadata": {}, + "source": [ + "Next, we initialize a ValidMind model object with `vm.init_model()`. This creates a standardized representation of the trained model that can be passed into ValidMind tests and other library functions, making it possible to evaluate the model and connect its results to the documentation." + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "6cc5aff8", + "metadata": {}, + "outputs": [], + "source": [ + "# Initialize the model\n", + "vm_model = vm.init_model(\n", + " model,\n", + " input_id=\"model\",\n", + ")" + ] + }, + { + "cell_type": "markdown", + "id": "48d23cf8", + "metadata": {}, + "source": [ + "Finally, we assign predictions from the trained model to the training and testing datasets. The `assign_predictions()` method links predicted classes and probabilities to each dataset, and can also compute predictions automatically if they are not passed explicitly. This step is what allows ValidMind to run performance and diagnostic tests using the model outputs." + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "922baa9d", + "metadata": {}, + "outputs": [], + "source": [ + "vm_train_ds.assign_predictions(\n", + " model=vm_model,\n", + ")\n", + "vm_test_ds.assign_predictions(\n", + " model=vm_model,\n", + ")" + ] + }, + { + "cell_type": "markdown", + "id": "7c9a174d", + "metadata": {}, + "source": [ + "\n", + "\n", + "## Document test results\n", + "\n", + "In this section, we run the documentation tests defined by the applied template to populate the quantitative parts of the model documentation. The `vm.run_documentation_tests()` function discovers each test-driven block in the template, executes the corresponding tests, and uploads the resulting artifacts to the ValidMind Platform.\n", + "\n", + "To run the full suite successfully, ValidMind needs to know which model and dataset inputs should be used for each test. This can be done with a shared `inputs` argument when all tests use the same objects, or with a `config` dictionary when individual tests require specific inputs or parameters. In this example, we use the default test parameters and provide the input configuration needed for the demo model." + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "47f7e709", + "metadata": {}, + "outputs": [], + "source": [ + "from validmind.utils import preview_test_config\n", + "\n", + "test_config = customer_churn.get_demo_test_config()\n", + "preview_test_config(test_config)" + ] + }, + { + "cell_type": "markdown", + "id": "3f22d37b", + "metadata": {}, + "source": [ + "Once the configuration is prepared, we pass it to `vm.run_documentation_tests()` and execute the full suite. The returned `full_suite` object contains the test results and represents the quantitative documentation that has been generated for the model." + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "999be7fe", + "metadata": {}, + "outputs": [], + "source": [ + "full_suite = vm.run_documentation_tests(config=test_config)" + ] + }, + { + "cell_type": "markdown", + "id": "5d531744", + "metadata": {}, + "source": [ + "\n", + "\n", + "## Document qualitative sections\n", + "\n", + "In addition to documenting quantitative results through tests, ValidMind now supports programmatic generation of qualitative content for the text blocks in a model documentation template through `vm.run_text_generation()`. This function allows you to generate AI-assisted text for a specific content block directly from a notebook and then log it back to the corresponding section of the document. As a result, you can populate qualitative sections without switching to the UI to write text manually or trigger generation one section at a time.\n", + "\n", + "In the next sections, we’ll walk through the main ways to use this functionality. We’ll start by generating text for a single content block with the default behavior, then show how to customize the output with a prompt, how to control the context used for generation by selecting specific sections, and finally how to scale the same pattern across all text blocks in the document." + ] + }, + { + "cell_type": "markdown", + "id": "899c8553", + "metadata": {}, + "source": [ + "\n", + "\n", + "### Generate text for a single content block\n", + "\n", + "First, we’ll use `vm.run_text_generation()` to generate qualitative text for a single documentation block. By providing a `content_id`, you can target the exact text placeholder you want to populate and let ValidMind generate content using the current document context. The helper `vm.get_content_ids()` is useful for inspecting which content blocks are available in the active template, making it easier to identify the IDs you can use when generating and logging text programmatically." + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "85cc552f", + "metadata": {}, + "outputs": [], + "source": [ + "vm.get_content_ids()" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "26fcddf9", + "metadata": {}, + "outputs": [], + "source": [ + "vm.run_text_generation(\n", + " content_id=\"dataset_summary_text\",\n", + ").log()" + ] + }, + { + "cell_type": "markdown", + "id": "caff6490", + "metadata": {}, + "source": [ + "\n", + "\n", + "### Customize the prompt\n", + "\n", + "Next, we’ll customize the generated output by passing a `prompt` to `vm.run_text_generation()`. This makes it possible to guide not just the subject of the generated text, but also its structure, tone, level of detail, and presentation format. In practice, this allows you to tailor the output for different documentation needs, such as producing a short narrative summary, a more structured section, or content written for a specific audience, while still relying on the same underlying document context for generation." + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "52165b98", + "metadata": {}, + "outputs": [], + "source": [ + "prompt = \"\"\"\n", + "Use exactly this structure:\n", + "\n", + "

Dataset Overview

\n", + "

Explain in 1-2 sentences what the dataset contains and what it is used for.

\n", + "\n", + "

Dataset Summary

\n", + "

Summarize the dataset structure, target outcome, and the main types of input features in 2-3 sentences.

\n", + "\n", + "

Key Characteristics

\n", + "\n", + "\n", + "

Data Quality and Considerations

\n", + "\n", + "\n", + "

Overall Assessment

\n", + "

End with a short balanced conclusion on the dataset's suitability for model development and evaluation.

\n", + "\"\"\"" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "fbf10ad9", + "metadata": {}, + "outputs": [], + "source": [ + "vm.run_text_generation(\n", + " content_id=\"dataset_summary_text\",\n", + " prompt=prompt,\n", + ").log()" + ] + }, + { + "cell_type": "markdown", + "id": "99a0740e", + "metadata": {}, + "source": [ + "\n", + "\n", + "### Pass section-specific context\n", + "\n", + "Then, we’ll control the `context` used for generation by passing a selected set of content IDs to `vm.run_text_generation()`. Rather than relying on the full document, this lets you focus the model on the most relevant parts of the documentation for a given text block. In practice, that means you can generate more targeted qualitative content by choosing which existing test and text blocks should inform the output." + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "43cf0e7d", + "metadata": {}, + "outputs": [], + "source": [ + "vm.get_content_ids(\"data_description\")" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "1e1a919e", + "metadata": {}, + "outputs": [], + "source": [ + "vm.run_text_generation(\n", + " content_id=\"dataset_summary_text\",\n", + " context={\"content_ids\": vm.get_content_ids(\"data_description\")},\n", + ").log()" + ] + }, + { + "cell_type": "markdown", + "id": "701a0323", + "metadata": {}, + "source": [ + "\n", + "\n", + "### Append a new text block to a section\n", + "\n", + "Sometimes you may want to generate text for a `content_id` that is not already defined in the template. In that case, you can still generate the text with `vm.run_text_generation()` and then use `.log(section_id=...)` to tell ValidMind where that new text block should be placed in the document. " + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "6a9ba924", + "metadata": {}, + "outputs": [], + "source": [ + "vm.run_text_generation(\n", + " content_id=\"intended_use\",\n", + " section_id=\"intended_use\",\n", + ").log()" + ] + }, + { + "cell_type": "markdown", + "id": "6e032b79", + "metadata": {}, + "source": [ + "\n", + "\n", + "### Generate text across the document\n", + "\n", + "At this stage, instead of generating one block at a time, we can populate multiple qualitative sections in a single pass.\n", + "\n", + "The [`vm.generate_documentation_text`](https://docs.validmind.ai/validmind/validmind.html#generate_documentation_text) function reads a configuration dictionary, generates content for each target block, logs the generated text to the ValidMind Platform, and returns a notebook summary grouped by section.\n", + "\n", + "- The function uses a `config` argument to describe which text blocks to generate and how each one should be handled.\n", + "- The `config` parameter is a dictionary with the following structure:\n", + "\n", + " ```python\n", + " config = {\n", + " \"\": {\n", + " \"section_id\": \"\",\n", + " \"prompt\": \"Optional custom prompt\",\n", + " \"context\": {\n", + " \"content_ids\": [\"\", \"\"]\n", + " }\n", + " },\n", + " ...\n", + " }\n", + " ```\n", + "\n", + " Each `` represents a documentation text block to populate. Use `section_id` when the block should be inserted into a specific section, `prompt` when you want to shape the output more explicitly, and `context.content_ids` when you want the generation step to focus on selected parts of the document. In this notebook, `text_config` comes from `customer_churn.get_demo_text_config()`, which provides the demo setup for the customer churn example." + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "a97bb129", + "metadata": {}, + "outputs": [], + "source": [ + "text_config = customer_churn.get_demo_text_config()\n", + "preview_test_config(text_config)" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "aff42702", + "metadata": {}, + "outputs": [], + "source": [ + "results = vm.generate_documentation_text(config=text_config)" + ] + }, + { + "cell_type": "markdown", + "id": "03b6b875", + "metadata": {}, + "source": [ + "\n", + "\n", + "## In summary\n", + "\n", + "In this notebook, you learned how to:\n", + "\n", + "- [x] Build and document an example customer churn model with ValidMind\n", + "- [x] Run documentation tests to populate the quantitative sections of a model document\n", + "- [x] Generate qualitative text for a single documentation content block with `vm.run_text_generation()`\n", + "- [x] Customize generated output by passing a prompt\n", + "- [x] Control generation context by selecting specific sections of the document\n", + "- [x] Use a configuration-driven workflow to generate qualitative content across the document with `vm.generate_documentation_text()`" + ] + }, + { + "cell_type": "markdown", + "id": "3db3c328", + "metadata": {}, + "source": [ + "\n", + "\n", + "## Next steps\n", + "\n", + "You can look at the output produced by the ValidMind Library right in the notebook where you ran the code, as you would expect. But there is a better way — use the ValidMind Platform to work with your model documentation." + ] + }, + { + "cell_type": "markdown", + "id": "d7bd8df8", + "metadata": {}, + "source": [ + "\n", + "\n", + "### Work with your model documentation\n", + "\n", + "1. From the **Inventory** in the ValidMind Platform, go to the model you registered earlier. ([Need more help?](https://docs.validmind.ai/guide/model-inventory/working-with-model-inventory.html))\n", + "\n", + "2. In the left sidebar that appears for your model, click **Development** under Documents.\n", + "\n", + "What you see is the full draft of your model documentation in a more easily consumable version. From here, you can make qualitative edits to model documentation, view guidelines, collaborate with validators, and submit your model documentation for approval when it's ready. [Learn more ...](https://docs.validmind.ai/guide/working-with-model-documentation.html)" + ] + }, + { + "cell_type": "markdown", + "id": "c0951457", + "metadata": {}, + "source": [ + "\n", + "\n", + "### Discover more learning resources\n", + "\n", + "For a more in-depth introduction to using the ValidMind Library for development, check out our introductory development series and the accompanying interactive training:\n", + "\n", + "- **[ValidMind for model development](https://docs.validmind.ai/developer/validmind-library.html#for-model-development)**\n", + "- **[Developer Fundamentals](https://docs.validmind.ai/training/developer-fundamentals/developer-fundamentals-register.html)**\n", + "\n", + "We also offer many interactive notebooks to help you document models:\n", + "\n", + "- [Run tests & test suites](https://docs.validmind.ai/developer/how-to/testing-overview.html)\n", + "- [Use ValidMind Library features](https://docs.validmind.ai/developer/how-to/feature-overview.html)\n", + "- [Code samples by use case](https://docs.validmind.ai/guide/samples-jupyter-notebooks.html)\n", + "\n", + "Or, visit our [documentation](https://docs.validmind.ai/) to learn more about ValidMind." + ] + }, + { + "cell_type": "markdown", + "id": "24532182", + "metadata": {}, + "source": [ + "\n", + "\n", + "## Upgrade ValidMind\n", + "\n", + "
After installing ValidMind, you’ll want to periodically make sure you are on the latest version to access any new features and other enhancements.
\n", + "\n", + "Retrieve the information for the currently installed version of ValidMind:" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "2e796c43", + "metadata": {}, + "outputs": [], + "source": [ + "%pip show validmind" + ] + }, + { + "cell_type": "markdown", + "id": "713a6722", + "metadata": {}, + "source": [ + "If the version returned is lower than the version indicated in our [production open-source code](https://github.com/validmind/validmind-library/blob/prod/validmind/__version__.py), restart your notebook and run:\n", + "\n", + "```bash\n", + "%pip install --upgrade validmind\n", + "```" + ] + }, + { + "cell_type": "markdown", + "id": "84a65def", + "metadata": {}, + "source": [ + "You may need to restart your kernel after running the upgrade package for changes to be applied." + ] + }, + { + "cell_type": "markdown", + "id": "copyright-18d82030e09942c4953248e9bf432249", + "metadata": {}, + "source": [ + "\n", + "\n", + "\n", + "\n", + "***\n", + "\n", + "Copyright © 2023-2026 ValidMind Inc. All rights reserved.
\n", + "Refer to [LICENSE](https://github.com/validmind/validmind-library/blob/main/LICENSE) for details.
\n", + "SPDX-License-Identifier: AGPL-3.0 AND ValidMind Commercial
" + ] + } + ], + "metadata": { + "kernelspec": { + "display_name": "ValidMind Library", + "language": "python", + "name": "validmind" + }, + "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.11" + } + }, + "nbformat": 4, + "nbformat_minor": 5 +} diff --git a/pyproject.toml b/pyproject.toml index 18afed736..90558321f 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -1,6 +1,6 @@ [project] name = "validmind" -version = "2.12.5" +version = "2.13.0" description = "ValidMind Library" readme = "README.pypi.md" requires-python = ">=3.9,<3.15" diff --git a/r/validmind/DESCRIPTION b/r/validmind/DESCRIPTION index ccd27a16f..4aed5bf4e 100644 --- a/r/validmind/DESCRIPTION +++ b/r/validmind/DESCRIPTION @@ -1,7 +1,7 @@ Package: validmind Type: Package Title: Interface to the 'ValidMind' Platform -Version: 2.12.5 +Version: 2.13.0 Authors@R: c(person("Andres", "Rodriguez", role = c("aut", "cre","cph"), email = "andres@validmind.ai")) Maintainer: Andres Rodriguez diff --git a/tests/test_api_client.py b/tests/test_api_client.py index 8ea5e4228..ba24f86ca 100644 --- a/tests/test_api_client.py +++ b/tests/test_api_client.py @@ -16,10 +16,11 @@ import validmind.api_client as api_client from validmind.__version__ import __version__ from validmind.errors import ( + APIRequestError, MissingAPICredentialsError, MissingModelIdError, - APIRequestError, ) +from validmind.utils import md_to_html from validmind.vm_models.figure import Figure @@ -225,6 +226,27 @@ def test_log_metadata(self, mock_post: MagicMock): ), ) + @patch("aiohttp.ClientSession.post") + def test_log_metadata_with_section_id(self, mock_post: MagicMock): + mock_post.return_value = MockAsyncResponse(200, json={"cuid": "abc1234"}) + + self.run_async( + api_client.alog_metadata, + "1234", + text="Some Text", + section_id="intended_use", + ) + + mock_post.assert_called_with( + f"{os.environ['VM_API_HOST']}/log_metadata?section_id=intended_use", + data=json.dumps( + { + "content_id": "1234", + "text": "Some Text", + } + ), + ) + @patch("aiohttp.ClientSession.post") def test_log_test_result(self, mock_post): result = { @@ -245,6 +267,128 @@ def test_log_test_result(self, mock_post): mock_post.assert_called_with(url, data=json.dumps(result)) + @patch("requests.post") + @patch("aiohttp.ClientSession.post") + def test_log_text_generates_text_and_logs_metadata( + self, mock_aiohttp_post, mock_requests_post + ): + mock_requests_post.return_value = Mock(status_code=200) + mock_requests_post.return_value.json.return_value = { + "content": "## Generated Summary\nGenerated content." + } + mock_aiohttp_post.return_value = MockAsyncResponse( + 200, + json={ + "content_id": "dataset_summary_text", + "text": md_to_html("## Generated Summary\nGenerated content.", mathml=True), + }, + ) + + api_client.log_text( + content_id="dataset_summary_text", + prompt="Summarize the dataset.", + context={"content_ids": ["train_dataset", "target_description_text"]}, + ) + + mock_requests_post.assert_called_once_with( + url=f"{os.environ['VM_API_HOST']}/ai/generate/qualitative_text_generation", + headers={ + "X-API-KEY": os.environ["VM_API_KEY"], + "X-API-SECRET": os.environ["VM_API_SECRET"], + "X-MODEL-CUID": os.environ["VM_API_MODEL"], + "X-MONITORING": "False", + "X-LIBRARY-VERSION": __version__, + }, + json={ + "content_id": "dataset_summary_text", + "generate": True, + "prompt": "Summarize the dataset.", + "context": { + "content_ids": ["train_dataset", "target_description_text"] + }, + }, + ) + mock_aiohttp_post.assert_called_once_with( + f"{os.environ['VM_API_HOST']}/log_metadata", + data=json.dumps( + { + "content_id": "dataset_summary_text", + "text": md_to_html( + "## Generated Summary\nGenerated content.", mathml=True + ), + } + ), + ) + + @patch("requests.post") + @patch("aiohttp.ClientSession.post") + def test_log_text_logs_metadata_with_section_id( + self, mock_aiohttp_post, mock_requests_post + ): + mock_requests_post.return_value = Mock(status_code=200) + mock_requests_post.return_value.json.return_value = { + "content": "Generated content." + } + mock_aiohttp_post.return_value = MockAsyncResponse( + 200, + json={ + "content_id": "dataset_summary_text", + "text": "Generated content.", + }, + ) + + api_client.log_text( + content_id="dataset_summary_text", + prompt="Summarize the dataset.", + section_id="intended_use", + ) + + mock_requests_post.assert_called_once_with( + url=f"{os.environ['VM_API_HOST']}/ai/generate/qualitative_text_generation", + headers={ + "X-API-KEY": os.environ["VM_API_KEY"], + "X-API-SECRET": os.environ["VM_API_SECRET"], + "X-MODEL-CUID": os.environ["VM_API_MODEL"], + "X-MONITORING": "False", + "X-LIBRARY-VERSION": __version__, + }, + json={ + "content_id": "dataset_summary_text", + "generate": True, + "prompt": "Summarize the dataset.", + "section_id": "intended_use", + }, + ) + mock_aiohttp_post.assert_called_once_with( + f"{os.environ['VM_API_HOST']}/log_metadata?section_id=intended_use", + data=json.dumps( + { + "content_id": "dataset_summary_text", + "text": md_to_html("Generated content.", mathml=True), + } + ), + ) + + def test_log_text_rejects_prompt_when_text_is_provided(self): + with self.assertRaisesRegex( + ValueError, "`prompt` is only supported when `text` is omitted" + ): + api_client.log_text( + content_id="dataset_summary_text", + text="Hello world", + prompt="Ignore the provided text.", + ) + + def test_log_text_rejects_invalid_context(self): + with self.assertRaisesRegex( + ValueError, + "`context\\['content_ids'\\]` must contain only non-empty strings", + ): + api_client.log_text( + content_id="dataset_summary_text", + context={"content_ids": ["valid", ""]}, + ) + if __name__ == "__main__": unittest.main() diff --git a/tests/test_client.py b/tests/test_client.py index 72a9579f5..53a059b6b 100644 --- a/tests/test_client.py +++ b/tests/test_client.py @@ -12,12 +12,16 @@ import validmind from validmind import ( + generate_documentation_text, + get_content_ids, init_dataset, init_model, get_test_suite, + run_text_generation, run_documentation_tests, ) from validmind.errors import UnsupportedModelError +from validmind.vm_models.result import TextGenerationResult @dataclass @@ -115,7 +119,7 @@ def test_init_model_invalid_metadata_dict(self): "key": "value", "foo": "bar", } - with self.assertRaises(UnsupportedModelError) as context: + with self.assertRaises(UnsupportedModelError): init_model(attributes=metadata, __log=False) def test_init_model_metadata_dict(self): @@ -163,6 +167,173 @@ def test_get_default_config(self): self.assertIn("params", config) +class TestGetContentIds(TestCase): + @mock.patch( + "validmind.client_config.client_config.documentation_template", + MockedConfig.documentation_template, + ) + def test_get_all_content_ids(self): + content_ids = get_content_ids() + self.assertEqual( + content_ids, + [ + "validmind.data_validation.ClassImbalance", + "validmind.data_validation.DatasetSplit", + ], + ) + + @mock.patch( + "validmind.client_config.client_config.documentation_template", + MockedConfig.documentation_template, + ) + def test_get_content_ids_for_single_section(self): + content_ids = get_content_ids("test_section_1") + self.assertEqual(content_ids, ["validmind.data_validation.ClassImbalance"]) + + @mock.patch( + "validmind.client_config.client_config.documentation_template", + MockedConfig.documentation_template, + ) + def test_get_content_ids_for_multiple_sections(self): + content_ids = get_content_ids(["test_section_1", "test_section_2"]) + self.assertEqual( + content_ids, + [ + "validmind.data_validation.ClassImbalance", + "validmind.data_validation.DatasetSplit", + ], + ) + + +class TestRunTextGeneration(TestCase): + @mock.patch( + "validmind.client.api_client._generate_log_text", + return_value="

Generated text

", + ) + def test_run_text_generation(self, mock_generate_text): + result = run_text_generation( + content_id="dataset_summary_text", + prompt="Summarize the dataset.", + section_id="data_description", + context={"content_ids": ["train_dataset"]}, + show=False, + ) + + self.assertIsInstance(result, TextGenerationResult) + self.assertEqual(result.content_id, "dataset_summary_text") + self.assertEqual(result.prompt, "Summarize the dataset.") + self.assertEqual(result.section_id, "data_description") + self.assertEqual(result.context, {"content_ids": ["train_dataset"]}) + self.assertEqual(result.description, "

Generated text

") + self.assertTrue(result._was_description_generated) + self.assertIn("validmind", result.metadata) + self.assertIn("timestamp", result.metadata) + self.assertIn("duration_seconds", result.metadata) + mock_generate_text.assert_called_once_with( + "dataset_summary_text", + "Summarize the dataset.", + {"content_ids": ["train_dataset"]}, + section_id="data_description", + ) + + +class TestGenerateDocumentationText(TestCase): + @mock.patch( + "validmind.client_config.client_config.documentation_template", + MockedConfig.documentation_template, + ) + @mock.patch("validmind.client.vm_display") + @mock.patch("validmind.client.DocumentationTextSummary") + @mock.patch("validmind.client.HTMLLabel") + @mock.patch("validmind.client.HTMLProgressBar") + @mock.patch("validmind.client.run_text_generation") + def test_generate_documentation_text( + self, + mock_run_text_generation, + mock_progress_bar, + mock_label, + mock_summary, + mock_display, + ): + first_result = mock.Mock() + second_result = mock.Mock() + mock_run_text_generation.side_effect = [first_result, second_result] + + progress_instance = mock_progress_bar.return_value + progress_instance.value = 0 + progress_instance.update.side_effect = ( + lambda value, description=None: setattr(progress_instance, "value", value) + ) + + config = { + "monitoring_plan": { + "prompt": "Write the monitoring plan.", + "section_id": "monitoring", + "context": {"content_ids": ["model_summary"]}, + }, + "governance_plan": { + "prompt": "Write the governance plan.", + }, + } + + results = generate_documentation_text(config) + + self.assertEqual( + results, + { + "monitoring_plan": first_result, + "governance_plan": second_result, + }, + ) + self.assertEqual(mock_run_text_generation.call_count, 2) + mock_run_text_generation.assert_has_calls( + [ + mock.call( + content_id="monitoring_plan", + prompt="Write the monitoring plan.", + section_id="monitoring", + context={"content_ids": ["model_summary"]}, + show=False, + ), + mock.call( + content_id="governance_plan", + prompt="Write the governance plan.", + section_id=None, + context=None, + show=False, + ), + ] + ) + first_result.log.assert_called_once_with() + second_result.log.assert_called_once_with() + mock_label.assert_called_once_with(value="Generating documentation text...") + mock_progress_bar.assert_called_once_with( + max_value=4, + description="Generating documentation text...", + ) + progress_instance.display.assert_called_once_with() + progress_instance.close.assert_called_once_with() + mock_label.return_value.update.assert_any_call( + "Sending result to ValidMind: monitoring_plan..." + ) + mock_label.return_value.update.assert_any_call( + "Sending result to ValidMind: governance_plan..." + ) + mock_label.return_value.update.assert_any_call( + "Documentation text generation complete!" + ) + mock_summary.assert_called_once_with( + title="Binary classification", + description="Template for binary classification models.", + results={ + "monitoring_plan": first_result, + "governance_plan": second_result, + }, + template_sections=MockedConfig.documentation_template["sections"], + ) + mock_display.assert_called_once_with(mock_summary.return_value) + + # TODO: Fix this test # class TestPreviewTemplate(TestCase): # @mock.patch( diff --git a/tests/test_results.py b/tests/test_results.py index 0eafb0679..87b74c3c0 100644 --- a/tests/test_results.py +++ b/tests/test_results.py @@ -13,6 +13,10 @@ ResultTable, RawData, ) +from validmind.vm_models.result.utils import ( + AI_REVISION_NAME, + DEFAULT_REVISION_NAME, +) from validmind.vm_models.figure import Figure from validmind.errors import InvalidParameterError @@ -181,11 +185,141 @@ def test_text_generation_result(self): self.assertEqual(text_result.name, "Text Generation Result") self.assertEqual(text_result.title, "Text Test") self.assertEqual(text_result.description, "Generated text") + self.assertIsNone(text_result.doc) + self.assertIsNone(text_result.test_name) html = text_result.to_html() self.assertIsInstance(html, str) self.assertIn("Generated text", html) + @patch("validmind.vm_models.result.result.api_client.alog_text") + async def test_text_generation_result_log_async(self, mock_log_text): + """Test async logging of TextGenerationResult through alog_text""" + text_result = TextGenerationResult( + result_id="text_1", + content_id="dataset_summary_text", + description="Generated text", + ) + text_result._client_config_cache = type( + "MockConfig", + (), + { + "documentation_template": { + "sections": [ + { + "id": "data_description", + "contents": [ + { + "content_id": "dataset_summary_text", + "content_type": "text", + } + ], + } + ] + } + }, + )() + + await text_result.log_async() + + mock_log_text.assert_called_once_with( + content_id=f"dataset_summary_text::{DEFAULT_REVISION_NAME}", + text="Generated text", + section_id=None, + ) + + @patch("validmind.vm_models.result.result.api_client.alog_text") + async def test_text_generation_result_log_async_with_section_id(self, mock_log_text): + """Test async logging of TextGenerationResult forwards section_id""" + text_result = TextGenerationResult( + result_id="text_1", + content_id="intended_use_text", + description="Generated text", + section_id="intended_use", + ) + text_result._client_config_cache = type( + "MockConfig", + (), + {"documentation_template": {"sections": [{"id": "intended_use"}]}}, + )() + + await text_result.log_async() + + mock_log_text.assert_called_once_with( + content_id=f"intended_use_text::{DEFAULT_REVISION_NAME}", + text="Generated text", + section_id="intended_use", + ) + + @patch("validmind.vm_models.result.result.api_client.alog_text") + async def test_text_generation_result_log_async_uses_ai_revision_name( + self, mock_log_text + ): + """Test generated text logs with the AI revision name""" + text_result = TextGenerationResult( + result_id="text_1", + content_id="dataset_summary_text", + description="Generated text", + _was_description_generated=True, + ) + text_result._client_config_cache = type( + "MockConfig", + (), + { + "documentation_template": { + "sections": [ + { + "id": "data_description", + "contents": [ + { + "content_id": "dataset_summary_text", + "content_type": "text", + } + ], + } + ] + } + }, + )() + + await text_result.log_async() + + mock_log_text.assert_called_once_with( + content_id=f"dataset_summary_text::{AI_REVISION_NAME}", + text="Generated text", + section_id=None, + ) + + async def test_text_generation_result_log_async_requires_section_id_for_new_block(self): + """Test new generated text requires a section_id for placement""" + text_result = TextGenerationResult( + result_id="text_1", + content_id="model_overview_text", + description="Generated text", + ) + text_result._client_config_cache = type( + "MockConfig", + (), + {"documentation_template": {"sections": [{"id": "existing_section"}]}}, + )() + + with self.assertRaisesRegex( + ValueError, "New generated content requires `section_id` for placement" + ): + await text_result.log_async() + + async def test_text_generation_result_log_async_requires_content_id(self): + """Test TextGenerationResult requires a content_id when logging""" + text_result = TextGenerationResult( + result_id="text_1", + description="Generated text", + ) + + with self.assertRaisesRegex( + ValueError, "`content_id` must be provided to log generated text" + ): + await text_result.log_async() + def test_validate_log_config(self): """Test validation of log configuration""" test_result = TestResult(result_id="test_1") diff --git a/tests/unit_tests/test_documentation_text_summary.py b/tests/unit_tests/test_documentation_text_summary.py new file mode 100644 index 000000000..fcf117f0e --- /dev/null +++ b/tests/unit_tests/test_documentation_text_summary.py @@ -0,0 +1,62 @@ +"""Tests for documentation text summary rendering.""" + +from unittest import mock + +from validmind.vm_models.text_generation_summary import DocumentationTextSummary + + +def test_documentation_text_summary_renders_link_and_accordion(): + monitoring_result = mock.Mock() + monitoring_result.section_id = "monitoring_details" + monitoring_result.to_html.return_value = "
Monitoring body
" + + governance_result = mock.Mock() + governance_result.section_id = None + governance_result.to_html.return_value = "
Governance body
" + + summary = DocumentationTextSummary( + title="Binary classification", + description="Template for binary classification models.", + results={ + "monitoring_plan": monitoring_result, + "governance_plan": governance_result, + }, + template_sections=[ + { + "id": "monitoring", + "title": "Monitoring Framework", + "contents": [], + }, + { + "id": "monitoring_details", + "title": "Monitoring Details", + "parent_section": "monitoring", + "contents": [], + }, + { + "id": "governance", + "title": "Governance and Controls", + "contents": [{"content_id": "governance_plan"}], + }, + ], + ) + + with mock.patch( + "validmind.api_client.get_api_host", + return_value="https://api.example.com/api/v1/tracking", + ), mock.patch("validmind.api_client.get_api_model", return_value="Cmd123"): + html = summary.to_html() + + assert "Generated Documentation Text" in html + assert "Binary classification" in html + assert "Check out the updated documentation on" in html + assert "https://app.example.com/model-inventory/Cmd123" in html + assert "Monitoring Framework" in html + assert "Governance and Controls" in html + assert "Monitoring Details" not in html + assert ">monitoring<" not in html + assert ">governance<" not in html + assert "Text Block: 'monitoring_plan'" in html + assert "Text Block: 'governance_plan'" in html + assert "Monitoring body" in html + assert "Governance body" in html diff --git a/tests/unit_tests/test_html_renderer.py b/tests/unit_tests/test_html_renderer.py new file mode 100644 index 000000000..eb8c41e18 --- /dev/null +++ b/tests/unit_tests/test_html_renderer.py @@ -0,0 +1,28 @@ +"""Tests for progress HTML rendering.""" + +from validmind.vm_models.html_renderer import StatefulHTMLRenderer + + +def test_render_progress_bar_includes_progress_css(): + result = StatefulHTMLRenderer.render_progress_bar( + value=1, + max_value=4, + description="Generating text for monitoring_plan...", + bar_id="progress-test", + ) + + assert ".vm-progress-description" in result + assert "font-weight: bold;" in result + assert "Generating text for monitoring_plan..." in result + + +def test_render_live_progress_bar_includes_progress_css(): + result = StatefulHTMLRenderer.render_live_progress_bar( + max_value=4, + description="Running test suite...", + bar_id="progress-live-test", + ) + + assert ".vm-progress-description" in result + assert "font-weight: bold;" in result + assert "window.updateProgress_progress_live_test" in result diff --git a/validmind/__init__.py b/validmind/__init__.py index 93cc0aaca..e9c98a412 100644 --- a/validmind/__init__.py +++ b/validmind/__init__.py @@ -52,6 +52,8 @@ from .__version__ import __version__ # noqa: E402 from .api_client import init, log_metric, log_test_result, log_text, reload from .client import ( # noqa: E402 + generate_documentation_text, + get_content_ids, get_test_suite, init_dataset, init_model, @@ -59,6 +61,7 @@ preview_template, run_documentation_tests, run_test_suite, + run_text_generation, ) from .experimental import agents as experimental_agent from .tests.decorator import scorer as scorer_decorator @@ -116,11 +119,14 @@ def check_version(): "init_dataset", "init_model", "init_r_model", + "generate_documentation_text", "get_test_suite", + "get_content_ids", "log_metric", "preview_template", "print_env", "reload", + "run_text_generation", "run_documentation_tests", # log metric function (for direct/bulk/retroactive logging of metrics) # test suite functions (less common) diff --git a/validmind/__version__.py b/validmind/__version__.py index 336282f43..930e2cd68 100644 --- a/validmind/__version__.py +++ b/validmind/__version__.py @@ -1 +1 @@ -__version__ = "2.12.5" +__version__ = "2.13.0" diff --git a/validmind/api_client.py b/validmind/api_client.py index affacb80e..fdde7561f 100644 --- a/validmind/api_client.py +++ b/validmind/api_client.py @@ -278,6 +278,7 @@ async def alog_metadata( content_id: str, text: Optional[str] = None, _json: Optional[Dict[str, Any]] = None, + section_id: Optional[str] = None, ) -> Dict[str, Any]: """Logs free-form metadata to ValidMind API. @@ -285,6 +286,8 @@ async def alog_metadata( content_id (str): Unique content identifier for the metadata. text (str, optional): Free-form text to assign to the metadata. Defaults to None. _json (dict, optional): Free-form key-value pairs to assign to the metadata. Defaults to None. + section_id (str, optional): Section ID to append the text block to when the + content ID does not already exist. Raises: Exception: If the API call fails. @@ -298,9 +301,14 @@ async def alog_metadata( if _json is not None: metadata_dict["json"] = _json + request_params = {} + if section_id: + request_params["section_id"] = section_id + try: return await _post( "log_metadata", + params=request_params, data=json.dumps(metadata_dict, cls=NumpyEncoder, allow_nan=False), ) except Exception as e: @@ -445,37 +453,196 @@ def log_input(input_id: str, type: str, metadata: Dict[str, Any]) -> Dict[str, A return run_async(alog_input, input_id, type, metadata) -def log_text(content_id: str, text: str, _json: Optional[Dict[str, Any]] = None) -> str: - """Logs free-form text to ValidMind API. +def _validate_log_text_context( + context: Optional[Dict[str, Any]], +) -> Optional[Dict[str, List[str]]]: + """Validate supported AI generation context for ``log_text``.""" + if context is None: + return None + + if not isinstance(context, dict): + raise ValueError("`context` must be a dictionary or None") + + allowed_keys = {"content_ids"} + unknown_keys = set(context.keys()) - allowed_keys + if unknown_keys: + raise ValueError( + "Unsupported `context` keys: " + f"{', '.join(sorted(unknown_keys))}. Only `content_ids` is supported." + ) + + content_ids = context.get("content_ids") + if content_ids is None: + raise ValueError("`context` must include `content_ids` when provided") + if not isinstance(content_ids, list) or not content_ids: + raise ValueError("`context['content_ids']` must be a non-empty list") + if any( + not isinstance(content_id, str) or not content_id for content_id in content_ids + ): + raise ValueError("`context['content_ids']` must contain only non-empty strings") + + return {"content_ids": content_ids} + + +def generate_qualitative_text(text_generation_data: Dict[str, Any]) -> Dict[str, Any]: + """Generate qualitative text using the ValidMind AI API.""" + r = requests.post( + url=_get_url("ai/generate/qualitative_text_generation"), + headers=_get_api_headers(), + json=text_generation_data, + ) + + if r.status_code != 200: + raise_api_error(r.text) + + return r.json() + + +def _normalize_logged_text(text: str, field_name: str) -> str: + """Validate text content and convert Markdown to HTML when needed.""" + if not isinstance(text, str) or not text: + raise ValueError(f"`{field_name}` must be a non-empty string") + + if not is_html(text): + return md_to_html(text, mathml=True) + + return text + + +def _validate_manual_log_text_args( + text: str, prompt: Optional[str], context: Optional[Dict[str, Any]] +) -> str: + """Validate manual log_text arguments.""" + if prompt is not None: + raise ValueError("`prompt` is only supported when `text` is omitted") + if context is not None: + raise ValueError("`context` is only supported when `text` is omitted") + + return _normalize_logged_text(text, "text") + + +def _build_log_text_generation_request( + content_id: str, + prompt: Optional[str], + context: Optional[Dict[str, Any]], + section_id: Optional[str] = None, +) -> Dict[str, Any]: + """Build the request payload for AI-assisted text generation.""" + request_data = { + "content_id": content_id, + "generate": True, + } + + if prompt is not None: + if not isinstance(prompt, str) or not prompt: + raise ValueError("`prompt` must be a non-empty string") + request_data["prompt"] = prompt + + if section_id is not None: + if not isinstance(section_id, str) or not section_id: + raise ValueError("`section_id` must be a non-empty string") + request_data["section_id"] = section_id + + validated_context = _validate_log_text_context(context) + if validated_context is not None: + request_data["context"] = validated_context + + return request_data + + +def _generate_log_text( + content_id: str, + prompt: Optional[str], + context: Optional[Dict[str, Any]], + section_id: Optional[str] = None, +) -> str: + """Generate text for log_text and normalize it to HTML.""" + request_data = _build_log_text_generation_request( + content_id, + prompt, + context, + section_id=section_id, + ) + generated_text = generate_qualitative_text(request_data)["content"] + return _normalize_logged_text(generated_text, "generated text") + + +def _render_logged_text(logged_text: Dict[str, Any]) -> str: + """Render logged text as notebook-friendly HTML.""" + from .vm_models.html_renderer import StatefulHTMLRenderer + + return StatefulHTMLRenderer.render_accordion( + items=[logged_text["text"]], + titles=[f"Text Block: '{logged_text['content_id']}'"], + ) + + +async def alog_text( + content_id: str, + text: Optional[str] = None, + prompt: Optional[str] = None, + context: Optional[Dict[str, Any]] = None, + _json: Optional[Dict[str, Any]] = None, + section_id: Optional[str] = None, +) -> Dict[str, Any]: + """Async variant of ``log_text`` that logs or generates text.""" + if not content_id or not isinstance(content_id, str): + raise ValueError("`content_id` must be a non-empty string") + + if text is not None: + text = _validate_manual_log_text_args(text, prompt, context) + else: + text = _generate_log_text( + content_id, + prompt, + context, + section_id=section_id, + ) + + return await alog_metadata(content_id, text, _json, section_id=section_id) + + +def log_text( + content_id: str, + text: Optional[str] = None, + prompt: Optional[str] = None, + context: Optional[Dict[str, Any]] = None, + _json: Optional[Dict[str, Any]] = None, + section_id: Optional[str] = None, +) -> str: + """Logs or generates free-form text to ValidMind API. Args: content_id (str): Unique content identifier for the text. - text (str): The text to log. Will be converted to HTML with MathML support. + text (str, optional): The text to log. Will be converted to HTML with + MathML support when Markdown is provided. If omitted, text is + generated using the qualitative text generation backend. + prompt (str, optional): Custom prompt used for AI-assisted text + generation. Only supported when `text` is omitted. + context (dict, optional): Context object for AI-assisted text + generation. When omitted, the full document is used as context. + Currently only supports `{"content_ids": [, ...]}`. _json (dict, optional): Additional metadata to associate with the text. Defaults to None. + section_id (str, optional): Section ID to append the text block to when the + content ID does not already exist. Raises: - ValueError: If content_id or text are empty or not strings. + ValueError: If arguments are invalid or use incompatible combinations. Exception: If the API call fails. Returns: str: HTML string containing the logged text in an accordion format. """ - if not content_id or not isinstance(content_id, str): - raise ValueError("`content_id` must be a non-empty string") - if not text or not isinstance(text, str): - raise ValueError("`text` must be a non-empty string") - - if not is_html(text): - text = md_to_html(text, mathml=True) - - log_text = run_async(alog_metadata, content_id, text, _json) - - from .vm_models.html_renderer import StatefulHTMLRenderer - - return StatefulHTMLRenderer.render_accordion( - items=[log_text["text"]], - titles=[f"Text Block: '{log_text['content_id']}'"], + logged_text = run_async( + alog_text, + content_id=content_id, + text=text, + prompt=prompt, + context=context, + _json=_json, + section_id=section_id, ) + return _render_logged_text(logged_text) async def alog_metric( diff --git a/validmind/client.py b/validmind/client.py index c0fe8b2ca..c6fea1cde 100644 --- a/validmind/client.py +++ b/validmind/client.py @@ -6,6 +6,7 @@ Client interface for all data and model validation functions """ +import time from typing import Any, Callable, Dict, List, Optional, Union import numpy as np @@ -17,6 +18,7 @@ except Exception: torch = None # type: ignore # noqa: F401 +from . import api_client from .api_client import log_input as log_input from .client_config import client_config from .errors import ( @@ -31,18 +33,23 @@ from .logging import get_logger from .models.metadata import MetadataModel from .models.r_model import RModel -from .template import get_template_test_suite +from .template import get_template_content_ids, get_template_test_suite from .template import preview_template as _preview_template from .test_suites import get_by_id as get_test_suite_by_id +from .tests.run import _get_run_metadata +from .utils import display as vm_display from .utils import get_dataset_info, get_model_info from .vm_models import TestSuite, TestSuiteRunner from .vm_models.dataset import DataFrameDataset, PolarsDataset, TorchDataset, VMDataset +from .vm_models.html_progress import HTMLLabel, HTMLProgressBar from .vm_models.model import ( ModelAttributes, VMModel, get_model_class, is_model_metadata, ) +from .vm_models.result import TextGenerationResult +from .vm_models.text_generation_summary import DocumentationTextSummary pd.option_context("format.precision", 2) @@ -433,6 +440,145 @@ def preview_template() -> None: _preview_template(client_config.documentation_template) +def get_content_ids(section_ids: Optional[Union[str, List[str]]] = None) -> List[str]: + """Get content IDs for one or more documentation template sections. + + Args: + section_ids: Section ID or list of section IDs. If omitted, all content + IDs in the current documentation template are returned. + + Returns: + A list of content IDs in template order. + """ + if client_config.documentation_template is None: + raise MissingDocumentationTemplate( + "No documentation template found. Please run `vm.init()`" + ) + + return get_template_content_ids(client_config.documentation_template, section_ids) + + +def run_text_generation( + content_id: str, + prompt: Optional[str] = None, + section_id: Optional[str] = None, + context: Optional[Dict[str, Any]] = None, + show: bool = True, +) -> TextGenerationResult: + """Generate qualitative text and return a loggable result object. + + Args: + content_id: Content ID to generate text for. + prompt: Custom prompt for text generation. + section_id: Optional section ID used to guide generation for new content + and later placement during logging. + context: Optional context object for text generation. + show: Whether to display the generated result. + + Returns: + A TextGenerationResult containing the generated text. + """ + start_time = time.perf_counter() + description = api_client._generate_log_text( + content_id, prompt, context, section_id=section_id + ) + metadata = _get_run_metadata(duration_seconds=time.perf_counter() - start_time) + result = TextGenerationResult( + result_id=content_id, + result_type="qualitative_text_generation", + content_id=content_id, + title=f"Text Generation: {content_id}", + description=description, + metadata=metadata, + prompt=prompt, + section_id=section_id, + context=context, + _was_description_generated=True, + ) + + if show: + result.show() + + return result + + +def generate_documentation_text( + config: Dict[str, Dict[str, Any]], +) -> Dict[str, TextGenerationResult]: + """Generate and log document text blocks from a configuration dictionary. + + The config is keyed by target content ID. Each value may contain: + - `section_id` (optional): Section to append the content block to when it + does not already exist in the template. + - `prompt` (optional): Prompt override for generation. + - `context` (optional): Context object passed to `run_text_generation()`, + e.g. `{"content_ids": [...]}`. + + Args: + config: Mapping of content IDs to generation settings. + + Returns: + A dictionary mapping content IDs to generated and logged results. + """ + results = {} + num_tasks = len(config) * 2 + html_pbar = None + html_pbar_description = None + template = client_config.documentation_template or {} + + if num_tasks: + html_pbar_description = HTMLLabel(value="Generating documentation text...") + html_pbar = HTMLProgressBar( + max_value=num_tasks, + description="Generating documentation text...", + ) + html_pbar.display() + + def _update_progress_message(message: str) -> None: + if html_pbar: + html_pbar.update(html_pbar.value, message) + if html_pbar_description: + html_pbar_description.update(message) + + def _increment_progress() -> None: + if html_pbar: + html_pbar.update(html_pbar.value + 1) + + try: + for content_id, spec in config.items(): + progress_message = f"Sending result to ValidMind: {content_id}..." + _update_progress_message(progress_message) + result = run_text_generation( + content_id=content_id, + prompt=spec.get("prompt"), + section_id=spec.get("section_id"), + context=spec.get("context"), + show=False, + ) + _increment_progress() + + result.log() + _increment_progress() + results[content_id] = result + finally: + if html_pbar: + html_pbar.update(num_tasks, "Documentation text generation complete!") + html_pbar.close() + if html_pbar_description: + html_pbar_description.update("Documentation text generation complete!") + + if results: + summary = DocumentationTextSummary( + title=template.get("template_name", "Documentation Text"), + description=template.get("description", ""), + results=results, + template_sections=template.get("sections"), + ) + vm_display(summary) + + return results + + def run_documentation_tests( section: Optional[str] = None, send: bool = True, diff --git a/validmind/datasets/classification/config.json b/validmind/datasets/classification/config.json new file mode 100644 index 000000000..cd05ebb20 --- /dev/null +++ b/validmind/datasets/classification/config.json @@ -0,0 +1,121 @@ +{ + "model_overview": { + "section_id": "model_overview", + "context": { + "content_ids": [ + "validmind.data_validation.DatasetDescription", + "validmind.model_validation.sklearn.ClassifierPerformance:in_sample", + "validmind.model_validation.sklearn.ClassifierPerformance:out_of_sample" + ] + } + }, + "intended_use": { + "section_id": "intended_use" + }, + "regulatory_requirements": { + "section_id": "regulatory_requirements", + "prompt": "Write a comprehensive paragraphs describing the regulatory requirements for this model, assuming SR 11-7 is the primary regulatory framework." + }, + "model_limitations": { + "section_id": "model_limitations", + "context": { + "content_ids": [ + "validmind.model_validation.sklearn.TrainingTestDegradation", + "validmind.model_validation.sklearn.MinimumAccuracy", + "validmind.model_validation.sklearn.MinimumF1Score", + "validmind.model_validation.sklearn.MinimumROCAUCScore" + ] + } + }, + "model_selection": { + "section_id": "model_selection", + "context": { + "content_ids": [ + "validmind.model_validation.ModelMetadata", + "validmind.model_validation.sklearn.ClassifierPerformance:in_sample", + "validmind.model_validation.sklearn.ClassifierPerformance:out_of_sample" + ] + } + }, + "dataset_summary_text": { + "context": { + "content_ids": [ + "validmind.data_validation.DatasetDescription" + ] + } + }, + "data_quality_tests_text": { + "context": { + "content_ids": [ + "validmind.data_validation.ClassImbalance", + "validmind.data_validation.Duplicates", + "validmind.data_validation.HighCardinality", + "validmind.data_validation.MissingValues", + "validmind.data_validation.Skewness", + "validmind.data_validation.UniqueRows", + "validmind.data_validation.TooManyZeroValues", + "validmind.data_validation.IQROutliersTable", + "validmind.data_validation.IQROutliersBarPlot" + ] + } + }, + "feature_selection": { + "context": { + "content_ids": [ + "validmind.data_validation.PearsonCorrelationMatrix", + "validmind.data_validation.HighPearsonCorrelation" + ] + } + }, + "model_validation_tests_text": { + "context": { + "content_ids": [ + "validmind.model_validation.sklearn.ConfusionMatrix", + "validmind.model_validation.sklearn.ClassifierPerformance:in_sample", + "validmind.model_validation.sklearn.ClassifierPerformance:out_of_sample", + "validmind.model_validation.sklearn.PrecisionRecallCurve", + "validmind.model_validation.sklearn.ROCCurve", + "validmind.model_validation.sklearn.MinimumAccuracy", + "validmind.model_validation.sklearn.MinimumF1Score", + "validmind.model_validation.sklearn.MinimumROCAUCScore" + ] + } + }, + "validmind.model_validation.sklearn.SHAPGlobalImportance_global_importance_text": { + "context": { + "content_ids": [ + "validmind.model_validation.sklearn.SHAPGlobalImportance" + ] + } + }, + "model_weak_spots_description": { + "context": { + "content_ids": [ + "validmind.model_validation.sklearn.WeakspotsDiagnosis" + ] + } + }, + "model_overfit_regions_description": { + "context": { + "content_ids": [ + "validmind.model_validation.sklearn.OverfitDiagnosis" + ] + } + }, + "model_robustness_description": { + "context": { + "content_ids": [ + "validmind.model_validation.sklearn.RobustnessDiagnosis" + ] + } + }, + "monitoring_plan": { + "prompt": "Write two comprehensive paragraphs for the monitoring plan section." + }, + "monitoring_implementation": { + "prompt": "Write two comprehensive paragraphs for the monitoring implementation section." + }, + "governance_plan": { + "prompt": "Write two comprehensive paragraphs for the governance plan section." + } +} diff --git a/validmind/datasets/classification/customer_churn.py b/validmind/datasets/classification/customer_churn.py index df0041544..344d47b3e 100644 --- a/validmind/datasets/classification/customer_churn.py +++ b/validmind/datasets/classification/customer_churn.py @@ -2,7 +2,9 @@ # Refer to the LICENSE file in the root of this repository for details. # SPDX-License-Identifier: AGPL-3.0 AND ValidMind Commercial +import json import os +from typing import Any, Dict import pandas as pd from sklearn.model_selection import train_test_split @@ -17,6 +19,7 @@ current_path = os.path.dirname(os.path.abspath(__file__)) dataset_path = os.path.join(current_path, "datasets") +config_path = os.path.join(current_path, "config.json") drop_columns = ["RowNumber", "CustomerId", "Surname"] boolean_columns = ["Gender"] @@ -60,6 +63,35 @@ def preprocess(df): return train_df, validation_df, test_df +def get_demo_text_config() -> Dict[str, Dict[str, Any]]: + """Return text generation config for the customer churn demo. + + The config is keyed by target `content_id`. Each entry describes how + `vm.run_text_generation()` should populate that block. + + Supported fields per content ID: + - `section_id` (optional): Only needed when the content block does not + already exist in the template and should be appended to a section. + - `prompt` (optional): Per-block prompt override for generation. + - `context` (optional): Context object passed through to + `vm.run_text_generation()`. When present, it follows the same shape + expected by the library, e.g. `{"content_ids": [...]}`. + + Returns: + Dict[str, Dict[str, Any]]: A previewable text-generation configuration + dictionary for the customer churn documentation demo. + """ + with open(config_path, encoding="utf-8") as config_file: + config = json.load(config_file) + + for _, spec in config.items(): + context = spec.get("context") + if context is not None: + context.setdefault("content_ids", []) + + return config + + def get_demo_test_config(test_suite=None): """ Returns input configuration for the default documentation diff --git a/validmind/experimental/agents.py b/validmind/experimental/agents.py index 3b52fa1b5..ce0d7249f 100644 --- a/validmind/experimental/agents.py +++ b/validmind/experimental/agents.py @@ -6,9 +6,12 @@ Agent interface for all text generation tasks """ +import time + import requests from validmind.api_client import _get_api_headers, _get_url, raise_api_error +from validmind.tests.run import _get_run_metadata from validmind.utils import is_html, md_to_html from validmind.vm_models.result import TextGenerationResult @@ -35,6 +38,8 @@ def run_task( ValueError: If an unsupported task is provided requests.exceptions.RequestException: If the API request fails """ + start_time = time.perf_counter() + if task == "code_explainer" or task == "qualitative_text_generation": r = requests.post( url=_get_url(f"ai/generate/{task}"), @@ -54,10 +59,15 @@ def run_task( # Create a test result with the generated text result = TextGenerationResult( + content_id=input.get("content_id"), result_type=f"{task}", description=generated_text, title=f"Text Generation: {task}", - doc=f"Generated {task}", + metadata=_get_run_metadata(duration_seconds=time.perf_counter() - start_time), + prompt=input.get("prompt"), + section_id=input.get("section_id"), + context=input.get("context"), + _was_description_generated=True, ) if show: result.show() diff --git a/validmind/models/r_model.py b/validmind/models/r_model.py index cf36e308c..e6caaede1 100644 --- a/validmind/models/r_model.py +++ b/validmind/models/r_model.py @@ -126,8 +126,7 @@ def predict(self, new_data, return_probs=False): Converts the predicted probabilities to classes """ try: - from rpy2.robjects import conversion, default_converter - from rpy2.robjects import pandas2ri + from rpy2.robjects import conversion, default_converter, pandas2ri except ImportError: raise MissingRExtrasError() diff --git a/validmind/template.py b/validmind/template.py index d4aec2a0e..7352e8b26 100644 --- a/validmind/template.py +++ b/validmind/template.py @@ -3,7 +3,7 @@ # SPDX-License-Identifier: AGPL-3.0 AND ValidMind Commercial import uuid -from typing import Any, Dict, List, Optional, Type +from typing import Any, Dict, List, Optional, Type, Union from .html_templates.content_blocks import ( failed_content_block_html, @@ -224,6 +224,53 @@ def preview_template(template: str) -> None: display(html_content) +def _get_section_content_ids(section: Dict[str, Any]) -> List[str]: + """Get all content IDs in a section and its subsections.""" + content_ids = [ + content["content_id"] + for content in section.get("contents", []) + if content.get("content_id") + ] + + for sub_section in section.get("sections", []): + content_ids.extend(_get_section_content_ids(sub_section)) + + return content_ids + + +def get_template_content_ids( + template: Dict[str, Any], section_ids: Optional[Union[str, List[str]]] = None +) -> List[str]: + """Get content IDs for one or more template sections. + + Args: + template: A valid flat template. + section_ids: Section ID or list of section IDs. If omitted, all content + IDs in the template are returned. + + Returns: + A list of content IDs, preserving template order. + """ + sections = [section_ids] if isinstance(section_ids, str) else section_ids + section_trees = ( + ( + _convert_sections_to_section_tree(template["sections"], start_section_id=s) + for s in sections + ) + if sections + else [_convert_sections_to_section_tree(template["sections"])] + ) + + content_ids = [] + for section_tree in section_trees: + for section_node in section_tree: + for content_id in _get_section_content_ids(section_node): + if content_id not in content_ids: + content_ids.append(content_id) + + return content_ids + + def _get_section_tests(section: Dict[str, Any]) -> List[str]: """ Get all the tests in a section and its subsections. diff --git a/validmind/tests/run.py b/validmind/tests/run.py index 7e316c177..194a80bec 100644 --- a/validmind/tests/run.py +++ b/validmind/tests/run.py @@ -11,7 +11,7 @@ from typing import Any, Callable, Dict, List, Optional, Tuple, Union from uuid import uuid4 -from validmind import __version__ +from validmind.__version__ import __version__ from validmind.ai.test_descriptions import get_result_description from validmind.errors import MissingRequiredTestInputError from validmind.input_registry import input_registry diff --git a/validmind/utils.py b/validmind/utils.py index af136184e..485d3a2be 100644 --- a/validmind/utils.py +++ b/validmind/utils.py @@ -13,6 +13,7 @@ from datetime import date, datetime, time from platform import python_version from typing import Any, Awaitable, Callable, Dict, List, Optional, TypeVar +from uuid import uuid4 import matplotlib.pylab as pylab import mistune @@ -528,15 +529,19 @@ def preview_test_config(config): print(formatted_json) return + unique_suffix = uuid4().hex + content_id = f"collapsibleContent-{unique_suffix}" + function_name = f"toggleOutput_{unique_suffix}" + collapsible_html = f""" - - + + """ ipy_display(HTML(collapsible_html)) diff --git a/validmind/vm_models/html_renderer.py b/validmind/vm_models/html_renderer.py index ae2e002f5..8157c865d 100644 --- a/validmind/vm_models/html_renderer.py +++ b/validmind/vm_models/html_renderer.py @@ -19,6 +19,26 @@ class StatefulHTMLRenderer: # Plotly.js CDN URL - using a stable version PLOTLY_CDN_URL = "https://cdn.plot.ly/plotly-2.27.0.min.js" + @staticmethod + def _get_progress_css() -> str: + """Get the CSS styles required for progress bars.""" + return """ + .vm-progress-container { + margin: 10px 0; + } + + .vm-progress-description { + margin-bottom: 5px; + font-weight: bold; + } + + .vm-progress-text { + margin-top: 5px; + font-size: 0.9em; + color: #666; + } + """ + @staticmethod def render_figure( figure_data: str, key: str, metadata: Optional[Dict[str, Any]] = None @@ -226,6 +246,7 @@ def render_progress_bar( percentage = (value / max_value * 100) if max_value > 0 else 0 return f""" +
{description}
{StatefulHTMLRenderer._get_progress_css()}
{description}
str: background-color: #e9ecef !important; } - .vm-progress-container { - margin: 10px 0; - } - - .vm-progress-description { - margin-bottom: 5px; - font-weight: bold; - } - - .vm-progress-text { - margin-top: 5px; - font-size: 0.9em; - color: #666; - } + {StatefulHTMLRenderer._get_progress_css()} .vm-result-header h3 { margin: 10px 0; diff --git a/validmind/vm_models/result/result.py b/validmind/vm_models/result/result.py index ce7e502f8..90fe63a02 100644 --- a/validmind/vm_models/result/result.py +++ b/validmind/vm_models/result/result.py @@ -693,17 +693,61 @@ class TextGenerationResult(Result): name: str = "Text Generation Result" ref_id: str = None + content_id: Optional[str] = None title: Optional[str] = None doc: Optional[str] = None description: Optional[Union[str, DescriptionFuture]] = None params: Optional[Dict[str, Any]] = None metadata: Optional[Dict[str, Any]] = None + prompt: Optional[str] = None + section_id: Optional[str] = None + context: Optional[Dict[str, Any]] = None _was_description_generated: bool = False + _client_config_cache: Optional[Any] = None def __post_init__(self): if self.ref_id is None: self.ref_id = str(uuid4()) + @classmethod + def _get_client_config(cls): + """Get the client config, loading it if not cached.""" + if cls._client_config_cache is None: + api_client.reload() + cls._client_config_cache = api_client.client_config + + if cls._client_config_cache is None: + raise ValueError( + "Failed to load client config: api_client.client_config is None" + ) + + if not hasattr(cls._client_config_cache, "documentation_template"): + raise ValueError( + "Invalid client config: missing documentation_template" + ) + + return cls._client_config_cache + + def _validate_section_id_exists(self, section_id: str): + """Validate the section_id exists on the template before logging.""" + client_config = self._get_client_config() + for section in client_config.documentation_template["sections"]: + if section["id"] == section_id: + return + + raise ValueError( + f"Section with id {section_id} not found in the model's document" + ) + + def _content_block_exists(self, content_id: str) -> bool: + """Check whether a content block already exists in the template.""" + client_config = self._get_client_config() + for section in client_config.documentation_template["sections"]: + for block in section.get("contents", []): + if block.get("content_id") == content_id: + return True + return False + def __repr__(self) -> str: attrs = [ attr @@ -734,17 +778,18 @@ def __getattribute__(self, name): return super().__getattribute__(name) @property - def test_name(self) -> str: - """Get the test name, using custom title if available.""" - return self.title or test_id_to_name(self.result_id) + def test_name(self) -> None: + """Text generation results do not expose a test-style name.""" + return None def to_html(self): """Generate HTML that persists in saved notebooks.""" html_parts = [StatefulHTMLRenderer.get_base_css()] + display_title = self.title or "" html_parts.append( StatefulHTMLRenderer.render_result_header( - test_name=self.test_name, passed=None + test_name=display_title, passed=None ) ) @@ -770,11 +815,34 @@ async def log_async( self, content_id: str = None, ): - return await asyncio.gather( - update_metadata( - content_id=f"{content_id}", - text=self.description, + resolved_content_id = content_id or self.content_id + if not resolved_content_id or not isinstance(resolved_content_id, str): + raise ValueError("`content_id` must be provided to log generated text") + + resolved_section_id = self.section_id + if resolved_section_id is not None: + self._validate_section_id_exists(resolved_section_id) + elif not self._content_block_exists(resolved_content_id): + raise ValueError( + "New generated content requires `section_id` for placement" ) + + revision_name = ( + AI_REVISION_NAME + if self._was_description_generated + else DEFAULT_REVISION_NAME + ) + + resolved_content_id = ( + resolved_content_id + if "::" in resolved_content_id + else f"{resolved_content_id}::{revision_name}" + ) + + return await api_client.alog_text( + content_id=resolved_content_id, + text=self.description, + section_id=resolved_section_id, ) def log( diff --git a/validmind/vm_models/text_generation_summary.py b/validmind/vm_models/text_generation_summary.py new file mode 100644 index 000000000..8d1374866 --- /dev/null +++ b/validmind/vm_models/text_generation_summary.py @@ -0,0 +1,146 @@ +# Copyright © 2023-2026 ValidMind Inc. All rights reserved. +# Refer to the LICENSE file in the root of this repository for details. +# SPDX-License-Identifier: AGPL-3.0 AND ValidMind Commercial + +from dataclasses import dataclass +from typing import Any, Dict, List, Optional + +from ..utils import display, md_to_html +from .html_renderer import StatefulHTMLRenderer +from .result import TextGenerationResult + + +@dataclass +class DocumentationTextSummary: + """Notebook-friendly summary for generated documentation text.""" + + title: str + description: str + results: Dict[str, TextGenerationResult] + template_sections: Optional[List[Dict[str, Any]]] = None + show_link: bool = True + + def display(self): + """Display the summary.""" + display(self.to_html()) + + def _get_section_map(self) -> Dict[str, Dict[str, Any]]: + return { + section["id"]: section + for section in self.template_sections or [] + if section.get("id") + } + + def _get_top_level_section_id(self, section_id: str) -> str: + section_map = self._get_section_map() + current_section_id = section_id + + while current_section_id in section_map: + parent_section_id = section_map[current_section_id].get("parent_section") + if not parent_section_id: + return current_section_id + current_section_id = parent_section_id + + return section_id + + def _get_section_title(self, section_id: str) -> str: + if section_id == "_ungrouped": + return "Other Generated Text" + + top_level_section_id = self._get_top_level_section_id(section_id) + + for section in self.template_sections or []: + if section.get("id") == top_level_section_id: + return section.get("title", top_level_section_id) + + return top_level_section_id + + def _get_result_section_id( + self, content_id: str, result: TextGenerationResult + ) -> str: + if result.section_id: + return self._get_top_level_section_id(result.section_id) + + for section in self.template_sections or []: + for content in section.get("contents", []): + if content.get("content_id") == content_id: + return self._get_top_level_section_id(section["id"]) + + return "_ungrouped" + + def _group_results_by_section(self) -> Dict[str, Dict[str, TextGenerationResult]]: + grouped_results: Dict[str, Dict[str, TextGenerationResult]] = {} + + for content_id, result in self.results.items(): + section_id = self._get_result_section_id(content_id, result) + grouped_results.setdefault(section_id, {})[content_id] = result + + return grouped_results + + def to_html(self) -> str: + """Generate HTML representation of the summary.""" + html_parts = [StatefulHTMLRenderer.get_base_css()] + + title_html = f""" +

Generated Documentation Text: {self.title}


+ """ + html_parts.append(title_html) + + if self.show_link: + from ..api_client import get_api_host, get_api_model + + api_host = get_api_host() + api_model = get_api_model() + if api_host and api_model: + ui_host = api_host.replace("/api/v1/tracking", "").replace("api", "app") + if not ui_host.endswith("/"): + ui_host = f"{ui_host}/" + link = f"{ui_host}model-inventory/{api_model}" + results_link_html = f""" +

+ Check out the updated documentation on + ValidMind. +

+ """ + html_parts.append(results_link_html) + + if self.description: + html_parts.append( + f'
{md_to_html(self.description)}
' + ) + + if self.results: + grouped_results = self._group_results_by_section() + section_items = [] + section_titles = [] + + ordered_section_ids = [ + section["id"] + for section in self.template_sections or [] + if section.get("id") in grouped_results + ] + for section_id in grouped_results: + if section_id not in ordered_section_ids: + ordered_section_ids.append(section_id) + + for section_id in ordered_section_ids: + section_results = grouped_results[section_id] + accordion_items = [] + accordion_titles = [] + + for content_id, result in section_results.items(): + accordion_items.append(result.to_html()) + accordion_titles.append(f"Text Block: '{content_id}'") + + section_items.append( + StatefulHTMLRenderer.render_accordion( + accordion_items, accordion_titles + ) + ) + section_titles.append(self._get_section_title(section_id)) + + html_parts.append( + StatefulHTMLRenderer.render_accordion(section_items, section_titles) + ) + + return f'
{"".join(html_parts)}
'