Add OpenAI-compatible self-hosted vision backend#14
Open
leonardomarino wants to merge 1 commit intoPiwigo:mainfrom
Open
Add OpenAI-compatible self-hosted vision backend#14leonardomarino wants to merge 1 commit intoPiwigo:mainfrom
leonardomarino wants to merge 1 commit intoPiwigo:mainfrom
Conversation
- New provider: OpenAI-compatible endpoint (llama.cpp, Ollama, vLLM, LiteLLM, etc.) - New feature: optionally write AI description as photo comment - Admin UI: checkbox and textarea field types for plugin configuration - Fallback tag extraction when model returns free text instead of JSON - Backward-compatible: no changes to existing Imagga/Azure behavior - PHPUnit test suite: 22 tests, 75 assertions - Live-tested against Qwen3-VL-30B-A3B on RTX 5090 with real Piwigo DB
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
This PR adds a third API provider — OpenAI-compatible — that works with any server implementing the /v1/chat/completions endpoint with vision support. This enables fully self-hosted, privacy-preserving auto-tagging using local models, with no data ever leaving the user's infrastructure.
Motivation
The existing Tag Recognition plugin supports Imagga and Azure, both of which are external cloud services. Many Piwigo users — particularly self-hosters — have expressed strong interest in local AI tagging that keeps all photo data on their own servers. With the recent availability of high-quality open-source vision models (Qwen3-VL, LLaVA, etc.) that run efficiently on consumer GPUs, a local backend is now practical.
What's included
New API provider: api_classes/OpenAICompatible.php
Works with llama.cpp, Ollama, vLLM, LiteLLM, and OpenAI itself
Sends images as base64-encoded image_url content blocks (standard format)
Configurable: endpoint URL, model name, API key (optional), max tokens, custom prompt override
Robust JSON parsing with markdown fence stripping (many models wrap output in ```json)
Free-text fallback: if a model ignores the JSON instruction, tags are extracted by splitting on commas/newlines and filtering out sentences
New feature: Write description as photo comment
When enabled, the model's natural-language image description is saved to the photo's comment field via pwg.images.setInfo
This works with all three backends conceptually, but is implemented in the OpenAI-compatible provider where it's most useful
Admin UI enhancements:
New getConfFieldTypes() hook on the base API class (returns [] by default — fully backward-compatible)
Template supports checkbox and textarea field types in addition to text inputs
Checkbox-aware save logic in admin.php
Test suite:
22 PHPUnit tests, 75 assertions
Mock HTTP server approach (curl is a C extension and can't be stubbed in PHP)
Live integration test against Qwen3-VL-30B-A3B on an NVIDIA RTX 5090 with a real Piwigo database — both JSON and free-text fallback paths verified end-to-end
Backward compatibility
No changes to Imagga or Azure behavior. The getConfFieldTypes() method is non-abstract with an empty default, so existing providers inherit it without modification. The TR_API_LIST constant adds OpenAICompatible as the third entry.
Tested with
Piwigo 16+ on Debian 13 / PHP 8.3 / MariaDB 11.8
llama.cpp (llama-server) with Qwen3-VL-30B-A3B-Instruct (Q4_K_XL, ~18GB) on RTX 5090
PHPUnit 10.5