Add tests for LlmSession and LlmSessionCaptionsAndKeywords in both integration and mocked scenarios (#12)
Tests / test_and_lint (push) Successful in 13s

Reviewed-on: #12
Co-authored-by: Mathias Scheider <mathias2784@web.de>
This commit was merged in pull request #12.
This commit is contained in:
2026-10-02 09:21:38 +00:00
committed by mathias2784
parent 906707de55
commit ce2da9d953
9 changed files with 994 additions and 29 deletions
+35 -2
View File
@@ -3,6 +3,7 @@ import re
import openai
from openai import OpenAI
from utils.misc import readlines
from photools3.models import (
LlmMessageContent,
@@ -12,7 +13,7 @@ from photools3.models import (
LlmRequestMessage,
)
from photools3.photo import Photo
from photools3.settings import Settings
from photools3.settings import KEYWORDS_LIST_PATH, Settings
settings = Settings() # type: ignore[reportCallIssue]
@@ -21,7 +22,9 @@ class LlmSession:
base_url: str = str(settings.llm_api_base_url)
api_key: str = settings.llm_api_key
def __init__(self, system_prompt: str, temperature: float = 1.0):
def __init__(
self, system_prompt: str, temperature: float = settings.llm_default_temperature
):
self._models: list = []
self.system_prompt = system_prompt
@@ -110,3 +113,33 @@ class LlmSession:
),
],
)
class LlmSessionCaptionsAndKeywords(LlmSession):
keywords_list = ",".join([s.strip() for s in readlines(str(KEYWORDS_LIST_PATH))])
def __init__(self, temperature: float = settings.llm_default_temperature):
super().__init__(
system_prompt=(
"# System prompt\n"
"\n"
"You are a helpful photographer's assistant that helps to write **captions** and **keywords** to photos:\n"
"- Answer in JSON format so it can easily be integrated in automatic workflows.\n"
" - JSON format is `{'caption': '...', 'tags': ['tag1', 'tag2', ...]}`.\n"
" - Do not add any other text outside of this JSON format!\n"
"- When writing captions, ONLY describe what is visible on the photo. Do not interpret!\n"
"- Only use keywords from the keyword list, appended below.\n"
"\n"
"## Keyword list"
"\n"
f"{self.keywords_list}"
),
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,
)
+3
View File
@@ -8,6 +8,8 @@ SRC_DIR = PHOTOOLS3_DIR.parent
ROOT_DIR = SRC_DIR.parent
EXAMPLES_DIR = ROOT_DIR / "examples"
DNG_EXAMPLES_DIR = EXAMPLES_DIR / "dng"
CONFIG_DIR = ROOT_DIR / "config"
KEYWORDS_LIST_PATH = CONFIG_DIR / "keywords.txt"
class Settings(BaseSettings):
@@ -15,3 +17,4 @@ class Settings(BaseSettings):
llm_api_base_url: Url
llm_api_key: str
llm_default_temperature: float