File size: 3,578 Bytes
4c6dadf b916b29 8c06509 b916b29 8c06509 b916b29 700dff9 7c6cf75 4c6dadf 15052fd bf1e3ff 16e3fae d1bf860 |
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 |
import { IFactory } from '@/interfaces/database/llm';
import isObject from 'lodash/isObject';
import snakeCase from 'lodash/snakeCase';
export const isFormData = (data: unknown): data is FormData => {
return data instanceof FormData;
};
const excludedFields = ['img2txt_id'];
const isExcludedField = (key: string) => {
return excludedFields.includes(key);
};
export const convertTheKeysOfTheObjectToSnake = (data: unknown) => {
if (isObject(data) && !isFormData(data)) {
return Object.keys(data).reduce<Record<string, any>>((pre, cur) => {
const value = (data as Record<string, any>)[cur];
pre[isFormData(value) || isExcludedField(cur) ? cur : snakeCase(cur)] =
value;
return pre;
}, {});
}
return data;
};
export const getSearchValue = (key: string) => {
const params = new URL(document.location as any).searchParams;
return params.get(key);
};
// Formatize numbers, add thousands of separators
export const formatNumberWithThousandsSeparator = (numberStr: string) => {
const formattedNumber = numberStr.replace(/\B(?=(\d{3})+(?!\d))/g, ',');
return formattedNumber;
};
const orderFactoryList = [
'OpenAI',
'Moonshot',
'ZHIPU-AI',
'Ollama',
'Xinference',
];
export const sortLLmFactoryListBySpecifiedOrder = (list: IFactory[]) => {
const finalList: IFactory[] = [];
orderFactoryList.forEach((orderItem) => {
const index = list.findIndex((item) => item.name === orderItem);
if (index !== -1) {
finalList.push(list[index]);
}
});
list.forEach((item) => {
if (finalList.every((x) => x.name !== item.name)) {
finalList.push(item);
}
});
return finalList;
};
export const filterOptionsByInput = (
input: string,
option: { label: string; value: string } | undefined,
) => (option?.label ?? '').toLowerCase().includes(input.toLowerCase());
export const toFixed = (value: unknown, fixed = 2) => {
if (typeof value === 'number') {
return value.toFixed(fixed);
}
return value;
};
export const stringToUint8Array = (str: string) => {
// const byteString = str.replace(/b'|'/g, '');
const byteString = str.slice(2, -1);
const uint8Array = new Uint8Array(byteString.length);
for (let i = 0; i < byteString.length; i++) {
uint8Array[i] = byteString.charCodeAt(i);
}
return uint8Array;
};
export const hexStringToUint8Array = (hex: string) => {
const arr = hex.match(/[\da-f]{2}/gi);
if (Array.isArray(arr)) {
return new Uint8Array(
arr.map(function (h) {
return parseInt(h, 16);
}),
);
}
};
export function hexToArrayBuffer(input: string) {
if (typeof input !== 'string') {
throw new TypeError('Expected input to be a string');
}
if (input.length % 2 !== 0) {
throw new RangeError('Expected string to be an even number of characters');
}
const view = new Uint8Array(input.length / 2);
for (let i = 0; i < input.length; i += 2) {
view[i / 2] = parseInt(input.substring(i, i + 2), 16);
}
return view.buffer;
}
export function formatFileSize(bytes: number, si = true, dp = 1) {
let nextBytes = bytes;
const thresh = si ? 1000 : 1024;
if (Math.abs(bytes) < thresh) {
return nextBytes + ' B';
}
const units = si
? ['kB', 'MB', 'GB', 'TB', 'PB', 'EB', 'ZB', 'YB']
: ['KiB', 'MiB', 'GiB', 'TiB', 'PiB', 'EiB', 'ZiB', 'YiB'];
let u = -1;
const r = 10 ** dp;
do {
nextBytes /= thresh;
++u;
} while (
Math.round(Math.abs(nextBytes) * r) / r >= thresh &&
u < units.length - 1
);
return nextBytes.toFixed(dp) + ' ' + units[u];
}
|