feat: Add OpenAI provider support
This commit is contained in:
@@ -1,4 +1,7 @@
|
||||
import { getOpenAIConfigById as providerInfo } from "./openai"
|
||||
import {
|
||||
getAllOpenAIConfig,
|
||||
getOpenAIConfigById as providerInfo
|
||||
} from "./openai"
|
||||
|
||||
type Model = {
|
||||
id: string
|
||||
@@ -16,11 +19,15 @@ export const generateID = () => {
|
||||
}
|
||||
|
||||
export const removeModelSuffix = (id: string) => {
|
||||
return id.replace(/_model-[a-f0-9]{4}-[a-f0-9]{4}-[a-f0-9]{3,4}-[a-f0-9]{4}/, "")
|
||||
return id.replace(
|
||||
/_model-[a-f0-9]{4}-[a-f0-9]{4}-[a-f0-9]{3,4}-[a-f0-9]{4}/,
|
||||
""
|
||||
)
|
||||
}
|
||||
|
||||
export const isCustomModel = (model: string) => {
|
||||
const customModelRegex = /_model-[a-f0-9]{4}-[a-f0-9]{4}-[a-f0-9]{3,4}-[a-f0-9]{4}/
|
||||
const customModelRegex =
|
||||
/_model-[a-f0-9]{4}-[a-f0-9]{4}-[a-f0-9]{3,4}-[a-f0-9]{4}/
|
||||
return customModelRegex.test(model)
|
||||
}
|
||||
export class ModelDb {
|
||||
@@ -174,6 +181,17 @@ export const deleteModel = async (id: string) => {
|
||||
await db.delete(id)
|
||||
}
|
||||
|
||||
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)
|
||||
}
|
||||
}
|
||||
|
||||
export const isLookupExist = async (lookup: string) => {
|
||||
const db = new ModelDb()
|
||||
const models = await db.getAll()
|
||||
@@ -181,17 +199,19 @@ export const isLookupExist = async (lookup: string) => {
|
||||
return model ? true : false
|
||||
}
|
||||
|
||||
|
||||
export const ollamaFormatAllCustomModels = async () => {
|
||||
|
||||
const allModles = await getAllCustomModels()
|
||||
|
||||
const allProviders = await getAllOpenAIConfig()
|
||||
|
||||
const ollamaModels = allModles.map((model) => {
|
||||
return {
|
||||
name: model.name,
|
||||
model: model.id,
|
||||
modified_at: "",
|
||||
provider: "custom",
|
||||
provider:
|
||||
allProviders.find((provider) => provider.id === model.provider_id)
|
||||
?.provider || "custom",
|
||||
size: 0,
|
||||
digest: "",
|
||||
details: {
|
||||
@@ -206,4 +226,4 @@ export const ollamaFormatAllCustomModels = async () => {
|
||||
})
|
||||
|
||||
return ollamaModels
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,4 +1,5 @@
|
||||
import { cleanUrl } from "@/libs/clean-url"
|
||||
import { deleteAllModelsByProviderId } from "./models"
|
||||
|
||||
type OpenAIModelConfig = {
|
||||
id: string
|
||||
@@ -93,7 +94,7 @@ export class OpenAIModelDb {
|
||||
}
|
||||
|
||||
|
||||
export const addOpenAICofig = async ({ name, baseUrl, apiKey }: { name: string, baseUrl: string, apiKey: string }) => {
|
||||
export const addOpenAICofig = async ({ name, baseUrl, apiKey, provider }: { name: string, baseUrl: string, apiKey: string, provider?: string }) => {
|
||||
const openaiDb = new OpenAIModelDb()
|
||||
const id = generateID()
|
||||
const config: OpenAIModelConfig = {
|
||||
@@ -102,7 +103,8 @@ export const addOpenAICofig = async ({ name, baseUrl, apiKey }: { name: string,
|
||||
baseUrl: cleanUrl(baseUrl),
|
||||
apiKey,
|
||||
createdAt: Date.now(),
|
||||
db_type: "openai"
|
||||
db_type: "openai",
|
||||
provider
|
||||
}
|
||||
await openaiDb.create(config)
|
||||
return id
|
||||
@@ -117,13 +119,15 @@ export const getAllOpenAIConfig = async () => {
|
||||
|
||||
export const updateOpenAIConfig = async ({ id, name, baseUrl, apiKey }: { id: string, name: string, baseUrl: string, apiKey: string }) => {
|
||||
const openaiDb = new OpenAIModelDb()
|
||||
const oldData = await openaiDb.getById(id)
|
||||
const config: OpenAIModelConfig = {
|
||||
...oldData,
|
||||
id,
|
||||
name,
|
||||
baseUrl: cleanUrl(baseUrl),
|
||||
apiKey,
|
||||
createdAt: Date.now(),
|
||||
db_type: "openai"
|
||||
db_type: "openai",
|
||||
}
|
||||
|
||||
await openaiDb.update(config)
|
||||
@@ -135,6 +139,7 @@ export const updateOpenAIConfig = async ({ id, name, baseUrl, apiKey }: { id: st
|
||||
export const deleteOpenAIConfig = async (id: string) => {
|
||||
const openaiDb = new OpenAIModelDb()
|
||||
await openaiDb.delete(id)
|
||||
await deleteAllModelsByProviderId(id)
|
||||
}
|
||||
|
||||
|
||||
|
||||
Reference in New Issue
Block a user