diff --git a/README.md b/README.md index cdac7e9..3cf7e0c 100644 --- a/README.md +++ b/README.md @@ -3,27 +3,6 @@ -This file will become your README and also the index of your -documentation. - -## Developer Guide - -If you are new to using `nbdev` here are some useful pointers to get you -started. - -### Install vertexauth in Development mode - -``` sh -# make sure vertexauth package is installed in development mode -$ pip install -e . - -# make changes under nbs/ directory -# ... - -# compile to have changes apply to vertexauth -$ nbdev_prepare -``` - ## Usage ### Installation @@ -58,10 +37,41 @@ find package manager specific guidelines on ## How to use -Fill me in please! Don’t forget code examples: +To use this library, you need install a “superkey” credential file in +your `~/.config/vertexauth/default/superkey.json`. + +Then, you can do the following to get access to a `claudette.Client`, +which is authenticated to use Anthropic’s Sonnet 3.5 v2 model, hosted on +Vertex AI and paid for by Google Cloud budget: ``` python -1+1 +from vertexauth.core import get_claudette_client +cl = get_claudette_client() ``` - 2 +``` python +cl("Hi, Claude!") +``` + +Hi! I’m happy to help. What would you like to discuss? + +
+ +- id: `msg_vrtx_016Tu5UWnM6q6uzASTpr7BTV` +- content: + `[{'text': "Hi! I'm happy to help. What would you like to discuss?", 'type': 'text'}]` +- model: `claude-3-5-sonnet-v2-20241022` +- role: `assistant` +- stop_reason: `end_turn` +- stop_sequence: `None` +- type: `message` +- usage: `{'input_tokens': 11, 'output_tokens': 18}` + +
+ +What is a superkey? It is merely a Google Cloud Service Account Key +File, encoded in JSON, with a `region` key/value pair added. + +Please consult the notebook `00_core.ipynb` for a working example of +creating a service account, creating a service account key, downloading +the key file, and saving a superkey file into the default location. diff --git a/nbs/00_core.ipynb b/nbs/00_core.ipynb index 6d87273..11c9e1e 100644 --- a/nbs/00_core.ipynb +++ b/nbs/00_core.ipynb @@ -56,26 +56,6 @@ "### 1. Creating a GC service account" ] }, - { - "cell_type": "code", - "execution_count": null, - "metadata": {}, - "outputs": [], - "source": [ - "from IPython.display import Markdown\n", - "from playwrightnb import url2md,read_page\n", - "from ContextKit import read_gist" - ] - }, - { - "cell_type": "code", - "execution_count": null, - "metadata": {}, - "outputs": [], - "source": [ - "sak_docs = read_gist('https://gist.github.com/jph00/3d12ac8ac76c7e7b3ef62dc889284838')" - ] - }, { "cell_type": "code", "execution_count": null, @@ -85,16 +65,6 @@ "# !pip install -q google-cloud-service-usage google-cloud-iam google-cloud-resource-manager" ] }, - { - "cell_type": "code", - "execution_count": null, - "metadata": {}, - "outputs": [], - "source": [ - "%%ai 0 -c\n", - "I want to create a service account key file using the Python API documented in $`sak_docs`. Can you read the docs and confirm that all the needed info is there, before we begin?" - ] - }, { "cell_type": "markdown", "metadata": {}, @@ -111,6 +81,8 @@ "metadata": {}, "outputs": [], "source": [ + "#|eval: false\n", + "\n", "import google.auth\n", "from google.cloud import iam_admin_v1\n", "from google.cloud.iam_admin_v1 import types\n", @@ -151,6 +123,8 @@ } ], "source": [ + "#|eval: false\n", + "\n", "cli = iam_admin_v1.IAMClient()\n", "credentials, project_id = google.auth.default()\n", "project = f\"projects/{project_id}\"\n", @@ -170,6 +144,8 @@ "metadata": {}, "outputs": [], "source": [ + "#|eval: false\n", + "\n", "accounts = cli.list_service_accounts(name = project)\n", "# accounts" ] @@ -187,6 +163,8 @@ "metadata": {}, "outputs": [], "source": [ + "#|eval: false\n", + "\n", "account_id=\"aiservice2\"\n", "display_name=\"Vertex AI Service Account 2\"\n", "description=\"Access Vertex AI\"" @@ -198,6 +176,8 @@ "metadata": {}, "outputs": [], "source": [ + "#|eval: false\n", + "\n", "svc = dict(display_name=display_name, description=description)\n", "account = cli.create_service_account(name=project, account_id=account_id, service_account=svc)\n", "# account" @@ -209,6 +189,8 @@ "metadata": {}, "outputs": [], "source": [ + "#|eval: false\n", + "\n", "polcli = resourcemanager_v3.ProjectsClient()\n", "policy = polcli.get_iam_policy(resource=project)" ] @@ -219,6 +201,8 @@ "metadata": {}, "outputs": [], "source": [ + "#|eval: false\n", + "\n", "member = f\"serviceAccount:{account.email}\"\n", "roles = [ \"roles/aiplatform.user\", \"roles/servicemanagement.quotaViewer\", \"roles/servicemanagement.quotaAdmin\" ]" ] @@ -229,6 +213,8 @@ "metadata": {}, "outputs": [], "source": [ + "#|eval: false\n", + "\n", "for role in roles:\n", " binding = policy_pb2.Binding()\n", " binding.role = role\n", @@ -267,6 +253,8 @@ "metadata": {}, "outputs": [], "source": [ + "#|eval: false\n", + "\n", "key = cli.create_service_account_key(name = f\"projects/{project_id}/serviceAccounts/{account.email}\")" ] }, @@ -276,6 +264,8 @@ "metadata": {}, "outputs": [], "source": [ + "#|eval: false\n", + "\n", "keyd = json.loads(key.private_key_data.decode())\n", "keyb = json.dumps(keyd).encode()" ] @@ -297,6 +287,8 @@ } ], "source": [ + "#|eval: false\n", + "\n", "path = Path('service-account-key.json')\n", "path.write_bytes(keyb)" ] @@ -367,6 +359,8 @@ } ], "source": [ + "#|eval: false\n", + "\n", "save_superkey_file(path,'us-east5')" ] }, @@ -420,6 +414,8 @@ "metadata": {}, "outputs": [], "source": [ + "#|eval: false\n", + "\n", "cl = get_claudette_client()" ] }, @@ -456,6 +452,8 @@ } ], "source": [ + "#|eval: false\n", + "\n", "cl('hi')" ] }, @@ -472,6 +470,8 @@ "metadata": {}, "outputs": [], "source": [ + "#|eval: false\n", + "\n", "quota_docs = read_gist('https://gist.github.com/jph00/943c51623abfe0deae65cfad2d821169')\n", "svcuse_docs = read_gist('https://gist.github.com/jph00/042580724e98ae0cce2db50de92abd1b')" ] @@ -482,19 +482,11 @@ "metadata": {}, "outputs": [], "source": [ + "#|eval: false\n", + "\n", "from google.cloud import service_usage_v1" ] }, - { - "cell_type": "code", - "execution_count": null, - "metadata": {}, - "outputs": [], - "source": [ - "%%ai 0 -c\n", - "I want to make sure we have all the quotas we need. See the docs in $`quota_docs` for examples of the quota API, and $`svcuse_docs` for service usage API docs. Do you see anything in the docs which might help here?" - ] - }, { "cell_type": "markdown", "metadata": {}, @@ -508,6 +500,8 @@ "metadata": {}, "outputs": [], "source": [ + "#|eval: false\n", + "\n", "scli = service_usage_v1.ServiceUsageClient()\n", "services = scli.list_services(request={\"parent\": project, \"filter\":\"state:ENABLED\"})" ] diff --git a/nbs/index.ipynb b/nbs/index.ipynb index 9778d77..a06ab07 100644 --- a/nbs/index.ipynb +++ b/nbs/index.ipynb @@ -19,43 +19,6 @@ "> A helper library for accessing Google Vertex AI models, via `claudette` or `anthropic`" ] }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "## Developer Guide" - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "If you are new to using `nbdev` here are some useful pointers to get you started." - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "### Install vertexauth in Development mode" - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "```sh\n", - "# make sure vertexauth package is installed in development mode\n", - "$ pip install -e .\n", - "\n", - "# make changes under nbs/ directory\n", - "# ...\n", - "\n", - "# compile to have changes apply to vertexauth\n", - "$ nbdev_prepare\n", - "```" - ] - }, { "cell_type": "markdown", "metadata": {}, @@ -141,6 +104,8 @@ "metadata": {}, "outputs": [], "source": [ + "#|eval: false\n", + "\n", "from vertexauth.core import get_claudette_client\n", "cl = get_claudette_client()" ] @@ -178,6 +143,8 @@ } ], "source": [ + "#|eval: false\n", + "\n", "cl(\"Hi, Claude!\")" ] }, diff --git a/settings.ini b/settings.ini index 31feb51..7f8f95e 100644 --- a/settings.ini +++ b/settings.ini @@ -31,7 +31,7 @@ audience = Developers author = Alexis Gallagher author_email = alexis@alexisgallagher.com copyright = 2024 onwards, %(author)s -description = A helper library for accessing Google Vertex AI models +description = A tiny helper library for accessing Google Vertex AI models keywords = nbdev jupyter notebook python language = English status = 3 diff --git a/vertexauth/core.py b/vertexauth/core.py index 748bdae..15d661c 100644 --- a/vertexauth/core.py +++ b/vertexauth/core.py @@ -14,11 +14,10 @@ from claudette import Client, AsyncClient -# %% ../nbs/00_core.ipynb 31 +# %% ../nbs/00_core.ipynb 28 SUPERKEY_DEFAULT_PATH = Path.home() / ".config" / "vertexauth" / "default" / "superkey.json" - -# %% ../nbs/00_core.ipynb 37 +# %% ../nbs/00_core.ipynb 34 def get_anthropic_client(asink=False,anthropic_kwargs=None): d = json.loads(SUPERKEY_DEFAULT_PATH.read_text()) os.environ["GOOGLE_APPLICATION_CREDENTIALS"] = str(SUPERKEY_DEFAULT_PATH)