modelfusion 0.66.1 → 0.68.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/browser/invokeFlow.cjs +1 -1
- package/browser/invokeFlow.js +1 -1
- package/browser/readEventSource.cjs +2 -2
- package/browser/readEventSource.d.ts +1 -1
- package/browser/readEventSource.js +2 -2
- package/browser/readEventSourceStream.cjs +2 -2
- package/browser/readEventSourceStream.d.ts +1 -1
- package/browser/readEventSourceStream.js +2 -2
- package/core/index.cjs +1 -1
- package/core/index.d.ts +1 -1
- package/core/index.js +1 -1
- package/{util → core/schema}/JSONParseError.cjs +6 -6
- package/{util → core/schema}/JSONParseError.d.ts +3 -3
- package/{util → core/schema}/JSONParseError.js +6 -6
- package/core/{structure → schema}/ZodStructureDefinition.cjs +1 -1
- package/core/{structure → schema}/ZodStructureDefinition.d.ts +1 -1
- package/core/{structure → schema}/ZodStructureDefinition.js +1 -1
- package/core/{structure → schema}/index.cjs +2 -0
- package/core/{structure → schema}/index.d.ts +2 -0
- package/core/{structure → schema}/index.js +2 -0
- package/core/schema/parseJSON.cjs +56 -0
- package/core/schema/parseJSON.d.ts +58 -0
- package/core/schema/parseJSON.js +48 -0
- package/model-function/generate-structure/StructureFromTextGenerationModel.d.ts +1 -1
- package/model-function/generate-structure/StructureGenerationModel.d.ts +1 -1
- package/model-function/generate-structure/StructureOrTextGenerationModel.d.ts +1 -1
- package/model-function/generate-structure/generateStructure.d.ts +1 -1
- package/model-function/generate-structure/generateStructureOrText.d.ts +1 -1
- package/model-function/generate-structure/streamStructure.d.ts +1 -1
- package/model-provider/anthropic/AnthropicError.cjs +8 -4
- package/model-provider/anthropic/AnthropicError.d.ts +3 -19
- package/model-provider/anthropic/AnthropicError.js +8 -4
- package/model-provider/anthropic/AnthropicTextGenerationModel.cjs +8 -4
- package/model-provider/anthropic/AnthropicTextGenerationModel.js +8 -4
- package/model-provider/automatic1111/Automatic1111Error.cjs +8 -4
- package/model-provider/automatic1111/Automatic1111Error.d.ts +3 -13
- package/model-provider/automatic1111/Automatic1111Error.js +8 -4
- package/model-provider/cohere/CohereError.cjs +8 -4
- package/model-provider/cohere/CohereError.d.ts +3 -7
- package/model-provider/cohere/CohereError.js +8 -4
- package/model-provider/cohere/CohereTextGenerationModel.cjs +3 -2
- package/model-provider/cohere/CohereTextGenerationModel.js +3 -2
- package/model-provider/elevenlabs/ElevenLabsSpeechModel.cjs +28 -24
- package/model-provider/elevenlabs/ElevenLabsSpeechModel.d.ts +1 -1
- package/model-provider/elevenlabs/ElevenLabsSpeechModel.js +28 -24
- package/model-provider/huggingface/HuggingFaceError.cjs +8 -4
- package/model-provider/huggingface/HuggingFaceError.d.ts +3 -7
- package/model-provider/huggingface/HuggingFaceError.js +8 -4
- package/model-provider/llamacpp/LlamaCppError.cjs +8 -4
- package/model-provider/llamacpp/LlamaCppError.d.ts +3 -7
- package/model-provider/llamacpp/LlamaCppError.js +8 -4
- package/model-provider/llamacpp/LlamaCppTextGenerationModel.cjs +10 -6
- package/model-provider/llamacpp/LlamaCppTextGenerationModel.js +10 -6
- package/model-provider/ollama/OllamaTextGenerationModel.cjs +8 -3
- package/model-provider/ollama/OllamaTextGenerationModel.d.ts +78 -1
- package/model-provider/ollama/OllamaTextGenerationModel.js +8 -3
- package/model-provider/openai/OpenAICompletionModel.cjs +8 -4
- package/model-provider/openai/OpenAICompletionModel.js +8 -4
- package/model-provider/openai/OpenAIError.cjs +8 -4
- package/model-provider/openai/OpenAIError.d.ts +3 -27
- package/model-provider/openai/OpenAIError.js +8 -4
- package/model-provider/openai/chat/OpenAIChatModel.d.ts +1 -1
- package/model-provider/openai/chat/OpenAIChatStreamIterable.cjs +8 -4
- package/model-provider/openai/chat/OpenAIChatStreamIterable.js +8 -4
- package/model-provider/stability/StabilityError.cjs +8 -4
- package/model-provider/stability/StabilityError.d.ts +3 -7
- package/model-provider/stability/StabilityError.js +8 -4
- package/package.json +1 -1
- package/tool/Tool.d.ts +3 -3
- package/tool/WebSearchTool.cjs +1 -1
- package/tool/WebSearchTool.d.ts +1 -1
- package/tool/WebSearchTool.js +1 -1
- package/util/index.cjs +0 -2
- package/util/index.d.ts +0 -2
- package/util/index.js +0 -2
- package/util/streaming/parseJsonStream.cjs +5 -5
- package/util/streaming/parseJsonStream.d.ts +2 -1
- package/util/streaming/parseJsonStream.js +5 -5
- package/vector-index/memory/MemoryVectorIndex.cjs +5 -4
- package/vector-index/memory/MemoryVectorIndex.d.ts +1 -1
- package/vector-index/memory/MemoryVectorIndex.js +5 -4
- package/util/parseJSON.cjs +0 -67
- package/util/parseJSON.d.ts +0 -18
- package/util/parseJSON.js +0 -58
- /package/core/{structure → schema}/JsonSchemaProducer.cjs +0 -0
- /package/core/{structure → schema}/JsonSchemaProducer.d.ts +0 -0
- /package/core/{structure → schema}/JsonSchemaProducer.js +0 -0
- /package/core/{structure → schema}/Schema.cjs +0 -0
- /package/core/{structure → schema}/Schema.d.ts +0 -0
- /package/core/{structure → schema}/Schema.js +0 -0
- /package/core/{structure → schema}/StructureDefinition.cjs +0 -0
- /package/core/{structure → schema}/StructureDefinition.d.ts +0 -0
- /package/core/{structure → schema}/StructureDefinition.js +0 -0
- /package/core/{structure → schema}/UncheckedSchema.cjs +0 -0
- /package/core/{structure → schema}/UncheckedSchema.d.ts +0 -0
- /package/core/{structure → schema}/UncheckedSchema.js +0 -0
- /package/core/{structure → schema}/UncheckedStructureDefinition.cjs +0 -0
- /package/core/{structure → schema}/UncheckedStructureDefinition.d.ts +0 -0
- /package/core/{structure → schema}/UncheckedStructureDefinition.js +0 -0
- /package/core/{structure → schema}/ZodSchema.cjs +0 -0
- /package/core/{structure → schema}/ZodSchema.d.ts +0 -0
- /package/core/{structure → schema}/ZodSchema.js +0 -0
@@ -1,11 +1,12 @@
|
|
1
1
|
import { z } from "zod";
|
2
2
|
import { callWithRetryAndThrottle } from "../../core/api/callWithRetryAndThrottle.js";
|
3
3
|
import { createJsonResponseHandler, postJsonToApi, } from "../../core/api/postToApi.js";
|
4
|
-
import {
|
5
|
-
import { parseEventSourceStream } from "../../util/streaming/parseEventSourceStream.js";
|
4
|
+
import { ZodSchema } from "../../core/schema/ZodSchema.js";
|
6
5
|
import { AbstractModel } from "../../model-function/AbstractModel.js";
|
7
6
|
import { PromptFormatTextStreamingModel } from "../../model-function/generate-text/PromptFormatTextStreamingModel.js";
|
8
|
-
import {
|
7
|
+
import { AsyncQueue } from "../../util/AsyncQueue.js";
|
8
|
+
import { parseJSON } from "../../core/schema/parseJSON.js";
|
9
|
+
import { parseEventSourceStream } from "../../util/streaming/parseEventSourceStream.js";
|
9
10
|
import { LlamaCppApiConfiguration } from "./LlamaCppApiConfiguration.js";
|
10
11
|
import { failedLlamaCppCallResponseHandler } from "./LlamaCppError.js";
|
11
12
|
import { LlamaCppTokenizer } from "./LlamaCppTokenizer.js";
|
@@ -168,13 +169,13 @@ const llamaCppTextGenerationResponseSchema = z.object({
|
|
168
169
|
tokens_predicted: z.number(),
|
169
170
|
truncated: z.boolean(),
|
170
171
|
});
|
171
|
-
const llamaCppTextStreamingResponseSchema = z.discriminatedUnion("stop", [
|
172
|
+
const llamaCppTextStreamingResponseSchema = new ZodSchema(z.discriminatedUnion("stop", [
|
172
173
|
z.object({
|
173
174
|
content: z.string(),
|
174
175
|
stop: z.literal(false),
|
175
176
|
}),
|
176
177
|
llamaCppTextGenerationResponseSchema,
|
177
|
-
]);
|
178
|
+
]));
|
178
179
|
async function callLlamaCppTextGenerationAPI({ api = new LlamaCppApiConfiguration(), abortSignal, responseFormat, prompt, temperature, topK, topP, nPredict, nKeep, stop, tfsZ, typicalP, repeatPenalty, repeatLastN, penalizeNl, mirostat, mirostatTau, mirostatEta, seed, ignoreEos, logitBias, }) {
|
179
180
|
return postJsonToApi({
|
180
181
|
url: api.assembleUrl(`/completion`),
|
@@ -220,7 +221,10 @@ async function createLlamaCppFullDeltaIterableQueue(stream) {
|
|
220
221
|
try {
|
221
222
|
for await (const event of events) {
|
222
223
|
const data = event.data;
|
223
|
-
const eventData =
|
224
|
+
const eventData = parseJSON({
|
225
|
+
text: data,
|
226
|
+
schema: llamaCppTextStreamingResponseSchema,
|
227
|
+
});
|
224
228
|
content += eventData.content;
|
225
229
|
queue.push({
|
226
230
|
type: "delta",
|
@@ -4,6 +4,7 @@ exports.OllamaTextGenerationResponseFormat = exports.OllamaTextGenerationModel =
|
|
4
4
|
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
|
+
const ZodSchema_js_1 = require("../../core/schema/ZodSchema.cjs");
|
7
8
|
const AbstractModel_js_1 = require("../../model-function/AbstractModel.cjs");
|
8
9
|
const PromptFormatTextStreamingModel_js_1 = require("../../model-function/generate-text/PromptFormatTextStreamingModel.cjs");
|
9
10
|
const AsyncQueue_js_1 = require("../../util/AsyncQueue.cjs");
|
@@ -72,6 +73,8 @@ class OllamaTextGenerationModel extends AbstractModel_js_1.AbstractModel {
|
|
72
73
|
"system",
|
73
74
|
"template",
|
74
75
|
"context",
|
76
|
+
"format",
|
77
|
+
"raw",
|
75
78
|
];
|
76
79
|
return Object.fromEntries(Object.entries(this.settings).filter(([key]) => eventSettingProperties.includes(key)));
|
77
80
|
}
|
@@ -118,7 +121,7 @@ const ollamaTextGenerationResponseSchema = zod_1.z.object({
|
|
118
121
|
eval_duration: zod_1.z.number(),
|
119
122
|
context: zod_1.z.array(zod_1.z.number()),
|
120
123
|
});
|
121
|
-
const ollamaTextStreamingResponseSchema = zod_1.z.discriminatedUnion("done", [
|
124
|
+
const ollamaTextStreamingResponseSchema = new ZodSchema_js_1.ZodSchema(zod_1.z.discriminatedUnion("done", [
|
122
125
|
zod_1.z.object({
|
123
126
|
done: zod_1.z.literal(false),
|
124
127
|
model: zod_1.z.string(),
|
@@ -139,8 +142,8 @@ const ollamaTextStreamingResponseSchema = zod_1.z.discriminatedUnion("done", [
|
|
139
142
|
eval_duration: zod_1.z.number(),
|
140
143
|
context: zod_1.z.array(zod_1.z.number()),
|
141
144
|
}),
|
142
|
-
]);
|
143
|
-
async function callOllamaTextGenerationAPI({ api = new OllamaApiConfiguration_js_1.OllamaApiConfiguration(), abortSignal, responseFormat, prompt, model, contextWindowSize, maxCompletionTokens, mirostat, mirostat_eta, mirostat_tau, num_gpu, num_gqa, num_threads, repeat_last_n, repeat_penalty, seed, stopSequences, temperature, tfs_z, top_k, top_p, system, template, context, }) {
|
145
|
+
]));
|
146
|
+
async function callOllamaTextGenerationAPI({ api = new OllamaApiConfiguration_js_1.OllamaApiConfiguration(), abortSignal, responseFormat, prompt, model, format, contextWindowSize, maxCompletionTokens, mirostat, mirostat_eta, mirostat_tau, num_gpu, num_gqa, num_threads, repeat_last_n, repeat_penalty, seed, stopSequences, temperature, tfs_z, top_k, top_p, system, template, context, raw, }) {
|
144
147
|
return (0, postToApi_js_1.postJsonToApi)({
|
145
148
|
url: api.assembleUrl(`/api/generate`),
|
146
149
|
headers: api.headers,
|
@@ -148,6 +151,7 @@ async function callOllamaTextGenerationAPI({ api = new OllamaApiConfiguration_js
|
|
148
151
|
stream: responseFormat.stream,
|
149
152
|
model,
|
150
153
|
prompt,
|
154
|
+
format,
|
151
155
|
options: {
|
152
156
|
mirostat,
|
153
157
|
mirostat_eta,
|
@@ -169,6 +173,7 @@ async function callOllamaTextGenerationAPI({ api = new OllamaApiConfiguration_js
|
|
169
173
|
system,
|
170
174
|
template,
|
171
175
|
context,
|
176
|
+
raw,
|
172
177
|
},
|
173
178
|
failedResponseHandler: OllamaError_js_1.failedOllamaCallResponseHandler,
|
174
179
|
successfulResponseHandler: responseFormat.handler,
|
@@ -7,27 +7,104 @@ import { Delta } from "../../model-function/Delta.js";
|
|
7
7
|
import { PromptFormatTextStreamingModel } from "../../model-function/generate-text/PromptFormatTextStreamingModel.js";
|
8
8
|
import { TextGenerationModelSettings, TextStreamingModel } from "../../model-function/generate-text/TextGenerationModel.js";
|
9
9
|
import { TextGenerationPromptFormat } from "../../model-function/generate-text/TextGenerationPromptFormat.js";
|
10
|
+
/**
|
11
|
+
* @see https://github.com/jmorganca/ollama/blob/main/docs/api.md#generate-a-completion
|
12
|
+
*/
|
10
13
|
export interface OllamaTextGenerationModelSettings<CONTEXT_WINDOW_SIZE extends number | undefined> extends TextGenerationModelSettings {
|
11
14
|
api?: ApiConfiguration;
|
15
|
+
/**
|
16
|
+
* The name of the model to use. For example, 'mistral'.
|
17
|
+
*
|
18
|
+
* @see https://ollama.ai/library
|
19
|
+
*/
|
12
20
|
model: string;
|
21
|
+
/**
|
22
|
+
* The temperature of the model. Increasing the temperature will make the model
|
23
|
+
* answer more creatively. (Default: 0.8)
|
24
|
+
*/
|
13
25
|
temperature?: number;
|
14
26
|
/**
|
15
27
|
* Specify the context window size of the model that you have loaded in your
|
16
|
-
* Ollama server.
|
28
|
+
* Ollama server. (Default: 2048)
|
17
29
|
*/
|
18
30
|
contextWindowSize?: CONTEXT_WINDOW_SIZE;
|
31
|
+
/**
|
32
|
+
* Enable Mirostat sampling for controlling perplexity.
|
33
|
+
* (default: 0, 0 = disabled, 1 = Mirostat, 2 = Mirostat 2.0)
|
34
|
+
*/
|
19
35
|
mirostat?: number;
|
36
|
+
/**
|
37
|
+
* Influences how quickly the algorithm responds to feedback from the generated text.
|
38
|
+
* A lower learning rate will result in slower adjustments,
|
39
|
+
* while a higher learning rate will make the algorithm more responsive. (Default: 0.1)
|
40
|
+
*/
|
20
41
|
mirostat_eta?: number;
|
42
|
+
/**
|
43
|
+
* Controls the balance between coherence and diversity of the output.
|
44
|
+
* A lower value will result in more focused and coherent text. (Default: 5.0)
|
45
|
+
*/
|
21
46
|
mirostat_tau?: number;
|
47
|
+
/**
|
48
|
+
* The number of GQA groups in the transformer layer. Required for some models,
|
49
|
+
* for example it is 8 for llama2:70b
|
50
|
+
*/
|
22
51
|
num_gqa?: number;
|
52
|
+
/**
|
53
|
+
* The number of layers to send to the GPU(s). On macOS it defaults to 1 to
|
54
|
+
* enable metal support, 0 to disable.
|
55
|
+
*/
|
23
56
|
num_gpu?: number;
|
57
|
+
/**
|
58
|
+
* Sets the number of threads to use during computation. By default, Ollama will
|
59
|
+
* detect this for optimal performance. It is recommended to set this value to the
|
60
|
+
* number of physical CPU cores your system has (as opposed to the logical number of cores).
|
61
|
+
*/
|
24
62
|
num_threads?: number;
|
63
|
+
/**
|
64
|
+
* Sets how far back for the model to look back to prevent repetition.
|
65
|
+
* (Default: 64, 0 = disabled, -1 = num_ctx)
|
66
|
+
*/
|
25
67
|
repeat_last_n?: number;
|
68
|
+
/**
|
69
|
+
* Sets how strongly to penalize repetitions. A higher value (e.g., 1.5)
|
70
|
+
* will penalize repetitions more strongly, while a lower value (e.g., 0.9)
|
71
|
+
* will be more lenient. (Default: 1.1)
|
72
|
+
*/
|
26
73
|
repeat_penalty?: number;
|
74
|
+
/**
|
75
|
+
* Sets the random number seed to use for generation. Setting this to a
|
76
|
+
* specific number will make the model generate the same text for the same prompt.
|
77
|
+
* (Default: 0)
|
78
|
+
*/
|
27
79
|
seed?: number;
|
80
|
+
/**
|
81
|
+
* Tail free sampling is used to reduce the impact of less probable tokens
|
82
|
+
* from the output. A higher value (e.g., 2.0) will reduce the impact more,
|
83
|
+
* while a value of 1.0 disables this setting. (default: 1)
|
84
|
+
*/
|
28
85
|
tfs_z?: number;
|
86
|
+
/**
|
87
|
+
* Reduces the probability of generating nonsense. A higher value (e.g. 100)
|
88
|
+
* will give more diverse answers, while a lower value (e.g. 10) will be more
|
89
|
+
* conservative. (Default: 40)
|
90
|
+
*/
|
29
91
|
top_k?: number;
|
92
|
+
/**
|
93
|
+
* Works together with top-k. A higher value (e.g., 0.95) will lead to more
|
94
|
+
* diverse text, while a lower value (e.g., 0.5) will generate more focused
|
95
|
+
* and conservative text. (Default: 0.9)
|
96
|
+
*/
|
30
97
|
top_p?: number;
|
98
|
+
/**
|
99
|
+
* When set to true, no formatting will be applied to the prompt and no context
|
100
|
+
* will be returned.
|
101
|
+
*/
|
102
|
+
raw?: boolean;
|
103
|
+
/**
|
104
|
+
* The format to return a response in. Currently the only accepted value is 'json'.
|
105
|
+
* Leave undefined to return a string.
|
106
|
+
*/
|
107
|
+
format?: "json";
|
31
108
|
system?: string;
|
32
109
|
template?: string;
|
33
110
|
context?: number[];
|
@@ -1,6 +1,7 @@
|
|
1
1
|
import { z } from "zod";
|
2
2
|
import { callWithRetryAndThrottle } from "../../core/api/callWithRetryAndThrottle.js";
|
3
3
|
import { createJsonResponseHandler, postJsonToApi, } from "../../core/api/postToApi.js";
|
4
|
+
import { ZodSchema } from "../../core/schema/ZodSchema.js";
|
4
5
|
import { AbstractModel } from "../../model-function/AbstractModel.js";
|
5
6
|
import { PromptFormatTextStreamingModel } from "../../model-function/generate-text/PromptFormatTextStreamingModel.js";
|
6
7
|
import { AsyncQueue } from "../../util/AsyncQueue.js";
|
@@ -69,6 +70,8 @@ export class OllamaTextGenerationModel extends AbstractModel {
|
|
69
70
|
"system",
|
70
71
|
"template",
|
71
72
|
"context",
|
73
|
+
"format",
|
74
|
+
"raw",
|
72
75
|
];
|
73
76
|
return Object.fromEntries(Object.entries(this.settings).filter(([key]) => eventSettingProperties.includes(key)));
|
74
77
|
}
|
@@ -114,7 +117,7 @@ const ollamaTextGenerationResponseSchema = z.object({
|
|
114
117
|
eval_duration: z.number(),
|
115
118
|
context: z.array(z.number()),
|
116
119
|
});
|
117
|
-
const ollamaTextStreamingResponseSchema = z.discriminatedUnion("done", [
|
120
|
+
const ollamaTextStreamingResponseSchema = new ZodSchema(z.discriminatedUnion("done", [
|
118
121
|
z.object({
|
119
122
|
done: z.literal(false),
|
120
123
|
model: z.string(),
|
@@ -135,8 +138,8 @@ const ollamaTextStreamingResponseSchema = z.discriminatedUnion("done", [
|
|
135
138
|
eval_duration: z.number(),
|
136
139
|
context: z.array(z.number()),
|
137
140
|
}),
|
138
|
-
]);
|
139
|
-
async function callOllamaTextGenerationAPI({ api = new OllamaApiConfiguration(), abortSignal, responseFormat, prompt, model, contextWindowSize, maxCompletionTokens, mirostat, mirostat_eta, mirostat_tau, num_gpu, num_gqa, num_threads, repeat_last_n, repeat_penalty, seed, stopSequences, temperature, tfs_z, top_k, top_p, system, template, context, }) {
|
141
|
+
]));
|
142
|
+
async function callOllamaTextGenerationAPI({ api = new OllamaApiConfiguration(), abortSignal, responseFormat, prompt, model, format, contextWindowSize, maxCompletionTokens, mirostat, mirostat_eta, mirostat_tau, num_gpu, num_gqa, num_threads, repeat_last_n, repeat_penalty, seed, stopSequences, temperature, tfs_z, top_k, top_p, system, template, context, raw, }) {
|
140
143
|
return postJsonToApi({
|
141
144
|
url: api.assembleUrl(`/api/generate`),
|
142
145
|
headers: api.headers,
|
@@ -144,6 +147,7 @@ async function callOllamaTextGenerationAPI({ api = new OllamaApiConfiguration(),
|
|
144
147
|
stream: responseFormat.stream,
|
145
148
|
model,
|
146
149
|
prompt,
|
150
|
+
format,
|
147
151
|
options: {
|
148
152
|
mirostat,
|
149
153
|
mirostat_eta,
|
@@ -165,6 +169,7 @@ async function callOllamaTextGenerationAPI({ api = new OllamaApiConfiguration(),
|
|
165
169
|
system,
|
166
170
|
template,
|
167
171
|
context,
|
172
|
+
raw,
|
168
173
|
},
|
169
174
|
failedResponseHandler: failedOllamaCallResponseHandler,
|
170
175
|
successfulResponseHandler: responseFormat.handler,
|
@@ -9,11 +9,12 @@ const PromptFormatTextStreamingModel_js_1 = require("../../model-function/genera
|
|
9
9
|
const TextPromptFormat_js_1 = require("../../model-function/generate-text/prompt-format/TextPromptFormat.cjs");
|
10
10
|
const countTokens_js_1 = require("../../model-function/tokenize-text/countTokens.cjs");
|
11
11
|
const AsyncQueue_js_1 = require("../../util/AsyncQueue.cjs");
|
12
|
-
const parseJSON_js_1 = require("../../
|
12
|
+
const parseJSON_js_1 = require("../../core/schema/parseJSON.cjs");
|
13
13
|
const parseEventSourceStream_js_1 = require("../../util/streaming/parseEventSourceStream.cjs");
|
14
14
|
const OpenAIApiConfiguration_js_1 = require("./OpenAIApiConfiguration.cjs");
|
15
15
|
const OpenAIError_js_1 = require("./OpenAIError.cjs");
|
16
16
|
const TikTokenTokenizer_js_1 = require("./TikTokenTokenizer.cjs");
|
17
|
+
const ZodSchema_js_1 = require("../../core/schema/ZodSchema.cjs");
|
17
18
|
/**
|
18
19
|
* @see https://platform.openai.com/docs/models/
|
19
20
|
* @see https://openai.com/pricing
|
@@ -335,7 +336,7 @@ exports.OpenAITextResponseFormat = {
|
|
335
336
|
handler: async ({ response }) => createOpenAITextFullDeltaIterableQueue(response.body),
|
336
337
|
},
|
337
338
|
};
|
338
|
-
const textResponseStreamEventSchema = zod_1.z.object({
|
339
|
+
const textResponseStreamEventSchema = new ZodSchema_js_1.ZodSchema(zod_1.z.object({
|
339
340
|
choices: zod_1.z.array(zod_1.z.object({
|
340
341
|
text: zod_1.z.string(),
|
341
342
|
finish_reason: zod_1.z
|
@@ -349,7 +350,7 @@ const textResponseStreamEventSchema = zod_1.z.object({
|
|
349
350
|
model: zod_1.z.string(),
|
350
351
|
system_fingerprint: zod_1.z.string().optional(),
|
351
352
|
object: zod_1.z.literal("text_completion"),
|
352
|
-
});
|
353
|
+
}));
|
353
354
|
async function createOpenAITextFullDeltaIterableQueue(stream) {
|
354
355
|
const queue = new AsyncQueue_js_1.AsyncQueue();
|
355
356
|
const streamDelta = [];
|
@@ -363,7 +364,10 @@ async function createOpenAITextFullDeltaIterableQueue(stream) {
|
|
363
364
|
queue.close();
|
364
365
|
return;
|
365
366
|
}
|
366
|
-
const eventData = (0, parseJSON_js_1.
|
367
|
+
const eventData = (0, parseJSON_js_1.parseJSON)({
|
368
|
+
text: data,
|
369
|
+
schema: textResponseStreamEventSchema,
|
370
|
+
});
|
367
371
|
for (let i = 0; i < eventData.choices.length; i++) {
|
368
372
|
const eventChoice = eventData.choices[i];
|
369
373
|
const delta = eventChoice.text;
|
@@ -6,11 +6,12 @@ import { PromptFormatTextStreamingModel } from "../../model-function/generate-te
|
|
6
6
|
import { mapChatPromptToTextFormat, mapInstructionPromptToTextFormat, } from "../../model-function/generate-text/prompt-format/TextPromptFormat.js";
|
7
7
|
import { countTokens } from "../../model-function/tokenize-text/countTokens.js";
|
8
8
|
import { AsyncQueue } from "../../util/AsyncQueue.js";
|
9
|
-
import {
|
9
|
+
import { parseJSON } from "../../core/schema/parseJSON.js";
|
10
10
|
import { parseEventSourceStream } from "../../util/streaming/parseEventSourceStream.js";
|
11
11
|
import { OpenAIApiConfiguration } from "./OpenAIApiConfiguration.js";
|
12
12
|
import { failedOpenAICallResponseHandler } from "./OpenAIError.js";
|
13
13
|
import { TikTokenTokenizer } from "./TikTokenTokenizer.js";
|
14
|
+
import { ZodSchema } from "../../core/schema/ZodSchema.js";
|
14
15
|
/**
|
15
16
|
* @see https://platform.openai.com/docs/models/
|
16
17
|
* @see https://openai.com/pricing
|
@@ -328,7 +329,7 @@ export const OpenAITextResponseFormat = {
|
|
328
329
|
handler: async ({ response }) => createOpenAITextFullDeltaIterableQueue(response.body),
|
329
330
|
},
|
330
331
|
};
|
331
|
-
const textResponseStreamEventSchema = z.object({
|
332
|
+
const textResponseStreamEventSchema = new ZodSchema(z.object({
|
332
333
|
choices: z.array(z.object({
|
333
334
|
text: z.string(),
|
334
335
|
finish_reason: z
|
@@ -342,7 +343,7 @@ const textResponseStreamEventSchema = z.object({
|
|
342
343
|
model: z.string(),
|
343
344
|
system_fingerprint: z.string().optional(),
|
344
345
|
object: z.literal("text_completion"),
|
345
|
-
});
|
346
|
+
}));
|
346
347
|
async function createOpenAITextFullDeltaIterableQueue(stream) {
|
347
348
|
const queue = new AsyncQueue();
|
348
349
|
const streamDelta = [];
|
@@ -356,7 +357,10 @@ async function createOpenAITextFullDeltaIterableQueue(stream) {
|
|
356
357
|
queue.close();
|
357
358
|
return;
|
358
359
|
}
|
359
|
-
const eventData =
|
360
|
+
const eventData = parseJSON({
|
361
|
+
text: data,
|
362
|
+
schema: textResponseStreamEventSchema,
|
363
|
+
});
|
360
364
|
for (let i = 0; i < eventData.choices.length; i++) {
|
361
365
|
const eventChoice = eventData.choices[i];
|
362
366
|
const delta = eventChoice.text;
|
@@ -3,15 +3,16 @@ Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
3
|
exports.failedOpenAICallResponseHandler = exports.OpenAIError = exports.openAIErrorDataSchema = void 0;
|
4
4
|
const zod_1 = require("zod");
|
5
5
|
const ApiCallError_js_1 = require("../../core/api/ApiCallError.cjs");
|
6
|
-
const
|
7
|
-
|
6
|
+
const ZodSchema_js_1 = require("../../core/schema/ZodSchema.cjs");
|
7
|
+
const parseJSON_js_1 = require("../../core/schema/parseJSON.cjs");
|
8
|
+
exports.openAIErrorDataSchema = new ZodSchema_js_1.ZodSchema(zod_1.z.object({
|
8
9
|
error: zod_1.z.object({
|
9
10
|
message: zod_1.z.string(),
|
10
11
|
type: zod_1.z.string(),
|
11
12
|
param: zod_1.z.any().nullable(),
|
12
13
|
code: zod_1.z.string().nullable(),
|
13
14
|
}),
|
14
|
-
});
|
15
|
+
}));
|
15
16
|
class OpenAIError extends ApiCallError_js_1.ApiCallError {
|
16
17
|
constructor({ data, statusCode, url, requestBodyValues, message, }) {
|
17
18
|
super({
|
@@ -38,7 +39,10 @@ const failedOpenAICallResponseHandler = async ({ response, url, requestBodyValue
|
|
38
39
|
const responseBody = await response.text();
|
39
40
|
// resilient parsing in case the response is not JSON or does not match the schema:
|
40
41
|
try {
|
41
|
-
const parsedError = (0, parseJSON_js_1.
|
42
|
+
const parsedError = (0, parseJSON_js_1.parseJSON)({
|
43
|
+
text: responseBody,
|
44
|
+
schema: exports.openAIErrorDataSchema,
|
45
|
+
});
|
42
46
|
return new OpenAIError({
|
43
47
|
url,
|
44
48
|
requestBodyValues,
|
@@ -1,31 +1,7 @@
|
|
1
|
-
import { z } from "zod";
|
2
1
|
import { ApiCallError } from "../../core/api/ApiCallError.js";
|
3
2
|
import { ResponseHandler } from "../../core/api/postToApi.js";
|
4
|
-
|
5
|
-
|
6
|
-
message: z.ZodString;
|
7
|
-
type: z.ZodString;
|
8
|
-
param: z.ZodNullable<z.ZodAny>;
|
9
|
-
code: z.ZodNullable<z.ZodString>;
|
10
|
-
}, "strip", z.ZodTypeAny, {
|
11
|
-
message: string;
|
12
|
-
code: string | null;
|
13
|
-
type: string;
|
14
|
-
param?: any;
|
15
|
-
}, {
|
16
|
-
message: string;
|
17
|
-
code: string | null;
|
18
|
-
type: string;
|
19
|
-
param?: any;
|
20
|
-
}>;
|
21
|
-
}, "strip", z.ZodTypeAny, {
|
22
|
-
error: {
|
23
|
-
message: string;
|
24
|
-
code: string | null;
|
25
|
-
type: string;
|
26
|
-
param?: any;
|
27
|
-
};
|
28
|
-
}, {
|
3
|
+
import { ZodSchema } from "../../core/schema/ZodSchema.js";
|
4
|
+
export declare const openAIErrorDataSchema: ZodSchema<{
|
29
5
|
error: {
|
30
6
|
message: string;
|
31
7
|
code: string | null;
|
@@ -33,7 +9,7 @@ export declare const openAIErrorDataSchema: z.ZodObject<{
|
|
33
9
|
param?: any;
|
34
10
|
};
|
35
11
|
}>;
|
36
|
-
export type OpenAIErrorData =
|
12
|
+
export type OpenAIErrorData = (typeof openAIErrorDataSchema)["_type"];
|
37
13
|
export declare class OpenAIError extends ApiCallError {
|
38
14
|
readonly data?: OpenAIErrorData;
|
39
15
|
constructor({ data, statusCode, url, requestBodyValues, message, }: {
|
@@ -1,14 +1,15 @@
|
|
1
1
|
import { z } from "zod";
|
2
2
|
import { ApiCallError } from "../../core/api/ApiCallError.js";
|
3
|
-
import {
|
4
|
-
|
3
|
+
import { ZodSchema } from "../../core/schema/ZodSchema.js";
|
4
|
+
import { parseJSON } from "../../core/schema/parseJSON.js";
|
5
|
+
export const openAIErrorDataSchema = new ZodSchema(z.object({
|
5
6
|
error: z.object({
|
6
7
|
message: z.string(),
|
7
8
|
type: z.string(),
|
8
9
|
param: z.any().nullable(),
|
9
10
|
code: z.string().nullable(),
|
10
11
|
}),
|
11
|
-
});
|
12
|
+
}));
|
12
13
|
export class OpenAIError extends ApiCallError {
|
13
14
|
constructor({ data, statusCode, url, requestBodyValues, message, }) {
|
14
15
|
super({
|
@@ -34,7 +35,10 @@ export const failedOpenAICallResponseHandler = async ({ response, url, requestBo
|
|
34
35
|
const responseBody = await response.text();
|
35
36
|
// resilient parsing in case the response is not JSON or does not match the schema:
|
36
37
|
try {
|
37
|
-
const parsedError =
|
38
|
+
const parsedError = parseJSON({
|
39
|
+
text: responseBody,
|
40
|
+
schema: openAIErrorDataSchema,
|
41
|
+
});
|
38
42
|
return new OpenAIError({
|
39
43
|
url,
|
40
44
|
requestBodyValues,
|
@@ -2,7 +2,7 @@ import { z } from "zod";
|
|
2
2
|
import { FunctionOptions } from "../../../core/FunctionOptions.js";
|
3
3
|
import { ApiConfiguration } from "../../../core/api/ApiConfiguration.js";
|
4
4
|
import { ResponseHandler } from "../../../core/api/postToApi.js";
|
5
|
-
import { StructureDefinition } from "../../../core/
|
5
|
+
import { StructureDefinition } from "../../../core/schema/StructureDefinition.js";
|
6
6
|
import { AbstractModel } from "../../../model-function/AbstractModel.js";
|
7
7
|
import { Delta } from "../../../model-function/Delta.js";
|
8
8
|
import { StructureGenerationModel } from "../../../model-function/generate-structure/StructureGenerationModel.js";
|
@@ -4,8 +4,9 @@ exports.createOpenAIChatDeltaIterableQueue = void 0;
|
|
4
4
|
const zod_1 = require("zod");
|
5
5
|
const AsyncQueue_js_1 = require("../../../util/AsyncQueue.cjs");
|
6
6
|
const parseEventSourceStream_js_1 = require("../../../util/streaming/parseEventSourceStream.cjs");
|
7
|
-
const parseJSON_js_1 = require("../../../
|
8
|
-
const
|
7
|
+
const parseJSON_js_1 = require("../../../core/schema/parseJSON.cjs");
|
8
|
+
const ZodSchema_js_1 = require("../../../core/schema/ZodSchema.cjs");
|
9
|
+
const chatResponseStreamEventSchema = new ZodSchema_js_1.ZodSchema(zod_1.z.object({
|
9
10
|
id: zod_1.z.string(),
|
10
11
|
choices: zod_1.z.array(zod_1.z.object({
|
11
12
|
delta: zod_1.z.object({
|
@@ -34,7 +35,7 @@ const chatResponseStreamEventSchema = zod_1.z.object({
|
|
34
35
|
model: zod_1.z.string(),
|
35
36
|
system_fingerprint: zod_1.z.string().optional(),
|
36
37
|
object: zod_1.z.literal("chat.completion.chunk"),
|
37
|
-
});
|
38
|
+
}));
|
38
39
|
async function createOpenAIChatDeltaIterableQueue(stream, extractDeltaValue) {
|
39
40
|
const queue = new AsyncQueue_js_1.AsyncQueue();
|
40
41
|
const streamDelta = [];
|
@@ -48,7 +49,10 @@ async function createOpenAIChatDeltaIterableQueue(stream, extractDeltaValue) {
|
|
48
49
|
queue.close();
|
49
50
|
return;
|
50
51
|
}
|
51
|
-
const parseResult = (0, parseJSON_js_1.
|
52
|
+
const parseResult = (0, parseJSON_js_1.safeParseJSON)({
|
53
|
+
text: data,
|
54
|
+
schema: chatResponseStreamEventSchema,
|
55
|
+
});
|
52
56
|
if (!parseResult.success) {
|
53
57
|
queue.push({
|
54
58
|
type: "error",
|
@@ -1,8 +1,9 @@
|
|
1
1
|
import { z } from "zod";
|
2
2
|
import { AsyncQueue } from "../../../util/AsyncQueue.js";
|
3
3
|
import { parseEventSourceStream } from "../../../util/streaming/parseEventSourceStream.js";
|
4
|
-
import {
|
5
|
-
|
4
|
+
import { safeParseJSON } from "../../../core/schema/parseJSON.js";
|
5
|
+
import { ZodSchema } from "../../../core/schema/ZodSchema.js";
|
6
|
+
const chatResponseStreamEventSchema = new ZodSchema(z.object({
|
6
7
|
id: z.string(),
|
7
8
|
choices: z.array(z.object({
|
8
9
|
delta: z.object({
|
@@ -31,7 +32,7 @@ const chatResponseStreamEventSchema = z.object({
|
|
31
32
|
model: z.string(),
|
32
33
|
system_fingerprint: z.string().optional(),
|
33
34
|
object: z.literal("chat.completion.chunk"),
|
34
|
-
});
|
35
|
+
}));
|
35
36
|
export async function createOpenAIChatDeltaIterableQueue(stream, extractDeltaValue) {
|
36
37
|
const queue = new AsyncQueue();
|
37
38
|
const streamDelta = [];
|
@@ -45,7 +46,10 @@ export async function createOpenAIChatDeltaIterableQueue(stream, extractDeltaVal
|
|
45
46
|
queue.close();
|
46
47
|
return;
|
47
48
|
}
|
48
|
-
const parseResult =
|
49
|
+
const parseResult = safeParseJSON({
|
50
|
+
text: data,
|
51
|
+
schema: chatResponseStreamEventSchema,
|
52
|
+
});
|
49
53
|
if (!parseResult.success) {
|
50
54
|
queue.push({
|
51
55
|
type: "error",
|
@@ -3,10 +3,11 @@ Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
3
|
exports.failedStabilityCallResponseHandler = exports.StabilityError = exports.stabilityErrorDataSchema = void 0;
|
4
4
|
const zod_1 = require("zod");
|
5
5
|
const ApiCallError_js_1 = require("../../core/api/ApiCallError.cjs");
|
6
|
-
const
|
7
|
-
|
6
|
+
const ZodSchema_js_1 = require("../../core/schema/ZodSchema.cjs");
|
7
|
+
const parseJSON_js_1 = require("../../core/schema/parseJSON.cjs");
|
8
|
+
exports.stabilityErrorDataSchema = new ZodSchema_js_1.ZodSchema(zod_1.z.object({
|
8
9
|
message: zod_1.z.string(),
|
9
|
-
});
|
10
|
+
}));
|
10
11
|
class StabilityError extends ApiCallError_js_1.ApiCallError {
|
11
12
|
constructor({ data, statusCode, url, requestBodyValues, message = data.message, }) {
|
12
13
|
super({ message, statusCode, requestBodyValues, url });
|
@@ -22,7 +23,10 @@ class StabilityError extends ApiCallError_js_1.ApiCallError {
|
|
22
23
|
exports.StabilityError = StabilityError;
|
23
24
|
const failedStabilityCallResponseHandler = async ({ response, url, requestBodyValues }) => {
|
24
25
|
const responseBody = await response.text();
|
25
|
-
const parsedError = (0, parseJSON_js_1.
|
26
|
+
const parsedError = (0, parseJSON_js_1.parseJSON)({
|
27
|
+
text: responseBody,
|
28
|
+
schema: exports.stabilityErrorDataSchema,
|
29
|
+
});
|
26
30
|
return new StabilityError({
|
27
31
|
url,
|
28
32
|
requestBodyValues,
|
@@ -1,14 +1,10 @@
|
|
1
|
-
import { z } from "zod";
|
2
1
|
import { ApiCallError } from "../../core/api/ApiCallError.js";
|
3
2
|
import { ResponseHandler } from "../../core/api/postToApi.js";
|
4
|
-
|
5
|
-
|
6
|
-
}, "strip", z.ZodTypeAny, {
|
7
|
-
message: string;
|
8
|
-
}, {
|
3
|
+
import { ZodSchema } from "../../core/schema/ZodSchema.js";
|
4
|
+
export declare const stabilityErrorDataSchema: ZodSchema<{
|
9
5
|
message: string;
|
10
6
|
}>;
|
11
|
-
export type StabilityErrorData =
|
7
|
+
export type StabilityErrorData = (typeof stabilityErrorDataSchema)["_type"];
|
12
8
|
export declare class StabilityError extends ApiCallError {
|
13
9
|
readonly data: StabilityErrorData;
|
14
10
|
constructor({ data, statusCode, url, requestBodyValues, message, }: {
|
@@ -1,9 +1,10 @@
|
|
1
1
|
import { z } from "zod";
|
2
2
|
import { ApiCallError } from "../../core/api/ApiCallError.js";
|
3
|
-
import {
|
4
|
-
|
3
|
+
import { ZodSchema } from "../../core/schema/ZodSchema.js";
|
4
|
+
import { parseJSON } from "../../core/schema/parseJSON.js";
|
5
|
+
export const stabilityErrorDataSchema = new ZodSchema(z.object({
|
5
6
|
message: z.string(),
|
6
|
-
});
|
7
|
+
}));
|
7
8
|
export class StabilityError extends ApiCallError {
|
8
9
|
constructor({ data, statusCode, url, requestBodyValues, message = data.message, }) {
|
9
10
|
super({ message, statusCode, requestBodyValues, url });
|
@@ -18,7 +19,10 @@ export class StabilityError extends ApiCallError {
|
|
18
19
|
}
|
19
20
|
export const failedStabilityCallResponseHandler = async ({ response, url, requestBodyValues }) => {
|
20
21
|
const responseBody = await response.text();
|
21
|
-
const parsedError =
|
22
|
+
const parsedError = parseJSON({
|
23
|
+
text: responseBody,
|
24
|
+
schema: stabilityErrorDataSchema,
|
25
|
+
});
|
22
26
|
return new StabilityError({
|
23
27
|
url,
|
24
28
|
requestBodyValues,
|
package/package.json
CHANGED
package/tool/Tool.d.ts
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
import { FunctionOptions } from "../core/FunctionOptions.js";
|
2
|
-
import { JsonSchemaProducer } from "../core/
|
3
|
-
import { Schema } from "../core/
|
4
|
-
import { StructureDefinition } from "../core/
|
2
|
+
import { JsonSchemaProducer } from "../core/schema/JsonSchemaProducer.js";
|
3
|
+
import { Schema } from "../core/schema/Schema.js";
|
4
|
+
import { StructureDefinition } from "../core/schema/StructureDefinition.js";
|
5
5
|
/**
|
6
6
|
* A tool is a function with a name, description and defined inputs that can be used
|
7
7
|
* by agents and chatbots.
|
package/tool/WebSearchTool.cjs
CHANGED
@@ -2,7 +2,7 @@
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
3
3
|
exports.WebSearchTool = void 0;
|
4
4
|
const zod_1 = require("zod");
|
5
|
-
const ZodSchema_js_1 = require("../core/
|
5
|
+
const ZodSchema_js_1 = require("../core/schema/ZodSchema.cjs");
|
6
6
|
const Tool_js_1 = require("./Tool.cjs");
|
7
7
|
const OUTPUT_SCHEMA = new ZodSchema_js_1.ZodSchema(zod_1.z.object({
|
8
8
|
results: zod_1.z.array(zod_1.z.object({
|
package/tool/WebSearchTool.d.ts
CHANGED
@@ -1,5 +1,5 @@
|
|
1
1
|
import { FunctionOptions } from "../core/FunctionOptions.js";
|
2
|
-
import { ZodSchema } from "../core/
|
2
|
+
import { ZodSchema } from "../core/schema/ZodSchema.js";
|
3
3
|
import { Tool } from "./Tool.js";
|
4
4
|
declare const OUTPUT_SCHEMA: ZodSchema<{
|
5
5
|
results: {
|