From 796251e5b0da524584c9e81b8ad6e1dcc0931aa3 Mon Sep 17 00:00:00 2001 From: tanaka takashi Date: Sat, 12 Sep 2026 21:19:16 +0900 Subject: base prompt結合機能を追加 MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit config.tomlの[prompt]でpositive/negativeのbase promptファイルを指定し、 MCP経由でLLMから渡されたプロンプトと結合してpipelineへ渡す機能を追加する。 プロンプトファイルは/prompts/に固定し、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はファイル名のみ受け付け、/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後置の結合順序が仕様通り機能することをサーバログで検証済み --- doc/config.md | 59 ++++++++++++++++++++++++++++++++++++++++++++++++++++------- 1 file changed, 52 insertions(+), 7 deletions(-) (limited to 'doc/config.md') diff --git a/doc/config.md b/doc/config.md index 48142dc..9fcdd55 100644 --- a/doc/config.md +++ b/doc/config.md @@ -25,10 +25,13 @@ uv run mcp-sd15 --config C:\path\to\config.toml │ └─ ├─ lora/ │ └─ -└─ controlnet/ - ├─ - ├─ - └─ +├─ controlnet/ +│ ├─ +│ ├─ +│ └─ +└─ prompts/ + ├─ + └─ ``` 各モデル項目にはパスではなくファイル名だけを記述する。絶対パス、ディレクトリ名を含む @@ -59,6 +62,8 @@ uv run mcp-sd15 --config C:\path\to\config.toml | `server.transport` | string enum | No | `stdio` | | `server.host` | string | No | `127.0.0.1` | | `server.port` | integer | No | `8000` | +| `prompt.positive_file` | string | No | なし | +| `prompt.negative_file` | string | No | なし | ## 4. `[model]` @@ -295,10 +300,46 @@ port = 8000 - 既定値は`8000` - `transport`が`stdio`のときは使用されない +## 10. `[prompt]` + +全ツールで共通するbase promptを指定する。プロンプトファイルは`/prompts/`に +配置し、TOMLにはファイル名だけを記述する。table自体を省略した場合はbase promptなし +(後方互換)となる。 + +```toml +[prompt] +positive_file = "positive.txt" +negative_file = "negative.txt" +``` + +起動時にファイルを読み込み、LLMからのプロンプトと結合してpipelineへ渡す。 + +### 結合規則 + +| 項目 | 結合方式 | +|---|---| +| positive | `base_positive, user_positive` の順でカンマ区切り | +| negative | `base_negative\nuser_negative` の順で改行区切り | + +baseが空の場合はuserプロンプトのみになる。userの`positive_prompt`が空の場合は +従来通りエラーになる。 + +### `positive_file` + +- `/prompts/`として解決 +- ファイル名のみ指定可能。パスは拒否される +- 省略時はbase positiveなし + +### `negative_file` + +- `/prompts/`として解決 +- ファイル名のみ指定可能。パスは拒否される +- 省略時はbase negativeなし + 相対`directory`はconfigファイルのディレクトリを基準に解決する。保存が有効な場合、 ディレクトリは最初の保存時に作成され、ファイル名はランダムUUIDの`.png`となる。 -## 9. 最小設定例 +## 11. 最小設定例 Text-to-Imageだけを使用する最小構成は次のとおり。 @@ -311,7 +352,7 @@ checkpoint = "model.safetensors" この場合、VAEとLoRAは使用せず、全ControlNetとローカル保存は無効になる。生成設定には 既定値が使用される。 -## 10. 全設定例 +## 12. 全設定例 ```toml [model] @@ -349,9 +390,13 @@ model = "control_v11p_sd15_inpaint.safetensors" [output] enabled = false directory = "./outputs" + +[prompt] +positive_file = "positive.txt" +negative_file = "negative.txt" ``` -## 11. 起動時validation +## 13. 起動時validation 次のいずれかに該当するとMCPサーバは起動しない。 -- cgit v1.2.3