Overview
Integrate AI capabilities into custom backoffice elements.
Umbraco.AI provides TypeScript APIs for integrating AI capabilities into custom backoffice elements. The frontend APIs work within the Umbraco backoffice context.
Package
The AI APIs are exported from the Umbraco.AI client package:
import { UaiChatController, UaiChatMessage, UaiChatResult } from "@umbraco-ai/core";Quick Start
import { LitElement, html } from "lit";
import { customElement, state } from "lit/decorators.js";
import { UaiChatController, UaiChatMessage, UaiChatResult } from "@umbraco-ai/core";
@customElement("my-ai-element")
export class MyAIElement extends LitElement {
#chatController = new UaiChatController(this);
@state()
private _response?: string;
@state()
private _loading = false;
async #handleSubmit() {
this._loading = true;
const messages: UaiChatMessage[] = [{ role: "user", content: "Hello, can you help me?" }];
const { data, error } = await this.#chatController.complete(messages);
if (data) {
this._response = data.message.content;
} else {
console.error("Chat error:", error);
}
this._loading = false;
}
render() {
return html`
<button @click=${this.#handleSubmit} ?disabled=${this._loading}>
${this._loading ? "Loading..." : "Ask AI"}
</button>
${this._response ? html`<p>${this._response}</p>` : ""}
`;
}
}Key Concepts
Controller Pattern
UaiChatController follows the Umbraco backoffice controller pattern. It requires a controller host (typically the element itself) for lifecycle management:
The controller handles:
API communication with the Management API
Request lifecycle management
Automatic cleanup on element disconnect
Profile Selection
Specify which AI profile to use via profileIdOrAlias:
Cancellation
Pass an AbortSignal to cancel requests:
In This Section
Chat ControllerEmbeddings ControllerSpeech-to-Text ControllerDecision ControllerExtraction ControllerTool ControllerChat RepositoryTypesLast updated
Was this helpful?