modelfusion 0.41.3 → 0.43.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.
Files changed (61) hide show
  1. package/README.md +55 -4
  2. package/guard/fixStructure.cjs +21 -0
  3. package/guard/fixStructure.d.ts +10 -0
  4. package/guard/fixStructure.js +17 -0
  5. package/guard/guard.cjs +69 -0
  6. package/guard/guard.d.ts +28 -0
  7. package/guard/guard.js +65 -0
  8. package/guard/index.cjs +18 -0
  9. package/guard/index.d.ts +2 -0
  10. package/guard/index.js +2 -0
  11. package/index.cjs +1 -0
  12. package/index.d.ts +1 -0
  13. package/index.js +1 -0
  14. package/model-function/generate-structure/StructureFromTextGenerationModel.cjs +15 -4
  15. package/model-function/generate-structure/StructureFromTextGenerationModel.d.ts +2 -1
  16. package/model-function/generate-structure/StructureFromTextGenerationModel.js +15 -4
  17. package/model-function/generate-structure/StructureGenerationModel.d.ts +2 -1
  18. package/model-function/generate-structure/StructureOrTextGenerationModel.d.ts +2 -0
  19. package/model-function/generate-structure/StructureParseError.cjs +34 -0
  20. package/model-function/generate-structure/StructureParseError.d.ts +10 -0
  21. package/model-function/generate-structure/StructureParseError.js +30 -0
  22. package/model-function/generate-structure/StructureValidationError.cjs +10 -3
  23. package/model-function/generate-structure/StructureValidationError.d.ts +3 -1
  24. package/model-function/generate-structure/StructureValidationError.js +10 -3
  25. package/model-function/generate-structure/generateStructure.cjs +2 -1
  26. package/model-function/generate-structure/generateStructure.js +2 -1
  27. package/model-function/generate-structure/generateStructureOrText.cjs +1 -0
  28. package/model-function/generate-structure/generateStructureOrText.js +1 -0
  29. package/model-function/generate-text/TextGenerationModel.d.ts +2 -4
  30. package/model-function/index.cjs +1 -0
  31. package/model-function/index.d.ts +1 -0
  32. package/model-function/index.js +1 -0
  33. package/model-provider/anthropic/AnthropicApiConfiguration.cjs +23 -0
  34. package/model-provider/anthropic/AnthropicApiConfiguration.d.ts +11 -0
  35. package/model-provider/anthropic/AnthropicApiConfiguration.js +19 -0
  36. package/model-provider/anthropic/AnthropicError.cjs +39 -0
  37. package/model-provider/anthropic/AnthropicError.d.ts +37 -0
  38. package/model-provider/anthropic/AnthropicError.js +31 -0
  39. package/model-provider/anthropic/AnthropicPromptFormat.cjs +66 -0
  40. package/model-provider/anthropic/AnthropicPromptFormat.d.ts +11 -0
  41. package/model-provider/anthropic/AnthropicPromptFormat.js +61 -0
  42. package/model-provider/anthropic/AnthropicTextGenerationModel.cjs +226 -0
  43. package/model-provider/anthropic/AnthropicTextGenerationModel.d.ts +102 -0
  44. package/model-provider/anthropic/AnthropicTextGenerationModel.js +219 -0
  45. package/model-provider/anthropic/index.cjs +23 -0
  46. package/model-provider/anthropic/index.d.ts +4 -0
  47. package/model-provider/anthropic/index.js +4 -0
  48. package/model-provider/huggingface/HuggingFaceTextGenerationModel.d.ts +1 -1
  49. package/model-provider/index.cjs +1 -0
  50. package/model-provider/index.d.ts +1 -0
  51. package/model-provider/index.js +1 -0
  52. package/model-provider/llamacpp/LlamaCppTextGenerationModel.cjs +0 -3
  53. package/model-provider/llamacpp/LlamaCppTextGenerationModel.d.ts +0 -1
  54. package/model-provider/llamacpp/LlamaCppTextGenerationModel.js +0 -3
  55. package/model-provider/openai/chat/OpenAIChatModel.cjs +47 -20
  56. package/model-provider/openai/chat/OpenAIChatModel.d.ts +36 -2
  57. package/model-provider/openai/chat/OpenAIChatModel.js +47 -20
  58. package/package.json +1 -1
  59. package/prompt/PromptFormatTextGenerationModel.cjs +3 -3
  60. package/prompt/PromptFormatTextGenerationModel.d.ts +1 -1
  61. package/prompt/PromptFormatTextGenerationModel.js +3 -3
