modelfusion 0.27.0 → 0.27.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/index.cjs +1 -0
- package/index.d.ts +1 -0
- package/index.js +1 -0
- package/model-function/ApiConfiguration.d.ts +14 -0
- package/model-function/BasicApiConfiguration.cjs +35 -0
- package/model-function/BasicApiConfiguration.d.ts +15 -0
- package/model-function/BasicApiConfiguration.js +31 -0
- package/model-function/index.cjs +2 -0
- package/model-function/index.d.ts +2 -0
- package/model-function/index.js +2 -0
- package/model-provider/automatic1111/Automatic1111ApiConfiguration.cjs +14 -0
- package/model-provider/automatic1111/Automatic1111ApiConfiguration.d.ts +10 -0
- package/model-provider/automatic1111/Automatic1111ApiConfiguration.js +10 -0
- package/model-provider/automatic1111/Automatic1111ImageGenerationModel.cjs +10 -7
- package/model-provider/automatic1111/Automatic1111ImageGenerationModel.d.ts +2 -5
- package/model-provider/automatic1111/Automatic1111ImageGenerationModel.js +10 -7
- package/model-provider/automatic1111/index.cjs +1 -0
- package/model-provider/automatic1111/index.d.ts +1 -0
- package/model-provider/automatic1111/index.js +1 -0
- package/model-provider/cohere/CohereApiConfiguration.cjs +29 -0
- package/model-provider/cohere/CohereApiConfiguration.d.ts +13 -0
- package/model-provider/cohere/CohereApiConfiguration.js +25 -0
- package/model-provider/cohere/CohereTextEmbeddingModel.cjs +11 -38
- package/model-provider/cohere/CohereTextEmbeddingModel.d.ts +2 -11
- package/model-provider/cohere/CohereTextEmbeddingModel.js +11 -38
- package/model-provider/cohere/CohereTextGenerationModel.cjs +7 -36
- package/model-provider/cohere/CohereTextGenerationModel.d.ts +2 -11
- package/model-provider/cohere/CohereTextGenerationModel.js +7 -36
- package/model-provider/cohere/CohereTokenizer.cjs +11 -33
- package/model-provider/cohere/CohereTokenizer.d.ts +4 -9
- package/model-provider/cohere/CohereTokenizer.js +11 -33
- package/model-provider/cohere/index.cjs +1 -0
- package/model-provider/cohere/index.d.ts +1 -0
- package/model-provider/cohere/index.js +1 -0
- package/model-provider/elevenlabs/ElevenLabsApiConfiguration.cjs +29 -0
- package/model-provider/elevenlabs/ElevenLabsApiConfiguration.d.ts +13 -0
- package/model-provider/elevenlabs/ElevenLabsApiConfiguration.js +25 -0
- package/model-provider/elevenlabs/ElevenLabsSpeechSynthesisModel.cjs +22 -32
- package/model-provider/elevenlabs/ElevenLabsSpeechSynthesisModel.d.ts +8 -8
- package/model-provider/elevenlabs/ElevenLabsSpeechSynthesisModel.js +22 -32
- package/model-provider/elevenlabs/index.cjs +1 -0
- package/model-provider/elevenlabs/index.d.ts +1 -0
- package/model-provider/elevenlabs/index.js +1 -0
- package/model-provider/huggingface/HuggingFaceApiConfiguration.cjs +29 -0
- package/model-provider/huggingface/HuggingFaceApiConfiguration.d.ts +13 -0
- package/model-provider/huggingface/HuggingFaceApiConfiguration.js +25 -0
- package/model-provider/huggingface/HuggingFaceError.cjs +3 -2
- package/model-provider/huggingface/HuggingFaceError.d.ts +3 -3
- package/model-provider/huggingface/HuggingFaceError.js +3 -2
- package/model-provider/huggingface/HuggingFaceTextEmbeddingModel.cjs +15 -21
- package/model-provider/huggingface/HuggingFaceTextEmbeddingModel.d.ts +3 -8
- package/model-provider/huggingface/HuggingFaceTextEmbeddingModel.js +15 -21
- package/model-provider/huggingface/HuggingFaceTextGenerationModel.cjs +7 -36
- package/model-provider/huggingface/HuggingFaceTextGenerationModel.d.ts +2 -7
- package/model-provider/huggingface/HuggingFaceTextGenerationModel.js +7 -36
- package/model-provider/huggingface/index.cjs +1 -0
- package/model-provider/huggingface/index.d.ts +1 -0
- package/model-provider/huggingface/index.js +1 -0
- package/model-provider/llamacpp/LlamaCppApiConfiguration.cjs +14 -0
- package/model-provider/llamacpp/LlamaCppApiConfiguration.d.ts +10 -0
- package/model-provider/llamacpp/LlamaCppApiConfiguration.js +10 -0
- package/model-provider/llamacpp/LlamaCppTextEmbeddingModel.cjs +11 -12
- package/model-provider/llamacpp/LlamaCppTextEmbeddingModel.d.ts +2 -9
- package/model-provider/llamacpp/LlamaCppTextEmbeddingModel.js +11 -12
- package/model-provider/llamacpp/LlamaCppTextGenerationModel.cjs +9 -10
- package/model-provider/llamacpp/LlamaCppTextGenerationModel.d.ts +2 -9
- package/model-provider/llamacpp/LlamaCppTextGenerationModel.js +9 -10
- package/model-provider/llamacpp/LlamaCppTokenizer.cjs +10 -8
- package/model-provider/llamacpp/LlamaCppTokenizer.d.ts +4 -10
- package/model-provider/llamacpp/LlamaCppTokenizer.js +10 -8
- package/model-provider/llamacpp/index.cjs +1 -0
- package/model-provider/llamacpp/index.d.ts +1 -0
- package/model-provider/llamacpp/index.js +1 -0
- package/model-provider/lmnt/LmntApiConfiguration.cjs +29 -0
- package/model-provider/lmnt/LmntApiConfiguration.d.ts +13 -0
- package/model-provider/lmnt/LmntApiConfiguration.js +25 -0
- package/model-provider/lmnt/LmntSpeechSynthesisModel.cjs +18 -34
- package/model-provider/lmnt/LmntSpeechSynthesisModel.d.ts +8 -8
- package/model-provider/lmnt/LmntSpeechSynthesisModel.js +18 -34
- package/model-provider/lmnt/index.cjs +1 -0
- package/model-provider/lmnt/index.d.ts +1 -0
- package/model-provider/lmnt/index.js +1 -0
- package/model-provider/openai/AzureOpenAIApiConfiguration.cjs +51 -0
- package/model-provider/openai/AzureOpenAIApiConfiguration.d.ts +26 -0
- package/model-provider/openai/AzureOpenAIApiConfiguration.js +47 -0
- package/model-provider/openai/OpenAIApiConfiguration.cjs +31 -0
- package/model-provider/openai/OpenAIApiConfiguration.d.ts +13 -0
- package/model-provider/openai/OpenAIApiConfiguration.js +27 -0
- package/model-provider/openai/OpenAIImageGenerationModel.cjs +16 -20
- package/model-provider/openai/OpenAIImageGenerationModel.d.ts +4 -4
- package/model-provider/openai/OpenAIImageGenerationModel.js +16 -20
- package/model-provider/openai/OpenAITextEmbeddingModel.cjs +16 -35
- package/model-provider/openai/OpenAITextEmbeddingModel.d.ts +2 -7
- package/model-provider/openai/OpenAITextEmbeddingModel.js +16 -35
- package/model-provider/openai/OpenAITextGenerationModel.cjs +6 -33
- package/model-provider/openai/OpenAITextGenerationModel.d.ts +4 -5
- package/model-provider/openai/OpenAITextGenerationModel.js +6 -33
- package/model-provider/openai/OpenAITranscriptionModel.cjs +16 -39
- package/model-provider/openai/OpenAITranscriptionModel.d.ts +4 -11
- package/model-provider/openai/OpenAITranscriptionModel.js +16 -39
- package/model-provider/openai/chat/OpenAIChatModel.cjs +9 -17
- package/model-provider/openai/chat/OpenAIChatModel.d.ts +4 -5
- package/model-provider/openai/chat/OpenAIChatModel.js +9 -17
- package/model-provider/openai/index.cjs +2 -1
- package/model-provider/openai/index.d.ts +2 -1
- package/model-provider/openai/index.js +2 -1
- package/model-provider/stability/StabilityApiConfiguration.cjs +31 -0
- package/model-provider/stability/StabilityApiConfiguration.d.ts +13 -0
- package/model-provider/stability/StabilityApiConfiguration.js +27 -0
- package/model-provider/stability/StabilityImageGenerationModel.cjs +12 -46
- package/model-provider/stability/StabilityImageGenerationModel.d.ts +2 -7
- package/model-provider/stability/StabilityImageGenerationModel.js +12 -46
- package/model-provider/stability/index.cjs +1 -0
- package/model-provider/stability/index.d.ts +1 -0
- package/model-provider/stability/index.js +1 -0
- package/observability/helicone/HeliconeOpenAIApiConfiguration.cjs +45 -0
- package/observability/helicone/HeliconeOpenAIApiConfiguration.d.ts +15 -0
- package/observability/helicone/HeliconeOpenAIApiConfiguration.js +41 -0
- package/observability/index.cjs +17 -0
- package/observability/index.d.ts +1 -0
- package/observability/index.js +1 -0
- package/package.json +1 -1
- package/util/api/loadApiKey.cjs +11 -0
- package/util/api/loadApiKey.d.ts +6 -0
- package/util/api/loadApiKey.js +7 -0
- package/model-provider/openai/OpenAIModelSettings.d.ts +0 -8
- /package/{model-provider/openai/OpenAIModelSettings.cjs → model-function/ApiConfiguration.cjs} +0 -0
- /package/{model-provider/openai/OpenAIModelSettings.js → model-function/ApiConfiguration.js} +0 -0
@@ -1,7 +1,13 @@
|
|
1
1
|
import { AbstractModel } from "../../model-function/AbstractModel.js";
|
2
2
|
import { callWithRetryAndThrottle } from "../../util/api/callWithRetryAndThrottle.js";
|
3
3
|
import { createAudioMpegResponseHandler, postJsonToApi, } from "../../util/api/postToApi.js";
|
4
|
+
import { ElevenLabsApiConfiguration } from "./ElevenLabsApiConfiguration.js";
|
4
5
|
import { failedElevenLabsCallResponseHandler } from "./ElevenLabsError.js";
|
6
|
+
/**
|
7
|
+
* Synthesize speech using the ElevenLabs Text to Speech API.
|
8
|
+
*
|
9
|
+
* @see https://api.elevenlabs.io/docs#/text-to-speech/Text_to_speech_v1_text_to_speech__voice_id__post
|
10
|
+
*/
|
5
11
|
export class ElevenLabsSpeechSynthesisModel extends AbstractModel {
|
6
12
|
constructor(settings) {
|
7
13
|
super({ settings });
|
@@ -11,19 +17,9 @@ export class ElevenLabsSpeechSynthesisModel extends AbstractModel {
|
|
11
17
|
writable: true,
|
12
18
|
value: "elevenlabs"
|
13
19
|
});
|
14
|
-
Object.defineProperty(this, "modelName", {
|
15
|
-
enumerable: true,
|
16
|
-
configurable: true,
|
17
|
-
writable: true,
|
18
|
-
value: null
|
19
|
-
});
|
20
20
|
}
|
21
|
-
get
|
22
|
-
|
23
|
-
if (apiKey == null) {
|
24
|
-
throw new Error("No ElevenLabs API key provided. Pass it in the constructor or set the ELEVENLABS_API_KEY environment variable.");
|
25
|
-
}
|
26
|
-
return apiKey;
|
21
|
+
get modelName() {
|
22
|
+
return this.settings.voice;
|
27
23
|
}
|
28
24
|
async callAPI(text, options) {
|
29
25
|
const run = options?.run;
|
@@ -32,23 +28,22 @@ export class ElevenLabsSpeechSynthesisModel extends AbstractModel {
|
|
32
28
|
...this.settings,
|
33
29
|
...settings,
|
34
30
|
};
|
31
|
+
const callSettings = {
|
32
|
+
api: combinedSettings.api,
|
33
|
+
abortSignal: run?.abortSignal,
|
34
|
+
text,
|
35
|
+
voiceId: combinedSettings.voice,
|
36
|
+
modelId: combinedSettings.model,
|
37
|
+
voiceSettings: combinedSettings.voiceSettings,
|
38
|
+
};
|
35
39
|
return callWithRetryAndThrottle({
|
36
|
-
retry:
|
37
|
-
throttle:
|
38
|
-
call: async () => callElevenLabsTextToSpeechAPI(
|
39
|
-
baseUrl: combinedSettings.baseUrl,
|
40
|
-
abortSignal: run?.abortSignal,
|
41
|
-
apiKey: this.apiKey,
|
42
|
-
text,
|
43
|
-
voiceId: combinedSettings.voice,
|
44
|
-
modelId: combinedSettings.model,
|
45
|
-
voiceSettings: combinedSettings.voiceSettings,
|
46
|
-
}),
|
40
|
+
retry: combinedSettings.api?.retry,
|
41
|
+
throttle: combinedSettings.api?.throttle,
|
42
|
+
call: async () => callElevenLabsTextToSpeechAPI(callSettings),
|
47
43
|
});
|
48
44
|
}
|
49
45
|
get settingsForEvent() {
|
50
46
|
return {
|
51
|
-
baseUrl: this.settings.baseUrl,
|
52
47
|
model: this.settings.model,
|
53
48
|
voice: this.settings.voice,
|
54
49
|
voiceSettings: this.settings.voiceSettings,
|
@@ -64,15 +59,10 @@ export class ElevenLabsSpeechSynthesisModel extends AbstractModel {
|
|
64
59
|
});
|
65
60
|
}
|
66
61
|
}
|
67
|
-
|
68
|
-
* @see https://api.elevenlabs.io/docs#/text-to-speech/Text_to_speech_v1_text_to_speech__voice_id__post
|
69
|
-
*/
|
70
|
-
async function callElevenLabsTextToSpeechAPI({ baseUrl = "https://api.elevenlabs.io/v1", abortSignal, apiKey, text, voiceId, modelId, voiceSettings, }) {
|
62
|
+
async function callElevenLabsTextToSpeechAPI({ api = new ElevenLabsApiConfiguration(), abortSignal, text, voiceId, modelId, voiceSettings, }) {
|
71
63
|
return postJsonToApi({
|
72
|
-
url:
|
73
|
-
headers:
|
74
|
-
"xi-api-key": apiKey,
|
75
|
-
},
|
64
|
+
url: api.assembleUrl(`/text-to-speech/${voiceId}`),
|
65
|
+
headers: api.headers,
|
76
66
|
body: {
|
77
67
|
text,
|
78
68
|
model_id: modelId,
|
@@ -14,4 +14,5 @@ var __exportStar = (this && this.__exportStar) || function(m, exports) {
|
|
14
14
|
for (var p in m) if (p !== "default" && !Object.prototype.hasOwnProperty.call(exports, p)) __createBinding(exports, m, p);
|
15
15
|
};
|
16
16
|
Object.defineProperty(exports, "__esModule", { value: true });
|
17
|
+
__exportStar(require("./ElevenLabsApiConfiguration.cjs"), exports);
|
17
18
|
__exportStar(require("./ElevenLabsSpeechSynthesisModel.cjs"), exports);
|
@@ -0,0 +1,29 @@
|
|
1
|
+
"use strict";
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
3
|
+
exports.HuggingFaceApiConfiguration = void 0;
|
4
|
+
const BasicApiConfiguration_js_1 = require("../../model-function/BasicApiConfiguration.cjs");
|
5
|
+
const loadApiKey_js_1 = require("../../util/api/loadApiKey.cjs");
|
6
|
+
class HuggingFaceApiConfiguration extends BasicApiConfiguration_js_1.BasicApiConfiguration {
|
7
|
+
constructor({ baseUrl = "https://api-inference.huggingface.co/models", apiKey, retry, throttle, } = {}) {
|
8
|
+
super({
|
9
|
+
baseUrl,
|
10
|
+
retry,
|
11
|
+
throttle,
|
12
|
+
});
|
13
|
+
Object.defineProperty(this, "apiKey", {
|
14
|
+
enumerable: true,
|
15
|
+
configurable: true,
|
16
|
+
writable: true,
|
17
|
+
value: void 0
|
18
|
+
});
|
19
|
+
this.apiKey = (0, loadApiKey_js_1.loadApiKey)({
|
20
|
+
apiKey,
|
21
|
+
environmentVariableName: "HUGGINGFACE_API_KEY",
|
22
|
+
description: "Hugging Face",
|
23
|
+
});
|
24
|
+
}
|
25
|
+
get headers() {
|
26
|
+
return { Authorization: `Bearer ${this.apiKey}` };
|
27
|
+
}
|
28
|
+
}
|
29
|
+
exports.HuggingFaceApiConfiguration = HuggingFaceApiConfiguration;
|
@@ -0,0 +1,13 @@
|
|
1
|
+
import { BasicApiConfiguration } from "../../model-function/BasicApiConfiguration.js";
|
2
|
+
import { RetryFunction } from "../../util/api/RetryFunction.js";
|
3
|
+
import { ThrottleFunction } from "../../util/api/ThrottleFunction.js";
|
4
|
+
export declare class HuggingFaceApiConfiguration extends BasicApiConfiguration {
|
5
|
+
readonly apiKey: string;
|
6
|
+
constructor({ baseUrl, apiKey, retry, throttle, }?: {
|
7
|
+
baseUrl?: string;
|
8
|
+
apiKey?: string;
|
9
|
+
retry?: RetryFunction;
|
10
|
+
throttle?: ThrottleFunction;
|
11
|
+
});
|
12
|
+
get headers(): Record<string, string>;
|
13
|
+
}
|
@@ -0,0 +1,25 @@
|
|
1
|
+
import { BasicApiConfiguration } from "../../model-function/BasicApiConfiguration.js";
|
2
|
+
import { loadApiKey } from "../../util/api/loadApiKey.js";
|
3
|
+
export class HuggingFaceApiConfiguration extends BasicApiConfiguration {
|
4
|
+
constructor({ baseUrl = "https://api-inference.huggingface.co/models", apiKey, retry, throttle, } = {}) {
|
5
|
+
super({
|
6
|
+
baseUrl,
|
7
|
+
retry,
|
8
|
+
throttle,
|
9
|
+
});
|
10
|
+
Object.defineProperty(this, "apiKey", {
|
11
|
+
enumerable: true,
|
12
|
+
configurable: true,
|
13
|
+
writable: true,
|
14
|
+
value: void 0
|
15
|
+
});
|
16
|
+
this.apiKey = loadApiKey({
|
17
|
+
apiKey,
|
18
|
+
environmentVariableName: "HUGGINGFACE_API_KEY",
|
19
|
+
description: "Hugging Face",
|
20
|
+
});
|
21
|
+
}
|
22
|
+
get headers() {
|
23
|
+
return { Authorization: `Bearer ${this.apiKey}` };
|
24
|
+
}
|
25
|
+
}
|
@@ -8,10 +8,10 @@ const secure_json_parse_1 = __importDefault(require("secure-json-parse"));
|
|
8
8
|
const zod_1 = require("zod");
|
9
9
|
const ApiCallError_js_1 = require("../../util/api/ApiCallError.cjs");
|
10
10
|
exports.huggingFaceErrorDataSchema = zod_1.z.object({
|
11
|
-
error: zod_1.z.string(),
|
11
|
+
error: zod_1.z.array(zod_1.z.string()),
|
12
12
|
});
|
13
13
|
class HuggingFaceError extends ApiCallError_js_1.ApiCallError {
|
14
|
-
constructor({ data, statusCode, url, requestBodyValues, message = data.error, }) {
|
14
|
+
constructor({ data, statusCode, url, requestBodyValues, message = data.error.join("\n\n"), }) {
|
15
15
|
super({ message, statusCode, requestBodyValues, url });
|
16
16
|
Object.defineProperty(this, "data", {
|
17
17
|
enumerable: true,
|
@@ -27,6 +27,7 @@ const failedHuggingFaceCallResponseHandler = async ({ response, url, requestBody
|
|
27
27
|
const responseBody = await response.text();
|
28
28
|
try {
|
29
29
|
const parsedError = exports.huggingFaceErrorDataSchema.parse(secure_json_parse_1.default.parse(responseBody));
|
30
|
+
console.error(requestBodyValues);
|
30
31
|
return new HuggingFaceError({
|
31
32
|
url,
|
32
33
|
requestBodyValues,
|
@@ -2,11 +2,11 @@ import { z } from "zod";
|
|
2
2
|
import { ApiCallError } from "../../util/api/ApiCallError.js";
|
3
3
|
import { ResponseHandler } from "../../util/api/postToApi.js";
|
4
4
|
export declare const huggingFaceErrorDataSchema: z.ZodObject<{
|
5
|
-
error: z.ZodString
|
5
|
+
error: z.ZodArray<z.ZodString, "many">;
|
6
6
|
}, "strip", z.ZodTypeAny, {
|
7
|
-
error: string;
|
7
|
+
error: string[];
|
8
8
|
}, {
|
9
|
-
error: string;
|
9
|
+
error: string[];
|
10
10
|
}>;
|
11
11
|
export type HuggingFaceErrorData = z.infer<typeof huggingFaceErrorDataSchema>;
|
12
12
|
export declare class HuggingFaceError extends ApiCallError {
|
@@ -2,10 +2,10 @@ import SecureJSON from "secure-json-parse";
|
|
2
2
|
import { z } from "zod";
|
3
3
|
import { ApiCallError } from "../../util/api/ApiCallError.js";
|
4
4
|
export const huggingFaceErrorDataSchema = z.object({
|
5
|
-
error: z.string(),
|
5
|
+
error: z.array(z.string()),
|
6
6
|
});
|
7
7
|
export class HuggingFaceError extends ApiCallError {
|
8
|
-
constructor({ data, statusCode, url, requestBodyValues, message = data.error, }) {
|
8
|
+
constructor({ data, statusCode, url, requestBodyValues, message = data.error.join("\n\n"), }) {
|
9
9
|
super({ message, statusCode, requestBodyValues, url });
|
10
10
|
Object.defineProperty(this, "data", {
|
11
11
|
enumerable: true,
|
@@ -20,6 +20,7 @@ export const failedHuggingFaceCallResponseHandler = async ({ response, url, requ
|
|
20
20
|
const responseBody = await response.text();
|
21
21
|
try {
|
22
22
|
const parsedError = huggingFaceErrorDataSchema.parse(SecureJSON.parse(responseBody));
|
23
|
+
console.error(requestBodyValues);
|
23
24
|
return new HuggingFaceError({
|
24
25
|
url,
|
25
26
|
requestBodyValues,
|
@@ -9,8 +9,9 @@ const AbstractModel_js_1 = require("../../model-function/AbstractModel.cjs");
|
|
9
9
|
const callWithRetryAndThrottle_js_1 = require("../../util/api/callWithRetryAndThrottle.cjs");
|
10
10
|
const postToApi_js_1 = require("../../util/api/postToApi.cjs");
|
11
11
|
const HuggingFaceError_js_1 = require("./HuggingFaceError.cjs");
|
12
|
+
const HuggingFaceApiConfiguration_js_1 = require("./HuggingFaceApiConfiguration.cjs");
|
12
13
|
/**
|
13
|
-
* Create a text
|
14
|
+
* Create a text embedding model that calls a Hugging Face Inference API Feature Extraction Task.
|
14
15
|
*
|
15
16
|
* @see https://huggingface.co/docs/api-inference/detailed_parameters#feature-extraction-task
|
16
17
|
*
|
@@ -75,38 +76,33 @@ class HuggingFaceTextEmbeddingModel extends AbstractModel_js_1.AbstractModel {
|
|
75
76
|
get modelName() {
|
76
77
|
return this.settings.model;
|
77
78
|
}
|
78
|
-
get apiKey() {
|
79
|
-
const apiKey = this.settings.apiKey ?? process.env.HUGGINGFACE_API_KEY;
|
80
|
-
if (apiKey == null) {
|
81
|
-
throw new Error("No Hugging Face API key provided. Pass it in the constructor or set the HUGGINGFACE_API_KEY environment variable.");
|
82
|
-
}
|
83
|
-
return apiKey;
|
84
|
-
}
|
85
79
|
async callAPI(texts, options) {
|
86
80
|
if (texts.length > this.maxTextsPerCall) {
|
87
81
|
throw new Error(`The HuggingFace feature extraction API is configured to only support ${this.maxTextsPerCall} texts per API call.`);
|
88
82
|
}
|
89
83
|
const run = options?.run;
|
90
84
|
const settings = options?.settings;
|
91
|
-
const
|
92
|
-
|
85
|
+
const combinedSettings = {
|
86
|
+
...this.settings,
|
87
|
+
...settings,
|
88
|
+
};
|
89
|
+
const callSettings = {
|
93
90
|
options: {
|
94
91
|
useCache: true,
|
95
92
|
waitForModel: true,
|
96
93
|
},
|
97
|
-
|
94
|
+
...combinedSettings,
|
98
95
|
abortSignal: run?.abortSignal,
|
99
96
|
inputs: texts,
|
100
|
-
}
|
97
|
+
};
|
101
98
|
return (0, callWithRetryAndThrottle_js_1.callWithRetryAndThrottle)({
|
102
|
-
retry:
|
103
|
-
throttle:
|
99
|
+
retry: callSettings.api?.retry,
|
100
|
+
throttle: callSettings.api?.throttle,
|
104
101
|
call: async () => callHuggingFaceTextGenerationAPI(callSettings),
|
105
102
|
});
|
106
103
|
}
|
107
104
|
get settingsForEvent() {
|
108
105
|
return {
|
109
|
-
baseUrl: this.settings.baseUrl,
|
110
106
|
embeddingDimensions: this.settings.embeddingDimensions,
|
111
107
|
options: this.settings.options,
|
112
108
|
};
|
@@ -123,12 +119,10 @@ class HuggingFaceTextEmbeddingModel extends AbstractModel_js_1.AbstractModel {
|
|
123
119
|
}
|
124
120
|
exports.HuggingFaceTextEmbeddingModel = HuggingFaceTextEmbeddingModel;
|
125
121
|
const huggingFaceTextEmbeddingResponseSchema = zod_1.default.array(zod_1.default.array(zod_1.default.number()));
|
126
|
-
async function callHuggingFaceTextGenerationAPI({
|
122
|
+
async function callHuggingFaceTextGenerationAPI({ api = new HuggingFaceApiConfiguration_js_1.HuggingFaceApiConfiguration(), abortSignal, model, inputs, options, }) {
|
127
123
|
return (0, postToApi_js_1.postJsonToApi)({
|
128
|
-
url:
|
129
|
-
headers:
|
130
|
-
Authorization: `Bearer ${apiKey}`,
|
131
|
-
},
|
124
|
+
url: api.assembleUrl(`/${model}`),
|
125
|
+
headers: api.headers,
|
132
126
|
body: {
|
133
127
|
inputs,
|
134
128
|
options: options
|
@@ -136,7 +130,7 @@ async function callHuggingFaceTextGenerationAPI({ baseUrl = "https://api-inferen
|
|
136
130
|
use_cache: options?.useCache,
|
137
131
|
wait_for_model: options?.waitForModel,
|
138
132
|
}
|
139
|
-
:
|
133
|
+
: {},
|
140
134
|
},
|
141
135
|
failedResponseHandler: HuggingFaceError_js_1.failedHuggingFaceCallResponseHandler,
|
142
136
|
successfulResponseHandler: (0, postToApi_js_1.createJsonResponseHandler)(huggingFaceTextEmbeddingResponseSchema),
|
@@ -1,24 +1,20 @@
|
|
1
1
|
import z from "zod";
|
2
2
|
import { AbstractModel } from "../../model-function/AbstractModel.js";
|
3
|
+
import { ApiConfiguration } from "../../model-function/ApiConfiguration.js";
|
3
4
|
import { ModelFunctionOptions } from "../../model-function/ModelFunctionOptions.js";
|
4
5
|
import { TextEmbeddingModel, TextEmbeddingModelSettings } from "../../model-function/embed-text/TextEmbeddingModel.js";
|
5
|
-
import { RetryFunction } from "../../util/api/RetryFunction.js";
|
6
|
-
import { ThrottleFunction } from "../../util/api/ThrottleFunction.js";
|
7
6
|
export interface HuggingFaceTextEmbeddingModelSettings extends TextEmbeddingModelSettings {
|
7
|
+
api?: ApiConfiguration;
|
8
8
|
model: string;
|
9
|
-
baseUrl?: string;
|
10
|
-
apiKey?: string;
|
11
9
|
maxTextsPerCall?: number;
|
12
10
|
embeddingDimensions?: number;
|
13
|
-
retry?: RetryFunction;
|
14
|
-
throttle?: ThrottleFunction;
|
15
11
|
options?: {
|
16
12
|
useCache?: boolean;
|
17
13
|
waitForModel?: boolean;
|
18
14
|
};
|
19
15
|
}
|
20
16
|
/**
|
21
|
-
* Create a text
|
17
|
+
* Create a text embedding model that calls a Hugging Face Inference API Feature Extraction Task.
|
22
18
|
*
|
23
19
|
* @see https://huggingface.co/docs/api-inference/detailed_parameters#feature-extraction-task
|
24
20
|
*
|
@@ -45,7 +41,6 @@ export declare class HuggingFaceTextEmbeddingModel extends AbstractModel<Hugging
|
|
45
41
|
readonly contextWindowSize: undefined;
|
46
42
|
readonly embeddingDimensions: number | undefined;
|
47
43
|
readonly tokenizer: undefined;
|
48
|
-
private get apiKey();
|
49
44
|
callAPI(texts: Array<string>, options?: ModelFunctionOptions<HuggingFaceTextEmbeddingModelSettings>): Promise<HuggingFaceTextEmbeddingResponse>;
|
50
45
|
get settingsForEvent(): Partial<HuggingFaceTextEmbeddingModelSettings>;
|
51
46
|
readonly countPromptTokens: undefined;
|
@@ -3,8 +3,9 @@ import { AbstractModel } from "../../model-function/AbstractModel.js";
|
|
3
3
|
import { callWithRetryAndThrottle } from "../../util/api/callWithRetryAndThrottle.js";
|
4
4
|
import { createJsonResponseHandler, postJsonToApi, } from "../../util/api/postToApi.js";
|
5
5
|
import { failedHuggingFaceCallResponseHandler } from "./HuggingFaceError.js";
|
6
|
+
import { HuggingFaceApiConfiguration } from "./HuggingFaceApiConfiguration.js";
|
6
7
|
/**
|
7
|
-
* Create a text
|
8
|
+
* Create a text embedding model that calls a Hugging Face Inference API Feature Extraction Task.
|
8
9
|
*
|
9
10
|
* @see https://huggingface.co/docs/api-inference/detailed_parameters#feature-extraction-task
|
10
11
|
*
|
@@ -69,38 +70,33 @@ export class HuggingFaceTextEmbeddingModel extends AbstractModel {
|
|
69
70
|
get modelName() {
|
70
71
|
return this.settings.model;
|
71
72
|
}
|
72
|
-
get apiKey() {
|
73
|
-
const apiKey = this.settings.apiKey ?? process.env.HUGGINGFACE_API_KEY;
|
74
|
-
if (apiKey == null) {
|
75
|
-
throw new Error("No Hugging Face API key provided. Pass it in the constructor or set the HUGGINGFACE_API_KEY environment variable.");
|
76
|
-
}
|
77
|
-
return apiKey;
|
78
|
-
}
|
79
73
|
async callAPI(texts, options) {
|
80
74
|
if (texts.length > this.maxTextsPerCall) {
|
81
75
|
throw new Error(`The HuggingFace feature extraction API is configured to only support ${this.maxTextsPerCall} texts per API call.`);
|
82
76
|
}
|
83
77
|
const run = options?.run;
|
84
78
|
const settings = options?.settings;
|
85
|
-
const
|
86
|
-
|
79
|
+
const combinedSettings = {
|
80
|
+
...this.settings,
|
81
|
+
...settings,
|
82
|
+
};
|
83
|
+
const callSettings = {
|
87
84
|
options: {
|
88
85
|
useCache: true,
|
89
86
|
waitForModel: true,
|
90
87
|
},
|
91
|
-
|
88
|
+
...combinedSettings,
|
92
89
|
abortSignal: run?.abortSignal,
|
93
90
|
inputs: texts,
|
94
|
-
}
|
91
|
+
};
|
95
92
|
return callWithRetryAndThrottle({
|
96
|
-
retry:
|
97
|
-
throttle:
|
93
|
+
retry: callSettings.api?.retry,
|
94
|
+
throttle: callSettings.api?.throttle,
|
98
95
|
call: async () => callHuggingFaceTextGenerationAPI(callSettings),
|
99
96
|
});
|
100
97
|
}
|
101
98
|
get settingsForEvent() {
|
102
99
|
return {
|
103
|
-
baseUrl: this.settings.baseUrl,
|
104
100
|
embeddingDimensions: this.settings.embeddingDimensions,
|
105
101
|
options: this.settings.options,
|
106
102
|
};
|
@@ -116,12 +112,10 @@ export class HuggingFaceTextEmbeddingModel extends AbstractModel {
|
|
116
112
|
}
|
117
113
|
}
|
118
114
|
const huggingFaceTextEmbeddingResponseSchema = z.array(z.array(z.number()));
|
119
|
-
async function callHuggingFaceTextGenerationAPI({
|
115
|
+
async function callHuggingFaceTextGenerationAPI({ api = new HuggingFaceApiConfiguration(), abortSignal, model, inputs, options, }) {
|
120
116
|
return postJsonToApi({
|
121
|
-
url:
|
122
|
-
headers:
|
123
|
-
Authorization: `Bearer ${apiKey}`,
|
124
|
-
},
|
117
|
+
url: api.assembleUrl(`/${model}`),
|
118
|
+
headers: api.headers,
|
125
119
|
body: {
|
126
120
|
inputs,
|
127
121
|
options: options
|
@@ -129,7 +123,7 @@ async function callHuggingFaceTextGenerationAPI({ baseUrl = "https://api-inferen
|
|
129
123
|
use_cache: options?.useCache,
|
130
124
|
wait_for_model: options?.waitForModel,
|
131
125
|
}
|
132
|
-
:
|
126
|
+
: {},
|
133
127
|
},
|
134
128
|
failedResponseHandler: failedHuggingFaceCallResponseHandler,
|
135
129
|
successfulResponseHandler: createJsonResponseHandler(huggingFaceTextEmbeddingResponseSchema),
|
@@ -6,10 +6,11 @@ Object.defineProperty(exports, "__esModule", { value: true });
|
|
6
6
|
exports.HuggingFaceTextGenerationModel = void 0;
|
7
7
|
const zod_1 = __importDefault(require("zod"));
|
8
8
|
const AbstractModel_js_1 = require("../../model-function/AbstractModel.cjs");
|
9
|
+
const PromptFormatTextGenerationModel_js_1 = require("../../prompt/PromptFormatTextGenerationModel.cjs");
|
9
10
|
const callWithRetryAndThrottle_js_1 = require("../../util/api/callWithRetryAndThrottle.cjs");
|
10
11
|
const postToApi_js_1 = require("../../util/api/postToApi.cjs");
|
12
|
+
const HuggingFaceApiConfiguration_js_1 = require("./HuggingFaceApiConfiguration.cjs");
|
11
13
|
const HuggingFaceError_js_1 = require("./HuggingFaceError.cjs");
|
12
|
-
const PromptFormatTextGenerationModel_js_1 = require("../../prompt/PromptFormatTextGenerationModel.cjs");
|
13
14
|
/**
|
14
15
|
* Create a text generation model that calls a Hugging Face Inference API Text Generation Task.
|
15
16
|
*
|
@@ -71,13 +72,6 @@ class HuggingFaceTextGenerationModel extends AbstractModel_js_1.AbstractModel {
|
|
71
72
|
get modelName() {
|
72
73
|
return this.settings.model;
|
73
74
|
}
|
74
|
-
get apiKey() {
|
75
|
-
const apiKey = this.settings.apiKey ?? process.env.HUGGINGFACE_API_KEY;
|
76
|
-
if (apiKey == null) {
|
77
|
-
throw new Error("No Hugging Face API key provided. Pass it in the constructor or set the HUGGINGFACE_API_KEY environment variable.");
|
78
|
-
}
|
79
|
-
return apiKey;
|
80
|
-
}
|
81
75
|
async callAPI(prompt, options) {
|
82
76
|
const run = options?.run;
|
83
77
|
const settings = options?.settings;
|
@@ -86,7 +80,6 @@ class HuggingFaceTextGenerationModel extends AbstractModel_js_1.AbstractModel {
|
|
86
80
|
...settings,
|
87
81
|
};
|
88
82
|
const callSettings = {
|
89
|
-
apiKey: this.apiKey,
|
90
83
|
options: {
|
91
84
|
useCache: true,
|
92
85
|
waitForModel: true,
|
@@ -97,8 +90,8 @@ class HuggingFaceTextGenerationModel extends AbstractModel_js_1.AbstractModel {
|
|
97
90
|
inputs: prompt,
|
98
91
|
};
|
99
92
|
return (0, callWithRetryAndThrottle_js_1.callWithRetryAndThrottle)({
|
100
|
-
retry:
|
101
|
-
throttle:
|
93
|
+
retry: callSettings.api?.retry,
|
94
|
+
throttle: callSettings.api?.throttle,
|
102
95
|
call: async () => callHuggingFaceTextGenerationAPI(callSettings),
|
103
96
|
});
|
104
97
|
}
|
@@ -106,7 +99,6 @@ class HuggingFaceTextGenerationModel extends AbstractModel_js_1.AbstractModel {
|
|
106
99
|
const eventSettingProperties = [
|
107
100
|
"stopSequences",
|
108
101
|
"maxCompletionTokens",
|
109
|
-
"baseUrl",
|
110
102
|
"topK",
|
111
103
|
"topP",
|
112
104
|
"temperature",
|
@@ -138,31 +130,10 @@ exports.HuggingFaceTextGenerationModel = HuggingFaceTextGenerationModel;
|
|
138
130
|
const huggingFaceTextGenerationResponseSchema = zod_1.default.array(zod_1.default.object({
|
139
131
|
generated_text: zod_1.default.string(),
|
140
132
|
}));
|
141
|
-
|
142
|
-
* Call a Hugging Face Inference API Text Generation Task to generate a text completion for the given prompt.
|
143
|
-
*
|
144
|
-
* @see https://huggingface.co/docs/api-inference/detailed_parameters#text-generation-task
|
145
|
-
*
|
146
|
-
* @example
|
147
|
-
* const response = await callHuggingFaceTextGenerationAPI({
|
148
|
-
* apiKey: HUGGINGFACE_API_KEY,
|
149
|
-
* model: "tiiuae/falcon-7b",
|
150
|
-
* inputs: "Write a short story about a robot learning to love:\n\n",
|
151
|
-
* temperature: 700,
|
152
|
-
* maxNewTokens: 500,
|
153
|
-
* options: {
|
154
|
-
* waitForModel: true,
|
155
|
-
* },
|
156
|
-
* });
|
157
|
-
*
|
158
|
-
* console.log(response[0].generated_text);
|
159
|
-
*/
|
160
|
-
async function callHuggingFaceTextGenerationAPI({ baseUrl = "https://api-inference.huggingface.co/models", abortSignal, apiKey, model, inputs, topK, topP, temperature, repetitionPenalty, maxNewTokens, maxTime, numReturnSequences, doSample, options, }) {
|
133
|
+
async function callHuggingFaceTextGenerationAPI({ api = new HuggingFaceApiConfiguration_js_1.HuggingFaceApiConfiguration(), abortSignal, model, inputs, topK, topP, temperature, repetitionPenalty, maxNewTokens, maxTime, numReturnSequences, doSample, options, }) {
|
161
134
|
return (0, postToApi_js_1.postJsonToApi)({
|
162
|
-
url:
|
163
|
-
headers:
|
164
|
-
Authorization: `Bearer ${apiKey}`,
|
165
|
-
},
|
135
|
+
url: api.assembleUrl(`/${model}`),
|
136
|
+
headers: api.headers,
|
166
137
|
body: {
|
167
138
|
inputs,
|
168
139
|
top_k: topK,
|
@@ -1,17 +1,13 @@
|
|
1
1
|
import z from "zod";
|
2
2
|
import { AbstractModel } from "../../model-function/AbstractModel.js";
|
3
|
+
import { ApiConfiguration } from "../../model-function/ApiConfiguration.js";
|
3
4
|
import { ModelFunctionOptions } from "../../model-function/ModelFunctionOptions.js";
|
4
5
|
import { TextGenerationModel, TextGenerationModelSettings } from "../../model-function/generate-text/TextGenerationModel.js";
|
5
|
-
import { RetryFunction } from "../../util/api/RetryFunction.js";
|
6
|
-
import { ThrottleFunction } from "../../util/api/ThrottleFunction.js";
|
7
6
|
import { PromptFormat } from "../../prompt/PromptFormat.js";
|
8
7
|
import { PromptFormatTextGenerationModel } from "../../prompt/PromptFormatTextGenerationModel.js";
|
9
8
|
export interface HuggingFaceTextGenerationModelSettings extends TextGenerationModelSettings {
|
9
|
+
api?: ApiConfiguration;
|
10
10
|
model: string;
|
11
|
-
baseUrl?: string;
|
12
|
-
apiKey?: string;
|
13
|
-
retry?: RetryFunction;
|
14
|
-
throttle?: ThrottleFunction;
|
15
11
|
topK?: number;
|
16
12
|
topP?: number;
|
17
13
|
temperature?: number;
|
@@ -48,7 +44,6 @@ export declare class HuggingFaceTextGenerationModel extends AbstractModel<Huggin
|
|
48
44
|
get modelName(): string;
|
49
45
|
readonly contextWindowSize: undefined;
|
50
46
|
readonly tokenizer: undefined;
|
51
|
-
private get apiKey();
|
52
47
|
callAPI(prompt: string, options?: ModelFunctionOptions<HuggingFaceTextGenerationModelSettings>): Promise<HuggingFaceTextGenerationResponse>;
|
53
48
|
get settingsForEvent(): Partial<HuggingFaceTextGenerationModelSettings>;
|
54
49
|
readonly countPromptTokens: undefined;
|
@@ -1,9 +1,10 @@
|
|
1
1
|
import z from "zod";
|
2
2
|
import { AbstractModel } from "../../model-function/AbstractModel.js";
|
3
|
+
import { PromptFormatTextGenerationModel } from "../../prompt/PromptFormatTextGenerationModel.js";
|
3
4
|
import { callWithRetryAndThrottle } from "../../util/api/callWithRetryAndThrottle.js";
|
4
5
|
import { createJsonResponseHandler, postJsonToApi, } from "../../util/api/postToApi.js";
|
6
|
+
import { HuggingFaceApiConfiguration } from "./HuggingFaceApiConfiguration.js";
|
5
7
|
import { failedHuggingFaceCallResponseHandler } from "./HuggingFaceError.js";
|
6
|
-
import { PromptFormatTextGenerationModel } from "../../prompt/PromptFormatTextGenerationModel.js";
|
7
8
|
/**
|
8
9
|
* Create a text generation model that calls a Hugging Face Inference API Text Generation Task.
|
9
10
|
*
|
@@ -65,13 +66,6 @@ export class HuggingFaceTextGenerationModel extends AbstractModel {
|
|
65
66
|
get modelName() {
|
66
67
|
return this.settings.model;
|
67
68
|
}
|
68
|
-
get apiKey() {
|
69
|
-
const apiKey = this.settings.apiKey ?? process.env.HUGGINGFACE_API_KEY;
|
70
|
-
if (apiKey == null) {
|
71
|
-
throw new Error("No Hugging Face API key provided. Pass it in the constructor or set the HUGGINGFACE_API_KEY environment variable.");
|
72
|
-
}
|
73
|
-
return apiKey;
|
74
|
-
}
|
75
69
|
async callAPI(prompt, options) {
|
76
70
|
const run = options?.run;
|
77
71
|
const settings = options?.settings;
|
@@ -80,7 +74,6 @@ export class HuggingFaceTextGenerationModel extends AbstractModel {
|
|
80
74
|
...settings,
|
81
75
|
};
|
82
76
|
const callSettings = {
|
83
|
-
apiKey: this.apiKey,
|
84
77
|
options: {
|
85
78
|
useCache: true,
|
86
79
|
waitForModel: true,
|
@@ -91,8 +84,8 @@ export class HuggingFaceTextGenerationModel extends AbstractModel {
|
|
91
84
|
inputs: prompt,
|
92
85
|
};
|
93
86
|
return callWithRetryAndThrottle({
|
94
|
-
retry:
|
95
|
-
throttle:
|
87
|
+
retry: callSettings.api?.retry,
|
88
|
+
throttle: callSettings.api?.throttle,
|
96
89
|
call: async () => callHuggingFaceTextGenerationAPI(callSettings),
|
97
90
|
});
|
98
91
|
}
|
@@ -100,7 +93,6 @@ export class HuggingFaceTextGenerationModel extends AbstractModel {
|
|
100
93
|
const eventSettingProperties = [
|
101
94
|
"stopSequences",
|
102
95
|
"maxCompletionTokens",
|
103
|
-
"baseUrl",
|
104
96
|
"topK",
|
105
97
|
"topP",
|
106
98
|
"temperature",
|
@@ -131,31 +123,10 @@ export class HuggingFaceTextGenerationModel extends AbstractModel {
|
|
131
123
|
const huggingFaceTextGenerationResponseSchema = z.array(z.object({
|
132
124
|
generated_text: z.string(),
|
133
125
|
}));
|
134
|
-
|
135
|
-
* Call a Hugging Face Inference API Text Generation Task to generate a text completion for the given prompt.
|
136
|
-
*
|
137
|
-
* @see https://huggingface.co/docs/api-inference/detailed_parameters#text-generation-task
|
138
|
-
*
|
139
|
-
* @example
|
140
|
-
* const response = await callHuggingFaceTextGenerationAPI({
|
141
|
-
* apiKey: HUGGINGFACE_API_KEY,
|
142
|
-
* model: "tiiuae/falcon-7b",
|
143
|
-
* inputs: "Write a short story about a robot learning to love:\n\n",
|
144
|
-
* temperature: 700,
|
145
|
-
* maxNewTokens: 500,
|
146
|
-
* options: {
|
147
|
-
* waitForModel: true,
|
148
|
-
* },
|
149
|
-
* });
|
150
|
-
*
|
151
|
-
* console.log(response[0].generated_text);
|
152
|
-
*/
|
153
|
-
async function callHuggingFaceTextGenerationAPI({ baseUrl = "https://api-inference.huggingface.co/models", abortSignal, apiKey, model, inputs, topK, topP, temperature, repetitionPenalty, maxNewTokens, maxTime, numReturnSequences, doSample, options, }) {
|
126
|
+
async function callHuggingFaceTextGenerationAPI({ api = new HuggingFaceApiConfiguration(), abortSignal, model, inputs, topK, topP, temperature, repetitionPenalty, maxNewTokens, maxTime, numReturnSequences, doSample, options, }) {
|
154
127
|
return postJsonToApi({
|
155
|
-
url:
|
156
|
-
headers:
|
157
|
-
Authorization: `Bearer ${apiKey}`,
|
158
|
-
},
|
128
|
+
url: api.assembleUrl(`/${model}`),
|
129
|
+
headers: api.headers,
|
159
130
|
body: {
|
160
131
|
inputs,
|
161
132
|
top_k: topK,
|
@@ -15,6 +15,7 @@ var __exportStar = (this && this.__exportStar) || function(m, exports) {
|
|
15
15
|
};
|
16
16
|
Object.defineProperty(exports, "__esModule", { value: true });
|
17
17
|
exports.HuggingFaceError = void 0;
|
18
|
+
__exportStar(require("./HuggingFaceApiConfiguration.cjs"), exports);
|
18
19
|
var HuggingFaceError_js_1 = require("./HuggingFaceError.cjs");
|
19
20
|
Object.defineProperty(exports, "HuggingFaceError", { enumerable: true, get: function () { return HuggingFaceError_js_1.HuggingFaceError; } });
|
20
21
|
__exportStar(require("./HuggingFaceTextEmbeddingModel.cjs"), exports);
|