|
| 1 | +{ |
| 2 | + "cells": [ |
| 3 | + { |
| 4 | + "cell_type": "code", |
| 5 | + "execution_count": null, |
| 6 | + "id": "58d4d40a", |
| 7 | + "metadata": {}, |
| 8 | + "outputs": [ |
| 9 | + { |
| 10 | + "name": "stdout", |
| 11 | + "output_type": "stream", |
| 12 | + "text": [ |
| 13 | + "Defaulting to user installation because normal site-packages is not writeable\n", |
| 14 | + "Collecting isaacus\n", |
| 15 | + " Downloading isaacus-0.9.1-py3-none-any.whl.metadata (15 kB)\n", |
| 16 | + "Collecting isaacus-sagemaker\n", |
| 17 | + " Downloading isaacus_sagemaker-0.1.2-py3-none-any.whl.metadata (9.8 kB)\n", |
| 18 | + "Requirement already satisfied: anyio<5,>=3.5.0 in /usr/lib/python3/dist-packages (from isaacus) (4.2.0)\n", |
| 19 | + "Requirement already satisfied: distro<2,>=1.7.0 in /usr/lib/python3/dist-packages (from isaacus) (1.9.0)\n", |
| 20 | + "Collecting httpx<1,>=0.23.0 (from isaacus)\n", |
| 21 | + " Downloading httpx-0.28.1-py3-none-any.whl.metadata (7.1 kB)\n", |
| 22 | + "Requirement already satisfied: pydantic<3,>=1.9.0 in /usr/lib/python3/dist-packages (from isaacus) (1.10.14)\n", |
| 23 | + "Requirement already satisfied: sniffio in /usr/lib/python3/dist-packages (from isaacus) (1.3.0)\n", |
| 24 | + "Requirement already satisfied: typing-extensions<5,>=4.10 in /usr/lib/python3/dist-packages (from isaacus) (4.10.0)\n", |
| 25 | + "Collecting boto3>=1.40.49 (from isaacus-sagemaker)\n", |
| 26 | + " Downloading boto3-1.40.59-py3-none-any.whl.metadata (6.6 kB)\n", |
| 27 | + "Collecting msgspec>=0.19.0 (from isaacus-sagemaker)\n", |
| 28 | + " Downloading msgspec-0.19.0-cp312-cp312-manylinux_2_17_x86_64.manylinux2014_x86_64.whl.metadata (6.9 kB)\n", |
| 29 | + "Collecting orjson>=3.11.4 (from isaacus-sagemaker)\n", |
| 30 | + " Downloading orjson-3.11.4-cp312-cp312-manylinux_2_17_x86_64.manylinux2014_x86_64.whl.metadata (41 kB)\n", |
| 31 | + "\u001b[2K \u001b[90m━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━\u001b[0m \u001b[32m41.8/41.8 kB\u001b[0m \u001b[31m14.6 MB/s\u001b[0m eta \u001b[36m0:00:00\u001b[0m\n", |
| 32 | + "\u001b[?25hCollecting pytest-asyncio>=1.2.0 (from isaacus-sagemaker)\n", |
| 33 | + " Downloading pytest_asyncio-1.2.0-py3-none-any.whl.metadata (4.1 kB)\n", |
| 34 | + "Collecting pytest>=8.4.2 (from isaacus-sagemaker)\n", |
| 35 | + " Downloading pytest-8.4.2-py3-none-any.whl.metadata (7.7 kB)\n", |
| 36 | + "Requirement already satisfied: idna>=2.8 in /usr/lib/python3/dist-packages (from anyio<5,>=3.5.0->isaacus) (3.6)\n", |
| 37 | + "Collecting botocore<1.41.0,>=1.40.59 (from boto3>=1.40.49->isaacus-sagemaker)\n", |
| 38 | + " Downloading botocore-1.40.59-py3-none-any.whl.metadata (5.7 kB)\n", |
| 39 | + "Collecting jmespath<2.0.0,>=0.7.1 (from boto3>=1.40.49->isaacus-sagemaker)\n", |
| 40 | + " Downloading jmespath-1.0.1-py3-none-any.whl.metadata (7.6 kB)\n", |
| 41 | + "Collecting s3transfer<0.15.0,>=0.14.0 (from boto3>=1.40.49->isaacus-sagemaker)\n", |
| 42 | + " Downloading s3transfer-0.14.0-py3-none-any.whl.metadata (1.7 kB)\n", |
| 43 | + "Requirement already satisfied: certifi in /usr/lib/python3/dist-packages (from httpx<1,>=0.23.0->isaacus) (2023.11.17)\n", |
| 44 | + "Collecting httpcore==1.* (from httpx<1,>=0.23.0->isaacus)\n", |
| 45 | + " Downloading httpcore-1.0.9-py3-none-any.whl.metadata (21 kB)\n", |
| 46 | + "Collecting h11>=0.16 (from httpcore==1.*->httpx<1,>=0.23.0->isaacus)\n", |
| 47 | + " Downloading h11-0.16.0-py3-none-any.whl.metadata (8.3 kB)\n", |
| 48 | + "Collecting iniconfig>=1 (from pytest>=8.4.2->isaacus-sagemaker)\n", |
| 49 | + " Downloading iniconfig-2.3.0-py3-none-any.whl.metadata (2.5 kB)\n", |
| 50 | + "Requirement already satisfied: packaging>=20 in /usr/lib/python3/dist-packages (from pytest>=8.4.2->isaacus-sagemaker) (24.0)\n", |
| 51 | + "Collecting pluggy<2,>=1.5 (from pytest>=8.4.2->isaacus-sagemaker)\n", |
| 52 | + " Downloading pluggy-1.6.0-py3-none-any.whl.metadata (4.8 kB)\n", |
| 53 | + "Requirement already satisfied: pygments>=2.7.2 in /usr/lib/python3/dist-packages (from pytest>=8.4.2->isaacus-sagemaker) (2.17.2)\n", |
| 54 | + "Collecting typing-extensions<5,>=4.10 (from isaacus)\n", |
| 55 | + " Downloading typing_extensions-4.15.0-py3-none-any.whl.metadata (3.3 kB)\n", |
| 56 | + "Requirement already satisfied: python-dateutil<3.0.0,>=2.1 in /usr/lib/python3/dist-packages (from botocore<1.41.0,>=1.40.59->boto3>=1.40.49->isaacus-sagemaker) (2.8.2)\n", |
| 57 | + "Requirement already satisfied: urllib3!=2.2.0,<3,>=1.25.4 in /usr/lib/python3/dist-packages (from botocore<1.41.0,>=1.40.59->boto3>=1.40.49->isaacus-sagemaker) (2.0.7)\n", |
| 58 | + "Downloading isaacus-0.9.1-py3-none-any.whl (90 kB)\n", |
| 59 | + "\u001b[2K \u001b[90m━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━\u001b[0m \u001b[32m90.9/90.9 kB\u001b[0m \u001b[31m56.4 MB/s\u001b[0m eta \u001b[36m0:00:00\u001b[0m\n", |
| 60 | + "\u001b[?25hDownloading isaacus_sagemaker-0.1.2-py3-none-any.whl (9.9 kB)\n", |
| 61 | + "Downloading boto3-1.40.59-py3-none-any.whl (139 kB)\n", |
| 62 | + "\u001b[2K \u001b[90m━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━\u001b[0m \u001b[32m139.3/139.3 kB\u001b[0m \u001b[31m85.2 MB/s\u001b[0m eta \u001b[36m0:00:00\u001b[0m\n", |
| 63 | + "\u001b[?25hDownloading httpx-0.28.1-py3-none-any.whl (73 kB)\n", |
| 64 | + "\u001b[2K \u001b[90m━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━\u001b[0m \u001b[32m73.5/73.5 kB\u001b[0m \u001b[31m46.0 MB/s\u001b[0m eta \u001b[36m0:00:00\u001b[0m\n", |
| 65 | + "\u001b[?25hDownloading httpcore-1.0.9-py3-none-any.whl (78 kB)\n", |
| 66 | + "\u001b[2K \u001b[90m━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━\u001b[0m \u001b[32m78.8/78.8 kB\u001b[0m \u001b[31m33.5 MB/s\u001b[0m eta \u001b[36m0:00:00\u001b[0m\n", |
| 67 | + "\u001b[?25hDownloading msgspec-0.19.0-cp312-cp312-manylinux_2_17_x86_64.manylinux2014_x86_64.whl (213 kB)\n", |
| 68 | + "\u001b[2K \u001b[90m━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━\u001b[0m \u001b[32m213.6/213.6 kB\u001b[0m \u001b[31m45.6 MB/s\u001b[0m eta \u001b[36m0:00:00\u001b[0m\n", |
| 69 | + "\u001b[?25hDownloading orjson-3.11.4-cp312-cp312-manylinux_2_17_x86_64.manylinux2014_x86_64.whl (136 kB)\n", |
| 70 | + "\u001b[2K \u001b[90m━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━\u001b[0m \u001b[32m136.3/136.3 kB\u001b[0m \u001b[31m60.1 MB/s\u001b[0m eta \u001b[36m0:00:00\u001b[0m\n", |
| 71 | + "\u001b[?25hDownloading pytest-8.4.2-py3-none-any.whl (365 kB)\n", |
| 72 | + "\u001b[2K \u001b[90m━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━\u001b[0m \u001b[32m365.8/365.8 kB\u001b[0m \u001b[31m160.5 MB/s\u001b[0m eta \u001b[36m0:00:00\u001b[0m\n", |
| 73 | + "\u001b[?25hDownloading pytest_asyncio-1.2.0-py3-none-any.whl (15 kB)\n", |
| 74 | + "Downloading typing_extensions-4.15.0-py3-none-any.whl (44 kB)\n", |
| 75 | + "\u001b[2K \u001b[90m━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━\u001b[0m \u001b[32m44.6/44.6 kB\u001b[0m \u001b[31m37.9 MB/s\u001b[0m eta \u001b[36m0:00:00\u001b[0m\n", |
| 76 | + "\u001b[?25hDownloading botocore-1.40.59-py3-none-any.whl (14.1 MB)\n", |
| 77 | + "\u001b[2K \u001b[90m━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━\u001b[0m \u001b[32m14.1/14.1 MB\u001b[0m \u001b[31m168.8 MB/s\u001b[0m eta \u001b[36m0:00:00\u001b[0m00:01\u001b[0m0:01\u001b[0m\n", |
| 78 | + "\u001b[?25hDownloading iniconfig-2.3.0-py3-none-any.whl (7.5 kB)\n", |
| 79 | + "Downloading jmespath-1.0.1-py3-none-any.whl (20 kB)\n", |
| 80 | + "Downloading pluggy-1.6.0-py3-none-any.whl (20 kB)\n", |
| 81 | + "Downloading s3transfer-0.14.0-py3-none-any.whl (85 kB)\n", |
| 82 | + "\u001b[2K \u001b[90m━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━\u001b[0m \u001b[32m85.7/85.7 kB\u001b[0m \u001b[31m69.9 MB/s\u001b[0m eta \u001b[36m0:00:00\u001b[0m\n", |
| 83 | + "\u001b[?25hDownloading h11-0.16.0-py3-none-any.whl (37 kB)\n", |
| 84 | + "Installing collected packages: typing-extensions, pluggy, orjson, msgspec, jmespath, iniconfig, h11, pytest, httpcore, botocore, s3transfer, pytest-asyncio, httpx, isaacus, boto3, isaacus-sagemaker\n", |
| 85 | + "\u001b[31mERROR: pip's dependency resolver does not currently take into account all the packages that are installed. This behaviour is the source of the following dependency conflicts.\n", |
| 86 | + "fastapi 0.101.0 requires starlette<0.28.0,>=0.27.0, but you have starlette 0.31.1 which is incompatible.\u001b[0m\u001b[31m\n", |
| 87 | + "\u001b[0mSuccessfully installed boto3-1.40.59 botocore-1.40.59 h11-0.16.0 httpcore-1.0.9 httpx-0.28.1 iniconfig-2.3.0 isaacus-0.9.1 isaacus-sagemaker-0.1.2 jmespath-1.0.1 msgspec-0.19.0 orjson-3.11.4 pluggy-1.6.0 pytest-8.4.2 pytest-asyncio-1.2.0 s3transfer-0.14.0 typing-extensions-4.15.0\n" |
| 88 | + ] |
| 89 | + } |
| 90 | + ], |
| 91 | + "source": [ |
| 92 | + "# Install the Isaacus SDK and Isaacus SageMaker Python integration.\n", |
| 93 | + "!pip install isaacus isaacus-sagemaker\n", |
| 94 | + "\n", |
| 95 | + "# Install optional dependencies necessary for this example (`httpx` and `numpy`).\n", |
| 96 | + "!pip install httpx numpy" |
| 97 | + ] |
| 98 | + }, |
| 99 | + { |
| 100 | + "cell_type": "code", |
| 101 | + "execution_count": null, |
| 102 | + "id": "d5cf275c", |
| 103 | + "metadata": {}, |
| 104 | + "outputs": [], |
| 105 | + "source": [ |
| 106 | + "from isaacus import Isaacus, AsyncIsaacus\n", |
| 107 | + "from isaacus_sagemaker import IsaacusSageMakerRuntimeHTTPClient, AsyncIsaacusSageMakerRuntimeHTTPClient, \\\n", |
| 108 | + " IsaacusSageMakerRuntimeEndpoint\n", |
| 109 | + "\n", |
| 110 | + "# NOTE This assumes you have already purchased Kanon 2 Embedder and set up an endpoint.\n", |
| 111 | + "endpoints = [\n", |
| 112 | + " IsaacusSageMakerRuntimeEndpoint(\n", |
| 113 | + " name=\"my-sagemaker-endpoint\",\n", |
| 114 | + " # region=\"us-west-2\", # Optional, defaults to the client or AWS SDK default region\n", |
| 115 | + " # profile=\"my-aws-profile\", # Optional, defaults to the client or AWS SDK default profile\n", |
| 116 | + " # models=[\"kanon-2-embedder\"], # Optional, models supported by this endpoint,\n", |
| 117 | + " # # defaults to all models\n", |
| 118 | + " )\n", |
| 119 | + "]\n", |
| 120 | + "\n", |
| 121 | + "client = Isaacus(\n", |
| 122 | + " http_client=IsaacusSageMakerRuntimeHTTPClient(\n", |
| 123 | + " endpoints=endpoints,\n", |
| 124 | + " # region=\"us-west-2\", # Optional, defaults to AWS SDK default region\n", |
| 125 | + " # profile=\"my-aws-profile\", # Optional, defaults to AWS SDK default profile\n", |
| 126 | + " # boto_session_kwargs={\"aws_access_key_id\": \"...\",}, # Optional, additional boto3 session kwargs\n", |
| 127 | + " # **{}, # Optional, additional httpx.Client kwargs\n", |
| 128 | + " )\n", |
| 129 | + ")\n", |
| 130 | + "\n", |
| 131 | + "# For asynchronous usage:\n", |
| 132 | + "aclient = AsyncIsaacus(\n", |
| 133 | + " http_client=AsyncIsaacusSageMakerRuntimeHTTPClient(\n", |
| 134 | + " endpoints=endpoints,\n", |
| 135 | + " # region=\"us-west-2\", # Optional, defaults to AWS SDK default region\n", |
| 136 | + " # profile=\"my-aws-profile\", # Optional, defaults to AWS SDK default profile\n", |
| 137 | + " # boto_session_kwargs={\"aws_access_key_id\": \"...\",}, # Optional, additional boto3 session kwargs\n", |
| 138 | + " # **{}, # Optional, additional httpx.AsyncClient kwargs\n", |
| 139 | + " )\n", |
| 140 | + ")" |
| 141 | + ] |
| 142 | + }, |
| 143 | + { |
| 144 | + "cell_type": "code", |
| 145 | + "execution_count": null, |
| 146 | + "id": "87b1a88c", |
| 147 | + "metadata": {}, |
| 148 | + "outputs": [], |
| 149 | + "source": [ |
| 150 | + "import httpx\n", |
| 151 | + "import numpy as np\n", |
| 152 | + "\n", |
| 153 | + "# Download the GitHub terms of service as an example.\n", |
| 154 | + "tos = httpx.get(\"https://examples.isaacus.com/github-tos.md\").text\n", |
| 155 | + "\n", |
| 156 | + "# Embed the terms of service.\n", |
| 157 | + "document_response = client.embeddings.create(\n", |
| 158 | + " model=\"kanon-2-embedder\",\n", |
| 159 | + " texts=tos, # You can pass a single text or a list of texts here.\n", |
| 160 | + " task=\"retrieval/document\",\n", |
| 161 | + " # dimensions=1792, # You may optionally wish to specify a lower dimension.\n", |
| 162 | + ")\n", |
| 163 | + "\n", |
| 164 | + "# Embed our search queries.\n", |
| 165 | + "query_responses = client.embeddings.create(\n", |
| 166 | + " model=\"kanon-2-embedder\",\n", |
| 167 | + " texts=[\n", |
| 168 | + " \"What are GitHub's billing policies?\", # This is a relevant query.\n", |
| 169 | + " \"What are Microsoft's billing policies?\", # This is an irrelevant query.\n", |
| 170 | + " ],\n", |
| 171 | + " task=\"retrieval/query\",\n", |
| 172 | + " # dimensions=1792, # You may optionally wish to specify a lower dimension.\n", |
| 173 | + ")\n", |
| 174 | + "\n", |
| 175 | + "# Unpack the embeddings.\n", |
| 176 | + "document_embedding = document_response.embeddings[0].embedding\n", |
| 177 | + "\n", |
| 178 | + "query_embeddings = query_responses.embeddings\n", |
| 179 | + "relevant_query_embedding = query_embeddings[0].embedding\n", |
| 180 | + "irrelevant_query_embedding = query_embeddings[1].embedding\n", |
| 181 | + "\n", |
| 182 | + "# Compute the similarity between the queries and the document.\n", |
| 183 | + "relevant_similarity = np.dot(relevant_query_embedding, document_embedding)\n", |
| 184 | + "irrelevant_similarity = np.dot(irrelevant_query_embedding, document_embedding)\n", |
| 185 | + "\n", |
| 186 | + "# Log the results.\n", |
| 187 | + "print(f\"Similarity of relevant query to the document: {relevant_similarity * 100:.2f}\")\n", |
| 188 | + "print(f\"Similarity of irrelevant query to the document: {irrelevant_similarity * 100:.2f}\")" |
| 189 | + ] |
| 190 | + } |
| 191 | + ], |
| 192 | + "metadata": { |
| 193 | + "kernelspec": { |
| 194 | + "display_name": ".venv", |
| 195 | + "language": "python", |
| 196 | + "name": "python3" |
| 197 | + }, |
| 198 | + "language_info": { |
| 199 | + "codemirror_mode": { |
| 200 | + "name": "ipython", |
| 201 | + "version": 3 |
| 202 | + }, |
| 203 | + "file_extension": ".py", |
| 204 | + "mimetype": "text/x-python", |
| 205 | + "name": "python", |
| 206 | + "nbconvert_exporter": "python", |
| 207 | + "pygments_lexer": "ipython3", |
| 208 | + "version": "3.13.7" |
| 209 | + } |
| 210 | + }, |
| 211 | + "nbformat": 4, |
| 212 | + "nbformat_minor": 5 |
| 213 | +} |
0 commit comments