New Ultralytics chat.js bot (#22639)

This commit is contained in:
Glenn Jocher 2025-11-10 00:34:14 +00:00 committed by GitHub
parent e15d6f50dc
commit 2db0e79b25
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
3 changed files with 120 additions and 134 deletions

View File

@ -12,154 +12,120 @@ const applyTheme = (isDark) => {
);
};
// Sync widget theme with Material theme
const syncWidgetTheme = () => {
const isDark = document.body.getAttribute("data-md-color-scheme") === "slate";
document.documentElement.setAttribute(
"data-theme",
isDark ? "dark" : "light",
);
};
// Check and apply appropriate theme based on system/user preference
const checkTheme = () => {
const palette = JSON.parse(localStorage.getItem(".__palette") || "{}");
if (palette.index === 0) {
// Auto mode is selected
applyTheme(window.matchMedia("(prefers-color-scheme: dark)").matches);
syncWidgetTheme();
}
};
// Watch for system theme changes
window
.matchMedia("(prefers-color-scheme: dark)")
.addEventListener("change", checkTheme);
// Initialize theme handling on page load
document.addEventListener("DOMContentLoaded", () => {
// Watch for theme toggle changes
document
.getElementById("__palette_1")
?.addEventListener(
"change",
(e) => e.target.checked && setTimeout(checkTheme),
);
// Initial theme check
checkTheme();
syncWidgetTheme();
// Watch for system theme changes
window
.matchMedia("(prefers-color-scheme: dark)")
.addEventListener("change", checkTheme);
// Watch for theme toggle changes
document.getElementById("__palette_1")?.addEventListener("change", (e) => {
if (e.target.checked) setTimeout(checkTheme);
});
// Watch for Material theme changes and sync to widget
new MutationObserver(syncWidgetTheme).observe(document.body, {
attributes: true,
attributeFilter: ["data-md-color-scheme"],
});
});
// Inkeep --------------------------------------------------------------------------------------------------------------
// Ultralytics Chat Widget ---------------------------------------------------------------------------------------------
let ultralyticsChat = null;
document.addEventListener("DOMContentLoaded", () => {
const enableSearchBar = true;
ultralyticsChat = new UltralyticsChat({
apiUrl: "https://chat-885297101091.europe-west1.run.app/api/chat",
branding: {
name: "Ultralytics AI",
tagline: "Ask anything about Ultralytics, YOLO, and more",
logo: "https://cdn.prod.website-files.com/680a070c3b99253410dd3dcf/680a070c3b99253410dd3e13_logo.svg",
logomark:
"https://cdn.prod.website-files.com/646dd1f1a3703e451ba81ecc/64f727ed3fd1e5e074574368_ultralytics-favicon.png",
pillText: "Ask AI",
},
theme: {
primary: "#042AFF",
dark: "#111F68",
yellow: "#E1FF25",
text: "#0b0b0f",
},
welcome: {
title: "Hi!",
message:
"I'm an AI assistant trained on documentation, guides, and other content.<br>Ask me anything about Ultralytics.",
examples: [
"What's new in SAM3?",
"How can I get started with YOLO?",
"How does Enterprise Licensing work?",
],
},
ui: {
placeholder: "Ask anything…",
copyText: "Copy thread",
downloadText: "Download thread",
clearText: "New chat",
},
});
const inkeepScript = document.createElement("script");
inkeepScript.src =
"https://cdn.jsdelivr.net/npm/@inkeep/cxkit-js@0.5/dist/embed.js";
inkeepScript.type = "module";
inkeepScript.defer = true;
document.head.appendChild(inkeepScript);
// Add search bar to header
const headerElement = document.querySelector(".md-header__inner");
const searchContainer = headerElement?.querySelector(".md-header__source");
if (enableSearchBar) {
const containerDiv = document.createElement("div");
containerDiv.style.transform = "scale(0.7)";
containerDiv.style.transformOrigin = "left center";
if (headerElement && searchContainer) {
const searchBar = document.createElement("div");
searchBar.className = "ult-header-search";
searchBar.innerHTML = `
<button class="ult-search-button" title="Search documentation (⌘K)">
<svg width="24" height="24" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2">
<circle cx="11" cy="11" r="8"/>
<path d="m21 21-4.35-4.35"/>
</svg>
<span>Search</span>
</button>
`;
headerElement.insertBefore(searchBar, searchContainer);
const inkeepDiv = document.createElement("div");
inkeepDiv.id = "inkeepSearchBar";
containerDiv.appendChild(inkeepDiv);
const headerElement = document.querySelector(".md-header__inner");
const searchContainer = headerElement.querySelector(".md-header__source");
if (headerElement && searchContainer) {
headerElement.insertBefore(containerDiv, searchContainer);
}
searchBar
.querySelector(".ult-search-button")
.addEventListener("click", () => {
ultralyticsChat?.toggle(true, "search");
});
}
// Configuration object for Inkeep
const config = {
baseSettings: {
apiKey: "13dfec2e75982bc9bae3199a08e13b86b5fbacd64e9b2f89",
primaryBrandColor: "#E1FF25",
organizationDisplayName: "Ultralytics",
colorMode: {
enableSystem: true,
},
theme: {
styles: [
{
key: "main",
type: "link",
value: "/stylesheets/style.css",
},
{
key: "chat-button",
type: "style",
value: `
/* Light mode styling */
.ikp-chat-button__button {
background-color: #E1FF25;
color: #111F68;
}
/* Dark mode styling */
[data-theme="dark"] .ikp-chat-button__button {
background-color: #40434f;
color: #ffffff;
}
.ikp-chat-button__container {
position: fixed;
right: 1rem;
bottom: 3rem;
}
`,
},
],
},
},
searchSettings: {
placeholder: "Search",
},
aiChatSettings: {
chatSubjectName: "Ultralytics",
aiAssistantAvatar:
"https://storage.googleapis.com/organization-image-assets/ultralytics-botAvatarSrcUrl-1729379860806.svg",
exampleQuestions: [
"What's new in Ultralytics YOLO11?",
"How can I get started with Ultralytics HUB?",
"How does Ultralytics Enterprise Licensing work?",
],
getHelpOptions: [
{
name: "Ask on Ultralytics GitHub",
icon: {
builtIn: "FaGithub",
},
action: {
type: "open_link",
url: "https://github.com/ultralytics/ultralytics",
},
},
{
name: "Ask on Ultralytics Discourse",
icon: {
builtIn: "FaDiscourse",
},
action: {
type: "open_link",
url: "https://community.ultralytics.com/",
},
},
{
name: "Ask on Ultralytics Discord",
icon: {
builtIn: "FaDiscord",
},
action: {
type: "open_link",
url: "https://discord.com/invite/ultralytics",
},
},
],
},
};
// Initialize Inkeep widgets when script loads
inkeepScript.addEventListener("load", () => {
const widgetContainer = document.getElementById("inkeepSearchBar");
Inkeep.ChatButton(config);
widgetContainer && Inkeep.SearchBar("#inkeepSearchBar", config);
// Keyboard shortcuts
document.addEventListener("keydown", (e) => {
if (
(e.metaKey || e.ctrlKey) &&
e.key === "k" &&
!/input|textarea/i.test(e.target.tagName) &&
!e.target.isContentEditable
) {
e.preventDefault();
ultralyticsChat?.toggle(true, "search");
}
});
});

View File

@ -211,12 +211,31 @@ div.highlight {
}
/* MkDocs Ultralytics Plugin ---------------------------------------------------------------------------------------- */
/* Inkeep ----------------------------------------------------------------------------------------------------------- */
#inkeepSearchBar {
transition: all 0.2s ease-in-out;
/* Ultralytics Search Bar ------------------------------------------------------------------------------------------- */
.ult-header-search {
transform: scale(0.7);
transform-origin: left center;
margin-right: 0.5rem;
}
#inkeepSearchBar:hover {
.ult-search-button {
display: flex;
align-items: center;
gap: 0.5rem;
padding: 0.5rem 1rem;
background: var(--md-default-bg-color);
border: 1px solid var(--md-default-fg-color--lighter);
border-radius: 0.5rem;
color: var(--md-default-fg-color);
font-size: 0.9rem;
cursor: pointer;
transition: all 0.2s ease;
}
.ult-search-button:hover {
transform: scale(1.1);
filter: brightness(1.2);
border-color: var(--md-accent-fg-color);
}
/* Inkeep ----------------------------------------------------------------------------------------------------------- */
.ult-search-button svg {
flex-shrink: 0;
}
/* Ultralytics Search Bar ------------------------------------------------------------------------------------------- */

View File

@ -142,6 +142,7 @@ extra_css:
- stylesheets/style.css
extra_javascript:
- https://cdn.jsdelivr.net/gh/ultralytics/llm@main/js/chat.min.js
- javascript/extra.js
- javascript/giscus.js
- https://unpkg.com/tablesort@5.3.0/dist/tablesort.min.js