Skip to content

Commit 96f9b55

Browse files
stainless-botRobertCraigie
authored andcommitted
feat(vertex): add support for google vertex (#265)
1 parent 8e13801 commit 96f9b55

19 files changed

+3953
-5
lines changed

.github/workflows/create-releases.yml

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@ jobs:
3636
- name: Publish to NPM
3737
if: ${{ steps.release.outputs.releases_created }}
3838
run: |
39-
bash ./bin/publish-npm
39+
yarn tsn scripts/publish-packages.ts '${{ toJSON(steps.release.outputs) }}'
40+
4041
env:
4142
NPM_TOKEN: ${{ secrets.ANTHROPIC_NPM_TOKEN || secrets.NPM_TOKEN }}

.github/workflows/publish-npm.yml

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,10 @@
33
name: Publish NPM
44
on:
55
workflow_dispatch:
6+
inputs:
7+
path:
8+
description: The path to run the release in, e.g. '.' or 'packages/vertex-sdk'
9+
required: true
610

711
jobs:
812
publish:
@@ -24,6 +28,6 @@ jobs:
2428
2529
- name: Publish to NPM
2630
run: |
27-
bash ./bin/publish-npm
31+
yarn tsn scripts/publish-packages.ts '{ "paths_released": "[\"${{ github.event.inputs.path }}\"]" }'
2832
env:
2933
NPM_TOKEN: ${{ secrets.ANTHROPIC_NPM_TOKEN || secrets.NPM_TOKEN }}

.release-please-manifest.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
11
{
2-
".": "0.12.3"
2+
".": "0.12.3",
3+
"packages/vertex-sdk": "0.0.1"
34
}

build-all

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
#!/usr/bin/env bash
2+
set -exuo pipefail
3+
4+
# build the core SDK package an all sub-packages
5+
6+
yarn build
7+
8+
for dir in packages/*; do
9+
if [ -d "$dir" ]; then
10+
(cd "$dir" && yarn install && yarn build)
11+
fi
12+
done

packages/vertex-sdk/README.md

Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
# Anthropic Vertex TypeScript API Library
2+
3+
> [!IMPORTANT]
4+
> This API is in private preview.
5+
6+
[![NPM version](https://img.shields.io/npm/v/@anthropic-ai/vertex-sdk.svg)](https://npmjs.org/package/@anthropic-ai/vertex-sdk)
7+
8+
This library provides convenient access to the private preview of the Anthropic Vertex API.
9+
10+
For the non-Vertex Anthropic API at api.anthropic.com, see [`@anthropic-ai/sdk`](https://github.com/anthropics/anthropic-sdk-typescript).
11+
12+
## Installation
13+
14+
```sh
15+
npm install --save @anthropic-ai/vertex-sdk
16+
# or
17+
yarn add @anthropic-ai/vertex-sdk
18+
```
19+
20+
## Usage
21+
22+
<!-- prettier-ignore -->
23+
```js
24+
import { AnthropicVertex } from '@anthropic-ai/vertex-sdk';
25+
26+
// Reads from the `CLOUD_ML_REGION` & `ANTHROPIC_VERTEX_PROJECT_ID` environment variables.
27+
// Additionally goes through the standard `google-auth-library` flow.
28+
const client = new AnthropicVertex();
29+
30+
async function main() {
31+
const result = await client.beta.messages.create({
32+
messages: [
33+
{
34+
role: 'user',
35+
content: 'Hey Claude! How can I recursively list all files in a directory in Rust?',
36+
},
37+
],
38+
model: 'claude-instant-1p2',
39+
max_tokens: 300,
40+
});
41+
console.log(JSON.stringify(result, null, 2));
42+
}
43+
44+
main();
45+
```
46+
47+
For more details on how to use the SDK, see the [README.md for the main Anthropic SDK](https://github.com/anthropics/anthropic-sdk-typescript/tree/main#anthropic-typescript-api-library) which this library extends.
48+
49+
## Requirements
50+
51+
TypeScript >= 4.5 is supported.
52+
53+
The following runtimes are supported:
54+
55+
- Node.js 18 LTS or later ([non-EOL](https://endoflife.date/nodejs)) versions.
56+
- Deno v1.28.0 or higher, using `import { AnthropicVertex } from "npm:@anthropic-ai/vertex-sdk"`.
57+
- Bun 1.0 or later.
58+
- Cloudflare Workers.
59+
- Vercel Edge Runtime.
60+
- Jest 28 or greater with the `"node"` environment (`"jsdom"` is not supported at this time).
61+
- Nitro v2.6 or greater.
62+
63+
Note that React Native is not supported at this time.
64+
65+
If you are interested in other runtime environments, please open or upvote an issue on GitHub.

packages/vertex-sdk/build

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
#!/usr/bin/env bash
2+
set -exuo pipefail
3+
4+
rm -rf dist; mkdir dist
5+
6+
# Copy src to dist/src and build from dist/src into dist, so that
7+
# the source map for index.js.map will refer to ./src/index.ts etc
8+
cp -rp src README.md dist
9+
10+
for file in LICENSE; do
11+
if [ -e "../../${file}" ]; then cp "../../${file}" dist; fi
12+
done
13+
14+
for file in CHANGELOG.md; do
15+
if [ -e "${file}" ]; then cp "${file}" dist; fi
16+
done
17+
18+
# this converts the export map paths for the dist directory
19+
# and does a few other minor things
20+
PKG_JSON_PATH=../packages/vertex-sdk/package.json node ../../scripts/make-dist-package-json.cjs > dist/package.json
21+
22+
# updates the `@anthropic-ai/sdk` dependency to point to NPM
23+
node scripts/postprocess-dist-package-json.cjs
24+
25+
# build to .js/.mjs/.d.ts files
26+
npm exec tsc-multi
27+
# we need to add exports = module.exports = Anthropic TypeScript to index.js;
28+
# No way to get that from index.ts because it would cause compile errors
29+
# when building .mjs
30+
DIST_PATH=./dist node ../../scripts/fix-index-exports.cjs
31+
# with "moduleResolution": "nodenext", if ESM resolves to index.d.ts,
32+
# it'll have TS errors on the default import. But if it resolves to
33+
# index.d.mts the default import will work (even though both files have
34+
# the same export default statement)
35+
cp dist/index.d.ts dist/index.d.mts
36+
cp tsconfig.dist-src.json dist/src/tsconfig.json
37+
38+
DIST_PATH=./dist PKG_IMPORT_PATH=@anthropic-ai/vertex-sdk/ node ../../scripts/postprocess-files.cjs
39+
40+
# make sure that nothing crashes when we require the output CJS or
41+
# import the output ESM
42+
(cd dist && node -e 'require("@anthropic-ai/vertex-sdk")')
43+
(cd dist && node -e 'import("@anthropic-ai/vertex-sdk")' --input-type=module)
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
#!/usr/bin/env -S npm run tsn -T
2+
3+
import { AnthropicVertex } from '@anthropic-ai/vertex-sdk';
4+
5+
// Reads from the `CLOUD_ML_REGION` & `ANTHROPIC_VERTEX_PROJECT_ID`
6+
// environment variables.
7+
const client = new AnthropicVertex();
8+
9+
async function main() {
10+
const result = await client.beta.messages.create({
11+
messages: [
12+
{
13+
role: 'user',
14+
content: 'Hello!',
15+
},
16+
],
17+
model: 'claude-instant-1p2',
18+
max_tokens: 300,
19+
});
20+
console.log(JSON.stringify(result, null, 2));
21+
}
22+
23+
main().catch((err) => {
24+
console.error(err);
25+
process.exit(1);
26+
});

packages/vertex-sdk/package.json

Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
1+
{
2+
"name": "@anthropic-ai/vertex-sdk",
3+
"version": "0.0.1",
4+
"description": "The official TypeScript library for the Anthropic Vertex API",
5+
"author": "Anthropic <[email protected]>",
6+
"types": "dist/index.d.ts",
7+
"main": "dist/index.js",
8+
"type": "commonjs",
9+
"repository": "github:anthropics/anthropic-sdk-typescript",
10+
"license": "MIT",
11+
"packageManager": "[email protected]",
12+
"private": false,
13+
"scripts": {
14+
"test": "bin/check-test-server && yarn jest",
15+
"build": "bash ./build",
16+
"prepack": "echo 'to pack, run yarn build && (cd dist; yarn pack)' && exit 1",
17+
"prepublishOnly": "echo 'to publish, run yarn build && (cd dist; yarn publish)' && exit 1",
18+
"format": "prettier --write --cache --cache-strategy metadata . !dist",
19+
"prepare": "if [ $(basename $(dirname $PWD)) = 'node_modules' ]; then npm run build; fi",
20+
"tsn": "ts-node -r tsconfig-paths/register",
21+
"lint": "eslint --ext ts,js .",
22+
"fix": "eslint --fix --ext ts,js ."
23+
},
24+
"dependencies": {
25+
"@anthropic-ai/sdk": "file:../../dist/",
26+
"google-auth-library": "^9.4.2"
27+
},
28+
"devDependencies": {
29+
"@types/jest": "^29.4.0",
30+
"@typescript-eslint/eslint-plugin": "^6.7.0",
31+
"@typescript-eslint/parser": "^6.7.0",
32+
"eslint": "^8.49.0",
33+
"eslint-plugin-prettier": "^5.0.1",
34+
"eslint-plugin-unused-imports": "^3.0.0",
35+
"jest": "^29.4.0",
36+
"prettier": "^3.0.0",
37+
"ts-jest": "^29.1.0",
38+
"ts-morph": "^19.0.0",
39+
"ts-node": "^10.5.0",
40+
"tsc-multi": "^1.1.0",
41+
"tsconfig-paths": "^4.0.0",
42+
"typescript": "^4.8.2"
43+
},
44+
"imports": {
45+
"@anthropic-ai/vertex-sdk": ".",
46+
"@anthropic-ai/vertex-sdk/*": "./src/*"
47+
},
48+
"exports": {
49+
".": {
50+
"require": {
51+
"types": "./dist/index.d.ts",
52+
"default": "./dist/index.js"
53+
},
54+
"types": "./dist/index.d.mts",
55+
"default": "./dist/index.mjs"
56+
},
57+
"./*.mjs": {
58+
"types": "./dist/*.d.ts",
59+
"default": "./dist/*.mjs"
60+
},
61+
"./*.js": {
62+
"types": "./dist/*.d.ts",
63+
"default": "./dist/*.js"
64+
},
65+
"./*": {
66+
"types": "./dist/*.d.ts",
67+
"require": "./dist/*.js",
68+
"default": "./dist/*.mjs"
69+
}
70+
}
71+
}
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
const fs = require('fs');
2+
const pkgJson = require('../dist/package.json');
3+
4+
for (const dep in pkgJson.dependencies) {
5+
// ensure we point to NPM instead of a local directory
6+
if (dep === '@anthropic-ai/sdk') {
7+
pkgJson.dependencies[dep] = '^0';
8+
}
9+
}
10+
11+
fs.writeFileSync('dist/package.json', JSON.stringify(pkgJson, null, 2))

0 commit comments

Comments
 (0)