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 /doc | |
| 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 'doc')
| -rw-r--r-- | doc/Design.md | 6 | ||||
| -rw-r--r-- | doc/Specifications.md | 8 | ||||
| -rw-r--r-- | doc/config.md | 59 | ||||
| -rw-r--r-- | doc/mcp.md | 4 |
4 files changed, 69 insertions, 8 deletions
diff --git a/doc/Design.md b/doc/Design.md index ede063c..a603406 100644 --- a/doc/Design.md +++ b/doc/Design.md @@ -146,6 +146,11 @@ class GenerationConfig: cfg_scale: float scheduler: str default_size: str + +@dataclass(frozen=True) +class PromptConfig: + positive: str = "" + negative: str = "" ``` Pydanticは必須とせず、`tomllib + dataclass + 明示的validation`を基本とする。 @@ -160,6 +165,7 @@ Pydanticは必須とせず、`tomllib + dataclass + 明示的validation`を基æœ | VAE | `<root>/vae/<file>` | | LoRA | `<root>/lora/<file>` | | ControlNet | `<root>/controlnet/<file>` | +| prompt | `<root>/prompts/<file>` | 設計上、MCP入力から任意モデルパスを指定することは禁止する。 diff --git a/doc/Specifications.md b/doc/Specifications.md index db59c28..cc178bb 100644 --- a/doc/Specifications.md +++ b/doc/Specifications.md @@ -133,6 +133,7 @@ LLM側には生成意図に必要な最小限の入力のみを公開し、モム| LoRA | 0個以上 | | LoRA weight | LoRAごとに指定 | | ControlNet | Depth / Openpose / Inpaintを個別設定 | +| Base prompt | positive / negative 各0または1ファイル | | モデルルート | TOMLで1つのルートディレクトリを指定 | ### 6.3 推奨ディレクトリ @@ -142,7 +143,8 @@ models/ ├─ checkpoints/ ├─ vae/ ├─ lora/ -└─ controlnet/ +├─ controlnet/ +└─ prompts/ ``` 設定値には原則としてファイル名を記述し、内部で用途別ディレクトリへ解決する。 @@ -188,6 +190,10 @@ model = "control_v11p_sd15_inpaint.safetensors" [output] directory = "./outputs" + +[prompt] +positive_file = "positive.txt" +negative_file = "negative.txt" ``` ### 7.1 Scheduler 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 │ └─ <model.vae> ├─ lora/ │ └─ <model.lora.file> -└─ controlnet/ - ├─ <controlnet.depth.model> - ├─ <controlnet.openpose.model> - └─ <controlnet.inpaint.model> +├─ controlnet/ +│ ├─ <controlnet.depth.model> +│ ├─ <controlnet.openpose.model> +│ └─ <controlnet.inpaint.model> +└─ prompts/ + ├─ <prompt.positive_file> + └─ <prompt.negative_file> ``` 各モデル項目にはパスではなくファイル名だけを記述する。絶対パス、ディレクトリ名を含む @@ -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を指定する。プロンプトファイルは`<root>/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` + +- `<root>/prompts/<positive_file>`として解決 +- ファイル名のみ指定可能。パスは拒否される +- 省略時はbase positiveなし + +### `negative_file` + +- `<root>/prompts/<negative_file>`として解決 +- ファイル名のみ指定可能。パスは拒否される +- 省略時はbase negativeなし + 相対`directory`はconfigファイルのディレクトリを基準に解決する。保存が有効な場合、 ディレクトリは最初の保存時に作成され、ファイル名はランダムUUIDの`<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サーバは起動しない。 @@ -24,6 +24,10 @@ Stable Diffusion 1.xによる画像生成を行う次の3ツールを公開すã‚ | `negative_prompt` | 任意の文字列。省略または`null`の場合は空文字列 | | 最大長 | それぞれ10,000文字 | +`[prompt]`設定でbase promptを指定している場合、LLMからのプロンプトと結合してから +pipelineへ渡す。positiveは`base, user`の順でカンマ区切り、negativeは`base\nuser`の順で +改行区切りとなる。base promptはMCPツール入力には公開しない。 + ### 2.2 Image size `size`には次の列挙値だけを指定できる。省略または`null`の場合は |
