mirror of
https://github.com/wassname/chatGPTBox.git
synced 2026-08-02 12:30:35 +08:00
WIP, custom API Modes
This commit is contained in:
@@ -21,7 +21,8 @@
|
||||
// SOFTWARE.
|
||||
|
||||
import { encode } from '@nem035/gpt-3-encoder'
|
||||
import { getUserConfig, Models } from '../config/index.mjs'
|
||||
import { getUserConfig } from '../config/index.mjs'
|
||||
import { modelNameToDesc } from './model-name-convert.mjs'
|
||||
|
||||
const clamp = (v, min, max) => {
|
||||
return Math.min(Math.max(v, min), max)
|
||||
@@ -35,7 +36,7 @@ export async function cropText(
|
||||
tiktoken = true,
|
||||
) {
|
||||
const userConfig = await getUserConfig()
|
||||
const k = Models[userConfig.modelName].desc.match(/[- (]*([0-9]+)k/)?.[1]
|
||||
const k = modelNameToDesc(userConfig.modelName).match(/[- (]*([0-9]+)k/)?.[1]
|
||||
if (k) {
|
||||
maxLength = Number(k) * 1000
|
||||
maxLength -= 100 + clamp(userConfig.maxResponseTokenLength, 1, maxLength - 1000)
|
||||
|
||||
@@ -19,3 +19,4 @@ export * from './set-element-position-in-viewport'
|
||||
export * from './eventsource-parser.mjs'
|
||||
export * from './update-ref-height'
|
||||
export * from './wait-for-element-to-exist-and-select.mjs'
|
||||
export * from './model-name-convert.mjs'
|
||||
|
||||
@@ -0,0 +1,80 @@
|
||||
import { AlwaysCustomGroups, ModelGroups, ModelMode, Models } from '../config/index.mjs'
|
||||
|
||||
export function modelNameToDesc(modelName, t) {
|
||||
if (!t) t = (x) => x
|
||||
if (modelName in Models) return t(Models[modelName].desc)
|
||||
|
||||
let desc = modelName
|
||||
if (isCustomModelName(modelName)) {
|
||||
const presetPart = modelNameToPresetPart(modelName)
|
||||
const customPart = modelNameToCustomPart(modelName)
|
||||
if (presetPart in Models) {
|
||||
if (customPart in ModelMode)
|
||||
desc = `${t(Models[presetPart].desc)} (${t(ModelMode[customPart])})`
|
||||
else desc = `${t(Models[presetPart].desc)} (${customPart})`
|
||||
} else if (presetPart in ModelGroups) {
|
||||
desc = `${t(ModelGroups[presetPart].desc)} (${customPart})`
|
||||
}
|
||||
}
|
||||
return desc
|
||||
}
|
||||
|
||||
export function modelNameToPresetPart(modelName) {
|
||||
if (isCustomModelName(modelName)) {
|
||||
return modelName.split('-')[0]
|
||||
} else {
|
||||
return modelName
|
||||
}
|
||||
}
|
||||
|
||||
export function modelNameToCustomPart(modelName) {
|
||||
if (isCustomModelName(modelName)) {
|
||||
return modelName.substring(modelName.indexOf('-') + 1)
|
||||
} else {
|
||||
return modelName
|
||||
}
|
||||
}
|
||||
|
||||
export function modelNameToValue(modelName) {
|
||||
if (modelName in Models) return Models[modelName].value
|
||||
|
||||
return modelNameToCustomPart(modelName)
|
||||
}
|
||||
|
||||
export function isCustomModelName(modelName) {
|
||||
return modelName.includes('-')
|
||||
}
|
||||
|
||||
export function modelNameToApiMode(modelName) {
|
||||
const presetPart = modelNameToPresetPart(modelName)
|
||||
const found =
|
||||
Object.entries(ModelGroups).find(([k]) => presetPart === k) ||
|
||||
Object.entries(ModelGroups).find(([, g]) => presetPart in g.value)
|
||||
if (found) {
|
||||
const [groupName] = found
|
||||
const isCustom = isCustomModelName(modelName)
|
||||
let customName = ''
|
||||
if (isCustom) customName = modelNameToCustomPart()
|
||||
return {
|
||||
groupName,
|
||||
itemName: presetPart,
|
||||
isCustom,
|
||||
customName,
|
||||
customUrl: '',
|
||||
apiKey: '',
|
||||
active: true,
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
export function apiModeToModelName(apiMode) {
|
||||
if (AlwaysCustomGroups.includes(apiMode.groupName))
|
||||
return apiMode.groupName + '-' + apiMode.customName
|
||||
|
||||
if (apiMode.isCustom) {
|
||||
if (apiMode.itemName === 'custom') return apiMode.groupName + '-' + apiMode.customName
|
||||
return apiMode.itemName + '-' + apiMode.customName
|
||||
}
|
||||
|
||||
return apiMode.itemName
|
||||
}
|
||||
Reference in New Issue
Block a user