Dynamically enable/disable GenAI (#19139)

* config

* dispatcher and mqtt

* docs

* use config updater

* add switch to frontend
This commit is contained in:
Josh Hawkins 2025-07-14 07:58:43 -05:00 committed by GitHub
parent d97b451d12
commit 99885b4bdc
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
12 changed files with 109 additions and 1 deletions

View File

@ -27,6 +27,8 @@ cameras:
enabled: False
```
Generative AI can also be toggled dynamically for a camera via MQTT with the topic `frigate/<camera_name>/genai/set`. See the [MQTT documentation](/integrations/mqtt/#frigatecamera_namegenaiset).
## Ollama
:::warning

View File

@ -397,6 +397,14 @@ Topic to turn review detections for a camera on or off. Expected values are `ON`
Topic with current state of review detections for a camera. Published values are `ON` and `OFF`.
### `frigate/<camera_name>/genai/set`
Topic to turn generative AI for a camera on or off. Expected values are `ON` and `OFF`.
### `frigate/<camera_name>/genai/state`
Topic with current state of generative AI for a camera. Published values are `ON` and `OFF`.
### `frigate/<camera_name>/birdseye/set`
Topic to turn Birdseye for a camera on and off. Expected values are `ON` and `OFF`. Birdseye mode

View File

@ -75,6 +75,7 @@ class Dispatcher:
"birdseye_mode": self._on_birdseye_mode_command,
"review_alerts": self._on_alerts_command,
"review_detections": self._on_detections_command,
"genai": self._on_genai_command,
}
self._global_settings_handlers: dict[str, Callable] = {
"notifications": self._on_global_notification_command,
@ -207,6 +208,7 @@ class Dispatcher:
].onvif.autotracking.enabled,
"alerts": self.config.cameras[camera].review.alerts.enabled,
"detections": self.config.cameras[camera].review.detections.enabled,
"genai": self.config.cameras[camera].genai.enabled,
}
self.publish("camera_activity", json.dumps(camera_status))
@ -737,3 +739,28 @@ class Dispatcher:
review_settings,
)
self.publish(f"{camera_name}/review_detections/state", payload, retain=True)
def _on_genai_command(self, camera_name: str, payload: str) -> None:
"""Callback for GenAI topic."""
genai_settings = self.config.cameras[camera_name].genai
if payload == "ON":
if not self.config.cameras[camera_name].genai.enabled_in_config:
logger.error(
"GenAI must be enabled in the config to be turned on via MQTT."
)
return
if not genai_settings.enabled:
logger.info(f"Turning on GenAI for {camera_name}")
genai_settings.enabled = True
elif payload == "OFF":
if genai_settings.enabled:
logger.info(f"Turning off GenAI for {camera_name}")
genai_settings.enabled = False
self.config_updater.publish_update(
CameraConfigUpdateTopic(CameraConfigUpdateEnum.genai, camera_name),
genai_settings,
)
self.publish(f"{camera_name}/genai/state", payload, retain=True)

View File

@ -122,6 +122,11 @@ class MqttClient(Communicator): # type: ignore[misc]
"ON" if camera.review.detections.enabled_in_config else "OFF",
retain=True,
)
self.publish(
f"{camera_name}/genai/state",
"ON" if camera.genai.enabled_in_config else "OFF",
retain=True,
)
if self.config.notifications.enabled_in_config:
self.publish(
@ -215,6 +220,7 @@ class MqttClient(Communicator): # type: ignore[misc]
"birdseye_mode",
"review_alerts",
"review_detections",
"genai",
]
for name in self.config.cameras.keys():

View File

@ -58,6 +58,10 @@ class GenAICameraConfig(BaseModel):
title="What triggers to use to send frames to generative AI for a tracked object.",
)
enabled_in_config: Optional[bool] = Field(
default=None, title="Keep track of original state of generative AI."
)
@field_validator("required_zones", mode="before")
@classmethod
def validate_required_zones(cls, v):

View File

@ -17,6 +17,7 @@ class CameraConfigUpdateEnum(str, Enum):
birdseye = "birdseye"
detect = "detect"
enabled = "enabled"
genai = "genai"
motion = "motion" # includes motion and motion masks
notifications = "notifications"
objects = "objects"
@ -97,6 +98,8 @@ class CameraConfigUpdateSubscriber:
config.detect = updated_config
elif update_type == CameraConfigUpdateEnum.enabled:
config.enabled = updated_config
elif update_type == CameraConfigUpdateEnum.genai:
config.genai = updated_config
elif update_type == CameraConfigUpdateEnum.motion:
config.motion = updated_config
elif update_type == CameraConfigUpdateEnum.notifications:

View File

@ -606,6 +606,7 @@ class FrigateConfig(FrigateBaseModel):
camera_config.review.detections.enabled_in_config = (
camera_config.review.detections.enabled
)
camera_config.genai.enabled_in_config = camera_config.genai.enabled
# Add default filters
object_keys = camera_config.objects.track

View File

@ -100,6 +100,7 @@ class EmbeddingMaintainer(threading.Thread):
[
CameraConfigUpdateEnum.add,
CameraConfigUpdateEnum.remove,
CameraConfigUpdateEnum.genai,
CameraConfigUpdateEnum.semantic_search,
],
)

View File

@ -150,6 +150,10 @@
"title": "Streams",
"desc": "Disabling a camera completely stops Frigate's processing of this camera's streams. Detection, recording, and debugging will be unavailable.<br /> <em>Note: This does not disable go2rtc restreams.</em>"
},
"genai": {
"title": "Generative AI",
"desc": "Temporarily enable/disable Generative AI for this camera. When disabled, AI generated descriptions will not be requested for tracked objects on this camera."
},
"review": {
"title": "Review",
"desc": "Enable/disable alerts and detections for this camera. When disabled, no new review items will be generated.",

View File

@ -68,6 +68,7 @@ function useValue(): useValueReturn {
autotracking,
alerts,
detections,
genai,
} = state["config"];
cameraStates[`${name}/recordings/state`] = record ? "ON" : "OFF";
cameraStates[`${name}/enabled/state`] = enabled ? "ON" : "OFF";
@ -89,6 +90,7 @@ function useValue(): useValueReturn {
cameraStates[`${name}/review_detections/state`] = detections
? "ON"
: "OFF";
cameraStates[`${name}/genai/state`] = genai ? "ON" : "OFF";
});
setWsState((prevState) => ({
@ -276,6 +278,17 @@ export function useDetectionsState(camera: string): {
return { payload: payload as ToggleableSetting, send };
}
export function useGenAIState(camera: string): {
payload: ToggleableSetting;
send: (payload: ToggleableSetting, retain?: boolean) => void;
} {
const {
value: { payload },
send,
} = useWs(`${camera}/genai/state`, `${camera}/genai/set`);
return { payload: payload as ToggleableSetting, send };
}
export function usePtzCommand(camera: string): {
payload: string;
send: (payload: string, retain?: boolean) => void;

View File

@ -64,6 +64,7 @@ export interface FrigateCameraState {
autotracking: boolean;
alerts: boolean;
detections: boolean;
genai: boolean;
};
motion: boolean;
objects: ObjectType[];

View File

@ -29,7 +29,12 @@ import { cn } from "@/lib/utils";
import { Trans, useTranslation } from "react-i18next";
import { Switch } from "@/components/ui/switch";
import { Label } from "@/components/ui/label";
import { useAlertsState, useDetectionsState, useEnabledState } from "@/api/ws";
import {
useAlertsState,
useDetectionsState,
useEnabledState,
useGenAIState,
} from "@/api/ws";
import CameraEditForm from "@/components/settings/CameraEditForm";
import { LuPlus } from "react-icons/lu";
import {
@ -142,6 +147,9 @@ export default function CameraSettingsView({
const { payload: detectionsState, send: sendDetections } =
useDetectionsState(selectedCamera);
const { payload: genAIState, send: sendGenAI } =
useGenAIState(selectedCamera);
const handleCheckedChange = useCallback(
(isChecked: boolean) => {
if (!isChecked) {
@ -402,6 +410,36 @@ export default function CameraSettingsView({
</div>
</div>
</div>
{config?.genai?.enabled && (
<>
<Separator className="my-2 flex bg-secondary" />
<Heading as="h4" className="my-2">
<Trans ns="views/settings">camera.genai.title</Trans>
</Heading>
<div className="mb-5 mt-2 flex max-w-5xl flex-col gap-2 space-y-3 text-sm text-primary-variant">
<div className="flex flex-row items-center">
<Switch
id="alerts-enabled"
className="mr-3"
checked={genAIState == "ON"}
onCheckedChange={(isChecked) => {
sendGenAI(isChecked ? "ON" : "OFF");
}}
/>
<div className="space-y-0.5">
<Label htmlFor="genai-enabled">
<Trans>button.enabled</Trans>
</Label>
</div>
</div>
<div className="mt-3 text-sm text-muted-foreground">
<Trans ns="views/settings">camera.genai.desc</Trans>
</div>
</div>
</>
)}
<Separator className="my-2 flex bg-secondary" />