modelfusion 0.112.0 → 0.114.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 (159) hide show
  1. package/CHANGELOG.md +105 -0
  2. package/README.md +108 -212
  3. package/core/FunctionOptions.d.ts +14 -0
  4. package/core/api/AbstractApiConfiguration.cjs +16 -1
  5. package/core/api/AbstractApiConfiguration.d.ts +7 -3
  6. package/core/api/AbstractApiConfiguration.js +16 -1
  7. package/core/api/ApiConfiguration.d.ts +10 -1
  8. package/core/api/BaseUrlApiConfiguration.cjs +9 -5
  9. package/core/api/BaseUrlApiConfiguration.d.ts +7 -7
  10. package/core/api/BaseUrlApiConfiguration.js +9 -5
  11. package/core/api/CustomHeaderProvider.cjs +2 -0
  12. package/core/api/CustomHeaderProvider.d.ts +2 -0
  13. package/core/api/CustomHeaderProvider.js +1 -0
  14. package/core/api/index.cjs +1 -0
  15. package/core/api/index.d.ts +1 -0
  16. package/core/api/index.js +1 -0
  17. package/core/cache/Cache.cjs +2 -0
  18. package/core/cache/Cache.d.ts +12 -0
  19. package/core/cache/Cache.js +1 -0
  20. package/core/cache/MemoryCache.cjs +23 -0
  21. package/core/cache/MemoryCache.d.ts +15 -0
  22. package/core/cache/MemoryCache.js +19 -0
  23. package/core/cache/index.cjs +18 -0
  24. package/core/cache/index.d.ts +2 -0
  25. package/core/cache/index.js +2 -0
  26. package/core/index.cjs +1 -0
  27. package/core/index.d.ts +1 -0
  28. package/core/index.js +1 -0
  29. package/core/schema/TypeValidationError.cjs +36 -0
  30. package/core/schema/TypeValidationError.d.ts +15 -0
  31. package/core/schema/TypeValidationError.js +32 -0
  32. package/core/schema/index.cjs +2 -0
  33. package/core/schema/index.d.ts +2 -0
  34. package/core/schema/index.js +2 -0
  35. package/core/schema/parseJSON.cjs +6 -14
  36. package/core/schema/parseJSON.d.ts +3 -2
  37. package/core/schema/parseJSON.js +6 -14
  38. package/core/schema/validateTypes.cjs +65 -0
  39. package/core/schema/validateTypes.d.ts +34 -0
  40. package/core/schema/validateTypes.js +60 -0
  41. package/model-function/embed/EmbeddingModel.d.ts +2 -2
  42. package/model-function/executeStandardCall.cjs +3 -1
  43. package/model-function/executeStandardCall.d.ts +2 -2
  44. package/model-function/executeStandardCall.js +3 -1
  45. package/model-function/executeStreamCall.cjs +2 -1
  46. package/model-function/executeStreamCall.d.ts +2 -2
  47. package/model-function/executeStreamCall.js +2 -1
  48. package/model-function/generate-image/ImageGenerationModel.d.ts +2 -2
  49. package/model-function/generate-image/PromptTemplateImageGenerationModel.d.ts +2 -2
  50. package/model-function/generate-speech/SpeechGenerationModel.d.ts +3 -3
  51. package/model-function/generate-structure/StructureFromTextPromptTemplate.d.ts +13 -0
  52. package/model-function/generate-structure/generateStructure.cjs +4 -1
  53. package/model-function/generate-structure/generateStructure.js +4 -1
  54. package/model-function/generate-structure/jsonStructurePrompt.cjs +12 -0
  55. package/model-function/generate-structure/jsonStructurePrompt.d.ts +3 -3
  56. package/model-function/generate-structure/jsonStructurePrompt.js +12 -0
  57. package/model-function/generate-structure/streamStructure.cjs +4 -1
  58. package/model-function/generate-structure/streamStructure.js +4 -1
  59. package/model-function/generate-text/PromptTemplateTextGenerationModel.cjs +3 -0
  60. package/model-function/generate-text/PromptTemplateTextGenerationModel.d.ts +11 -2
  61. package/model-function/generate-text/PromptTemplateTextGenerationModel.js +3 -0
  62. package/model-function/generate-text/PromptTemplateTextStreamingModel.d.ts +2 -2
  63. package/model-function/generate-text/TextGenerationModel.d.ts +16 -3
  64. package/model-function/generate-text/generateText.cjs +43 -1
  65. package/model-function/generate-text/generateText.js +43 -1
  66. package/model-function/generate-transcription/TranscriptionModel.d.ts +2 -2
  67. package/model-provider/anthropic/AnthropicTextGenerationModel.cjs +20 -8
  68. package/model-provider/anthropic/AnthropicTextGenerationModel.d.ts +27 -5
  69. package/model-provider/anthropic/AnthropicTextGenerationModel.js +20 -8
  70. package/model-provider/automatic1111/Automatic1111ImageGenerationModel.cjs +8 -3
  71. package/model-provider/automatic1111/Automatic1111ImageGenerationModel.d.ts +3 -3
  72. package/model-provider/automatic1111/Automatic1111ImageGenerationModel.js +8 -3
  73. package/model-provider/cohere/CohereTextEmbeddingModel.cjs +8 -3
  74. package/model-provider/cohere/CohereTextEmbeddingModel.d.ts +3 -3
  75. package/model-provider/cohere/CohereTextEmbeddingModel.js +8 -3
  76. package/model-provider/cohere/CohereTextGenerationModel.cjs +20 -8
  77. package/model-provider/cohere/CohereTextGenerationModel.d.ts +45 -5
  78. package/model-provider/cohere/CohereTextGenerationModel.js +20 -8
  79. package/model-provider/cohere/CohereTokenizer.cjs +16 -6
  80. package/model-provider/cohere/CohereTokenizer.d.ts +3 -3
  81. package/model-provider/cohere/CohereTokenizer.js +16 -6
  82. package/model-provider/elevenlabs/ElevenLabsApiConfiguration.cjs +1 -1
  83. package/model-provider/elevenlabs/ElevenLabsApiConfiguration.js +1 -1
  84. package/model-provider/elevenlabs/ElevenLabsSpeechModel.cjs +8 -3
  85. package/model-provider/elevenlabs/ElevenLabsSpeechModel.d.ts +2 -2
  86. package/model-provider/elevenlabs/ElevenLabsSpeechModel.js +8 -3
  87. package/model-provider/huggingface/HuggingFaceTextEmbeddingModel.cjs +8 -3
  88. package/model-provider/huggingface/HuggingFaceTextEmbeddingModel.d.ts +3 -3
  89. package/model-provider/huggingface/HuggingFaceTextEmbeddingModel.js +8 -3
  90. package/model-provider/huggingface/HuggingFaceTextGenerationModel.cjs +18 -4
  91. package/model-provider/huggingface/HuggingFaceTextGenerationModel.d.ts +21 -3
  92. package/model-provider/huggingface/HuggingFaceTextGenerationModel.js +18 -4
  93. package/model-provider/llamacpp/LlamaCppCompletionModel.cjs +20 -8
  94. package/model-provider/llamacpp/LlamaCppCompletionModel.d.ts +125 -5
  95. package/model-provider/llamacpp/LlamaCppCompletionModel.js +20 -8
  96. package/model-provider/llamacpp/LlamaCppTextEmbeddingModel.cjs +8 -3
  97. package/model-provider/llamacpp/LlamaCppTextEmbeddingModel.d.ts +3 -3
  98. package/model-provider/llamacpp/LlamaCppTextEmbeddingModel.js +8 -3
  99. package/model-provider/llamacpp/LlamaCppTokenizer.cjs +8 -3
  100. package/model-provider/llamacpp/LlamaCppTokenizer.d.ts +2 -2
  101. package/model-provider/llamacpp/LlamaCppTokenizer.js +8 -3
  102. package/model-provider/lmnt/LmntSpeechModel.cjs +8 -3
  103. package/model-provider/lmnt/LmntSpeechModel.d.ts +2 -2
  104. package/model-provider/lmnt/LmntSpeechModel.js +8 -3
  105. package/model-provider/mistral/MistralChatModel.cjs +20 -8
  106. package/model-provider/mistral/MistralChatModel.d.ts +55 -5
  107. package/model-provider/mistral/MistralChatModel.js +20 -8
  108. package/model-provider/mistral/MistralTextEmbeddingModel.cjs +8 -3
  109. package/model-provider/mistral/MistralTextEmbeddingModel.d.ts +3 -3
  110. package/model-provider/mistral/MistralTextEmbeddingModel.js +8 -3
  111. package/model-provider/ollama/OllamaChatModel.cjs +35 -8
  112. package/model-provider/ollama/OllamaChatModel.d.ts +31 -5
  113. package/model-provider/ollama/OllamaChatModel.js +35 -8
  114. package/model-provider/ollama/OllamaCompletionModel.cjs +20 -7
  115. package/model-provider/ollama/OllamaCompletionModel.d.ts +43 -5
  116. package/model-provider/ollama/OllamaCompletionModel.js +20 -7
  117. package/model-provider/ollama/OllamaTextEmbeddingModel.cjs +8 -3
  118. package/model-provider/ollama/OllamaTextEmbeddingModel.d.ts +3 -3
  119. package/model-provider/ollama/OllamaTextEmbeddingModel.js +8 -3
  120. package/model-provider/openai/AbstractOpenAIChatModel.cjs +23 -13
  121. package/model-provider/openai/AbstractOpenAIChatModel.d.ts +94 -7
  122. package/model-provider/openai/AbstractOpenAIChatModel.js +23 -13
  123. package/model-provider/openai/AbstractOpenAICompletionModel.cjs +21 -9
  124. package/model-provider/openai/AbstractOpenAICompletionModel.d.ts +35 -5
  125. package/model-provider/openai/AbstractOpenAICompletionModel.js +21 -9
  126. package/model-provider/openai/AzureOpenAIApiConfiguration.cjs +5 -2
  127. package/model-provider/openai/AzureOpenAIApiConfiguration.d.ts +2 -1
  128. package/model-provider/openai/AzureOpenAIApiConfiguration.js +5 -2
  129. package/model-provider/openai/OpenAIChatFunctionCallStructureGenerationModel.cjs +12 -6
  130. package/model-provider/openai/OpenAIChatFunctionCallStructureGenerationModel.d.ts +89 -5
  131. package/model-provider/openai/OpenAIChatFunctionCallStructureGenerationModel.js +12 -6
  132. package/model-provider/openai/OpenAIChatModel.cjs +12 -4
  133. package/model-provider/openai/OpenAIChatModel.d.ts +3 -2
  134. package/model-provider/openai/OpenAIChatModel.js +12 -4
  135. package/model-provider/openai/OpenAIImageGenerationModel.cjs +10 -6
  136. package/model-provider/openai/OpenAIImageGenerationModel.d.ts +4 -4
  137. package/model-provider/openai/OpenAIImageGenerationModel.js +10 -6
  138. package/model-provider/openai/OpenAISpeechModel.cjs +9 -4
  139. package/model-provider/openai/OpenAISpeechModel.d.ts +3 -3
  140. package/model-provider/openai/OpenAISpeechModel.js +9 -4
  141. package/model-provider/openai/OpenAITextEmbeddingModel.cjs +11 -6
  142. package/model-provider/openai/OpenAITextEmbeddingModel.d.ts +3 -3
  143. package/model-provider/openai/OpenAITextEmbeddingModel.js +11 -6
  144. package/model-provider/openai/OpenAITranscriptionModel.cjs +9 -6
  145. package/model-provider/openai/OpenAITranscriptionModel.d.ts +4 -4
  146. package/model-provider/openai/OpenAITranscriptionModel.js +9 -6
  147. package/model-provider/openai-compatible/OpenAICompatibleChatModel.cjs +12 -4
  148. package/model-provider/openai-compatible/OpenAICompatibleChatModel.d.ts +3 -2
  149. package/model-provider/openai-compatible/OpenAICompatibleChatModel.js +12 -4
  150. package/model-provider/stability/StabilityImageGenerationModel.cjs +10 -5
  151. package/model-provider/stability/StabilityImageGenerationModel.d.ts +3 -3
  152. package/model-provider/stability/StabilityImageGenerationModel.js +10 -5
  153. package/model-provider/whispercpp/WhisperCppTranscriptionModel.cjs +9 -7
  154. package/model-provider/whispercpp/WhisperCppTranscriptionModel.d.ts +3 -3
  155. package/model-provider/whispercpp/WhisperCppTranscriptionModel.js +9 -7
  156. package/observability/helicone/HeliconeOpenAIApiConfiguration.cjs +2 -1
  157. package/observability/helicone/HeliconeOpenAIApiConfiguration.d.ts +3 -1
  158. package/observability/helicone/HeliconeOpenAIApiConfiguration.js +2 -1
  159. package/package.json +2 -2
