2024-09-28 21:21:42 +02:00
|
|
|
from enum import Enum
|
|
|
|
from typing import Optional, Union
|
|
|
|
|
|
|
|
from pydantic import BaseModel, Field, field_validator
|
|
|
|
|
|
|
|
from ..base import FrigateBaseModel
|
|
|
|
from ..env import EnvString
|
|
|
|
|
|
|
|
__all__ = ["GenAIConfig", "GenAICameraConfig", "GenAIProviderEnum"]
|
|
|
|
|
|
|
|
|
|
|
|
class GenAIProviderEnum(str, Enum):
|
|
|
|
openai = "openai"
|
2024-10-01 21:57:40 +02:00
|
|
|
azure_openai = "azure_openai"
|
2024-09-28 21:21:42 +02:00
|
|
|
gemini = "gemini"
|
|
|
|
ollama = "ollama"
|
|
|
|
|
|
|
|
|
|
|
|
# uses BaseModel because some global attributes are not available at the camera level
|
|
|
|
class GenAICameraConfig(BaseModel):
|
|
|
|
enabled: bool = Field(default=False, title="Enable GenAI for camera.")
|
2024-09-30 23:54:53 +02:00
|
|
|
use_snapshot: bool = Field(
|
|
|
|
default=False, title="Use snapshots for generating descriptions."
|
|
|
|
)
|
2024-09-28 21:21:42 +02:00
|
|
|
prompt: str = Field(
|
|
|
|
default="Describe the {label} in the sequence of images with as much detail as possible. Do not describe the background.",
|
|
|
|
title="Default caption prompt.",
|
|
|
|
)
|
|
|
|
object_prompts: dict[str, str] = Field(
|
|
|
|
default_factory=dict, title="Object specific prompts."
|
|
|
|
)
|
|
|
|
|
|
|
|
objects: Union[str, list[str]] = Field(
|
|
|
|
default_factory=list,
|
|
|
|
title="List of objects to run generative AI for.",
|
|
|
|
)
|
|
|
|
required_zones: Union[str, list[str]] = Field(
|
|
|
|
default_factory=list,
|
|
|
|
title="List of required zones to be entered in order to run generative AI.",
|
|
|
|
)
|
|
|
|
|
|
|
|
@field_validator("required_zones", mode="before")
|
|
|
|
@classmethod
|
|
|
|
def validate_required_zones(cls, v):
|
|
|
|
if isinstance(v, str) and "," not in v:
|
|
|
|
return [v]
|
|
|
|
|
|
|
|
return v
|
|
|
|
|
|
|
|
|
|
|
|
class GenAIConfig(FrigateBaseModel):
|
|
|
|
enabled: bool = Field(default=False, title="Enable GenAI.")
|
|
|
|
prompt: str = Field(
|
|
|
|
default="Describe the {label} in the sequence of images with as much detail as possible. Do not describe the background.",
|
|
|
|
title="Default caption prompt.",
|
|
|
|
)
|
|
|
|
object_prompts: dict[str, str] = Field(
|
|
|
|
default_factory=dict, title="Object specific prompts."
|
|
|
|
)
|
|
|
|
|
|
|
|
api_key: Optional[EnvString] = Field(default=None, title="Provider API key.")
|
|
|
|
base_url: Optional[str] = Field(default=None, title="Provider base url.")
|
|
|
|
model: str = Field(default="gpt-4o", title="GenAI model.")
|
|
|
|
provider: GenAIProviderEnum = Field(
|
|
|
|
default=GenAIProviderEnum.openai, title="GenAI provider."
|
|
|
|
)
|