Spaces:
Running
Running
File size: 1,587 Bytes
3f76ff4 | 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 | import { NextResponse } from "next/server";
import { z } from "zod";
import { generateLlmText } from "@/lib/llm-client";
import { isLlmConfigReady, normalizeLlmConfig } from "@/lib/llm-config";
const RequestSchema = z.object({
llmConfig: z.object({
apiKey: z.string().optional(),
baseUrl: z.string().optional(),
model: z.string().optional(),
}),
});
export async function POST(req: Request) {
const json = await req.json().catch(() => null);
const parsed = RequestSchema.safeParse(json);
if (!parsed.success) {
return NextResponse.json(
{ ok: false, status: "error", message: "Bad request." },
{ status: 400 },
);
}
if (!isLlmConfigReady(parsed.data.llmConfig)) {
return NextResponse.json({
ok: false,
status: "idle",
message: "Add API key, base URL, and model to test the connection.",
});
}
const config = normalizeLlmConfig(parsed.data.llmConfig);
try {
const result = await generateLlmText({
config,
systemPrompt:
"You are running a connection test. Reply with a short confirmation only.",
userPrompt:
"Connection test. Reply with exactly: Connected to the configured model.",
temperature: 0,
maxTokens: 60,
});
return NextResponse.json({
ok: true,
status: "connected",
message: result.text,
model: config.model,
endpoint: result.endpoint,
});
} catch (error) {
return NextResponse.json({
ok: false,
status: "error",
message: String(error),
model: config.model,
});
}
}
|