Skip to content

Commit

Permalink
after running nbdev prepare
Browse files Browse the repository at this point in the history
  • Loading branch information
algal committed Dec 10, 2024
1 parent ad55708 commit fc52ea0
Show file tree
Hide file tree
Showing 5 changed files with 75 additions and 105 deletions.
58 changes: 34 additions & 24 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,27 +3,6 @@

<!-- WARNING: THIS FILE WAS AUTOGENERATED! DO NOT EDIT! -->

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
Expand Down Expand Up @@ -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?

<details>

- 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}`

</details>

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.
74 changes: 34 additions & 40 deletions nbs/00_core.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -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": {},
Expand All @@ -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",
Expand Down Expand Up @@ -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",
Expand All @@ -170,6 +144,8 @@
"metadata": {},
"outputs": [],
"source": [
"#|eval: false\n",
"\n",
"accounts = cli.list_service_accounts(name = project)\n",
"# accounts"
]
Expand All @@ -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\""
Expand All @@ -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"
Expand All @@ -209,6 +189,8 @@
"metadata": {},
"outputs": [],
"source": [
"#|eval: false\n",
"\n",
"polcli = resourcemanager_v3.ProjectsClient()\n",
"policy = polcli.get_iam_policy(resource=project)"
]
Expand All @@ -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\" ]"
]
Expand All @@ -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",
Expand Down Expand Up @@ -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}\")"
]
},
Expand All @@ -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()"
]
Expand All @@ -297,6 +287,8 @@
}
],
"source": [
"#|eval: false\n",
"\n",
"path = Path('service-account-key.json')\n",
"path.write_bytes(keyb)"
]
Expand Down Expand Up @@ -367,6 +359,8 @@
}
],
"source": [
"#|eval: false\n",
"\n",
"save_superkey_file(path,'us-east5')"
]
},
Expand Down Expand Up @@ -420,6 +414,8 @@
"metadata": {},
"outputs": [],
"source": [
"#|eval: false\n",
"\n",
"cl = get_claudette_client()"
]
},
Expand Down Expand Up @@ -456,6 +452,8 @@
}
],
"source": [
"#|eval: false\n",
"\n",
"cl('hi')"
]
},
Expand All @@ -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')"
]
Expand All @@ -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": {},
Expand All @@ -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\"})"
]
Expand Down
41 changes: 4 additions & 37 deletions nbs/index.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -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": {},
Expand Down Expand Up @@ -141,6 +104,8 @@
"metadata": {},
"outputs": [],
"source": [
"#|eval: false\n",
"\n",
"from vertexauth.core import get_claudette_client\n",
"cl = get_claudette_client()"
]
Expand Down Expand Up @@ -178,6 +143,8 @@
}
],
"source": [
"#|eval: false\n",
"\n",
"cl(\"Hi, Claude!\")"
]
},
Expand Down
2 changes: 1 addition & 1 deletion settings.ini
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ audience = Developers
author = Alexis Gallagher
author_email = [email protected]
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
Expand Down
5 changes: 2 additions & 3 deletions vertexauth/core.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down

0 comments on commit fc52ea0

Please sign in to comment.