modelfusion 0.113.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.
- package/CHANGELOG.md +59 -0
- package/README.md +89 -89
- package/core/FunctionOptions.d.ts +14 -0
- package/core/api/AbstractApiConfiguration.cjs +16 -1
- package/core/api/AbstractApiConfiguration.d.ts +7 -3
- package/core/api/AbstractApiConfiguration.js +16 -1
- package/core/api/ApiConfiguration.d.ts +10 -1
- package/core/api/BaseUrlApiConfiguration.cjs +9 -5
- package/core/api/BaseUrlApiConfiguration.d.ts +7 -7
- package/core/api/BaseUrlApiConfiguration.js +9 -5
- package/core/api/CustomHeaderProvider.cjs +2 -0
- package/core/api/CustomHeaderProvider.d.ts +2 -0
- package/core/api/CustomHeaderProvider.js +1 -0
- package/core/api/index.cjs +1 -0
- package/core/api/index.d.ts +1 -0
- package/core/api/index.js +1 -0
- package/core/cache/Cache.cjs +2 -0
- package/core/cache/Cache.d.ts +12 -0
- package/core/cache/Cache.js +1 -0
- package/core/cache/MemoryCache.cjs +23 -0
- package/core/cache/MemoryCache.d.ts +15 -0
- package/core/cache/MemoryCache.js +19 -0
- package/core/cache/index.cjs +18 -0
- package/core/cache/index.d.ts +2 -0
- package/core/cache/index.js +2 -0
- package/core/index.cjs +1 -0
- package/core/index.d.ts +1 -0
- package/core/index.js +1 -0
- package/core/schema/TypeValidationError.cjs +36 -0
- package/core/schema/TypeValidationError.d.ts +15 -0
- package/core/schema/TypeValidationError.js +32 -0
- package/core/schema/index.cjs +2 -0
- package/core/schema/index.d.ts +2 -0
- package/core/schema/index.js +2 -0
- package/core/schema/parseJSON.cjs +6 -14
- package/core/schema/parseJSON.d.ts +3 -2
- package/core/schema/parseJSON.js +6 -14
- package/core/schema/validateTypes.cjs +65 -0
- package/core/schema/validateTypes.d.ts +34 -0
- package/core/schema/validateTypes.js +60 -0
- package/model-function/embed/EmbeddingModel.d.ts +2 -2
- package/model-function/executeStandardCall.cjs +3 -1
- package/model-function/executeStandardCall.d.ts +2 -2
- package/model-function/executeStandardCall.js +3 -1
- package/model-function/executeStreamCall.cjs +2 -1
- package/model-function/executeStreamCall.d.ts +2 -2
- package/model-function/executeStreamCall.js +2 -1
- package/model-function/generate-image/ImageGenerationModel.d.ts +2 -2
- package/model-function/generate-image/PromptTemplateImageGenerationModel.d.ts +2 -2
- package/model-function/generate-speech/SpeechGenerationModel.d.ts +3 -3
- package/model-function/generate-structure/generateStructure.cjs +4 -1
- package/model-function/generate-structure/generateStructure.js +4 -1
- package/model-function/generate-structure/streamStructure.cjs +4 -1
- package/model-function/generate-structure/streamStructure.js +4 -1
- package/model-function/generate-text/PromptTemplateTextGenerationModel.cjs +3 -0
- package/model-function/generate-text/PromptTemplateTextGenerationModel.d.ts +11 -2
- package/model-function/generate-text/PromptTemplateTextGenerationModel.js +3 -0
- package/model-function/generate-text/PromptTemplateTextStreamingModel.d.ts +2 -2
- package/model-function/generate-text/TextGenerationModel.d.ts +12 -3
- package/model-function/generate-text/generateText.cjs +43 -1
- package/model-function/generate-text/generateText.js +43 -1
- package/model-function/generate-transcription/TranscriptionModel.d.ts +2 -2
- package/model-provider/anthropic/AnthropicTextGenerationModel.cjs +20 -8
- package/model-provider/anthropic/AnthropicTextGenerationModel.d.ts +27 -5
- package/model-provider/anthropic/AnthropicTextGenerationModel.js +20 -8
- package/model-provider/automatic1111/Automatic1111ImageGenerationModel.cjs +8 -3
- package/model-provider/automatic1111/Automatic1111ImageGenerationModel.d.ts +3 -3
- package/model-provider/automatic1111/Automatic1111ImageGenerationModel.js +8 -3
- package/model-provider/cohere/CohereTextEmbeddingModel.cjs +8 -3
- package/model-provider/cohere/CohereTextEmbeddingModel.d.ts +3 -3
- package/model-provider/cohere/CohereTextEmbeddingModel.js +8 -3
- package/model-provider/cohere/CohereTextGenerationModel.cjs +20 -8
- package/model-provider/cohere/CohereTextGenerationModel.d.ts +45 -5
- package/model-provider/cohere/CohereTextGenerationModel.js +20 -8
- package/model-provider/cohere/CohereTokenizer.cjs +16 -6
- package/model-provider/cohere/CohereTokenizer.d.ts +3 -3
- package/model-provider/cohere/CohereTokenizer.js +16 -6
- package/model-provider/elevenlabs/ElevenLabsApiConfiguration.cjs +1 -1
- package/model-provider/elevenlabs/ElevenLabsApiConfiguration.js +1 -1
- package/model-provider/elevenlabs/ElevenLabsSpeechModel.cjs +8 -3
- package/model-provider/elevenlabs/ElevenLabsSpeechModel.d.ts +2 -2
- package/model-provider/elevenlabs/ElevenLabsSpeechModel.js +8 -3
- package/model-provider/huggingface/HuggingFaceTextEmbeddingModel.cjs +8 -3
- package/model-provider/huggingface/HuggingFaceTextEmbeddingModel.d.ts +3 -3
- package/model-provider/huggingface/HuggingFaceTextEmbeddingModel.js +8 -3
- package/model-provider/huggingface/HuggingFaceTextGenerationModel.cjs +18 -4
- package/model-provider/huggingface/HuggingFaceTextGenerationModel.d.ts +21 -3
- package/model-provider/huggingface/HuggingFaceTextGenerationModel.js +18 -4
- package/model-provider/llamacpp/LlamaCppCompletionModel.cjs +20 -8
- package/model-provider/llamacpp/LlamaCppCompletionModel.d.ts +125 -5
- package/model-provider/llamacpp/LlamaCppCompletionModel.js +20 -8
- package/model-provider/llamacpp/LlamaCppTextEmbeddingModel.cjs +8 -3
- package/model-provider/llamacpp/LlamaCppTextEmbeddingModel.d.ts +3 -3
- package/model-provider/llamacpp/LlamaCppTextEmbeddingModel.js +8 -3
- package/model-provider/llamacpp/LlamaCppTokenizer.cjs +8 -3
- package/model-provider/llamacpp/LlamaCppTokenizer.d.ts +2 -2
- package/model-provider/llamacpp/LlamaCppTokenizer.js +8 -3
- package/model-provider/lmnt/LmntSpeechModel.cjs +8 -3
- package/model-provider/lmnt/LmntSpeechModel.d.ts +2 -2
- package/model-provider/lmnt/LmntSpeechModel.js +8 -3
- package/model-provider/mistral/MistralChatModel.cjs +20 -8
- package/model-provider/mistral/MistralChatModel.d.ts +55 -5
- package/model-provider/mistral/MistralChatModel.js +20 -8
- package/model-provider/mistral/MistralTextEmbeddingModel.cjs +8 -3
- package/model-provider/mistral/MistralTextEmbeddingModel.d.ts +3 -3
- package/model-provider/mistral/MistralTextEmbeddingModel.js +8 -3
- package/model-provider/ollama/OllamaChatModel.cjs +20 -8
- package/model-provider/ollama/OllamaChatModel.d.ts +27 -5
- package/model-provider/ollama/OllamaChatModel.js +20 -8
- package/model-provider/ollama/OllamaCompletionModel.cjs +20 -7
- package/model-provider/ollama/OllamaCompletionModel.d.ts +43 -5
- package/model-provider/ollama/OllamaCompletionModel.js +20 -7
- package/model-provider/ollama/OllamaTextEmbeddingModel.cjs +8 -3
- package/model-provider/ollama/OllamaTextEmbeddingModel.d.ts +3 -3
- package/model-provider/ollama/OllamaTextEmbeddingModel.js +8 -3
- package/model-provider/openai/AbstractOpenAIChatModel.cjs +23 -13
- package/model-provider/openai/AbstractOpenAIChatModel.d.ts +94 -7
- package/model-provider/openai/AbstractOpenAIChatModel.js +23 -13
- package/model-provider/openai/AbstractOpenAICompletionModel.cjs +21 -9
- package/model-provider/openai/AbstractOpenAICompletionModel.d.ts +35 -5
- package/model-provider/openai/AbstractOpenAICompletionModel.js +21 -9
- package/model-provider/openai/AzureOpenAIApiConfiguration.cjs +5 -2
- package/model-provider/openai/AzureOpenAIApiConfiguration.d.ts +2 -1
- package/model-provider/openai/AzureOpenAIApiConfiguration.js +5 -2
- package/model-provider/openai/OpenAIChatFunctionCallStructureGenerationModel.cjs +12 -6
- package/model-provider/openai/OpenAIChatFunctionCallStructureGenerationModel.d.ts +89 -5
- package/model-provider/openai/OpenAIChatFunctionCallStructureGenerationModel.js +12 -6
- package/model-provider/openai/OpenAIImageGenerationModel.cjs +10 -6
- package/model-provider/openai/OpenAIImageGenerationModel.d.ts +4 -4
- package/model-provider/openai/OpenAIImageGenerationModel.js +10 -6
- package/model-provider/openai/OpenAISpeechModel.cjs +9 -4
- package/model-provider/openai/OpenAISpeechModel.d.ts +3 -3
- package/model-provider/openai/OpenAISpeechModel.js +9 -4
- package/model-provider/openai/OpenAITextEmbeddingModel.cjs +11 -6
- package/model-provider/openai/OpenAITextEmbeddingModel.d.ts +3 -3
- package/model-provider/openai/OpenAITextEmbeddingModel.js +11 -6
- package/model-provider/openai/OpenAITranscriptionModel.cjs +9 -6
- package/model-provider/openai/OpenAITranscriptionModel.d.ts +4 -4
- package/model-provider/openai/OpenAITranscriptionModel.js +9 -6
- package/model-provider/stability/StabilityImageGenerationModel.cjs +10 -5
- package/model-provider/stability/StabilityImageGenerationModel.d.ts +3 -3
- package/model-provider/stability/StabilityImageGenerationModel.js +10 -5
- package/model-provider/whispercpp/WhisperCppTranscriptionModel.cjs +9 -7
- package/model-provider/whispercpp/WhisperCppTranscriptionModel.d.ts +3 -3
- package/model-provider/whispercpp/WhisperCppTranscriptionModel.js +9 -7
- package/observability/helicone/HeliconeOpenAIApiConfiguration.cjs +2 -1
- package/observability/helicone/HeliconeOpenAIApiConfiguration.d.ts +3 -1
- package/observability/helicone/HeliconeOpenAIApiConfiguration.js +2 -1
- package/package.json +1 -1
@@ -43,12 +43,12 @@ class MistralTextEmbeddingModel extends AbstractModel_js_1.AbstractModel {
|
|
43
43
|
get modelName() {
|
44
44
|
return this.settings.model;
|
45
45
|
}
|
46
|
-
async callAPI(texts,
|
46
|
+
async callAPI(texts, callOptions) {
|
47
47
|
if (texts.length > this.maxValuesPerCall) {
|
48
48
|
throw new Error(`The Mistral embedding API only supports ${this.maxValuesPerCall} texts per API call.`);
|
49
49
|
}
|
50
50
|
const api = this.settings.api ?? new MistralApiConfiguration_js_1.MistralApiConfiguration();
|
51
|
-
const abortSignal =
|
51
|
+
const abortSignal = callOptions.run?.abortSignal;
|
52
52
|
const model = this.settings.model;
|
53
53
|
const encodingFormat = this.settings.encodingFormat ?? "float";
|
54
54
|
return (0, callWithRetryAndThrottle_js_1.callWithRetryAndThrottle)({
|
@@ -56,7 +56,12 @@ class MistralTextEmbeddingModel extends AbstractModel_js_1.AbstractModel {
|
|
56
56
|
throttle: this.settings.api?.throttle,
|
57
57
|
call: async () => (0, postToApi_js_1.postJsonToApi)({
|
58
58
|
url: api.assembleUrl(`/embeddings`),
|
59
|
-
headers: api.headers
|
59
|
+
headers: api.headers({
|
60
|
+
functionType: callOptions.functionType,
|
61
|
+
functionId: callOptions.functionId,
|
62
|
+
run: callOptions.run,
|
63
|
+
callId: callOptions.callId,
|
64
|
+
}),
|
60
65
|
body: {
|
61
66
|
model,
|
62
67
|
input: texts,
|
@@ -1,5 +1,5 @@
|
|
1
1
|
import { z } from "zod";
|
2
|
-
import {
|
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";
|
@@ -27,9 +27,9 @@ export declare class MistralTextEmbeddingModel extends AbstractModel<MistralText
|
|
27
27
|
*/
|
28
28
|
readonly isParallelizable = false;
|
29
29
|
readonly embeddingDimensions = 1024;
|
30
|
-
callAPI(texts: Array<string>,
|
30
|
+
callAPI(texts: Array<string>, callOptions: FunctionCallOptions): Promise<MistralTextEmbeddingResponse>;
|
31
31
|
get settingsForEvent(): Partial<MistralTextEmbeddingModelSettings>;
|
32
|
-
doEmbedValues(texts: string[], options
|
32
|
+
doEmbedValues(texts: string[], options: FunctionCallOptions): Promise<{
|
33
33
|
response: {
|
34
34
|
object: string;
|
35
35
|
data: {
|
@@ -40,12 +40,12 @@ export class MistralTextEmbeddingModel extends AbstractModel {
|
|
40
40
|
get modelName() {
|
41
41
|
return this.settings.model;
|
42
42
|
}
|
43
|
-
async callAPI(texts,
|
43
|
+
async callAPI(texts, callOptions) {
|
44
44
|
if (texts.length > this.maxValuesPerCall) {
|
45
45
|
throw new Error(`The Mistral embedding API only supports ${this.maxValuesPerCall} texts per API call.`);
|
46
46
|
}
|
47
47
|
const api = this.settings.api ?? new MistralApiConfiguration();
|
48
|
-
const abortSignal =
|
48
|
+
const abortSignal = callOptions.run?.abortSignal;
|
49
49
|
const model = this.settings.model;
|
50
50
|
const encodingFormat = this.settings.encodingFormat ?? "float";
|
51
51
|
return callWithRetryAndThrottle({
|
@@ -53,7 +53,12 @@ export class MistralTextEmbeddingModel extends AbstractModel {
|
|
53
53
|
throttle: this.settings.api?.throttle,
|
54
54
|
call: async () => postJsonToApi({
|
55
55
|
url: api.assembleUrl(`/embeddings`),
|
56
|
-
headers: api.headers
|
56
|
+
headers: api.headers({
|
57
|
+
functionType: callOptions.functionType,
|
58
|
+
functionId: callOptions.functionId,
|
59
|
+
run: callOptions.run,
|
60
|
+
callId: callOptions.callId,
|
61
|
+
}),
|
57
62
|
body: {
|
58
63
|
model,
|
59
64
|
input: texts,
|
@@ -7,6 +7,7 @@ const callWithRetryAndThrottle_js_1 = require("../../core/api/callWithRetryAndTh
|
|
7
7
|
const postToApi_js_1 = require("../../core/api/postToApi.cjs");
|
8
8
|
const ZodSchema_js_1 = require("../../core/schema/ZodSchema.cjs");
|
9
9
|
const parseJSON_js_1 = require("../../core/schema/parseJSON.cjs");
|
10
|
+
const validateTypes_js_1 = require("../../core/schema/validateTypes.cjs");
|
10
11
|
const AbstractModel_js_1 = require("../../model-function/AbstractModel.cjs");
|
11
12
|
const StructureFromTextStreamingModel_js_1 = require("../../model-function/generate-structure/StructureFromTextStreamingModel.cjs");
|
12
13
|
const PromptTemplateTextStreamingModel_js_1 = require("../../model-function/generate-text/PromptTemplateTextStreamingModel.cjs");
|
@@ -51,16 +52,21 @@ class OllamaChatModel extends AbstractModel_js_1.AbstractModel {
|
|
51
52
|
get modelName() {
|
52
53
|
return this.settings.model;
|
53
54
|
}
|
54
|
-
async callAPI(prompt, options) {
|
55
|
+
async callAPI(prompt, callOptions, options) {
|
55
56
|
const { responseFormat } = options;
|
56
57
|
const api = this.settings.api ?? new OllamaApiConfiguration_js_1.OllamaApiConfiguration();
|
57
|
-
const abortSignal =
|
58
|
+
const abortSignal = callOptions.run?.abortSignal;
|
58
59
|
return (0, callWithRetryAndThrottle_js_1.callWithRetryAndThrottle)({
|
59
60
|
retry: api.retry,
|
60
61
|
throttle: api.throttle,
|
61
62
|
call: async () => (0, postToApi_js_1.postJsonToApi)({
|
62
63
|
url: api.assembleUrl(`/api/chat`),
|
63
|
-
headers: api.headers
|
64
|
+
headers: api.headers({
|
65
|
+
functionType: callOptions.functionType,
|
66
|
+
functionId: callOptions.functionId,
|
67
|
+
run: callOptions.run,
|
68
|
+
callId: callOptions.callId,
|
69
|
+
}),
|
64
70
|
body: {
|
65
71
|
stream: responseFormat.stream,
|
66
72
|
model: this.settings.model,
|
@@ -113,10 +119,17 @@ class OllamaChatModel extends AbstractModel_js_1.AbstractModel {
|
|
113
119
|
return Object.fromEntries(Object.entries(this.settings).filter(([key]) => eventSettingProperties.includes(key)));
|
114
120
|
}
|
115
121
|
async doGenerateTexts(prompt, options) {
|
116
|
-
|
117
|
-
...options,
|
122
|
+
return this.processTextGenerationResponse(await this.callAPI(prompt, options, {
|
118
123
|
responseFormat: exports.OllamaChatResponseFormat.json,
|
119
|
-
});
|
124
|
+
}));
|
125
|
+
}
|
126
|
+
restoreGeneratedTexts(rawResponse) {
|
127
|
+
return this.processTextGenerationResponse((0, validateTypes_js_1.validateTypes)({
|
128
|
+
structure: rawResponse,
|
129
|
+
schema: (0, ZodSchema_js_1.zodSchema)(ollamaChatResponseSchema),
|
130
|
+
}));
|
131
|
+
}
|
132
|
+
processTextGenerationResponse(response) {
|
120
133
|
return {
|
121
134
|
response,
|
122
135
|
textGenerationResults: [
|
@@ -128,8 +141,7 @@ class OllamaChatModel extends AbstractModel_js_1.AbstractModel {
|
|
128
141
|
};
|
129
142
|
}
|
130
143
|
doStreamText(prompt, options) {
|
131
|
-
return this.callAPI(prompt, {
|
132
|
-
...options,
|
144
|
+
return this.callAPI(prompt, options, {
|
133
145
|
responseFormat: exports.OllamaChatResponseFormat.deltaIterable,
|
134
146
|
});
|
135
147
|
}
|
@@ -1,5 +1,5 @@
|
|
1
1
|
import { z } from "zod";
|
2
|
-
import {
|
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";
|
@@ -34,11 +34,11 @@ export declare class OllamaChatModel extends AbstractModel<OllamaChatModelSettin
|
|
34
34
|
readonly tokenizer: undefined;
|
35
35
|
readonly countPromptTokens: undefined;
|
36
36
|
readonly contextWindowSize: undefined;
|
37
|
-
callAPI<RESPONSE>(prompt: OllamaChatPrompt, options: {
|
37
|
+
callAPI<RESPONSE>(prompt: OllamaChatPrompt, callOptions: FunctionCallOptions, options: {
|
38
38
|
responseFormat: OllamaChatResponseFormatType<RESPONSE>;
|
39
|
-
}
|
39
|
+
}): Promise<RESPONSE>;
|
40
40
|
get settingsForEvent(): Partial<OllamaChatModelSettings>;
|
41
|
-
doGenerateTexts(prompt: OllamaChatPrompt, options
|
41
|
+
doGenerateTexts(prompt: OllamaChatPrompt, options: FunctionCallOptions): Promise<{
|
42
42
|
response: {
|
43
43
|
message: {
|
44
44
|
role: string;
|
@@ -59,7 +59,29 @@ export declare class OllamaChatModel extends AbstractModel<OllamaChatModelSettin
|
|
59
59
|
finishReason: "unknown";
|
60
60
|
}[];
|
61
61
|
}>;
|
62
|
-
|
62
|
+
restoreGeneratedTexts(rawResponse: unknown): {
|
63
|
+
response: {
|
64
|
+
message: {
|
65
|
+
role: string;
|
66
|
+
content: string;
|
67
|
+
};
|
68
|
+
model: string;
|
69
|
+
done: true;
|
70
|
+
created_at: string;
|
71
|
+
total_duration: number;
|
72
|
+
prompt_eval_count: number;
|
73
|
+
eval_count: number;
|
74
|
+
eval_duration: number;
|
75
|
+
load_duration?: number | undefined;
|
76
|
+
prompt_eval_duration?: number | undefined;
|
77
|
+
};
|
78
|
+
textGenerationResults: {
|
79
|
+
text: string;
|
80
|
+
finishReason: "unknown";
|
81
|
+
}[];
|
82
|
+
};
|
83
|
+
private processTextGenerationResponse;
|
84
|
+
doStreamText(prompt: OllamaChatPrompt, options: FunctionCallOptions): Promise<AsyncIterable<import("../../index.js").Delta<{
|
63
85
|
message: {
|
64
86
|
role: string;
|
65
87
|
content: string;
|
@@ -4,6 +4,7 @@ import { callWithRetryAndThrottle } from "../../core/api/callWithRetryAndThrottl
|
|
4
4
|
import { postJsonToApi } from "../../core/api/postToApi.js";
|
5
5
|
import { zodSchema } from "../../core/schema/ZodSchema.js";
|
6
6
|
import { safeParseJSON } from "../../core/schema/parseJSON.js";
|
7
|
+
import { validateTypes } from "../../core/schema/validateTypes.js";
|
7
8
|
import { AbstractModel } from "../../model-function/AbstractModel.js";
|
8
9
|
import { StructureFromTextStreamingModel } from "../../model-function/generate-structure/StructureFromTextStreamingModel.js";
|
9
10
|
import { PromptTemplateTextStreamingModel } from "../../model-function/generate-text/PromptTemplateTextStreamingModel.js";
|
@@ -48,16 +49,21 @@ export class OllamaChatModel extends AbstractModel {
|
|
48
49
|
get modelName() {
|
49
50
|
return this.settings.model;
|
50
51
|
}
|
51
|
-
async callAPI(prompt, options) {
|
52
|
+
async callAPI(prompt, callOptions, options) {
|
52
53
|
const { responseFormat } = options;
|
53
54
|
const api = this.settings.api ?? new OllamaApiConfiguration();
|
54
|
-
const abortSignal =
|
55
|
+
const abortSignal = callOptions.run?.abortSignal;
|
55
56
|
return callWithRetryAndThrottle({
|
56
57
|
retry: api.retry,
|
57
58
|
throttle: api.throttle,
|
58
59
|
call: async () => postJsonToApi({
|
59
60
|
url: api.assembleUrl(`/api/chat`),
|
60
|
-
headers: api.headers
|
61
|
+
headers: api.headers({
|
62
|
+
functionType: callOptions.functionType,
|
63
|
+
functionId: callOptions.functionId,
|
64
|
+
run: callOptions.run,
|
65
|
+
callId: callOptions.callId,
|
66
|
+
}),
|
61
67
|
body: {
|
62
68
|
stream: responseFormat.stream,
|
63
69
|
model: this.settings.model,
|
@@ -110,10 +116,17 @@ export class OllamaChatModel extends AbstractModel {
|
|
110
116
|
return Object.fromEntries(Object.entries(this.settings).filter(([key]) => eventSettingProperties.includes(key)));
|
111
117
|
}
|
112
118
|
async doGenerateTexts(prompt, options) {
|
113
|
-
|
114
|
-
...options,
|
119
|
+
return this.processTextGenerationResponse(await this.callAPI(prompt, options, {
|
115
120
|
responseFormat: OllamaChatResponseFormat.json,
|
116
|
-
});
|
121
|
+
}));
|
122
|
+
}
|
123
|
+
restoreGeneratedTexts(rawResponse) {
|
124
|
+
return this.processTextGenerationResponse(validateTypes({
|
125
|
+
structure: rawResponse,
|
126
|
+
schema: zodSchema(ollamaChatResponseSchema),
|
127
|
+
}));
|
128
|
+
}
|
129
|
+
processTextGenerationResponse(response) {
|
117
130
|
return {
|
118
131
|
response,
|
119
132
|
textGenerationResults: [
|
@@ -125,8 +138,7 @@ export class OllamaChatModel extends AbstractModel {
|
|
125
138
|
};
|
126
139
|
}
|
127
140
|
doStreamText(prompt, options) {
|
128
|
-
return this.callAPI(prompt, {
|
129
|
-
...options,
|
141
|
+
return this.callAPI(prompt, options, {
|
130
142
|
responseFormat: OllamaChatResponseFormat.deltaIterable,
|
131
143
|
});
|
132
144
|
}
|
@@ -7,6 +7,7 @@ const callWithRetryAndThrottle_js_1 = require("../../core/api/callWithRetryAndTh
|
|
7
7
|
const postToApi_js_1 = require("../../core/api/postToApi.cjs");
|
8
8
|
const ZodSchema_js_1 = require("../../core/schema/ZodSchema.cjs");
|
9
9
|
const parseJSON_js_1 = require("../../core/schema/parseJSON.cjs");
|
10
|
+
const validateTypes_js_1 = require("../../core/schema/validateTypes.cjs");
|
10
11
|
const AbstractModel_js_1 = require("../../model-function/AbstractModel.cjs");
|
11
12
|
const PromptTemplateTextStreamingModel_js_1 = require("../../model-function/generate-text/PromptTemplateTextStreamingModel.cjs");
|
12
13
|
const TextGenerationModel_js_1 = require("../../model-function/generate-text/TextGenerationModel.cjs");
|
@@ -43,16 +44,21 @@ class OllamaCompletionModel extends AbstractModel_js_1.AbstractModel {
|
|
43
44
|
get contextWindowSize() {
|
44
45
|
return this.settings.contextWindowSize;
|
45
46
|
}
|
46
|
-
async callAPI(prompt, options) {
|
47
|
+
async callAPI(prompt, callOptions, options) {
|
47
48
|
const { responseFormat } = options;
|
48
49
|
const api = this.settings.api ?? new OllamaApiConfiguration_js_1.OllamaApiConfiguration();
|
49
|
-
const abortSignal =
|
50
|
+
const abortSignal = callOptions.run?.abortSignal;
|
50
51
|
return (0, callWithRetryAndThrottle_js_1.callWithRetryAndThrottle)({
|
51
52
|
retry: api.retry,
|
52
53
|
throttle: api.throttle,
|
53
54
|
call: async () => (0, postToApi_js_1.postJsonToApi)({
|
54
55
|
url: api.assembleUrl(`/api/generate`),
|
55
|
-
headers: api.headers
|
56
|
+
headers: api.headers({
|
57
|
+
functionType: callOptions.functionType,
|
58
|
+
functionId: callOptions.functionId,
|
59
|
+
run: callOptions.run,
|
60
|
+
callId: callOptions.callId,
|
61
|
+
}),
|
56
62
|
body: {
|
57
63
|
stream: responseFormat.stream,
|
58
64
|
model: this.settings.model,
|
@@ -114,10 +120,17 @@ class OllamaCompletionModel extends AbstractModel_js_1.AbstractModel {
|
|
114
120
|
return Object.fromEntries(Object.entries(this.settings).filter(([key]) => eventSettingProperties.includes(key)));
|
115
121
|
}
|
116
122
|
async doGenerateTexts(prompt, options) {
|
117
|
-
|
118
|
-
...options,
|
123
|
+
return this.processTextGenerationResponse(await this.callAPI(prompt, options, {
|
119
124
|
responseFormat: exports.OllamaCompletionResponseFormat.json,
|
120
|
-
});
|
125
|
+
}));
|
126
|
+
}
|
127
|
+
restoreGeneratedTexts(rawResponse) {
|
128
|
+
return this.processTextGenerationResponse((0, validateTypes_js_1.validateTypes)({
|
129
|
+
structure: rawResponse,
|
130
|
+
schema: (0, ZodSchema_js_1.zodSchema)(ollamaCompletionResponseSchema),
|
131
|
+
}));
|
132
|
+
}
|
133
|
+
processTextGenerationResponse(response) {
|
121
134
|
return {
|
122
135
|
response,
|
123
136
|
textGenerationResults: [
|
@@ -129,7 +142,7 @@ class OllamaCompletionModel extends AbstractModel_js_1.AbstractModel {
|
|
129
142
|
};
|
130
143
|
}
|
131
144
|
doStreamText(prompt, options) {
|
132
|
-
return this.callAPI(prompt, {
|
145
|
+
return this.callAPI(prompt, options, {
|
133
146
|
...options,
|
134
147
|
responseFormat: exports.OllamaCompletionResponseFormat.deltaIterable,
|
135
148
|
});
|
@@ -1,5 +1,5 @@
|
|
1
1
|
import { z } from "zod";
|
2
|
-
import {
|
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";
|
@@ -47,11 +47,11 @@ export declare class OllamaCompletionModel<CONTEXT_WINDOW_SIZE extends number |
|
|
47
47
|
readonly tokenizer: undefined;
|
48
48
|
readonly countPromptTokens: undefined;
|
49
49
|
get contextWindowSize(): CONTEXT_WINDOW_SIZE;
|
50
|
-
callAPI<RESPONSE>(prompt: OllamaCompletionPrompt, options: {
|
50
|
+
callAPI<RESPONSE>(prompt: OllamaCompletionPrompt, callOptions: FunctionCallOptions, options: {
|
51
51
|
responseFormat: OllamaCompletionResponseFormatType<RESPONSE>;
|
52
|
-
}
|
52
|
+
}): Promise<RESPONSE>;
|
53
53
|
get settingsForEvent(): Partial<OllamaCompletionModelSettings<CONTEXT_WINDOW_SIZE>>;
|
54
|
-
doGenerateTexts(prompt: OllamaCompletionPrompt, options
|
54
|
+
doGenerateTexts(prompt: OllamaCompletionPrompt, options: FunctionCallOptions): Promise<{
|
55
55
|
response: {
|
56
56
|
response: string;
|
57
57
|
model: string;
|
@@ -70,7 +70,45 @@ export declare class OllamaCompletionModel<CONTEXT_WINDOW_SIZE extends number |
|
|
70
70
|
finishReason: "unknown";
|
71
71
|
}[];
|
72
72
|
}>;
|
73
|
-
|
73
|
+
restoreGeneratedTexts(rawResponse: unknown): {
|
74
|
+
response: {
|
75
|
+
response: string;
|
76
|
+
model: string;
|
77
|
+
done: true;
|
78
|
+
created_at: string;
|
79
|
+
total_duration: number;
|
80
|
+
prompt_eval_count: number;
|
81
|
+
eval_count: number;
|
82
|
+
eval_duration: number;
|
83
|
+
load_duration?: number | undefined;
|
84
|
+
prompt_eval_duration?: number | undefined;
|
85
|
+
context?: number[] | undefined;
|
86
|
+
};
|
87
|
+
textGenerationResults: {
|
88
|
+
text: string;
|
89
|
+
finishReason: "unknown";
|
90
|
+
}[];
|
91
|
+
};
|
92
|
+
processTextGenerationResponse(response: OllamaCompletionResponse): {
|
93
|
+
response: {
|
94
|
+
response: string;
|
95
|
+
model: string;
|
96
|
+
done: true;
|
97
|
+
created_at: string;
|
98
|
+
total_duration: number;
|
99
|
+
prompt_eval_count: number;
|
100
|
+
eval_count: number;
|
101
|
+
eval_duration: number;
|
102
|
+
load_duration?: number | undefined;
|
103
|
+
prompt_eval_duration?: number | undefined;
|
104
|
+
context?: number[] | undefined;
|
105
|
+
};
|
106
|
+
textGenerationResults: {
|
107
|
+
text: string;
|
108
|
+
finishReason: "unknown";
|
109
|
+
}[];
|
110
|
+
};
|
111
|
+
doStreamText(prompt: OllamaCompletionPrompt, options: FunctionCallOptions): Promise<AsyncIterable<import("../../index.js").Delta<{
|
74
112
|
response: string;
|
75
113
|
model: string;
|
76
114
|
done: false;
|
@@ -4,6 +4,7 @@ import { callWithRetryAndThrottle } from "../../core/api/callWithRetryAndThrottl
|
|
4
4
|
import { postJsonToApi } from "../../core/api/postToApi.js";
|
5
5
|
import { zodSchema } from "../../core/schema/ZodSchema.js";
|
6
6
|
import { safeParseJSON } from "../../core/schema/parseJSON.js";
|
7
|
+
import { validateTypes } from "../../core/schema/validateTypes.js";
|
7
8
|
import { AbstractModel } from "../../model-function/AbstractModel.js";
|
8
9
|
import { PromptTemplateTextStreamingModel } from "../../model-function/generate-text/PromptTemplateTextStreamingModel.js";
|
9
10
|
import { textGenerationModelProperties, } from "../../model-function/generate-text/TextGenerationModel.js";
|
@@ -40,16 +41,21 @@ export class OllamaCompletionModel extends AbstractModel {
|
|
40
41
|
get contextWindowSize() {
|
41
42
|
return this.settings.contextWindowSize;
|
42
43
|
}
|
43
|
-
async callAPI(prompt, options) {
|
44
|
+
async callAPI(prompt, callOptions, options) {
|
44
45
|
const { responseFormat } = options;
|
45
46
|
const api = this.settings.api ?? new OllamaApiConfiguration();
|
46
|
-
const abortSignal =
|
47
|
+
const abortSignal = callOptions.run?.abortSignal;
|
47
48
|
return callWithRetryAndThrottle({
|
48
49
|
retry: api.retry,
|
49
50
|
throttle: api.throttle,
|
50
51
|
call: async () => postJsonToApi({
|
51
52
|
url: api.assembleUrl(`/api/generate`),
|
52
|
-
headers: api.headers
|
53
|
+
headers: api.headers({
|
54
|
+
functionType: callOptions.functionType,
|
55
|
+
functionId: callOptions.functionId,
|
56
|
+
run: callOptions.run,
|
57
|
+
callId: callOptions.callId,
|
58
|
+
}),
|
53
59
|
body: {
|
54
60
|
stream: responseFormat.stream,
|
55
61
|
model: this.settings.model,
|
@@ -111,10 +117,17 @@ export class OllamaCompletionModel extends AbstractModel {
|
|
111
117
|
return Object.fromEntries(Object.entries(this.settings).filter(([key]) => eventSettingProperties.includes(key)));
|
112
118
|
}
|
113
119
|
async doGenerateTexts(prompt, options) {
|
114
|
-
|
115
|
-
...options,
|
120
|
+
return this.processTextGenerationResponse(await this.callAPI(prompt, options, {
|
116
121
|
responseFormat: OllamaCompletionResponseFormat.json,
|
117
|
-
});
|
122
|
+
}));
|
123
|
+
}
|
124
|
+
restoreGeneratedTexts(rawResponse) {
|
125
|
+
return this.processTextGenerationResponse(validateTypes({
|
126
|
+
structure: rawResponse,
|
127
|
+
schema: zodSchema(ollamaCompletionResponseSchema),
|
128
|
+
}));
|
129
|
+
}
|
130
|
+
processTextGenerationResponse(response) {
|
118
131
|
return {
|
119
132
|
response,
|
120
133
|
textGenerationResults: [
|
@@ -126,7 +139,7 @@ export class OllamaCompletionModel extends AbstractModel {
|
|
126
139
|
};
|
127
140
|
}
|
128
141
|
doStreamText(prompt, options) {
|
129
|
-
return this.callAPI(prompt, {
|
142
|
+
return this.callAPI(prompt, options, {
|
130
143
|
...options,
|
131
144
|
responseFormat: OllamaCompletionResponseFormat.deltaIterable,
|
132
145
|
});
|
@@ -33,18 +33,23 @@ class OllamaTextEmbeddingModel extends AbstractModel_js_1.AbstractModel {
|
|
33
33
|
get embeddingDimensions() {
|
34
34
|
return this.settings.embeddingDimensions;
|
35
35
|
}
|
36
|
-
async callAPI(texts,
|
36
|
+
async callAPI(texts, callOptions) {
|
37
37
|
if (texts.length > this.maxValuesPerCall) {
|
38
38
|
throw new Error(`The Ollama embedding API only supports ${this.maxValuesPerCall} texts per API call.`);
|
39
39
|
}
|
40
40
|
const api = this.settings.api ?? new OllamaApiConfiguration_js_1.OllamaApiConfiguration();
|
41
|
-
const abortSignal =
|
41
|
+
const abortSignal = callOptions.run?.abortSignal;
|
42
42
|
return (0, callWithRetryAndThrottle_js_1.callWithRetryAndThrottle)({
|
43
43
|
retry: api.retry,
|
44
44
|
throttle: api.throttle,
|
45
45
|
call: async () => (0, postToApi_js_1.postJsonToApi)({
|
46
46
|
url: api.assembleUrl(`/api/embeddings`),
|
47
|
-
headers: api.headers
|
47
|
+
headers: api.headers({
|
48
|
+
functionType: callOptions.functionType,
|
49
|
+
functionId: callOptions.functionId,
|
50
|
+
run: callOptions.run,
|
51
|
+
callId: callOptions.callId,
|
52
|
+
}),
|
48
53
|
body: {
|
49
54
|
model: this.settings.model,
|
50
55
|
prompt: texts[0],
|
@@ -1,5 +1,5 @@
|
|
1
1
|
import { z } from "zod";
|
2
|
-
import {
|
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";
|
@@ -16,9 +16,9 @@ export declare class OllamaTextEmbeddingModel extends AbstractModel<OllamaTextEm
|
|
16
16
|
readonly maxValuesPerCall = 1;
|
17
17
|
get isParallelizable(): boolean;
|
18
18
|
get embeddingDimensions(): number | undefined;
|
19
|
-
callAPI(texts: Array<string>,
|
19
|
+
callAPI(texts: Array<string>, callOptions: FunctionCallOptions): Promise<OllamaTextEmbeddingResponse>;
|
20
20
|
get settingsForEvent(): Partial<OllamaTextEmbeddingModelSettings>;
|
21
|
-
doEmbedValues(texts: string[], options
|
21
|
+
doEmbedValues(texts: string[], options: FunctionCallOptions): Promise<{
|
22
22
|
response: {
|
23
23
|
embedding: number[];
|
24
24
|
};
|
@@ -30,18 +30,23 @@ export class OllamaTextEmbeddingModel extends AbstractModel {
|
|
30
30
|
get embeddingDimensions() {
|
31
31
|
return this.settings.embeddingDimensions;
|
32
32
|
}
|
33
|
-
async callAPI(texts,
|
33
|
+
async callAPI(texts, callOptions) {
|
34
34
|
if (texts.length > this.maxValuesPerCall) {
|
35
35
|
throw new Error(`The Ollama embedding API only supports ${this.maxValuesPerCall} texts per API call.`);
|
36
36
|
}
|
37
37
|
const api = this.settings.api ?? new OllamaApiConfiguration();
|
38
|
-
const abortSignal =
|
38
|
+
const abortSignal = callOptions.run?.abortSignal;
|
39
39
|
return callWithRetryAndThrottle({
|
40
40
|
retry: api.retry,
|
41
41
|
throttle: api.throttle,
|
42
42
|
call: async () => postJsonToApi({
|
43
43
|
url: api.assembleUrl(`/api/embeddings`),
|
44
|
-
headers: api.headers
|
44
|
+
headers: api.headers({
|
45
|
+
functionType: callOptions.functionType,
|
46
|
+
functionId: callOptions.functionId,
|
47
|
+
run: callOptions.run,
|
48
|
+
callId: callOptions.callId,
|
49
|
+
}),
|
45
50
|
body: {
|
46
51
|
model: this.settings.model,
|
47
52
|
prompt: texts[0],
|
@@ -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 createEventSourceResponseHandler_js_1 = require("../../util/streaming/createEventSourceResponseHandler.cjs");
|
11
12
|
const OpenAIApiConfiguration_js_1 = require("./OpenAIApiConfiguration.cjs");
|
@@ -19,12 +20,12 @@ class AbstractOpenAIChatModel extends AbstractModel_js_1.AbstractModel {
|
|
19
20
|
constructor(settings) {
|
20
21
|
super({ settings });
|
21
22
|
}
|
22
|
-
async callAPI(messages, options) {
|
23
|
+
async callAPI(messages, callOptions, options) {
|
23
24
|
const api = this.settings.api ?? new OpenAIApiConfiguration_js_1.OpenAIApiConfiguration();
|
24
25
|
const responseFormat = options.responseFormat;
|
25
|
-
const abortSignal =
|
26
|
+
const abortSignal = callOptions.run?.abortSignal;
|
26
27
|
const user = this.settings.isUserIdForwardingEnabled
|
27
|
-
?
|
28
|
+
? callOptions.run?.userId
|
28
29
|
: undefined;
|
29
30
|
const openAIResponseFormat = this.settings.responseFormat;
|
30
31
|
// function & tool calling:
|
@@ -45,7 +46,12 @@ class AbstractOpenAIChatModel extends AbstractModel_js_1.AbstractModel {
|
|
45
46
|
}
|
46
47
|
return (0, postToApi_js_1.postJsonToApi)({
|
47
48
|
url: api.assembleUrl("/chat/completions"),
|
48
|
-
headers: api.headers
|
49
|
+
headers: api.headers({
|
50
|
+
functionType: callOptions.functionType,
|
51
|
+
functionId: callOptions.functionId,
|
52
|
+
run: callOptions.run,
|
53
|
+
callId: callOptions.callId,
|
54
|
+
}),
|
49
55
|
body: {
|
50
56
|
stream: responseFormat.stream,
|
51
57
|
model: this.settings.model,
|
@@ -74,10 +80,17 @@ class AbstractOpenAIChatModel extends AbstractModel_js_1.AbstractModel {
|
|
74
80
|
});
|
75
81
|
}
|
76
82
|
async doGenerateTexts(prompt, options) {
|
77
|
-
|
78
|
-
...options,
|
83
|
+
return this.processTextGenerationResponse(await this.callAPI(prompt, options, {
|
79
84
|
responseFormat: exports.OpenAIChatResponseFormat.json,
|
80
|
-
});
|
85
|
+
}));
|
86
|
+
}
|
87
|
+
restoreGeneratedTexts(rawResponse) {
|
88
|
+
return this.processTextGenerationResponse((0, validateTypes_js_1.validateTypes)({
|
89
|
+
structure: rawResponse,
|
90
|
+
schema: (0, ZodSchema_js_1.zodSchema)(openAIChatResponseSchema),
|
91
|
+
}));
|
92
|
+
}
|
93
|
+
processTextGenerationResponse(response) {
|
81
94
|
return {
|
82
95
|
response,
|
83
96
|
textGenerationResults: response.choices.map((choice) => ({
|
@@ -103,8 +116,7 @@ class AbstractOpenAIChatModel extends AbstractModel_js_1.AbstractModel {
|
|
103
116
|
}
|
104
117
|
}
|
105
118
|
doStreamText(prompt, options) {
|
106
|
-
return this.callAPI(prompt, {
|
107
|
-
...options,
|
119
|
+
return this.callAPI(prompt, options, {
|
108
120
|
responseFormat: exports.OpenAIChatResponseFormat.deltaIterable,
|
109
121
|
});
|
110
122
|
}
|
@@ -121,8 +133,7 @@ class AbstractOpenAIChatModel extends AbstractModel_js_1.AbstractModel {
|
|
121
133
|
return firstChoice.delta.content ?? undefined;
|
122
134
|
}
|
123
135
|
async doGenerateToolCall(tool, prompt, options) {
|
124
|
-
const response = await this.callAPI(prompt, {
|
125
|
-
...options,
|
136
|
+
const response = await this.callAPI(prompt, options, {
|
126
137
|
responseFormat: exports.OpenAIChatResponseFormat.json,
|
127
138
|
toolChoice: {
|
128
139
|
type: "function",
|
@@ -152,8 +163,7 @@ class AbstractOpenAIChatModel extends AbstractModel_js_1.AbstractModel {
|
|
152
163
|
};
|
153
164
|
}
|
154
165
|
async doGenerateToolCalls(tools, prompt, options) {
|
155
|
-
const response = await this.callAPI(prompt, {
|
156
|
-
...options,
|
166
|
+
const response = await this.callAPI(prompt, options, {
|
157
167
|
responseFormat: exports.OpenAIChatResponseFormat.json,
|
158
168
|
toolChoice: "auto",
|
159
169
|
tools: tools.map((tool) => ({
|