diff options
| author | tanaka takashi <isazaka@gmail.com> | 2026-09-12 21:19:16 +0900 |
|---|---|---|
| committer | tanaka takashi <isazaka@gmail.com> | 2026-09-12 21:19:16 +0900 |
| commit | 796251e5b0da524584c9e81b8ad6e1dcc0931aa3 (patch) | |
| tree | 783d55ee9c557ebed5bdd32d99366f22f3fc1139 /src/mcp_sd15/config.py | |
| parent | 4f3f548b1b55d4c7477250fc3373b3bd6d0d397f (diff) | |
base prompt結合機能を追加
config.tomlの[prompt]でpositive/negativeのbase promptファイルを指定し、
MCP経由でLLMから渡されたプロンプトと結合してpipelineへ渡す機能を追加する。
プロンプトファイルは<root>/prompts/<file>に固定し、TOMLにはファイル名のみ
指定する。モデルパスと同じ方針でパストラバーサルを拒否する。
結合方式:
- positive: base_positive, user_positive の順でカンマ区切り
- negative: base_negative\nuser_negative の順で改行区切り
- baseが空の場合はuserプロンプトのみ(後方互換)
- [prompt]省略時はbase promptなし(後方互換)
結合場所はschema.pyのTextToImageRequest.create()に集約し、GuidedImageRequest
とInpaintImageRequestは内部でTextToImageRequest.create()を呼ぶため1箇所の
変更で3ツールすべてに適用される。MCP Tool入力にはbase promptを公開しない。
変更内容:
- config.py: PromptConfig dataclass、AppConfig.promptフィールド、
_read_promptヘルパー、load_configの[prompt]読込・validation追加。
positive_file/negative_fileはファイル名のみ受け付け、<root>/prompts/へ解決
- schema.py: _combineヘルパー、TextToImageRequest.create()に
base_positive/base_negative引数追加、GuidedImageRequest.create()と
InpaintImageRequest.create()から転送
- tools/generate_image.py: service.config.promptからbaseを取りcreate()へ渡す
- tools/generate_guided_image.py: 同上
- tools/inpaint_image.py: 同上
- tests/conftest.py: app_config fixtureにprompt=PromptConfig()追加
- tests/test_config.py: [prompt]の正常系・異常系テスト追加
(省略時空、ファイル読込、パストラバーサル拒否、ファイル不存在、positiveのみ)
- tests/test_schema.py: 結合ロジックのテスト追加
(base両方、positiveのみ、negativeのみ、base空、guided/inpaint転送)
- doc/config.md: §10 [prompt]セクション追記、設定項目一覧表・ディレクトリ構成・
全設定例更新、既存セクション番号を§11〜§13へ繰り下げ
- config.example.toml: [prompt]コメント例追記
- doc/Specifications.md: モデル構成表にBase prompt行追加、推奨ディレクトリに
prompts/追記、設定例更新
- doc/Design.md: 設定オブジェクトにPromptConfig追記、モデルパス解決表に
prompts/追加
- doc/mcp.md: §2.1にbase prompt結合の記載追記
- README.md: [prompt]設定例と結合規則の説明追記
検証:
- ruff format/check、pyright 0 errors、pytest 61 passed
- 実GPU・実モデルでstreamable-http経由generate_imageでbase prompt結合を確認
positive='masterpiece, best quality, ..., sharp focus, a small red cube on a white table'
negative='low quality, ..., jpeg artifacts\nnoise'
base前置・user後置の結合順序が仕様通り機能することをサーバログで検証済み
Diffstat (limited to 'src/mcp_sd15/config.py')
| -rw-r--r-- | src/mcp_sd15/config.py | 27 |
1 files changed, 27 insertions, 0 deletions
diff --git a/src/mcp_sd15/config.py b/src/mcp_sd15/config.py index df2da34..e41bdfd 100644 --- a/src/mcp_sd15/config.py +++ b/src/mcp_sd15/config.py @@ -80,12 +80,19 @@ class ServerConfig: @dataclass(frozen=True) +class PromptConfig: + positive: str = "" + negative: str = "" + + +@dataclass(frozen=True) class AppConfig: model: ModelConfig generation: GenerationConfig controlnet: ControlNetConfig output: OutputConfig server: ServerConfig = ServerConfig() + prompt: PromptConfig = PromptConfig() def controlnet_path(self, mode: str) -> Path: item = self.controlnet.for_mode(mode) @@ -121,6 +128,13 @@ def _filename(value: str | None, label: str) -> str | None: return value +def _read_prompt(path: Path) -> str: + try: + return path.read_text(encoding="utf-8").strip() + except OSError as exc: + raise ConfigError(f"cannot read prompt file {path}: {exc}") from exc + + def _bool(table: dict[str, Any], key: str, default: bool) -> bool: value = table.get(key, default) if not isinstance(value, bool): @@ -230,12 +244,25 @@ def load_config(path: str | Path, *, validate_paths: bool = True) -> AppConfig: ): raise ConfigError("server.port must be an integer between 1 and 65535") + prompt_raw = _table(raw, "prompt") + positive_file = _filename(_string(prompt_raw, "positive_file"), "prompt.positive_file") + negative_file = _filename(_string(prompt_raw, "negative_file"), "prompt.negative_file") + prompt_positive = "" + prompt_negative = "" + if positive_file: + positive_path = root / "prompts" / positive_file + prompt_positive = _read_prompt(positive_path) + if negative_file: + negative_path = root / "prompts" / negative_file + prompt_negative = _read_prompt(negative_path) + config = AppConfig( model=ModelConfig(root, checkpoint, vae, tuple(loras)), generation=GenerationConfig(steps_value, cfg_scale, scheduler, default_size), controlnet=controlnet, output=OutputConfig(output_dir.resolve(), _bool(output_raw, "enabled", False)), server=ServerConfig(transport, host, port_value), + prompt=PromptConfig(prompt_positive, prompt_negative), ) if validate_paths: validate_model_paths(config) |
