smart tdee
This commit is contained in:
@@ -34,6 +34,16 @@ class LLMClient:
|
||||
finally:
|
||||
db.close()
|
||||
|
||||
def _vision_model_runtime(self) -> str:
|
||||
from app.db.base import SessionLocal
|
||||
from app.settings.service import SettingsService
|
||||
|
||||
db = SessionLocal()
|
||||
try:
|
||||
return str(SettingsService(db).get_effective("openrouter_vision_model"))
|
||||
finally:
|
||||
db.close()
|
||||
|
||||
@property
|
||||
def model(self) -> str:
|
||||
return self._runtime()[0]
|
||||
@@ -46,6 +56,10 @@ class LLMClient:
|
||||
def reasoning_effort(self) -> str:
|
||||
return self._runtime()[2]
|
||||
|
||||
@property
|
||||
def vision_model(self) -> str:
|
||||
return self._vision_model_runtime()
|
||||
|
||||
def _reasoning_extra_body(self) -> dict[str, Any] | None:
|
||||
if not self.reasoning_effort:
|
||||
return None
|
||||
@@ -272,6 +286,43 @@ class LLMClient:
|
||||
|
||||
return result
|
||||
|
||||
async def complete_vision(
|
||||
self,
|
||||
messages: list[dict[str, Any]],
|
||||
*,
|
||||
temperature: float = 0.1,
|
||||
model: str | None = None,
|
||||
) -> dict[str, Any]:
|
||||
use_model = model or self.vision_model
|
||||
kwargs: dict[str, Any] = {
|
||||
"model": use_model,
|
||||
"messages": messages,
|
||||
"temperature": temperature,
|
||||
"extra_body": {"reasoning": {"effort": "none", "exclude": True}},
|
||||
}
|
||||
response = await self.client.chat.completions.create(**kwargs)
|
||||
usage = getattr(response, "usage", None)
|
||||
usage_dict: dict[str, Any] = {}
|
||||
if usage is not None:
|
||||
usage_dict = {
|
||||
"prompt_tokens": getattr(usage, "prompt_tokens", None),
|
||||
"completion_tokens": getattr(usage, "completion_tokens", None),
|
||||
"total_tokens": getattr(usage, "total_tokens", None),
|
||||
}
|
||||
logger.info(
|
||||
"LLM vision usage: prompt=%s completion=%s total=%s model=%s",
|
||||
usage_dict.get("prompt_tokens"),
|
||||
usage_dict.get("completion_tokens"),
|
||||
usage_dict.get("total_tokens"),
|
||||
use_model,
|
||||
)
|
||||
message = response.choices[0].message
|
||||
return {
|
||||
"content": message.content or "",
|
||||
"model": use_model,
|
||||
"usage": usage_dict,
|
||||
}
|
||||
|
||||
@staticmethod
|
||||
def parse_tool_arguments(arguments: str) -> dict[str, Any]:
|
||||
if not arguments:
|
||||
|
||||
Reference in New Issue
Block a user