mirror of
https://github.com/blakeblackshear/frigate.git
synced 2026-04-05 22:57:40 +03:00
Simplify using correect types
This commit is contained in:
parent
42560de302
commit
7e41e1ecef
@ -23,10 +23,10 @@ class OpenAIClient(GenAIClient):
|
|||||||
def _init_provider(self) -> AzureOpenAI | None:
|
def _init_provider(self) -> AzureOpenAI | None:
|
||||||
"""Initialize the client."""
|
"""Initialize the client."""
|
||||||
try:
|
try:
|
||||||
parsed_url = urlparse(self.genai_config.base_url)
|
parsed_url = urlparse(self.genai_config.base_url or "")
|
||||||
query_params = parse_qs(parsed_url.query) # type: ignore[type-var]
|
query_params = parse_qs(parsed_url.query)
|
||||||
api_version = query_params.get("api-version", [None])[0]
|
api_version = query_params.get("api-version", [None])[0]
|
||||||
azure_endpoint = f"{parsed_url.scheme}://{parsed_url.netloc}/" # type: ignore[str-bytes-safe]
|
azure_endpoint = f"{parsed_url.scheme}://{parsed_url.netloc}/"
|
||||||
|
|
||||||
if not api_version:
|
if not api_version:
|
||||||
logger.warning("Azure OpenAI url is missing API version.")
|
logger.warning("Azure OpenAI url is missing API version.")
|
||||||
@ -36,7 +36,7 @@ class OpenAIClient(GenAIClient):
|
|||||||
logger.warning("Error parsing Azure OpenAI url: %s", str(e))
|
logger.warning("Error parsing Azure OpenAI url: %s", str(e))
|
||||||
return None
|
return None
|
||||||
|
|
||||||
return AzureOpenAI( # type: ignore[call-overload,no-any-return]
|
return AzureOpenAI(
|
||||||
api_key=self.genai_config.api_key,
|
api_key=self.genai_config.api_key,
|
||||||
api_version=api_version,
|
api_version=api_version,
|
||||||
azure_endpoint=azure_endpoint,
|
azure_endpoint=azure_endpoint,
|
||||||
|
|||||||
@ -6,6 +6,7 @@ from typing import Any, AsyncGenerator, Optional
|
|||||||
|
|
||||||
from google import genai
|
from google import genai
|
||||||
from google.genai import errors, types
|
from google.genai import errors, types
|
||||||
|
from google.genai.types import FunctionCallingConfigMode
|
||||||
|
|
||||||
from frigate.config import GenAIProviderEnum
|
from frigate.config import GenAIProviderEnum
|
||||||
from frigate.genai import GenAIClient, register_genai_provider
|
from frigate.genai import GenAIClient, register_genai_provider
|
||||||
@ -78,7 +79,9 @@ class GeminiClient(GenAIClient):
|
|||||||
return None
|
return None
|
||||||
|
|
||||||
try:
|
try:
|
||||||
description = response.text.strip() # type: ignore[union-attr]
|
if response.text is None:
|
||||||
|
return None
|
||||||
|
description = response.text.strip()
|
||||||
except (ValueError, AttributeError):
|
except (ValueError, AttributeError):
|
||||||
# No description was generated
|
# No description was generated
|
||||||
return None
|
return None
|
||||||
@ -110,10 +113,10 @@ class GeminiClient(GenAIClient):
|
|||||||
# Map roles to Gemini format
|
# Map roles to Gemini format
|
||||||
if role == "system":
|
if role == "system":
|
||||||
# Gemini doesn't have system role, prepend to first user message
|
# Gemini doesn't have system role, prepend to first user message
|
||||||
if gemini_messages and gemini_messages[0].role == "user":
|
if gemini_messages and gemini_messages[0].role == "user" and gemini_messages[0].parts:
|
||||||
gemini_messages[0].parts[ # type: ignore[index]
|
gemini_messages[0].parts[
|
||||||
0
|
0
|
||||||
].text = f"{content}\n\n{gemini_messages[0].parts[0].text}" # type: ignore[index]
|
].text = f"{content}\n\n{gemini_messages[0].parts[0].text}"
|
||||||
else:
|
else:
|
||||||
gemini_messages.append(
|
gemini_messages.append(
|
||||||
types.Content(
|
types.Content(
|
||||||
@ -171,15 +174,15 @@ class GeminiClient(GenAIClient):
|
|||||||
if tool_choice:
|
if tool_choice:
|
||||||
if tool_choice == "none":
|
if tool_choice == "none":
|
||||||
tool_config = types.ToolConfig(
|
tool_config = types.ToolConfig(
|
||||||
function_calling_config=types.FunctionCallingConfig(mode="NONE") # type: ignore[arg-type]
|
function_calling_config=types.FunctionCallingConfig(mode=FunctionCallingConfigMode.NONE)
|
||||||
)
|
)
|
||||||
elif tool_choice == "auto":
|
elif tool_choice == "auto":
|
||||||
tool_config = types.ToolConfig(
|
tool_config = types.ToolConfig(
|
||||||
function_calling_config=types.FunctionCallingConfig(mode="AUTO") # type: ignore[arg-type]
|
function_calling_config=types.FunctionCallingConfig(mode=FunctionCallingConfigMode.AUTO)
|
||||||
)
|
)
|
||||||
elif tool_choice == "required":
|
elif tool_choice == "required":
|
||||||
tool_config = types.ToolConfig(
|
tool_config = types.ToolConfig(
|
||||||
function_calling_config=types.FunctionCallingConfig(mode="ANY") # type: ignore[arg-type]
|
function_calling_config=types.FunctionCallingConfig(mode=FunctionCallingConfigMode.ANY)
|
||||||
)
|
)
|
||||||
|
|
||||||
# Build request config
|
# Build request config
|
||||||
@ -307,10 +310,10 @@ class GeminiClient(GenAIClient):
|
|||||||
# Map roles to Gemini format
|
# Map roles to Gemini format
|
||||||
if role == "system":
|
if role == "system":
|
||||||
# Gemini doesn't have system role, prepend to first user message
|
# Gemini doesn't have system role, prepend to first user message
|
||||||
if gemini_messages and gemini_messages[0].role == "user":
|
if gemini_messages and gemini_messages[0].role == "user" and gemini_messages[0].parts:
|
||||||
gemini_messages[0].parts[ # type: ignore[index]
|
gemini_messages[0].parts[
|
||||||
0
|
0
|
||||||
].text = f"{content}\n\n{gemini_messages[0].parts[0].text}" # type: ignore[index]
|
].text = f"{content}\n\n{gemini_messages[0].parts[0].text}"
|
||||||
else:
|
else:
|
||||||
gemini_messages.append(
|
gemini_messages.append(
|
||||||
types.Content(
|
types.Content(
|
||||||
@ -368,15 +371,15 @@ class GeminiClient(GenAIClient):
|
|||||||
if tool_choice:
|
if tool_choice:
|
||||||
if tool_choice == "none":
|
if tool_choice == "none":
|
||||||
tool_config = types.ToolConfig(
|
tool_config = types.ToolConfig(
|
||||||
function_calling_config=types.FunctionCallingConfig(mode="NONE") # type: ignore[arg-type]
|
function_calling_config=types.FunctionCallingConfig(mode=FunctionCallingConfigMode.NONE)
|
||||||
)
|
)
|
||||||
elif tool_choice == "auto":
|
elif tool_choice == "auto":
|
||||||
tool_config = types.ToolConfig(
|
tool_config = types.ToolConfig(
|
||||||
function_calling_config=types.FunctionCallingConfig(mode="AUTO") # type: ignore[arg-type]
|
function_calling_config=types.FunctionCallingConfig(mode=FunctionCallingConfigMode.AUTO)
|
||||||
)
|
)
|
||||||
elif tool_choice == "required":
|
elif tool_choice == "required":
|
||||||
tool_config = types.ToolConfig(
|
tool_config = types.ToolConfig(
|
||||||
function_calling_config=types.FunctionCallingConfig(mode="ANY") # type: ignore[arg-type]
|
function_calling_config=types.FunctionCallingConfig(mode=FunctionCallingConfigMode.ANY)
|
||||||
)
|
)
|
||||||
|
|
||||||
# Build request config
|
# Build request config
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user