@@ -12,11 +12,12 @@ export function generateStructure(model, structureDefinition, prompt, options) {
12
12
  options,
13
13
  generateResponse: async (options) => {
14
14
  const result = await model.doGenerateStructure(structureDefinition, expandedPrompt, options);
15
- const structure = result.structure;
15
+ const structure = result.value;
16
16
  const parseResult = structureDefinition.schema.validate(structure);
17
17
  if (!parseResult.success) {
18
18
  throw new StructureValidationError({
19
19
  structureName: structureDefinition.name,
20
+ valueText: result.valueText,
20
21
  value: structure,
21
22
  cause: parseResult.error,
22
23
  });
@@ -34,6 +34,7 @@ function generateStructureOrText(model, structureDefinitions, prompt, options) {
34
34
  throw new StructureValidationError_js_1.StructureValidationError({
35
35
  structureName: structure,
36
36
  value,
37
+ valueText: result.structureAndText.valueText,
37
38
  cause: parseResult.error,
38
39
  });
39
40
  }
@@ -31,6 +31,7 @@ export function generateStructureOrText(model, structureDefinitions, prompt, opt
31
31
  throw new StructureValidationError({
32
32
  structureName: structure,
33
33
  value,
34
+ valueText: result.structureAndText.valueText,
34
35
  cause: parseResult.error,
35
36
  });
36
37
  }
@@ -1,7 +1,5 @@
1
1
  import { FunctionOptions } from "../../core/FunctionOptions.js";
2
2
  import { PromptFormat } from "../../prompt/PromptFormat.js";
3
- import { PromptFormatTextGenerationModel } from "../../prompt/PromptFormatTextGenerationModel.js";
4
- import { PromptFormatTextStreamingModel } from "../../prompt/PromptFormatTextStreamingModel.js";
5
3
  import { Delta } from "../Delta.js";
6
4
  import { Model, ModelSettings } from "../Model.js";
7
5
  import { BasicTokenizer, FullTokenizer } from "../tokenize-text/Tokenizer.js";
@@ -38,9 +36,9 @@ export interface TextGenerationModel<PROMPT, SETTINGS extends TextGenerationMode
38
36
  totalTokens: number;
39
37
  };
40
38
  }>;
41
- withPromptFormat<INPUT_PROMPT>(promptFormat: PromptFormat<INPUT_PROMPT, PROMPT>): PromptFormatTextGenerationModel<INPUT_PROMPT, PROMPT, SETTINGS, this>;
39
+ withPromptFormat<INPUT_PROMPT>(promptFormat: PromptFormat<INPUT_PROMPT, PROMPT>): TextGenerationModel<INPUT_PROMPT, SETTINGS>;
42
40
  }
43
41
  export interface TextStreamingModel<PROMPT, SETTINGS extends TextGenerationModelSettings = TextGenerationModelSettings> extends TextGenerationModel<PROMPT, SETTINGS> {
44
42
  doStreamText(prompt: PROMPT, options?: FunctionOptions): PromiseLike<AsyncIterable<Delta<string>>>;
45
- withPromptFormat<INPUT_PROMPT>(promptFormat: PromptFormat<INPUT_PROMPT, PROMPT>): PromptFormatTextStreamingModel<INPUT_PROMPT, PROMPT, SETTINGS, this>;
43
+ withPromptFormat<INPUT_PROMPT>(promptFormat: PromptFormat<INPUT_PROMPT, PROMPT>): TextStreamingModel<INPUT_PROMPT, SETTINGS>;
46
44
  }
