Spaces:
Running
Running
File size: 7,090 Bytes
b39afbe |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 |
/**
* Copyright (c) 2023 MERCENARIES.AI PTE. LTD.
* All rights reserved.
*/
//@ts-check
import path from 'path';
// @ts-ignore
import {
walkDirForExtension,
validateDirectoryExists,
validateFileExists,
readJsonFromDisk
} from './files.js';
import { is_valid, console_log, pauseForSeconds } from './utils.js';
const DEFAULT_UNKNOWN_CONTEXT_SIZE = 2048;
const MODELS_DIR_JSON_PATH = ['..', '..', 'user_files', 'local_llms_directories.json']; // from process.cwd(), which is ./packages/server/
// @ts-ignore
function generateModelId(model_name, model_provider) {
return `${model_name}|${model_provider}`;
}
// @ts-ignore
function getModelNameAndProviderFromId(model_id) {
if (!model_id) throw new Error(`getModelNameAndProviderFromId: model_id is not valid: ${model_id}`);
const splits = model_id.split('|');
if (splits.length !== 2) throw new Error(`splitModelNameFromType: model_id is not valid: ${model_id}`);
return { model_name: splits[0], model_provider: splits[1] };
}
// @ts-ignore
async function isProviderAvailable(model_provider) {
const models_dir_json = await getModelsDirJson();
if (!models_dir_json) return false;
const provider_model_dir = models_dir_json[model_provider];
if (!provider_model_dir) return false;
const dir_exists = await validateDirectoryExists(provider_model_dir);
if (!dir_exists) return false;
return true;
}
// @ts-ignore
async function addLocalLlmChoices(choices, llm_model_types, llm_context_sizes, model_type, model_provider) {
const models_dir_json = await getModelsDirJson();
if (!models_dir_json) return;
const provider_model_dir = models_dir_json[model_provider];
if (!provider_model_dir) return;
const dir_exists = await validateDirectoryExists(provider_model_dir);
if (!dir_exists) return;
// @ts-ignore
let filePaths = [];
// @ts-ignore
filePaths = await walkDirForExtension(filePaths, provider_model_dir, '.bin');
for (const filepath of filePaths) {
const name = path.basename(filepath);
const id = generateModelId(name, model_provider);
const title = deduceLlmTitle(name, model_provider);
const description = deduceLlmDescription(name);
const choice = { value: id, title, description };
llm_model_types[name] = model_type;
llm_context_sizes[name] = DEFAULT_UNKNOWN_CONTEXT_SIZE;
choices.push(choice);
}
}
// @ts-ignore
function deduceLlmTitle(model_name, model_provider, provider_icon = '?') {
const title =
provider_icon +
// @ts-ignore
model_name.replace(/-/g, ' ').replace(/\b\w/g, (l) => l.toUpperCase()) +
' (' +
model_provider +
')';
return title;
}
// @ts-ignore
function deduceLlmDescription(model_name, context_size = 0) {
let description = model_name.substring(0, model_name.length - 4); // remove ".bin"
if (context_size > 0) description += ` (${Math.floor(context_size / 1024)}k)`;
return description;
}
async function getModelsDirJson() {
const json_path = path.resolve(process.cwd(), ...MODELS_DIR_JSON_PATH);
const file_exist = await validateFileExists(json_path);
if (!file_exist) return null;
const models_dir_json = await readJsonFromDisk(json_path);
return models_dir_json;
}
// @ts-ignore
async function fixJsonWithLlm(llm, json_string_to_fix) {
const ctx = llm.ctx;
let response = null;
const args = {};
args.user = ctx.userId;
args.prompt = json_string_to_fix;
args.instruction = 'Fix the JSON string below. Do not output anything else but the carefully fixed JSON string.';
args.temperature = 0;
try {
response = await llm.runLlmBlock(ctx, args);
} catch (err) {
console.error(`[FIXING] fixJsonWithLlm: Error fixing json: ${err}`);
return null;
}
const text = response?.answer_text || '';
console_log(`[FIXING] fixJsonWithLlm: text: ${text}`);
if (!is_valid(text)) return null;
return text;
}
// @ts-ignore
async function fixJsonString(llm, passed_string) {
if (!is_valid(passed_string)) {
throw new Error(`[FIXING] fixJsonString: passed string is not valid: ${passed_string}`);
}
if (typeof passed_string !== 'string') {
throw new Error(
`[FIXING] fixJsonString: passed string is not a string: ${passed_string}, type = ${typeof passed_string}`
);
}
// Replace \n with actual line breaks
const cleanedString = passed_string.replace(/\\n/g, '\n');
let jsonObject = null;
let fixed = false;
let attempt_count = 0;
let attempt_at_cleaned_string = cleanedString;
while (!fixed && attempt_count < 10) {
attempt_count++;
console_log(`[FIXING] Attempting to fix JSON string after ${attempt_count} attempts.\n`);
try {
jsonObject = JSON.parse(attempt_at_cleaned_string);
} catch (err) {
console.error(
`[FIXING] [${attempt_count}] Error fixing JSON string: ${err}, attempt_at_cleaned_string: ${attempt_at_cleaned_string}`
);
}
if (jsonObject !== null && jsonObject !== undefined) {
fixed = true;
console_log(`[FIXING] Successfully fixed JSON string after ${attempt_count} attempts.\n`);
return jsonObject;
}
const response = await fixJsonWithLlm(llm, passed_string);
if (response !== null && response !== undefined) {
attempt_at_cleaned_string = response;
}
await pauseForSeconds(0.5);
}
if (!fixed) {
throw new Error(`Error fixing JSON string after ${attempt_count} attempts.\ncleanedString: ${cleanedString})`);
}
return '{}';
}
class Llm {
// @ts-ignore
constructor(tokenizer, params = null) {
this.tokenizer = tokenizer;
this.context_sizes = {};
}
// @ts-ignore
countTextTokens(text) {
return this.tokenizer.countTextTokens(text);
}
// @ts-ignore
getModelContextSizeFromModelInfo(model_name) {
// @ts-ignore
return this.context_sizes[model_name];
}
// -----------------------------------------------------------------------
/**
* @param {any} ctx
* @param {string} prompt
* @param {string} instruction
* @param {string} model_name
* @param {number} [temperature=0]
* @param {any} args
* @returns {Promise<{ answer_text: string; answer_json: any; }>}
*/
// @ts-ignore
async query(ctx, prompt, instruction, model_name, temperature = 0, args = null) {
throw new Error('You have to implement this method');
}
/**
* @param {any} ctx
* @param {any} args
* @returns {Promise<{ answer_text: string; answer_json: any; }>}
*/
// @ts-ignore
async runLlmBlock(ctx, args) {
throw new Error('You have to implement this method');
}
getProvider() {
throw new Error('You have to implement this method');
}
getModelType() {
throw new Error('You have to implement this method');
}
// @ts-ignore
async getModelChoices(choices, llm_model_types, llm_context_sizes) {
throw new Error('You have to implement this method');
}
}
export {
Llm,
generateModelId,
getModelNameAndProviderFromId,
isProviderAvailable,
addLocalLlmChoices,
deduceLlmTitle,
deduceLlmDescription,
getModelsDirJson,
fixJsonString
};
export { DEFAULT_UNKNOWN_CONTEXT_SIZE };
|