@anthropic-ai/sdk 0.2.4 → 0.3.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/README.md CHANGED
@@ -1,10 +1,24 @@
1
- # Anthropic TypeScript SDK
1
+ # Anthropic TypeScript SDK [![npm](https://img.shields.io/npm/v/@anthropic-ai/sdk)](https://www.npmjs.com/package/@anthropic-ai/sdk)
2
2
 
3
3
  This package provides access to Anthropic's safety-first language model APIs.
4
4
 
5
5
  For more information on our APIs, please check the confidential documentation portal.
6
6
 
7
- ## Getting started
7
+ ## Installation
8
+
9
+ Install using your package manager of choice. For example:
10
+
11
+ ```sh
12
+ npm install @anthropic-ai/sdk
13
+ ```
14
+
15
+ … or:
16
+
17
+ ```sh
18
+ yarn add @anthropic-ai/sdk
19
+ ```
20
+
21
+ ## Development and testing
8
22
 
9
23
  Examples are available in the [examples/](examples/) folder.
10
24
 
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@anthropic-ai/sdk",
3
- "version": "0.2.4",
3
+ "version": "0.3.0",
4
4
  "description": "Library for accessing the Anthropic API",
5
5
  "repository": "https://github.com/anthropics/anthropic-sdk-typescript",
6
6
  "license": "MIT",
@@ -1,26 +0,0 @@
1
- export type SamplingParameters = {
2
- prompt: string;
3
- model: string;
4
- stop_sequences: string[];
5
- max_tokens_to_sample: number;
6
- };
7
- export type OnSampleChange = (completion: CompletionResponse) => void | Promise<void>;
8
- export declare const HUMAN_PROMPT = "\n\nHuman:";
9
- export declare const AI_PROMPT = "\n\nAssistant:";
10
- export type CompletionResponse = {
11
- completion: string;
12
- stop: string | null;
13
- stop_reason: string | null;
14
- truncated: boolean;
15
- exception: string | null;
16
- log_id: string;
17
- };
18
- export declare class Client {
19
- private apiKey;
20
- private apiUrl;
21
- constructor(apiKey: string, options?: {
22
- apiUrl?: string;
23
- });
24
- complete(params: SamplingParameters): Promise<CompletionResponse>;
25
- completeStream(params: SamplingParameters, onSampleChange: OnSampleChange): Promise<CompletionResponse>;
26
- }
@@ -1,93 +0,0 @@
1
- "use strict";
2
- var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
3
- function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
4
- return new (P || (P = Promise))(function (resolve, reject) {
5
- function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
6
- function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
7
- function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
8
- step((generator = generator.apply(thisArg, _arguments || [])).next());
9
- });
10
- };
11
- Object.defineProperty(exports, "__esModule", { value: true });
12
- exports.Client = exports.AI_PROMPT = exports.HUMAN_PROMPT = void 0;
13
- const fetch_event_source_1 = require("@fortaine/fetch-event-source");
14
- exports.HUMAN_PROMPT = "\n\nHuman:";
15
- exports.AI_PROMPT = "\n\nAssistant:";
16
- const CLIENT_ID = "anthropic-typescript/0.2.3";
17
- const DEFAULT_API_URL = "https://api.anthropic.com";
18
- var Event;
19
- (function (Event) {
20
- Event["Ping"] = "ping";
21
- })(Event || (Event = {}));
22
- class Client {
23
- constructor(apiKey, options) {
24
- var _a;
25
- this.apiKey = apiKey;
26
- this.apiUrl = (_a = options === null || options === void 0 ? void 0 : options.apiUrl) !== null && _a !== void 0 ? _a : DEFAULT_API_URL;
27
- }
28
- complete(params) {
29
- return __awaiter(this, void 0, void 0, function* () {
30
- const response = yield fetch(`${this.apiUrl}/v1/complete`, {
31
- method: "POST",
32
- headers: {
33
- Accept: "application/json",
34
- "Content-Type": "application/json",
35
- Client: CLIENT_ID,
36
- "X-API-Key": this.apiKey,
37
- },
38
- body: JSON.stringify(params),
39
- });
40
- if (!response.ok) {
41
- const error = new Error(`Sampling error: ${response.status} ${response.statusText}`);
42
- console.error(error);
43
- throw error;
44
- }
45
- const completion = (yield response.json());
46
- return completion;
47
- });
48
- }
49
- completeStream(params, onSampleChange) {
50
- const abortController = new AbortController();
51
- return new Promise((resolve, reject) => {
52
- (0, fetch_event_source_1.fetchEventSource)(`${this.apiUrl}/v1/complete`, {
53
- method: "POST",
54
- headers: {
55
- Accept: "application/json",
56
- "Content-Type": "application/json",
57
- Client: CLIENT_ID,
58
- "X-API-Key": this.apiKey,
59
- },
60
- body: JSON.stringify(Object.assign(Object.assign({}, params), { stream: true })),
61
- signal: abortController.signal,
62
- onopen: (response) => __awaiter(this, void 0, void 0, function* () {
63
- if (!response.ok) {
64
- abortController.abort();
65
- return reject(Error(`Failed to open sampling stream, HTTP status code ${response.status}: ${response.statusText}`));
66
- }
67
- }),
68
- onmessage: (ev) => {
69
- if (ev.event === Event.Ping) {
70
- return;
71
- }
72
- const completion = JSON.parse(ev.data);
73
- if (completion.stop_reason !== null) {
74
- abortController.abort();
75
- return resolve(completion);
76
- }
77
- if (onSampleChange) {
78
- Promise.resolve(onSampleChange(completion)).catch((error) => {
79
- abortController.abort();
80
- reject(error);
81
- });
82
- }
83
- },
84
- onerror: (error) => {
85
- console.error("Sampling error:", error);
86
- abortController.abort();
87
- return reject(error);
88
- },
89
- });
90
- });
91
- }
92
- }
93
- exports.Client = Client;