@@ -34,6 +34,7 @@ __exportStar(require("./generate-structure/StructureFromTextGenerationModel.cjs"
34
34
  __exportStar(require("./generate-structure/StructureGenerationEvent.cjs"), exports);
35
35
  __exportStar(require("./generate-structure/StructureGenerationModel.cjs"), exports);
36
36
  __exportStar(require("./generate-structure/StructureOrTextGenerationModel.cjs"), exports);
37
+ __exportStar(require("./generate-structure/StructureParseError.cjs"), exports);
37
38
  __exportStar(require("./generate-structure/StructureStreamingEvent.cjs"), exports);
38
39
  __exportStar(require("./generate-structure/StructureValidationError.cjs"), exports);
39
40
  __exportStar(require("./generate-structure/generateStructure.cjs"), exports);
@@ -18,6 +18,7 @@ export * from "./generate-structure/StructureFromTextGenerationModel.js";
18
18
  export * from "./generate-structure/StructureGenerationEvent.js";
19
19
  export * from "./generate-structure/StructureGenerationModel.js";
20
20
  export * from "./generate-structure/StructureOrTextGenerationModel.js";
21
+ export * from "./generate-structure/StructureParseError.js";
21
22
  export * from "./generate-structure/StructureStreamingEvent.js";
22
23
  export * from "./generate-structure/StructureValidationError.js";
23
24
  export * from "./generate-structure/generateStructure.js";
@@ -18,6 +18,7 @@ export * from "./generate-structure/StructureFromTextGenerationModel.js";
18
18
  export * from "./generate-structure/StructureGenerationEvent.js";
19
19
  export * from "./generate-structure/StructureGenerationModel.js";
20
20
  export * from "./generate-structure/StructureOrTextGenerationModel.js";
21
+ export * from "./generate-structure/StructureParseError.js";
21
22
  export * from "./generate-structure/StructureStreamingEvent.js";
22
23
  export * from "./generate-structure/StructureValidationError.js";
23
24
  export * from "./generate-structure/generateStructure.js";
@@ -0,0 +1,23 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.AnthropicApiConfiguration = void 0;
4
+ const BaseUrlApiConfiguration_js_1 = require("../../core/api/BaseUrlApiConfiguration.cjs");
5
+ const loadApiKey_js_1 = require("../../core/api/loadApiKey.cjs");
6
+ class AnthropicApiConfiguration extends BaseUrlApiConfiguration_js_1.BaseUrlApiConfiguration {
7
+ constructor({ baseUrl = "https://api.anthropic.com/v1", apiKey, retry, throttle, } = {}) {
8
+ super({
9
+ baseUrl,
10
+ headers: {
11
+ "x-api-key": (0, loadApiKey_js_1.loadApiKey)({
12
+ apiKey,
13
+ environmentVariableName: "ANTHROPIC_API_KEY",
14
+ description: "Anthropic",
15
+ }),
16
+ "anthropic-version": "2023-06-01",
17
+ },
18
+ retry,
19
+ throttle,
20
+ });
21
+ }
22
+ }
23
+ exports.AnthropicApiConfiguration = AnthropicApiConfiguration;
@@ -0,0 +1,11 @@
1
+ import { BaseUrlApiConfiguration } from "../../core/api/BaseUrlApiConfiguration.js";
2
+ import { RetryFunction } from "../../core/api/RetryFunction.js";
3
+ import { ThrottleFunction } from "../../core/api/ThrottleFunction.js";
4
+ export declare class AnthropicApiConfiguration extends BaseUrlApiConfiguration {
5
+ constructor({ baseUrl, apiKey, retry, throttle, }?: {
6
+ baseUrl?: string;
7
+ apiKey?: string;
8
+ retry?: RetryFunction;
9
+ throttle?: ThrottleFunction;
10
+ });
11
+ }
@@ -0,0 +1,19 @@
1
+ import { BaseUrlApiConfiguration } from "../../core/api/BaseUrlApiConfiguration.js";
2
+ import { loadApiKey } from "../../core/api/loadApiKey.js";
3
+ export class AnthropicApiConfiguration extends BaseUrlApiConfiguration {
4
+ constructor({ baseUrl = "https://api.anthropic.com/v1", apiKey, retry, throttle, } = {}) {
5
+ super({
6
+ baseUrl,
7
+ headers: {
8
+ "x-api-key": loadApiKey({
9
+ apiKey,
10
+ environmentVariableName: "ANTHROPIC_API_KEY",
11
+ description: "Anthropic",
12
+ }),
13
+ "anthropic-version": "2023-06-01",
14
+ },
15
+ retry,
16
+ throttle,
17
+ });
18
+ }
19
+ }
@@ -0,0 +1,39 @@
1
+ "use strict";
2
+ var __importDefault = (this && this.__importDefault) || function (mod) {
3
+ return (mod && mod.__esModule) ? mod : { "default": mod };
4
+ };
5
+ Object.defineProperty(exports, "__esModule", { value: true });
6
+ exports.failedAnthropicCallResponseHandler = exports.AnthropicError = exports.anthropicErrorDataSchema = void 0;
7
+ const secure_json_parse_1 = __importDefault(require("secure-json-parse"));
8
+ const zod_1 = require("zod");
9
+ const ApiCallError_js_1 = require("../../core/api/ApiCallError.cjs");
10
+ exports.anthropicErrorDataSchema = zod_1.z.object({
11
+ error: zod_1.z.object({
12
+ type: zod_1.z.string(),
13
+ message: zod_1.z.string(),
14
+ }),
15
+ });
16
+ class AnthropicError extends ApiCallError_js_1.ApiCallError {
17
+ constructor({ data, statusCode, url, requestBodyValues, message = data.error.message, }) {
18
+ super({ message, statusCode, requestBodyValues, url });
19
+ Object.defineProperty(this, "data", {
20
+ enumerable: true,
21
+ configurable: true,
22
+ writable: true,
23
+ value: void 0
24
+ });
25
+ this.data = data;
26
+ }
27
+ }
28
+ exports.AnthropicError = AnthropicError;
29
+ const failedAnthropicCallResponseHandler = async ({ response, url, requestBodyValues }) => {
30
+ const responseBody = await response.text();
31
+ const parsedError = exports.anthropicErrorDataSchema.parse(secure_json_parse_1.default.parse(responseBody));
32
+ return new AnthropicError({
33
+ url,
34
+ requestBodyValues,
35
+ statusCode: response.status,
36
+ data: parsedError,
37
+ });
38
+ };
39
+ exports.failedAnthropicCallResponseHandler = failedAnthropicCallResponseHandler;
@@ -0,0 +1,37 @@
1
+ import { z } from "zod";
2
+ import { ApiCallError } from "../../core/api/ApiCallError.js";
3
+ import { ResponseHandler } from "../../core/api/postToApi.js";
4
+ export declare const anthropicErrorDataSchema: z.ZodObject<{
5
+ error: z.ZodObject<{
6
+ type: z.ZodString;
7
+ message: z.ZodString;
8
+ }, "strip", z.ZodTypeAny, {
9
+ message: string;
10
+ type: string;
11
+ }, {
12
+ message: string;
13
+ type: string;
14
+ }>;
15
+ }, "strip", z.ZodTypeAny, {
16
+ error: {
17
+ message: string;
18
+ type: string;
19
+ };
20
+ }, {
21
+ error: {
22
+ message: string;
23
+ type: string;
24
+ };
25
+ }>;
26
+ export type AnthropicErrorData = z.infer<typeof anthropicErrorDataSchema>;
27
+ export declare class AnthropicError extends ApiCallError {
28
+ readonly data: AnthropicErrorData;
29
+ constructor({ data, statusCode, url, requestBodyValues, message, }: {
30
+ message?: string;
31
+ statusCode: number;
32
+ url: string;
33
+ requestBodyValues: unknown;
34
+ data: AnthropicErrorData;
35
+ });
36
+ }
37
+ export declare const failedAnthropicCallResponseHandler: ResponseHandler<ApiCallError>;
@@ -0,0 +1,31 @@
1
+ import SecureJSON from "secure-json-parse";
2
+ import { z } from "zod";
3
+ import { ApiCallError } from "../../core/api/ApiCallError.js";
4
+ export const anthropicErrorDataSchema = z.object({
5
+ error: z.object({
6
+ type: z.string(),
7
+ message: z.string(),
8
+ }),
9
+ });
10
+ export class AnthropicError extends ApiCallError {
11
+ constructor({ data, statusCode, url, requestBodyValues, message = data.error.message, }) {
12
+ super({ message, statusCode, requestBodyValues, url });
13
+ Object.defineProperty(this, "data", {
14
+ enumerable: true,
15
+ configurable: true,
16
+ writable: true,
17
+ value: void 0
18
+ });
19
+ this.data = data;
20
+ }
21
+ }
22
+ export const failedAnthropicCallResponseHandler = async ({ response, url, requestBodyValues }) => {
23
+ const responseBody = await response.text();
24
+ const parsedError = anthropicErrorDataSchema.parse(SecureJSON.parse(responseBody));
25
+ return new AnthropicError({
26
+ url,
27
+ requestBodyValues,
28
+ statusCode: response.status,
29
+ data: parsedError,
30
+ });
31
+ };
@@ -0,0 +1,66 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.mapChatPromptToAnthropicFormat = exports.mapInstructionPromptToAnthropicFormat = void 0;
4
+ const validateChatPrompt_js_1 = require("../../prompt/chat/validateChatPrompt.cjs");
5
+ /**
6
+ * Formats an instruction prompt as an Anthropic prompt.
7
+ */
8
+ function mapInstructionPromptToAnthropicFormat() {
9
+ return {
10
+ format: (instruction) => {
11
+ let text = "";
12
+ if (instruction.system != null) {
13
+ text += `${instruction.system}`;
14
+ }
15
+ text += "\n\nHuman:";
16
+ text += instruction.instruction;
17
+ if (instruction.input != null) {
18
+ // use tags per Anthropic instruction:
19
+ // https://docs.anthropic.com/claude/docs/constructing-a-prompt
20
+ text += `\n\n<data>${instruction.input}</data>`;
21
+ }
22
+ text += "\n\nAssistant:";
23
+ return text;
24
+ },
25
+ stopSequences: [],
26
+ };
27
+ }
28
+ exports.mapInstructionPromptToAnthropicFormat = mapInstructionPromptToAnthropicFormat;
29
+ /**
30
+ * Formats a chat prompt as an Anthropic prompt.
31
+ */
32
+ function mapChatPromptToAnthropicFormat() {
33
+ return {
34
+ format: (chatPrompt) => {
35
+ (0, validateChatPrompt_js_1.validateChatPrompt)(chatPrompt);
36
+ let text = "";
37
+ for (let i = 0; i < chatPrompt.length; i++) {
38
+ const message = chatPrompt[i];
39
+ // system message:
40
+ if (i === 0 &&
41
+ "system" in message &&
42
+ typeof message.system === "string") {
43
+ text += `${message.system}\n\n`;
44
+ continue;
45
+ }
46
+ // user message
47
+ if ("user" in message) {
48
+ text += `\n\nHuman:${message.user}`;
49
+ continue;
50
+ }
51
+ // ai message:
52
+ if ("ai" in message) {
53
+ text += `\n\nAssistant:${message.ai}`;
54
+ continue;
55
+ }
56
+ // unsupported message:
57
+ throw new Error(`Unsupported message: ${JSON.stringify(message)}`);
58
+ }
59
+ // AI message prefix:
60
+ text += `\n\nAssistant:`;
61
+ return text;
62
+ },
63
+ stopSequences: [],
64
+ };
65
+ }
66
+ exports.mapChatPromptToAnthropicFormat = mapChatPromptToAnthropicFormat;
@@ -0,0 +1,11 @@
1
+ import { ChatPrompt } from "../../prompt/chat/ChatPrompt.js";
2
+ import { InstructionPrompt } from "../../prompt/InstructionPrompt.js";
3
+ import { PromptFormat } from "../../prompt/PromptFormat.js";
4
+ /**
5
+ * Formats an instruction prompt as an Anthropic prompt.
6
+ */
7
+ export declare function mapInstructionPromptToAnthropicFormat(): PromptFormat<InstructionPrompt, string>;
8
+ /**
9
+ * Formats a chat prompt as an Anthropic prompt.
10
+ */
11
+ export declare function mapChatPromptToAnthropicFormat(): PromptFormat<ChatPrompt, string>;
@@ -0,0 +1,61 @@
1
+ import { validateChatPrompt } from "../../prompt/chat/validateChatPrompt.js";
2
+ /**
3
+ * Formats an instruction prompt as an Anthropic prompt.
4
+ */
5
+ export function mapInstructionPromptToAnthropicFormat() {
6
+ return {
7
+ format: (instruction) => {
8
+ let text = "";
9
+ if (instruction.system != null) {
10
+ text += `${instruction.system}`;
11
+ }
12
+ text += "\n\nHuman:";
13
+ text += instruction.instruction;
14
+ if (instruction.input != null) {
15
+ // use tags per Anthropic instruction:
16
+ // https://docs.anthropic.com/claude/docs/constructing-a-prompt
17
+ text += `\n\n<data>${instruction.input}</data>`;
18
+ }
19
+ text += "\n\nAssistant:";
20
+ return text;
21
+ },
22
+ stopSequences: [],
23
+ };
24
+ }
25
+ /**
26
+ * Formats a chat prompt as an Anthropic prompt.
27
+ */
28
+ export function mapChatPromptToAnthropicFormat() {
29
+ return {
30
+ format: (chatPrompt) => {
31
+ validateChatPrompt(chatPrompt);
32
+ let text = "";
33
+ for (let i = 0; i < chatPrompt.length; i++) {
34
+ const message = chatPrompt[i];
35
+ // system message:
36
+ if (i === 0 &&
37
+ "system" in message &&
38
+ typeof message.system === "string") {
39
+ text += `${message.system}\n\n`;
40
+ continue;
41
+ }
42
+ // user message
43
+ if ("user" in message) {
44
+ text += `\n\nHuman:${message.user}`;
45
+ continue;
46
+ }
47
+ // ai message:
48
+ if ("ai" in message) {
49
+ text += `\n\nAssistant:${message.ai}`;
50
+ continue;
51
+ }
52
+ // unsupported message:
53
+ throw new Error(`Unsupported message: ${JSON.stringify(message)}`);
54
+ }
55
+ // AI message prefix:
56
+ text += `\n\nAssistant:`;
57
+ return text;
58
+ },
59
+ stopSequences: [],
60
+ };
61
+ }
@@ -0,0 +1,226 @@
1
+ "use strict";
2
+ var __importDefault = (this && this.__importDefault) || function (mod) {
3
+ return (mod && mod.__esModule) ? mod : { "default": mod };
4
+ };
5
+ Object.defineProperty(exports, "__esModule", { value: true });
6
+ exports.AnthropicTextGenerationResponseFormat = exports.AnthropicTextGenerationModel = exports.ANTHROPIC_TEXT_GENERATION_MODELS = void 0;
7
+ const secure_json_parse_1 = __importDefault(require("secure-json-parse"));
8
+ const zod_1 = require("zod");
9
+ const callWithRetryAndThrottle_js_1 = require("../../core/api/callWithRetryAndThrottle.cjs");
10
+ const postToApi_js_1 = require("../../core/api/postToApi.cjs");
11
+ const AsyncQueue_js_1 = require("../../event-source/AsyncQueue.cjs");
12
+ const parseEventSourceStream_js_1 = require("../../event-source/parseEventSourceStream.cjs");
13
+ const AbstractModel_js_1 = require("../../model-function/AbstractModel.cjs");
14
+ const PromptFormatTextStreamingModel_js_1 = require("../../prompt/PromptFormatTextStreamingModel.cjs");
15
+ const AnthropicApiConfiguration_js_1 = require("./AnthropicApiConfiguration.cjs");
16
+ const AnthropicError_js_1 = require("./AnthropicError.cjs");
17
+ exports.ANTHROPIC_TEXT_GENERATION_MODELS = {
18
+ "claude-instant-1": {
19
+ contextWindowSize: 100000,
20
+ },
21
+ "claude-instant-1.2": {
22
+ contextWindowSize: 100000,
23
+ },
24
+ "claude-2": {
25
+ contextWindowSize: 100000,
26
+ },
27
+ "claude-2.0": {
28
+ contextWindowSize: 100000,
29
+ },
30
+ };
31
+ /**
32
+ * Create a text generation model that calls the Anthropic API.
33
+ *
34
+ * @see https://docs.anthropic.com/claude/reference/complete_post
35
+ */
36
+ class AnthropicTextGenerationModel extends AbstractModel_js_1.AbstractModel {
37
+ constructor(settings) {
38
+ super({ settings });
39
+ Object.defineProperty(this, "provider", {
40
+ enumerable: true,
41
+ configurable: true,
42
+ writable: true,
43
+ value: "anthropic"
44
+ });
45
+ Object.defineProperty(this, "contextWindowSize", {
46
+ enumerable: true,
47
+ configurable: true,
48
+ writable: true,
49
+ value: void 0
50
+ });
51
+ Object.defineProperty(this, "tokenizer", {
52
+ enumerable: true,
53
+ configurable: true,
54
+ writable: true,
55
+ value: undefined
56
+ });
57
+ Object.defineProperty(this, "countPromptTokens", {
58
+ enumerable: true,
59
+ configurable: true,
60
+ writable: true,
61
+ value: undefined
62
+ });
63
+ this.contextWindowSize =
64
+ exports.ANTHROPIC_TEXT_GENERATION_MODELS[this.settings.model].contextWindowSize;
65
+ }
66
+ get modelName() {
67
+ return this.settings.model;
68
+ }
69
+ async callAPI(prompt, options) {
70
+ return (0, callWithRetryAndThrottle_js_1.callWithRetryAndThrottle)({
71
+ retry: this.settings.api?.retry,
72
+ throttle: this.settings.api?.throttle,
73
+ call: async () => callAnthropicTextGenerationAPI({
74
+ ...this.settings,
75
+ stopSequences: this.settings.stopSequences,
76
+ maxTokens: this.settings.maxCompletionTokens,
77
+ abortSignal: options.run?.abortSignal,
78
+ responseFormat: options.responseFormat,
79
+ prompt,
80
+ }),
81
+ });
82
+ }
83
+ get settingsForEvent() {
84
+ const eventSettingProperties = [
85
+ "maxCompletionTokens",
86
+ "stopSequences",
87
+ "temperature",
88
+ "topK",
89
+ "topP",
90
+ "userId",
91
+ ];
92
+ return Object.fromEntries(Object.entries(this.settings).filter(([key]) => eventSettingProperties.includes(key)));
93
+ }
94
+ async doGenerateText(prompt, options) {
95
+ const response = await this.callAPI(prompt, {
96
+ ...options,
97
+ responseFormat: exports.AnthropicTextGenerationResponseFormat.json,
98
+ });
99
+ return {
100
+ response,
101
+ text: response.completion,
102
+ };
103
+ }
104
+ doStreamText(prompt, options) {
105
+ return this.callAPI(prompt, {
106
+ ...options,
107
+ responseFormat: exports.AnthropicTextGenerationResponseFormat.deltaIterable,
108
+ });
109
+ }
110
+ withPromptFormat(promptFormat) {
111
+ return new PromptFormatTextStreamingModel_js_1.PromptFormatTextStreamingModel({
112
+ model: this.withSettings({
113
+ stopSequences: [
114
+ ...(this.settings.stopSequences ?? []),
115
+ ...promptFormat.stopSequences,
116
+ ],
117
+ }),
118
+ promptFormat,
119
+ });
120
+ }
121
+ withSettings(additionalSettings) {
122
+ return new AnthropicTextGenerationModel(Object.assign({}, this.settings, additionalSettings));
123
+ }
124
+ }
125
+ exports.AnthropicTextGenerationModel = AnthropicTextGenerationModel;
126
+ const anthropicTextGenerationResponseSchema = zod_1.z.object({
127
+ completion: zod_1.z.string(),
128
+ stop_reason: zod_1.z.string(),
129
+ model: zod_1.z.string(),
130
+ });
131
+ async function callAnthropicTextGenerationAPI({ api = new AnthropicApiConfiguration_js_1.AnthropicApiConfiguration(), abortSignal, responseFormat, model, prompt, maxTokens, stopSequences, temperature, topK, topP, userId, }) {
132
+ return (0, postToApi_js_1.postJsonToApi)({
133
+ url: api.assembleUrl(`/complete`),
134
+ headers: api.headers,
135
+ body: {
136
+ model,
137
+ prompt,
138
+ stream: responseFormat.stream,
139
+ max_tokens_to_sample: maxTokens,
140
+ temperature,
141
+ top_k: topK,
142
+ top_p: topP,
143
+ stop_sequences: stopSequences,
144
+ metadata: userId != null ? { user_id: userId } : undefined,
145
+ },
146
+ failedResponseHandler: AnthropicError_js_1.failedAnthropicCallResponseHandler,
147
+ successfulResponseHandler: responseFormat.handler,
148
+ abortSignal,
149
+ });
150
+ }
151
+ const anthropicTextStreamingResponseSchema = zod_1.z.object({
152
+ completion: zod_1.z.string(),
153
+ stop_reason: zod_1.z.string().nullable(),
154
+ model: zod_1.z.string(),
155
+ });
156
+ async function createAnthropicFullDeltaIterableQueue(stream) {
157
+ const queue = new AsyncQueue_js_1.AsyncQueue();
158
+ let content = "";
159
+ // process the stream asynchonously (no 'await' on purpose):
160
+ (0, parseEventSourceStream_js_1.parseEventSourceStream)({ stream })
161
+ .then(async (events) => {
162
+ try {
163
+ for await (const event of events) {
164
+ if (event.event === "error") {
165
+ queue.push({ type: "error", error: event.data });
166
+ queue.close();
167
+ return;
168
+ }
169
+ if (event.event !== "completion") {
170
+ continue;
171
+ }
172
+ const data = event.data;
173
+ const json = secure_json_parse_1.default.parse(data);
174
+ const parseResult = anthropicTextStreamingResponseSchema.safeParse(json);
175
+ if (!parseResult.success) {
176
+ queue.push({
177
+ type: "error",
178
+ error: parseResult.error,
179
+ });
180
+ queue.close();
181
+ return;
182
+ }
183
+ const eventData = parseResult.data;
184
+ content += eventData.completion;
185
+ queue.push({
186
+ type: "delta",
187
+ fullDelta: {
188
+ content,
189
+ isComplete: eventData.stop_reason != null,
190
+ delta: eventData.completion,
191
+ },
192
+ valueDelta: eventData.completion,
193
+ });
194
+ if (eventData.stop_reason != null) {
195
+ queue.close();
196
+ }
197
+ }
198
+ }
199
+ catch (error) {
200
+ queue.push({ type: "error", error });
201
+ queue.close();
202
+ }
203
+ })
204
+ .catch((error) => {
205
+ queue.push({ type: "error", error });
206
+ queue.close();
207
+ });
208
+ return queue;
209
+ }
210
+ exports.AnthropicTextGenerationResponseFormat = {
211
+ /**
212
+ * Returns the response as a JSON object.
213
+ */
214
+ json: {
215
+ stream: false,
216
+ handler: (0, postToApi_js_1.createJsonResponseHandler)(anthropicTextGenerationResponseSchema),
217
+ },
218
+ /**
219
+ * Returns an async iterable over the full deltas (all choices, including full current state at time of event)
220
+ * of the response stream.
221
+ */
222
+ deltaIterable: {
223
+ stream: true,
224
+ handler: async ({ response }) => createAnthropicFullDeltaIterableQueue(response.body),
225
+ },
226
+ };