First version running with npm run dev
This commit is contained in:
commit
f90ec06a4f
28 files changed
+12042
No files matched your search
@@ -0,0 +1,121 @@
|
||||
import {
|
||||
defineEventHandler,
|
||||
readBody,
|
||||
createError,
|
||||
setResponseHeaders,
|
||||
} from "h3";
|
||||
|
||||
interface TranslateRequestBody {
|
||||
text: string;
|
||||
source: string;
|
||||
target: string;
|
||||
model?: string;
|
||||
}
|
||||
|
||||
export default defineEventHandler(async (event) => {
|
||||
const body = await readBody<TranslateRequestBody>(event);
|
||||
|
||||
if (!body.text || !body.source || !body.target) {
|
||||
throw createError({
|
||||
statusCode: 400,
|
||||
statusMessage: "Missing required fields: text, source, target",
|
||||
});
|
||||
}
|
||||
|
||||
if (!body.text.trim()) {
|
||||
throw createError({
|
||||
statusCode: 400,
|
||||
statusMessage: "Text cannot be empty",
|
||||
});
|
||||
}
|
||||
|
||||
const config = useRuntimeConfig();
|
||||
const ollamaUrl = config.ollamahost;
|
||||
const ALLOWED_MODELS = [
|
||||
"translategemma:1b",
|
||||
"translategemma:2b",
|
||||
"translategemma:4b",
|
||||
];
|
||||
const model =
|
||||
body.model && ALLOWED_MODELS.includes(body.model)
|
||||
? body.model
|
||||
: config.ollamamodel;
|
||||
|
||||
const prompt = `Translate the following text from ${body.source} to ${body.target}. Only output the translation, nothing else.
|
||||
|
||||
Text to translate: ${body.text}`;
|
||||
|
||||
try {
|
||||
const response = await fetch(`${ollamaUrl}/api/generate`, {
|
||||
method: "POST",
|
||||
headers: {
|
||||
"Content-Type": "application/json",
|
||||
},
|
||||
body: JSON.stringify({
|
||||
model,
|
||||
prompt,
|
||||
stream: true,
|
||||
}),
|
||||
});
|
||||
|
||||
if (!response.ok) {
|
||||
const errorBody = await response.json().catch(() => ({}));
|
||||
const errorMsg = errorBody.error || "Ollama request failed";
|
||||
|
||||
if (
|
||||
errorMsg.includes("model not found") ||
|
||||
errorMsg.includes("does not exist")
|
||||
) {
|
||||
throw createError({
|
||||
statusCode: 404,
|
||||
statusMessage: `Model "${model}" not found. Install it by running: ollama pull ${model}`,
|
||||
});
|
||||
}
|
||||
|
||||
throw createError({
|
||||
statusCode: response.status,
|
||||
statusMessage: errorMsg,
|
||||
});
|
||||
}
|
||||
|
||||
if (!response.body) {
|
||||
throw createError({
|
||||
statusCode: 502,
|
||||
statusMessage: "No response body from Ollama",
|
||||
});
|
||||
}
|
||||
|
||||
setResponseHeaders(event, {
|
||||
"Content-Type": "application/x-ndjson",
|
||||
"Transfer-Encoding": "chunked",
|
||||
"Cache-Control": "no-cache",
|
||||
});
|
||||
|
||||
return response.body;
|
||||
} catch (error) {
|
||||
if (error instanceof Error) {
|
||||
if (
|
||||
error.message.includes("ECONNREFUSED") ||
|
||||
error.message.includes("fetch failed")
|
||||
) {
|
||||
throw createError({
|
||||
statusCode: 503,
|
||||
statusMessage:
|
||||
"Ollama service is not available. Please ensure Ollama is running on " +
|
||||
ollamaUrl,
|
||||
});
|
||||
}
|
||||
if ("statusCode" in error) {
|
||||
throw error;
|
||||
}
|
||||
throw createError({
|
||||
statusCode: 500,
|
||||
statusMessage: error.message,
|
||||
});
|
||||
}
|
||||
throw createError({
|
||||
statusCode: 500,
|
||||
statusMessage: "Unknown error occurred during streaming translation",
|
||||
});
|
||||
}
|
||||
});
|
||||
Reference in new issue
Block a user