@anthropic-ai/sdk 0.3.1 → 0.4.1
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 +2 -0
- package/build/src/index.d.ts +6 -3
- package/build/src/index.js +16 -7
- package/package.json +3 -2
package/README.md
CHANGED
|
@@ -4,6 +4,8 @@ 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
|
+
**Note that this package only officially supports Node 16+.**
|
|
8
|
+
|
|
7
9
|
## Installation
|
|
8
10
|
|
|
9
11
|
Install using your package manager of choice. For example:
|
package/build/src/index.d.ts
CHANGED
|
@@ -1,8 +1,11 @@
|
|
|
1
1
|
export type SamplingParameters = {
|
|
2
2
|
prompt: string;
|
|
3
|
-
|
|
4
|
-
stop_sequences: string[];
|
|
3
|
+
temperature?: number;
|
|
5
4
|
max_tokens_to_sample: number;
|
|
5
|
+
stop_sequences: string[];
|
|
6
|
+
top_k?: number;
|
|
7
|
+
top_p?: number;
|
|
8
|
+
model: string;
|
|
6
9
|
};
|
|
7
10
|
export type OnOpen = (response: Response) => void | Promise<void>;
|
|
8
11
|
export type OnUpdate = (completion: CompletionResponse) => void | Promise<void>;
|
|
@@ -11,7 +14,7 @@ export declare const AI_PROMPT = "\n\nAssistant:";
|
|
|
11
14
|
export type CompletionResponse = {
|
|
12
15
|
completion: string;
|
|
13
16
|
stop: string | null;
|
|
14
|
-
stop_reason:
|
|
17
|
+
stop_reason: "stop_sequence" | "max_tokens";
|
|
15
18
|
truncated: boolean;
|
|
16
19
|
exception: string | null;
|
|
17
20
|
log_id: string;
|
package/build/src/index.js
CHANGED
|
@@ -8,17 +8,22 @@ var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, ge
|
|
|
8
8
|
step((generator = generator.apply(thisArg, _arguments || [])).next());
|
|
9
9
|
});
|
|
10
10
|
};
|
|
11
|
+
var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
12
|
+
return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
13
|
+
};
|
|
11
14
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
12
15
|
exports.Client = exports.AI_PROMPT = exports.HUMAN_PROMPT = void 0;
|
|
13
16
|
const fetch_event_source_1 = require("@fortaine/fetch-event-source");
|
|
17
|
+
const cross_fetch_1 = __importDefault(require("cross-fetch"));
|
|
14
18
|
exports.HUMAN_PROMPT = "\n\nHuman:";
|
|
15
19
|
exports.AI_PROMPT = "\n\nAssistant:";
|
|
16
|
-
const CLIENT_ID = "anthropic-typescript/0.
|
|
20
|
+
const CLIENT_ID = "anthropic-typescript/0.4.1";
|
|
17
21
|
const DEFAULT_API_URL = "https://api.anthropic.com";
|
|
18
22
|
var Event;
|
|
19
23
|
(function (Event) {
|
|
20
24
|
Event["Ping"] = "ping";
|
|
21
25
|
})(Event || (Event = {}));
|
|
26
|
+
const DONE_MESSAGE = "[DONE]";
|
|
22
27
|
class Client {
|
|
23
28
|
constructor(apiKey, options) {
|
|
24
29
|
var _a;
|
|
@@ -27,7 +32,7 @@ class Client {
|
|
|
27
32
|
}
|
|
28
33
|
complete(params) {
|
|
29
34
|
return __awaiter(this, void 0, void 0, function* () {
|
|
30
|
-
const response = yield
|
|
35
|
+
const response = yield (0, cross_fetch_1.default)(`${this.apiUrl}/v1/complete`, {
|
|
31
36
|
method: "POST",
|
|
32
37
|
headers: {
|
|
33
38
|
Accept: "application/json",
|
|
@@ -35,7 +40,7 @@ class Client {
|
|
|
35
40
|
Client: CLIENT_ID,
|
|
36
41
|
"X-API-Key": this.apiKey,
|
|
37
42
|
},
|
|
38
|
-
body: JSON.stringify(params),
|
|
43
|
+
body: JSON.stringify(Object.assign(Object.assign({}, params), { stream: false })),
|
|
39
44
|
});
|
|
40
45
|
if (!response.ok) {
|
|
41
46
|
const error = new Error(`Sampling error: ${response.status} ${response.statusText}`);
|
|
@@ -72,17 +77,21 @@ class Client {
|
|
|
72
77
|
if (ev.event === Event.Ping) {
|
|
73
78
|
return;
|
|
74
79
|
}
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
return resolve(completion);
|
|
80
|
+
if (ev.data === DONE_MESSAGE) {
|
|
81
|
+
console.error("Unexpected done message before stop_reason has been issued");
|
|
82
|
+
return;
|
|
79
83
|
}
|
|
84
|
+
const completion = JSON.parse(ev.data);
|
|
80
85
|
if (onUpdate) {
|
|
81
86
|
Promise.resolve(onUpdate(completion)).catch((error) => {
|
|
82
87
|
abortController.abort();
|
|
83
88
|
reject(error);
|
|
84
89
|
});
|
|
85
90
|
}
|
|
91
|
+
if (completion.stop_reason !== null) {
|
|
92
|
+
abortController.abort();
|
|
93
|
+
return resolve(completion);
|
|
94
|
+
}
|
|
86
95
|
},
|
|
87
96
|
onerror: (error) => {
|
|
88
97
|
console.error("Sampling error:", error);
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@anthropic-ai/sdk",
|
|
3
|
-
"version": "0.
|
|
3
|
+
"version": "0.4.1",
|
|
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",
|