@heripo/pdf-parser 0.2.0 → 0.2.2
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/dist/index.js +52 -11
- package/dist/index.js.map +1 -1
- package/package.json +5 -5
package/dist/index.js
CHANGED
|
@@ -144,6 +144,50 @@ function detectProvider(model) {
|
|
|
144
144
|
if (providerId.includes("lmstudio")) return "lmstudio";
|
|
145
145
|
return "unknown";
|
|
146
146
|
}
|
|
147
|
+
function withSystemPromptCache(model, systemPrompt, providerOptions) {
|
|
148
|
+
const unchanged = {
|
|
149
|
+
...systemPrompt === void 0 ? {} : { instructions: systemPrompt },
|
|
150
|
+
...providerOptions === void 0 ? {} : { providerOptions }
|
|
151
|
+
};
|
|
152
|
+
const provider = detectProvider(model);
|
|
153
|
+
if (provider === "anthropic")
|
|
154
|
+
return systemPrompt ? {
|
|
155
|
+
...unchanged,
|
|
156
|
+
instructions: cachedSystem(systemPrompt, {
|
|
157
|
+
anthropic: { cacheControl: { type: "ephemeral" } }
|
|
158
|
+
})
|
|
159
|
+
} : unchanged;
|
|
160
|
+
if (provider !== "openai" || !supportsOpenAICacheBreakpoints(modelIdOf(model)) || providerOptions?.openai?.promptCacheOptions !== void 0)
|
|
161
|
+
return unchanged;
|
|
162
|
+
return {
|
|
163
|
+
...systemPrompt ? {
|
|
164
|
+
instructions: cachedSystem(systemPrompt, {
|
|
165
|
+
openai: { promptCacheBreakpoint: { mode: "explicit" } }
|
|
166
|
+
})
|
|
167
|
+
} : unchanged,
|
|
168
|
+
providerOptions: {
|
|
169
|
+
...providerOptions,
|
|
170
|
+
openai: {
|
|
171
|
+
...providerOptions?.openai,
|
|
172
|
+
promptCacheOptions: { mode: "explicit" }
|
|
173
|
+
}
|
|
174
|
+
}
|
|
175
|
+
};
|
|
176
|
+
}
|
|
177
|
+
function cachedSystem(content, providerOptions) {
|
|
178
|
+
return { role: "system", content, providerOptions };
|
|
179
|
+
}
|
|
180
|
+
function modelIdOf(model) {
|
|
181
|
+
const modelId = model.modelId;
|
|
182
|
+
return typeof modelId === "string" ? modelId : "";
|
|
183
|
+
}
|
|
184
|
+
function supportsOpenAICacheBreakpoints(modelId) {
|
|
185
|
+
const version = /^(?:openai\/)?gpt-(\d+)(?:\.(\d+))?(?:-.+)?$/u.exec(modelId);
|
|
186
|
+
if (!version) return false;
|
|
187
|
+
const major = Number(version[1]);
|
|
188
|
+
const minor = Number(version[2] ?? 0);
|
|
189
|
+
return major > 5 || major === 5 && minor >= 6;
|
|
190
|
+
}
|
|
147
191
|
function cacheWrite1hTokens(steps) {
|
|
148
192
|
let total;
|
|
149
193
|
for (const step of steps ?? []) {
|
|
@@ -345,7 +389,8 @@ var LLMCaller = class {
|
|
|
345
389
|
* 2. If all fail and fallbackModel provided, try fallback up to maxRetries times
|
|
346
390
|
* 3. Throw error if all attempts exhausted
|
|
347
391
|
*
|
|
348
|
-
* Provider-aware strategy is automatically applied based on the model's provider field
|
|
392
|
+
* Provider-aware strategy is automatically applied based on the model's provider field, and the prompt cache
|
|
393
|
+
* breakpoint is placed only at the end of the system prompt (see `withSystemPromptCache`).
|
|
349
394
|
*
|
|
350
395
|
* @template TOutput - Output type from schema validation
|
|
351
396
|
* @param config - LLM call configuration
|
|
@@ -356,17 +401,12 @@ var LLMCaller = class {
|
|
|
356
401
|
return this.executeWithFallback(
|
|
357
402
|
config,
|
|
358
403
|
(model) => this.generateStructuredOutput(model, config.schema, {
|
|
359
|
-
|
|
360
|
-
|
|
361
|
-
|
|
362
|
-
providerOptions
|
|
363
|
-
|
|
364
|
-
}
|
|
365
|
-
} : config.systemPrompt,
|
|
404
|
+
...withSystemPromptCache(
|
|
405
|
+
model,
|
|
406
|
+
config.systemPrompt,
|
|
407
|
+
config.providerOptions
|
|
408
|
+
),
|
|
366
409
|
prompt: config.userPrompt,
|
|
367
|
-
...config.providerOptions && {
|
|
368
|
-
providerOptions: config.providerOptions
|
|
369
|
-
},
|
|
370
410
|
temperature: config.temperature,
|
|
371
411
|
maxRetries: config.maxRetries,
|
|
372
412
|
abortSignal: config.abortSignal
|
|
@@ -389,6 +429,7 @@ var LLMCaller = class {
|
|
|
389
429
|
config,
|
|
390
430
|
(model) => this.generateStructuredOutput(model, config.schema, {
|
|
391
431
|
messages: config.messages,
|
|
432
|
+
...withSystemPromptCache(model, void 0, void 0),
|
|
392
433
|
temperature: config.temperature,
|
|
393
434
|
maxRetries: config.maxRetries,
|
|
394
435
|
abortSignal: config.abortSignal
|