Skip to content

Commit

Permalink
work-in-progress on version 2
Browse files Browse the repository at this point in the history
Add comment in core notebook.

Reduced functionality to just:
- helper function to save a superkey, given a SAKF file
- helper functions to create anthropic and claudette clients

Dropped:
- authentication from an env variable
- support for named or non-default superkey
- all docs and comment redundant with code
  • Loading branch information
algal committed Dec 5, 2024
1 parent 4b5e941 commit b3c44eb
Showing 1 changed file with 89 additions and 48 deletions.
137 changes: 89 additions & 48 deletions nbs/00_core.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -15,18 +15,43 @@
"source": [
"# core\n",
"\n",
"> Fill in a module description here"
"> Tiny helper to authenticate Claudette to Vertex AI"
]
},
{
"cell_type": "code",
"execution_count": null,
"execution_count": 1,
"metadata": {},
"outputs": [],
"source": [
"#| export\n",
"import json, base64\n",
"from fastcore.utils import *"
"import json, os\n",
"from pathlib import Path\n",
"from fastcore.utils import *\n",
"\n",
"from anthropic import AnthropicVertex, AsyncAnthropicVertex\n",
"from claudette import Client, AsyncClient\n"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Optional: Getting a GC Service Account Key File (SAKF)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"If you do not already have a Google CLoud Service Account Key File (SAKF), but you are already authenticated via Google cloud, these are the steps for getting a SAKF by using the Google Cloud API. "
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"### Creating a GC service account"
]
},
{
Expand Down Expand Up @@ -203,6 +228,13 @@
"polres = polcli.set_iam_policy(request={\"resource\": project, \"policy\": policy})"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"### Creating a GC Service Account Key File from service account"
]
},
{
"cell_type": "code",
"execution_count": 21,
Expand Down Expand Up @@ -255,65 +287,91 @@
]
},
{
"cell_type": "code",
"execution_count": 39,
"cell_type": "markdown",
"metadata": {},
"outputs": [],
"source": [
"from anthropic import AnthropicVertex, AsyncAnthropicVertex\n",
"from operator import itemgetter"
"## Creating a vertexauth \"superkey\" to use for authentication"
]
},
{
"cell_type": "code",
"execution_count": 41,
"cell_type": "markdown",
"metadata": {},
"outputs": [],
"source": [
"nk['SAKF_path']=str(path)"
"A vertexauth \"superkey\" is just SAKF file with key/value pair added for the region, so that this file can be the ONLY resource a needs to install to use this library.\n",
"\n",
"If someone has given you a superkey, save it in `~/.config/vertexauth/default/superkey.json`.\n",
"\n",
"If you only have a SAKF file, save a superkey by calling the following function"
]
},
{
"cell_type": "code",
"execution_count": 44,
"execution_count": 6,
"metadata": {},
"outputs": [],
"source": [
"gauth_proj_id,gauth_creds,region = itemgetter('project_id','SAKF_path','region')(nk)"
"#| default_exp core\n",
"\n",
"SUPERKEY_DEFAULT_PATH = Path.home() / \".config\" / \"vertexauth\" / \"default\" / \"superkey.json\"\n",
"\n",
"def save_superkey_file(SAKF_path, region) -> Path:\n",
" d = json.loads(Path(SAKF_path)).read_text()\n",
" d[\"region\"] = region\n",
" SUPERKEY_DEFAULT_PATH.parent.mkdir(parents=True,exists_ok=True)\n",
" SUPERKEY_DEFAULT_PATH.write_text(json.dumps(d))\n",
" path.chmod(0o600)\n",
" return path"
]
},
{
"cell_type": "code",
"execution_count": 45,
"cell_type": "markdown",
"metadata": {},
"outputs": [],
"source": [
"os.environ[\"GOOGLE_APPLICATION_CREDENTIALS\"] = gauth_creds\n",
"os.environ[\"GOOGLE_CLOUD_PROJECT\"] = gauth_proj_id\n",
"acli = AnthropicVertex(region=region, project_id=gauth_proj_id)"
"## Authenticating to Claudette"
]
},
{
"cell_type": "code",
"execution_count": 47,
"execution_count": 7,
"metadata": {},
"outputs": [],
"source": [
"from claudette import *"
"#| default_exp core\n",
"\n",
"def get_anthropic_client(asink=False,anthropic_kwargs=None):\n",
" d = json.loads(SUPERKEY_DEFAULT_PATH.read_text())\n",
" os.environ[\"GOOGLE_APPLICATION_CREDENTIALS\"] = str(SUPERKEY_DEFAULT_PATH)\n",
" os.environ[\"GOOGLE_CLOUD_PROJECT\"] = d['project_id']\n",
" anthropic_kwargs = anthropic_kwargs or {}\n",
" AV = AnthropicVertex if not asink else AsyncAnthropicVertex\n",
" return AV(region=d['region'],project_id=d['project_id'], **anthropic_kwargs) \n",
"\n",
"def get_claudette_client(vertex_model='claude-3-5-sonnet-v2@20241022', \n",
" asink=False, anthropic_kwargs=None):\n",
" vertex_cli = get_anthropic_client(asink, anthropic_kwargs)\n",
" if asink: return AsyncClient(vertex_model, vertex_cli)\n",
" else: return Client(vertex_model, vertex_cli)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Using claudette"
]
},
{
"cell_type": "code",
"execution_count": 50,
"execution_count": 8,
"metadata": {},
"outputs": [],
"source": [
"cla = Client('claude-3-5-sonnet-v2@20241022', acli)"
"cl = get_claudette_client()"
]
},
{
"cell_type": "code",
"execution_count": 51,
"execution_count": 9,
"metadata": {},
"outputs": [
{
Expand All @@ -323,7 +381,7 @@
"\n",
"<details>\n",
"\n",
"- id: `msg_vrtx_01UfBEg5LPiemupWnBsAQh8n`\n",
"- id: `msg_vrtx_01VR4dF59fxMfGgxtMoXQ6jj`\n",
"- content: `[{'text': 'Hello! How can I help you today?', 'type': 'text'}]`\n",
"- model: `claude-3-5-sonnet-v2-20241022`\n",
"- role: `assistant`\n",
Expand All @@ -335,33 +393,16 @@
"</details>"
],
"text/plain": [
"Message(id='msg_vrtx_01UfBEg5LPiemupWnBsAQh8n', content=[TextBlock(text='Hello! How can I help you today?', type='text')], model='claude-3-5-sonnet-v2-20241022', role='assistant', stop_reason='end_turn', stop_sequence=None, type='message', usage=In: 8; Out: 12; Cache create: 0; Cache read: 0; Total: 20)"
"Message(id='msg_vrtx_01VR4dF59fxMfGgxtMoXQ6jj', content=[TextBlock(text='Hello! How can I help you today?', type='text')], model='claude-3-5-sonnet-v2-20241022', role='assistant', stop_reason='end_turn', stop_sequence=None, type='message', usage=In: 8; Out: 12; Cache create: 0; Cache read: 0; Total: 20)"
]
},
"execution_count": 51,
"execution_count": 9,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"cla('hi')"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"import json, base64\n",
"\n",
"# Decode the private key data\n",
"key_data = base64.b64decode(key.private_key_data)\n",
"json_key_file = json.loads(key_data)\n",
"\n",
"# Save to file\n",
"with open('service-account-key.json', 'w') as f:\n",
" json.dump(json_key_file, f)"
"cl('hi')"
]
},
{
Expand Down Expand Up @@ -460,7 +501,7 @@
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.11.8"
"version": "3.12.0"
}
},
"nbformat": 4,
Expand Down

0 comments on commit b3c44eb

Please sign in to comment.