Skip to content

Add OpenAI-compatible self-hosted vision backend#14

Open
leonardomarino wants to merge 1 commit intoPiwigo:mainfrom
leonardomarino:feature/openai-compatible-backend
Open

Add OpenAI-compatible self-hosted vision backend#14
leonardomarino wants to merge 1 commit intoPiwigo:mainfrom
leonardomarino:feature/openai-compatible-backend

Conversation

@leonardomarino
Copy link
Copy Markdown

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

- 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
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant