Skip to content

Commit 1a565fe

Browse files
stainless-botRobertCraigie
authored andcommitted
chore(bedrock): move bedrock SDK to the main repo (#274)
1 parent a9fef06 commit 1a565fe

19 files changed

+5291
-3
lines changed

.release-please-manifest.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
{
22
".": "0.12.6",
3-
"packages/vertex-sdk": "0.1.1"
3+
"packages/vertex-sdk": "0.1.1",
4+
"packages/bedrock-sdk": "0.0.1"
45
}

packages/bedrock-sdk/README.md

Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
# Anthropic Bedrock TypeScript API Library
2+
3+
[![NPM version](https://img.shields.io/npm/v/@anthropic-ai/bedrock-sdk.svg)](https://npmjs.org/package/@anthropic-ai/bedrock-sdk)
4+
5+
This library provides convenient access to the Anthropic Bedrock API.
6+
7+
For the non-Bedrock Anthropic API at api.anthropic.com, see [`@anthropic-ai/sdk`](https://github.com/anthropics/anthropic-sdk-typescript).
8+
9+
## Installation
10+
11+
```sh
12+
npm install --save @anthropic-ai/bedrock-sdk
13+
# or
14+
yarn add @anthropic-ai/bedrock-sdk
15+
```
16+
17+
## Usage
18+
19+
<!-- prettier-ignore -->
20+
```js
21+
import Anthropic from '@anthropic-ai/sdk';
22+
import { AnthropicBedrock } from '@anthropic-ai/bedrock-sdk';
23+
24+
// Note: this assumes you have configured AWS credentials in a way
25+
// that the AWS Node SDK will recognise, typicaly a shared `~/.aws/credentials`
26+
// file or `AWS_ACCESS_KEY_ID` & `AWS_SECRET_ACCESS_KEY` environment variables.
27+
//
28+
// https://docs.aws.amazon.com/AWSJavaScriptSDK/v3/latest/modules/_aws_sdk_credential_provider_node.html
29+
const anthropic = new AnthropicBedrock();
30+
31+
async function main() {
32+
const completion = await anthropic.completions.create({
33+
model: 'anthropic.claude-instant-v1',
34+
prompt: `${Anthropic.HUMAN_PROMPT} how does a court case get to the Supreme Court? ${Anthropic.AI_PROMPT}`,
35+
stop_sequences: [Anthropic.HUMAN_PROMPT],
36+
max_tokens_to_sample: 800,
37+
temperature: 0.5,
38+
top_k: 250,
39+
top_p: 0.5,
40+
});
41+
console.log(completion);
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 { AnthropicBedrock } from "npm:@anthropic-ai/bedrock-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/bedrock-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/bedrock-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/bedrock-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/bedrock-sdk")')
43+
(cd dist && node -e 'import("@anthropic-ai/bedrock-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 Anthropic from '@anthropic-ai/sdk';
4+
import { AnthropicBedrock } from '@anthropic-ai/bedrock-sdk';
5+
6+
// Note: this assumes you have configured AWS credentials in a way
7+
// that the AWS Node SDK will recognise, typicaly a shared `~/.aws/credentials`
8+
// file or `AWS_ACCESS_KEY_ID` & `AWS_SECRET_ACCESS_KEY` environment variables.
9+
//
10+
// https://docs.aws.amazon.com/AWSJavaScriptSDK/v3/latest/modules/_aws_sdk_credential_provider_node.html
11+
const anthropic = new AnthropicBedrock();
12+
13+
async function main() {
14+
const completion = await anthropic.completions.create({
15+
model: 'anthropic.claude-instant-v1',
16+
prompt: `${Anthropic.HUMAN_PROMPT} how does a court case get to the Supreme Court? ${Anthropic.AI_PROMPT}`,
17+
stop_sequences: [Anthropic.HUMAN_PROMPT],
18+
max_tokens_to_sample: 800,
19+
temperature: 0.5,
20+
top_k: 250,
21+
top_p: 0.5,
22+
});
23+
console.log(completion);
24+
}
25+
26+
main();
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
#!/usr/bin/env -S npm run tsn -T
2+
3+
import Anthropic from '@anthropic-ai/sdk';
4+
import { AnthropicBedrock } from '@anthropic-ai/bedrock-sdk';
5+
6+
// Note: this assumes you have configured AWS credentials in a way
7+
// that the AWS Node SDK will recognise, typicaly a shared `~/.aws/credentials`
8+
// file or `AWS_ACCESS_KEY_ID` & `AWS_SECRET_ACCESS_KEY` environment variables.
9+
//
10+
// https://docs.aws.amazon.com/AWSJavaScriptSDK/v3/latest/modules/_aws_sdk_credential_provider_node.html
11+
const client = new AnthropicBedrock();
12+
13+
async function main() {
14+
const question = 'Hey Claude! How can I recursively list all files in a directory in Rust?';
15+
16+
const stream = await client.completions.create({
17+
prompt: `${Anthropic.HUMAN_PROMPT}${question}${Anthropic.AI_PROMPT}:`,
18+
model: 'anthropic.claude-v2:1',
19+
stream: true,
20+
max_tokens_to_sample: 500,
21+
});
22+
23+
for await (const completion of stream) {
24+
process.stdout.write(completion.completion);
25+
}
26+
}
27+
28+
main();

packages/bedrock-sdk/package.json

Lines changed: 81 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,81 @@
1+
{
2+
"name": "@anthropic-ai/bedrock-sdk",
3+
"version": "0.6.5",
4+
"description": "The official TypeScript library for the Anthropic Bedrock 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+
"@aws-crypto/sha256-js": "^4.0.0",
27+
"@aws-sdk/client-bedrock-runtime": "^3.423.0",
28+
"@aws-sdk/credential-providers": "^3.341.0",
29+
"@aws-sdk/protocol-http": "^3.341.0",
30+
"@aws-sdk/signature-v4": "^3.341.0",
31+
"@smithy/eventstream-serde-node": "^2.0.10",
32+
"@smithy/fetch-http-handler": "^2.2.1",
33+
"@smithy/protocol-http": "^3.0.6",
34+
"@smithy/smithy-client": "^2.1.9",
35+
"@smithy/types": "^2.3.4",
36+
"@smithy/util-base64": "^2.0.0"
37+
},
38+
"devDependencies": {
39+
"@types/jest": "^29.4.0",
40+
"@typescript-eslint/eslint-plugin": "^6.7.0",
41+
"@typescript-eslint/parser": "^6.7.0",
42+
"eslint": "^8.49.0",
43+
"eslint-plugin-prettier": "^5.0.1",
44+
"eslint-plugin-unused-imports": "^3.0.0",
45+
"jest": "^29.4.0",
46+
"prettier": "^3.0.0",
47+
"ts-jest": "^29.1.0",
48+
"ts-morph": "^19.0.0",
49+
"ts-node": "^10.5.0",
50+
"tsc-multi": "^1.1.0",
51+
"tsconfig-paths": "^4.0.0",
52+
"typescript": "^4.8.2"
53+
},
54+
"imports": {
55+
"@anthropic-ai/bedrock-sdk": ".",
56+
"@anthropic-ai/bedrock-sdk/*": "./src/*"
57+
},
58+
"exports": {
59+
".": {
60+
"require": {
61+
"types": "./dist/index.d.ts",
62+
"default": "./dist/index.js"
63+
},
64+
"types": "./dist/index.d.mts",
65+
"default": "./dist/index.mjs"
66+
},
67+
"./*.mjs": {
68+
"types": "./dist/*.d.ts",
69+
"default": "./dist/*.mjs"
70+
},
71+
"./*.js": {
72+
"types": "./dist/*.d.ts",
73+
"default": "./dist/*.js"
74+
},
75+
"./*": {
76+
"types": "./dist/*.d.ts",
77+
"require": "./dist/*.js",
78+
"default": "./dist/*.mjs"
79+
}
80+
}
81+
}
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)