@@ -35,9 +35,9 @@ export class ElevenLabsSpeechModel extends AbstractModel {
35
35
  get modelName() {
36
36
  return this.settings.voice;
37
37
  }
38
- async callAPI(text, options) {
38
+ async callAPI(text, callOptions) {
39
39
  const api = this.settings.api ?? new ElevenLabsApiConfiguration();
40
- const abortSignal = options?.run?.abortSignal;
40
+ const abortSignal = callOptions?.run?.abortSignal;
41
41
  return callWithRetryAndThrottle({
42
42
  retry: api.retry,
43
43
  throttle: api.throttle,
@@ -46,7 +46,12 @@ export class ElevenLabsSpeechModel extends AbstractModel {
46
46
  optimize_streaming_latency: this.settings.optimizeStreamingLatency,
47
47
  output_format: this.settings.outputFormat,
48
48
  })}`),
49
- headers: api.headers,
49
+ headers: api.headers({
50
+ functionType: callOptions.functionType,
51
+ functionId: callOptions.functionId,
52
+ run: callOptions.run,
53
+ callId: callOptions.callId,
54
+ }),
50
55
  body: {
51
56
  text,
52
57
  model_id: this.settings.model ?? defaultModel,
@@ -80,18 +80,23 @@ class HuggingFaceTextEmbeddingModel extends AbstractModel_js_1.AbstractModel {
80
80
  get modelName() {
81
81
  return this.settings.model;
82
82
  }
83
- async callAPI(texts, options) {
83
+ async callAPI(texts, callOptions) {
84
84
  if (texts.length > this.maxValuesPerCall) {
85
85
  throw new Error(`The HuggingFace feature extraction API is configured to only support ${this.maxValuesPerCall} texts per API call.`);
86
86
  }
87
87
  const api = this.settings.api ?? new HuggingFaceApiConfiguration_js_1.HuggingFaceApiConfiguration();
88
- const abortSignal = options?.run?.abortSignal;
88
+ const abortSignal = callOptions?.run?.abortSignal;
89
89
  return (0, callWithRetryAndThrottle_js_1.callWithRetryAndThrottle)({
90
90
  retry: api.retry,
91
91
  throttle: api.throttle,
92
92
  call: async () => (0, postToApi_js_1.postJsonToApi)({
93
93
  url: api.assembleUrl(`/${this.settings.model}`),
94
- headers: api.headers,
94
+ headers: api.headers({
95
+ functionType: callOptions.functionType,
96
+ functionId: callOptions.functionId,
97
+ run: callOptions.run,
98
+ callId: callOptions.callId,
99
+ }),
95
100
  body: {
96
101
  inputs: texts,
97
102
  options: {
@@ -1,5 +1,5 @@
1
1
  import { z } from "zod";
2
- import { FunctionOptions } from "../../core/FunctionOptions.js";
2
+ import { FunctionCallOptions } from "../../core/FunctionOptions.js";
3
3
  import { ApiConfiguration } from "../../core/api/ApiConfiguration.js";
4
4
  import { AbstractModel } from "../../model-function/AbstractModel.js";
5
5
  import { EmbeddingModel, EmbeddingModelSettings } from "../../model-function/embed/EmbeddingModel.js";
@@ -42,10 +42,10 @@ export declare class HuggingFaceTextEmbeddingModel extends AbstractModel<Hugging
42
42
  readonly contextWindowSize: undefined;
43
43
  readonly embeddingDimensions: number | undefined;
44
44
  readonly tokenizer: undefined;
45
- callAPI(texts: Array<string>, options?: FunctionOptions): Promise<HuggingFaceTextEmbeddingResponse>;
45
+ callAPI(texts: Array<string>, callOptions: FunctionCallOptions): Promise<HuggingFaceTextEmbeddingResponse>;
46
46
  get settingsForEvent(): Partial<HuggingFaceTextEmbeddingModelSettings>;
47
47
  readonly countPromptTokens: undefined;
48
- doEmbedValues(texts: string[], options?: FunctionOptions): Promise<{
48
+ doEmbedValues(texts: string[], options: FunctionCallOptions): Promise<{
49
49
  response: number[][];
50
50
  embeddings: number[][];
51
51
  }>;
@@ -77,18 +77,23 @@ export class HuggingFaceTextEmbeddingModel extends AbstractModel {
77
77
  get modelName() {
78
78
  return this.settings.model;
79
79
  }
80
- async callAPI(texts, options) {
80
+ async callAPI(texts, callOptions) {
81
81
  if (texts.length > this.maxValuesPerCall) {
82
82
  throw new Error(`The HuggingFace feature extraction API is configured to only support ${this.maxValuesPerCall} texts per API call.`);
83
83
  }
84
84
  const api = this.settings.api ?? new HuggingFaceApiConfiguration();
85
- const abortSignal = options?.run?.abortSignal;
85
+ const abortSignal = callOptions?.run?.abortSignal;
86
86
  return callWithRetryAndThrottle({
87
87
  retry: api.retry,
88
88
  throttle: api.throttle,
89
89
  call: async () => postJsonToApi({
90
90
  url: api.assembleUrl(`/${this.settings.model}`),
91
- headers: api.headers,
91
+ headers: api.headers({
92
+ functionType: callOptions.functionType,
93
+ functionId: callOptions.functionId,
94
+ run: callOptions.run,
95
+ callId: callOptions.callId,
96
+ }),
92
97
  body: {
93
98
  inputs: texts,
94
99
  options: {
@@ -5,6 +5,7 @@ const zod_1 = require("zod");
5
5
  const callWithRetryAndThrottle_js_1 = require("../../core/api/callWithRetryAndThrottle.cjs");
6
6
  const postToApi_js_1 = require("../../core/api/postToApi.cjs");
7
7
  const ZodSchema_js_1 = require("../../core/schema/ZodSchema.cjs");
8
+ const validateTypes_js_1 = require("../../core/schema/validateTypes.cjs");
8
9
  const AbstractModel_js_1 = require("../../model-function/AbstractModel.cjs");
9
10
  const PromptTemplateTextGenerationModel_js_1 = require("../../model-function/generate-text/PromptTemplateTextGenerationModel.cjs");
10
11
  const TextGenerationModel_js_1 = require("../../model-function/generate-text/TextGenerationModel.cjs");
@@ -59,15 +60,20 @@ class HuggingFaceTextGenerationModel extends AbstractModel_js_1.AbstractModel {
59
60
  get modelName() {
60
61
  return this.settings.model;
61
62
  }
62
- async callAPI(prompt, options) {
63
+ async callAPI(prompt, callOptions) {
63
64
  const api = this.settings.api ?? new HuggingFaceApiConfiguration_js_1.HuggingFaceApiConfiguration();
64
- const abortSignal = options?.run?.abortSignal;
65
+ const abortSignal = callOptions?.run?.abortSignal;
65
66
  return (0, callWithRetryAndThrottle_js_1.callWithRetryAndThrottle)({
66
67
  retry: api.retry,
67
68
  throttle: api.throttle,
68
69
  call: async () => (0, postToApi_js_1.postJsonToApi)({
69
70
  url: api.assembleUrl(`/${this.settings.model}`),
70
- headers: api.headers,
71
+ headers: api.headers({
72
+ functionType: callOptions.functionType,
73
+ functionId: callOptions.functionId,
74
+ run: callOptions.run,
75
+ callId: callOptions.callId,
76
+ }),
71
77
  body: {
72
78
  inputs: prompt,
73
79
  top_k: this.settings.topK,
@@ -102,7 +108,15 @@ class HuggingFaceTextGenerationModel extends AbstractModel_js_1.AbstractModel {
102
108
  return Object.fromEntries(Object.entries(this.settings).filter(([key]) => eventSettingProperties.includes(key)));
103
109
  }
104
110
  async doGenerateTexts(prompt, options) {
105
- const response = await this.callAPI(prompt, options);
111
+ return this.processTextGenerationResponse(await this.callAPI(prompt, options));
112
+ }
113
+ restoreGeneratedTexts(rawResponse) {
114
+ return this.processTextGenerationResponse((0, validateTypes_js_1.validateTypes)({
115
+ structure: rawResponse,
116
+ schema: (0, ZodSchema_js_1.zodSchema)(huggingFaceTextGenerationResponseSchema),
117
+ }));
118
+ }
119
+ processTextGenerationResponse(response) {
106
120
  return {
107
121
  response,
108
122
  textGenerationResults: response.map((response) => ({
@@ -1,5 +1,5 @@
1
1
  import { z } from "zod";
2
- import { FunctionOptions } from "../../core/FunctionOptions.js";
2
+ import { FunctionCallOptions } from "../../core/FunctionOptions.js";
3
3
  import { ApiConfiguration } from "../../core/api/ApiConfiguration.js";
4
4
  import { AbstractModel } from "../../model-function/AbstractModel.js";
5
5
  import { PromptTemplateTextGenerationModel } from "../../model-function/generate-text/PromptTemplateTextGenerationModel.js";
@@ -40,9 +40,9 @@ export declare class HuggingFaceTextGenerationModel extends AbstractModel<Huggin
40
40
  readonly contextWindowSize: undefined;
41
41
  readonly tokenizer: undefined;
42
42
  readonly countPromptTokens: undefined;
43
- callAPI(prompt: string, options?: FunctionOptions): Promise<HuggingFaceTextGenerationResponse>;
43
+ callAPI(prompt: string, callOptions: FunctionCallOptions): Promise<HuggingFaceTextGenerationResponse>;
44
44
  get settingsForEvent(): Partial<HuggingFaceTextGenerationModelSettings>;
45
- doGenerateTexts(prompt: string, options?: FunctionOptions): Promise<{
45
+ doGenerateTexts(prompt: string, options: FunctionCallOptions): Promise<{
46
46
  response: {
47
47
  generated_text: string;
48
48
  }[];
@@ -51,6 +51,24 @@ export declare class HuggingFaceTextGenerationModel extends AbstractModel<Huggin
51
51
  finishReason: "unknown";
52
52
  }[];
53
53
  }>;
54
+ restoreGeneratedTexts(rawResponse: unknown): {
55
+ response: {
56
+ generated_text: string;
57
+ }[];
58
+ textGenerationResults: {
59
+ text: string;
60
+ finishReason: "unknown";
61
+ }[];
62
+ };
63
+ processTextGenerationResponse(response: HuggingFaceTextGenerationResponse): {
64
+ response: {
65
+ generated_text: string;
66
+ }[];
67
+ textGenerationResults: {
68
+ text: string;
69
+ finishReason: "unknown";
70
+ }[];
71
+ };
54
72
  withPromptTemplate<INPUT_PROMPT>(promptTemplate: TextGenerationPromptTemplate<INPUT_PROMPT, string>): PromptTemplateTextGenerationModel<INPUT_PROMPT, string, HuggingFaceTextGenerationModelSettings, this>;
55
73
  withSettings(additionalSettings: Partial<HuggingFaceTextGenerationModelSettings>): this;
56
74
  }
@@ -2,6 +2,7 @@ import { z } from "zod";
2
2
  import { callWithRetryAndThrottle } from "../../core/api/callWithRetryAndThrottle.js";
3
3
  import { createJsonResponseHandler, postJsonToApi, } from "../../core/api/postToApi.js";
4
4
  import { zodSchema } from "../../core/schema/ZodSchema.js";
5
+ import { validateTypes } from "../../core/schema/validateTypes.js";
5
6
  import { AbstractModel } from "../../model-function/AbstractModel.js";
6
7
  import { PromptTemplateTextGenerationModel } from "../../model-function/generate-text/PromptTemplateTextGenerationModel.js";
7
8
  import { textGenerationModelProperties, } from "../../model-function/generate-text/TextGenerationModel.js";
@@ -56,15 +57,20 @@ export class HuggingFaceTextGenerationModel extends AbstractModel {
56
57
  get modelName() {
57
58
  return this.settings.model;
58
59
  }
59
- async callAPI(prompt, options) {
60
+ async callAPI(prompt, callOptions) {
60
61
  const api = this.settings.api ?? new HuggingFaceApiConfiguration();
61
- const abortSignal = options?.run?.abortSignal;
62
+ const abortSignal = callOptions?.run?.abortSignal;
62
63
  return callWithRetryAndThrottle({
63
64
  retry: api.retry,
64
65
  throttle: api.throttle,
65
66
  call: async () => postJsonToApi({
66
67
  url: api.assembleUrl(`/${this.settings.model}`),
67
- headers: api.headers,
68
+ headers: api.headers({
69
+ functionType: callOptions.functionType,
70
+ functionId: callOptions.functionId,
71
+ run: callOptions.run,
72
+ callId: callOptions.callId,
73
+ }),
68
74
  body: {
69
75
  inputs: prompt,
70
76
  top_k: this.settings.topK,
@@ -99,7 +105,15 @@ export class HuggingFaceTextGenerationModel extends AbstractModel {
99
105
  return Object.fromEntries(Object.entries(this.settings).filter(([key]) => eventSettingProperties.includes(key)));
100
106
  }
101
107
  async doGenerateTexts(prompt, options) {
102
- const response = await this.callAPI(prompt, options);
108
+ return this.processTextGenerationResponse(await this.callAPI(prompt, options));
109
+ }
110
+ restoreGeneratedTexts(rawResponse) {
111
+ return this.processTextGenerationResponse(validateTypes({
112
+ structure: rawResponse,
113
+ schema: zodSchema(huggingFaceTextGenerationResponseSchema),
114
+ }));
115
+ }
116
+ processTextGenerationResponse(response) {
103
117
  return {
104
118
  response,
105
119
  textGenerationResults: response.map((response) => ({
@@ -6,6 +6,7 @@ const callWithRetryAndThrottle_js_1 = require("../../core/api/callWithRetryAndTh
6
6
  const postToApi_js_1 = require("../../core/api/postToApi.cjs");
7
7
  const ZodSchema_js_1 = require("../../core/schema/ZodSchema.cjs");
8
8
  const parseJSON_js_1 = require("../../core/schema/parseJSON.cjs");
9
+ const validateTypes_js_1 = require("../../core/schema/validateTypes.cjs");
9
10
  const AbstractModel_js_1 = require("../../model-function/AbstractModel.cjs");
10
11
  const PromptTemplateTextStreamingModel_js_1 = require("../../model-function/generate-text/PromptTemplateTextStreamingModel.cjs");
11
12
  const TextGenerationModel_js_1 = require("../../model-function/generate-text/TextGenerationModel.cjs");
@@ -37,16 +38,21 @@ class LlamaCppCompletionModel extends AbstractModel_js_1.AbstractModel {
37
38
  get contextWindowSize() {
38
39
  return this.settings.contextWindowSize;
39
40
  }
40
- async callAPI(prompt, options) {
41
+ async callAPI(prompt, callOptions, options) {
41
42
  const api = this.settings.api ?? new LlamaCppApiConfiguration_js_1.LlamaCppApiConfiguration();
42
43
  const responseFormat = options.responseFormat;
43
- const abortSignal = options.run?.abortSignal;
44
+ const abortSignal = callOptions.run?.abortSignal;
44
45
  return (0, callWithRetryAndThrottle_js_1.callWithRetryAndThrottle)({
45
46
  retry: api.retry,
46
47
  throttle: api.throttle,
47
48
  call: async () => (0, postToApi_js_1.postJsonToApi)({
48
49
  url: api.assembleUrl(`/completion`),
49
- headers: api.headers,
50
+ headers: api.headers({
51
+ functionType: callOptions.functionType,
52
+ functionId: callOptions.functionId,
53
+ run: callOptions.run,
54
+ callId: callOptions.callId,
55
+ }),
50
56
  body: {
51
57
  stream: responseFormat.stream,
52
58
  prompt: prompt.text,
@@ -123,10 +129,17 @@ class LlamaCppCompletionModel extends AbstractModel_js_1.AbstractModel {
123
129
  return tokens.length;
124
130
  }
125
131
  async doGenerateTexts(prompt, options) {
126
- const response = await this.callAPI(prompt, {
127
- ...options,
132
+ return this.processTextGenerationResponse(await this.callAPI(prompt, options, {
128
133
  responseFormat: exports.LlamaCppCompletionResponseFormat.json,
129
- });
134
+ }));
135
+ }
136
+ restoreGeneratedTexts(rawResponse) {
137
+ return this.processTextGenerationResponse((0, validateTypes_js_1.validateTypes)({
138
+ structure: rawResponse,
139
+ schema: (0, ZodSchema_js_1.zodSchema)(llamaCppTextGenerationResponseSchema),
140
+ }));
141
+ }
142
+ processTextGenerationResponse(response) {
130
143
  return {
131
144
  response,
132
145
  textGenerationResults: [
@@ -147,8 +160,7 @@ class LlamaCppCompletionModel extends AbstractModel_js_1.AbstractModel {
147
160
  };
148
161
  }
149
162
  doStreamText(prompt, options) {
150
- return this.callAPI(prompt, {
151
- ...options,
163
+ return this.callAPI(prompt, options, {
152
164
  responseFormat: exports.LlamaCppCompletionResponseFormat.deltaIterable,
153
165
  });
154
166
  }
@@ -1,5 +1,5 @@
1
1
  import { z } from "zod";
2
- import { FunctionOptions } from "../../core/FunctionOptions.js";
2
+ import { FunctionCallOptions } from "../../core/FunctionOptions.js";
3
3
  import { ApiConfiguration } from "../../core/api/ApiConfiguration.js";
4
4
  import { ResponseHandler } from "../../core/api/postToApi.js";
5
5
  import { AbstractModel } from "../../model-function/AbstractModel.js";
@@ -138,12 +138,12 @@ export declare class LlamaCppCompletionModel<CONTEXT_WINDOW_SIZE extends number
138
138
  get modelName(): null;
139
139
  get contextWindowSize(): CONTEXT_WINDOW_SIZE;
140
140
  readonly tokenizer: LlamaCppTokenizer;
141
- callAPI<RESPONSE>(prompt: LlamaCppCompletionPrompt, options: {
141
+ callAPI<RESPONSE>(prompt: LlamaCppCompletionPrompt, callOptions: FunctionCallOptions, options: {
142
142
  responseFormat: LlamaCppCompletionResponseFormatType<RESPONSE>;
143
- } & FunctionOptions): Promise<RESPONSE>;
143
+ }): Promise<RESPONSE>;
144
144
  get settingsForEvent(): Partial<LlamaCppCompletionModelSettings<CONTEXT_WINDOW_SIZE>>;
145
145
  countPromptTokens(prompt: LlamaCppCompletionPrompt): Promise<number>;
146
- doGenerateTexts(prompt: LlamaCppCompletionPrompt, options?: FunctionOptions): Promise<{
146
+ doGenerateTexts(prompt: LlamaCppCompletionPrompt, options: FunctionCallOptions): Promise<{
147
147
  response: {
148
148
  model: string;
149
149
  stop: true;
@@ -203,7 +203,127 @@ export declare class LlamaCppCompletionModel<CONTEXT_WINDOW_SIZE extends number
203
203
  totalTokens: number;
204
204
  };
205
205
  }>;
206
- doStreamText(prompt: LlamaCppCompletionPrompt, options?: FunctionOptions): Promise<AsyncIterable<Delta<{
206
+ restoreGeneratedTexts(rawResponse: unknown): {
207
+ response: {
208
+ model: string;
209
+ stop: true;
210
+ content: string;
211
+ prompt: string;
212
+ generation_settings: {
213
+ model: string;
214
+ stream: boolean;
215
+ stop: string[];
216
+ seed: number;
217
+ mirostat: number;
218
+ frequency_penalty: number;
219
+ ignore_eos: boolean;
220
+ logit_bias: number[];
221
+ mirostat_eta: number;
222
+ mirostat_tau: number;
223
+ n_ctx: number;
224
+ n_keep: number;
225
+ n_predict: number;
226
+ n_probs: number;
227
+ penalize_nl: boolean;
228
+ presence_penalty: number;
229
+ repeat_last_n: number;
230
+ repeat_penalty: number;
231
+ tfs_z: number;
232
+ top_k: number;
233
+ top_p: number;
234
+ typical_p: number;
235
+ temperature?: number | undefined;
236
+ };
237
+ stopped_eos: boolean;
238
+ stopped_limit: boolean;
239
+ stopped_word: boolean;
240
+ stopping_word: string;
241
+ timings: {
242
+ predicted_ms: number;
243
+ predicted_n: number;
244
+ predicted_per_second: number | null;
245
+ predicted_per_token_ms: number | null;
246
+ prompt_n: number;
247
+ prompt_per_second: number | null;
248
+ prompt_per_token_ms: number | null;
249
+ prompt_ms?: number | null | undefined;
250
+ };
251
+ tokens_cached: number;
252
+ tokens_evaluated: number;
253
+ tokens_predicted: number;
254
+ truncated: boolean;
255
+ };
256
+ textGenerationResults: {
257
+ text: string;
258
+ finishReason: "length" | "stop" | "unknown";
259
+ }[];
260
+ usage: {
261
+ promptTokens: number;
262
+ completionTokens: number;
263
+ totalTokens: number;
264
+ };
265
+ };
266
+ processTextGenerationResponse(response: LlamaCppTextGenerationResponse): {
267
+ response: {
268
+ model: string;
269
+ stop: true;
270
+ content: string;
271
+ prompt: string;
272
+ generation_settings: {
273
+ model: string;
274
+ stream: boolean;
275
+ stop: string[];
276
+ seed: number;
277
+ mirostat: number;
278
+ frequency_penalty: number;
279
+ ignore_eos: boolean;
280
+ logit_bias: number[];
281
+ mirostat_eta: number;
282
+ mirostat_tau: number;
283
+ n_ctx: number;
284
+ n_keep: number;
285
+ n_predict: number;
286
+ n_probs: number;
287
+ penalize_nl: boolean;
288
+ presence_penalty: number;
289
+ repeat_last_n: number;
290
+ repeat_penalty: number;
291
+ tfs_z: number;
292
+ top_k: number;
293
+ top_p: number;
294
+ typical_p: number;
295
+ temperature?: number | undefined;
296
+ };
297
+ stopped_eos: boolean;
298
+ stopped_limit: boolean;
299
+ stopped_word: boolean;
300
+ stopping_word: string;
301
+ timings: {
302
+ predicted_ms: number;
303
+ predicted_n: number;
304
+ predicted_per_second: number | null;
305
+ predicted_per_token_ms: number | null;
306
+ prompt_n: number;
307
+ prompt_per_second: number | null;
308
+ prompt_per_token_ms: number | null;
309
+ prompt_ms?: number | null | undefined;
310
+ };
311
+ tokens_cached: number;
312
+ tokens_evaluated: number;
313
+ tokens_predicted: number;
314
+ truncated: boolean;
315
+ };
316
+ textGenerationResults: {
317
+ text: string;
318
+ finishReason: "length" | "stop" | "unknown";
319
+ }[];
320
+ usage: {
321
+ promptTokens: number;
322
+ completionTokens: number;
323
+ totalTokens: number;
324
+ };
325
+ };
326
+ doStreamText(prompt: LlamaCppCompletionPrompt, options: FunctionCallOptions): Promise<AsyncIterable<Delta<{
207
327
  model: string;
208
328
  stop: true;
209
329
  content: string;
@@ -3,6 +3,7 @@ import { callWithRetryAndThrottle } from "../../core/api/callWithRetryAndThrottl
3
3
  import { createJsonResponseHandler, postJsonToApi, } from "../../core/api/postToApi.js";
4
4
  import { zodSchema } from "../../core/schema/ZodSchema.js";
5
5
  import { parseJSON } from "../../core/schema/parseJSON.js";
6
+ import { validateTypes } from "../../core/schema/validateTypes.js";
6
7
  import { AbstractModel } from "../../model-function/AbstractModel.js";
7
8
  import { PromptTemplateTextStreamingModel } from "../../model-function/generate-text/PromptTemplateTextStreamingModel.js";
8
9
  import { textGenerationModelProperties, } from "../../model-function/generate-text/TextGenerationModel.js";
@@ -34,16 +35,21 @@ export class LlamaCppCompletionModel extends AbstractModel {
34
35
  get contextWindowSize() {
35
36
  return this.settings.contextWindowSize;
36
37
  }
37
- async callAPI(prompt, options) {
38
+ async callAPI(prompt, callOptions, options) {
38
39
  const api = this.settings.api ?? new LlamaCppApiConfiguration();
39
40
  const responseFormat = options.responseFormat;
40
- const abortSignal = options.run?.abortSignal;
41
+ const abortSignal = callOptions.run?.abortSignal;
41
42
  return callWithRetryAndThrottle({
42
43
  retry: api.retry,
43
44
  throttle: api.throttle,
44
45
  call: async () => postJsonToApi({
45
46
  url: api.assembleUrl(`/completion`),
46
- headers: api.headers,
47
+ headers: api.headers({
48
+ functionType: callOptions.functionType,
49
+ functionId: callOptions.functionId,
50
+ run: callOptions.run,
51
+ callId: callOptions.callId,
52
+ }),
47
53
  body: {
48
54
  stream: responseFormat.stream,
49
55
  prompt: prompt.text,
@@ -120,10 +126,17 @@ export class LlamaCppCompletionModel extends AbstractModel {
120
126
  return tokens.length;
121
127
  }
122
128
  async doGenerateTexts(prompt, options) {
123
- const response = await this.callAPI(prompt, {
124
- ...options,
129
+ return this.processTextGenerationResponse(await this.callAPI(prompt, options, {
125
130
  responseFormat: LlamaCppCompletionResponseFormat.json,
126
- });
131
+ }));
132
+ }
133
+ restoreGeneratedTexts(rawResponse) {
134
+ return this.processTextGenerationResponse(validateTypes({
135
+ structure: rawResponse,
136
+ schema: zodSchema(llamaCppTextGenerationResponseSchema),
137
+ }));
138
+ }
139
+ processTextGenerationResponse(response) {
127
140
  return {
128
141
  response,
129
142
  textGenerationResults: [
@@ -144,8 +157,7 @@ export class LlamaCppCompletionModel extends AbstractModel {
144
157
  };
145
158
  }
146
159
  doStreamText(prompt, options) {
147
- return this.callAPI(prompt, {
148
- ...options,
160
+ return this.callAPI(prompt, options, {
149
161
  responseFormat: LlamaCppCompletionResponseFormat.deltaIterable,
150
162
  });
151
163
  }
@@ -50,18 +50,23 @@ class LlamaCppTextEmbeddingModel extends AbstractModel_js_1.AbstractModel {
50
50
  async tokenize(text) {
51
51
  return this.tokenizer.tokenize(text);
52
52
  }
53
- async callAPI(texts, options) {
53
+ async callAPI(texts, callOptions) {
54
54
  if (texts.length > this.maxValuesPerCall) {
55
55
  throw new Error(`The Llama.cpp embedding API only supports ${this.maxValuesPerCall} texts per API call.`);
56
56
  }
57
57
  const api = this.settings.api ?? new LlamaCppApiConfiguration_js_1.LlamaCppApiConfiguration();
58
- const abortSignal = options?.run?.abortSignal;
58
+ const abortSignal = callOptions.run?.abortSignal;
59
59
  return (0, callWithRetryAndThrottle_js_1.callWithRetryAndThrottle)({
60
60
  retry: this.settings.api?.retry,
61
61
  throttle: this.settings.api?.throttle,
62
62
  call: async () => (0, postToApi_js_1.postJsonToApi)({
63
63
  url: api.assembleUrl(`/embedding`),
64
- headers: api.headers,
64
+ headers: api.headers({
65
+ functionType: callOptions.functionType,
66
+ functionId: callOptions.functionId,
67
+ run: callOptions.run,
68
+ callId: callOptions.callId,
69
+ }),
65
70
  body: { content: texts[0] },
66
71
  failedResponseHandler: LlamaCppError_js_1.failedLlamaCppCallResponseHandler,
67
72
  successfulResponseHandler: (0, postToApi_js_1.createJsonResponseHandler)((0, ZodSchema_js_1.zodSchema)(llamaCppTextEmbeddingResponseSchema)),
@@ -1,5 +1,5 @@
1
1
  import { z } from "zod";
2
- import { FunctionOptions } from "../../core/FunctionOptions.js";
2
+ import { FunctionCallOptions } from "../../core/FunctionOptions.js";
3
3
  import { ApiConfiguration } from "../../core/api/ApiConfiguration.js";
4
4
  import { AbstractModel } from "../../model-function/AbstractModel.js";
5
5
  import { EmbeddingModel, EmbeddingModelSettings } from "../../model-function/embed/EmbeddingModel.js";
@@ -18,9 +18,9 @@ export declare class LlamaCppTextEmbeddingModel extends AbstractModel<LlamaCppTe
18
18
  get embeddingDimensions(): number | undefined;
19
19
  private readonly tokenizer;
20
20
  tokenize(text: string): Promise<number[]>;
21
- callAPI(texts: Array<string>, options?: FunctionOptions): Promise<LlamaCppTextEmbeddingResponse>;
21
+ callAPI(texts: Array<string>, callOptions: FunctionCallOptions): Promise<LlamaCppTextEmbeddingResponse>;
22
22
  get settingsForEvent(): Partial<LlamaCppTextEmbeddingModelSettings>;
23
- doEmbedValues(texts: string[], options?: FunctionOptions): Promise<{
23
+ doEmbedValues(texts: string[], options: FunctionCallOptions): Promise<{
24
24
  response: {
25
25
  embedding: number[];
26
26
  };
@@ -47,18 +47,23 @@ export class LlamaCppTextEmbeddingModel extends AbstractModel {
47
47
  async tokenize(text) {
48
48
  return this.tokenizer.tokenize(text);
49
49
  }
50
- async callAPI(texts, options) {
50
+ async callAPI(texts, callOptions) {
51
51
  if (texts.length > this.maxValuesPerCall) {
52
52
  throw new Error(`The Llama.cpp embedding API only supports ${this.maxValuesPerCall} texts per API call.`);
53
53
  }
54
54
  const api = this.settings.api ?? new LlamaCppApiConfiguration();
55
- const abortSignal = options?.run?.abortSignal;
55
+ const abortSignal = callOptions.run?.abortSignal;
56
56
  return callWithRetryAndThrottle({
57
57
  retry: this.settings.api?.retry,
58
58
  throttle: this.settings.api?.throttle,
59
59
  call: async () => postJsonToApi({
60
60
  url: api.assembleUrl(`/embedding`),
61
- headers: api.headers,
61
+ headers: api.headers({
62
+ functionType: callOptions.functionType,
63
+ functionId: callOptions.functionId,
64
+ run: callOptions.run,
65
+ callId: callOptions.callId,
66
+ }),
62
67
  body: { content: texts[0] },
63
68
  failedResponseHandler: failedLlamaCppCallResponseHandler,
64
69
  successfulResponseHandler: createJsonResponseHandler(zodSchema(llamaCppTextEmbeddingResponseSchema)),
@@ -30,15 +30,20 @@ class LlamaCppTokenizer {
30
30
  });
31
31
  this.api = api;
32
32
  }
33
- async callTokenizeAPI(text, context) {
33
+ async callTokenizeAPI(text, callOptions) {
34
34
  const api = this.api;
35
- const abortSignal = context?.abortSignal;
35
+ const abortSignal = callOptions?.run?.abortSignal;
36
36
  return (0, callWithRetryAndThrottle_js_1.callWithRetryAndThrottle)({
37
37
  retry: api.retry,
38
38
  throttle: api.throttle,
39
39
  call: async () => (0, postToApi_js_1.postJsonToApi)({
40
40
  url: api.assembleUrl(`/tokenize`),
41
- headers: api.headers,
41
+ headers: api.headers({
42
+ functionType: "tokenize",
43
+ functionId: callOptions?.functionId,
44
+ run: callOptions?.run,
45
+ callId: "",
46
+ }),
42
47
  body: {
43
48
  content: text,
44
49
  },
@@ -1,5 +1,5 @@
1
1
  import { z } from "zod";
2
- import { Run } from "../../core/Run.js";
2
+ import { FunctionCallOptions } from "../../core/FunctionOptions.js";
3
3
  import { ApiConfiguration } from "../../core/api/ApiConfiguration.js";
4
4
  import { BasicTokenizer } from "../../model-function/tokenize-text/Tokenizer.js";
5
5
  /**
@@ -18,7 +18,7 @@ import { BasicTokenizer } from "../../model-function/tokenize-text/Tokenizer.js"
18
18
  export declare class LlamaCppTokenizer implements BasicTokenizer {
19
19
  readonly api: ApiConfiguration;
20
20
  constructor(api?: ApiConfiguration);
21
- callTokenizeAPI(text: string, context?: Run): Promise<LlamaCppTokenizationResponse>;
21
+ callTokenizeAPI(text: string, callOptions?: FunctionCallOptions): Promise<LlamaCppTokenizationResponse>;
22
22
  tokenize(text: string): Promise<number[]>;
23
23
  }
24
24
  declare const llamaCppTokenizationResponseSchema: z.ZodObject<{