Skip to content

Commit

Permalink
feat(vertex): add support for google vertex (#265)
Browse files Browse the repository at this point in the history
  • Loading branch information
stainless-bot authored and RobertCraigie committed Jan 23, 2024
1 parent 8e13801 commit 96f9b55
Show file tree
Hide file tree
Showing 19 changed files with 3,953 additions and 5 deletions.
3 changes: 2 additions & 1 deletion .github/workflows/create-releases.yml
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ jobs:
- name: Publish to NPM
if: ${{ steps.release.outputs.releases_created }}
run: |
bash ./bin/publish-npm
yarn tsn scripts/publish-packages.ts '${{ toJSON(steps.release.outputs) }}'
env:
NPM_TOKEN: ${{ secrets.ANTHROPIC_NPM_TOKEN || secrets.NPM_TOKEN }}
6 changes: 5 additions & 1 deletion .github/workflows/publish-npm.yml
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,10 @@
name: Publish NPM
on:
workflow_dispatch:
inputs:
path:
description: The path to run the release in, e.g. '.' or 'packages/vertex-sdk'
required: true

jobs:
publish:
Expand All @@ -24,6 +28,6 @@ jobs:
- name: Publish to NPM
run: |
bash ./bin/publish-npm
yarn tsn scripts/publish-packages.ts '{ "paths_released": "[\"${{ github.event.inputs.path }}\"]" }'
env:
NPM_TOKEN: ${{ secrets.ANTHROPIC_NPM_TOKEN || secrets.NPM_TOKEN }}
3 changes: 2 additions & 1 deletion .release-please-manifest.json
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
{
".": "0.12.3"
".": "0.12.3",
"packages/vertex-sdk": "0.0.1"
}
12 changes: 12 additions & 0 deletions build-all
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
#!/usr/bin/env bash
set -exuo pipefail

# build the core SDK package an all sub-packages

yarn build

for dir in packages/*; do
if [ -d "$dir" ]; then
(cd "$dir" && yarn install && yarn build)
fi
done
65 changes: 65 additions & 0 deletions packages/vertex-sdk/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
# Anthropic Vertex TypeScript API Library

> [!IMPORTANT]
> This API is in private preview.
[![NPM version](https://img.shields.io/npm/v/@anthropic-ai/vertex-sdk.svg)](https://npmjs.org/package/@anthropic-ai/vertex-sdk)

This library provides convenient access to the private preview of the Anthropic Vertex API.

For the non-Vertex Anthropic API at api.anthropic.com, see [`@anthropic-ai/sdk`](https://github.com/anthropics/anthropic-sdk-typescript).

## Installation

```sh
npm install --save @anthropic-ai/vertex-sdk
# or
yarn add @anthropic-ai/vertex-sdk
```

## Usage

<!-- prettier-ignore -->
```js
import { AnthropicVertex } from '@anthropic-ai/vertex-sdk';

// Reads from the `CLOUD_ML_REGION` & `ANTHROPIC_VERTEX_PROJECT_ID` environment variables.
// Additionally goes through the standard `google-auth-library` flow.
const client = new AnthropicVertex();

async function main() {
const result = await client.beta.messages.create({
messages: [
{
role: 'user',
content: 'Hey Claude! How can I recursively list all files in a directory in Rust?',
},
],
model: 'claude-instant-1p2',
max_tokens: 300,
});
console.log(JSON.stringify(result, null, 2));
}

main();
```

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.

## Requirements

TypeScript >= 4.5 is supported.

The following runtimes are supported:

- Node.js 18 LTS or later ([non-EOL](https://endoflife.date/nodejs)) versions.
- Deno v1.28.0 or higher, using `import { AnthropicVertex } from "npm:@anthropic-ai/vertex-sdk"`.
- Bun 1.0 or later.
- Cloudflare Workers.
- Vercel Edge Runtime.
- Jest 28 or greater with the `"node"` environment (`"jsdom"` is not supported at this time).
- Nitro v2.6 or greater.

Note that React Native is not supported at this time.

If you are interested in other runtime environments, please open or upvote an issue on GitHub.
43 changes: 43 additions & 0 deletions packages/vertex-sdk/build
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
#!/usr/bin/env bash
set -exuo pipefail

rm -rf dist; mkdir dist

# Copy src to dist/src and build from dist/src into dist, so that
# the source map for index.js.map will refer to ./src/index.ts etc
cp -rp src README.md dist

for file in LICENSE; do
if [ -e "../../${file}" ]; then cp "../../${file}" dist; fi
done

for file in CHANGELOG.md; do
if [ -e "${file}" ]; then cp "${file}" dist; fi
done

# this converts the export map paths for the dist directory
# and does a few other minor things
PKG_JSON_PATH=../packages/vertex-sdk/package.json node ../../scripts/make-dist-package-json.cjs > dist/package.json

# updates the `@anthropic-ai/sdk` dependency to point to NPM
node scripts/postprocess-dist-package-json.cjs

# build to .js/.mjs/.d.ts files
npm exec tsc-multi
# we need to add exports = module.exports = Anthropic TypeScript to index.js;
# No way to get that from index.ts because it would cause compile errors
# when building .mjs
DIST_PATH=./dist node ../../scripts/fix-index-exports.cjs
# with "moduleResolution": "nodenext", if ESM resolves to index.d.ts,
# it'll have TS errors on the default import. But if it resolves to
# index.d.mts the default import will work (even though both files have
# the same export default statement)
cp dist/index.d.ts dist/index.d.mts
cp tsconfig.dist-src.json dist/src/tsconfig.json

DIST_PATH=./dist PKG_IMPORT_PATH=@anthropic-ai/vertex-sdk/ node ../../scripts/postprocess-files.cjs

# make sure that nothing crashes when we require the output CJS or
# import the output ESM
(cd dist && node -e 'require("@anthropic-ai/vertex-sdk")')
(cd dist && node -e 'import("@anthropic-ai/vertex-sdk")' --input-type=module)
26 changes: 26 additions & 0 deletions packages/vertex-sdk/examples/vertex.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
#!/usr/bin/env -S npm run tsn -T

import { AnthropicVertex } from '@anthropic-ai/vertex-sdk';

// Reads from the `CLOUD_ML_REGION` & `ANTHROPIC_VERTEX_PROJECT_ID`
// environment variables.
const client = new AnthropicVertex();

async function main() {
const result = await client.beta.messages.create({
messages: [
{
role: 'user',
content: 'Hello!',
},
],
model: 'claude-instant-1p2',
max_tokens: 300,
});
console.log(JSON.stringify(result, null, 2));
}

main().catch((err) => {
console.error(err);
process.exit(1);
});
71 changes: 71 additions & 0 deletions packages/vertex-sdk/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
{
"name": "@anthropic-ai/vertex-sdk",
"version": "0.0.1",
"description": "The official TypeScript library for the Anthropic Vertex API",
"author": "Anthropic <[email protected]>",
"types": "dist/index.d.ts",
"main": "dist/index.js",
"type": "commonjs",
"repository": "github:anthropics/anthropic-sdk-typescript",
"license": "MIT",
"packageManager": "[email protected]",
"private": false,
"scripts": {
"test": "bin/check-test-server && yarn jest",
"build": "bash ./build",
"prepack": "echo 'to pack, run yarn build && (cd dist; yarn pack)' && exit 1",
"prepublishOnly": "echo 'to publish, run yarn build && (cd dist; yarn publish)' && exit 1",
"format": "prettier --write --cache --cache-strategy metadata . !dist",
"prepare": "if [ $(basename $(dirname $PWD)) = 'node_modules' ]; then npm run build; fi",
"tsn": "ts-node -r tsconfig-paths/register",
"lint": "eslint --ext ts,js .",
"fix": "eslint --fix --ext ts,js ."
},
"dependencies": {
"@anthropic-ai/sdk": "file:../../dist/",
"google-auth-library": "^9.4.2"
},
"devDependencies": {
"@types/jest": "^29.4.0",
"@typescript-eslint/eslint-plugin": "^6.7.0",
"@typescript-eslint/parser": "^6.7.0",
"eslint": "^8.49.0",
"eslint-plugin-prettier": "^5.0.1",
"eslint-plugin-unused-imports": "^3.0.0",
"jest": "^29.4.0",
"prettier": "^3.0.0",
"ts-jest": "^29.1.0",
"ts-morph": "^19.0.0",
"ts-node": "^10.5.0",
"tsc-multi": "^1.1.0",
"tsconfig-paths": "^4.0.0",
"typescript": "^4.8.2"
},
"imports": {
"@anthropic-ai/vertex-sdk": ".",
"@anthropic-ai/vertex-sdk/*": "./src/*"
},
"exports": {
".": {
"require": {
"types": "./dist/index.d.ts",
"default": "./dist/index.js"
},
"types": "./dist/index.d.mts",
"default": "./dist/index.mjs"
},
"./*.mjs": {
"types": "./dist/*.d.ts",
"default": "./dist/*.mjs"
},
"./*.js": {
"types": "./dist/*.d.ts",
"default": "./dist/*.js"
},
"./*": {
"types": "./dist/*.d.ts",
"require": "./dist/*.js",
"default": "./dist/*.mjs"
}
}
}
11 changes: 11 additions & 0 deletions packages/vertex-sdk/scripts/postprocess-dist-package-json.cjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
const fs = require('fs');
const pkgJson = require('../dist/package.json');

for (const dep in pkgJson.dependencies) {
// ensure we point to NPM instead of a local directory
if (dep === '@anthropic-ai/sdk') {
pkgJson.dependencies[dep] = '^0';
}
}

fs.writeFileSync('dist/package.json', JSON.stringify(pkgJson, null, 2))
Loading

0 comments on commit 96f9b55

Please sign in to comment.