modelfusion 0.114.1 → 0.115.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/CHANGELOG.md +6 -0
- package/README.md +5 -6
- package/model-provider/index.cjs +0 -1
- package/model-provider/index.d.ts +0 -1
- package/model-provider/index.js +0 -1
- package/package.json +1 -1
- package/model-provider/anthropic/AnthropicApiConfiguration.cjs +0 -31
- package/model-provider/anthropic/AnthropicApiConfiguration.d.ts +0 -10
- package/model-provider/anthropic/AnthropicApiConfiguration.js +0 -27
- package/model-provider/anthropic/AnthropicError.cjs +0 -16
- package/model-provider/anthropic/AnthropicError.d.ts +0 -26
- package/model-provider/anthropic/AnthropicError.js +0 -13
- package/model-provider/anthropic/AnthropicFacade.cjs +0 -24
- package/model-provider/anthropic/AnthropicFacade.d.ts +0 -18
- package/model-provider/anthropic/AnthropicFacade.js +0 -19
- package/model-provider/anthropic/AnthropicPromptTemplate.cjs +0 -82
- package/model-provider/anthropic/AnthropicPromptTemplate.d.ts +0 -17
- package/model-provider/anthropic/AnthropicPromptTemplate.js +0 -76
- package/model-provider/anthropic/AnthropicPromptTemplate.test.cjs +0 -49
- package/model-provider/anthropic/AnthropicPromptTemplate.test.d.ts +0 -1
- package/model-provider/anthropic/AnthropicPromptTemplate.test.js +0 -47
- package/model-provider/anthropic/AnthropicTextGenerationModel.cjs +0 -254
- package/model-provider/anthropic/AnthropicTextGenerationModel.d.ts +0 -153
- package/model-provider/anthropic/AnthropicTextGenerationModel.js +0 -250
- package/model-provider/anthropic/AnthropicTextGenerationModel.test.cjs +0 -44
- package/model-provider/anthropic/AnthropicTextGenerationModel.test.d.ts +0 -1
- package/model-provider/anthropic/AnthropicTextGenerationModel.test.js +0 -42
- package/model-provider/anthropic/index.cjs +0 -33
- package/model-provider/anthropic/index.d.ts +0 -5
- package/model-provider/anthropic/index.js +0 -4
@@ -1,254 +0,0 @@
|
|
1
|
-
"use strict";
|
2
|
-
Object.defineProperty(exports, "__esModule", { value: true });
|
3
|
-
exports.AnthropicTextGenerationResponseFormat = exports.AnthropicTextGenerationModel = exports.ANTHROPIC_TEXT_GENERATION_MODELS = void 0;
|
4
|
-
const zod_1 = require("zod");
|
5
|
-
const callWithRetryAndThrottle_js_1 = require("../../core/api/callWithRetryAndThrottle.cjs");
|
6
|
-
const postToApi_js_1 = require("../../core/api/postToApi.cjs");
|
7
|
-
const ZodSchema_js_1 = require("../../core/schema/ZodSchema.cjs");
|
8
|
-
const parseJSON_js_1 = require("../../core/schema/parseJSON.cjs");
|
9
|
-
const validateTypes_js_1 = require("../../core/schema/validateTypes.cjs");
|
10
|
-
const AbstractModel_js_1 = require("../../model-function/AbstractModel.cjs");
|
11
|
-
const PromptTemplateTextStreamingModel_js_1 = require("../../model-function/generate-text/PromptTemplateTextStreamingModel.cjs");
|
12
|
-
const TextGenerationModel_js_1 = require("../../model-function/generate-text/TextGenerationModel.cjs");
|
13
|
-
const AsyncQueue_js_1 = require("../../util/AsyncQueue.cjs");
|
14
|
-
const parseEventSourceStream_js_1 = require("../../util/streaming/parseEventSourceStream.cjs");
|
15
|
-
const AnthropicApiConfiguration_js_1 = require("./AnthropicApiConfiguration.cjs");
|
16
|
-
const AnthropicError_js_1 = require("./AnthropicError.cjs");
|
17
|
-
const AnthropicPromptTemplate_js_1 = require("./AnthropicPromptTemplate.cjs");
|
18
|
-
exports.ANTHROPIC_TEXT_GENERATION_MODELS = {
|
19
|
-
"claude-instant-1": {
|
20
|
-
contextWindowSize: 100000,
|
21
|
-
},
|
22
|
-
"claude-instant-1.2": {
|
23
|
-
contextWindowSize: 100000,
|
24
|
-
},
|
25
|
-
"claude-2": {
|
26
|
-
contextWindowSize: 200000,
|
27
|
-
},
|
28
|
-
"claude-2.0": {
|
29
|
-
contextWindowSize: 100000,
|
30
|
-
},
|
31
|
-
"claude-2.1": {
|
32
|
-
contextWindowSize: 200000,
|
33
|
-
},
|
34
|
-
};
|
35
|
-
/**
|
36
|
-
* Create a text generation model that calls the Anthropic API.
|
37
|
-
*
|
38
|
-
* @see https://docs.anthropic.com/claude/reference/complete_post
|
39
|
-
*/
|
40
|
-
class AnthropicTextGenerationModel extends AbstractModel_js_1.AbstractModel {
|
41
|
-
constructor(settings) {
|
42
|
-
super({ settings });
|
43
|
-
Object.defineProperty(this, "provider", {
|
44
|
-
enumerable: true,
|
45
|
-
configurable: true,
|
46
|
-
writable: true,
|
47
|
-
value: "anthropic"
|
48
|
-
});
|
49
|
-
Object.defineProperty(this, "contextWindowSize", {
|
50
|
-
enumerable: true,
|
51
|
-
configurable: true,
|
52
|
-
writable: true,
|
53
|
-
value: void 0
|
54
|
-
});
|
55
|
-
Object.defineProperty(this, "tokenizer", {
|
56
|
-
enumerable: true,
|
57
|
-
configurable: true,
|
58
|
-
writable: true,
|
59
|
-
value: undefined
|
60
|
-
});
|
61
|
-
Object.defineProperty(this, "countPromptTokens", {
|
62
|
-
enumerable: true,
|
63
|
-
configurable: true,
|
64
|
-
writable: true,
|
65
|
-
value: undefined
|
66
|
-
});
|
67
|
-
this.contextWindowSize =
|
68
|
-
exports.ANTHROPIC_TEXT_GENERATION_MODELS[this.settings.model].contextWindowSize;
|
69
|
-
}
|
70
|
-
get modelName() {
|
71
|
-
return this.settings.model;
|
72
|
-
}
|
73
|
-
async callAPI(prompt, callOptions, options) {
|
74
|
-
const api = this.settings.api ?? new AnthropicApiConfiguration_js_1.AnthropicApiConfiguration();
|
75
|
-
const responseFormat = options.responseFormat;
|
76
|
-
const abortSignal = callOptions.run?.abortSignal;
|
77
|
-
const userId = this.settings.userId;
|
78
|
-
return (0, callWithRetryAndThrottle_js_1.callWithRetryAndThrottle)({
|
79
|
-
retry: api.retry,
|
80
|
-
throttle: api.throttle,
|
81
|
-
call: async () => (0, postToApi_js_1.postJsonToApi)({
|
82
|
-
url: api.assembleUrl(`/complete`),
|
83
|
-
headers: api.headers({
|
84
|
-
functionType: callOptions.functionType,
|
85
|
-
functionId: callOptions.functionId,
|
86
|
-
run: callOptions.run,
|
87
|
-
callId: callOptions.callId,
|
88
|
-
}),
|
89
|
-
body: {
|
90
|
-
model: this.settings.model,
|
91
|
-
prompt,
|
92
|
-
stream: responseFormat.stream,
|
93
|
-
max_tokens_to_sample: this.settings.maxGenerationTokens ?? 100,
|
94
|
-
temperature: this.settings.temperature,
|
95
|
-
top_k: this.settings.topK,
|
96
|
-
top_p: this.settings.topP,
|
97
|
-
stop_sequences: this.settings.stopSequences,
|
98
|
-
metadata: userId != null ? { user_id: userId } : undefined,
|
99
|
-
},
|
100
|
-
failedResponseHandler: AnthropicError_js_1.failedAnthropicCallResponseHandler,
|
101
|
-
successfulResponseHandler: responseFormat.handler,
|
102
|
-
abortSignal,
|
103
|
-
}),
|
104
|
-
});
|
105
|
-
}
|
106
|
-
get settingsForEvent() {
|
107
|
-
const eventSettingProperties = [
|
108
|
-
...TextGenerationModel_js_1.textGenerationModelProperties,
|
109
|
-
"temperature",
|
110
|
-
"topK",
|
111
|
-
"topP",
|
112
|
-
"userId",
|
113
|
-
];
|
114
|
-
return Object.fromEntries(Object.entries(this.settings).filter(([key]) => eventSettingProperties.includes(key)));
|
115
|
-
}
|
116
|
-
async doGenerateTexts(prompt, options) {
|
117
|
-
return this.processTextGenerationResponse(await this.callAPI(prompt, options, {
|
118
|
-
responseFormat: exports.AnthropicTextGenerationResponseFormat.json,
|
119
|
-
}));
|
120
|
-
}
|
121
|
-
restoreGeneratedTexts(rawResponse) {
|
122
|
-
return this.processTextGenerationResponse((0, validateTypes_js_1.validateTypes)({
|
123
|
-
structure: rawResponse,
|
124
|
-
schema: (0, ZodSchema_js_1.zodSchema)(anthropicTextGenerationResponseSchema),
|
125
|
-
}));
|
126
|
-
}
|
127
|
-
processTextGenerationResponse(response) {
|
128
|
-
return {
|
129
|
-
response,
|
130
|
-
textGenerationResults: [
|
131
|
-
{
|
132
|
-
text: response.completion,
|
133
|
-
finishReason: this.translateFinishReason(response.stop_reason),
|
134
|
-
},
|
135
|
-
],
|
136
|
-
};
|
137
|
-
}
|
138
|
-
translateFinishReason(finishReason) {
|
139
|
-
switch (finishReason) {
|
140
|
-
case "stop_sequence":
|
141
|
-
return "stop";
|
142
|
-
case "max_tokens":
|
143
|
-
return "length";
|
144
|
-
default:
|
145
|
-
return "unknown";
|
146
|
-
}
|
147
|
-
}
|
148
|
-
doStreamText(prompt, options) {
|
149
|
-
return this.callAPI(prompt, options, {
|
150
|
-
responseFormat: exports.AnthropicTextGenerationResponseFormat.deltaIterable,
|
151
|
-
});
|
152
|
-
}
|
153
|
-
extractTextDelta(delta) {
|
154
|
-
const chunk = delta;
|
155
|
-
return chunk.completion;
|
156
|
-
}
|
157
|
-
/**
|
158
|
-
* Returns this model with a text prompt template.
|
159
|
-
*/
|
160
|
-
withTextPrompt() {
|
161
|
-
return this.withPromptTemplate((0, AnthropicPromptTemplate_js_1.text)());
|
162
|
-
}
|
163
|
-
/**
|
164
|
-
* Returns this model with an instruction prompt template.
|
165
|
-
*/
|
166
|
-
withInstructionPrompt() {
|
167
|
-
return this.withPromptTemplate((0, AnthropicPromptTemplate_js_1.instruction)());
|
168
|
-
}
|
169
|
-
/**
|
170
|
-
* Returns this model with a chat prompt template.
|
171
|
-
*/
|
172
|
-
withChatPrompt() {
|
173
|
-
return this.withPromptTemplate((0, AnthropicPromptTemplate_js_1.chat)());
|
174
|
-
}
|
175
|
-
withPromptTemplate(promptTemplate) {
|
176
|
-
return new PromptTemplateTextStreamingModel_js_1.PromptTemplateTextStreamingModel({
|
177
|
-
model: this.withSettings({
|
178
|
-
stopSequences: [
|
179
|
-
...(this.settings.stopSequences ?? []),
|
180
|
-
...promptTemplate.stopSequences,
|
181
|
-
],
|
182
|
-
}),
|
183
|
-
promptTemplate,
|
184
|
-
});
|
185
|
-
}
|
186
|
-
withSettings(additionalSettings) {
|
187
|
-
return new AnthropicTextGenerationModel(Object.assign({}, this.settings, additionalSettings));
|
188
|
-
}
|
189
|
-
}
|
190
|
-
exports.AnthropicTextGenerationModel = AnthropicTextGenerationModel;
|
191
|
-
const anthropicTextGenerationResponseSchema = zod_1.z.object({
|
192
|
-
completion: zod_1.z.string(),
|
193
|
-
stop_reason: zod_1.z.string(),
|
194
|
-
model: zod_1.z.string(),
|
195
|
-
});
|
196
|
-
const anthropicTextStreamChunkSchema = zod_1.z.object({
|
197
|
-
completion: zod_1.z.string(),
|
198
|
-
stop_reason: zod_1.z.string().nullable(),
|
199
|
-
model: zod_1.z.string(),
|
200
|
-
});
|
201
|
-
async function createAnthropicFullDeltaIterableQueue(stream) {
|
202
|
-
const queue = new AsyncQueue_js_1.AsyncQueue();
|
203
|
-
// process the stream asynchonously (no 'await' on purpose):
|
204
|
-
(0, parseEventSourceStream_js_1.parseEventSourceStream)({ stream })
|
205
|
-
.then(async (events) => {
|
206
|
-
try {
|
207
|
-
for await (const event of events) {
|
208
|
-
if (event.event === "error") {
|
209
|
-
queue.push({ type: "error", error: event.data });
|
210
|
-
queue.close();
|
211
|
-
return;
|
212
|
-
}
|
213
|
-
if (event.event !== "completion") {
|
214
|
-
continue;
|
215
|
-
}
|
216
|
-
const data = event.data;
|
217
|
-
const eventData = (0, parseJSON_js_1.parseJSON)({
|
218
|
-
text: data,
|
219
|
-
schema: (0, ZodSchema_js_1.zodSchema)(anthropicTextStreamChunkSchema),
|
220
|
-
});
|
221
|
-
queue.push({ type: "delta", deltaValue: eventData });
|
222
|
-
if (eventData.stop_reason != null) {
|
223
|
-
queue.close();
|
224
|
-
}
|
225
|
-
}
|
226
|
-
}
|
227
|
-
catch (error) {
|
228
|
-
queue.push({ type: "error", error });
|
229
|
-
queue.close();
|
230
|
-
}
|
231
|
-
})
|
232
|
-
.catch((error) => {
|
233
|
-
queue.push({ type: "error", error });
|
234
|
-
queue.close();
|
235
|
-
});
|
236
|
-
return queue;
|
237
|
-
}
|
238
|
-
exports.AnthropicTextGenerationResponseFormat = {
|
239
|
-
/**
|
240
|
-
* Returns the response as a JSON object.
|
241
|
-
*/
|
242
|
-
json: {
|
243
|
-
stream: false,
|
244
|
-
handler: (0, postToApi_js_1.createJsonResponseHandler)((0, ZodSchema_js_1.zodSchema)(anthropicTextGenerationResponseSchema)),
|
245
|
-
},
|
246
|
-
/**
|
247
|
-
* Returns an async iterable over the full deltas (all choices, including full current state at time of event)
|
248
|
-
* of the response stream.
|
249
|
-
*/
|
250
|
-
deltaIterable: {
|
251
|
-
stream: true,
|
252
|
-
handler: async ({ response }) => createAnthropicFullDeltaIterableQueue(response.body),
|
253
|
-
},
|
254
|
-
};
|
@@ -1,153 +0,0 @@
|
|
1
|
-
import { z } from "zod";
|
2
|
-
import { FunctionCallOptions } from "../../core/FunctionOptions.js";
|
3
|
-
import { ApiConfiguration } from "../../core/api/ApiConfiguration.js";
|
4
|
-
import { ResponseHandler } from "../../core/api/postToApi.js";
|
5
|
-
import { AbstractModel } from "../../model-function/AbstractModel.js";
|
6
|
-
import { Delta } from "../../model-function/Delta.js";
|
7
|
-
import { PromptTemplateTextStreamingModel } from "../../model-function/generate-text/PromptTemplateTextStreamingModel.js";
|
8
|
-
import { TextGenerationModelSettings, TextStreamingModel } from "../../model-function/generate-text/TextGenerationModel.js";
|
9
|
-
import { TextGenerationPromptTemplate } from "../../model-function/generate-text/TextGenerationPromptTemplate.js";
|
10
|
-
import { TextGenerationFinishReason } from "../../model-function/generate-text/TextGenerationResult.js";
|
11
|
-
export declare const ANTHROPIC_TEXT_GENERATION_MODELS: {
|
12
|
-
"claude-instant-1": {
|
13
|
-
contextWindowSize: number;
|
14
|
-
};
|
15
|
-
"claude-instant-1.2": {
|
16
|
-
contextWindowSize: number;
|
17
|
-
};
|
18
|
-
"claude-2": {
|
19
|
-
contextWindowSize: number;
|
20
|
-
};
|
21
|
-
"claude-2.0": {
|
22
|
-
contextWindowSize: number;
|
23
|
-
};
|
24
|
-
"claude-2.1": {
|
25
|
-
contextWindowSize: number;
|
26
|
-
};
|
27
|
-
};
|
28
|
-
export type AnthropicTextGenerationModelType = keyof typeof ANTHROPIC_TEXT_GENERATION_MODELS;
|
29
|
-
export interface AnthropicTextGenerationModelSettings extends TextGenerationModelSettings {
|
30
|
-
api?: ApiConfiguration;
|
31
|
-
model: AnthropicTextGenerationModelType;
|
32
|
-
temperature?: number;
|
33
|
-
topP?: number;
|
34
|
-
topK?: number;
|
35
|
-
userId?: number;
|
36
|
-
}
|
37
|
-
/**
|
38
|
-
* Create a text generation model that calls the Anthropic API.
|
39
|
-
*
|
40
|
-
* @see https://docs.anthropic.com/claude/reference/complete_post
|
41
|
-
*/
|
42
|
-
export declare class AnthropicTextGenerationModel extends AbstractModel<AnthropicTextGenerationModelSettings> implements TextStreamingModel<string, AnthropicTextGenerationModelSettings> {
|
43
|
-
constructor(settings: AnthropicTextGenerationModelSettings);
|
44
|
-
readonly provider: "anthropic";
|
45
|
-
get modelName(): "claude-instant-1" | "claude-instant-1.2" | "claude-2" | "claude-2.0" | "claude-2.1";
|
46
|
-
readonly contextWindowSize: number;
|
47
|
-
readonly tokenizer: undefined;
|
48
|
-
readonly countPromptTokens: undefined;
|
49
|
-
callAPI<RESPONSE>(prompt: string, callOptions: FunctionCallOptions, options: {
|
50
|
-
responseFormat: AnthropicTextGenerationResponseFormatType<RESPONSE>;
|
51
|
-
}): Promise<RESPONSE>;
|
52
|
-
get settingsForEvent(): Partial<AnthropicTextGenerationModelSettings>;
|
53
|
-
doGenerateTexts(prompt: string, options: FunctionCallOptions): Promise<{
|
54
|
-
response: {
|
55
|
-
model: string;
|
56
|
-
completion: string;
|
57
|
-
stop_reason: string;
|
58
|
-
};
|
59
|
-
textGenerationResults: {
|
60
|
-
text: string;
|
61
|
-
finishReason: TextGenerationFinishReason;
|
62
|
-
}[];
|
63
|
-
}>;
|
64
|
-
restoreGeneratedTexts(rawResponse: unknown): {
|
65
|
-
response: {
|
66
|
-
model: string;
|
67
|
-
completion: string;
|
68
|
-
stop_reason: string;
|
69
|
-
};
|
70
|
-
textGenerationResults: {
|
71
|
-
text: string;
|
72
|
-
finishReason: TextGenerationFinishReason;
|
73
|
-
}[];
|
74
|
-
};
|
75
|
-
processTextGenerationResponse(response: AnthropicTextGenerationResponse): {
|
76
|
-
response: {
|
77
|
-
model: string;
|
78
|
-
completion: string;
|
79
|
-
stop_reason: string;
|
80
|
-
};
|
81
|
-
textGenerationResults: {
|
82
|
-
text: string;
|
83
|
-
finishReason: TextGenerationFinishReason;
|
84
|
-
}[];
|
85
|
-
};
|
86
|
-
private translateFinishReason;
|
87
|
-
doStreamText(prompt: string, options: FunctionCallOptions): Promise<AsyncIterable<Delta<{
|
88
|
-
model: string;
|
89
|
-
completion: string;
|
90
|
-
stop_reason: string | null;
|
91
|
-
}>>>;
|
92
|
-
extractTextDelta(delta: unknown): string;
|
93
|
-
/**
|
94
|
-
* Returns this model with a text prompt template.
|
95
|
-
*/
|
96
|
-
withTextPrompt(): PromptTemplateTextStreamingModel<string, string, AnthropicTextGenerationModelSettings, this>;
|
97
|
-
/**
|
98
|
-
* Returns this model with an instruction prompt template.
|
99
|
-
*/
|
100
|
-
withInstructionPrompt(): PromptTemplateTextStreamingModel<import("../../index.js").InstructionPrompt, string, AnthropicTextGenerationModelSettings, this>;
|
101
|
-
/**
|
102
|
-
* Returns this model with a chat prompt template.
|
103
|
-
*/
|
104
|
-
withChatPrompt(): PromptTemplateTextStreamingModel<import("../../index.js").ChatPrompt, string, AnthropicTextGenerationModelSettings, this>;
|
105
|
-
withPromptTemplate<INPUT_PROMPT>(promptTemplate: TextGenerationPromptTemplate<INPUT_PROMPT, string>): PromptTemplateTextStreamingModel<INPUT_PROMPT, string, AnthropicTextGenerationModelSettings, this>;
|
106
|
-
withSettings(additionalSettings: Partial<AnthropicTextGenerationModelSettings>): this;
|
107
|
-
}
|
108
|
-
declare const anthropicTextGenerationResponseSchema: z.ZodObject<{
|
109
|
-
completion: z.ZodString;
|
110
|
-
stop_reason: z.ZodString;
|
111
|
-
model: z.ZodString;
|
112
|
-
}, "strip", z.ZodTypeAny, {
|
113
|
-
model: string;
|
114
|
-
completion: string;
|
115
|
-
stop_reason: string;
|
116
|
-
}, {
|
117
|
-
model: string;
|
118
|
-
completion: string;
|
119
|
-
stop_reason: string;
|
120
|
-
}>;
|
121
|
-
export type AnthropicTextGenerationResponse = z.infer<typeof anthropicTextGenerationResponseSchema>;
|
122
|
-
export type AnthropicTextGenerationResponseFormatType<T> = {
|
123
|
-
stream: boolean;
|
124
|
-
handler: ResponseHandler<T>;
|
125
|
-
};
|
126
|
-
export declare const AnthropicTextGenerationResponseFormat: {
|
127
|
-
/**
|
128
|
-
* Returns the response as a JSON object.
|
129
|
-
*/
|
130
|
-
json: {
|
131
|
-
stream: false;
|
132
|
-
handler: ResponseHandler<{
|
133
|
-
model: string;
|
134
|
-
completion: string;
|
135
|
-
stop_reason: string;
|
136
|
-
}>;
|
137
|
-
};
|
138
|
-
/**
|
139
|
-
* Returns an async iterable over the full deltas (all choices, including full current state at time of event)
|
140
|
-
* of the response stream.
|
141
|
-
*/
|
142
|
-
deltaIterable: {
|
143
|
-
stream: true;
|
144
|
-
handler: ({ response }: {
|
145
|
-
response: Response;
|
146
|
-
}) => Promise<AsyncIterable<Delta<{
|
147
|
-
model: string;
|
148
|
-
completion: string;
|
149
|
-
stop_reason: string | null;
|
150
|
-
}>>>;
|
151
|
-
};
|
152
|
-
};
|
153
|
-
export {};
|
@@ -1,250 +0,0 @@
|
|
1
|
-
import { z } from "zod";
|
2
|
-
import { callWithRetryAndThrottle } from "../../core/api/callWithRetryAndThrottle.js";
|
3
|
-
import { createJsonResponseHandler, postJsonToApi, } from "../../core/api/postToApi.js";
|
4
|
-
import { zodSchema } from "../../core/schema/ZodSchema.js";
|
5
|
-
import { parseJSON } from "../../core/schema/parseJSON.js";
|
6
|
-
import { validateTypes } from "../../core/schema/validateTypes.js";
|
7
|
-
import { AbstractModel } from "../../model-function/AbstractModel.js";
|
8
|
-
import { PromptTemplateTextStreamingModel } from "../../model-function/generate-text/PromptTemplateTextStreamingModel.js";
|
9
|
-
import { textGenerationModelProperties, } from "../../model-function/generate-text/TextGenerationModel.js";
|
10
|
-
import { AsyncQueue } from "../../util/AsyncQueue.js";
|
11
|
-
import { parseEventSourceStream } from "../../util/streaming/parseEventSourceStream.js";
|
12
|
-
import { AnthropicApiConfiguration } from "./AnthropicApiConfiguration.js";
|
13
|
-
import { failedAnthropicCallResponseHandler } from "./AnthropicError.js";
|
14
|
-
import { chat, instruction, text } from "./AnthropicPromptTemplate.js";
|
15
|
-
export const ANTHROPIC_TEXT_GENERATION_MODELS = {
|
16
|
-
"claude-instant-1": {
|
17
|
-
contextWindowSize: 100000,
|
18
|
-
},
|
19
|
-
"claude-instant-1.2": {
|
20
|
-
contextWindowSize: 100000,
|
21
|
-
},
|
22
|
-
"claude-2": {
|
23
|
-
contextWindowSize: 200000,
|
24
|
-
},
|
25
|
-
"claude-2.0": {
|
26
|
-
contextWindowSize: 100000,
|
27
|
-
},
|
28
|
-
"claude-2.1": {
|
29
|
-
contextWindowSize: 200000,
|
30
|
-
},
|
31
|
-
};
|
32
|
-
/**
|
33
|
-
* Create a text generation model that calls the Anthropic API.
|
34
|
-
*
|
35
|
-
* @see https://docs.anthropic.com/claude/reference/complete_post
|
36
|
-
*/
|
37
|
-
export class AnthropicTextGenerationModel extends AbstractModel {
|
38
|
-
constructor(settings) {
|
39
|
-
super({ settings });
|
40
|
-
Object.defineProperty(this, "provider", {
|
41
|
-
enumerable: true,
|
42
|
-
configurable: true,
|
43
|
-
writable: true,
|
44
|
-
value: "anthropic"
|
45
|
-
});
|
46
|
-
Object.defineProperty(this, "contextWindowSize", {
|
47
|
-
enumerable: true,
|
48
|
-
configurable: true,
|
49
|
-
writable: true,
|
50
|
-
value: void 0
|
51
|
-
});
|
52
|
-
Object.defineProperty(this, "tokenizer", {
|
53
|
-
enumerable: true,
|
54
|
-
configurable: true,
|
55
|
-
writable: true,
|
56
|
-
value: undefined
|
57
|
-
});
|
58
|
-
Object.defineProperty(this, "countPromptTokens", {
|
59
|
-
enumerable: true,
|
60
|
-
configurable: true,
|
61
|
-
writable: true,
|
62
|
-
value: undefined
|
63
|
-
});
|
64
|
-
this.contextWindowSize =
|
65
|
-
ANTHROPIC_TEXT_GENERATION_MODELS[this.settings.model].contextWindowSize;
|
66
|
-
}
|
67
|
-
get modelName() {
|
68
|
-
return this.settings.model;
|
69
|
-
}
|
70
|
-
async callAPI(prompt, callOptions, options) {
|
71
|
-
const api = this.settings.api ?? new AnthropicApiConfiguration();
|
72
|
-
const responseFormat = options.responseFormat;
|
73
|
-
const abortSignal = callOptions.run?.abortSignal;
|
74
|
-
const userId = this.settings.userId;
|
75
|
-
return callWithRetryAndThrottle({
|
76
|
-
retry: api.retry,
|
77
|
-
throttle: api.throttle,
|
78
|
-
call: async () => postJsonToApi({
|
79
|
-
url: api.assembleUrl(`/complete`),
|
80
|
-
headers: api.headers({
|
81
|
-
functionType: callOptions.functionType,
|
82
|
-
functionId: callOptions.functionId,
|
83
|
-
run: callOptions.run,
|
84
|
-
callId: callOptions.callId,
|
85
|
-
}),
|
86
|
-
body: {
|
87
|
-
model: this.settings.model,
|
88
|
-
prompt,
|
89
|
-
stream: responseFormat.stream,
|
90
|
-
max_tokens_to_sample: this.settings.maxGenerationTokens ?? 100,
|
91
|
-
temperature: this.settings.temperature,
|
92
|
-
top_k: this.settings.topK,
|
93
|
-
top_p: this.settings.topP,
|
94
|
-
stop_sequences: this.settings.stopSequences,
|
95
|
-
metadata: userId != null ? { user_id: userId } : undefined,
|
96
|
-
},
|
97
|
-
failedResponseHandler: failedAnthropicCallResponseHandler,
|
98
|
-
successfulResponseHandler: responseFormat.handler,
|
99
|
-
abortSignal,
|
100
|
-
}),
|
101
|
-
});
|
102
|
-
}
|
103
|
-
get settingsForEvent() {
|
104
|
-
const eventSettingProperties = [
|
105
|
-
...textGenerationModelProperties,
|
106
|
-
"temperature",
|
107
|
-
"topK",
|
108
|
-
"topP",
|
109
|
-
"userId",
|
110
|
-
];
|
111
|
-
return Object.fromEntries(Object.entries(this.settings).filter(([key]) => eventSettingProperties.includes(key)));
|
112
|
-
}
|
113
|
-
async doGenerateTexts(prompt, options) {
|
114
|
-
return this.processTextGenerationResponse(await this.callAPI(prompt, options, {
|
115
|
-
responseFormat: AnthropicTextGenerationResponseFormat.json,
|
116
|
-
}));
|
117
|
-
}
|
118
|
-
restoreGeneratedTexts(rawResponse) {
|
119
|
-
return this.processTextGenerationResponse(validateTypes({
|
120
|
-
structure: rawResponse,
|
121
|
-
schema: zodSchema(anthropicTextGenerationResponseSchema),
|
122
|
-
}));
|
123
|
-
}
|
124
|
-
processTextGenerationResponse(response) {
|
125
|
-
return {
|
126
|
-
response,
|
127
|
-
textGenerationResults: [
|
128
|
-
{
|
129
|
-
text: response.completion,
|
130
|
-
finishReason: this.translateFinishReason(response.stop_reason),
|
131
|
-
},
|
132
|
-
],
|
133
|
-
};
|
134
|
-
}
|
135
|
-
translateFinishReason(finishReason) {
|
136
|
-
switch (finishReason) {
|
137
|
-
case "stop_sequence":
|
138
|
-
return "stop";
|
139
|
-
case "max_tokens":
|
140
|
-
return "length";
|
141
|
-
default:
|
142
|
-
return "unknown";
|
143
|
-
}
|
144
|
-
}
|
145
|
-
doStreamText(prompt, options) {
|
146
|
-
return this.callAPI(prompt, options, {
|
147
|
-
responseFormat: AnthropicTextGenerationResponseFormat.deltaIterable,
|
148
|
-
});
|
149
|
-
}
|
150
|
-
extractTextDelta(delta) {
|
151
|
-
const chunk = delta;
|
152
|
-
return chunk.completion;
|
153
|
-
}
|
154
|
-
/**
|
155
|
-
* Returns this model with a text prompt template.
|
156
|
-
*/
|
157
|
-
withTextPrompt() {
|
158
|
-
return this.withPromptTemplate(text());
|
159
|
-
}
|
160
|
-
/**
|
161
|
-
* Returns this model with an instruction prompt template.
|
162
|
-
*/
|
163
|
-
withInstructionPrompt() {
|
164
|
-
return this.withPromptTemplate(instruction());
|
165
|
-
}
|
166
|
-
/**
|
167
|
-
* Returns this model with a chat prompt template.
|
168
|
-
*/
|
169
|
-
withChatPrompt() {
|
170
|
-
return this.withPromptTemplate(chat());
|
171
|
-
}
|
172
|
-
withPromptTemplate(promptTemplate) {
|
173
|
-
return new PromptTemplateTextStreamingModel({
|
174
|
-
model: this.withSettings({
|
175
|
-
stopSequences: [
|
176
|
-
...(this.settings.stopSequences ?? []),
|
177
|
-
...promptTemplate.stopSequences,
|
178
|
-
],
|
179
|
-
}),
|
180
|
-
promptTemplate,
|
181
|
-
});
|
182
|
-
}
|
183
|
-
withSettings(additionalSettings) {
|
184
|
-
return new AnthropicTextGenerationModel(Object.assign({}, this.settings, additionalSettings));
|
185
|
-
}
|
186
|
-
}
|
187
|
-
const anthropicTextGenerationResponseSchema = z.object({
|
188
|
-
completion: z.string(),
|
189
|
-
stop_reason: z.string(),
|
190
|
-
model: z.string(),
|
191
|
-
});
|
192
|
-
const anthropicTextStreamChunkSchema = z.object({
|
193
|
-
completion: z.string(),
|
194
|
-
stop_reason: z.string().nullable(),
|
195
|
-
model: z.string(),
|
196
|
-
});
|
197
|
-
async function createAnthropicFullDeltaIterableQueue(stream) {
|
198
|
-
const queue = new AsyncQueue();
|
199
|
-
// process the stream asynchonously (no 'await' on purpose):
|
200
|
-
parseEventSourceStream({ stream })
|
201
|
-
.then(async (events) => {
|
202
|
-
try {
|
203
|
-
for await (const event of events) {
|
204
|
-
if (event.event === "error") {
|
205
|
-
queue.push({ type: "error", error: event.data });
|
206
|
-
queue.close();
|
207
|
-
return;
|
208
|
-
}
|
209
|
-
if (event.event !== "completion") {
|
210
|
-
continue;
|
211
|
-
}
|
212
|
-
const data = event.data;
|
213
|
-
const eventData = parseJSON({
|
214
|
-
text: data,
|
215
|
-
schema: zodSchema(anthropicTextStreamChunkSchema),
|
216
|
-
});
|
217
|
-
queue.push({ type: "delta", deltaValue: eventData });
|
218
|
-
if (eventData.stop_reason != null) {
|
219
|
-
queue.close();
|
220
|
-
}
|
221
|
-
}
|
222
|
-
}
|
223
|
-
catch (error) {
|
224
|
-
queue.push({ type: "error", error });
|
225
|
-
queue.close();
|
226
|
-
}
|
227
|
-
})
|
228
|
-
.catch((error) => {
|
229
|
-
queue.push({ type: "error", error });
|
230
|
-
queue.close();
|
231
|
-
});
|
232
|
-
return queue;
|
233
|
-
}
|
234
|
-
export const AnthropicTextGenerationResponseFormat = {
|
235
|
-
/**
|
236
|
-
* Returns the response as a JSON object.
|
237
|
-
*/
|
238
|
-
json: {
|
239
|
-
stream: false,
|
240
|
-
handler: createJsonResponseHandler(zodSchema(anthropicTextGenerationResponseSchema)),
|
241
|
-
},
|
242
|
-
/**
|
243
|
-
* Returns an async iterable over the full deltas (all choices, including full current state at time of event)
|
244
|
-
* of the response stream.
|
245
|
-
*/
|
246
|
-
deltaIterable: {
|
247
|
-
stream: true,
|
248
|
-
handler: async ({ response }) => createAnthropicFullDeltaIterableQueue(response.body),
|
249
|
-
},
|
250
|
-
};
|