@anthropic-ai/sdk 0.4.4 → 0.5.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/_shims/agent.d.ts +9 -0
- package/_shims/agent.d.ts.map +1 -0
- package/_shims/agent.js +14 -0
- package/_shims/agent.js.map +1 -0
- package/_shims/agent.mjs +10 -0
- package/_shims/agent.mjs.map +1 -0
- package/_shims/agent.node.d.ts +7 -0
- package/_shims/agent.node.d.ts.map +1 -0
- package/_shims/agent.node.js +28 -0
- package/_shims/agent.node.js.map +1 -0
- package/_shims/agent.node.mjs +16 -0
- package/_shims/agent.node.mjs.map +1 -0
- package/_shims/fetch.d.ts +52 -0
- package/_shims/fetch.js +13 -0
- package/_shims/fetch.mjs +15 -0
- package/_shims/fetch.node.d.ts +53 -0
- package/_shims/fetch.node.js +12 -0
- package/_shims/fetch.node.mjs +14 -0
- package/_shims/fileFromPath.d.ts +22 -0
- package/_shims/fileFromPath.d.ts.map +1 -0
- package/_shims/fileFromPath.js +16 -0
- package/_shims/fileFromPath.js.map +1 -0
- package/_shims/fileFromPath.mjs +12 -0
- package/_shims/fileFromPath.mjs.map +1 -0
- package/_shims/fileFromPath.node.d.ts +17 -0
- package/_shims/fileFromPath.node.d.ts.map +1 -0
- package/_shims/fileFromPath.node.js +17 -0
- package/_shims/fileFromPath.node.js.map +1 -0
- package/_shims/fileFromPath.node.mjs +13 -0
- package/_shims/fileFromPath.node.mjs.map +1 -0
- package/_shims/formdata.d.ts +43 -0
- package/_shims/formdata.js +9 -0
- package/_shims/formdata.mjs +11 -0
- package/_shims/formdata.node.d.ts +44 -0
- package/_shims/formdata.node.js +11 -0
- package/_shims/formdata.node.mjs +9 -0
- package/_shims/getMultipartRequestOptions.d.ts +10 -0
- package/_shims/getMultipartRequestOptions.d.ts.map +1 -0
- package/_shims/getMultipartRequestOptions.js +12 -0
- package/_shims/getMultipartRequestOptions.js.map +1 -0
- package/_shims/getMultipartRequestOptions.mjs +8 -0
- package/_shims/getMultipartRequestOptions.mjs.map +1 -0
- package/_shims/getMultipartRequestOptions.node.d.ts +10 -0
- package/_shims/getMultipartRequestOptions.node.d.ts.map +1 -0
- package/_shims/getMultipartRequestOptions.node.js +22 -0
- package/_shims/getMultipartRequestOptions.node.js.map +1 -0
- package/_shims/getMultipartRequestOptions.node.mjs +18 -0
- package/_shims/getMultipartRequestOptions.node.mjs.map +1 -0
- package/_shims/node-readable.d.ts +23 -0
- package/_shims/node-readable.d.ts.map +1 -0
- package/_shims/node-readable.js +11 -0
- package/_shims/node-readable.js.map +1 -0
- package/_shims/node-readable.mjs +7 -0
- package/_shims/node-readable.mjs.map +1 -0
- package/_shims/node-readable.node.d.ts +8 -0
- package/_shims/node-readable.node.d.ts.map +1 -0
- package/_shims/node-readable.node.js +9 -0
- package/_shims/node-readable.node.js.map +1 -0
- package/_shims/node-readable.node.mjs +5 -0
- package/_shims/node-readable.node.mjs.map +1 -0
- package/core.d.ts +226 -0
- package/core.d.ts.map +1 -0
- package/core.js +683 -0
- package/core.js.map +1 -0
- package/core.mjs +587 -0
- package/core.mjs.map +1 -0
- package/error.d.ts +48 -0
- package/error.d.ts.map +1 -0
- package/error.js +126 -0
- package/error.js.map +1 -0
- package/error.mjs +101 -0
- package/error.mjs.map +1 -0
- package/index.d.mts +104 -0
- package/index.d.ts +104 -0
- package/index.d.ts.map +1 -0
- package/index.js +177 -0
- package/index.js.map +1 -0
- package/index.mjs +118 -0
- package/index.mjs.map +1 -0
- package/package.json +73 -23
- package/resource.d.ts +12 -0
- package/resource.d.ts.map +1 -0
- package/resource.js +17 -0
- package/resource.js.map +1 -0
- package/resource.mjs +13 -0
- package/resource.mjs.map +1 -0
- package/resources/completions.d.ts +311 -0
- package/resources/completions.d.ts.map +1 -0
- package/resources/completions.js +19 -0
- package/resources/completions.js.map +1 -0
- package/resources/completions.mjs +15 -0
- package/resources/completions.mjs.map +1 -0
- package/resources/index.d.ts +2 -0
- package/resources/index.d.ts.map +1 -0
- package/resources/index.js +12 -0
- package/resources/index.js.map +1 -0
- package/resources/index.mjs +3 -0
- package/resources/index.mjs.map +1 -0
- package/resources/top-level.d.ts +2 -0
- package/resources/top-level.d.ts.map +1 -0
- package/resources/top-level.js +4 -0
- package/resources/top-level.js.map +1 -0
- package/resources/top-level.mjs +3 -0
- package/resources/top-level.mjs.map +1 -0
- package/src/_shims/agent.node.ts +22 -0
- package/src/_shims/agent.ts +12 -0
- package/src/_shims/fetch.d.ts +52 -0
- package/src/_shims/fetch.js +13 -0
- package/src/_shims/fetch.mjs +15 -0
- package/src/_shims/fetch.node.d.ts +53 -0
- package/src/_shims/fetch.node.js +12 -0
- package/src/_shims/fetch.node.mjs +14 -0
- package/src/_shims/fileFromPath.node.ts +29 -0
- package/src/_shims/fileFromPath.ts +29 -0
- package/src/_shims/formdata.d.ts +43 -0
- package/src/_shims/formdata.js +9 -0
- package/src/_shims/formdata.mjs +11 -0
- package/src/_shims/formdata.node.d.ts +44 -0
- package/src/_shims/formdata.node.js +11 -0
- package/src/_shims/formdata.node.mjs +9 -0
- package/src/_shims/getMultipartRequestOptions.node.ts +25 -0
- package/src/_shims/getMultipartRequestOptions.ts +14 -0
- package/src/_shims/node-readable.node.ts +10 -0
- package/src/_shims/node-readable.ts +30 -0
- package/src/core.ts +783 -0
- package/src/error.ts +115 -0
- package/src/index.ts +200 -0
- package/src/resource.ts +24 -0
- package/src/resources/completions.ts +344 -0
- package/src/resources/index.ts +3 -0
- package/src/resources/top-level.ts +3 -0
- package/src/streaming.ts +220 -0
- package/src/uploads.ts +248 -0
- package/src/version.ts +1 -0
- package/streaming.d.ts +14 -0
- package/streaming.d.ts.map +1 -0
- package/streaming.js +168 -0
- package/streaming.js.map +1 -0
- package/streaming.mjs +164 -0
- package/streaming.mjs.map +1 -0
- package/uploads.d.ts +90 -0
- package/uploads.d.ts.map +1 -0
- package/uploads.js +207 -0
- package/uploads.js.map +1 -0
- package/uploads.mjs +174 -0
- package/uploads.mjs.map +1 -0
- package/version.d.ts +2 -0
- package/version.d.ts.map +1 -0
- package/version.js +5 -0
- package/version.js.map +1 -0
- package/version.mjs +2 -0
- package/version.mjs.map +1 -0
- package/LICENSE +0 -21
- package/README.md +0 -34
- package/build/src/index.d.ts +0 -39
- package/build/src/index.js +0 -116
package/src/error.ts
ADDED
|
@@ -0,0 +1,115 @@
|
|
|
1
|
+
// File generated from our OpenAPI spec by Stainless.
|
|
2
|
+
|
|
3
|
+
import { castToError, Headers } from './core';
|
|
4
|
+
|
|
5
|
+
export class APIError extends Error {
|
|
6
|
+
readonly status: number | undefined;
|
|
7
|
+
readonly headers: Headers | undefined;
|
|
8
|
+
readonly error: Object | undefined;
|
|
9
|
+
|
|
10
|
+
constructor(
|
|
11
|
+
status: number | undefined,
|
|
12
|
+
error: Object | undefined,
|
|
13
|
+
message: string | undefined,
|
|
14
|
+
headers: Headers | undefined,
|
|
15
|
+
) {
|
|
16
|
+
super(message || (error as any)?.message || 'Unknown error occurred.');
|
|
17
|
+
this.status = status;
|
|
18
|
+
this.headers = headers;
|
|
19
|
+
this.error = error;
|
|
20
|
+
}
|
|
21
|
+
|
|
22
|
+
static generate(
|
|
23
|
+
status: number | undefined,
|
|
24
|
+
errorResponse: Object | undefined,
|
|
25
|
+
message: string | undefined,
|
|
26
|
+
headers: Headers | undefined,
|
|
27
|
+
) {
|
|
28
|
+
if (!status) {
|
|
29
|
+
return new APIConnectionError({ cause: castToError(errorResponse) });
|
|
30
|
+
}
|
|
31
|
+
|
|
32
|
+
const error = errorResponse as Record<string, any>;
|
|
33
|
+
|
|
34
|
+
if (status === 400) {
|
|
35
|
+
return new BadRequestError(status, error, message, headers);
|
|
36
|
+
}
|
|
37
|
+
|
|
38
|
+
if (status === 401) {
|
|
39
|
+
return new AuthenticationError(status, error, message, headers);
|
|
40
|
+
}
|
|
41
|
+
|
|
42
|
+
if (status === 403) {
|
|
43
|
+
return new PermissionDeniedError(status, error, message, headers);
|
|
44
|
+
}
|
|
45
|
+
|
|
46
|
+
if (status === 404) {
|
|
47
|
+
return new NotFoundError(status, error, message, headers);
|
|
48
|
+
}
|
|
49
|
+
|
|
50
|
+
if (status === 409) {
|
|
51
|
+
return new ConflictError(status, error, message, headers);
|
|
52
|
+
}
|
|
53
|
+
|
|
54
|
+
if (status === 422) {
|
|
55
|
+
return new UnprocessableEntityError(status, error, message, headers);
|
|
56
|
+
}
|
|
57
|
+
|
|
58
|
+
if (status === 429) {
|
|
59
|
+
return new RateLimitError(status, error, message, headers);
|
|
60
|
+
}
|
|
61
|
+
|
|
62
|
+
if (status >= 500) {
|
|
63
|
+
return new InternalServerError(status, error, message, headers);
|
|
64
|
+
}
|
|
65
|
+
|
|
66
|
+
return new APIError(status, error, message, headers);
|
|
67
|
+
}
|
|
68
|
+
}
|
|
69
|
+
|
|
70
|
+
export class APIConnectionError extends APIError {
|
|
71
|
+
override readonly status: undefined = undefined;
|
|
72
|
+
|
|
73
|
+
constructor({ message, cause }: { message?: string; cause?: Error | undefined }) {
|
|
74
|
+
super(undefined, undefined, message || 'Connection error.', undefined);
|
|
75
|
+
// in some environments the 'cause' property is already declared
|
|
76
|
+
// @ts-ignore
|
|
77
|
+
if (cause) this.cause = cause;
|
|
78
|
+
}
|
|
79
|
+
}
|
|
80
|
+
|
|
81
|
+
export class APIConnectionTimeoutError extends APIConnectionError {
|
|
82
|
+
constructor() {
|
|
83
|
+
super({ message: 'Request timed out.' });
|
|
84
|
+
}
|
|
85
|
+
}
|
|
86
|
+
|
|
87
|
+
export class BadRequestError extends APIError {
|
|
88
|
+
override readonly status: 400 = 400;
|
|
89
|
+
}
|
|
90
|
+
|
|
91
|
+
export class AuthenticationError extends APIError {
|
|
92
|
+
override readonly status: 401 = 401;
|
|
93
|
+
}
|
|
94
|
+
|
|
95
|
+
export class PermissionDeniedError extends APIError {
|
|
96
|
+
override readonly status: 403 = 403;
|
|
97
|
+
}
|
|
98
|
+
|
|
99
|
+
export class NotFoundError extends APIError {
|
|
100
|
+
override readonly status: 404 = 404;
|
|
101
|
+
}
|
|
102
|
+
|
|
103
|
+
export class ConflictError extends APIError {
|
|
104
|
+
override readonly status: 409 = 409;
|
|
105
|
+
}
|
|
106
|
+
|
|
107
|
+
export class UnprocessableEntityError extends APIError {
|
|
108
|
+
override readonly status: 422 = 422;
|
|
109
|
+
}
|
|
110
|
+
|
|
111
|
+
export class RateLimitError extends APIError {
|
|
112
|
+
override readonly status: 429 = 429;
|
|
113
|
+
}
|
|
114
|
+
|
|
115
|
+
export class InternalServerError extends APIError {}
|
package/src/index.ts
ADDED
|
@@ -0,0 +1,200 @@
|
|
|
1
|
+
// File generated from our OpenAPI spec by Stainless.
|
|
2
|
+
|
|
3
|
+
import * as qs from 'qs';
|
|
4
|
+
import * as Core from './core';
|
|
5
|
+
import * as API from './resources/index';
|
|
6
|
+
import * as Errors from './error';
|
|
7
|
+
import type { Agent } from '@anthropic-ai/sdk/_shims/agent';
|
|
8
|
+
import * as Uploads from './uploads';
|
|
9
|
+
|
|
10
|
+
type Config = {
|
|
11
|
+
/**
|
|
12
|
+
* Defaults to process.env["ANTHROPIC_API_KEY"]. Set it to null if you want to send unauthenticated requests.
|
|
13
|
+
*/
|
|
14
|
+
apiKey?: string | null;
|
|
15
|
+
|
|
16
|
+
/**
|
|
17
|
+
* Override the default base URL for the API, e.g., "https://api.example.com/v2/"
|
|
18
|
+
*/
|
|
19
|
+
baseURL?: string;
|
|
20
|
+
|
|
21
|
+
/**
|
|
22
|
+
* The maximum amount of time (in milliseconds) that the client should wait for a response
|
|
23
|
+
* from the server before timing out a single request.
|
|
24
|
+
*
|
|
25
|
+
* Note that request timeouts are retried by default, so in a worst-case scenario you may wait
|
|
26
|
+
* much longer than this timeout before the promise succeeds or fails.
|
|
27
|
+
*/
|
|
28
|
+
timeout?: number;
|
|
29
|
+
|
|
30
|
+
/**
|
|
31
|
+
* An HTTP agent used to manage HTTP(S) connections.
|
|
32
|
+
*
|
|
33
|
+
* If not provided, an agent will be constructed by default in the Node.js environment,
|
|
34
|
+
* otherwise no agent is used.
|
|
35
|
+
*/
|
|
36
|
+
httpAgent?: Agent;
|
|
37
|
+
|
|
38
|
+
/**
|
|
39
|
+
* The maximum number of times that the client will retry a request in case of a
|
|
40
|
+
* temporary failure, like a network error or a 5XX error from the server.
|
|
41
|
+
*
|
|
42
|
+
* @default 2
|
|
43
|
+
*/
|
|
44
|
+
maxRetries?: number;
|
|
45
|
+
|
|
46
|
+
/**
|
|
47
|
+
* Default headers to include with every request to the API.
|
|
48
|
+
*
|
|
49
|
+
* These can be removed in individual requests by explicitly setting the
|
|
50
|
+
* header to `undefined` or `null` in request options.
|
|
51
|
+
*/
|
|
52
|
+
defaultHeaders?: Core.Headers;
|
|
53
|
+
|
|
54
|
+
/**
|
|
55
|
+
* Default query parameters to include with every request to the API.
|
|
56
|
+
*
|
|
57
|
+
* These can be removed in individual requests by explicitly setting the
|
|
58
|
+
* param to `undefined` in request options.
|
|
59
|
+
*/
|
|
60
|
+
defaultQuery?: Core.DefaultQuery;
|
|
61
|
+
|
|
62
|
+
authToken?: string | null;
|
|
63
|
+
};
|
|
64
|
+
|
|
65
|
+
/** Instantiate the API Client. */
|
|
66
|
+
export class Anthropic extends Core.APIClient {
|
|
67
|
+
apiKey: string | null;
|
|
68
|
+
authToken?: string | null;
|
|
69
|
+
|
|
70
|
+
private _options: Config;
|
|
71
|
+
|
|
72
|
+
constructor(config?: Config) {
|
|
73
|
+
const options: Config = {
|
|
74
|
+
apiKey: typeof process === 'undefined' ? '' : process.env['ANTHROPIC_API_KEY'] || '',
|
|
75
|
+
baseURL: 'https://api.anthropic.com',
|
|
76
|
+
...config,
|
|
77
|
+
};
|
|
78
|
+
|
|
79
|
+
super({
|
|
80
|
+
baseURL: options.baseURL!,
|
|
81
|
+
timeout: options.timeout,
|
|
82
|
+
httpAgent: options.httpAgent,
|
|
83
|
+
maxRetries: options.maxRetries,
|
|
84
|
+
});
|
|
85
|
+
this.apiKey = options.apiKey || null;
|
|
86
|
+
this._options = options;
|
|
87
|
+
|
|
88
|
+
this.authToken = config?.authToken || process.env['ANTHROPIC_AUTH_TOKEN'] || null;
|
|
89
|
+
}
|
|
90
|
+
|
|
91
|
+
completions: API.Completions = new API.Completions(this);
|
|
92
|
+
|
|
93
|
+
protected override defaultQuery(): Core.DefaultQuery | undefined {
|
|
94
|
+
return this._options.defaultQuery;
|
|
95
|
+
}
|
|
96
|
+
|
|
97
|
+
protected override defaultHeaders(): Core.Headers {
|
|
98
|
+
return {
|
|
99
|
+
...super.defaultHeaders(),
|
|
100
|
+
'anthropic-version': '2023-06-01',
|
|
101
|
+
...this._options.defaultHeaders,
|
|
102
|
+
};
|
|
103
|
+
}
|
|
104
|
+
|
|
105
|
+
protected override validateHeaders(headers: Core.Headers, customHeaders: Core.Headers) {
|
|
106
|
+
if (this.apiKey && headers['X-Api-Key']) {
|
|
107
|
+
return;
|
|
108
|
+
}
|
|
109
|
+
if (customHeaders['X-Api-Key'] === null) {
|
|
110
|
+
return;
|
|
111
|
+
}
|
|
112
|
+
|
|
113
|
+
if (this.authToken && headers['Authorization']) {
|
|
114
|
+
return;
|
|
115
|
+
}
|
|
116
|
+
if (customHeaders['Authorization'] === null) {
|
|
117
|
+
return;
|
|
118
|
+
}
|
|
119
|
+
|
|
120
|
+
throw new Error(
|
|
121
|
+
'Could not resolve authentication method. Expected either apiKey or authToken to be set. Or for one of the "X-Api-Key" or "Authorization" headers to be explicitly omitted',
|
|
122
|
+
);
|
|
123
|
+
}
|
|
124
|
+
|
|
125
|
+
protected override authHeaders(): Core.Headers {
|
|
126
|
+
const apiKeyHeader = this.apiKeyHeader();
|
|
127
|
+
if (apiKeyHeader != null && !Core.isEmptyObj(apiKeyHeader)) {
|
|
128
|
+
return apiKeyHeader;
|
|
129
|
+
}
|
|
130
|
+
const authTokenBearer = this.authTokenBearer();
|
|
131
|
+
if (authTokenBearer != null && !Core.isEmptyObj(authTokenBearer)) {
|
|
132
|
+
return authTokenBearer;
|
|
133
|
+
}
|
|
134
|
+
return {};
|
|
135
|
+
}
|
|
136
|
+
|
|
137
|
+
protected apiKeyHeader(): Core.Headers {
|
|
138
|
+
if (this.apiKey == null) {
|
|
139
|
+
return {};
|
|
140
|
+
}
|
|
141
|
+
return { 'X-Api-Key': this.apiKey };
|
|
142
|
+
}
|
|
143
|
+
|
|
144
|
+
protected authTokenBearer(): Core.Headers {
|
|
145
|
+
if (this.authToken == null) {
|
|
146
|
+
return {};
|
|
147
|
+
}
|
|
148
|
+
return { Authorization: `Bearer ${this.authToken}` };
|
|
149
|
+
}
|
|
150
|
+
|
|
151
|
+
protected override qsOptions(): qs.IStringifyOptions {
|
|
152
|
+
return { arrayFormat: 'comma' };
|
|
153
|
+
}
|
|
154
|
+
|
|
155
|
+
static HUMAN_PROMPT = '\n\nHuman:';
|
|
156
|
+
static AI_PROMPT = '\n\nAssistant:';
|
|
157
|
+
|
|
158
|
+
static APIError = Errors.APIError;
|
|
159
|
+
static APIConnectionError = Errors.APIConnectionError;
|
|
160
|
+
static APIConnectionTimeoutError = Errors.APIConnectionTimeoutError;
|
|
161
|
+
static NotFoundError = Errors.NotFoundError;
|
|
162
|
+
static ConflictError = Errors.ConflictError;
|
|
163
|
+
static RateLimitError = Errors.RateLimitError;
|
|
164
|
+
static BadRequestError = Errors.BadRequestError;
|
|
165
|
+
static AuthenticationError = Errors.AuthenticationError;
|
|
166
|
+
static InternalServerError = Errors.InternalServerError;
|
|
167
|
+
static PermissionDeniedError = Errors.PermissionDeniedError;
|
|
168
|
+
static UnprocessableEntityError = Errors.UnprocessableEntityError;
|
|
169
|
+
}
|
|
170
|
+
|
|
171
|
+
export const { HUMAN_PROMPT, AI_PROMPT } = Anthropic;
|
|
172
|
+
|
|
173
|
+
export const {
|
|
174
|
+
APIError,
|
|
175
|
+
APIConnectionError,
|
|
176
|
+
APIConnectionTimeoutError,
|
|
177
|
+
NotFoundError,
|
|
178
|
+
ConflictError,
|
|
179
|
+
RateLimitError,
|
|
180
|
+
BadRequestError,
|
|
181
|
+
AuthenticationError,
|
|
182
|
+
InternalServerError,
|
|
183
|
+
PermissionDeniedError,
|
|
184
|
+
UnprocessableEntityError,
|
|
185
|
+
} = Errors;
|
|
186
|
+
|
|
187
|
+
export import toFile = Uploads.toFile;
|
|
188
|
+
export import fileFromPath = Uploads.fileFromPath;
|
|
189
|
+
|
|
190
|
+
export namespace Anthropic {
|
|
191
|
+
// Helper functions
|
|
192
|
+
export import toFile = Uploads.toFile;
|
|
193
|
+
export import fileFromPath = Uploads.fileFromPath;
|
|
194
|
+
|
|
195
|
+
export import Completions = API.Completions;
|
|
196
|
+
export import Completion = API.Completion;
|
|
197
|
+
export import CompletionCreateParams = API.CompletionCreateParams;
|
|
198
|
+
}
|
|
199
|
+
|
|
200
|
+
export default Anthropic;
|
package/src/resource.ts
ADDED
|
@@ -0,0 +1,24 @@
|
|
|
1
|
+
// File generated from our OpenAPI spec by Stainless.
|
|
2
|
+
|
|
3
|
+
import type { Anthropic } from './index';
|
|
4
|
+
|
|
5
|
+
export class APIResource {
|
|
6
|
+
protected client: Anthropic;
|
|
7
|
+
constructor(client: Anthropic) {
|
|
8
|
+
this.client = client;
|
|
9
|
+
|
|
10
|
+
this.get = client.get.bind(client);
|
|
11
|
+
this.post = client.post.bind(client);
|
|
12
|
+
this.patch = client.patch.bind(client);
|
|
13
|
+
this.put = client.put.bind(client);
|
|
14
|
+
this.delete = client.delete.bind(client);
|
|
15
|
+
this.getAPIList = client.getAPIList.bind(client);
|
|
16
|
+
}
|
|
17
|
+
|
|
18
|
+
protected get: Anthropic['get'];
|
|
19
|
+
protected post: Anthropic['post'];
|
|
20
|
+
protected patch: Anthropic['patch'];
|
|
21
|
+
protected put: Anthropic['put'];
|
|
22
|
+
protected delete: Anthropic['delete'];
|
|
23
|
+
protected getAPIList: Anthropic['getAPIList'];
|
|
24
|
+
}
|
|
@@ -0,0 +1,344 @@
|
|
|
1
|
+
// File generated from our OpenAPI spec by Stainless.
|
|
2
|
+
|
|
3
|
+
import * as Core from '@anthropic-ai/sdk/core';
|
|
4
|
+
import { APIResource } from '@anthropic-ai/sdk/resource';
|
|
5
|
+
import * as API from './';
|
|
6
|
+
import { Stream } from '@anthropic-ai/sdk/streaming';
|
|
7
|
+
|
|
8
|
+
export class Completions extends APIResource {
|
|
9
|
+
/**
|
|
10
|
+
* Create a completion
|
|
11
|
+
*/
|
|
12
|
+
create(
|
|
13
|
+
body: CompletionCreateParams.CompletionRequestNonStreaming,
|
|
14
|
+
options?: Core.RequestOptions,
|
|
15
|
+
): Promise<Core.APIResponse<Completion>>;
|
|
16
|
+
create(
|
|
17
|
+
body: CompletionCreateParams.CompletionRequestStreaming,
|
|
18
|
+
options?: Core.RequestOptions,
|
|
19
|
+
): Promise<Core.APIResponse<Stream<Completion>>>;
|
|
20
|
+
create(
|
|
21
|
+
body: CompletionCreateParams,
|
|
22
|
+
options?: Core.RequestOptions,
|
|
23
|
+
): Promise<Core.APIResponse<Completion | Stream<Completion>>> {
|
|
24
|
+
return this.post('/v1/complete', { body, timeout: 600000, ...options, stream: body.stream ?? false });
|
|
25
|
+
}
|
|
26
|
+
}
|
|
27
|
+
|
|
28
|
+
export interface Completion {
|
|
29
|
+
/**
|
|
30
|
+
* The resulting completion up to and excluding the stop sequences.
|
|
31
|
+
*/
|
|
32
|
+
completion: string;
|
|
33
|
+
|
|
34
|
+
/**
|
|
35
|
+
* The model that performed the completion.
|
|
36
|
+
*/
|
|
37
|
+
model: string;
|
|
38
|
+
|
|
39
|
+
/**
|
|
40
|
+
* The reason that we stopped sampling.
|
|
41
|
+
*
|
|
42
|
+
* This may be one the following values:
|
|
43
|
+
*
|
|
44
|
+
* - `"stop_sequence"`: we reached a stop sequence — either provided by you via the
|
|
45
|
+
* `stop_sequences` parameter, or a stop sequence built into the model
|
|
46
|
+
* - `"max_tokens"`: we exceeded `max_tokens_to_sample` or the model's maximum
|
|
47
|
+
*/
|
|
48
|
+
stop_reason: string;
|
|
49
|
+
}
|
|
50
|
+
|
|
51
|
+
export type CompletionCreateParams =
|
|
52
|
+
| CompletionCreateParams.CompletionRequestNonStreaming
|
|
53
|
+
| CompletionCreateParams.CompletionRequestStreaming;
|
|
54
|
+
|
|
55
|
+
export namespace CompletionCreateParams {
|
|
56
|
+
export interface CompletionRequestNonStreaming {
|
|
57
|
+
/**
|
|
58
|
+
* The maximum number of tokens to generate before stopping.
|
|
59
|
+
*
|
|
60
|
+
* Note that our models may stop _before_ reaching this maximum. This parameter
|
|
61
|
+
* only specifies the absolute maximum number of tokens to generate.
|
|
62
|
+
*/
|
|
63
|
+
max_tokens_to_sample: number;
|
|
64
|
+
|
|
65
|
+
/**
|
|
66
|
+
* The model that will complete your prompt.
|
|
67
|
+
*
|
|
68
|
+
* As we improve Claude, we develop new versions of it that you can query. This
|
|
69
|
+
* controls which version of Claude answers your request. Right now we are offering
|
|
70
|
+
* two model families: Claude and Claude Instant.
|
|
71
|
+
*
|
|
72
|
+
* Specifiying any of the following models will automatically switch to you the
|
|
73
|
+
* newest compatible models as they are released:
|
|
74
|
+
*
|
|
75
|
+
* - `"claude-1"`: Our largest model, ideal for a wide range of more complex tasks.
|
|
76
|
+
* - `"claude-1-100k"`: An enhanced version of `claude-1` with a 100,000 token
|
|
77
|
+
* (roughly 75,000 word) context window. Ideal for summarizing, analyzing, and
|
|
78
|
+
* querying long documents and conversations for nuanced understanding of complex
|
|
79
|
+
* topics and relationships across very long spans of text.
|
|
80
|
+
* - `"claude-instant-1"`: A smaller model with far lower latency, sampling at
|
|
81
|
+
* roughly 40 words/sec! Its output quality is somewhat lower than the latest
|
|
82
|
+
* `claude-1` model, particularly for complex tasks. However, it is much less
|
|
83
|
+
* expensive and blazing fast. We believe that this model provides more than
|
|
84
|
+
* adequate performance on a range of tasks including text classification,
|
|
85
|
+
* summarization, and lightweight chat applications, as well as search result
|
|
86
|
+
* summarization.
|
|
87
|
+
* - `"claude-instant-1-100k"`: An enhanced version of `claude-instant-1` with a
|
|
88
|
+
* 100,000 token context window that retains its performance. Well-suited for
|
|
89
|
+
* high throughput use cases needing both speed and additional context, allowing
|
|
90
|
+
* deeper understanding from extended conversations and documents.
|
|
91
|
+
*
|
|
92
|
+
* You can also select specific sub-versions of the above models:
|
|
93
|
+
*
|
|
94
|
+
* - `"claude-1.3"`: Compared to `claude-1.2`, it's more robust against red-team
|
|
95
|
+
* inputs, better at precise instruction-following, better at code, and better
|
|
96
|
+
* and non-English dialogue and writing.
|
|
97
|
+
* - `"claude-1.3-100k"`: An enhanced version of `claude-1.3` with a 100,000 token
|
|
98
|
+
* (roughly 75,000 word) context window.
|
|
99
|
+
* - `"claude-1.2"`: An improved version of `claude-1`. It is slightly improved at
|
|
100
|
+
* general helpfulness, instruction following, coding, and other tasks. It is
|
|
101
|
+
* also considerably better with non-English languages. This model also has the
|
|
102
|
+
* ability to role play (in harmless ways) more consistently, and it defaults to
|
|
103
|
+
* writing somewhat longer and more thorough responses.
|
|
104
|
+
* - `"claude-1.0"`: An earlier version of `claude-1`.
|
|
105
|
+
* - `"claude-instant-1.1"`: Our latest version of `claude-instant-1`. It is better
|
|
106
|
+
* than `claude-instant-1.0` at a wide variety of tasks including writing,
|
|
107
|
+
* coding, and instruction following. It performs better on academic benchmarks,
|
|
108
|
+
* including math, reading comprehension, and coding tests. It is also more
|
|
109
|
+
* robust against red-teaming inputs.
|
|
110
|
+
* - `"claude-instant-1.1-100k"`: An enhanced version of `claude-instant-1.1` with
|
|
111
|
+
* a 100,000 token context window that retains its lightning fast 40 word/sec
|
|
112
|
+
* performance.
|
|
113
|
+
* - `"claude-instant-1.0"`: An earlier version of `claude-instant-1`.
|
|
114
|
+
*/
|
|
115
|
+
model: string;
|
|
116
|
+
|
|
117
|
+
/**
|
|
118
|
+
* The prompt that you want Claude to complete.
|
|
119
|
+
*
|
|
120
|
+
* For proper response generation you will need to format your prompt as follows:
|
|
121
|
+
*
|
|
122
|
+
* ```javascript
|
|
123
|
+
* const userQuestion = r"Why is the sky blue?";
|
|
124
|
+
* const prompt = `\n\nHuman: ${userQuestion}\n\nAssistant:`;
|
|
125
|
+
* ```
|
|
126
|
+
*
|
|
127
|
+
* See our [comments on prompts](https://console.anthropic.com/docs/prompt-design)
|
|
128
|
+
* for more context.
|
|
129
|
+
*/
|
|
130
|
+
prompt: string;
|
|
131
|
+
|
|
132
|
+
/**
|
|
133
|
+
* An object describing metadata about the request.
|
|
134
|
+
*/
|
|
135
|
+
metadata?: CompletionCreateParams.CompletionRequestNonStreaming.Metadata;
|
|
136
|
+
|
|
137
|
+
/**
|
|
138
|
+
* Sequences that will cause the model to stop generating completion text.
|
|
139
|
+
*
|
|
140
|
+
* Our models stop on `"\n\nHuman:"`, and may include additional built-in stop
|
|
141
|
+
* sequences in the future. By providing the stop_sequences parameter, you may
|
|
142
|
+
* include additional strings that will cause the model to stop generating.
|
|
143
|
+
*/
|
|
144
|
+
stop_sequences?: Array<string>;
|
|
145
|
+
|
|
146
|
+
/**
|
|
147
|
+
* Whether to incrementally stream the response using server-sent events.
|
|
148
|
+
*
|
|
149
|
+
* See
|
|
150
|
+
* [this guide to SSE events](https://developer.mozilla.org/en-US/docs/Web/API/Server-sent_events/Using_server-sent_events)
|
|
151
|
+
* for details.
|
|
152
|
+
*/
|
|
153
|
+
stream?: false;
|
|
154
|
+
|
|
155
|
+
/**
|
|
156
|
+
* Amount of randomness injected into the response.
|
|
157
|
+
*
|
|
158
|
+
* Defaults to 1. Ranges from 0 to 1. Use temp closer to 0 for analytical /
|
|
159
|
+
* multiple choice, and closer to 1 for creative and generative tasks.
|
|
160
|
+
*/
|
|
161
|
+
temperature?: number;
|
|
162
|
+
|
|
163
|
+
/**
|
|
164
|
+
* Only sample from the top K options for each subsequent token.
|
|
165
|
+
*
|
|
166
|
+
* Used to remove "long tail" low probability responses.
|
|
167
|
+
* [Learn more technical details here](https://towardsdatascience.com/how-to-sample-from-language-models-682bceb97277).
|
|
168
|
+
*/
|
|
169
|
+
top_k?: number;
|
|
170
|
+
|
|
171
|
+
/**
|
|
172
|
+
* Use nucleus sampling.
|
|
173
|
+
*
|
|
174
|
+
* In nucleus sampling, we compute the cumulative distribution over all the options
|
|
175
|
+
* for each subsequent token in decreasing probability order and cut it off once it
|
|
176
|
+
* reaches a particular probability specified by `top_p`. You should either alter
|
|
177
|
+
* `temperature` or `top_p`, but not both.
|
|
178
|
+
*/
|
|
179
|
+
top_p?: number;
|
|
180
|
+
}
|
|
181
|
+
|
|
182
|
+
export namespace CompletionRequestNonStreaming {
|
|
183
|
+
/**
|
|
184
|
+
* An object describing metadata about the request.
|
|
185
|
+
*/
|
|
186
|
+
export interface Metadata {
|
|
187
|
+
/**
|
|
188
|
+
* An external identifier for the user who is associated with the request.
|
|
189
|
+
*
|
|
190
|
+
* This should be a uuid, hash value, or other opaque identifier. Anthropic may use
|
|
191
|
+
* this id to help detect abuse. Do not include any identifying information such as
|
|
192
|
+
* name, email address, or phone number.
|
|
193
|
+
*/
|
|
194
|
+
user_id?: string;
|
|
195
|
+
}
|
|
196
|
+
}
|
|
197
|
+
|
|
198
|
+
export interface CompletionRequestStreaming {
|
|
199
|
+
/**
|
|
200
|
+
* The maximum number of tokens to generate before stopping.
|
|
201
|
+
*
|
|
202
|
+
* Note that our models may stop _before_ reaching this maximum. This parameter
|
|
203
|
+
* only specifies the absolute maximum number of tokens to generate.
|
|
204
|
+
*/
|
|
205
|
+
max_tokens_to_sample: number;
|
|
206
|
+
|
|
207
|
+
/**
|
|
208
|
+
* The model that will complete your prompt.
|
|
209
|
+
*
|
|
210
|
+
* As we improve Claude, we develop new versions of it that you can query. This
|
|
211
|
+
* controls which version of Claude answers your request. Right now we are offering
|
|
212
|
+
* two model families: Claude and Claude Instant.
|
|
213
|
+
*
|
|
214
|
+
* Specifiying any of the following models will automatically switch to you the
|
|
215
|
+
* newest compatible models as they are released:
|
|
216
|
+
*
|
|
217
|
+
* - `"claude-1"`: Our largest model, ideal for a wide range of more complex tasks.
|
|
218
|
+
* - `"claude-1-100k"`: An enhanced version of `claude-1` with a 100,000 token
|
|
219
|
+
* (roughly 75,000 word) context window. Ideal for summarizing, analyzing, and
|
|
220
|
+
* querying long documents and conversations for nuanced understanding of complex
|
|
221
|
+
* topics and relationships across very long spans of text.
|
|
222
|
+
* - `"claude-instant-1"`: A smaller model with far lower latency, sampling at
|
|
223
|
+
* roughly 40 words/sec! Its output quality is somewhat lower than the latest
|
|
224
|
+
* `claude-1` model, particularly for complex tasks. However, it is much less
|
|
225
|
+
* expensive and blazing fast. We believe that this model provides more than
|
|
226
|
+
* adequate performance on a range of tasks including text classification,
|
|
227
|
+
* summarization, and lightweight chat applications, as well as search result
|
|
228
|
+
* summarization.
|
|
229
|
+
* - `"claude-instant-1-100k"`: An enhanced version of `claude-instant-1` with a
|
|
230
|
+
* 100,000 token context window that retains its performance. Well-suited for
|
|
231
|
+
* high throughput use cases needing both speed and additional context, allowing
|
|
232
|
+
* deeper understanding from extended conversations and documents.
|
|
233
|
+
*
|
|
234
|
+
* You can also select specific sub-versions of the above models:
|
|
235
|
+
*
|
|
236
|
+
* - `"claude-1.3"`: Compared to `claude-1.2`, it's more robust against red-team
|
|
237
|
+
* inputs, better at precise instruction-following, better at code, and better
|
|
238
|
+
* and non-English dialogue and writing.
|
|
239
|
+
* - `"claude-1.3-100k"`: An enhanced version of `claude-1.3` with a 100,000 token
|
|
240
|
+
* (roughly 75,000 word) context window.
|
|
241
|
+
* - `"claude-1.2"`: An improved version of `claude-1`. It is slightly improved at
|
|
242
|
+
* general helpfulness, instruction following, coding, and other tasks. It is
|
|
243
|
+
* also considerably better with non-English languages. This model also has the
|
|
244
|
+
* ability to role play (in harmless ways) more consistently, and it defaults to
|
|
245
|
+
* writing somewhat longer and more thorough responses.
|
|
246
|
+
* - `"claude-1.0"`: An earlier version of `claude-1`.
|
|
247
|
+
* - `"claude-instant-1.1"`: Our latest version of `claude-instant-1`. It is better
|
|
248
|
+
* than `claude-instant-1.0` at a wide variety of tasks including writing,
|
|
249
|
+
* coding, and instruction following. It performs better on academic benchmarks,
|
|
250
|
+
* including math, reading comprehension, and coding tests. It is also more
|
|
251
|
+
* robust against red-teaming inputs.
|
|
252
|
+
* - `"claude-instant-1.1-100k"`: An enhanced version of `claude-instant-1.1` with
|
|
253
|
+
* a 100,000 token context window that retains its lightning fast 40 word/sec
|
|
254
|
+
* performance.
|
|
255
|
+
* - `"claude-instant-1.0"`: An earlier version of `claude-instant-1`.
|
|
256
|
+
*/
|
|
257
|
+
model: string;
|
|
258
|
+
|
|
259
|
+
/**
|
|
260
|
+
* The prompt that you want Claude to complete.
|
|
261
|
+
*
|
|
262
|
+
* For proper response generation you will need to format your prompt as follows:
|
|
263
|
+
*
|
|
264
|
+
* ```javascript
|
|
265
|
+
* const userQuestion = r"Why is the sky blue?";
|
|
266
|
+
* const prompt = `\n\nHuman: ${userQuestion}\n\nAssistant:`;
|
|
267
|
+
* ```
|
|
268
|
+
*
|
|
269
|
+
* See our [comments on prompts](https://console.anthropic.com/docs/prompt-design)
|
|
270
|
+
* for more context.
|
|
271
|
+
*/
|
|
272
|
+
prompt: string;
|
|
273
|
+
|
|
274
|
+
/**
|
|
275
|
+
* Whether to incrementally stream the response using server-sent events.
|
|
276
|
+
*
|
|
277
|
+
* See
|
|
278
|
+
* [this guide to SSE events](https://developer.mozilla.org/en-US/docs/Web/API/Server-sent_events/Using_server-sent_events)
|
|
279
|
+
* for details.
|
|
280
|
+
*/
|
|
281
|
+
stream: true;
|
|
282
|
+
|
|
283
|
+
/**
|
|
284
|
+
* An object describing metadata about the request.
|
|
285
|
+
*/
|
|
286
|
+
metadata?: CompletionCreateParams.CompletionRequestStreaming.Metadata;
|
|
287
|
+
|
|
288
|
+
/**
|
|
289
|
+
* Sequences that will cause the model to stop generating completion text.
|
|
290
|
+
*
|
|
291
|
+
* Our models stop on `"\n\nHuman:"`, and may include additional built-in stop
|
|
292
|
+
* sequences in the future. By providing the stop_sequences parameter, you may
|
|
293
|
+
* include additional strings that will cause the model to stop generating.
|
|
294
|
+
*/
|
|
295
|
+
stop_sequences?: Array<string>;
|
|
296
|
+
|
|
297
|
+
/**
|
|
298
|
+
* Amount of randomness injected into the response.
|
|
299
|
+
*
|
|
300
|
+
* Defaults to 1. Ranges from 0 to 1. Use temp closer to 0 for analytical /
|
|
301
|
+
* multiple choice, and closer to 1 for creative and generative tasks.
|
|
302
|
+
*/
|
|
303
|
+
temperature?: number;
|
|
304
|
+
|
|
305
|
+
/**
|
|
306
|
+
* Only sample from the top K options for each subsequent token.
|
|
307
|
+
*
|
|
308
|
+
* Used to remove "long tail" low probability responses.
|
|
309
|
+
* [Learn more technical details here](https://towardsdatascience.com/how-to-sample-from-language-models-682bceb97277).
|
|
310
|
+
*/
|
|
311
|
+
top_k?: number;
|
|
312
|
+
|
|
313
|
+
/**
|
|
314
|
+
* Use nucleus sampling.
|
|
315
|
+
*
|
|
316
|
+
* In nucleus sampling, we compute the cumulative distribution over all the options
|
|
317
|
+
* for each subsequent token in decreasing probability order and cut it off once it
|
|
318
|
+
* reaches a particular probability specified by `top_p`. You should either alter
|
|
319
|
+
* `temperature` or `top_p`, but not both.
|
|
320
|
+
*/
|
|
321
|
+
top_p?: number;
|
|
322
|
+
}
|
|
323
|
+
|
|
324
|
+
export namespace CompletionRequestStreaming {
|
|
325
|
+
/**
|
|
326
|
+
* An object describing metadata about the request.
|
|
327
|
+
*/
|
|
328
|
+
export interface Metadata {
|
|
329
|
+
/**
|
|
330
|
+
* An external identifier for the user who is associated with the request.
|
|
331
|
+
*
|
|
332
|
+
* This should be a uuid, hash value, or other opaque identifier. Anthropic may use
|
|
333
|
+
* this id to help detect abuse. Do not include any identifying information such as
|
|
334
|
+
* name, email address, or phone number.
|
|
335
|
+
*/
|
|
336
|
+
user_id?: string;
|
|
337
|
+
}
|
|
338
|
+
}
|
|
339
|
+
}
|
|
340
|
+
|
|
341
|
+
export namespace Completions {
|
|
342
|
+
export import Completion = API.Completion;
|
|
343
|
+
export import CompletionCreateParams = API.CompletionCreateParams;
|
|
344
|
+
}
|