Llm session refactor #19
@@ -29,7 +29,11 @@ jobs:
|
||||
fi
|
||||
- name: Run pytest
|
||||
run: uv run pytest .
|
||||
- name: Check formatting (black)
|
||||
run: uv run black --check . --extend-exclude .venv
|
||||
- name: Lint (ruff)
|
||||
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
|
||||
|
||||
@@ -34,6 +34,10 @@ utils = { index = "gitea" }
|
||||
[tool.black]
|
||||
target-version = [ "py314" ]
|
||||
|
||||
[[tool.mypy.overrides]]
|
||||
module = ["exiftool", "exiftool.*", "utils", "utils.*"]
|
||||
ignore_missing_imports = true
|
||||
|
||||
[[tool.uv.index]]
|
||||
name = "gitea"
|
||||
url = "https://gitea.schneiderserver.site/api/packages/mathias2784/pypi/simple"
|
||||
|
||||
@@ -4,7 +4,7 @@ import re
|
||||
|
||||
import openai
|
||||
from openai import OpenAI
|
||||
from pydantic import ValidationError
|
||||
from pydantic import BaseModel, ValidationError
|
||||
|
||||
from photools3.models import (
|
||||
LlmMessageContent,
|
||||
@@ -47,7 +47,8 @@ def _correction_prompt(
|
||||
class LlmSession:
|
||||
base_url: str = str(settings.llm_api_base_url)
|
||||
api_key: str = settings.llm_api_key
|
||||
response_model = LlmResponseGeneral
|
||||
response_model: type[BaseModel] = LlmResponseGeneral
|
||||
prompt: str = ""
|
||||
|
||||
def __init__(
|
||||
self, system_prompt: str, temperature: float = settings.llm_default_temperature
|
||||
@@ -81,15 +82,15 @@ class LlmSession:
|
||||
|
||||
def call(
|
||||
self,
|
||||
prompt: str,
|
||||
photos: list[Photo],
|
||||
model_id=None,
|
||||
max_retries: int = 2,
|
||||
prompt: str | None = None,
|
||||
):
|
||||
if not model_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
|
||||
for attempt in range(max_retries + 1):
|
||||
@@ -156,6 +157,7 @@ class LlmSession:
|
||||
|
||||
class LlmSessionCaptionsAndKeywords(LlmSession):
|
||||
response_model = LlmResponseCaptionsAndKeywords
|
||||
prompt = "Give me a caption and keyword for the following photo(s)"
|
||||
|
||||
def __init__(self, temperature: float = settings.llm_default_temperature):
|
||||
super().__init__(
|
||||
@@ -168,16 +170,10 @@ class LlmSessionCaptionsAndKeywords(LlmSession):
|
||||
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):
|
||||
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):
|
||||
super().__init__(
|
||||
@@ -189,10 +185,3 @@ class LlmSessionQuality(LlmSession):
|
||||
),
|
||||
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,
|
||||
)
|
||||
|
||||
@@ -22,4 +22,4 @@ class Settings(BaseSettings):
|
||||
metadata_prefix: str
|
||||
|
||||
|
||||
settings = Settings() # type: ignore[reportCallIssue]
|
||||
settings = Settings() # type: ignore[call-arg]
|
||||
|
||||
Reference in New Issue
Block a user