Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
56 changes: 40 additions & 16 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,7 @@

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

The Anthropic TypeScript library provides convenient access to the Anthropic REST API from applications written in server-side JavaScript.
It includes TypeScript definitions for all request params and response fields.
This library provides convenient access to the Anthropic TypeScript REST API from server-side TypeScript or JavaScript.

## Migration from v0.4.x and below

Expand Down Expand Up @@ -79,8 +78,6 @@ Key interface changes:

</details>

## Documentation

The API documentation can be found [here](https://docs.anthropic.com/claude/reference/).

## Installation
Expand All @@ -104,7 +101,7 @@ async function main() {
const completion = await anthropic.completions.create({
model: 'claude-2',
max_tokens_to_sample: 300,
prompt: `${Anthropic.HUMAN_PROMPT} how does a court case get to the Supreme Court? ${Anthropic.AI_PROMPT}`,
prompt: `${Anthropic.HUMAN_PROMPT} how does a court case get to the Supreme Court?${Anthropic.AI_PROMPT}`,
});
}

Expand All @@ -121,7 +118,7 @@ import Anthropic from '@anthropic-ai/sdk';
const anthropic = new Anthropic();

const stream = await anthropic.completions.create({
prompt: `${Anthropic.HUMAN_PROMPT} Your prompt here ${Anthropic.AI_PROMPT}`,
prompt: `${Anthropic.HUMAN_PROMPT} Your prompt here${Anthropic.AI_PROMPT}`,
model: 'claude-2',
stream: true,
max_tokens_to_sample: 300,
Expand All @@ -134,10 +131,9 @@ for await (const completion of stream) {
If you need to cancel a stream, you can `break` from the loop
or call `stream.controller.abort()`.

### Usage with TypeScript
### Request & Response types

Importing, instantiating, and interacting with the library are the same as above.
If you like, you may reference our types directly:
This library includes TypeScript definitions for all request params and response fields. You may import and use them like so:

```ts
import Anthropic from '@anthropic-ai/sdk';
Expand All @@ -148,7 +144,7 @@ const anthropic = new Anthropic({

async function main() {
const params: Anthropic.CompletionCreateParams = {
prompt: `${Anthropic.HUMAN_PROMPT} how does a court case get to the Supreme Court? ${Anthropic.AI_PROMPT}`,
prompt: `${Anthropic.HUMAN_PROMPT} how does a court case get to the Supreme Court?${Anthropic.AI_PROMPT}`,
max_tokens_to_sample: 300,
model: 'claude-2',
};
Expand Down Expand Up @@ -176,7 +172,7 @@ a subclass of `APIError` will be thrown:
async function main() {
const completion = await anthropic.completions
.create({
prompt: `${Anthropic.HUMAN_PROMPT} Your prompt here ${Anthropic.AI_PROMPT}`,
prompt: `${Anthropic.HUMAN_PROMPT} Your prompt here${Anthropic.AI_PROMPT}`,
max_tokens_to_sample: 300,
model: 'claude-2',
})
Expand Down Expand Up @@ -225,7 +221,7 @@ const anthropic = new Anthropic({
// Or, configure per-request:
await anthropic.completions.create(
{
prompt: `${Anthropic.HUMAN_PROMPT} Can you help me effectively ask for a raise at work? ${Anthropic.AI_PROMPT}`,
prompt: `${Anthropic.HUMAN_PROMPT} Can you help me effectively ask for a raise at work?${Anthropic.AI_PROMPT}`,
max_tokens_to_sample: 300,
model: 'claude-2',
},
Expand All @@ -249,7 +245,7 @@ const anthropic = new Anthropic({
// Override per-request:
await anthropic.completions.create(
{
prompt: `${Anthropic.HUMAN_PROMPT} Where can I get a good coffee in my neighbourhood? ${Anthropic.AI_PROMPT}`,
prompt: `${Anthropic.HUMAN_PROMPT} Where can I get a good coffee in my neighbourhood?${Anthropic.AI_PROMPT}`,
max_tokens_to_sample: 300,
model: 'claude-2',
},
Expand All @@ -263,6 +259,32 @@ On timeout, an `APIConnectionTimeoutError` is thrown.

Note that requests which time out will be [retried twice by default](#retries).

## Advanced Usage

### Accessing raw Response data (e.g., headers)

The "raw" `Response` returned by `fetch()` can be accessed through the `.asResponse()` method on the `APIPromise` type that all methods return.

You can also use the `.withResponse()` method to get the raw `Response` along with the parsed data.

```ts
const anthropic = new Anthropic();

const response = await anthropic.completions
.create({
prompt: `${Anthropic.HUMAN_PROMPT} Can you help me effectively ask for a raise at work?${Anthropic.AI_PROMPT}`,
max_tokens_to_sample: 300,
model: 'claude-2',
})
.asResponse();
console.log(response.headers.get('X-My-Header'));
console.log(response.raw.statusText); // access the underlying Response object

// parses the response body, returning an object if the API responds with JSON
const completion: Completions.Completion = await response.parse();
console.log(completion.completion);
```

## Configuring an HTTP(S) Agent (e.g., for proxies)

By default, this library uses a stable agent for all http/https requests to reuse TCP connections, eliminating many TCP & TLS handshakes and shaving around 100ms off most requests.
Expand All @@ -283,7 +305,7 @@ const anthropic = new Anthropic({
// Override per-request:
await anthropic.completions.create(
{
prompt: `${Anthropic.HUMAN_PROMPT} How does a court case get to the Supreme Court? ${Anthropic.AI_PROMPT}`,
prompt: `${Anthropic.HUMAN_PROMPT} How does a court case get to the Supreme Court?${Anthropic.AI_PROMPT}`,
max_tokens_to_sample: 300,
model: 'claude-2',
},
Expand Down Expand Up @@ -311,7 +333,9 @@ We are keen for your feedback; please open an [issue](https://www.github.com/ant
The following runtimes are supported:

- Node.js 16 LTS or later ([non-EOL](https://endoflife.date/nodejs)) versions.
- Deno v1.28.0 or higher (experimental).
Use `import Anthropic from "npm:@anthropic-ai/sdk"`.
- Deno v1.28.0 or higher, using `import Anthropic from "npm:@anthropic-ai/sdk"`.
Deno Deploy is not yet supported.
- Cloudflare Workers.
- Vercel Edge Runtime.

If you are interested in other runtime environments, please open or upvote an issue on GitHub.
4 changes: 4 additions & 0 deletions build
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,10 @@ cp -rp src README.md dist
for file in LICENSE CHANGELOG.md; do
if [ -e "${file}" ]; then cp "${file}" dist; fi
done
if [ -e "bin/cli" ]; then
mkdir dist/bin
cp -p "bin/cli" dist/bin/;
fi
# this converts the export map paths for the dist directory
# and does a few other minor things
node scripts/make-dist-package-json.cjs > dist/package.json
Expand Down
2 changes: 1 addition & 1 deletion jest.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,5 +7,5 @@ module.exports = {
'^@anthropic-ai/sdk/_shims/(.*)$': '<rootDir>/src/_shims/$1.node',
'^@anthropic-ai/sdk/(.*)$': '<rootDir>/src/$1',
},
modulePathIgnorePatterns: ['<rootDir>/ecosystem-tests/', '<rootDir>/dist/'],
modulePathIgnorePatterns: ['<rootDir>/ecosystem-tests/', '<rootDir>/dist/', '<rootDir>/deno_tests/'],
};
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,7 @@
"ts-node": "^10.5.0",
"tsc-alias": "^1.8.6",
"tsc-multi": "^1.1.0",
"tsconfig-paths": "^3.12.0",
"tsconfig-paths": "^4.0.0",
"typescript": "^4.8.2"
}
}
38 changes: 38 additions & 0 deletions src/_shims/ReadableStream.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
/**
* Disclaimer: modules in _shims aren't intended to be imported by SDK users.
*/

/**
* >>> Confused? <<<
*
* If you're getting errors from these types, try adding "lib": ["DOM"]
* to your tsconfig.json, or otherwise configure the appropriate builtin
* `ReadableStream` type for your environment.
*/

// @ts-ignore
type _ReadableStream<R = any> = unknown extends ReadableStream ? never : ReadableStream<R>;
declare const _ReadableStream: {
prototype: _ReadableStream;
new (
underlyingSource: _UnderlyingByteSource,
strategy?: { highWaterMark?: number },
): _ReadableStream<Uint8Array>;
new <R = any>(
underlyingSource: _UnderlyingDefaultSource<R>,
strategy?: _QueuingStrategy<R>,
): _ReadableStream<R>;
new <R = any>(underlyingSource?: _UnderlyingSource<R>, strategy?: _QueuingStrategy<R>): _ReadableStream<R>;
};

// @ts-ignore
type _UnderlyingSource<R = any> = unknown extends UnderlyingSource ? never : UnderlyingSource<R>;
// @ts-ignore
type _UnderlyingByteSource = unknown extends UnderlyingByteSource ? never : UnderlyingByteSource;
type _UnderlyingDefaultSource<R = any> =
// @ts-ignore
unknown extends UnderlyingDefaultSource ? never : UnderlyingDefaultSource<R>;
// @ts-ignore
type _QueuingStrategy<R = any> = unknown extends QueuingStrategy ? never : QueuingStrategy<R>;

export { _ReadableStream as ReadableStream };
5 changes: 5 additions & 0 deletions src/_shims/ReadableStream.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
/**
* Disclaimer: modules in _shims aren't intended to be imported by SDK users.
*/

exports.ReadableStream = ReadableStream;
7 changes: 7 additions & 0 deletions src/_shims/ReadableStream.mjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
/**
* Disclaimer: modules in _shims aren't intended to be imported by SDK users.
*/

const _ReadableStream = ReadableStream;

export { _ReadableStream as ReadableStream };
6 changes: 6 additions & 0 deletions src/_shims/ReadableStream.node.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
/**
* Disclaimer: modules in _shims aren't intended to be imported by SDK users.
*/
import { ReadableStream } from 'web-streams-polyfill';

export { ReadableStream };
9 changes: 8 additions & 1 deletion src/_shims/fetch.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,8 @@ type _Response = unknown extends Response ? never : Response;
// @ts-ignore
type _ResponseInit = unknown extends ResponseInit ? never : ResponseInit;
// @ts-ignore
type _ResponseType = unknown extends ResponseType ? never : ResponseType;
// @ts-ignore
type _BodyInit = unknown extends BodyInit ? never : BodyInit;
// @ts-ignore
type _Headers = unknown extends Headers ? never : Headers;
Expand All @@ -49,4 +51,9 @@ declare const _Headers: {
export const isPolyfilled = false;

export { _fetch as fetch, _Request as Request, _Response as Response, _Headers as Headers };
export type { _RequestInit as RequestInit, _RequestInfo as RequestInfo, _BodyInit as BodyInit };
export type {
_RequestInit as RequestInit,
_RequestInfo as RequestInfo,
_ResponseType as ResponseType,
_BodyInit as BodyInit,
};
12 changes: 11 additions & 1 deletion src/_shims/fetch.node.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,11 @@ type _RequestInit = unknown extends RequestInit ? nf.RequestInit : RequestInit;
type _Response = unknown extends Response ? nf.Response : Response;
// @ts-ignore
type _ResponseInit = unknown extends ResponseInit ? nf.ResponseInit : ResponseInit;
type _ResponseType =
// @ts-ignore
unknown extends ResponseType ? 'basic' | 'cors' | 'default' | 'error' | 'opaque' | 'opaqueredirect'
: // @ts-ignore
ResponseType;
// @ts-ignore
type _BodyInit = unknown extends BodyInit ? nf.BodyInit : BodyInit;
// @ts-ignore
Expand All @@ -50,4 +55,9 @@ declare const _Headers: {
export const isPolyfilled = false;

export { _fetch as fetch, _Request as Request, _Response as Response, _Headers as Headers };
export type { _RequestInit as RequestInit, _RequestInfo as RequestInfo, _BodyInit as BodyInit };
export type {
_RequestInit as RequestInit,
_RequestInfo as RequestInfo,
_ResponseType as ResponseType,
_BodyInit as BodyInit,
};
Loading