2024-10-12 19:05:21 +05:30
|
|
|
import { getAllOpenAIModels } from "@/libs/openai"
|
2024-10-12 18:28:29 +05:30
|
|
|
import {
|
|
|
|
|
getAllOpenAIConfig,
|
|
|
|
|
getOpenAIConfigById as providerInfo
|
|
|
|
|
} from "./openai"
|
2024-09-29 19:12:19 +05:30
|
|
|
|
|
|
|
|
type Model = {
|
|
|
|
|
id: string
|
|
|
|
|
model_id: string
|
|
|
|
|
name: string
|
|
|
|
|
provider_id: string
|
|
|
|
|
lookup: string
|
|
|
|
|
db_type: string
|
|
|
|
|
}
|
|
|
|
|
export const generateID = () => {
|
|
|
|
|
return "model-xxxx-xxxx-xxx-xxxx".replace(/[x]/g, () => {
|
|
|
|
|
const r = Math.floor(Math.random() * 16)
|
|
|
|
|
return r.toString(16)
|
|
|
|
|
})
|
|
|
|
|
}
|
|
|
|
|
|
2024-10-12 16:53:42 +05:30
|
|
|
export const removeModelSuffix = (id: string) => {
|
2024-10-12 18:28:29 +05:30
|
|
|
return id.replace(
|
|
|
|
|
/_model-[a-f0-9]{4}-[a-f0-9]{4}-[a-f0-9]{3,4}-[a-f0-9]{4}/,
|
|
|
|
|
""
|
2024-10-12 19:05:21 +05:30
|
|
|
).replace(/_lmstudio_openai-[a-f0-9]{4}-[a-f0-9]{3}-[a-f0-9]{4}/, "")
|
|
|
|
|
}
|
|
|
|
|
export const isLMStudioModel = (model: string) => {
|
|
|
|
|
const lmstudioModelRegex = /_lmstudio_openai-[a-f0-9]{4}-[a-f0-9]{3}-[a-f0-9]{4}/
|
|
|
|
|
return lmstudioModelRegex.test(model)
|
2024-09-29 19:12:19 +05:30
|
|
|
}
|
2024-09-29 19:57:26 +05:30
|
|
|
|
2024-10-12 19:05:21 +05:30
|
|
|
export const getLMStudioModelId = (model: string): { model_id: string, provider_id: string } => {
|
|
|
|
|
const lmstudioModelRegex = /_lmstudio_openai-[a-f0-9]{4}-[a-f0-9]{3}-[a-f0-9]{4}/
|
|
|
|
|
const match = model.match(lmstudioModelRegex)
|
|
|
|
|
if (match) {
|
|
|
|
|
const modelId = match[0]
|
|
|
|
|
const providerId = match[0].replace("_lmstudio_openai-", "")
|
|
|
|
|
return { model_id: modelId, provider_id: providerId }
|
|
|
|
|
}
|
|
|
|
|
return null
|
|
|
|
|
}
|
2024-09-29 19:57:26 +05:30
|
|
|
export const isCustomModel = (model: string) => {
|
2024-10-12 19:05:21 +05:30
|
|
|
if (isLMStudioModel(model)) {
|
|
|
|
|
return true
|
|
|
|
|
}
|
2024-10-12 18:28:29 +05:30
|
|
|
const customModelRegex =
|
|
|
|
|
/_model-[a-f0-9]{4}-[a-f0-9]{4}-[a-f0-9]{3,4}-[a-f0-9]{4}/
|
2024-09-29 19:57:26 +05:30
|
|
|
return customModelRegex.test(model)
|
|
|
|
|
}
|
2024-09-29 19:12:19 +05:30
|
|
|
export class ModelDb {
|
|
|
|
|
db: chrome.storage.StorageArea
|
|
|
|
|
|
|
|
|
|
constructor() {
|
|
|
|
|
this.db = chrome.storage.local
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
getAll = async (): Promise<Model[]> => {
|
|
|
|
|
return new Promise((resolve, reject) => {
|
|
|
|
|
this.db.get(null, (result) => {
|
|
|
|
|
if (chrome.runtime.lastError) {
|
|
|
|
|
reject(chrome.runtime.lastError)
|
|
|
|
|
} else {
|
|
|
|
|
const data = Object.keys(result).map((key) => result[key])
|
|
|
|
|
resolve(data)
|
|
|
|
|
}
|
|
|
|
|
})
|
|
|
|
|
})
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
create = async (model: Model): Promise<void> => {
|
|
|
|
|
return new Promise((resolve, reject) => {
|
|
|
|
|
this.db.set({ [model.id]: model }, () => {
|
|
|
|
|
if (chrome.runtime.lastError) {
|
|
|
|
|
reject(chrome.runtime.lastError)
|
|
|
|
|
} else {
|
|
|
|
|
resolve()
|
|
|
|
|
}
|
|
|
|
|
})
|
|
|
|
|
})
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
getById = async (id: string): Promise<Model> => {
|
|
|
|
|
return new Promise((resolve, reject) => {
|
|
|
|
|
this.db.get(id, (result) => {
|
|
|
|
|
if (chrome.runtime.lastError) {
|
|
|
|
|
reject(chrome.runtime.lastError)
|
|
|
|
|
} else {
|
|
|
|
|
resolve(result[id])
|
|
|
|
|
}
|
|
|
|
|
})
|
|
|
|
|
})
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
update = async (model: Model): Promise<void> => {
|
|
|
|
|
return new Promise((resolve, reject) => {
|
|
|
|
|
this.db.set({ [model.id]: model }, () => {
|
|
|
|
|
if (chrome.runtime.lastError) {
|
|
|
|
|
reject(chrome.runtime.lastError)
|
|
|
|
|
} else {
|
|
|
|
|
resolve()
|
|
|
|
|
}
|
|
|
|
|
})
|
|
|
|
|
})
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
delete = async (id: string): Promise<void> => {
|
|
|
|
|
return new Promise((resolve, reject) => {
|
|
|
|
|
this.db.remove(id, () => {
|
|
|
|
|
if (chrome.runtime.lastError) {
|
|
|
|
|
reject(chrome.runtime.lastError)
|
|
|
|
|
} else {
|
|
|
|
|
resolve()
|
|
|
|
|
}
|
|
|
|
|
})
|
|
|
|
|
})
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
deleteAll = async (): Promise<void> => {
|
|
|
|
|
return new Promise((resolve, reject) => {
|
|
|
|
|
this.db.clear(() => {
|
|
|
|
|
if (chrome.runtime.lastError) {
|
|
|
|
|
reject(chrome.runtime.lastError)
|
|
|
|
|
} else {
|
|
|
|
|
resolve()
|
|
|
|
|
}
|
|
|
|
|
})
|
|
|
|
|
})
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
export const createManyModels = async (
|
|
|
|
|
data: { model_id: string; name: string; provider_id: string }[]
|
|
|
|
|
) => {
|
|
|
|
|
const db = new ModelDb()
|
|
|
|
|
|
|
|
|
|
const models = data.map((item) => {
|
|
|
|
|
return {
|
|
|
|
|
...item,
|
|
|
|
|
lookup: `${item.model_id}_${item.provider_id}`,
|
|
|
|
|
id: `${item.model_id}_${generateID()}`,
|
2024-10-12 16:53:42 +05:30
|
|
|
db_type: "openai_model",
|
|
|
|
|
name: item.name.replaceAll(/accounts\/[^\/]+\/models\//g, "")
|
2024-09-29 19:12:19 +05:30
|
|
|
}
|
|
|
|
|
})
|
|
|
|
|
|
|
|
|
|
for (const model of models) {
|
|
|
|
|
const isExist = await isLookupExist(model.lookup)
|
|
|
|
|
|
|
|
|
|
if (isExist) {
|
|
|
|
|
continue
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
await db.create(model)
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
export const createModel = async (
|
|
|
|
|
model_id: string,
|
|
|
|
|
name: string,
|
|
|
|
|
provider_id: string
|
|
|
|
|
) => {
|
|
|
|
|
const db = new ModelDb()
|
|
|
|
|
const id = generateID()
|
|
|
|
|
const model: Model = {
|
|
|
|
|
id: `${model_id}_${id}`,
|
|
|
|
|
model_id,
|
|
|
|
|
name,
|
|
|
|
|
provider_id,
|
|
|
|
|
lookup: `${model_id}_${provider_id}`,
|
|
|
|
|
db_type: "openai_model"
|
|
|
|
|
}
|
|
|
|
|
await db.create(model)
|
|
|
|
|
return model
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
export const getModelInfo = async (id: string) => {
|
|
|
|
|
const db = new ModelDb()
|
2024-10-12 19:05:21 +05:30
|
|
|
|
|
|
|
|
if (isLMStudioModel(id)) {
|
|
|
|
|
const lmstudioId = getLMStudioModelId(id)
|
|
|
|
|
if (!lmstudioId) {
|
|
|
|
|
throw new Error("Invalid LMStudio model ID")
|
|
|
|
|
}
|
|
|
|
|
return {
|
|
|
|
|
model_id: id.replace(/_lmstudio_openai-[a-f0-9]{4}-[a-f0-9]{3}-[a-f0-9]{4}/, ""),
|
|
|
|
|
provider_id: `openai-${lmstudioId.provider_id}`,
|
|
|
|
|
name: id.replace(/_lmstudio_openai-[a-f0-9]{4}-[a-f0-9]{3}-[a-f0-9]{4}/, "")
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2024-09-29 19:12:19 +05:30
|
|
|
const model = await db.getById(id)
|
|
|
|
|
return model
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
export const getAllCustomModels = async () => {
|
|
|
|
|
const db = new ModelDb()
|
|
|
|
|
const models = (await db.getAll()).filter(
|
|
|
|
|
(model) => model.db_type === "openai_model"
|
|
|
|
|
)
|
|
|
|
|
const modelsWithProvider = await Promise.all(
|
|
|
|
|
models.map(async (model) => {
|
|
|
|
|
const provider = await providerInfo(model.provider_id)
|
|
|
|
|
return { ...model, provider }
|
|
|
|
|
})
|
|
|
|
|
)
|
|
|
|
|
return modelsWithProvider
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
export const deleteModel = async (id: string) => {
|
|
|
|
|
const db = new ModelDb()
|
|
|
|
|
await db.delete(id)
|
|
|
|
|
}
|
|
|
|
|
|
2024-10-12 18:28:29 +05:30
|
|
|
export const deleteAllModelsByProviderId = async (provider_id: string) => {
|
|
|
|
|
const db = new ModelDb()
|
|
|
|
|
const models = await db.getAll()
|
|
|
|
|
const modelsToDelete = models.filter(
|
|
|
|
|
(model) => model.provider_id === provider_id
|
|
|
|
|
)
|
|
|
|
|
for (const model of modelsToDelete) {
|
|
|
|
|
await db.delete(model.id)
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2024-09-29 19:12:19 +05:30
|
|
|
export const isLookupExist = async (lookup: string) => {
|
|
|
|
|
const db = new ModelDb()
|
|
|
|
|
const models = await db.getAll()
|
|
|
|
|
const model = models.find((model) => model.lookup === lookup)
|
|
|
|
|
return model ? true : false
|
|
|
|
|
}
|
2024-09-29 19:57:26 +05:30
|
|
|
|
2024-10-12 19:05:21 +05:30
|
|
|
export const dynamicFetchLMStudio = async ({
|
|
|
|
|
baseUrl,
|
|
|
|
|
providerId
|
|
|
|
|
}: {
|
|
|
|
|
baseUrl: string
|
|
|
|
|
providerId: string
|
|
|
|
|
}) => {
|
|
|
|
|
const models = await getAllOpenAIModels(baseUrl)
|
|
|
|
|
const lmstudioModels = models.map((e) => {
|
|
|
|
|
return {
|
|
|
|
|
name: e?.name || e?.id,
|
|
|
|
|
id: `${e?.id}_lmstudio_${providerId}`,
|
|
|
|
|
provider: providerId,
|
|
|
|
|
lookup: `${e?.id}_${providerId}`,
|
|
|
|
|
provider_id: providerId,
|
|
|
|
|
}
|
|
|
|
|
})
|
|
|
|
|
|
|
|
|
|
return lmstudioModels
|
|
|
|
|
}
|
|
|
|
|
|
2024-09-29 19:57:26 +05:30
|
|
|
export const ollamaFormatAllCustomModels = async () => {
|
2024-10-12 19:05:21 +05:30
|
|
|
const [allModles, allProviders] = await Promise.all([
|
|
|
|
|
getAllCustomModels(),
|
|
|
|
|
getAllOpenAIConfig()
|
|
|
|
|
])
|
|
|
|
|
|
|
|
|
|
const lmstudioProviders = allProviders.filter(
|
|
|
|
|
(provider) => provider.provider === "lmstudio"
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
const lmModelsPromises = lmstudioProviders.map((provider) =>
|
|
|
|
|
dynamicFetchLMStudio({
|
|
|
|
|
baseUrl: provider.baseUrl,
|
|
|
|
|
providerId: provider.id
|
|
|
|
|
})
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
const lmModelsFetch = await Promise.all(lmModelsPromises)
|
|
|
|
|
|
|
|
|
|
const lmModels = lmModelsFetch.flat()
|
2024-09-29 19:57:26 +05:30
|
|
|
|
2024-10-12 19:05:21 +05:30
|
|
|
// merge allModels and lmModels
|
|
|
|
|
const allModlesWithLMStudio = [...allModles, ...lmModels]
|
2024-10-12 18:28:29 +05:30
|
|
|
|
2024-10-12 19:05:21 +05:30
|
|
|
const ollamaModels = allModlesWithLMStudio.map((model) => {
|
2024-09-29 19:57:26 +05:30
|
|
|
return {
|
|
|
|
|
name: model.name,
|
|
|
|
|
model: model.id,
|
|
|
|
|
modified_at: "",
|
2024-10-12 18:28:29 +05:30
|
|
|
provider:
|
|
|
|
|
allProviders.find((provider) => provider.id === model.provider_id)
|
|
|
|
|
?.provider || "custom",
|
2024-09-29 19:57:26 +05:30
|
|
|
size: 0,
|
|
|
|
|
digest: "",
|
|
|
|
|
details: {
|
|
|
|
|
parent_model: "",
|
|
|
|
|
format: "",
|
|
|
|
|
family: "",
|
|
|
|
|
families: [],
|
|
|
|
|
parameter_size: "",
|
|
|
|
|
quantization_level: ""
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
})
|
|
|
|
|
|
|
|
|
|
return ollamaModels
|
2024-10-12 18:28:29 +05:30
|
|
|
}
|