Spaces:
Sleeping
Sleeping
File size: 4,581 Bytes
4c2a557 a572854 4c2a557 a572854 4c2a557 a572854 4c2a557 a572854 4c2a557 a572854 4c2a557 a572854 4c2a557 a572854 4c2a557 a572854 4c2a557 | 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 | import { NextResponse } from "next/server";
import { ensureTablesExist, getOrCreateModelPrices } from "@/lib/db/client";
import { verifyApiToken } from "@/lib/auth";
interface ModelInfo {
id: string;
base_model_id: string;
name: string;
params: {
system: string;
};
meta: {
profile_image_url: string;
};
}
interface ModelResponse {
data: {
id: string;
name: string;
info: ModelInfo;
}[];
}
export async function GET(req: Request) {
const authError = verifyApiToken(req);
if (authError) {
return authError;
}
try {
await ensureTablesExist();
const domain = process.env.OPENWEBUI_DOMAIN;
if (!domain) {
throw new Error("OPENWEBUI_DOMAIN environment variable is not set.");
}
const apiUrl = domain.replace(/\/+$/, "") + "/api/models";
const response = await fetch(apiUrl, {
headers: {
Authorization: `Bearer ${process.env.OPENWEBUI_API_KEY}`,
Accept: "application/json",
},
});
if (!response.ok) {
console.error("API response status:", response.status);
console.error("API response text:", await response.text());
throw new Error(`Failed to fetch models: ${response.status}`);
}
const responseText = await response.text();
let data: ModelResponse;
try {
data = JSON.parse(responseText);
} catch (error) {
console.error("Failed to parse JSON:", error);
throw new Error("Invalid JSON response from API");
}
if (!data || !Array.isArray(data.data)) {
console.error("Unexpected API response structure:", data);
throw new Error("Unexpected API response structure");
}
const apiModelsMap = new Map();
data.data.forEach((item) => {
apiModelsMap.set(String(item.id), {
name: String(item.name),
base_model_id: item.info?.base_model_id || "",
imageUrl: item.info?.meta?.profile_image_url || "/static/favicon.png",
system_prompt: item.info?.params?.system || "",
});
});
const modelsWithPrices = await getOrCreateModelPrices(
data.data.map((item) => {
let baseModelId = item.info?.base_model_id;
if (!baseModelId && item.id) {
const idParts = String(item.id).split(".");
if (idParts.length > 1) {
baseModelId = idParts[idParts.length - 1];
}
}
return {
id: String(item.id),
name: String(item.name),
base_model_id: baseModelId,
};
})
);
const dbModelsMap = new Map();
modelsWithPrices.forEach((model) => {
dbModelsMap.set(model.id, {
input_price: model.input_price,
output_price: model.output_price,
per_msg_price: model.per_msg_price,
updated_at: model.updated_at,
});
});
const validModels = Array.from(apiModelsMap.entries()).map(
([id, apiModel]) => {
const dbModel = dbModelsMap.get(id) || {
input_price: 60,
output_price: 60,
per_msg_price: -1,
updated_at: new Date(),
};
let baseModelId = apiModel.base_model_id;
if (!baseModelId && id) {
const idParts = String(id).split(".");
if (idParts.length > 1) {
baseModelId = idParts[idParts.length - 1];
}
}
return {
id: id,
base_model_id: baseModelId,
name: apiModel.name,
imageUrl: apiModel.imageUrl,
system_prompt: apiModel.system_prompt,
input_price: dbModel.input_price,
output_price: dbModel.output_price,
per_msg_price: dbModel.per_msg_price,
updated_at: dbModel.updated_at,
};
}
);
return NextResponse.json(validModels);
} catch (error) {
console.error("Error fetching models:", error);
return NextResponse.json(
{
error:
error instanceof Error ? error.message : "Failed to fetch models",
},
{ status: 500 }
);
}
}
export async function POST(req: Request) {
const authError = verifyApiToken(req);
if (authError) {
return authError;
}
const data = await req.json();
return new Response("Inlet placeholder response", {
headers: { "Content-Type": "application/json" },
});
}
export async function PUT(req: Request) {
const authError = verifyApiToken(req);
if (authError) {
return authError;
}
const data = await req.json();
return new Response("Outlet placeholder response", {
headers: { "Content-Type": "application/json" },
});
}
|