Skip to content

Commit

Permalink
chore(bedrock): move bedrock SDK to the main repo (#274)
Browse files Browse the repository at this point in the history
  • Loading branch information
stainless-bot committed Jan 31, 2024
1 parent db3585d commit b4ef3a8
Show file tree
Hide file tree
Showing 19 changed files with 5,291 additions and 3 deletions.
3 changes: 2 additions & 1 deletion .release-please-manifest.json
@@ -1,4 +1,5 @@
{
".": "0.12.6",
"packages/vertex-sdk": "0.1.1"
"packages/vertex-sdk": "0.1.1",
"packages/bedrock-sdk": "0.0.1"
}
65 changes: 65 additions & 0 deletions packages/bedrock-sdk/README.md
@@ -0,0 +1,65 @@
# Anthropic Bedrock TypeScript API Library

[![NPM version](https://img.shields.io/npm/v/@anthropic-ai/bedrock-sdk.svg)](https://npmjs.org/package/@anthropic-ai/bedrock-sdk)

This library provides convenient access to the Anthropic Bedrock API.

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

## Installation

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

## Usage

<!-- prettier-ignore -->
```js
import Anthropic from '@anthropic-ai/sdk';
import { AnthropicBedrock } from '@anthropic-ai/bedrock-sdk';

// Note: this assumes you have configured AWS credentials in a way
// that the AWS Node SDK will recognise, typicaly a shared `~/.aws/credentials`
// file or `AWS_ACCESS_KEY_ID` & `AWS_SECRET_ACCESS_KEY` environment variables.
//
// https://docs.aws.amazon.com/AWSJavaScriptSDK/v3/latest/modules/_aws_sdk_credential_provider_node.html
const anthropic = new AnthropicBedrock();

async function main() {
const completion = await anthropic.completions.create({
model: 'anthropic.claude-instant-v1',
prompt: `${Anthropic.HUMAN_PROMPT} how does a court case get to the Supreme Court? ${Anthropic.AI_PROMPT}`,
stop_sequences: [Anthropic.HUMAN_PROMPT],
max_tokens_to_sample: 800,
temperature: 0.5,
top_k: 250,
top_p: 0.5,
});
console.log(completion);
}

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 { AnthropicBedrock } from "npm:@anthropic-ai/bedrock-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/bedrock-sdk/build
@@ -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/bedrock-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/bedrock-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/bedrock-sdk")')
(cd dist && node -e 'import("@anthropic-ai/bedrock-sdk")' --input-type=module)
26 changes: 26 additions & 0 deletions packages/bedrock-sdk/examples/demo.ts
@@ -0,0 +1,26 @@
#!/usr/bin/env -S npm run tsn -T

import Anthropic from '@anthropic-ai/sdk';
import { AnthropicBedrock } from '@anthropic-ai/bedrock-sdk';

// Note: this assumes you have configured AWS credentials in a way
// that the AWS Node SDK will recognise, typicaly a shared `~/.aws/credentials`
// file or `AWS_ACCESS_KEY_ID` & `AWS_SECRET_ACCESS_KEY` environment variables.
//
// https://docs.aws.amazon.com/AWSJavaScriptSDK/v3/latest/modules/_aws_sdk_credential_provider_node.html
const anthropic = new AnthropicBedrock();

async function main() {
const completion = await anthropic.completions.create({
model: 'anthropic.claude-instant-v1',
prompt: `${Anthropic.HUMAN_PROMPT} how does a court case get to the Supreme Court? ${Anthropic.AI_PROMPT}`,
stop_sequences: [Anthropic.HUMAN_PROMPT],
max_tokens_to_sample: 800,
temperature: 0.5,
top_k: 250,
top_p: 0.5,
});
console.log(completion);
}

main();
28 changes: 28 additions & 0 deletions packages/bedrock-sdk/examples/streaming.ts
@@ -0,0 +1,28 @@
#!/usr/bin/env -S npm run tsn -T

import Anthropic from '@anthropic-ai/sdk';
import { AnthropicBedrock } from '@anthropic-ai/bedrock-sdk';

// Note: this assumes you have configured AWS credentials in a way
// that the AWS Node SDK will recognise, typicaly a shared `~/.aws/credentials`
// file or `AWS_ACCESS_KEY_ID` & `AWS_SECRET_ACCESS_KEY` environment variables.
//
// https://docs.aws.amazon.com/AWSJavaScriptSDK/v3/latest/modules/_aws_sdk_credential_provider_node.html
const client = new AnthropicBedrock();

async function main() {
const question = 'Hey Claude! How can I recursively list all files in a directory in Rust?';

const stream = await client.completions.create({
prompt: `${Anthropic.HUMAN_PROMPT}${question}${Anthropic.AI_PROMPT}:`,
model: 'anthropic.claude-v2:1',
stream: true,
max_tokens_to_sample: 500,
});

for await (const completion of stream) {
process.stdout.write(completion.completion);
}
}

main();
81 changes: 81 additions & 0 deletions packages/bedrock-sdk/package.json
@@ -0,0 +1,81 @@
{
"name": "@anthropic-ai/bedrock-sdk",
"version": "0.6.5",
"description": "The official TypeScript library for the Anthropic Bedrock API",
"author": "Anthropic <support@anthropic.com>",
"types": "dist/index.d.ts",
"main": "dist/index.js",
"type": "commonjs",
"repository": "github:anthropics/anthropic-sdk-typescript",
"license": "MIT",
"packageManager": "yarn@1.22.21",
"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/",
"@aws-crypto/sha256-js": "^4.0.0",
"@aws-sdk/client-bedrock-runtime": "^3.423.0",
"@aws-sdk/credential-providers": "^3.341.0",
"@aws-sdk/protocol-http": "^3.341.0",
"@aws-sdk/signature-v4": "^3.341.0",
"@smithy/eventstream-serde-node": "^2.0.10",
"@smithy/fetch-http-handler": "^2.2.1",
"@smithy/protocol-http": "^3.0.6",
"@smithy/smithy-client": "^2.1.9",
"@smithy/types": "^2.3.4",
"@smithy/util-base64": "^2.0.0"
},
"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/bedrock-sdk": ".",
"@anthropic-ai/bedrock-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/bedrock-sdk/scripts/postprocess-dist-package-json.cjs
@@ -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))

0 comments on commit b4ef3a8

Please sign in to comment.