@anthropic-ai/sdk 0.3.0 → 0.4.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.
@@ -0,0 +1,30 @@
1
+ export type SamplingParameters = {
2
+ prompt: string;
3
+ model: string;
4
+ stop_sequences: string[];
5
+ max_tokens_to_sample: number;
6
+ };
7
+ export type OnOpen = (response: Response) => void | Promise<void>;
8
+ export type OnUpdate = (completion: CompletionResponse) => void | Promise<void>;
9
+ export declare const HUMAN_PROMPT = "\n\nHuman:";
10
+ export declare const AI_PROMPT = "\n\nAssistant:";
11
+ export type CompletionResponse = {
12
+ completion: string;
13
+ stop: string | null;
14
+ stop_reason: string | null;
15
+ truncated: boolean;
16
+ exception: string | null;
17
+ log_id: string;
18
+ };
19
+ export declare class Client {
20
+ private apiKey;
21
+ private apiUrl;
22
+ constructor(apiKey: string, options?: {
23
+ apiUrl?: string;
24
+ });
25
+ complete(params: SamplingParameters): Promise<CompletionResponse>;
26
+ completeStream(params: SamplingParameters, { onOpen, onUpdate }: {
27
+ onOpen?: OnOpen;
28
+ onUpdate?: OnUpdate;
29
+ }): Promise<CompletionResponse>;
30
+ }
@@ -0,0 +1,100 @@
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
+ var __importDefault = (this && this.__importDefault) || function (mod) {
12
+ return (mod && mod.__esModule) ? mod : { "default": mod };
13
+ };
14
+ Object.defineProperty(exports, "__esModule", { value: true });
15
+ exports.Client = exports.AI_PROMPT = exports.HUMAN_PROMPT = void 0;
16
+ const fetch_event_source_1 = require("@fortaine/fetch-event-source");
17
+ const cross_fetch_1 = __importDefault(require("cross-fetch"));
18
+ exports.HUMAN_PROMPT = "\n\nHuman:";
19
+ exports.AI_PROMPT = "\n\nAssistant:";
20
+ const CLIENT_ID = "anthropic-typescript/0.4.0";
21
+ const DEFAULT_API_URL = "https://api.anthropic.com";
22
+ var Event;
23
+ (function (Event) {
24
+ Event["Ping"] = "ping";
25
+ })(Event || (Event = {}));
26
+ class Client {
27
+ constructor(apiKey, options) {
28
+ var _a;
29
+ this.apiKey = apiKey;
30
+ this.apiUrl = (_a = options === null || options === void 0 ? void 0 : options.apiUrl) !== null && _a !== void 0 ? _a : DEFAULT_API_URL;
31
+ }
32
+ complete(params) {
33
+ return __awaiter(this, void 0, void 0, function* () {
34
+ const response = yield (0, cross_fetch_1.default)(`${this.apiUrl}/v1/complete`, {
35
+ method: "POST",
36
+ headers: {
37
+ Accept: "application/json",
38
+ "Content-Type": "application/json",
39
+ Client: CLIENT_ID,
40
+ "X-API-Key": this.apiKey,
41
+ },
42
+ body: JSON.stringify(params),
43
+ });
44
+ if (!response.ok) {
45
+ const error = new Error(`Sampling error: ${response.status} ${response.statusText}`);
46
+ console.error(error);
47
+ throw error;
48
+ }
49
+ const completion = (yield response.json());
50
+ return completion;
51
+ });
52
+ }
53
+ completeStream(params, { onOpen, onUpdate }) {
54
+ const abortController = new AbortController();
55
+ return new Promise((resolve, reject) => {
56
+ (0, fetch_event_source_1.fetchEventSource)(`${this.apiUrl}/v1/complete`, {
57
+ method: "POST",
58
+ headers: {
59
+ Accept: "application/json",
60
+ "Content-Type": "application/json",
61
+ Client: CLIENT_ID,
62
+ "X-API-Key": this.apiKey,
63
+ },
64
+ body: JSON.stringify(Object.assign(Object.assign({}, params), { stream: true })),
65
+ signal: abortController.signal,
66
+ onopen: (response) => __awaiter(this, void 0, void 0, function* () {
67
+ if (!response.ok) {
68
+ abortController.abort();
69
+ return reject(Error(`Failed to open sampling stream, HTTP status code ${response.status}: ${response.statusText}`));
70
+ }
71
+ if (onOpen) {
72
+ yield Promise.resolve(onOpen(response));
73
+ }
74
+ }),
75
+ onmessage: (ev) => {
76
+ if (ev.event === Event.Ping) {
77
+ return;
78
+ }
79
+ const completion = JSON.parse(ev.data);
80
+ if (completion.stop_reason !== null) {
81
+ abortController.abort();
82
+ return resolve(completion);
83
+ }
84
+ if (onUpdate) {
85
+ Promise.resolve(onUpdate(completion)).catch((error) => {
86
+ abortController.abort();
87
+ reject(error);
88
+ });
89
+ }
90
+ },
91
+ onerror: (error) => {
92
+ console.error("Sampling error:", error);
93
+ abortController.abort();
94
+ return reject(error);
95
+ },
96
+ });
97
+ });
98
+ }
99
+ }
100
+ exports.Client = Client;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@anthropic-ai/sdk",
3
- "version": "0.3.0",
3
+ "version": "0.4.0",
4
4
  "description": "Library for accessing the Anthropic API",
5
5
  "repository": "https://github.com/anthropics/anthropic-sdk-typescript",
6
6
  "license": "MIT",
@@ -18,7 +18,8 @@
18
18
  "example:basic_sync": "tsc --build && node ./build/examples/basic_sync.js"
19
19
  },
20
20
  "dependencies": {
21
- "@fortaine/fetch-event-source": "^3.0.6"
21
+ "@fortaine/fetch-event-source": "^3.0.6",
22
+ "cross-fetch": "^3.1.5"
22
23
  },
23
24
  "devDependencies": {
24
25
  "@types/node": "^18.11.18",