mirror of
https://github.com/blakeblackshear/frigate.git
synced 2024-11-21 19:07:46 +01:00
36cbffcc5e
* Initial re-implementation of semantic search * put docker-compose back and make reindex match docs * remove debug code and fix import * fix docs * manually build pysqlite3 as binaries are only available for x86-64 * update comment in build_pysqlite3.sh * only embed objects * better error handling when genai fails * ask ollama to pull requested model at startup * update ollama docs * address some PR review comments * fix lint * use IPC to write description, update docs for reindex * remove gemini-pro-vision from docs as it will be unavailable soon * fix OpenAI doc available models * fix api error in gemini and metadata for embeddings
52 lines
1.7 KiB
Python
52 lines
1.7 KiB
Python
"""OpenAI Provider for Frigate AI."""
|
|
|
|
import base64
|
|
from typing import Optional
|
|
|
|
from httpx import TimeoutException
|
|
from openai import OpenAI
|
|
|
|
from frigate.config import GenAIProviderEnum
|
|
from frigate.genai import GenAIClient, register_genai_provider
|
|
|
|
|
|
@register_genai_provider(GenAIProviderEnum.openai)
|
|
class OpenAIClient(GenAIClient):
|
|
"""Generative AI client for Frigate using OpenAI."""
|
|
|
|
provider: OpenAI
|
|
|
|
def _init_provider(self):
|
|
"""Initialize the client."""
|
|
return OpenAI(api_key=self.genai_config.api_key)
|
|
|
|
def _send(self, prompt: str, images: list[bytes]) -> Optional[str]:
|
|
"""Submit a request to OpenAI."""
|
|
encoded_images = [base64.b64encode(image).decode("utf-8") for image in images]
|
|
try:
|
|
result = self.provider.chat.completions.create(
|
|
model=self.genai_config.model,
|
|
messages=[
|
|
{
|
|
"role": "user",
|
|
"content": [
|
|
{
|
|
"type": "image_url",
|
|
"image_url": {
|
|
"url": f"data:image/jpeg;base64,{image}",
|
|
"detail": "low",
|
|
},
|
|
}
|
|
for image in encoded_images
|
|
]
|
|
+ [prompt],
|
|
},
|
|
],
|
|
timeout=self.timeout,
|
|
)
|
|
except TimeoutException:
|
|
return None
|
|
if len(result.choices) > 0:
|
|
return result.choices[0].message.content.strip()
|
|
return None
|