{
 "cells": [
  {
   "cell_type": "markdown",
   "id": "reranker-cell-00",
   "metadata": {},
   "source": [
    "# Multilingual Relevance Reranker — buyer quickstart\n",
    "\n",
    "Subscribe through the [AWS Marketplace listing](https://aws.amazon.com/marketplace/pp/prodview-zv7hrxosynwvu).\n",
    "On **Continue to configuration**, choose **us-east-1**, copy the regional product/model-package ARN,\n",
    "and set `SAGEMAKER_MARKETPLACE_MODEL_PACKAGE_ARN` in your own environment or paste it into\n",
    "`PACKAGE_ARN` below. Use the ARN provided for your subscription; the seller's private\n",
    "qualification package is not a buyer deployment default. Review the release status and\n",
    "software terms before accepting any offer. This unexecuted notebook contains no credentials.\n",
    "\n",
    "Qualification measurements are for us-east-1. Other catalog regions have not been qualified\n",
    "by Software Sushi. The component orders caller-supplied passages; raw logits are not\n",
    "probabilities or proof of a complete answer. Apply tenant, permission and current-version\n",
    "filters before invocation, and inspect the original sources.\n",
    "\n",
    "Creating resources incurs AWS charges, including during a software trial. The\n",
    "2026-11-15 us-east-1 ml.g5.xlarge compute rate was USD1.408/hour; software and other\n",
    "AWS services are separate. Delete test compute when finished. All creation, invocation,\n",
    "batch and cleanup flags below are disabled by default.\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "reranker-cell-01",
   "metadata": {},
   "outputs": [],
   "source": [
    "import json\n",
    "import os\n",
    "import time\n",
    "\n",
    "import boto3\n",
    "from botocore.config import Config\n",
    "\n",
    "REGION = \"us-east-1\"\n",
    "PACKAGE_ARN = os.environ.get(\"SAGEMAKER_MARKETPLACE_MODEL_PACKAGE_ARN\", \"\")\n",
    "EXPECTED_IMAGE_DIGEST = \"sha256:506dd8f807a30844a98a03d0bc52efddf392c2971088063efaf7d4939b2ec85c\"\n",
    "EXECUTION_ROLE_ARN = \"REPLACE_WITH_YOUR_SAGEMAKER_EXECUTION_ROLE_ARN\"\n",
    "CREATE_RESOURCES = False  # Explicit opt-in after reviewing costs and IAM.\n",
    "INVOKE_ENDPOINT = False  # Set true only after your chosen endpoint is ready.\n",
    "ENDPOINT_NAME = \"ss-reranker-buyer-\" + str(int(time.time()))\n",
    "MODEL_NAME = ENDPOINT_NAME\n",
    "CONFIG_NAME = ENDPOINT_NAME\n",
    "session = boto3.Session(region_name=REGION)\n",
    "sm = session.client(\"sagemaker\")\n",
    "runtime = session.client(\n",
    "    \"sagemaker-runtime\", config=Config(connect_timeout=10, read_timeout=65, retries={\"total_max_attempts\": 1})\n",
    ")\n",
    "\n",
    "\n",
    "def validate_subscription_configuration():\n",
    "    if not PACKAGE_ARN.startswith(\"arn:aws:sagemaker:\" + REGION + \":\"):\n",
    "        raise ValueError(\"Copy the subscribed regional product ARN from Marketplace configuration\")\n",
    "    if not EXECUTION_ROLE_ARN.startswith(\"arn:aws:iam::\"):\n",
    "        raise ValueError(\"Supply your own SageMaker execution role ARN\")\n",
    "    package = sm.describe_model_package(ModelPackageName=PACKAGE_ARN)\n",
    "    containers = package.get(\"InferenceSpecification\", {}).get(\"Containers\", [])\n",
    "    if (\n",
    "        package.get(\"ModelPackageStatus\") != \"Completed\"\n",
    "        or len(containers) != 1\n",
    "        or not containers[0].get(\"ProductId\")\n",
    "        or (containers[0].get(\"ImageDigest\") or containers[0].get(\"Image\", \"\").partition(\"@\")[2])\n",
    "        != EXPECTED_IMAGE_DIGEST\n",
    "    ):\n",
    "        raise ValueError(\"The package is not the completed subscribed Marketplace product; check its regional ARN\")"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "reranker-cell-02",
   "metadata": {},
   "source": [
    "## Create your endpoint\n",
    "\n",
    "Provide your own execution role with the required SageMaker/Marketplace image\n",
    "permissions. Do not paste access keys into this notebook. The explicit AMI is\n",
    "required: the default g5 driver 470 cannot run this CUDA 13 image. Keep network\n",
    "isolation enabled. This example does not enable request data capture.\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "reranker-cell-03",
   "metadata": {},
   "outputs": [],
   "source": [
    "if CREATE_RESOURCES:\n",
    "    validate_subscription_configuration()\n",
    "    sm.create_model(\n",
    "        ModelName=MODEL_NAME,\n",
    "        ExecutionRoleArn=EXECUTION_ROLE_ARN,\n",
    "        EnableNetworkIsolation=True,\n",
    "        PrimaryContainer={\"ModelPackageName\": PACKAGE_ARN},\n",
    "    )\n",
    "    sm.create_endpoint_config(\n",
    "        EndpointConfigName=CONFIG_NAME,\n",
    "        ProductionVariants=[\n",
    "            {\n",
    "                \"VariantName\": \"AllTraffic\",\n",
    "                \"ModelName\": MODEL_NAME,\n",
    "                \"InitialInstanceCount\": 1,\n",
    "                \"InstanceType\": \"ml.g5.xlarge\",\n",
    "                \"InitialVariantWeight\": 1,\n",
    "                \"InferenceAmiVersion\": \"al2023-ami-sagemaker-inference-gpu-4-1\",\n",
    "                \"ContainerStartupHealthCheckTimeoutInSeconds\": 600,\n",
    "                \"ModelDataDownloadTimeoutInSeconds\": 1200,\n",
    "            }\n",
    "        ],\n",
    "    )\n",
    "    sm.create_endpoint(EndpointName=ENDPOINT_NAME, EndpointConfigName=CONFIG_NAME)\n",
    "    sm.get_waiter(\"endpoint_in_service\").wait(EndpointName=ENDPOINT_NAME, WaiterConfig={\"Delay\": 30, \"MaxAttempts\": 40})\n",
    "else:\n",
    "    print(\"No AWS resources created. Configure an existing endpoint or explicitly opt in above.\")"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "reranker-cell-04",
   "metadata": {},
   "source": [
    "## Rank a fictional passage pool\n",
    "\n",
    "One JSON object, 1..16 passages and 512 complete-pair tokens maximum. Both query\n",
    "and passage consume the budget. Inputs that exceed it are rejected, not truncated.\n",
    "Set ENDPOINT_NAME to your own ready endpoint before invoking. Results preserve\n",
    "IDs and zero-based input indexes. Do not use a score threshold as a missing-answer\n",
    "detector. Review the original source before using the context downstream.\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "reranker-cell-05",
   "metadata": {},
   "outputs": [],
   "source": [
    "request = {\n",
    "    \"query\": \"How do I update my billing address?\",\n",
    "    \"documents\": [\n",
    "        {\"id\": \"billing_steps\", \"text\": \"Open Settings, choose Billing, edit the address, and save your changes.\"},\n",
    "        {\"id\": \"delivery\", \"text\": \"Delivery tracking appears in your order history.\"},\n",
    "    ],\n",
    "    \"top_k\": 2,\n",
    "}\n",
    "if INVOKE_ENDPOINT:\n",
    "    result = runtime.invoke_endpoint(\n",
    "        EndpointName=ENDPOINT_NAME,\n",
    "        ContentType=\"application/json\",\n",
    "        Accept=\"application/json\",\n",
    "        Body=json.dumps(request).encode(\"utf-8\"),\n",
    "    )\n",
    "    output = json.loads(result[\"Body\"].read())\n",
    "    assert output[\"model\"] == \"multilingual-reranker\" and output[\"version\"] == \"1.0.0\"\n",
    "    assert output[\"review_required\"] is True and output[\"answerability_measured\"] is False\n",
    "    for row in output[\"results\"]:\n",
    "        assert request[\"documents\"][row[\"index\"]][\"id\"] == row[\"id\"]\n",
    "    print(json.dumps(output, indent=2, ensure_ascii=False))"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "reranker-cell-06",
   "metadata": {},
   "source": [
    "## Run a line-split batch job\n",
    "\n",
    "Download [sample_batch.jsonl](https://ss-reranker-media-637423513289-us-east-1.s3.us-east-1.amazonaws.com/multilingual-reranker/1.0.0/sample_batch.jsonl)\n",
    "and upload it to a private encrypted S3 bucket in your own account. Set the input object and\n",
    "output prefix below. Use a dedicated output prefix and retain output/input line order.\n",
    "The notebook does not upload your data or create/reconfigure any bucket.\n",
    "\n",
    "The qualified batch instance is `ml.g6e.xlarge`, with the explicit driver-535 batch AMI\n",
    "and compatibility setting below. Your account needs the appropriate SageMaker quota;\n",
    "quota does not guarantee capacity. After reviewing costs, set `START_BATCH=True` to create\n",
    "one model and batch job. The waiter is bounded to one hour; a waiter timeout does not stop\n",
    "a running job. Inspect its status and use the explicit cleanup cell after failures.\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "7fb27b941602401d91542211134fc71a",
   "metadata": {},
   "outputs": [],
   "source": [
    "START_BATCH = False\n",
    "BATCH_INPUT_S3_URI = \"s3://YOUR_PRIVATE_BUCKET/reranker/input/sample_batch.jsonl\"\n",
    "BATCH_OUTPUT_S3_URI = \"s3://YOUR_PRIVATE_BUCKET/reranker/output/\"\n",
    "BATCH_MODEL_NAME = ENDPOINT_NAME + \"-batch\"\n",
    "BATCH_JOB_NAME = ENDPOINT_NAME + \"-batch\"\n",
    "if START_BATCH:\n",
    "    validate_subscription_configuration()\n",
    "    if (\n",
    "        not BATCH_INPUT_S3_URI.startswith(\"s3://\")\n",
    "        or not BATCH_OUTPUT_S3_URI.startswith(\"s3://\")\n",
    "        or \"YOUR_PRIVATE_BUCKET\" in BATCH_INPUT_S3_URI + BATCH_OUTPUT_S3_URI\n",
    "        or BATCH_INPUT_S3_URI == BATCH_OUTPUT_S3_URI\n",
    "    ):\n",
    "        raise ValueError(\"Supply your authorized private input object and a separate output prefix\")\n",
    "    sm.create_model(\n",
    "        ModelName=BATCH_MODEL_NAME,\n",
    "        ExecutionRoleArn=EXECUTION_ROLE_ARN,\n",
    "        EnableNetworkIsolation=True,\n",
    "        PrimaryContainer={\"ModelPackageName\": PACKAGE_ARN},\n",
    "    )\n",
    "    sm.create_transform_job(\n",
    "        TransformJobName=BATCH_JOB_NAME,\n",
    "        ModelName=BATCH_MODEL_NAME,\n",
    "        BatchStrategy=\"SingleRecord\",\n",
    "        MaxConcurrentTransforms=1,\n",
    "        MaxPayloadInMB=1,\n",
    "        ModelClientConfig={\"InvocationsTimeoutInSeconds\": 60, \"InvocationsMaxRetries\": 0},\n",
    "        Environment={\"SAGEMAKER_CUDA_COMPAT_DISABLED\": \"true\"},\n",
    "        TransformInput={\n",
    "            \"DataSource\": {\"S3DataSource\": {\"S3DataType\": \"S3Prefix\", \"S3Uri\": BATCH_INPUT_S3_URI}},\n",
    "            \"ContentType\": \"application/jsonlines\",\n",
    "            \"SplitType\": \"Line\",\n",
    "        },\n",
    "        TransformOutput={\n",
    "            \"S3OutputPath\": BATCH_OUTPUT_S3_URI,\n",
    "            \"Accept\": \"application/jsonlines\",\n",
    "            \"AssembleWith\": \"Line\",\n",
    "        },\n",
    "        TransformResources={\n",
    "            \"InstanceType\": \"ml.g6e.xlarge\",\n",
    "            \"InstanceCount\": 1,\n",
    "            \"TransformAmiVersion\": \"al2-ami-sagemaker-batch-gpu-535\",\n",
    "        },\n",
    "    )\n",
    "    sm.get_waiter(\"transform_job_completed_or_stopped\").wait(\n",
    "        TransformJobName=BATCH_JOB_NAME, WaiterConfig={\"Delay\": 30, \"MaxAttempts\": 120}\n",
    "    )\n",
    "    job = sm.describe_transform_job(TransformJobName=BATCH_JOB_NAME)\n",
    "    if job[\"TransformJobStatus\"] != \"Completed\":\n",
    "        raise RuntimeError(\"Batch did not complete; inspect its redacted failure reason and clean up\")\n",
    "    print({\"status\": job[\"TransformJobStatus\"], \"output\": job[\"TransformOutput\"][\"S3OutputPath\"]})\n",
    "else:\n",
    "    print(\"No batch resources created. Configure your private S3 locations and explicitly opt in.\")"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "acae54e37e7d407bbb7b55eff062a284",
   "metadata": {},
   "source": [
    "## Delete test resources\n",
    "\n",
    "Confirm the exact endpoint, configuration, batch job and model names before enabling\n",
    "these flags. Endpoint deletion releases paid hosting; model/configuration deletion removes\n",
    "metadata. Cleanup preserves S3 inputs and outputs. If a creation call failed halfway through,\n",
    "inspect your AWS account and remove partially created resources by their exact names.\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "reranker-cell-07",
   "metadata": {},
   "outputs": [],
   "source": [
    "DELETE_TEST_RESOURCES = False\n",
    "if DELETE_TEST_RESOURCES:\n",
    "    description = sm.describe_endpoint(EndpointName=ENDPOINT_NAME)\n",
    "    if description[\"EndpointConfigName\"] != CONFIG_NAME:\n",
    "        raise ValueError(\"Endpoint configuration differs; inspect before deleting\")\n",
    "    configuration = sm.describe_endpoint_config(EndpointConfigName=CONFIG_NAME)\n",
    "    if any(v[\"ModelName\"] != MODEL_NAME for v in configuration[\"ProductionVariants\"]):\n",
    "        raise ValueError(\"Model identity differs; inspect before deleting\")\n",
    "    sm.delete_endpoint(EndpointName=ENDPOINT_NAME)\n",
    "    sm.get_waiter(\"endpoint_deleted\").wait(EndpointName=ENDPOINT_NAME, WaiterConfig={\"Delay\": 15, \"MaxAttempts\": 80})\n",
    "    sm.delete_endpoint_config(EndpointConfigName=CONFIG_NAME)\n",
    "    sm.delete_model(ModelName=MODEL_NAME)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "9a63283cbaf04dbcab1f6479b197f3a8",
   "metadata": {},
   "outputs": [],
   "source": [
    "DELETE_BATCH_TEST_RESOURCES = False\n",
    "if DELETE_BATCH_TEST_RESOURCES:\n",
    "    job = sm.describe_transform_job(TransformJobName=BATCH_JOB_NAME)\n",
    "    if job[\"ModelName\"] != BATCH_MODEL_NAME:\n",
    "        raise ValueError(\"Batch model identity differs; inspect before cleanup\")\n",
    "    if job[\"TransformJobStatus\"] in (\"InProgress\", \"Stopping\"):\n",
    "        if job[\"TransformJobStatus\"] == \"InProgress\":\n",
    "            sm.stop_transform_job(TransformJobName=BATCH_JOB_NAME)\n",
    "        sm.get_waiter(\"transform_job_completed_or_stopped\").wait(\n",
    "            TransformJobName=BATCH_JOB_NAME, WaiterConfig={\"Delay\": 15, \"MaxAttempts\": 120}\n",
    "        )\n",
    "    sm.delete_model(ModelName=BATCH_MODEL_NAME)"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "name": "python"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
