Skip to content

Instantly share code, notes, and snippets.

View thangman22's full-sized avatar

Warat Wongmaneekit thangman22

View GitHub Profile
// Check availability first
const availability = await LanguageModel.availability({
})
// Create session with monitoring
const session = await LanguageModel.create({
temperature: 0.7,
monitor(m) {
m.addEventListener('downloadprogress', (e) => {
console.log(`Downloaded ${e.loaded * 100}%`)
const initialPrompts = [
{
role: 'system',
content: `You are a knowledgeable Thai food and cuisine specialist. Your role is to help customers learn about authentic Thai dishes, ingredients, cooking methods, and food culture using accurate information from the FAQ data. Be enthusiastic about Thai cuisine and highlight its unique flavors and traditions.
Key guidelines:
- Use the provided FAQ information to give accurate, helpful answers about Thai food
- Emphasize the authentic flavors, spices, and cooking techniques of Thai cuisine
- Mention popular dishes like Pad Thai, Tom Yum, Green Curry, and Som Tam
- Highlight the balance of sweet, sour, spicy, and savory flavors in Thai cooking
import { pipeline, env } from '@huggingface/transformers';
import faqs from "@/faq.json";
const extractor = await pipeline("feature-extraction", "Xenova/all-MiniLM-L6-v2");
const embeddings = [];
for (const faq of faqs) {
const embedding = await extractor(faq.question + " " + faq.answer, {
pooling: "mean",
normalize: true,
import {
FilesetResolver,
LlmInference,
} from "https://cdn.jsdelivr.net/npm/@mediapipe/tasks-genai";
const genai = await FilesetResolver.forGenAiTasks(
"https://cdn.jsdelivr.net/npm/@mediapipe/tasks-genai@latest/wasm"
);
llmInference = await LlmInference.createFromOptions(genai, {
if (HTMLScriptElement.supports && HTMLScriptElement.supports('speculationrules')) {
console.log('Your browser supports the Speculation Rules API.');
}
if (HTMLScriptElement.supports?.('speculationrules')) {
const specScript = document.createElement('script');
specScript.type = 'speculationrules';
const specRules = {
prerender: [
{
urls: ['/next.html'],
},
],
};
<script type="speculationrules">
{
"prerender": [
{
"source": "list",
"urls": ["next.html", "next2.html"],
"eagerness": "moderate"
},
{
"where": {
@media all and (display-mode: picture-in-picture) {
......
}
pipWindow.addEventListener("pagehide", (event) => {
const chatScreen Container = document.querySelector("#chat-screen-container");
const pipChatScreen = event.target.querySelector("#chat-screen");
playerContainer.append(pipPlayer);
});
pipButton.addEventListener("click", async () => {
const chatScreen = document.querySelector("#chat-screen");
// Open a Picture-in-Picture window.
const pipWindow = await documentPictureInPicture.requestWindow();
// Copy style sheets over from the initial document
// so that the player looks the same.
[...document.styleSheets].forEach((styleSheet) => {
try {