Llm session refactor #19

Merged
mathias2784 merged 5 commits from llm-session-refactor into master 2026-10-18 06:29:38 +00:00
4 changed files with 18 additions and 21 deletions
Showing only changes of commit 5c2cb07607 - Show all commits
+6 -2
View File
@@ -29,7 +29,11 @@ jobs:
fi fi
- name: Run pytest - name: Run pytest
run: uv run pytest . run: uv run pytest .
- name: Check formatting (black)
run: uv run black --check . --extend-exclude .venv
- name: Lint (ruff) - name: Lint (ruff)
run: uv run ruff check . run: uv run ruff check .
- name: Typing (mypy)
run: uv run mypy .
- name: Check formatting (black)
run: uv run black --check . --extend-exclude .venv
- name: Check formatting (black)
run: uv run black --check . --extend-exclude .venv
+4
View File
@@ -34,6 +34,10 @@ utils = { index = "gitea" }
[tool.black] [tool.black]
target-version = [ "py314" ] target-version = [ "py314" ]
[[tool.mypy.overrides]]
module = ["exiftool", "exiftool.*", "utils", "utils.*"]
ignore_missing_imports = true
[[tool.uv.index]] [[tool.uv.index]]
name = "gitea" name = "gitea"
url = "https://gitea.schneiderserver.site/api/packages/mathias2784/pypi/simple" url = "https://gitea.schneiderserver.site/api/packages/mathias2784/pypi/simple"
+7 -18
View File
@@ -4,7 +4,7 @@ import re
import openai import openai
from openai import OpenAI from openai import OpenAI
from pydantic import ValidationError from pydantic import BaseModel, ValidationError
from photools3.models import ( from photools3.models import (
LlmMessageContent, LlmMessageContent,
@@ -47,7 +47,8 @@ def _correction_prompt(
class LlmSession: class LlmSession:
base_url: str = str(settings.llm_api_base_url) base_url: str = str(settings.llm_api_base_url)
api_key: str = settings.llm_api_key api_key: str = settings.llm_api_key
response_model = LlmResponseGeneral response_model: type[BaseModel] = LlmResponseGeneral
prompt: str = ""
def __init__( def __init__(
self, system_prompt: str, temperature: float = settings.llm_default_temperature self, system_prompt: str, temperature: float = settings.llm_default_temperature
@@ -81,15 +82,15 @@ class LlmSession:
def call( def call(
self, self,
prompt: str,
photos: list[Photo], photos: list[Photo],
model_id=None, model_id=None,
max_retries: int = 2, max_retries: int = 2,
prompt: str | None = None,
): ):
if not model_id: if not model_id:
model_id = self.models[0].id model_id = self.models[0].id
request = self._build_request(prompt, photos, model_id) request = self._build_request(prompt or self.prompt, photos, model_id)
last_error = None last_error = None
for attempt in range(max_retries + 1): for attempt in range(max_retries + 1):
@@ -156,6 +157,7 @@ class LlmSession:
class LlmSessionCaptionsAndKeywords(LlmSession): class LlmSessionCaptionsAndKeywords(LlmSession):
response_model = LlmResponseCaptionsAndKeywords response_model = LlmResponseCaptionsAndKeywords
prompt = "Give me a caption and keyword for the following photo(s)"
def __init__(self, temperature: float = settings.llm_default_temperature): def __init__(self, temperature: float = settings.llm_default_temperature):
super().__init__( super().__init__(
@@ -168,16 +170,10 @@ class LlmSessionCaptionsAndKeywords(LlmSession):
temperature=temperature, temperature=temperature,
) )
def call(self, photos: list[Photo], model_id=None):
return super().call(
prompt="Give me a caption and keyword for the following photo(s)",
photos=photos,
model_id=model_id,
)
class LlmSessionQuality(LlmSession): class LlmSessionQuality(LlmSession):
response_model = LlmResponseQuality response_model = LlmResponseQuality
prompt = "Assess the quality of the following photos to help me decide which ones to sort out"
def __init__(self, temperature: float = settings.llm_default_temperature): def __init__(self, temperature: float = settings.llm_default_temperature):
super().__init__( super().__init__(
@@ -189,10 +185,3 @@ class LlmSessionQuality(LlmSession):
), ),
temperature=temperature, temperature=temperature,
) )
def call(self, photos: list[Photo], model_id=None):
return super().call(
prompt="Assess the quality of the following photos to help me decide which ones to sort out",
photos=photos,
model_id=model_id,
)
+1 -1
View File
@@ -22,4 +22,4 @@ class Settings(BaseSettings):
metadata_prefix: str metadata_prefix: str
settings = Settings() # type: ignore[reportCallIssue] settings = Settings() # type: ignore[call-arg]