Improve local startup checks and Korean STT defaults
This commit is contained in:
@@ -21,6 +21,13 @@ interface OllamaChatResponse {
|
||||
error?: string;
|
||||
}
|
||||
|
||||
interface OllamaTagsResponse {
|
||||
models?: Array<{
|
||||
name?: string;
|
||||
model?: string;
|
||||
}>;
|
||||
}
|
||||
|
||||
function normalizeReply(text: string): string {
|
||||
const strippedThink = text.replace(/<think>[\s\S]*?<\/think>/gi, " ");
|
||||
const compact = strippedThink.replace(/\s+/g, " ").trim();
|
||||
@@ -39,35 +46,73 @@ function normalizeReply(text: string): string {
|
||||
export class OllamaLlmService implements LlmService {
|
||||
constructor(private readonly config: AssistantRuntimeConfig) {}
|
||||
|
||||
async warmup(): Promise<void> {
|
||||
const url = new URL("/api/tags", this.config.OLLAMA_BASE_URL);
|
||||
let response: Response;
|
||||
|
||||
try {
|
||||
response = await fetch(url);
|
||||
} catch {
|
||||
throw new Error(
|
||||
`Ollama 서버에 연결할 수 없습니다. ${this.config.OLLAMA_BASE_URL} 확인 후 Ollama 앱 또는 \`ollama serve\` 를 실행하고 \`ollama pull ${this.config.OLLAMA_MODEL}\` 까지 끝내 주세요.`,
|
||||
);
|
||||
}
|
||||
|
||||
const body = (await response.json().catch(() => ({}))) as OllamaTagsResponse & { error?: string };
|
||||
if (!response.ok) {
|
||||
throw new Error(body.error ?? `Ollama 상태 확인 실패: HTTP ${response.status}`);
|
||||
}
|
||||
|
||||
const models = body.models ?? [];
|
||||
const exists = models.some((model) => {
|
||||
const name = model.name?.trim();
|
||||
const alias = model.model?.trim();
|
||||
return name === this.config.OLLAMA_MODEL || alias === this.config.OLLAMA_MODEL;
|
||||
});
|
||||
|
||||
if (!exists) {
|
||||
throw new Error(
|
||||
`Ollama 모델 ${this.config.OLLAMA_MODEL} 이 없습니다. \`ollama pull ${this.config.OLLAMA_MODEL}\` 를 먼저 실행해 주세요.`,
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
async generateReply(memory: ConversationMemory, utterance: UserUtterance): Promise<string> {
|
||||
const url = new URL("/api/chat", this.config.OLLAMA_BASE_URL);
|
||||
const response = await fetch(url, {
|
||||
method: "POST",
|
||||
headers: {
|
||||
"Content-Type": "application/json",
|
||||
},
|
||||
body: JSON.stringify({
|
||||
model: this.config.OLLAMA_MODEL,
|
||||
messages: [
|
||||
{
|
||||
role: "system",
|
||||
content: ASSISTANT_INSTRUCTIONS,
|
||||
},
|
||||
{
|
||||
role: "user",
|
||||
content: memory.buildPrompt(utterance),
|
||||
},
|
||||
],
|
||||
think: false,
|
||||
stream: false,
|
||||
keep_alive: this.config.OLLAMA_KEEP_ALIVE,
|
||||
options: {
|
||||
num_ctx: this.config.OLLAMA_NUM_CTX,
|
||||
temperature: 0.4,
|
||||
num_predict: 120,
|
||||
let response: Response;
|
||||
try {
|
||||
response = await fetch(url, {
|
||||
method: "POST",
|
||||
headers: {
|
||||
"Content-Type": "application/json",
|
||||
},
|
||||
}),
|
||||
});
|
||||
body: JSON.stringify({
|
||||
model: this.config.OLLAMA_MODEL,
|
||||
messages: [
|
||||
{
|
||||
role: "system",
|
||||
content: ASSISTANT_INSTRUCTIONS,
|
||||
},
|
||||
{
|
||||
role: "user",
|
||||
content: memory.buildPrompt(utterance),
|
||||
},
|
||||
],
|
||||
think: false,
|
||||
stream: false,
|
||||
keep_alive: this.config.OLLAMA_KEEP_ALIVE,
|
||||
options: {
|
||||
num_ctx: this.config.OLLAMA_NUM_CTX,
|
||||
temperature: 0.4,
|
||||
num_predict: 120,
|
||||
},
|
||||
}),
|
||||
});
|
||||
} catch {
|
||||
throw new Error(
|
||||
`Ollama 서버에 연결할 수 없습니다. ${this.config.OLLAMA_BASE_URL} 확인 후 Ollama 앱 또는 \`ollama serve\` 를 실행해 주세요.`,
|
||||
);
|
||||
}
|
||||
|
||||
const body = (await response.json().catch(() => ({}))) as OllamaChatResponse;
|
||||
|
||||
|
||||
Reference in New Issue
Block a user