@rocketnew/llm-sdk
Version:
Universal LLM SDK for JavaScript/TypeScript - OpenAI, Anthropic, Gemini, Perplexity and more
289 lines (284 loc) • 10.5 kB
JavaScript
import {
BinaryResponseContent,
init_responses
} from "./chunk-W4XUZXUW.mjs";
import {
BaseLLMException,
init_transformation
} from "./chunk-EMCXL27T.mjs";
import {
__esm,
all_rocketllm_params,
init_constants
} from "./chunk-DDRQKMV4.mjs";
// src/llms/base_llm/text_to_speech/transformation.ts
var BaseTextToSpeechConfig;
var init_transformation2 = __esm({
"src/llms/base_llm/text_to_speech/transformation.ts"() {
"use strict";
init_transformation();
BaseTextToSpeechConfig = class {
constructor() {
}
static getConfig() {
const config = {};
for (const [key, value] of Object.entries(this)) {
if (!key.startsWith("__") && !key.startsWith("_abc") && typeof value !== "function" && value !== null && value !== void 0) {
config[key] = value;
}
}
return config;
}
getErrorClass(errorMessage, statusCode, headers) {
throw new BaseLLMException({
statusCode,
message: errorMessage,
headers
});
}
};
}
});
// src/llms/elevenlabs/common_utils.ts
var ElevenLabsException;
var init_common_utils = __esm({
"src/llms/elevenlabs/common_utils.ts"() {
"use strict";
init_transformation();
ElevenLabsException = class extends BaseLLMException {
constructor(params) {
super(params);
}
};
}
});
// src/llms/elevenlabs/text_to_speech/transformation.ts
var _ElevenLabsTextToSpeechConfig, ElevenLabsTextToSpeechConfig;
var init_transformation3 = __esm({
"src/llms/elevenlabs/text_to_speech/transformation.ts"() {
init_transformation2();
init_common_utils();
init_constants();
init_responses();
_ElevenLabsTextToSpeechConfig = class _ElevenLabsTextToSpeechConfig extends BaseTextToSpeechConfig {
getSupportedOpenAIParams(_model) {
return ["voice", "response_format", "speed"];
}
_extractVoiceId(voice) {
const normalizedVoice = voice.trim();
const mapped = _ElevenLabsTextToSpeechConfig.VOICE_MAPPINGS[normalizedVoice.toLowerCase()];
return mapped || normalizedVoice;
}
_resolveVoiceId(voice, params) {
let mappedVoice = null;
if (typeof voice === "string" && voice.trim()) {
mappedVoice = this._extractVoiceId(voice);
} else if (voice !== null && voice !== void 0 && typeof voice === "object") {
for (const key of ["voice_id", "id", "name"]) {
const candidate = voice[key];
if (typeof candidate === "string" && candidate.trim()) {
mappedVoice = this._extractVoiceId(candidate);
break;
}
}
} else if (voice !== null && voice !== void 0) {
mappedVoice = this._extractVoiceId(String(voice));
}
if (mappedVoice === null) {
const voiceOverride = params.voice_id;
delete params.voice_id;
if (typeof voiceOverride === "string" && voiceOverride.trim()) {
mappedVoice = this._extractVoiceId(voiceOverride);
}
}
if (mappedVoice === null) {
throw new Error(
"ElevenLabs voice_id is required. Pass `voice` when calling `rocketllm.speech()`."
);
}
return mappedVoice;
}
mapOpenAIParams(_model, optionalParams, voice, dropParams, kwargs) {
const mappedParams = {};
const queryParams = {};
const params = optionalParams ? { ...optionalParams } : {};
const passthroughKwargs = kwargs ?? {};
const mappedVoice = this._resolveVoiceId(voice, params);
const responseFormat = params.response_format;
delete params.response_format;
if (typeof responseFormat === "string") {
const mapped = _ElevenLabsTextToSpeechConfig.FORMAT_MAPPINGS[responseFormat] || responseFormat;
queryParams["output_format"] = mapped;
}
const speed = params.speed;
delete params.speed;
if (speed !== null && speed !== void 0) {
let speedValue;
try {
speedValue = parseFloat(String(speed));
if (isNaN(speedValue)) speedValue = null;
} catch {
speedValue = null;
}
if (speedValue !== null) {
if (typeof params.voice_settings === "object" && params.voice_settings !== null) {
params.voice_settings.speed = speedValue;
} else {
params.voice_settings = { speed: speedValue };
}
}
}
delete params.instructions;
this._addElevenLabsSpecificParams(
mappedVoice,
queryParams,
mappedParams,
passthroughKwargs,
params
);
return [mappedVoice, mappedParams];
}
_addElevenLabsSpecificParams(mappedVoice, queryParams, mappedParams, kwargs, remainingParams) {
for (const [key, value] of Object.entries(remainingParams)) {
if (value === null || value === void 0) continue;
mappedParams[key] = value;
}
const reservedKwargKeys = /* @__PURE__ */ new Set([
...all_rocketllm_params,
_ElevenLabsTextToSpeechConfig.ELEVENLABS_QUERY_PARAMS_KEY,
_ElevenLabsTextToSpeechConfig.ELEVENLABS_VOICE_ID_KEY,
"voice",
"model",
"response_format",
"output_format",
"extra_body",
"user"
]);
const extraBodyFromKwargs = kwargs.extra_body;
delete kwargs.extra_body;
if (typeof extraBodyFromKwargs === "object" && extraBodyFromKwargs !== null) {
for (const [key, value] of Object.entries(extraBodyFromKwargs)) {
if (value === null || value === void 0) continue;
mappedParams[key] = value;
}
}
for (const key of Object.keys(kwargs)) {
if (reservedKwargKeys.has(key)) continue;
const value = kwargs[key];
if (value === null || value === void 0) continue;
mappedParams[key] = value;
delete kwargs[key];
}
if (Object.keys(queryParams).length > 0) {
kwargs[_ElevenLabsTextToSpeechConfig.ELEVENLABS_QUERY_PARAMS_KEY] = queryParams;
} else {
delete kwargs[_ElevenLabsTextToSpeechConfig.ELEVENLABS_QUERY_PARAMS_KEY];
}
kwargs[_ElevenLabsTextToSpeechConfig.ELEVENLABS_VOICE_ID_KEY] = mappedVoice;
}
validateEnvironment(headers, model, apiKey, apiBase) {
const resolvedApiKey = apiKey || process.env.ELEVENLABS_API_KEY;
if (!resolvedApiKey) {
throw new Error(
"ElevenLabs API key is required. Set ELEVENLABS_API_KEY environment variable."
);
}
headers["xi-api-key"] = resolvedApiKey;
headers["Content-Type"] = "application/json";
return headers;
}
getErrorClass(errorMessage, statusCode, headers) {
return new ElevenLabsException({
statusCode,
message: errorMessage,
headers
});
}
transformTextToSpeechRequest(model, input, voice, optionalParams, rocketllmParams, headers) {
const params = optionalParams ? { ...optionalParams } : {};
const extraBody = params.extra_body;
delete params.extra_body;
const requestBody = {
text: input,
model_id: model
};
for (const [key, value] of Object.entries(params)) {
if (value === null || value === void 0) continue;
requestBody[key] = value;
}
if (typeof extraBody === "object" && extraBody !== null) {
for (const [key, value] of Object.entries(extraBody)) {
if (value === null || value === void 0) continue;
requestBody[key] = value;
}
}
return {
dictBody: requestBody,
headers: { "Content-Type": "application/json" }
};
}
getCompleteUrl(model, apiBase, rocketllmParams) {
let baseUrl = apiBase || process.env.ELEVENLABS_API_BASE || _ElevenLabsTextToSpeechConfig.TTS_BASE_URL;
baseUrl = baseUrl.replace(/\/+$/, "");
const voiceId = rocketllmParams[_ElevenLabsTextToSpeechConfig.ELEVENLABS_VOICE_ID_KEY];
if (typeof voiceId !== "string" || !voiceId.trim()) {
throw new Error(
"ElevenLabs voice_id is required. Pass `voice` when calling `rocketllm.speech()`."
);
}
let url = `${baseUrl}${_ElevenLabsTextToSpeechConfig.TTS_ENDPOINT_PATH}/${voiceId}`;
const queryParams = rocketllmParams[_ElevenLabsTextToSpeechConfig.ELEVENLABS_QUERY_PARAMS_KEY] || {};
if (Object.keys(queryParams).length > 0) {
const searchParams = new URLSearchParams();
for (const [k, v] of Object.entries(queryParams)) {
searchParams.set(k, String(v));
}
url = `${url}?${searchParams.toString()}`;
}
return url;
}
async transformTextToSpeechResponse(_model, rawResponse) {
return new BinaryResponseContent(rawResponse);
}
};
_ElevenLabsTextToSpeechConfig.TTS_BASE_URL = "https://api.elevenlabs.io";
_ElevenLabsTextToSpeechConfig.TTS_ENDPOINT_PATH = "/v1/text-to-speech";
_ElevenLabsTextToSpeechConfig.DEFAULT_OUTPUT_FORMAT = "pcm_44100";
_ElevenLabsTextToSpeechConfig.VOICE_MAPPINGS = {
alloy: "21m00Tcm4TlvDq8ikWAM",
// Rachel
amber: "5Q0t7uMcjvnagumLfvZi",
// Paul
ash: "AZnzlk1XvdvUeBnXmlld",
// Domi
august: "D38z5RcWu1voky8WS1ja",
// Fin
blue: "2EiwWnXFnvU5JabPnv8n",
// Clyde
coral: "9BWtsMINqrJLrRacOk9x",
// Aria
lily: "EXAVITQu4vr4xnSDxMaL",
// Sarah
onyx: "29vD33N1CtxCmqQRPOHJ",
// Drew
sage: "CwhRBWXzGAHq8TQ4Fs17",
// Roger
verse: "CYw3kZ02Hs0563khs1Fj"
// Dave
};
_ElevenLabsTextToSpeechConfig.FORMAT_MAPPINGS = {
mp3: "mp3_44100_128",
pcm: "pcm_44100",
opus: "opus_48000_128"
};
_ElevenLabsTextToSpeechConfig.ELEVENLABS_QUERY_PARAMS_KEY = "__elevenlabs_query_params__";
_ElevenLabsTextToSpeechConfig.ELEVENLABS_VOICE_ID_KEY = "__elevenlabs_voice_id__";
ElevenLabsTextToSpeechConfig = _ElevenLabsTextToSpeechConfig;
}
});
export {
ElevenLabsException,
init_common_utils,
ElevenLabsTextToSpeechConfig,
init_transformation3 as init_transformation
};