restruct / silverstripe-ai
AI provider layer for Silverstripe (Anthropic and OpenAI): 'auto' model resolution, a token usage log per call, estimated cost and a monthly cap per feature, CMS-editable prompts with history, variant comparison, a queued-job base; Markdown pages and llms.txt when silverstripe/cms is installed
Package info
github.com/restruct/silverstripe-ai
Type:silverstripe-vendormodule
pkg:composer/restruct/silverstripe-ai
Requires
- php: ^8.2
- guzzlehttp/guzzle: ^7.5
- league/html-to-markdown: ^5.1
- silverstripe/admin: ^2 || ^3
- silverstripe/framework: ^5.4.31 || ^6
- silverstripe/versioned: ^2 || ^3
- symbiote/silverstripe-queuedjobs: ^5 || ^6
Requires (Dev)
- phpunit/phpunit: ^9.6 || ^11.3
- silverstripe/cms: ^5 || ^6
- silverstripe/siteconfig: ^5 || ^6
Suggests
- silverstripe/cms: Markdown variants of published pages (<page>.md) and /llms.txt
- silverstripe/siteconfig: site title and tagline in /llms.txt (comes with silverstripe/cms)
Provides
None
Conflicts
None
Replaces
None
This package is auto-updated.
Last update: 2026-12-02 16:01:08 UTC
README
AI provider layer for Silverstripe 5.4 and 6: Anthropic and OpenAI over plain Guzzle, with auto model resolution,
a token usage log per call, an estimated cost in euros and a monthly cap per feature, prompts that editors can
change in the CMS (with history), a base class for "content in, structured result out" features, a way to compare
prompts, models and providers on the same input, and Markdown variants of published pages plus /llms.txt.
Requirements
- PHP ^8.2
- Silverstripe framework ^5.4 or ^6, with silverstripe/admin, silverstripe/versioned and symbiote/silverstripe-queuedjobs
- An Anthropic and/or OpenAI API key
silverstripe/cmsis optional: without it you get everything except the Markdown pages and/llms.txt
Install
composer require restruct/silverstripe-ai
vendor/bin/sake dev/build flush=1 # SS6: vendor/bin/sake db:build --flush
AI_INTEGRATION_ENABLED=1
AI_INTEGRATION_ANTHROPIC_KEY=sk-ant-...
AI_INTEGRATION_OPENAI_KEY=sk-...
Installing opens no public endpoint and sends nothing until you call it.
Quick start
An analyzer turns content into a structured result:
use Restruct\AI\Provider\AIContentAnalyzer; class TicketClassifier extends AIContentAnalyzer { private static ?string $ai_feature = 'ticket_classification'; # usage log and cap key private static string $anthropic_model = 'auto:haiku'; # newest Haiku protected function buildPrompt(string $content, array $context): string { return "Classify this support ticket. Reply with JSON {\"label\": ...}.\n\n" . $content; } protected function parseResponse(string $response): array { return $this->parseJsonResponse($response) ?? []; } protected function getTypeName(): string { return 'ticket'; } }
Restruct\AI\Usage\AIUsageGuard: features: ticket_classification: { label: 'Ticket classification', monthly_cap: 10 } # estimated EUR per month
$result = TicketClassifier::singleton()->analyze($ticketText);
Every call is logged (tokens, model, status, duration; no prompt or reply text) and refused before sending once the feature's monthly cap is reached. The CMS shows usage and estimated cost under AI.
Documentation
- Installation and configuration: env keys, providers, model resolution
- Provider layer:
sendRequest(),AIResponse, structured output, errors - Content analyzer: subclassing, prompts, parsing, exceptions
- Comparing prompts, models and providers:
compareVariants(), the table, the CLI script - Usage log and budgets: features, caps, cap groups, rates, the report
- Prompt caching: what is cached, defaults, the hit rate
- Prompts: the registry, YAML prompts, CMS editing and history
- Markdown pages and llms.txt
- CMS section
- Testing: the shared HTTP mock
- Extension hooks
- Configuration reference
- Security and privacy
- Upgrading from 0.x
A site search and "ask the site" assistant built on this package exists as a separate, private package.
License
MIT, see LICENSE.