Add Ollama status checks and Docker deployment
This commit is contained in:
@@ -5,14 +5,75 @@ export class OllamaUnavailableError extends Error {
|
||||
}
|
||||
}
|
||||
|
||||
export type OllamaStatus = {
|
||||
available: boolean;
|
||||
configuredModel: string;
|
||||
configuredUrl: string;
|
||||
installedModels: string[];
|
||||
modelReady: boolean;
|
||||
message: string;
|
||||
};
|
||||
|
||||
type GenerateJsonInput = {
|
||||
prompt: string;
|
||||
model?: string;
|
||||
};
|
||||
|
||||
function getOllamaConfig() {
|
||||
return {
|
||||
baseUrl: (process.env.OLLAMA_URL ?? "http://127.0.0.1:11434").replace(/\/$/, ""),
|
||||
model: process.env.OLLAMA_MODEL ?? "qwen3.5:9b",
|
||||
};
|
||||
}
|
||||
|
||||
export async function getOllamaStatus(): Promise<OllamaStatus> {
|
||||
const { baseUrl, model } = getOllamaConfig();
|
||||
|
||||
try {
|
||||
const response = await fetch(`${baseUrl}/api/tags`, {
|
||||
method: "GET",
|
||||
headers: { "Content-Type": "application/json" },
|
||||
cache: "no-store",
|
||||
});
|
||||
|
||||
if (!response.ok) {
|
||||
throw new OllamaUnavailableError(`Ollama status request failed with status ${response.status}.`);
|
||||
}
|
||||
|
||||
const payload = (await response.json()) as { models?: Array<{ name?: string }> };
|
||||
const installedModels = (payload.models ?? []).map((entry) => entry.name).filter((name): name is string => Boolean(name));
|
||||
const modelReady = installedModels.includes(model);
|
||||
|
||||
return {
|
||||
available: true,
|
||||
configuredModel: model,
|
||||
configuredUrl: baseUrl,
|
||||
installedModels,
|
||||
modelReady,
|
||||
message: modelReady
|
||||
? `Ollama is reachable and ${model} is ready.`
|
||||
: `Ollama is reachable, but ${model} is not pulled yet.`,
|
||||
};
|
||||
} catch (error) {
|
||||
const message =
|
||||
error instanceof OllamaUnavailableError
|
||||
? error.message
|
||||
: "Ollama is not reachable at the configured URL.";
|
||||
|
||||
return {
|
||||
available: false,
|
||||
configuredModel: model,
|
||||
configuredUrl: baseUrl,
|
||||
installedModels: [],
|
||||
modelReady: false,
|
||||
message,
|
||||
};
|
||||
}
|
||||
}
|
||||
|
||||
export async function generateOllamaJson<T>({ prompt, model }: GenerateJsonInput): Promise<T> {
|
||||
const baseUrl = (process.env.OLLAMA_URL ?? "http://127.0.0.1:11434").replace(/\/$/, "");
|
||||
const selectedModel = model ?? process.env.OLLAMA_MODEL ?? "qwen3.5:9b";
|
||||
const { baseUrl, model: configuredModel } = getOllamaConfig();
|
||||
const selectedModel = model ?? configuredModel;
|
||||
|
||||
let response: Response;
|
||||
|
||||
|
||||
Reference in New Issue
Block a user