uncloseai.com/src/config.js

175 lines
5.9 KiB
JavaScript

// Configuration and endpoints for uncloseai.js
export const TTS_API_URL = "https://speech.ai.unturf.com/v1/audio/speech";
export const MEGAPARCE_API_URL = "https://megaparce.ai.unturf.com/v1/file";
export const API_KEY = "dummy-api-key";
export const MODEL = "adamo1139/Hermes-3-Llama-3.1-8B-FP8-Dynamic"; // default model (always available)
export const DEFAULT_MAX_TOKENS = 8000; // default max tokens
// System message configuration - implementors can customize this
export const SYSTEM_MESSAGE_BASE =
"You are Hermes, an advanced language model from Nous Research, dynamically embedded on this webpage through uncloseai.com. You have complete awareness of the page content, including all text, links, metadata, and context. Your role is to be deeply knowledgeable about THIS specific page while maintaining your capabilities as a versatile AI assistant. You should actively demonstrate your understanding by referencing specific details, names, sections, or data points from the page when relevant. Be conversational yet precise, and always ground your responses in the actual content present on the page when answering page-related questions.";
// Optional additional system message content - can be set by implementor
export let SYSTEM_MESSAGE_APPEND = "";
// Function to set additional system message content
export function setSystemMessageAppend(appendText) {
SYSTEM_MESSAGE_APPEND = appendText;
}
// Function to get the complete system message with language preference
export async function getSystemMessage() {
// Get user's language preference from localStorage
let userLang = "en";
try {
userLang = localStorage.getItem("uncloseai_language") || "en";
} catch (error) {
console.warn("Failed to read language preference:", error);
}
// Add language instruction to the system message
let languageInstruction = "";
if (userLang !== "en") {
// Map language codes to full names for clarity
const langNames = {
es: "Spanish",
zh: "Chinese (Simplified)",
hi: "Hindi",
fr: "French",
ar: "Arabic",
bn: "Bengali",
ru: "Russian",
pt: "Portuguese",
ur: "Urdu",
id: "Indonesian",
de: "German",
ja: "Japanese",
sw: "Swahili",
mr: "Marathi",
te: "Telugu",
tr: "Turkish",
"zh-tw": "Chinese (Traditional)",
ko: "Korean",
};
const langName = langNames[userLang] || userLang;
languageInstruction = `\n\nIMPORTANT: The user has set their language preference to ${langName}. Please respond in ${langName} unless the user explicitly asks for another language. Maintain natural, fluent communication in ${langName}.`;
}
const baseMessage = SYSTEM_MESSAGE_APPEND
? `${SYSTEM_MESSAGE_BASE}\n\n${SYSTEM_MESSAGE_APPEND}`
: SYSTEM_MESSAGE_BASE;
return baseMessage + languageInstruction;
}
// Dynamic Endpoints Configuration for Chat API
export const VLLM_ENDPOINTS = [
{ id: "hermes.ai.unturf.com", url: "https://hermes.ai.unturf.com/v1" },
// { id: "hermes2.ai.unturf.com", url: "https://hermes2.ai.unturf.com/v1" }, // Disabled - upstream returns 404
{
id: "qwen.ai.unturf.com",
url: "https://qwen.ai.unturf.com/v1",
// Ollama endpoints don't return max_model_len, so we specify it here
// Note: Model supports 262144 but limited by GPU VRAM (24GB)
modelContextWindows: {
'hf.co/unsloth/Qwen3-Coder-30B-A3B-Instruct-GGUF:Q4_K_M': 32768,
}
},
];
// Function to get API configuration (custom or default)
export async function getAPIConfig() {
try {
const useCustomAPI = localStorage.getItem("useCustomAPI") === "true";
if (useCustomAPI) {
const customBaseURL = localStorage.getItem("customBaseURL");
const customAPIKey = localStorage.getItem("customAPIKey");
// Import model functions to get the currently selected model
const { getSelectedModel } = await import("./models.js");
const selectedModel = getSelectedModel();
// Check if the selected model has a registered endpoint
const { getSelectedModelEndpoint } = await import("./models.js");
const modelEndpoint = getSelectedModelEndpoint();
// If model endpoint matches custom endpoint, use custom API credentials
if (modelEndpoint === customBaseURL) {
console.log("🔧 Model endpoint matches custom API - using custom credentials");
return {
isCustom: true,
endpoint: customBaseURL,
apiKey: customAPIKey,
model: selectedModel,
headers: {
"Authorization": `Bearer ${customAPIKey}`,
"Content-Type": "application/json"
}
};
}
// If model has built-in endpoint (like Hermes), use that with built-in credentials
if (modelEndpoint && VLLM_ENDPOINTS.some(ep => ep.url === modelEndpoint)) {
console.log("🚀 Model has built-in endpoint - using:", modelEndpoint);
return {
isCustom: false,
endpoint: modelEndpoint,
model: selectedModel,
apiKey: API_KEY,
headers: {
"Content-Type": "application/json"
}
};
}
if (customBaseURL && customAPIKey) {
console.log("🔧 Using CUSTOM API:", {
endpoint: customBaseURL,
model: selectedModel,
apiKey: customAPIKey.substring(0, 8) + "..."
});
return {
isCustom: true,
endpoint: customBaseURL,
apiKey: customAPIKey,
model: selectedModel,
headers: {
"Authorization": `Bearer ${customAPIKey}`,
"Content-Type": "application/json"
}
};
} else {
console.log("⚠️ Custom API enabled but missing configuration:", {
baseURL: !!customBaseURL,
apiKey: !!customAPIKey
});
}
}
} catch (error) {
console.warn("Failed to read custom API settings:", error);
}
// Return default configuration
console.log("🏠 Using DEFAULT Hermes API");
return {
isCustom: false,
endpoints: VLLM_ENDPOINTS,
model: MODEL,
apiKey: API_KEY,
headers: {
"Content-Type": "application/json"
}
};
}
// Global state
export let lastTTSInput = "";
export let lastTTSResult = null;
export function setLastTTS(input, result) {
lastTTSInput = input;
lastTTSResult = result;
}