svelte-chatbot-test / src /lib /chatCompletion.ts
ferrywuai's picture
Replace manual token input with Hugging Face OAuth login
6493ff0
import { get } from "svelte/store";
import { chatParams } from "../stores/chatParams";
import { messages } from "../stores/messages";
import { uiParams } from "../stores/uiParams";
import { hfClient } from "./hfClient";
import { runTypingAnimation } from "./typingAnimation";
export async function sendChatMessage(userInput: string) {
const client = get(hfClient);
const chat = get(chatParams);
if (!client) {
messages.addMessage("assistant", "Error: Please login Hugging Face.");
return;
}
try {
const response = await client.chatCompletion({
model: "openai/gpt-oss-20b",
messages: [
{ role: "system", content: chat.system_prompt },
{ role: "user", content: userInput },
],
temperature: chat.temperature,
top_p: chat.top_p,
max_tokens: chat.max_tokens,
});
const reply = response.choices?.[0]?.message?.content || "[No response]";
await runTypingAnimation(reply, get(uiParams).typingAnimation);
messages.addMessage("assistant", reply);
} catch (err) {
const errorMsg =
err instanceof Error ? err.message : "Unknown error occurred";
messages.addMessage("assistant", `Error: ${errorMsg}`);
}
}