From 4f0fa1624272767373088dbb02d47215fc9c62b6 Mon Sep 17 00:00:00 2001 From: Oliver Bartsch Date: Fri, 6 Nov 2026 13:02:47 +0100 Subject: [PATCH] [FEATURE] Estimate the energy use and CO2e of AI requests Every logged text request now stores an estimated energy use (Wh) and CO2e footprint (g), or the reason why it has none. Hosted requests to OpenAI, Anthropic, Gemini and Mistral are estimated with a PHP port of the EcoLogits LLM inference model. It computes a min-max range from the output tokens, the model size, the provider's PUE and the grid of its data center country. The EcoLogits model and electricity data ship as a subset under MPL-2.0, and Build/Scripts/updateEcoLogitsData.py rebuilds that subset from an upstream checkout. Configurations with a power draw, for example local Ollama models, use the inference time times that power draw instead. The inference time comes from Ollama's own timings where available, otherwise from the request duration. Both the power draw and the grid intensity can be set per configuration. A request that cannot be estimated is shown as "No data" with its reason, never as zero, and every total says how many requests it covers. When smart routing answers with a cheaper model, the requested model's estimate for the same answer is stored as well, and the statistics show the CO2e routing avoided or added. The footprint is shown on the request detail and in the Request Log list (sortable), its statistics card and the Providers overview. A new "AI Footprint" dashboard widget shows the totals and a breakdown per provider. Run the database analyzer to add the new columns to tx_aim_request_log and tx_aim_configuration. Signed-off-by: Oliver Bartsch --- Build/Scripts/updateEcoLogitsData.py | 107 + CHANGELOG.md | 12 + Classes/Controller/ProviderController.php | 7 +- Classes/Controller/RequestLogController.php | 33 +- .../Model/ProviderConfigurationFactory.php | 23 + .../Domain/Repository/RequestLogDemand.php | 2 +- .../Repository/RequestLogRepository.php | 93 + Classes/Footprint/Avoided.php | 25 + Classes/Footprint/Display.php | 193 + Classes/Footprint/EcoLogitsData.php | 97 + Classes/Footprint/Estimate.php | 31 + Classes/Footprint/Estimator.php | 393 ++ Classes/Footprint/Format.php | 128 + Classes/Footprint/Method.php | 25 + Classes/Footprint/Unavailable.php | 28 + .../Middleware/RequestLoggingMiddleware.php | 67 +- Classes/Middleware/SmartRoutingMiddleware.php | 1 + .../SymfonyAi/SymfonyAiPlatformAdapter.php | 19 +- Classes/Widgets/FootprintWidget.php | 58 + .../Provider/FootprintDataProvider.php | 49 + Configuration/Backend/DashboardPresets.php | 1 + Configuration/Services.php | 19 + Configuration/TCA/tx_aim_configuration.php | 26 + Documentation/BackendModules.md | 3 +- Documentation/EnvironmentalImpact.md | 111 + README.md | 3 + Resources/Private/Data/EcoLogits/LICENSE | 373 ++ Resources/Private/Data/EcoLogits/data.json | 3807 +++++++++++++++++ .../Private/Language/locallang_module.xlf | 165 + Resources/Private/Language/locallang_tca.xlf | 18 + .../Private/Partials/RequestLog/Row.html | 1 + .../Private/Partials/RequestLog/Stats.html | 14 + .../Private/Partials/RequestLog/Table.html | 1 + .../Private/Partials/RequestLogDetail.html | 31 + Resources/Private/Templates/Aim/Overview.html | 12 + .../Private/Templates/Aim/RequestLogShow.html | 2 +- .../Aim/RequestLogShowContextual.html | 2 +- .../Templates/Widget/FootprintWidget.html | 63 + Resources/Public/Css/base.css | 12 + .../Public/JavaScript/request-log-poll.js | 11 + .../Controller/RequestLogFootprintTest.php | 221 + Tests/Functional/Footprint/EstimatorTest.php | 385 ++ .../Footprint/FootprintLoggingTest.php | 353 ++ Tests/Functional/Footprint/FormatTest.php | 81 + .../Footprint/SmartRoutingFootprintTest.php | 122 + .../Governance/PrivacyLoggingTest.php | 3 + .../Widgets/FootprintWidgetTest.php | 125 + .../RequestLoggingMiddlewareTest.php | 12 +- .../SymfonyAiPlatformAdapterTest.php | 41 + ext_tables.sql | 12 +- phpstan-baseline.neon | 12 + 51 files changed, 7415 insertions(+), 18 deletions(-) create mode 100644 Build/Scripts/updateEcoLogitsData.py create mode 100644 Classes/Footprint/Avoided.php create mode 100644 Classes/Footprint/Display.php create mode 100644 Classes/Footprint/EcoLogitsData.php create mode 100644 Classes/Footprint/Estimate.php create mode 100644 Classes/Footprint/Estimator.php create mode 100644 Classes/Footprint/Format.php create mode 100644 Classes/Footprint/Method.php create mode 100644 Classes/Footprint/Unavailable.php create mode 100644 Classes/Widgets/FootprintWidget.php create mode 100644 Classes/Widgets/Provider/FootprintDataProvider.php create mode 100644 Documentation/EnvironmentalImpact.md create mode 100644 Resources/Private/Data/EcoLogits/LICENSE create mode 100644 Resources/Private/Data/EcoLogits/data.json create mode 100644 Resources/Private/Templates/Widget/FootprintWidget.html create mode 100644 Tests/Functional/Controller/RequestLogFootprintTest.php create mode 100644 Tests/Functional/Footprint/EstimatorTest.php create mode 100644 Tests/Functional/Footprint/FootprintLoggingTest.php create mode 100644 Tests/Functional/Footprint/FormatTest.php create mode 100644 Tests/Functional/Footprint/SmartRoutingFootprintTest.php create mode 100644 Tests/Functional/Widgets/FootprintWidgetTest.php diff --git a/Build/Scripts/updateEcoLogitsData.py b/Build/Scripts/updateEcoLogitsData.py new file mode 100644 index 0000000..d4026d2 --- /dev/null +++ b/Build/Scripts/updateEcoLogitsData.py @@ -0,0 +1,107 @@ +#!/usr/bin/env python3 +""" +Rebuild Resources/Private/Data/EcoLogits/data.json from an EcoLogits checkout. + + git clone https://github.com/genai-impact/ecologits.git /tmp/ecologits + git -C /tmp/ecologits checkout + python3 -I Build/Scripts/updateEcoLogitsData.py /tmp/ecologits + +Standard library only. Reads the upstream files, never imports them. +Afterwards check Classes/Footprint/Estimator.php against +ecologits/impacts/llm.py at the same commit; constants there are ported by hand. +""" + +import ast +import json +import subprocess +import sys +from pathlib import Path + +# EcoLogits providers that Estimator::PROVIDER_MAP maps to, in that order +PROVIDERS = ['openai', 'anthropic', 'google_genai', 'mistralai'] +# Besides the provider locations: WOR, the default for local models, and DEU +EXTRA_ZONES = ['WOR', 'DEU'] + +TARGET = Path(__file__).resolve().parents[2] / 'Resources/Private/Data/EcoLogits/data.json' + + +def provider_settings(utils_py: Path) -> dict: + """PROVIDER_CONFIG_MAP from ecologits/tracers/utils.py, parsed with ast.""" + tree = ast.parse(utils_py.read_text()) + config_map = next( + node.value for node in tree.body + if isinstance(node, (ast.Assign, ast.AnnAssign)) + and any(isinstance(t, ast.Name) and t.id == 'PROVIDER_CONFIG_MAP' + for t in (node.targets if isinstance(node, ast.Assign) else [node.target])) + ) + + def value(node): + if isinstance(node, ast.Call): # RangeValue(min=…, max=…) + return {kw.arg: ast.literal_eval(kw.value) for kw in node.keywords} + return ast.literal_eval(node) + + settings = {} + for key, call in zip(config_map.keys, config_map.values): + fields = {kw.arg: value(kw.value) for kw in call.keywords} + settings[ast.literal_eval(key)] = { + 'location': fields['datacenter_location'], + 'pue': fields['datacenter_pue'], + } + return settings + + +def main() -> int: + if len(sys.argv) != 2: + print(__doc__.strip(), file=sys.stderr) + return 2 + source = Path(sys.argv[1]) + commit, date = subprocess.run( + ['git', '-C', str(source), 'log', '-1', '--format=%H %cs'], + check=True, capture_output=True, text=True, + ).stdout.split() + + upstream = json.loads((source / 'ecologits/data/models.json').read_text()) + mixes = json.loads((source / 'ecologits/data/electricity_mixes.json').read_text())['electricity_mixes'] + all_settings = provider_settings(source / 'ecologits/tracers/utils.py') + + missing = [p for p in PROVIDERS if p not in all_settings] + if missing: + print('Providers missing upstream: ' + ', '.join(missing), file=sys.stderr) + return 1 + providers = {p: all_settings[p] for p in PROVIDERS} + + zones = sorted({s['location'] for s in providers.values()} | set(EXTRA_ZONES)) + gwp = {m['name']: m['gwp'] for m in mixes} + missing = [z for z in zones if z not in gwp] + if missing: + print('Electricity mixes missing upstream: ' + ', '.join(missing), file=sys.stderr) + return 1 + + data = { + '_source': { + 'project': 'EcoLogits (GenAI Impact)', + 'repository': 'https://github.com/genai-impact/ecologits', + 'commit': commit, + 'date': date, + 'files': [ + 'ecologits/data/models.json', + 'ecologits/data/electricity_mixes.json', + 'ecologits/tracers/utils.py', + ], + 'license': 'MPL-2.0, see LICENSE in this directory', + 'note': 'Subset for the providers AiM maps; values unchanged except unit conversion of electricity mixes to gCO2eq/kWh.' + ' Built by Build/Scripts/updateEcoLogitsData.py.', + }, + 'providers': providers, + # kgCO2eq/kWh upstream, gCO2eq/kWh here + 'electricityMixes': {z: round(gwp[z] * 1000, 10) for z in zones}, + 'aliases': [a for a in upstream['aliases'] if a['provider'] in PROVIDERS], + 'models': [m for m in upstream['models'] if m['provider'] in PROVIDERS], + } + TARGET.write_text(json.dumps(data, indent=1, ensure_ascii=False)) + print(f'{TARGET}: {len(data["models"])} models, {len(data["aliases"])} aliases, zones {", ".join(zones)}, commit {commit[:7]}') + return 0 + + +if __name__ == '__main__': + sys.exit(main()) diff --git a/CHANGELOG.md b/CHANGELOG.md index c5bd4d0..a75d6dc 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -2,6 +2,18 @@ ## Unreleased +- **Feature "Environmental footprint"**: Every text-generating request is logged with an estimated +energy use and CO₂e footprint, shown on the request detail, next to the cost in the Request Log list +(sortable), as a card in the Request Log statistics, per configuration in the Providers overview, +and in the new *AI Footprint* dashboard widget. Requests to OpenAI, Anthropic, Gemini and Mistral +are estimated with the EcoLogits methodology and model data, as a min–max range; requests to local +models such as Ollama use the inference time and a power draw set on the provider configuration. +Requests that cannot be estimated show "No data" and its reason, never zero, and every total says +how many requests it covers. When smart routing answers a simple prompt with a cheaper model, the +requested model's estimate for the same answer is kept too, and the statistics card and the widget +show what smart routing avoided, or added. See `Documentation/EnvironmentalImpact.md`. Run the database +analyzer for the new columns of `tx_aim_request_log` and `tx_aim_configuration`. + - **Bugfix "Workspace versioning of prompt fragments"**: `tx_aim_prompt_fragment` and `tx_aim_page_prompt_fragment` set `versioningWS_alwaysAllowLiveEdit` next to `versioningWS`, so an edit made in a workspace went straight to live, unlike the workspace behaviour `Documentation/ToneOfVoice.md` describes. diff --git a/Classes/Controller/ProviderController.php b/Classes/Controller/ProviderController.php index 735c2ff..21aa9a5 100644 --- a/Classes/Controller/ProviderController.php +++ b/Classes/Controller/ProviderController.php @@ -24,6 +24,7 @@ use B13\Aim\Domain\Repository\ProviderConfigurationDemand; use B13\Aim\Domain\Repository\ProviderConfigurationRepository; use B13\Aim\Domain\Repository\RequestLogRepository; +use B13\Aim\Footprint\Format; use B13\Aim\Pagination\DemandedArrayPaginator; use B13\Aim\Provider\CredentialRedactor; use B13\Aim\Provider\EndpointCredential; @@ -136,8 +137,12 @@ public function overviewAction(ServerRequestInterface $request): ResponseInterfa $configurations = $this->configurationRepository->findByDemand($demand); $totalCount = $this->configurationRepository->countByDemand($demand); - $rows = array_map(function (ProviderConfiguration $configuration): array { + $footprints = $this->requestLogRepository->getFootprintPerConfiguration(); + $rows = array_map(function (ProviderConfiguration $configuration) use ($footprints): array { $row = $configuration->row; + $row['footprint'] = Format::summary($footprints[$configuration->uid] ?? [ + 'requests' => 0, 'estimated_requests' => 0, 'co2_min' => 0.0, 'co2_max' => 0.0, 'energy_min' => 0.0, 'energy_max' => 0.0, + ]); unset($row['api_key']); $row['endpoint'] = EndpointCredential::forDisplay($configuration->endpoint); $row['modelDisabled'] = $this->disabledModelRegistry->isDisabled( diff --git a/Classes/Controller/RequestLogController.php b/Classes/Controller/RequestLogController.php index 324eef2..5e13c6c 100644 --- a/Classes/Controller/RequestLogController.php +++ b/Classes/Controller/RequestLogController.php @@ -16,6 +16,7 @@ use B13\Aim\Backend\SortUrlBuilder; use B13\Aim\Domain\Repository\RequestLogDemand; use B13\Aim\Domain\Repository\RequestLogRepository; +use B13\Aim\Footprint\Display; use B13\Aim\Pagination\DemandedArrayPaginator; use Psr\Http\Message\ResponseInterface; use Psr\Http\Message\ServerRequestInterface; @@ -50,6 +51,7 @@ public function __construct( private readonly SortUrlBuilder $sortUrlBuilder, private readonly ConsoleStylesheetProvider $consoleStylesheetProvider, private readonly ModuleProvider $moduleProvider, + private readonly Display $footprintDisplay, ) {} public function logAction(ServerRequestInterface $request): ResponseInterface @@ -79,7 +81,7 @@ public function logAction(ServerRequestInterface $request): ResponseInterface $buttonBar->addButton($shortcutButton, ButtonBar::BUTTON_POSITION_RIGHT, 3); $demand = RequestLogDemand::fromRequest($request); - $logEntries = array_map($this->withModelRequestedDiffers(...), $this->logRepository->findByDemand($demand)); + $logEntries = array_map($this->withFootprintCell(...), array_map($this->withModelRequestedDiffers(...), $this->logRepository->findByDemand($demand))); $totalCount = $this->logRepository->countByDemand($demand); $paginator = new DemandedArrayPaginator( @@ -90,7 +92,7 @@ public function logAction(ServerRequestInterface $request): ResponseInterface ); $pagination = new SimplePagination($paginator); - $statistics = $this->logRepository->getStatistics(); + $statistics = $this->withFootprintSummary($this->logRepository->getStatistics()); $statistics['pending_grades'] = $this->logRepository->countPendingGradesOlderThan(3600); // Build pagination base URL with demand filters (append &page=N in template) @@ -169,7 +171,7 @@ public function showContextualAction(ServerRequestInterface $request): ResponseI } /** - * @return array{entry: array|null, username?: string, extensionIcon?: string|null} + * @return array{entry: array|null, footprint?: array, username?: string, extensionIcon?: string|null} */ private function resolveShowViewData(int $uid): array { @@ -192,6 +194,7 @@ private function resolveShowViewData(int $uid): array return [ 'entry' => $entry, + 'footprint' => $this->footprintDisplay->entry($entry), 'username' => $userMap[$userId] ?? '', 'extensionIcon' => ($entry['extension_key'] ?? '') !== '' ? ($this->resolveExtensionIcons([$entry['extension_key']])[$entry['extension_key']] ?? null) : null, ]; @@ -206,7 +209,7 @@ public function pollAction(ServerRequestInterface $request): ResponseInterface $demand = RequestLogDemand::fromRequest($request); $logEntries = array_map($this->withModelRequestedDiffers(...), $this->logRepository->findByDemand($demand)); $totalCount = $this->logRepository->countByDemand($demand); - $statistics = $this->logRepository->getStatistics(); + $statistics = $this->withFootprintSummary($this->logRepository->getStatistics()); $requestLogReturnUrl = $this->buildRequestLogUrl($demand); @@ -223,7 +226,7 @@ public function pollAction(ServerRequestInterface $request): ResponseInterface $extensionIcons = $this->resolveExtensionIcons($extensionKeys); $rows = []; - foreach ($logEntries as $entry) { + foreach (array_map($this->withFootprintCell(...), $logEntries) as $entry) { $configurationUid = (int)($entry['configuration_uid'] ?? 0); $userId = (int)($entry['user_id'] ?? 0); $rows[] = [ @@ -257,6 +260,8 @@ public function pollAction(ServerRequestInterface $request): ResponseInterface 'cached_tokens' => (int)($entry['cached_tokens'] ?? 0), 'reasoning_tokens' => (int)($entry['reasoning_tokens'] ?? 0), 'cost' => number_format((float)($entry['cost'] ?? 0), 6), + 'footprint' => $entry['footprint'], + 'footprint_title' => $entry['footprint_title'], 'duration_ms' => number_format((int)($entry['duration_ms'] ?? 0), 0, '.', ','), 'success' => (bool)($entry['success'] ?? false), 'rerouted' => (bool)($entry['rerouted'] ?? false), @@ -360,6 +365,24 @@ private function resolveExtensionIcons(array $extensionKeys): array return $icons; } + /** + * @param array $statistics + * @return array + */ + private function withFootprintSummary(array $statistics): array + { + return [...$statistics, ...$this->footprintDisplay->statistics($statistics)]; + } + + /** + * @param array $entry + * @return array + */ + private function withFootprintCell(array $entry): array + { + return [...$entry, ...$this->footprintDisplay->cell($entry)]; + } + private function getLanguageService(): LanguageService { return $GLOBALS['LANG']; diff --git a/Classes/Domain/Model/ProviderConfigurationFactory.php b/Classes/Domain/Model/ProviderConfigurationFactory.php index b2606a0..bf9a4d2 100644 --- a/Classes/Domain/Model/ProviderConfigurationFactory.php +++ b/Classes/Domain/Model/ProviderConfigurationFactory.php @@ -76,14 +76,37 @@ public static function markedAsDowngrade( ProviderConfiguration $config, string $fromModel, string $reason, + ?ProviderConfiguration $from = null, ): ProviderConfiguration { return new ProviderConfiguration(array_merge($config->row, [ '_auto_model_switch' => true, '_auto_model_switch_from' => $fromModel, '_auto_model_switch_reason' => $reason, + '_smart_routing_from_provider' => $from->providerIdentifier ?? '', + '_smart_routing_from_grid_intensity' => $from?->get('grid_intensity'), + '_smart_routing_from_power_draw_watts' => $from?->get('power_draw_watts'), ])); } + /** + * The configuration a smart-routing downgrade routed away from, as far as + * markedAsDowngrade() carries it, or null for any other configuration. + */ + public static function routedFrom(ProviderConfiguration $config): ?ProviderConfiguration + { + $provider = (string)$config->get('_smart_routing_from_provider', ''); + $model = (string)$config->get('_auto_model_switch_from', ''); + if ($provider === '' || $model === '') { + return null; + } + return new ProviderConfiguration([ + 'ai_provider' => $provider, + 'model' => $model, + 'grid_intensity' => $config->get('_smart_routing_from_grid_intensity'), + 'power_draw_watts' => $config->get('_smart_routing_from_power_draw_watts'), + ]); + } + /** * Create a configuration from TYPO3 Site Settings. * diff --git a/Classes/Domain/Repository/RequestLogDemand.php b/Classes/Domain/Repository/RequestLogDemand.php index c25639f..3c4bda1 100644 --- a/Classes/Domain/Repository/RequestLogDemand.php +++ b/Classes/Domain/Repository/RequestLogDemand.php @@ -25,7 +25,7 @@ class RequestLogDemand implements SortableDemandInterface * implements via a join. Sorting by the raw user_id instead would be poor UX * since the column displays the resolved username, not the ID. */ - protected const ORDER_FIELDS = ['crdate', 'cost', 'duration_ms', 'request_type', 'extension_key', 'total_tokens', 'provider_identifier', 'username']; + protected const ORDER_FIELDS = ['crdate', 'cost', 'co2_g_max', 'duration_ms', 'request_type', 'extension_key', 'total_tokens', 'provider_identifier', 'username']; protected int $limit = 25; diff --git a/Classes/Domain/Repository/RequestLogRepository.php b/Classes/Domain/Repository/RequestLogRepository.php index 023bf35..1c0907d 100644 --- a/Classes/Domain/Repository/RequestLogRepository.php +++ b/Classes/Domain/Repository/RequestLogRepository.php @@ -174,6 +174,14 @@ public function getStatistics(): array 'total_tokens' => (int)($result['total_tokens'] ?? 0), 'avg_duration_ms' => (int)($result['avg_duration_ms'] ?? 0), 'success_rate' => $totalRequests > 0 ? round($successfulRequests / $totalRequests * 100, 1) : 0, + 'footprint_estimated_requests' => (int)($result['footprint_estimated_requests'] ?? 0), + 'footprint_co2_min' => (float)($result['footprint_co2_min'] ?? 0), + 'footprint_co2_max' => (float)($result['footprint_co2_max'] ?? 0), + 'footprint_energy_min' => (float)($result['footprint_energy_min'] ?? 0), + 'footprint_energy_max' => (float)($result['footprint_energy_max'] ?? 0), + 'footprint_routing_estimated_requests' => (int)($result['footprint_routing_estimated_requests'] ?? 0), + 'footprint_avoided_min' => (float)($result['footprint_avoided_min'] ?? 0), + 'footprint_avoided_max' => (float)($result['footprint_avoided_max'] ?? 0), ]; } @@ -190,9 +198,88 @@ private function buildStatisticsQueryBuilder(): QueryBuilder 'SUM(total_tokens) AS total_tokens', 'AVG(duration_ms) AS avg_duration_ms', 'SUM(success) AS successful_requests', + 'COUNT(co2_g_avoided_min) AS footprint_routing_estimated_requests', + 'SUM(co2_g_avoided_min) AS footprint_avoided_min', + 'SUM(co2_g_avoided_max) AS footprint_avoided_max', + ...self::footprintSelects(), ); } + /** + * Footprint totals per provider configuration. Rows without an estimate + * count as requests but not as estimated ones, so callers can show coverage. + * + * @return array + */ + public function getFootprintPerConfiguration(): array + { + $qb = $this->getQueryBuilder(); + $rows = $qb + ->selectLiteral('configuration_uid', $qb->expr()->count('*', 'total_requests'), ...self::footprintSelects()) + ->where($qb->expr()->gt('configuration_uid', $qb->createNamedParameter(0, Connection::PARAM_INT))) + ->groupBy('configuration_uid') + ->executeQuery() + ->fetchAllAssociative(); + + $result = []; + foreach ($rows as $row) { + $result[(int)$row['configuration_uid']] = self::mapFootprintRow($row); + } + return $result; + } + + /** + * @return list + */ + public function getFootprintByProvider(): array + { + $qb = $this->getQueryBuilder(); + $rows = $qb + ->selectLiteral('provider_identifier', $qb->expr()->count('*', 'total_requests'), ...self::footprintSelects()) + ->groupBy('provider_identifier') + ->executeQuery() + ->fetchAllAssociative(); + // In PHP: SUM() over only NULLs is NULL, which PostgreSQL sorts first + usort($rows, static fn(array $a, array $b): int => (float)$b['footprint_co2_max'] <=> (float)$a['footprint_co2_max']); + + return array_map( + static fn(array $row): array => ['provider_identifier' => (string)$row['provider_identifier']] + self::mapFootprintRow($row), + $rows, + ); + } + + /** + * SQL aggregates skip NULL, which is what keeps unestimated requests out of the sums. + * + * @return list + */ + private static function footprintSelects(): array + { + return [ + 'COUNT(co2_g_min) AS footprint_estimated_requests', + 'SUM(co2_g_min) AS footprint_co2_min', + 'SUM(co2_g_max) AS footprint_co2_max', + 'SUM(energy_wh_min) AS footprint_energy_min', + 'SUM(energy_wh_max) AS footprint_energy_max', + ]; + } + + /** + * @param array $row + * @return array{requests: int, estimated_requests: int, co2_min: float, co2_max: float, energy_min: float, energy_max: float} + */ + private static function mapFootprintRow(array $row): array + { + return [ + 'requests' => (int)($row['total_requests'] ?? 0), + 'estimated_requests' => (int)($row['footprint_estimated_requests'] ?? 0), + 'co2_min' => (float)($row['footprint_co2_min'] ?? 0), + 'co2_max' => (float)($row['footprint_co2_max'] ?? 0), + 'energy_min' => (float)($row['footprint_energy_min'] ?? 0), + 'energy_max' => (float)($row['footprint_energy_max'] ?? 0), + ]; + } + public function getStatisticsByProvider(): array { $qb = $this->getQueryBuilder(); @@ -370,6 +457,12 @@ protected function getQueryBuilderForDemand(RequestLogDemand $demand, bool $with $qb->orderBy('be_users.username', $demand->getOrderDirection()) ->addOrderBy(self::TABLE . '.uid', 'desc'); } + } elseif ($withOrdering && $demand->getOrderField() === 'co2_g_max') { + // Rows without an estimate last in either direction, as every DBMS + // has its own default for NULL + $qb->getConcreteQueryBuilder()->orderBy(sprintf('CASE WHEN %s IS NULL THEN 1 ELSE 0 END', $qb->quoteIdentifier('co2_g_max'))); + $qb->addOrderBy('co2_g_max', $demand->getOrderDirection()) + ->addOrderBy('uid', 'desc'); } elseif ($withOrdering) { $qb->orderBy($demand->getOrderField(), $demand->getOrderDirection()); if ($demand->getOrderField() !== 'uid') { diff --git a/Classes/Footprint/Avoided.php b/Classes/Footprint/Avoided.php new file mode 100644 index 0000000..2268522 --- /dev/null +++ b/Classes/Footprint/Avoided.php @@ -0,0 +1,25 @@ + $row + * @return array + */ + public function entry(array $row): array + { + $details = self::details($row); + if (!self::isEstimated($row)) { + return [ + 'available' => false, + 'reason' => (string)($details['unavailable'] ?? 'not_recorded'), + ]; + } + + $facts = []; + if (($row['impact_method'] ?? '') === Method::ModelEstimate->value) { + $parameterDecimals = static fn(mixed $value): int => array_filter((array)$value, static fn(mixed $v): bool => fmod((float)$v, 1.0) !== 0.0) === [] ? 0 : 1; + $facts['activeParameters'] = self::range($details['activeParameters'] ?? 0, $parameterDecimals($details['activeParameters'] ?? 0), ' B'); + if (($details['totalParameters'] ?? null) !== ($details['activeParameters'] ?? null)) { + $facts['totalParameters'] = self::range($details['totalParameters'] ?? 0, $parameterDecimals($details['totalParameters'] ?? 0), ' B'); + } + $facts['gpus'] = self::range($details['gpus'] ?? 0, 0); + $facts['pue'] = self::range($details['pue'] ?? 0, 2); + $facts['outputTokens'] = number_format((int)($details['outputTokens'] ?? 0), 0, '.', ','); + $facts['generationSeconds'] = self::range($details['generationSeconds'] ?? 0, 2, ' s'); + if (is_array($details['embodiedCo2Grams'] ?? null)) { + $facts['embodiedCo2Grams'] = Format::co2((float)($details['embodiedCo2Grams']['min'] ?? 0), (float)($details['embodiedCo2Grams']['max'] ?? 0)); + } + } else { + $facts['powerDrawWatts'] = self::range($details['powerDrawWatts'] ?? 0, 1, ' W'); + $facts['inferenceSeconds'] = self::range($details['inferenceSeconds'] ?? 0, 2, ' s'); + $facts['durationSource'] = $this->translate('footprint.durationSource.' . ($details['durationSource'] ?? '')); + } + $requested = $details['requestedModel'] ?? null; + if (is_array($requested['co2Grams'] ?? null)) { + $facts['requestedModel'] = Format::co2((float)($requested['co2Grams']['min'] ?? 0), (float)($requested['co2Grams']['max'] ?? 0)); + $routingChange = is_array($requested['avoidedCo2Grams'] ?? null) ? Format::routingChange( + 1, + (float)($requested['avoidedCo2Grams']['min'] ?? 0), + (float)($requested['avoidedCo2Grams']['max'] ?? 0), + ) : null; + if ($routingChange !== null) { + $facts['routingChange'] = vsprintf($this->translate('footprint.routingChange.' . $routingChange['key']), $routingChange['values']); + } + } + $gridSource = (string)($details['gridSource'] ?? ''); + $facts['gridIntensity'] = self::range($details['gridIntensity'] ?? 0, 1, ' gCO₂e/kWh') . ', ' + . ($this->translate('footprint.gridSource.' . $gridSource) ?: $gridSource); + + return [ + 'available' => true, + 'method' => (string)($row['impact_method'] ?? ''), + 'co2' => Format::co2((float)$row['co2_g_min'], (float)$row['co2_g_max']), + 'energy' => Format::energy((float)$row['energy_wh_min'], (float)$row['energy_wh_max']), + 'facts' => $facts, + 'warnings' => array_values(array_filter((array)($details['warnings'] ?? []), 'is_string')), + 'dataVersion' => substr((string)($details['dataVersion'] ?? ''), 0, 7), + ]; + } + + /** + * List cell of one log row: the CO2e, or a dash with the reason as its title. + * + * @param array $row + * @return array{footprint: string, footprint_title: string} + */ + public function cell(array $row): array + { + if (self::isEstimated($row)) { + return [ + 'footprint' => Format::co2((float)$row['co2_g_min'], (float)$row['co2_g_max']), + 'footprint_title' => Format::energy((float)($row['energy_wh_min'] ?? 0), (float)($row['energy_wh_max'] ?? 0)), + ]; + } + $reason = (string)(self::details($row)['unavailable'] ?? 'not_recorded'); + return [ + 'footprint' => '–', + 'footprint_title' => $this->translate('footprint.noData') . ': ' . $this->translate('footprint.unavailable.' . $reason), + ]; + } + + /** + * Statistics card, as finished strings so the poll script only swaps text. + * + * @param array $statistics + * @return array{footprint: array, footprint_display: string, footprint_coverage: string, footprint_routing: string} + */ + public function statistics(array $statistics): array + { + $total = self::total($statistics); + $routing = self::routing($statistics); + return [ + 'footprint' => $total, + 'footprint_display' => $total['hasData'] ? $total['co2'] : $this->translate('footprint.noData'), + 'footprint_coverage' => sprintf($this->translate('footprint.coverage'), $total['estimatedRequests'], $total['requests']), + 'footprint_routing' => $routing === null ? '' : vsprintf($this->translate('footprint.routing.' . $routing['key']), $routing['arguments']), + ]; + } + + /** + * @param array $statistics + * @return array{hasData: bool, co2: string, energy: string, pageViews: string, requests: string, estimatedRequests: string} + */ + public static function total(array $statistics): array + { + return Format::summary([ + 'requests' => (int)$statistics['total_requests'], + 'estimated_requests' => (int)$statistics['footprint_estimated_requests'], + 'co2_min' => (float)$statistics['footprint_co2_min'], + 'co2_max' => (float)$statistics['footprint_co2_max'], + 'energy_min' => (float)$statistics['footprint_energy_min'], + 'energy_max' => (float)$statistics['footprint_energy_max'], + ]); + } + + /** + * What smart routing changed, as a footprint.routing.* label key and its arguments. Null when nothing was routed. + * + * @param array $statistics + * @return array{key: string, arguments: list}|null + */ + public static function routing(array $statistics): ?array + { + $estimatedRoutings = (int)$statistics['footprint_routing_estimated_requests']; + $change = Format::routingChange($estimatedRoutings, (float)$statistics['footprint_avoided_min'], (float)$statistics['footprint_avoided_max']); + return $change === null ? null : [ + 'key' => $change['key'], + 'arguments' => [...$change['values'], number_format($estimatedRoutings, 0, '.', ',')], + ]; + } + + /** + * @param array $row + */ + private static function isEstimated(array $row): bool + { + return ($row['co2_g_min'] ?? null) !== null && ($row['co2_g_max'] ?? null) !== null; + } + + /** + * @param array $row + * @return array + */ + private static function details(array $row): array + { + $details = json_decode((string)($row['impact_details'] ?? ''), true); + return is_array($details) ? $details : []; + } + + private static function range(mixed $value, int $decimals, string $suffix = ''): string + { + $min = number_format((float)(is_array($value) ? ($value['min'] ?? 0) : $value), $decimals, '.', ','); + $max = number_format((float)(is_array($value) ? ($value['max'] ?? 0) : $value), $decimals, '.', ','); + return ($min === $max ? $max : $min . '–' . $max) . $suffix; + } + + private function translate(string $key): string + { + return $this->getLanguageService()->sL(self::LLL . $key); + } + + private function getLanguageService(): LanguageService + { + return $GLOBALS['LANG']; + } +} diff --git a/Classes/Footprint/EcoLogitsData.php b/Classes/Footprint/EcoLogitsData.php new file mode 100644 index 0000000..d07bc0c --- /dev/null +++ b/Classes/Footprint/EcoLogitsData.php @@ -0,0 +1,97 @@ +|null */ + private ?array $data = null; + + /** @var array>|null keyed by "provider|name", aliases included */ + private ?array $models = null; + + public function __construct( + private readonly string $dataFile = self::DATA_FILE, + ) {} + + /** + * Exact (provider, name) lookup, aliases included, as EcoLogits does it. + * + * @return array|null + */ + public function findModel(string $provider, string $modelName): ?array + { + return $this->getModels()[$provider . '|' . $modelName] ?? null; + } + + /** + * @return array{location: string, pue: float|array{min: float, max: float}}|null + */ + public function getProvider(string $provider): ?array + { + return $this->load()['providers'][$provider] ?? null; + } + + /** + * Grid carbon intensity in gCO2e/kWh for an ISO 3166-1 alpha-3 zone, WOR for the world average. + */ + public function getGridIntensity(string $zone): ?float + { + $value = $this->load()['electricityMixes'][$zone] ?? null; + return $value === null ? null : (float)$value; + } + + public function getVersion(): string + { + return (string)($this->load()['_source']['commit'] ?? ''); + } + + /** + * @return array> + */ + private function getModels(): array + { + if ($this->models !== null) { + return $this->models; + } + $models = []; + foreach ($this->load()['models'] ?? [] as $model) { + $models[$model['provider'] . '|' . $model['name']] = $model; + } + foreach ($this->load()['aliases'] ?? [] as $alias) { + $target = $models[$alias['provider'] . '|' . $alias['alias']] ?? null; + if ($target !== null) { + $models[$alias['provider'] . '|' . $alias['name']] = $target; + } + } + return $this->models = $models; + } + + /** + * @return array + */ + private function load(): array + { + if ($this->data === null) { + $contents = @file_get_contents($this->dataFile); + $this->data = $contents !== false ? (json_decode($contents, true) ?: []) : []; + } + return $this->data; + } +} diff --git a/Classes/Footprint/Estimate.php b/Classes/Footprint/Estimate.php new file mode 100644 index 0000000..27ae788 --- /dev/null +++ b/Classes/Footprint/Estimate.php @@ -0,0 +1,31 @@ + $details Inputs the estimate was computed from, persisted for traceability + */ + public function __construct( + public readonly Method $method, + public readonly float $energyWhMin, + public readonly float $energyWhMax, + public readonly float $co2GramsMin, + public readonly float $co2GramsMax, + public readonly array $details, + ) {} +} diff --git a/Classes/Footprint/Estimator.php b/Classes/Footprint/Estimator.php new file mode 100644 index 0000000..47bcd2d --- /dev/null +++ b/Classes/Footprint/Estimator.php @@ -0,0 +1,393 @@ + EcoLogits provider */ + private const PROVIDER_MAP = [ + 'openai' => 'openai', + 'anthropic' => 'anthropic', + 'gemini' => 'google_genai', + 'mistral' => 'mistralai', + ]; + + /** Providers whose completion token count excludes reasoning tokens */ + private const REASONING_REPORTED_SEPARATELY = ['gemini']; + + private const SUPPORTED_REQUESTS = [ + TextGenerationRequest::class, + VisionRequest::class, + ConversationRequest::class, + ToolCallingRequest::class, + TranslationRequest::class, + ]; + + private const LOCAL_DEFAULT_ZONE = 'WOR'; + + // EcoLogits constants, ecologits/impacts/llm.py + private const GPU_ENERGY_ALPHA = 1.1665273170451914e-06; + private const GPU_ENERGY_BETA = -0.011205921025579175; + private const GPU_ENERGY_GAMMA = 4.052928146734005e-05; + private const LATENCY_ALPHA = 0.0006785088094353663; + private const LATENCY_BETA = 0.0003119310311688259; + private const LATENCY_GAMMA = 0.019473717579473387; + private const MODEL_QUANTIZATION_BITS = 16; + private const GPU_MEMORY_GB = 80; + private const SERVER_GPUS = 8; + private const SERVER_POWER_KW = 1.2; + private const BATCH_SIZE = 64; + private const GPU_EMBODIED_GWP_KG = 273; + private const SERVER_EMBODIED_GWP_KG = 5700; + private const HARDWARE_LIFESPAN_SECONDS = 3 * 365 * 24 * 60 * 60; + + public function __construct( + private readonly EcoLogitsData $data, + ) {} + + public function estimate( + AiRequestInterface $request, + ?TextResponse $response, + ProviderConfiguration $configuration, + int $durationMs, + bool $streamed = false, + ): Estimate|Unavailable { + if ($response === null || !$response->isSuccessful()) { + return Unavailable::RequestFailed; + } + if (!$this->isSupported($request)) { + return Unavailable::UnsupportedRequestType; + } + + // A configured power draw says the model runs on known hardware, + // e.g. an OpenAI-compatible endpoint served by a local Ollama. + $watts = self::positiveFloat($configuration->get('power_draw_watts')); + if ($watts !== null) { + return $this->estimateFromDuration($watts, $response, $configuration, $durationMs, $streamed); + } + $ecoLogitsProvider = self::PROVIDER_MAP[$configuration->providerIdentifier] ?? null; + if ($ecoLogitsProvider !== null) { + $usage = $response->usage; + $modelName = $usage->modelUsed !== '' ? $usage->modelUsed : $configuration->model; + return $this->estimateFromModel($ecoLogitsProvider, self::outputTokens($usage, $configuration), $modelName, $configuration, $durationMs); + } + return Unavailable::NoDataSource; + } + + /** + * The CO2e smart routing avoided by answering with $answered instead of + * $requested, the configuration it routed away from: the requested model's + * estimate for the same output tokens minus the answer's, low end minus + * high end and vice versa. Both leave out the measured duration, which + * belongs to one of the two models only. Null unless both are model estimates. + */ + public function estimateAvoided( + AiRequestInterface $request, + ?TextResponse $response, + ProviderConfiguration $answered, + ProviderConfiguration $requested, + ): ?Avoided { + $answeredProvider = self::PROVIDER_MAP[$answered->providerIdentifier] ?? null; + $requestedProvider = self::PROVIDER_MAP[$requested->providerIdentifier] ?? null; + if ($answeredProvider === null || $requestedProvider === null + || self::positiveFloat($answered->get('power_draw_watts')) !== null + || self::positiveFloat($requested->get('power_draw_watts')) !== null + || $response === null || !$response->isSuccessful() || !$this->isSupported($request) + ) { + return null; + } + $usage = $response->usage; + $outputTokens = self::outputTokens($usage, $answered); + $answeredEstimate = $this->estimateFromModel( + $answeredProvider, + $outputTokens, + $usage->modelUsed !== '' ? $usage->modelUsed : $answered->model, + $answered, + 0, + ); + $requestedEstimate = $this->estimateFromModel($requestedProvider, $outputTokens, $requested->model, $requested, 0); + if (!$answeredEstimate instanceof Estimate || !$requestedEstimate instanceof Estimate) { + return null; + } + return new Avoided( + $requestedEstimate, + $requestedEstimate->co2GramsMin - $answeredEstimate->co2GramsMax, + $requestedEstimate->co2GramsMax - $answeredEstimate->co2GramsMin, + ); + } + + private function estimateFromModel( + string $provider, + int $outputTokens, + string $modelName, + ProviderConfiguration $configuration, + int $durationMs, + ): Estimate|Unavailable { + if ($outputTokens <= 0) { + return Unavailable::NoOutputTokens; + } + + $model = $this->findModel($provider, $modelName); + $providerSettings = $this->data->getProvider($provider); + if ($model === null || $providerSettings === null) { + return Unavailable::UnknownModel; + } + + [$activeMin, $activeMax, $totalMin, $totalMax] = self::parameterRange($model['architecture'] ?? []); + if ($activeMax <= 0.0 || $totalMax <= 0.0) { + return Unavailable::UnknownModel; + } + [$pueMin, $pueMax] = self::range($providerSettings['pue']); + $tps = isset($model['deployment']['tps']) ? (float)$model['deployment']['tps'] : null; + $ttft = isset($model['deployment']['ttft']) ? (float)$model['deployment']['ttft'] : null; + $requestLatency = $durationMs > 0 ? $durationMs / 1000 : INF; + + $low = $this->itEnergy($activeMin, $totalMin, $outputTokens, $requestLatency, $tps, $ttft); + $high = $this->itEnergy($activeMax, $totalMax, $outputTokens, $requestLatency, $tps, $ttft); + $energyWhMin = $low['kWh'] * $pueMin * 1000; + $energyWhMax = $high['kWh'] * $pueMax * 1000; + + [$gridIntensity, $gridSource] = $this->resolveGridIntensity($configuration, (string)$providerSettings['location']); + if ($gridIntensity === null) { + return Unavailable::NoDataSource; + } + + return new Estimate( + Method::ModelEstimate, + $energyWhMin, + $energyWhMax, + $energyWhMin / 1000 * $gridIntensity, + $energyWhMax / 1000 * $gridIntensity, + [ + 'method' => Method::ModelEstimate->value, + 'provider' => $provider, + 'model' => $model['name'] ?? $modelName, + 'activeParameters' => ['min' => $activeMin, 'max' => $activeMax], + 'totalParameters' => ['min' => $totalMin, 'max' => $totalMax], + 'gpus' => ['min' => $low['gpus'], 'max' => $high['gpus']], + 'pue' => ['min' => $pueMin, 'max' => $pueMax], + 'outputTokens' => $outputTokens, + 'generationSeconds' => ['min' => $low['seconds'], 'max' => $high['seconds']], + // Hardware manufacturing share, reported next to the usage-phase CO2e, not included in it + 'embodiedCo2Grams' => ['min' => $low['embodiedGrams'], 'max' => $high['embodiedGrams']], + 'gridIntensity' => $gridIntensity, + 'gridSource' => $gridSource, + 'warnings' => array_values(array_filter((array)($model['warnings'] ?? []), 'is_string')), + 'dataVersion' => $this->data->getVersion(), + ], + ); + } + + private function estimateFromDuration( + float $watts, + TextResponse $response, + ProviderConfiguration $configuration, + int $durationMs, + bool $streamed, + ): Estimate|Unavailable { + // The duration of a streamed request covers the whole PHP request, + // not just inference, so it can't be attributed to the model. + if ($streamed) { + return Unavailable::StreamedLocalRequest; + } + + [$seconds, $durationSource] = self::inferenceSeconds($response->usage->rawUsage, $durationMs); + if ($seconds <= 0.0) { + return Unavailable::NoDuration; + } + + [$gridIntensity, $gridSource] = $this->resolveGridIntensity($configuration, self::LOCAL_DEFAULT_ZONE); + if ($gridIntensity === null) { + return Unavailable::NoDataSource; + } + $energyWh = $watts * $seconds / 3600; + $co2Grams = $energyWh / 1000 * $gridIntensity; + + return new Estimate( + Method::DurationCalibrated, + $energyWh, + $energyWh, + $co2Grams, + $co2Grams, + [ + 'method' => Method::DurationCalibrated->value, + 'powerDrawWatts' => $watts, + 'inferenceSeconds' => $seconds, + 'durationSource' => $durationSource, + 'gridIntensity' => $gridIntensity, + 'gridSource' => $gridSource, + ], + ); + } + + /** + * Exact match first, since dated names can have their own entries, + * then the name without a date suffix. + * + * @return array|null + */ + private function findModel(string $provider, string $modelName): ?array + { + $model = $this->data->findModel($provider, $modelName); + if ($model !== null) { + return $model; + } + $undated = (string)preg_replace('/-(\d{4}-\d{2}-\d{2}|\d{8})$/', '', $modelName); + return $undated !== $modelName ? $this->data->findModel($provider, $undated) : null; + } + + /** + * IT energy of one inference in kWh (GPUs plus their share of the server), before PUE. + * + * @return array{kWh: float, gpus: int, seconds: float, embodiedGrams: float} + */ + private function itEnergy( + float $activeParameters, + float $totalParameters, + int $outputTokens, + float $requestLatency, + ?float $tps, + ?float $ttft, + ): array { + $gpuEnergyPerToken = (self::GPU_ENERGY_ALPHA * exp(self::GPU_ENERGY_BETA * self::BATCH_SIZE) * $activeParameters + + self::GPU_ENERGY_GAMMA) / 1000; + $gpuEnergy = $outputTokens * $gpuEnergyPerToken; + + $latencyPerToken = $tps === null || $tps <= 0.0 + ? self::LATENCY_ALPHA * $activeParameters + self::LATENCY_BETA * self::BATCH_SIZE + self::LATENCY_GAMMA + : 1 / $tps; + $generationLatency = min($requestLatency, $outputTokens * $latencyPerToken + ($ttft ?? 0.0)); + + $requiredMemory = 1.2 * $totalParameters * self::MODEL_QUANTIZATION_BITS / 8; + $gpus = (int)(2 ** (int)ceil(log(max(1, (int)ceil($requiredMemory / self::GPU_MEMORY_GB)), 2))); + + $serverEnergy = ($generationLatency / 3600) * self::SERVER_POWER_KW * ($gpus / self::SERVER_GPUS) / self::BATCH_SIZE; + + $embodiedKg = $generationLatency + * (($gpus / self::SERVER_GPUS) * self::SERVER_EMBODIED_GWP_KG + $gpus * self::GPU_EMBODIED_GWP_KG) + / (self::HARDWARE_LIFESPAN_SECONDS * self::BATCH_SIZE); + + return [ + 'kWh' => $serverEnergy + $gpus * $gpuEnergy, + 'gpus' => $gpus, + 'seconds' => $generationLatency, + 'embodiedGrams' => $embodiedKg * 1000, + ]; + } + + /** + * @return array{0: ?float, 1: string} intensity in gCO2e/kWh and where it came from + */ + private function resolveGridIntensity(ProviderConfiguration $configuration, string $defaultZone): array + { + // Zero is a valid setting, unlike an empty field + $configured = $configuration->get('grid_intensity'); + if (is_numeric($configured) && (float)$configured >= 0.0) { + return [(float)$configured, 'configuration']; + } + return [$this->data->getGridIntensity($defaultZone), $defaultZone]; + } + + private function isSupported(AiRequestInterface $request): bool + { + foreach (self::SUPPORTED_REQUESTS as $className) { + if ($request instanceof $className) { + return true; + } + } + return false; + } + + /** + * Output tokens including reasoning, by what the answering provider reports. + */ + private static function outputTokens(AiUsageStatistics $usage, ProviderConfiguration $answered): int + { + if (in_array($answered->providerIdentifier, self::REASONING_REPORTED_SEPARATELY, true)) { + return $usage->completionTokens + $usage->reasoningTokens; + } + return $usage->completionTokens; + } + + /** + * Inference time without model loading, from the provider's own timings + * (Ollama, in nanoseconds) when present, else the measured request duration. + * + * @param array $rawUsage + * @return array{0: float, 1: string} + */ + private static function inferenceSeconds(array $rawUsage, int $durationMs): array + { + $total = $rawUsage['total_duration'] ?? null; + if (is_int($total) && $total > 0) { + $load = $rawUsage['load_duration'] ?? 0; + return [max(0, $total - (is_int($load) ? $load : 0)) / 1e9, 'provider_timings']; + } + return [max(0, $durationMs) / 1000, 'request_duration']; + } + + /** + * @param array $architecture + * @return array{0: float, 1: float, 2: float, 3: float} active min/max, total min/max (billions) + */ + private static function parameterRange(array $architecture): array + { + $parameters = $architecture['parameters'] ?? null; + if (($architecture['type'] ?? '') === 'moe' && is_array($parameters)) { + [$activeMin, $activeMax] = self::range($parameters['active'] ?? 0); + [$totalMin, $totalMax] = self::range($parameters['total'] ?? 0); + return [$activeMin, $activeMax, $totalMin, $totalMax]; + } + [$min, $max] = self::range($parameters ?? 0); + return [$min, $max, $min, $max]; + } + + /** + * @return array{0: float, 1: float} + */ + private static function range(mixed $value): array + { + if (is_array($value)) { + return [(float)($value['min'] ?? 0), (float)($value['max'] ?? 0)]; + } + return [(float)$value, (float)$value]; + } + + private static function positiveFloat(mixed $value): ?float + { + return is_numeric($value) && (float)$value > 0 ? (float)$value : null; + } +} diff --git a/Classes/Footprint/Format.php b/Classes/Footprint/Format.php new file mode 100644 index 0000000..b6784a8 --- /dev/null +++ b/Classes/Footprint/Format.php @@ -0,0 +1,128 @@ + 0; + return [ + 'hasData' => $hasData, + 'co2' => $hasData ? self::co2($aggregate['co2_min'], $aggregate['co2_max']) : '', + 'energy' => $hasData ? self::energy($aggregate['energy_min'], $aggregate['energy_max']) : '', + 'pageViews' => $hasData ? self::pageViews($aggregate['co2_min'], $aggregate['co2_max']) : '', + 'requests' => number_format($aggregate['requests'], 0, '.', ','), + 'estimatedRequests' => number_format($aggregate['estimated_requests'], 0, '.', ','), + ]; + } + + /** + * What smart routing changed: CO2e "avoided" or "added", or "mixed" when + * the range spans zero, with the added and the avoided end. Null when nothing was routed. + * + * @return array{key: 'avoided'|'added'|'mixed', values: list}|null + */ + public static function routingChange(int $routedRequests, float $low, float $high): ?array + { + [$low, $high] = [min($low, $high), max($low, $high)]; + if ($routedRequests === 0 || ($low === 0.0 && $high === 0.0)) { + return null; + } + if ($low >= 0.0) { + return ['key' => 'avoided', 'values' => [self::co2($low, $high)]]; + } + if ($high <= 0.0) { + return ['key' => 'added', 'values' => [self::co2(-$high, -$low)]]; + } + return ['key' => 'mixed', 'values' => [self::co2(-$low, -$low), self::co2($high, $high)]]; + } + + public static function co2(float $min, float $max): string + { + return self::range($min, $max, self::MASS_UNITS) . ' CO₂e'; + } + + public static function energy(float $min, float $max): string + { + return self::range($min, $max, self::ENERGY_UNITS); + } + + public static function pageViews(float $co2Min, float $co2Max): string + { + $min = $co2Min / self::PAGE_VIEW_GRAMS; + $max = $co2Max / self::PAGE_VIEW_GRAMS; + $decimals = self::decimals($max); + return self::join(self::number($min, $decimals), self::number($max, $decimals)); + } + + /** + * @param list $units + */ + private static function range(float $min, float $max, array $units): string + { + $max = max($min, $max); + // Rounded first, so 0.99996 g becomes 1.0 g rather than 1,000 mg. + // The largest unit that keeps the value at 0.1 or more: 0.85 mg, not 849 µg. + $rounded = $max > 0.0 ? round($max, self::decimals($max)) : $max; + [$factor, $unit] = end($units); + foreach ($units as [$candidateFactor, $candidateUnit]) { + if ($rounded >= $candidateFactor / 10) { + [$factor, $unit] = [$candidateFactor, $candidateUnit]; + break; + } + } + $decimals = self::decimals($rounded / $factor); + return self::join(self::number($min / $factor, $decimals), self::number($max / $factor, $decimals)) . ' ' . $unit; + } + + /** + * Two significant digits for the larger value of a range; more would suggest a precision the estimates don't have. + */ + private static function decimals(float $value): int + { + if ($value <= 0.0) { + return 0; + } + return max(0, 1 - (int)floor(log10($value))); + } + + private static function number(float $value, int $decimals): string + { + $formatted = number_format($value, $decimals, '.', ','); + if ($value > 0.0 && (float)str_replace(',', '', $formatted) === 0.0) { + return '< ' . number_format(10 ** -$decimals, $decimals, '.', ','); + } + return $formatted; + } + + private static function join(string $min, string $max): string + { + return $min === $max ? $max : $min . '–' . $max; + } +} diff --git a/Classes/Footprint/Method.php b/Classes/Footprint/Method.php new file mode 100644 index 0000000..c09e5cf --- /dev/null +++ b/Classes/Footprint/Method.php @@ -0,0 +1,25 @@ +resolveStreamedResponse($response, $streamIterator); $durationMs = (int)((hrtime(true) - $start) / 1_000_000); - $this->logRequest($request, $resolved, $configuration, $durationMs, null, $context); + $this->logRequest($request, $resolved, $configuration, $durationMs, null, $context, true); } /** @@ -147,6 +153,7 @@ private function logRequest( int $durationMs, ?\Throwable $error, RequestContext $context, + bool $streamed = false, ): void { // Determine effective privacy level $privacyLevel = $this->resolvePrivacyLevel($configuration, $request, $context); @@ -234,6 +241,42 @@ private function logRequest( $data['reroute_reason'] = (string)($context->fallbackInfo['reason'] ?? ''); } + // Before redaction: local estimates read the provider timings from raw usage. + try { + $footprint = $this->footprintEstimator->estimate($request, $response, $configuration, $durationMs, $streamed); + } catch (\Throwable $estimateError) { + // Must not cost the caller an already-paid response + $this->logger->warning('AiM footprint estimate failed: ' . $estimateError::class . ': ' . $estimateError->getMessage(), [ + 'request_type' => $data['request_type'], + 'provider_identifier' => $data['provider_identifier'], + ]); + $footprint = Unavailable::EstimationFailed; + } + if ($footprint instanceof Estimate) { + $data['energy_wh_min'] = $footprint->energyWhMin; + $data['energy_wh_max'] = $footprint->energyWhMax; + $data['co2_g_min'] = $footprint->co2GramsMin; + $data['co2_g_max'] = $footprint->co2GramsMax; + $data['impact_method'] = $footprint->method->value; + $details = $footprint->details; + $avoided = $this->estimateAvoided($request, $response, $configuration); + if ($avoided !== null) { + $requested = $avoided->requested; + $data['co2_g_avoided_min'] = $avoided->co2GramsMin; + $data['co2_g_avoided_max'] = $avoided->co2GramsMax; + $details['requestedModel'] = [ + 'provider' => $requested->details['provider'] ?? '', + 'model' => $requested->details['model'] ?? '', + 'co2Grams' => ['min' => $requested->co2GramsMin, 'max' => $requested->co2GramsMax], + 'energyWh' => ['min' => $requested->energyWhMin, 'max' => $requested->energyWhMax], + 'avoidedCo2Grams' => ['min' => $avoided->co2GramsMin, 'max' => $avoided->co2GramsMax], + ]; + } + $data['impact_details'] = self::encodeJson($details); + } else { + $data['impact_details'] = self::encodeJson(['unavailable' => $footprint->value]); + } + // Redact content for reduced privacy. A provider's own error text and // the reroute reason routinely quote the input that was refused, and // raw_usage is provider-shaped, so none of them can be assumed @@ -268,6 +311,28 @@ private function logRequest( } } + /** + * What smart routing avoided, if it routed this request. + */ + private function estimateAvoided( + AiRequestInterface $request, + ?TextResponse $response, + ProviderConfiguration $configuration, + ): ?Avoided { + $requested = ProviderConfigurationFactory::routedFrom($configuration); + if ($requested === null) { + return null; + } + try { + return $this->footprintEstimator->estimateAvoided($request, $response, $configuration, $requested); + } catch (\Throwable $estimateError) { + $this->logger->warning('AiM avoided footprint estimate failed: ' . $estimateError::class . ': ' . $estimateError->getMessage(), [ + 'provider_identifier' => $configuration->providerIdentifier, + ]); + return null; + } + } + /** * Resolve the effective privacy level from provider config, user TSconfig, * and any per-request override carried on the request. The strictest of diff --git a/Classes/Middleware/SmartRoutingMiddleware.php b/Classes/Middleware/SmartRoutingMiddleware.php index 53ae679..fe40663 100644 --- a/Classes/Middleware/SmartRoutingMiddleware.php +++ b/Classes/Middleware/SmartRoutingMiddleware.php @@ -163,6 +163,7 @@ public function process( $cheaperResult['configuration'], $configuration->model, sprintf('smart routing: simple prompt (%.2f), %s', $classification['score'], $measured), + $configuration, ); // The privacy level is read from the configuration of the attempt that gets logged, diff --git a/Classes/Provider/SymfonyAi/SymfonyAiPlatformAdapter.php b/Classes/Provider/SymfonyAi/SymfonyAiPlatformAdapter.php index 59af1d8..7153ccc 100644 --- a/Classes/Provider/SymfonyAi/SymfonyAiPlatformAdapter.php +++ b/Classes/Provider/SymfonyAi/SymfonyAiPlatformAdapter.php @@ -474,6 +474,7 @@ private function resolveTextContent(object $result): string * TokenUsage::getCachedTokens() -> cachedTokens * Raw API response 'model' -> modelUsed * Raw API response 'usage' -> rawUsage + * Raw API response '*_duration' -> rawUsage (Ollama's native API reports timings at the top level) */ private function extractUsage(object $result, ProviderConfiguration $config): AiUsageStatistics { @@ -505,10 +506,26 @@ private function extractUsage(object $result, ProviderConfiguration $config): Ai reasoningTokens: $reasoningTokens, modelUsed: (string)($rawResponse['model'] ?? ''), systemFingerprint: (string)($rawResponse['system_fingerprint'] ?? ''), - rawUsage: $rawResponse['usage'] ?? [], + rawUsage: ($rawResponse['usage'] ?? []) + self::extractTimings($rawResponse), ); } + /** + * Ollama reports its timings (nanoseconds) at the top level of the response. + * + * @return array + */ + private static function extractTimings(array $rawResponse): array + { + $timings = []; + foreach (['total_duration', 'load_duration', 'prompt_eval_duration', 'eval_duration'] as $key) { + if (is_int($rawResponse[$key] ?? null)) { + $timings[$key] = $rawResponse[$key]; + } + } + return $timings; + } + private function extractUsageFromRawResponse(object $result, ProviderConfiguration $config): AiUsageStatistics { $rawResponse = $this->extractRawResponse($result); diff --git a/Classes/Widgets/FootprintWidget.php b/Classes/Widgets/FootprintWidget.php new file mode 100644 index 0000000..021ffbd --- /dev/null +++ b/Classes/Widgets/FootprintWidget.php @@ -0,0 +1,58 @@ +request = $request; + } + + public function renderWidgetContent(): string + { + $view = $this->backendViewFactory->create($this->request, ['b13/aim', 'typo3/cms-dashboard']); + $view->assignMultiple([ + 'footprint' => $this->dataProvider->getData(), + 'configuration' => $this->configuration, + 'button' => $this->buttonProvider, + ]); + return $view->render('Widget/FootprintWidget'); + } + + public function getOptions(): array + { + return $this->options; + } +} diff --git a/Classes/Widgets/Provider/FootprintDataProvider.php b/Classes/Widgets/Provider/FootprintDataProvider.php new file mode 100644 index 0000000..fb06166 --- /dev/null +++ b/Classes/Widgets/Provider/FootprintDataProvider.php @@ -0,0 +1,49 @@ +, providers: list>, routing: array{key: string, arguments: list}|null} + */ + public function getData(): array + { + $statistics = $this->repository->getStatistics(); + $providers = array_map( + static function (array $row): array { + $identifier = $row['provider_identifier']; + unset($row['provider_identifier']); + return ['identifier' => $identifier] + Format::summary($row); + }, + $this->repository->getFootprintByProvider(), + ); + + return [ + 'total' => Display::total($statistics), + 'providers' => $providers, + 'routing' => Display::routing($statistics), + ]; + } +} diff --git a/Configuration/Backend/DashboardPresets.php b/Configuration/Backend/DashboardPresets.php index 922f840..2766a93 100644 --- a/Configuration/Backend/DashboardPresets.php +++ b/Configuration/Backend/DashboardPresets.php @@ -11,6 +11,7 @@ 'aim_model_usage', 'aim_success_rate', 'aim_extension_usage', + 'aim_footprint', ], 'showInWizard' => true, ], diff --git a/Configuration/Services.php b/Configuration/Services.php index 8ae8f2b..957accb 100644 --- a/Configuration/Services.php +++ b/Configuration/Services.php @@ -7,7 +7,9 @@ use B13\Aim\DependencyInjection\AiMiddlewareCompilerPass; use B13\Aim\DependencyInjection\AiProviderCompilerPass; use B13\Aim\DependencyInjection\SymfonyAiCompilerPass; +use B13\Aim\Widgets\FootprintWidget; use B13\Aim\Widgets\Provider\ExtensionUsageDataProvider; +use B13\Aim\Widgets\Provider\FootprintDataProvider; use B13\Aim\Widgets\Provider\ModelUsageDataProvider; use B13\Aim\Widgets\Provider\ProviderUsageDataProvider; use B13\Aim\Widgets\Provider\RecentRequestsDataProvider; @@ -64,6 +66,7 @@ static function (ChildDefinition $definition, AsAiMiddleware $attribute): void { $services->set(ModelUsageDataProvider::class)->public()->autowire(); $services->set(SuccessRateDataProvider::class)->public()->autowire(); $services->set(ExtensionUsageDataProvider::class)->public()->autowire(); + $services->set(FootprintDataProvider::class)->public()->autowire(); $services->set('dashboard.buttons.aim_request_log') ->class(RequestLogButtonProvider::class) @@ -85,6 +88,22 @@ static function (ChildDefinition $definition, AsAiMiddleware $attribute): void { 'width' => 'medium', ]); + $services->set('dashboard.widget.aim_footprint') + ->class(FootprintWidget::class) + ->arg('$dataProvider', new Reference(FootprintDataProvider::class)) + ->arg('$backendViewFactory', new Reference(BackendViewFactory::class)) + ->arg('$buttonProvider', new Reference('dashboard.buttons.aim_request_log')) + ->arg('$options', ['refreshAvailable' => true]) + ->tag('dashboard.widget', [ + 'identifier' => 'aim_footprint', + 'groupNames' => 'aim', + 'title' => 'LLL:EXT:aim/Resources/Private/Language/locallang_module.xlf:widgets.footprint.title', + 'description' => 'LLL:EXT:aim/Resources/Private/Language/locallang_module.xlf:widgets.footprint.description', + 'iconIdentifier' => 'tx-aim', + 'height' => 'large', + 'width' => 'medium', + ]); + $chartWidgets = [ 'aim_provider_usage' => ['class' => DoughnutChartWidget::class, 'provider' => ProviderUsageDataProvider::class, 'title' => 'widgets.providerUsage'], 'aim_model_usage' => ['class' => BarChartWidget::class, 'provider' => ModelUsageDataProvider::class, 'title' => 'widgets.modelUsage'], diff --git a/Configuration/TCA/tx_aim_configuration.php b/Configuration/TCA/tx_aim_configuration.php index 1297521..d6a91e3 100644 --- a/Configuration/TCA/tx_aim_configuration.php +++ b/Configuration/TCA/tx_aim_configuration.php @@ -30,6 +30,7 @@ --palette--;;config, --palette--;;tokenCosts, --palette--;;cost, + --palette--;;footprint, --div--;LLL:EXT:core/Resources/Private/Language/Form/locallang_tabs.xlf:access, --palette--;;access, --palette--;;governance, @@ -51,6 +52,11 @@ 'label' => 'LLL:EXT:aim/Resources/Private/Language/locallang_tca.xlf:tx_aim_configuration.palette.cost.label', 'showitem' => 'total_cost, cost_currency', ], + 'footprint' => [ + 'label' => 'LLL:EXT:aim/Resources/Private/Language/locallang_tca.xlf:tx_aim_configuration.palette.footprint.label', + 'description' => 'LLL:EXT:aim/Resources/Private/Language/locallang_tca.xlf:tx_aim_configuration.palette.footprint.description', + 'showitem' => 'power_draw_watts, grid_intensity', + ], 'governance' => [ 'label' => 'LLL:EXT:aim/Resources/Private/Language/locallang_tca.xlf:tx_aim_configuration.palette.governance.label', 'showitem' => 'be_groups, --linebreak--, privacy_level, --linebreak--, rerouting_allowed, --linebreak--, accepts_rerouted_requests, --linebreak--, auto_model_switch', @@ -180,6 +186,26 @@ 'readOnly' => true, ], ], + 'power_draw_watts' => [ + 'label' => 'LLL:EXT:aim/Resources/Private/Language/locallang_tca.xlf:tx_aim_configuration.columns.power_draw_watts.label', + 'description' => 'LLL:EXT:aim/Resources/Private/Language/locallang_tca.xlf:tx_aim_configuration.columns.power_draw_watts.description', + 'config' => [ + 'type' => 'number', + 'format' => 'decimal', + 'nullable' => true, + 'range' => ['lower' => 0, 'upper' => 999999.99], + ], + ], + 'grid_intensity' => [ + 'label' => 'LLL:EXT:aim/Resources/Private/Language/locallang_tca.xlf:tx_aim_configuration.columns.grid_intensity.label', + 'description' => 'LLL:EXT:aim/Resources/Private/Language/locallang_tca.xlf:tx_aim_configuration.columns.grid_intensity.description', + 'config' => [ + 'type' => 'number', + 'format' => 'decimal', + 'nullable' => true, + 'range' => ['lower' => 0, 'upper' => 999999.99], + ], + ], 'cost_currency' => [ 'label' => 'LLL:EXT:aim/Resources/Private/Language/locallang_tca.xlf:tx_aim_configuration.columns.cost_currency.label', 'config' => [ diff --git a/Documentation/BackendModules.md b/Documentation/BackendModules.md index cf48208..09992f2 100644 --- a/Documentation/BackendModules.md +++ b/Documentation/BackendModules.md @@ -74,7 +74,7 @@ A fragment assigned to multiple pages is still one Library row; editing it from ## Dashboard Widgets -When `typo3/cms-dashboard` is installed, AiM registers five widgets and a pre-configured dashboard preset ("AiM: AI Analytics"): +When `typo3/cms-dashboard` is installed, AiM registers six widgets and a pre-configured dashboard preset ("AiM: AI Analytics"): | Widget | Type | Shows | |---|---|---| @@ -83,6 +83,7 @@ When `typo3/cms-dashboard` is installed, AiM registers five widgets and a pre-co | Model Usage | Bar chart | Request count per model | | Success Rate | Doughnut chart | Successful vs failed requests | | Extension Usage | Doughnut chart | Which extensions generate the most requests | +| AI Footprint | Summary and table | Estimated energy and CO₂e of all requests, per provider, with coverage; see [Environmental footprint](EnvironmentalImpact.md) | All widgets are refreshable and grouped under "AiM" in the widget picker. The recent requests widget includes a button to open the full request log module. diff --git a/Documentation/EnvironmentalImpact.md b/Documentation/EnvironmentalImpact.md new file mode 100644 index 0000000..f5199ca --- /dev/null +++ b/Documentation/EnvironmentalImpact.md @@ -0,0 +1,111 @@ +# Environmental footprint + +How AiM estimates the energy use and CO₂e of each AI request, where the numbers come from, and what they leave out. + +[Back to the README](../README.md) + +Every text-generating request in the request log gets an estimated energy use (Wh) and CO₂e footprint (grams), or a reason why it has none. The values are shown on the request detail, next to the cost in the Request Log list, in a card in the Request Log statistics, per configuration in the Providers overview, and in the *AI Footprint* dashboard widget. + +**These are estimates, not measurements.** None of the hosted providers reports the energy a request used. AiM computes a plausible range from what is known: the number of generated tokens, the size of the model, and the data center the provider runs in. Where that knowledge is missing, AiM shows *No data*, never zero. + +## Two methods + +| Method | Used for | Input | Result | +|---|---|---|---| +| EcoLogits model (`model_estimate`) | OpenAI, Anthropic, Gemini, Mistral | Output tokens, model size from the EcoLogits data, request duration | A min–max range | +| Configured power draw (`duration_calibrated`) | Any configuration with a power draw set, e.g. Ollama, LM Studio, or an OpenAI-compatible endpoint on your own hardware | Inference time × the power draw set on the configuration | A single value | + +A configuration with a power draw always uses that, also for a provider with model data, since it says the model runs on known hardware. Without one, OpenAI, Anthropic, Gemini and Mistral use the EcoLogits method and any other provider has no data. + +### EcoLogits model + +AiM ports the LLM inference model of [EcoLogits](https://ecologits.ai/latest/methodology/llm_inference/) (GenAI Impact, MPL-2.0) to PHP, and ships a subset of its model and electricity data in `Resources/Private/Data/EcoLogits/`. The data file names the upstream commit it was taken from. + +To update the data, check out EcoLogits at the new commit and run `python3 -I Build/Scripts/updateEcoLogitsData.py ` (standard library only). Then compare `ecologits/impacts/llm.py` at that commit with `Classes/Footprint/Estimator.php`, whose constants are ported by hand. + +Per request, with `T` output tokens, `P` active and `P_total` total parameters (billions), and a batch size of 64: + +1. Energy of one GPU: `T × (α·e^(β·64)·P + γ)` Wh, with the EcoLogits regression constants. +2. GPUs needed to hold the model: `1.2 × P_total × 2` GB on 80 GB GPUs, rounded up to a power of two. +3. Generation time: `T / tokens-per-second + time to first token` from the EcoLogits data, or a regression on the model size where the data has none; at most the measured request duration. +4. The rest of the server: 1.2 kW for 8 GPUs, for the generation time, shared by the batch. +5. Data center overhead: multiplied by the provider's PUE (OpenAI 1.20, Anthropic 1.09–1.14, Google 1.09, Mistral 1.16). +6. CO₂e: energy × the carbon intensity of the grid in the provider's data center country (USA 384.4 g/kWh, Sweden 35.26 g/kWh for Mistral). + +Closed models do not publish their size. EcoLogits assumes a range, for example 8–28 billion parameters for `gpt-4o-mini`, and AiM computes both ends, together with the PUE range where a provider has one. The range is that assumption only, not a confidence interval: the regression, the assumed hardware and batch size, and the uncounted input tokens add uncertainty that no range shows. A model with a published size therefore shows a single value without being more certain. The detail view lists every input and the EcoLogits warnings for the model. + +The model is looked up by provider and model name, the way EcoLogits does it. A name with a date suffix (`gpt-4.1-mini-2025-04-14`) falls back to the undated name when it has no entry of its own. A model name on a provider other than its own (an OpenAI model name behind an OpenAI-compatible gateway) does not borrow that provider's data. The other way round, every configuration of the `openai` provider is estimated as OpenAI in a US data center, also when its endpoint is a proxy or a different host; set the grid intensity on the configuration if you know better. + +Output tokens are counted the way each provider reports them: OpenAI and Anthropic include reasoning tokens in the output tokens, Gemini reports them separately and AiM adds them, and Mistral reports a single count that AiM takes as it is. + +### Configured power draw + +For a model running on your own hardware, AiM multiplies the inference time by the power draw you configure: + +- **Inference time**: with AiM's native Ollama provider, Ollama's own `total_duration` minus `load_duration`, so loading the model into memory after idling is left out. Through an OpenAI-compatible endpoint (Ollama's `/v1` API, LM Studio) there are no timings, so it is the measured request duration, model loading included. +- **Power draw**: the extra power the machine draws while generating, idle draw subtracted. Set it in the configuration's *Environmental Footprint* section. +- **Grid**: the world average (458.29 g/kWh) unless the configuration sets one. 0 is taken as set, for example for a machine on its own solar power; leave the field empty for the default. + +The value is used as entered, without range, so the estimate is exactly as good as the measurement behind it. Two requests running at the same time on one machine are each charged the full power draw, which counts the overlap twice. Without provider timings, the measured request duration includes loading the model after idling, so the first request after a pause is overestimated. + +Streamed requests to local models are not estimated yet: their measured duration covers the whole PHP request, not only inference. Streamed requests to hosted providers are estimated when the stream reports its token usage, and show *No data* when it does not. + +#### Measuring the power draw on macOS + +`powermetrics` ships with macOS and reads the power sensors of Apple silicon. It needs root. + +1. Send one request to load the model. +2. Measure idle draw for 30 seconds with the model loaded: + `sudo powermetrics --samplers cpu_power,gpu_power -i 1000 -n 30` +3. Measure again while requests run back to back, and keep them running for longer than the 30 seconds. +4. Use the average *Combined Power* under load minus the idle average, in watts. + +The value covers the chip (CPU, GPU, Neural Engine), not the display, storage or power supply, so it is a lower bound for the whole machine. It also belongs to the workload it was measured with: image descriptions and short text completions draw differently. + +On Linux with an NVIDIA GPU, `nvidia-smi --query-gpu=power.draw --format=csv -l 1` gives the GPU share. + +## Smart routing + +When [smart routing](Pipeline.md) answers a simple prompt with a cheaper model than the one requested, AiM also estimates what the requested model would have used for the same answer, with the EcoLogits method and the output tokens of the answer that was given. The difference is what smart routing avoided. + +- The request detail lists the requested model's estimate as *Requested model, without smart routing*, and the difference. +- The Request Log statistics card and the dashboard widget add up what smart routing avoided, as a range, with the number of routed requests it covers: those where both models have an estimate (see below), so it can be lower than the rerouted requests in the log. Where routing added CO₂e, they say so instead. +- Both models have to be known to the EcoLogits data, and neither configuration may set a power draw. A request routed to or away from a local model has no such estimate. +- The requested model would not have written exactly the same answer; taking the given answer's length is the assumption. +- Both sides take their generation time from the EcoLogits data, since the measured one belongs to the model that answered, so the answer's side can differ slightly from the estimate stored for the request. Each side uses the grid of its own configuration. +- The range runs from the requested model's low end minus the answer's high end to the requested model's high end minus the answer's low end. For two models with wide parameter ranges it can span zero. + +Smart routing picks the cheaper model by cost, success rate and speed, not by energy. A cheaper model is usually a smaller one, which is why both tend to go down together. + +## What is not included + +- **Input tokens** (hosted providers): the energy of reading the prompt, and of encoding images in vision requests. EcoLogits counts generated tokens only. For prompt-heavy requests such as image descriptions, translations and page-content prompts this may well be the larger part; we have no data to say how large. The local method does include it, since Ollama's timings cover reading the prompt. +- **Hardware manufacturing** (embodied emissions): not part of the CO₂e figures and totals, so that all of them share the usage-phase boundary. For hosted providers the detail view lists the EcoLogits manufacturing share separately. On US grids it was 2–14 % of the usage-phase CO₂e in our reference cases; on Mistral's Swedish grid it is as large as the usage phase or larger, so Mistral figures understate the total by half or more. For local models it is not estimated. +- **Water use and resource depletion.** +- **Failed requests**, which still use energy. +- **Embeddings, image generation and other request types** without generated text. +- **Requests logged before this feature**, which show *No data*. + +## Reading the numbers + +- Ranges span a factor of about 1.2 to 2.6 between the models we tested. The upper end is the safer number to quote, and still not an upper bound. +- Totals only add up the estimated requests, so they are a lower bound. Every total says how many requests it covers, for example *132 of 140 requests estimated*; the requests left out (failed, streamed local, embeddings) can be the heavier ones. +- The dashboard widget compares the total with page views of 0.36 g CO₂e each (Sustainable Web Design Model v4), as a sense of scale only: the page view figure includes devices, network and hardware manufacturing, the AI figure does not. +- The estimates of the two methods are not directly comparable, and totals add them up anyway: one models a share of a shared data center server including its idle time, the other is the extra draw of your own chip, idle subtracted, on a different grid. + +## Stored values + +`tx_aim_request_log` holds `energy_wh_min`, `energy_wh_max`, `co2_g_min`, `co2_g_max` (all `NULL` without an estimate), `impact_method`, and `impact_details`: a JSON record of the inputs (model, parameter range, GPUs, PUE, output tokens, grid intensity, data version), or `{"unavailable": ""}`. The inputs are kept so an estimate stays explainable after the bundled data changes. Under the *reduced* privacy level the estimate is still computed and stored; it contains no content. + +`tx_aim_configuration` holds `power_draw_watts` and `grid_intensity`, both empty by default. + +## License of the EcoLogits parts + +`Classes/Footprint/Estimator.php` ports `ecologits/impacts/llm.py`, and `Resources/Private/Data/EcoLogits/` holds a subset of the EcoLogits data, both by GenAI Impact under the Mozilla Public License 2.0 (the license text is next to the data). Both are available under MPL-2.0 as well as under the GPL-2.0-or-later of the extension. + +## Sources + +- EcoLogits methodology: https://ecologits.ai/latest/methodology/llm_inference/ +- EcoLogits code and data: https://github.com/genai-impact/ecologits +- Sustainable Web Design Model v4: https://sustainablewebdesign.org/estimating-digital-emissions/ +- The approach was prompted by Wagtail's "The carbon footprint of Wagtail AI": https://wagtail.org/blog/the-carbon-footprint-of-wagtail-ai/ diff --git a/README.md b/README.md index da0cae9..c09c236 100644 --- a/README.md +++ b/README.md @@ -124,6 +124,7 @@ below, beautifully rendered rather than raw. | [Governance and access control](Documentation/Governance.md) | Credential storage, provider and capability restrictions, budgets, rate limits, privacy levels, and rerouting protection. | | [Tone of voice](Documentation/ToneOfVoice.md) | How the prompt is composed, page-tree fragments and their inheritance, the fragment library, and voice calibration. | | [The request pipeline](Documentation/Pipeline.md) | Smart routing, LLM grading, the eleven stages, and writing middleware of your own. | +| [Environmental footprint](Documentation/EnvironmentalImpact.md) | How the energy and CO₂e estimate of each request is computed, how to calibrate local models, and what it leaves out. | | [Backend modules](Documentation/BackendModules.md) | Providers, Request Log and Prompt Management, the dashboard widgets, and the database tables. | | [Development](Documentation/Development.md) | Running the tests, static analysis and coding standards on every supported TYPO3 and PHP version. | @@ -139,6 +140,8 @@ below, beautifully rendered rather than raw. GPL-2.0-or-later +The environmental footprint estimate ports parts of [EcoLogits](https://github.com/genai-impact/ecologits) by GenAI Impact and ships a subset of its data, under MPL-2.0; see [Environmental footprint](Documentation/EnvironmentalImpact.md). + ## Credits Created with 🧡 by [Oli Bartsch](https://github.com/o-ba) for [b13 GmbH, Stuttgart](https://b13.com). diff --git a/Resources/Private/Data/EcoLogits/LICENSE b/Resources/Private/Data/EcoLogits/LICENSE new file mode 100644 index 0000000..a612ad9 --- /dev/null +++ b/Resources/Private/Data/EcoLogits/LICENSE @@ -0,0 +1,373 @@ +Mozilla Public License Version 2.0 +================================== + +1. Definitions +-------------- + +1.1. "Contributor" + means each individual or legal entity that creates, contributes to + the creation of, or owns Covered Software. + +1.2. "Contributor Version" + means the combination of the Contributions of others (if any) used + by a Contributor and that particular Contributor's Contribution. + +1.3. "Contribution" + means Covered Software of a particular Contributor. + +1.4. "Covered Software" + means Source Code Form to which the initial Contributor has attached + the notice in Exhibit A, the Executable Form of such Source Code + Form, and Modifications of such Source Code Form, in each case + including portions thereof. + +1.5. "Incompatible With Secondary Licenses" + means + + (a) that the initial Contributor has attached the notice described + in Exhibit B to the Covered Software; or + + (b) that the Covered Software was made available under the terms of + version 1.1 or earlier of the License, but not also under the + terms of a Secondary License. + +1.6. "Executable Form" + means any form of the work other than Source Code Form. + +1.7. "Larger Work" + means a work that combines Covered Software with other material, in + a separate file or files, that is not Covered Software. + +1.8. "License" + means this document. + +1.9. "Licensable" + means having the right to grant, to the maximum extent possible, + whether at the time of the initial grant or subsequently, any and + all of the rights conveyed by this License. + +1.10. "Modifications" + means any of the following: + + (a) any file in Source Code Form that results from an addition to, + deletion from, or modification of the contents of Covered + Software; or + + (b) any new file in Source Code Form that contains any Covered + Software. + +1.11. "Patent Claims" of a Contributor + means any patent claim(s), including without limitation, method, + process, and apparatus claims, in any patent Licensable by such + Contributor that would be infringed, but for the grant of the + License, by the making, using, selling, offering for sale, having + made, import, or transfer of either its Contributions or its + Contributor Version. + +1.12. "Secondary License" + means either the GNU General Public License, Version 2.0, the GNU + Lesser General Public License, Version 2.1, the GNU Affero General + Public License, Version 3.0, or any later versions of those + licenses. + +1.13. "Source Code Form" + means the form of the work preferred for making modifications. + +1.14. "You" (or "Your") + means an individual or a legal entity exercising rights under this + License. For legal entities, "You" includes any entity that + controls, is controlled by, or is under common control with You. For + purposes of this definition, "control" means (a) the power, direct + or indirect, to cause the direction or management of such entity, + whether by contract or otherwise, or (b) ownership of more than + fifty percent (50%) of the outstanding shares or beneficial + ownership of such entity. + +2. License Grants and Conditions +-------------------------------- + +2.1. Grants + +Each Contributor hereby grants You a world-wide, royalty-free, +non-exclusive license: + +(a) under intellectual property rights (other than patent or trademark) + Licensable by such Contributor to use, reproduce, make available, + modify, display, perform, distribute, and otherwise exploit its + Contributions, either on an unmodified basis, with Modifications, or + as part of a Larger Work; and + +(b) under Patent Claims of such Contributor to make, use, sell, offer + for sale, have made, import, and otherwise transfer either its + Contributions or its Contributor Version. + +2.2. Effective Date + +The licenses granted in Section 2.1 with respect to any Contribution +become effective for each Contribution on the date the Contributor first +distributes such Contribution. + +2.3. Limitations on Grant Scope + +The licenses granted in this Section 2 are the only rights granted under +this License. No additional rights or licenses will be implied from the +distribution or licensing of Covered Software under this License. +Notwithstanding Section 2.1(b) above, no patent license is granted by a +Contributor: + +(a) for any code that a Contributor has removed from Covered Software; + or + +(b) for infringements caused by: (i) Your and any other third party's + modifications of Covered Software, or (ii) the combination of its + Contributions with other software (except as part of its Contributor + Version); or + +(c) under Patent Claims infringed by Covered Software in the absence of + its Contributions. + +This License does not grant any rights in the trademarks, service marks, +or logos of any Contributor (except as may be necessary to comply with +the notice requirements in Section 3.4). + +2.4. Subsequent Licenses + +No Contributor makes additional grants as a result of Your choice to +distribute the Covered Software under a subsequent version of this +License (see Section 10.2) or under the terms of a Secondary License (if +permitted under the terms of Section 3.3). + +2.5. Representation + +Each Contributor represents that the Contributor believes its +Contributions are its original creation(s) or it has sufficient rights +to grant the rights to its Contributions conveyed by this License. + +2.6. Fair Use + +This License is not intended to limit any rights You have under +applicable copyright doctrines of fair use, fair dealing, or other +equivalents. + +2.7. Conditions + +Sections 3.1, 3.2, 3.3, and 3.4 are conditions of the licenses granted +in Section 2.1. + +3. Responsibilities +------------------- + +3.1. Distribution of Source Form + +All distribution of Covered Software in Source Code Form, including any +Modifications that You create or to which You contribute, must be under +the terms of this License. You must inform recipients that the Source +Code Form of the Covered Software is governed by the terms of this +License, and how they can obtain a copy of this License. You may not +attempt to alter or restrict the recipients' rights in the Source Code +Form. + +3.2. Distribution of Executable Form + +If You distribute Covered Software in Executable Form then: + +(a) such Covered Software must also be made available in Source Code + Form, as described in Section 3.1, and You must inform recipients of + the Executable Form how they can obtain a copy of such Source Code + Form by reasonable means in a timely manner, at a charge no more + than the cost of distribution to the recipient; and + +(b) You may distribute such Executable Form under the terms of this + License, or sublicense it under different terms, provided that the + license for the Executable Form does not attempt to limit or alter + the recipients' rights in the Source Code Form under this License. + +3.3. Distribution of a Larger Work + +You may create and distribute a Larger Work under terms of Your choice, +provided that You also comply with the requirements of this License for +the Covered Software. If the Larger Work is a combination of Covered +Software with a work governed by one or more Secondary Licenses, and the +Covered Software is not Incompatible With Secondary Licenses, this +License permits You to additionally distribute such Covered Software +under the terms of such Secondary License(s), so that the recipient of +the Larger Work may, at their option, further distribute the Covered +Software under the terms of either this License or such Secondary +License(s). + +3.4. Notices + +You may not remove or alter the substance of any license notices +(including copyright notices, patent notices, disclaimers of warranty, +or limitations of liability) contained within the Source Code Form of +the Covered Software, except that You may alter any license notices to +the extent required to remedy known factual inaccuracies. + +3.5. Application of Additional Terms + +You may choose to offer, and to charge a fee for, warranty, support, +indemnity or liability obligations to one or more recipients of Covered +Software. However, You may do so only on Your own behalf, and not on +behalf of any Contributor. You must make it absolutely clear that any +such warranty, support, indemnity, or liability obligation is offered by +You alone, and You hereby agree to indemnify every Contributor for any +liability incurred by such Contributor as a result of warranty, support, +indemnity or liability terms You offer. You may include additional +disclaimers of warranty and limitations of liability specific to any +jurisdiction. + +4. Inability to Comply Due to Statute or Regulation +--------------------------------------------------- + +If it is impossible for You to comply with any of the terms of this +License with respect to some or all of the Covered Software due to +statute, judicial order, or regulation then You must: (a) comply with +the terms of this License to the maximum extent possible; and (b) +describe the limitations and the code they affect. Such description must +be placed in a text file included with all distributions of the Covered +Software under this License. Except to the extent prohibited by statute +or regulation, such description must be sufficiently detailed for a +recipient of ordinary skill to be able to understand it. + +5. Termination +-------------- + +5.1. The rights granted under this License will terminate automatically +if You fail to comply with any of its terms. However, if You become +compliant, then the rights granted under this License from a particular +Contributor are reinstated (a) provisionally, unless and until such +Contributor explicitly and finally terminates Your grants, and (b) on an +ongoing basis, if such Contributor fails to notify You of the +non-compliance by some reasonable means prior to 60 days after You have +come back into compliance. Moreover, Your grants from a particular +Contributor are reinstated on an ongoing basis if such Contributor +notifies You of the non-compliance by some reasonable means, this is the +first time You have received notice of non-compliance with this License +from such Contributor, and You become compliant prior to 30 days after +Your receipt of the notice. + +5.2. If You initiate litigation against any entity by asserting a patent +infringement claim (excluding declaratory judgment actions, +counter-claims, and cross-claims) alleging that a Contributor Version +directly or indirectly infringes any patent, then the rights granted to +You by any and all Contributors for the Covered Software under Section +2.1 of this License shall terminate. + +5.3. In the event of termination under Sections 5.1 or 5.2 above, all +end user license agreements (excluding distributors and resellers) which +have been validly granted by You or Your distributors under this License +prior to termination shall survive termination. + +************************************************************************ +* * +* 6. Disclaimer of Warranty * +* ------------------------- * +* * +* Covered Software is provided under this License on an "as is" * +* basis, without warranty of any kind, either expressed, implied, or * +* statutory, including, without limitation, warranties that the * +* Covered Software is free of defects, merchantable, fit for a * +* particular purpose or non-infringing. The entire risk as to the * +* quality and performance of the Covered Software is with You. * +* Should any Covered Software prove defective in any respect, You * +* (not any Contributor) assume the cost of any necessary servicing, * +* repair, or correction. This disclaimer of warranty constitutes an * +* essential part of this License. No use of any Covered Software is * +* authorized under this License except under this disclaimer. * +* * +************************************************************************ + +************************************************************************ +* * +* 7. Limitation of Liability * +* -------------------------- * +* * +* Under no circumstances and under no legal theory, whether tort * +* (including negligence), contract, or otherwise, shall any * +* Contributor, or anyone who distributes Covered Software as * +* permitted above, be liable to You for any direct, indirect, * +* special, incidental, or consequential damages of any character * +* including, without limitation, damages for lost profits, loss of * +* goodwill, work stoppage, computer failure or malfunction, or any * +* and all other commercial damages or losses, even if such party * +* shall have been informed of the possibility of such damages. This * +* limitation of liability shall not apply to liability for death or * +* personal injury resulting from such party's negligence to the * +* extent applicable law prohibits such limitation. Some * +* jurisdictions do not allow the exclusion or limitation of * +* incidental or consequential damages, so this exclusion and * +* limitation may not apply to You. * +* * +************************************************************************ + +8. Litigation +------------- + +Any litigation relating to this License may be brought only in the +courts of a jurisdiction where the defendant maintains its principal +place of business and such litigation shall be governed by laws of that +jurisdiction, without reference to its conflict-of-law provisions. +Nothing in this Section shall prevent a party's ability to bring +cross-claims or counter-claims. + +9. Miscellaneous +---------------- + +This License represents the complete agreement concerning the subject +matter hereof. If any provision of this License is held to be +unenforceable, such provision shall be reformed only to the extent +necessary to make it enforceable. Any law or regulation which provides +that the language of a contract shall be construed against the drafter +shall not be used to construe this License against a Contributor. + +10. Versions of the License +--------------------------- + +10.1. New Versions + +Mozilla Foundation is the license steward. Except as provided in Section +10.3, no one other than the license steward has the right to modify or +publish new versions of this License. Each version will be given a +distinguishing version number. + +10.2. Effect of New Versions + +You may distribute the Covered Software under the terms of the version +of the License under which You originally received the Covered Software, +or under the terms of any subsequent version published by the license +steward. + +10.3. Modified Versions + +If you create software not governed by this License, and you want to +create a new license for such software, you may create and use a +modified version of this License if you rename the license and remove +any references to the name of the license steward (except to note that +such modified license differs from this License). + +10.4. Distributing Source Code Form that is Incompatible With Secondary +Licenses + +If You choose to distribute Source Code Form that is Incompatible With +Secondary Licenses under the terms of this version of the License, the +notice described in Exhibit B of this License must be attached. + +Exhibit A - Source Code Form License Notice +------------------------------------------- + + This Source Code Form is subject to the terms of the Mozilla Public + License, v. 2.0. If a copy of the MPL was not distributed with this + file, You can obtain one at http://mozilla.org/MPL/2.0/. + +If it is not possible or desirable to put the notice in a particular +file, then You may include the notice in a location (such as a LICENSE +file in a relevant directory) where a recipient would be likely to look +for such a notice. + +You may add additional accurate notices of copyright ownership. + +Exhibit B - "Incompatible With Secondary Licenses" Notice +--------------------------------------------------------- + + This Source Code Form is "Incompatible With Secondary Licenses", as + defined by the Mozilla Public License, v. 2.0. diff --git a/Resources/Private/Data/EcoLogits/data.json b/Resources/Private/Data/EcoLogits/data.json new file mode 100644 index 0000000..10841b0 --- /dev/null +++ b/Resources/Private/Data/EcoLogits/data.json @@ -0,0 +1,3807 @@ +{ + "_source": { + "project": "EcoLogits (GenAI Impact)", + "repository": "https://github.com/genai-impact/ecologits", + "commit": "d586c3566da37ecd2c35878d77ae6e7de1a0719c", + "date": "2026-10-23", + "files": [ + "ecologits/data/models.json", + "ecologits/data/electricity_mixes.json", + "ecologits/tracers/utils.py" + ], + "license": "MPL-2.0, see LICENSE in this directory", + "note": "Subset for the providers AiM maps; values unchanged except unit conversion of electricity mixes to gCO2eq/kWh. Built by Build/Scripts/updateEcoLogitsData.py." + }, + "providers": { + "openai": { + "location": "USA", + "pue": 1.2 + }, + "anthropic": { + "location": "USA", + "pue": { + "min": 1.09, + "max": 1.14 + } + }, + "google_genai": { + "location": "USA", + "pue": 1.09 + }, + "mistralai": { + "location": "SWE", + "pue": 1.16 + } + }, + "electricityMixes": { + "DEU": 329.65, + "SWE": 35.26, + "USA": 384.4, + "WOR": 458.29 + }, + "aliases": [ + { + "type": "alias", + "provider": "anthropic", + "name": "claude-haiku-4-5", + "alias": "claude-haiku-4-5-20251001" + }, + { + "type": "alias", + "provider": "anthropic", + "name": "claude-sonnet-4-5", + "alias": "claude-sonnet-4-5-20250929" + }, + { + "type": "alias", + "provider": "anthropic", + "name": "claude-opus-4-5", + "alias": "claude-opus-4-5-20251101" + }, + { + "type": "alias", + "provider": "openai", + "name": "gpt-35-turbo", + "alias": "gpt-3.5-turbo" + }, + { + "type": "alias", + "provider": "openai", + "name": "gpt-35-turbo-0125", + "alias": "gpt-3.5-turbo-0125" + }, + { + "type": "alias", + "provider": "openai", + "name": "gpt-35-turbo-1106", + "alias": "gpt-3.5-turbo-1106" + }, + { + "type": "alias", + "provider": "openai", + "name": "gpt-35-turbo-16k", + "alias": "gpt-3.5-turbo-16k" + }, + { + "type": "alias", + "provider": "openai", + "name": "gpt-35-turbo-instruct-0914", + "alias": "gpt-3.5-turbo-instruct-0914" + }, + { + "type": "alias", + "provider": "openai", + "name": "gpt-35-turbo-instruct", + "alias": "gpt-3.5-turbo-instruct" + } + ], + "models": [ + { + "type": "model", + "provider": "mistralai", + "name": "codestral-2508", + "architecture": { + "type": "dense", + "parameters": 22.2 + }, + "warnings": null, + "sources": [ + "https://docs.mistral.ai/getting-started/models/", + "https://mistral.ai/news/codestral" + ], + "deployment": { + "tps": 98.0, + "ttft": 0.38 + } + }, + { + "type": "model", + "provider": "mistralai", + "name": "codestral-latest", + "architecture": { + "type": "dense", + "parameters": 22.2 + }, + "warnings": null, + "sources": [ + "https://docs.mistral.ai/getting-started/models/", + "https://mistral.ai/news/codestral" + ], + "deployment": { + "tps": null, + "ttft": null + } + }, + { + "type": "model", + "provider": "mistralai", + "name": "glm-5-2", + "architecture": { + "type": "moe", + "parameters": { + "total": 744, + "active": 40 + } + }, + "warnings": null, + "sources": [ + "https://docs.mistral.ai/models/zai-glm-5-2", + "https://github.com/zai-org/GLM-5" + ], + "deployment": { + "tps": 79.8, + "ttft": 2.24 + } + }, + { + "type": "model", + "provider": "mistralai", + "name": "labs-leanstral-1-5", + "architecture": { + "type": "moe", + "parameters": { + "total": 119, + "active": 6.5 + } + }, + "warnings": null, + "sources": [ + "https://docs.mistral.ai/models/leanstral-1-5", + "https://mistral.ai/news/leanstral-1-5/" + ], + "deployment": { + "tps": null, + "ttft": null + } + }, + { + "type": "model", + "provider": "mistralai", + "name": "labs-leanstral-1-5-1", + "architecture": { + "type": "moe", + "parameters": { + "total": 119, + "active": 6.5 + } + }, + "warnings": null, + "sources": [ + "https://docs.mistral.ai/models/leanstral-1-5", + "https://mistral.ai/news/leanstral-1-5/" + ], + "deployment": { + "tps": null, + "ttft": null + } + }, + { + "type": "model", + "provider": "mistralai", + "name": "magistral-medium-latest", + "architecture": { + "type": "dense", + "parameters": { + "min": 70, + "max": 120 + } + }, + "warnings": [ + "model-arch-not-released" + ], + "sources": [ + "https://docs.mistral.ai/getting-started/models/" + ], + "deployment": { + "tps": null, + "ttft": null + } + }, + { + "type": "model", + "provider": "mistralai", + "name": "magistral-small-latest", + "architecture": { + "type": "dense", + "parameters": 23.6 + }, + "warnings": null, + "sources": [ + "https://docs.mistral.ai/getting-started/models/" + ], + "deployment": { + "tps": null, + "ttft": null + } + }, + { + "type": "model", + "provider": "mistralai", + "name": "ministral-14b-2512", + "architecture": { + "type": "dense", + "parameters": 14 + }, + "warnings": null, + "sources": [ + "https://docs.mistral.ai/getting-started/models/" + ], + "deployment": { + "tps": 77.0, + "ttft": 0.38 + } + }, + { + "type": "model", + "provider": "mistralai", + "name": "ministral-14b-latest", + "architecture": { + "type": "dense", + "parameters": 14 + }, + "warnings": null, + "sources": [ + "https://docs.mistral.ai/getting-started/models/" + ], + "deployment": { + "tps": null, + "ttft": null + } + }, + { + "type": "model", + "provider": "mistralai", + "name": "ministral-3b-2512", + "architecture": { + "type": "dense", + "parameters": 3.32 + }, + "warnings": null, + "sources": [ + "https://docs.mistral.ai/getting-started/models/", + "https://mistral.ai/news/ministraux" + ], + "deployment": { + "tps": 131.0, + "ttft": 0.32 + } + }, + { + "type": "model", + "provider": "mistralai", + "name": "ministral-3b-latest", + "architecture": { + "type": "dense", + "parameters": 3.32 + }, + "warnings": null, + "sources": [ + "https://docs.mistral.ai/getting-started/models/", + "https://mistral.ai/news/ministraux" + ], + "deployment": { + "tps": null, + "ttft": null + } + }, + { + "type": "model", + "provider": "mistralai", + "name": "ministral-8b-2512", + "architecture": { + "type": "dense", + "parameters": 8.02 + }, + "warnings": null, + "sources": [ + "https://docs.mistral.ai/getting-started/models/", + "https://mistral.ai/news/ministraux" + ], + "deployment": { + "tps": 81.5, + "ttft": 0.32 + } + }, + { + "type": "model", + "provider": "mistralai", + "name": "ministral-8b-latest", + "architecture": { + "type": "dense", + "parameters": 8.02 + }, + "warnings": null, + "sources": [ + "https://docs.mistral.ai/getting-started/models/", + "https://mistral.ai/news/ministraux" + ], + "deployment": { + "tps": null, + "ttft": null + } + }, + { + "type": "model", + "provider": "mistralai", + "name": "mistral-code-fim-latest", + "architecture": { + "type": "dense", + "parameters": 22.2 + }, + "warnings": null, + "sources": [ + "https://docs.mistral.ai/getting-started/models/", + "https://mistral.ai/news/codestral" + ], + "deployment": { + "tps": null, + "ttft": null + } + }, + { + "type": "model", + "provider": "mistralai", + "name": "mistral-large-2512", + "architecture": { + "type": "moe", + "parameters": { + "total": 675, + "active": 41 + } + }, + "warnings": [ + "model-arch-multimodal" + ], + "sources": [ + "https://docs.mistral.ai/models/mistral-large-3-25-12", + "https://mistral.ai/news/mistral-3/", + "https://huggingface.co/mistralai/Mistral-Large-3-675B-Instruct-2512" + ], + "deployment": { + "tps": 59.2, + "ttft": 0.63 + } + }, + { + "type": "model", + "provider": "mistralai", + "name": "mistral-large-4", + "architecture": { + "type": "moe", + "parameters": { + "total": 1050, + "active": 52 + } + }, + "warnings": [ + "model-arch-multimodal" + ], + "sources": [ + "https://docs.mistral.ai/models/mistral-large-4-0" + ], + "deployment": { + "tps": 51.0, + "ttft": 1.11 + } + }, + { + "type": "model", + "provider": "mistralai", + "name": "mistral-large-4-0", + "architecture": { + "type": "moe", + "parameters": { + "total": 1050, + "active": 52 + } + }, + "warnings": [ + "model-arch-multimodal" + ], + "sources": [ + "https://docs.mistral.ai/models/mistral-large-4-0" + ], + "deployment": { + "tps": 51.0, + "ttft": 1.11 + } + }, + { + "type": "model", + "provider": "mistralai", + "name": "mistral-large-latest", + "architecture": { + "type": "moe", + "parameters": { + "total": 675, + "active": 41 + } + }, + "warnings": [ + "model-arch-multimodal" + ], + "sources": [ + "https://docs.mistral.ai/models/mistral-large-3-25-12", + "https://mistral.ai/news/mistral-3/", + "https://huggingface.co/mistralai/Mistral-Large-3-675B-Instruct-2512" + ], + "deployment": { + "tps": 52.0, + "ttft": 0.68 + } + }, + { + "type": "model", + "provider": "mistralai", + "name": "mistral-medium", + "architecture": { + "type": "dense", + "parameters": { + "min": 70, + "max": 120 + } + }, + "warnings": [ + "model-arch-not-released" + ], + "sources": [ + "https://docs.mistral.ai/getting-started/models/" + ], + "deployment": { + "tps": null, + "ttft": null + } + }, + { + "type": "model", + "provider": "mistralai", + "name": "mistral-medium-2604", + "architecture": { + "type": "dense", + "parameters": 128 + }, + "warnings": [ + "model-arch-multimodal" + ], + "sources": [ + "https://docs.mistral.ai/models/model-cards/mistral-medium-3-5-26-04", + "https://huggingface.co/mistralai/Mistral-Medium-3.5-128B" + ], + "deployment": { + "tps": null, + "ttft": null + } + }, + { + "type": "model", + "provider": "mistralai", + "name": "mistral-medium-3", + "architecture": { + "type": "dense", + "parameters": 128 + }, + "warnings": [ + "model-arch-multimodal" + ], + "sources": [ + "https://docs.mistral.ai/models/model-cards/mistral-medium-3-5-26-04", + "https://huggingface.co/mistralai/Mistral-Medium-3.5-128B" + ], + "deployment": { + "tps": 78.5, + "ttft": 0.41 + } + }, + { + "type": "model", + "provider": "mistralai", + "name": "mistral-medium-3-5", + "architecture": { + "type": "dense", + "parameters": 128 + }, + "warnings": [ + "model-arch-multimodal" + ], + "sources": [ + "https://docs.mistral.ai/models/model-cards/mistral-medium-3-5-26-04", + "https://huggingface.co/mistralai/Mistral-Medium-3.5-128B" + ], + "deployment": { + "tps": 93.5, + "ttft": 0.36 + } + }, + { + "type": "model", + "provider": "mistralai", + "name": "mistral-medium-3.5", + "architecture": { + "type": "dense", + "parameters": 128 + }, + "warnings": [ + "model-arch-multimodal" + ], + "sources": [ + "https://docs.mistral.ai/models/model-cards/mistral-medium-3-5-26-04", + "https://huggingface.co/mistralai/Mistral-Medium-3.5-128B" + ], + "deployment": { + "tps": 93.5, + "ttft": 0.36 + } + }, + { + "type": "model", + "provider": "mistralai", + "name": "mistral-medium-latest", + "architecture": { + "type": "dense", + "parameters": { + "min": 70, + "max": 120 + } + }, + "warnings": [ + "model-arch-not-released" + ], + "sources": [ + "https://docs.mistral.ai/getting-started/models/" + ], + "deployment": { + "tps": null, + "ttft": null + } + }, + { + "type": "model", + "provider": "mistralai", + "name": "mistral-small-2603", + "architecture": { + "type": "moe", + "parameters": { + "total": 119, + "active": 8 + } + }, + "warnings": null, + "sources": [ + "https://docs.mistral.ai/getting-started/models/", + "https://mistral.ai/news/mistral-small-4" + ], + "deployment": { + "tps": 110.2, + "ttft": 0.44 + } + }, + { + "type": "model", + "provider": "mistralai", + "name": "mistral-small-latest", + "architecture": { + "type": "moe", + "parameters": { + "total": 119, + "active": 8 + } + }, + "warnings": null, + "sources": [ + "https://docs.mistral.ai/getting-started/models/", + "https://mistral.ai/news/mistral-small-4" + ], + "deployment": { + "tps": null, + "ttft": null + } + }, + { + "type": "model", + "provider": "mistralai", + "name": "voxtral-mini-2602", + "architecture": { + "type": "dense", + "parameters": 4.68 + }, + "warnings": [ + "model-arch-multimodal" + ], + "sources": [ + "https://docs.mistral.ai/getting-started/models/" + ], + "deployment": { + "tps": null, + "ttft": null + } + }, + { + "type": "model", + "provider": "mistralai", + "name": "voxtral-mini-latest", + "architecture": { + "type": "dense", + "parameters": 4.68 + }, + "warnings": [ + "model-arch-multimodal" + ], + "sources": [ + "https://docs.mistral.ai/getting-started/models/" + ], + "deployment": { + "tps": null, + "ttft": null + } + }, + { + "type": "model", + "provider": "mistralai", + "name": "voxtral-small-2507", + "architecture": { + "type": "dense", + "parameters": 24.3 + }, + "warnings": [ + "model-arch-multimodal" + ], + "sources": [ + "https://docs.mistral.ai/getting-started/models/" + ], + "deployment": { + "tps": null, + "ttft": null + } + }, + { + "type": "model", + "provider": "mistralai", + "name": "voxtral-small-latest", + "architecture": { + "type": "dense", + "parameters": 24.3 + }, + "warnings": [ + "model-arch-multimodal" + ], + "sources": [ + "https://docs.mistral.ai/getting-started/models/" + ], + "deployment": { + "tps": null, + "ttft": null + } + }, + { + "type": "model", + "provider": "mistralai", + "name": "zai-glm-5", + "architecture": { + "type": "moe", + "parameters": { + "total": 744, + "active": 40 + } + }, + "warnings": null, + "sources": [ + "https://docs.mistral.ai/models/zai-glm-5-2", + "https://github.com/zai-org/GLM-5" + ], + "deployment": { + "tps": null, + "ttft": null + } + }, + { + "type": "model", + "provider": "mistralai", + "name": "zai-glm-5-2", + "architecture": { + "type": "moe", + "parameters": { + "total": 744, + "active": 40 + } + }, + "warnings": null, + "sources": [ + "https://docs.mistral.ai/models/zai-glm-5-2", + "https://github.com/zai-org/GLM-5" + ], + "deployment": { + "tps": null, + "ttft": null + } + }, + { + "type": "model", + "provider": "mistralai", + "name": "zai-glm-5-3", + "architecture": { + "type": "moe", + "parameters": { + "total": 744, + "active": 40 + } + }, + "warnings": null, + "sources": [ + "https://docs.mistral.ai/models/zai-glm-5-2", + "https://github.com/zai-org/GLM-5" + ], + "deployment": { + "tps": null, + "ttft": null + } + }, + { + "type": "model", + "provider": "mistralai", + "name": "zai-glm-latest", + "architecture": { + "type": "moe", + "parameters": { + "total": 744, + "active": 40 + } + }, + "warnings": null, + "sources": [ + "https://docs.mistral.ai/models/zai-glm-5-2", + "https://github.com/zai-org/GLM-5" + ], + "deployment": { + "tps": null, + "ttft": null + } + }, + { + "type": "model", + "provider": "anthropic", + "name": "claude-fable-5", + "architecture": { + "type": "moe", + "parameters": { + "total": 1340, + "active": { + "min": 134, + "max": 400 + } + } + }, + "warnings": [ + "model-arch-not-released", + "model-arch-multimodal" + ], + "sources": [ + "https://platform.claude.com/docs/en/models/fable-5/overview", + "https://platform.claude.com/docs/en/models/overview" + ], + "deployment": { + "tps": 57.0, + "ttft": 3.58 + } + }, + { + "type": "model", + "provider": "anthropic", + "name": "claude-fable-5-1", + "architecture": { + "type": "moe", + "parameters": { + "total": 1340, + "active": { + "min": 134, + "max": 400 + } + } + }, + "warnings": [ + "model-arch-not-released", + "model-arch-multimodal" + ], + "sources": [ + "https://platform.claude.com/docs/en/models/fable-5-1/overview", + "https://platform.claude.com/docs/en/models/overview" + ], + "deployment": { + "tps": 51.0, + "ttft": 3.89 + } + }, + { + "type": "model", + "provider": "anthropic", + "name": "claude-haiku-4-5-20251001", + "architecture": { + "type": "dense", + "parameters": { + "min": 10, + "max": 35 + } + }, + "warnings": [ + "model-arch-not-released" + ], + "sources": [ + "https://docs.anthropic.com/en/docs/about-claude/models" + ], + "deployment": { + "tps": 79.0, + "ttft": 0.5 + } + }, + { + "type": "model", + "provider": "anthropic", + "name": "claude-haiku-5-5", + "architecture": { + "type": "dense", + "parameters": { + "min": 10, + "max": 35 + } + }, + "warnings": [ + "model-arch-not-released" + ], + "sources": [ + "https://platform.claude.com/docs/en/models/haiku-5-5/overview" + ], + "deployment": { + "tps": 147.0, + "ttft": 1.75 + } + }, + { + "type": "model", + "provider": "anthropic", + "name": "claude-opus-4-5-20251101", + "architecture": { + "type": "moe", + "parameters": { + "total": 670, + "active": { + "min": 67, + "max": 200 + } + } + }, + "warnings": [ + "model-arch-not-released" + ], + "sources": [ + "https://docs.anthropic.com/en/docs/about-claude/models" + ], + "deployment": { + "tps": 30.5, + "ttft": 5.03 + } + }, + { + "type": "model", + "provider": "anthropic", + "name": "claude-opus-4-6", + "architecture": { + "type": "moe", + "parameters": { + "total": 670, + "active": { + "min": 67, + "max": 200 + } + } + }, + "warnings": [ + "model-arch-not-released" + ], + "sources": [ + "https://docs.anthropic.com/en/docs/about-claude/models" + ], + "deployment": { + "tps": 35.0, + "ttft": 1.95 + } + }, + { + "type": "model", + "provider": "anthropic", + "name": "claude-opus-4-7", + "architecture": { + "type": "moe", + "parameters": { + "total": 670, + "active": { + "min": 67, + "max": 200 + } + } + }, + "warnings": [ + "model-arch-not-released" + ], + "sources": [ + "https://docs.anthropic.com/en/docs/about-claude/models" + ], + "deployment": { + "tps": 42.0, + "ttft": 1.75 + } + }, + { + "type": "model", + "provider": "anthropic", + "name": "claude-opus-4-8", + "architecture": { + "type": "moe", + "parameters": { + "total": 670, + "active": { + "min": 67, + "max": 200 + } + } + }, + "warnings": [ + "model-arch-not-released" + ], + "sources": [ + "https://docs.anthropic.com/en/docs/about-claude/models" + ], + "deployment": { + "tps": 110.8, + "ttft": 0.87 + } + }, + { + "type": "model", + "provider": "anthropic", + "name": "claude-opus-5", + "architecture": { + "type": "moe", + "parameters": { + "total": 670, + "active": { + "min": 67, + "max": 200 + } + } + }, + "warnings": [ + "model-arch-not-released", + "model-arch-multimodal" + ], + "sources": [ + "https://platform.claude.com/docs/en/models/opus-5/overview", + "https://platform.claude.com/docs/en/models/overview" + ], + "deployment": { + "tps": 89.5, + "ttft": 1.84 + } + }, + { + "type": "model", + "provider": "anthropic", + "name": "claude-opus-5-5", + "architecture": { + "type": "moe", + "parameters": { + "total": 670, + "active": { + "min": 70, + "max": 200 + } + } + }, + "warnings": [ + "model-arch-not-released", + "model-arch-multimodal" + ], + "sources": [ + "https://platform.claude.com/docs/en/models/opus-5-5/overview", + "https://platform.claude.com/docs/en/models/overview" + ], + "deployment": { + "tps": 102.8, + "ttft": 2.68 + } + }, + { + "type": "model", + "provider": "anthropic", + "name": "claude-sonnet-4-5-20250929", + "architecture": { + "type": "moe", + "parameters": { + "total": 440, + "active": { + "min": 44, + "max": 132 + } + } + }, + "warnings": [ + "model-arch-not-released" + ], + "sources": [ + "https://docs.anthropic.com/en/docs/about-claude/models" + ], + "deployment": { + "tps": 42.0, + "ttft": 1.15 + } + }, + { + "type": "model", + "provider": "anthropic", + "name": "claude-sonnet-4-6", + "architecture": { + "type": "moe", + "parameters": { + "total": 440, + "active": { + "min": 44, + "max": 132 + } + } + }, + "warnings": [ + "model-arch-not-released" + ], + "sources": [ + "https://docs.anthropic.com/en/docs/about-claude/models" + ], + "deployment": { + "tps": 40.0, + "ttft": 1.15 + } + }, + { + "type": "model", + "provider": "anthropic", + "name": "claude-sonnet-5", + "architecture": { + "type": "moe", + "parameters": { + "total": 293, + "active": { + "min": 29, + "max": 88 + } + } + }, + "warnings": [ + "model-arch-not-released", + "model-arch-multimodal" + ], + "sources": [ + "https://platform.claude.com/docs/en/models/sonnet-5/overview", + "https://platform.claude.com/docs/en/models/overview" + ], + "deployment": { + "tps": 79.5, + "ttft": 1.76 + } + }, + { + "type": "model", + "provider": "anthropic", + "name": "claude-sonnet-5-5", + "architecture": { + "type": "moe", + "parameters": { + "total": 293, + "active": { + "min": 29, + "max": 88 + } + } + }, + "warnings": [ + "model-arch-not-released", + "model-arch-multimodal" + ], + "sources": [ + "https://platform.claude.com/docs/en/models/sonnet-5-5/overview", + "https://www.anthropic.com/claude-sonnet-5-5" + ], + "deployment": { + "tps": 102.0, + "ttft": 2.21 + } + }, + { + "type": "model", + "provider": "google_genai", + "name": "gemini-2.5-flash", + "architecture": { + "type": "moe", + "parameters": { + "total": 440, + "active": { + "min": 44, + "max": 132 + } + } + }, + "warnings": [ + "model-arch-not-released", + "model-arch-multimodal" + ], + "sources": [ + "https://ai.google.dev/gemini-api/docs/models" + ], + "deployment": { + "tps": 68.5, + "ttft": 1.14 + } + }, + { + "type": "model", + "provider": "google_genai", + "name": "gemini-2.5-flash-image", + "architecture": { + "type": "moe", + "parameters": { + "total": 440, + "active": { + "min": 44, + "max": 132 + } + } + }, + "warnings": [ + "model-arch-not-released", + "model-arch-multimodal" + ], + "sources": [ + "https://ai.google.dev/gemini-api/docs/models" + ], + "deployment": { + "tps": null, + "ttft": 15.05 + } + }, + { + "type": "model", + "provider": "google_genai", + "name": "gemini-2.5-flash-lite", + "architecture": { + "type": "dense", + "parameters": { + "min": 8, + "max": 28 + } + }, + "warnings": [ + "model-arch-not-released", + "model-arch-multimodal" + ], + "sources": [ + "https://ai.google.dev/gemini-api/docs/models" + ], + "deployment": { + "tps": 131.5, + "ttft": 0.92 + } + }, + { + "type": "model", + "provider": "google_genai", + "name": "gemini-2.5-pro", + "architecture": { + "type": "moe", + "parameters": { + "total": 2000, + "active": { + "min": 200, + "max": 600 + } + } + }, + "warnings": [ + "model-arch-not-released", + "model-arch-multimodal" + ], + "sources": [ + "https://ai.google.dev/gemini-api/docs/models" + ], + "deployment": { + "tps": 91.8, + "ttft": 2.67 + } + }, + { + "type": "model", + "provider": "google_genai", + "name": "gemini-3-flash-preview", + "architecture": { + "type": "moe", + "parameters": { + "total": 300, + "active": { + "min": 100, + "max": 300 + } + } + }, + "warnings": [ + "model-arch-not-released", + "model-arch-multimodal" + ], + "sources": [ + "https://ai.google.dev/gemini-api/docs/models" + ], + "deployment": { + "tps": 86.0, + "ttft": 1.64 + } + }, + { + "type": "model", + "provider": "google_genai", + "name": "gemini-3-pro-image-preview", + "architecture": { + "type": "moe", + "parameters": { + "total": 1200, + "active": { + "min": 120, + "max": 360 + } + } + }, + "warnings": [ + "model-arch-not-released", + "model-arch-multimodal" + ], + "sources": [ + "https://ai.google.dev/gemini-api/docs/models" + ], + "deployment": { + "tps": null, + "ttft": null + } + }, + { + "type": "model", + "provider": "google_genai", + "name": "gemini-3.1-flash-image-preview", + "architecture": { + "type": "moe", + "parameters": { + "total": 1200, + "active": { + "min": 120, + "max": 360 + } + } + }, + "warnings": [ + "model-arch-not-released", + "model-arch-multimodal" + ], + "sources": [ + "https://ai.google.dev/gemini-api/docs/models" + ], + "deployment": { + "tps": null, + "ttft": null + } + }, + { + "type": "model", + "provider": "google_genai", + "name": "gemini-3.1-flash-lite", + "architecture": { + "type": "dense", + "parameters": { + "min": 30, + "max": 105 + } + }, + "warnings": [ + "model-arch-not-released", + "model-arch-multimodal" + ], + "sources": [ + "https://ai.google.dev/gemini-api/docs/models" + ], + "deployment": { + "tps": 107.0, + "ttft": 0.82 + } + }, + { + "type": "model", + "provider": "google_genai", + "name": "gemini-3.1-flash-lite-preview", + "architecture": { + "type": "dense", + "parameters": { + "min": 30, + "max": 105 + } + }, + "warnings": [ + "model-arch-not-released", + "model-arch-multimodal" + ], + "sources": [ + "https://ai.google.dev/gemini-api/docs/models" + ], + "deployment": { + "tps": null, + "ttft": null + } + }, + { + "type": "model", + "provider": "google_genai", + "name": "gemini-3.1-pro-preview", + "architecture": { + "type": "moe", + "parameters": { + "total": 1200, + "active": { + "min": 120, + "max": 360 + } + } + }, + "warnings": [ + "model-arch-not-released", + "model-arch-multimodal" + ], + "sources": [ + "https://ai.google.dev/gemini-api/docs/models" + ], + "deployment": { + "tps": 77.0, + "ttft": 3.12 + } + }, + { + "type": "model", + "provider": "google_genai", + "name": "gemini-3.1-pro-preview-customtools", + "architecture": { + "type": "moe", + "parameters": { + "total": 1200, + "active": { + "min": 120, + "max": 360 + } + } + }, + "warnings": [ + "model-arch-not-released", + "model-arch-multimodal" + ], + "sources": [ + "https://ai.google.dev/gemini-api/docs/models" + ], + "deployment": { + "tps": null, + "ttft": null + } + }, + { + "type": "model", + "provider": "google_genai", + "name": "gemini-3.5-flash", + "architecture": { + "type": "moe", + "parameters": { + "total": 900, + "active": { + "min": 90, + "max": 240 + } + } + }, + "warnings": [ + "model-arch-not-released", + "model-arch-multimodal" + ], + "sources": [ + "https://ai.google.dev/gemini-api/docs/models/gemini-3.5-flash", + "https://deepmind.google/models/model-cards/gemini-3-5-flash/", + "https://blog.google/innovation-and-ai/models-and-research/gemini-models/gemini-3-5/" + ], + "deployment": { + "tps": 73.0, + "ttft": 4.18 + } + }, + { + "type": "model", + "provider": "google_genai", + "name": "gemini-3.5-flash-lite", + "architecture": { + "type": "dense", + "parameters": { + "min": 30, + "max": 105 + } + }, + "warnings": [ + "model-arch-not-released", + "model-arch-multimodal" + ], + "sources": [ + "https://ai.google.dev/gemini-api/docs/models" + ], + "deployment": { + "tps": 137.0, + "ttft": 1.05 + } + }, + { + "type": "model", + "provider": "google_genai", + "name": "gemini-3.6-flash", + "architecture": { + "type": "moe", + "parameters": { + "total": 750, + "active": { + "min": 75, + "max": 200 + } + } + }, + "warnings": [ + "model-arch-not-released", + "model-arch-multimodal" + ], + "sources": [ + "https://ai.google.dev/gemini-api/docs/models/gemini-3.6-flash", + "https://deepmind.google/models/model-cards/gemini-3-6-flash/" + ], + "deployment": { + "tps": 110.5, + "ttft": 1.74 + } + }, + { + "type": "model", + "provider": "google_genai", + "name": "gemini-3.7-flash", + "architecture": { + "type": "moe", + "parameters": { + "total": 750, + "active": { + "min": 75, + "max": 200 + } + } + }, + "warnings": [ + "model-arch-not-released", + "model-arch-multimodal" + ], + "sources": [ + "https://ai.google.dev/gemini-api/docs/models/gemini-3.6-flash", + "https://deepmind.google/models/model-cards/gemini-3-6-flash/" + ], + "deployment": { + "tps": 71.0, + "ttft": 3.01 + } + }, + { + "type": "model", + "provider": "google_genai", + "name": "gemini-3.8-flash", + "architecture": { + "type": "moe", + "parameters": { + "total": 750, + "active": { + "min": 75, + "max": 200 + } + } + }, + "warnings": [ + "model-arch-not-released", + "model-arch-multimodal" + ], + "sources": [ + "https://ai.google.dev/gemini-api/docs/models/gemini-3.6-flash", + "https://deepmind.google/models/model-cards/gemini-3-6-flash/" + ], + "deployment": { + "tps": 74.0, + "ttft": 2.7 + } + }, + { + "type": "model", + "provider": "google_genai", + "name": "gemini-flash-latest", + "architecture": { + "type": "moe", + "parameters": { + "total": 440, + "active": { + "min": 44, + "max": 132 + } + } + }, + "warnings": [ + "model-arch-not-released", + "model-arch-multimodal" + ], + "sources": [ + "https://ai.google.dev/gemini-api/docs/models" + ], + "deployment": { + "tps": null, + "ttft": null + } + }, + { + "type": "model", + "provider": "google_genai", + "name": "gemini-flash-lite-latest", + "architecture": { + "type": "dense", + "parameters": { + "min": 8, + "max": 28 + } + }, + "warnings": [ + "model-arch-not-released", + "model-arch-multimodal" + ], + "sources": [ + "https://ai.google.dev/gemini-api/docs/models" + ], + "deployment": { + "tps": null, + "ttft": null + } + }, + { + "type": "model", + "provider": "google_genai", + "name": "gemini-pro-latest", + "architecture": { + "type": "moe", + "parameters": { + "total": 2000, + "active": { + "min": 200, + "max": 600 + } + } + }, + "warnings": [ + "model-arch-not-released", + "model-arch-multimodal" + ], + "sources": [ + "https://ai.google.dev/gemini-api/docs/models" + ], + "deployment": { + "tps": null, + "ttft": null + } + }, + { + "type": "model", + "provider": "google_genai", + "name": "gemma-4-26b-a4b-it", + "architecture": { + "type": "moe", + "parameters": { + "total": 26, + "active": 4 + } + }, + "warnings": [ + "model-arch-multimodal" + ], + "sources": [ + "https://ai.google.dev/gemma/docs/core/model_card_4", + "https://huggingface.co/google/gemma-4-26B-A4B-it", + "https://deepmind.google/models/gemma/gemma-4/" + ], + "deployment": { + "tps": 20.0, + "ttft": 1.09 + } + }, + { + "type": "model", + "provider": "google_genai", + "name": "gemma-4-31b-it", + "architecture": { + "type": "dense", + "parameters": 31 + }, + "warnings": [ + "model-arch-multimodal" + ], + "sources": [ + "https://ai.google.dev/gemma/docs/core/model_card_4", + "https://huggingface.co/google/gemma-4-31B-it", + "https://deepmind.google/models/gemma/gemma-4/" + ], + "deployment": { + "tps": null, + "ttft": null + } + }, + { + "type": "model", + "provider": "openai", + "name": "chat-latest", + "architecture": { + "type": "moe", + "parameters": { + "total": 900, + "active": { + "min": 90, + "max": 300 + } + } + }, + "warnings": [ + "model-arch-not-released", + "model-arch-multimodal" + ], + "sources": [ + "https://openai.com/index/introducing-gpt-5-5/", + "https://developers.openai.com/api/docs/models/gpt-5.5", + "https://inferencebench.io/models/proprietary/gpt-5-5/" + ], + "deployment": { + "tps": null, + "ttft": null + } + }, + { + "type": "model", + "provider": "openai", + "name": "gpt-3.5-turbo", + "architecture": { + "type": "dense", + "parameters": { + "min": 20, + "max": 70 + } + }, + "warnings": [ + "model-arch-not-released" + ], + "sources": [ + "https://platform.openai.com/docs/models#gpt-3-5-turbo" + ], + "deployment": { + "tps": 88.0, + "ttft": 0.71 + } + }, + { + "type": "model", + "provider": "openai", + "name": "gpt-3.5-turbo-0125", + "architecture": { + "type": "dense", + "parameters": { + "min": 20, + "max": 70 + } + }, + "warnings": [ + "model-arch-not-released" + ], + "sources": [ + "https://platform.openai.com/docs/models#gpt-3-5-turbo" + ], + "deployment": { + "tps": null, + "ttft": null + } + }, + { + "type": "model", + "provider": "openai", + "name": "gpt-3.5-turbo-1106", + "architecture": { + "type": "dense", + "parameters": { + "min": 20, + "max": 70 + } + }, + "warnings": [ + "model-arch-not-released" + ], + "sources": [ + "https://platform.openai.com/docs/models#gpt-3-5-turbo" + ], + "deployment": { + "tps": null, + "ttft": null + } + }, + { + "type": "model", + "provider": "openai", + "name": "gpt-3.5-turbo-16k", + "architecture": { + "type": "dense", + "parameters": { + "min": 20, + "max": 70 + } + }, + "warnings": [ + "model-arch-not-released" + ], + "sources": [ + "https://platform.openai.com/docs/models#gpt-3-5-turbo" + ], + "deployment": { + "tps": 95.0, + "ttft": 0.81 + } + }, + { + "type": "model", + "provider": "openai", + "name": "gpt-3.5-turbo-instruct", + "architecture": { + "type": "dense", + "parameters": { + "min": 20, + "max": 70 + } + }, + "warnings": [ + "model-arch-not-released" + ], + "sources": [ + "https://platform.openai.com/docs/models#gpt-3-5-turbo" + ], + "deployment": { + "tps": null, + "ttft": null + } + }, + { + "type": "model", + "provider": "openai", + "name": "gpt-3.5-turbo-instruct-0914", + "architecture": { + "type": "dense", + "parameters": { + "min": 20, + "max": 70 + } + }, + "warnings": [ + "model-arch-not-released" + ], + "sources": [ + "https://platform.openai.com/docs/models#gpt-3-5-turbo" + ], + "deployment": { + "tps": null, + "ttft": null + } + }, + { + "type": "model", + "provider": "openai", + "name": "gpt-4", + "architecture": { + "type": "moe", + "parameters": { + "total": 1760, + "active": { + "min": 176, + "max": 528 + } + } + }, + "warnings": [ + "model-arch-not-released", + "model-arch-multimodal" + ], + "sources": [ + "https://platform.openai.com/docs/models#gpt-4-turbo-and-gpt-4" + ], + "deployment": { + "tps": 46.0, + "ttft": 0.89 + } + }, + { + "type": "model", + "provider": "openai", + "name": "gpt-4-0613", + "architecture": { + "type": "moe", + "parameters": { + "total": 1760, + "active": { + "min": 176, + "max": 528 + } + } + }, + "warnings": [ + "model-arch-not-released", + "model-arch-multimodal" + ], + "sources": [ + "https://platform.openai.com/docs/models#gpt-4-turbo-and-gpt-4" + ], + "deployment": { + "tps": null, + "ttft": null + } + }, + { + "type": "model", + "provider": "openai", + "name": "gpt-4-turbo", + "architecture": { + "type": "moe", + "parameters": { + "total": 880, + "active": { + "min": 88, + "max": 264 + } + } + }, + "warnings": [ + "model-arch-not-released", + "model-arch-multimodal" + ], + "sources": [ + "https://platform.openai.com/docs/models#gpt-4-turbo-and-gpt-4" + ], + "deployment": { + "tps": 23.0, + "ttft": 1.25 + } + }, + { + "type": "model", + "provider": "openai", + "name": "gpt-4-turbo-2024-04-09", + "architecture": { + "type": "moe", + "parameters": { + "total": 880, + "active": { + "min": 88, + "max": 264 + } + } + }, + "warnings": [ + "model-arch-not-released", + "model-arch-multimodal" + ], + "sources": [ + "https://platform.openai.com/docs/models#gpt-4-turbo-and-gpt-4" + ], + "deployment": { + "tps": 23.0, + "ttft": 1.25 + } + }, + { + "type": "model", + "provider": "openai", + "name": "gpt-4.1", + "architecture": { + "type": "moe", + "parameters": { + "total": 352, + "active": { + "min": 35, + "max": 106 + } + } + }, + "warnings": [ + "model-arch-not-released", + "model-arch-multimodal" + ], + "sources": [ + "https://platform.openai.com/docs/models" + ], + "deployment": { + "tps": 81.0, + "ttft": 1.02 + } + }, + { + "type": "model", + "provider": "openai", + "name": "gpt-4.1-2025-04-14", + "architecture": { + "type": "moe", + "parameters": { + "total": 352, + "active": { + "min": 35, + "max": 106 + } + } + }, + "warnings": [ + "model-arch-not-released", + "model-arch-multimodal" + ], + "sources": [ + "https://platform.openai.com/docs/models" + ], + "deployment": { + "tps": 81.0, + "ttft": 1.02 + } + }, + { + "type": "model", + "provider": "openai", + "name": "gpt-4.1-mini", + "architecture": { + "type": "dense", + "parameters": { + "min": 40, + "max": 112 + } + }, + "warnings": [ + "model-arch-not-released", + "model-arch-multimodal" + ], + "sources": [ + "https://platform.openai.com/docs/models" + ], + "deployment": { + "tps": 79.0, + "ttft": 0.93 + } + }, + { + "type": "model", + "provider": "openai", + "name": "gpt-4.1-mini-2025-04-14", + "architecture": { + "type": "dense", + "parameters": { + "min": 40, + "max": 112 + } + }, + "warnings": [ + "model-arch-not-released", + "model-arch-multimodal" + ], + "sources": [ + "https://platform.openai.com/docs/models" + ], + "deployment": { + "tps": 79.0, + "ttft": 0.93 + } + }, + { + "type": "model", + "provider": "openai", + "name": "gpt-4.1-nano", + "architecture": { + "type": "dense", + "parameters": { + "min": 10, + "max": 37 + } + }, + "warnings": [ + "model-arch-not-released", + "model-arch-multimodal" + ], + "sources": [ + "https://platform.openai.com/docs/models" + ], + "deployment": { + "tps": 83.0, + "ttft": 0.83 + } + }, + { + "type": "model", + "provider": "openai", + "name": "gpt-4.1-nano-2025-04-14", + "architecture": { + "type": "dense", + "parameters": { + "min": 10, + "max": 37 + } + }, + "warnings": [ + "model-arch-not-released", + "model-arch-multimodal" + ], + "sources": [ + "https://platform.openai.com/docs/models" + ], + "deployment": { + "tps": 83.0, + "ttft": 0.83 + } + }, + { + "type": "model", + "provider": "openai", + "name": "gpt-4o", + "architecture": { + "type": "moe", + "parameters": { + "total": 440, + "active": { + "min": 44, + "max": 132 + } + } + }, + "warnings": [ + "model-arch-not-released", + "model-arch-multimodal" + ], + "sources": [ + "https://platform.openai.com/docs/models#gpt-4o" + ], + "deployment": { + "tps": 74.0, + "ttft": 0.61 + } + }, + { + "type": "model", + "provider": "openai", + "name": "gpt-4o-2024-05-13", + "architecture": { + "type": "moe", + "parameters": { + "total": 440, + "active": { + "min": 44, + "max": 132 + } + } + }, + "warnings": [ + "model-arch-not-released", + "model-arch-multimodal" + ], + "sources": [ + "https://platform.openai.com/docs/models#gpt-4o" + ], + "deployment": { + "tps": 44.5, + "ttft": 1.1 + } + }, + { + "type": "model", + "provider": "openai", + "name": "gpt-4o-2024-08-06", + "architecture": { + "type": "moe", + "parameters": { + "total": 440, + "active": { + "min": 44, + "max": 132 + } + } + }, + "warnings": [ + "model-arch-not-released", + "model-arch-multimodal" + ], + "sources": [ + "https://platform.openai.com/docs/models#gpt-4o" + ], + "deployment": { + "tps": 40.0, + "ttft": 2.03 + } + }, + { + "type": "model", + "provider": "openai", + "name": "gpt-4o-2024-11-20", + "architecture": { + "type": "moe", + "parameters": { + "total": 440, + "active": { + "min": 44, + "max": 132 + } + } + }, + "warnings": [ + "model-arch-not-released", + "model-arch-multimodal" + ], + "sources": [ + "https://platform.openai.com/docs/models#gpt-4o" + ], + "deployment": { + "tps": 95.0, + "ttft": 0.67 + } + }, + { + "type": "model", + "provider": "openai", + "name": "gpt-4o-mini", + "architecture": { + "type": "dense", + "parameters": { + "min": 8, + "max": 28 + } + }, + "warnings": [ + "model-arch-not-released", + "model-arch-multimodal" + ], + "sources": [ + "https://platform.openai.com/docs/models#gpt-4o-mini" + ], + "deployment": { + "tps": 79.0, + "ttft": 0.54 + } + }, + { + "type": "model", + "provider": "openai", + "name": "gpt-4o-mini-2024-07-18", + "architecture": { + "type": "dense", + "parameters": { + "min": 8, + "max": 28 + } + }, + "warnings": [ + "model-arch-not-released", + "model-arch-multimodal" + ], + "sources": [ + "https://platform.openai.com/docs/models#gpt-4o-mini" + ], + "deployment": { + "tps": 63.0, + "ttft": 0.53 + } + }, + { + "type": "model", + "provider": "openai", + "name": "gpt-4o-mini-search-preview", + "architecture": { + "type": "dense", + "parameters": { + "min": 8, + "max": 28 + } + }, + "warnings": [ + "model-arch-not-released", + "model-arch-multimodal" + ], + "sources": [ + "https://platform.openai.com/docs/models#gpt-4o-mini" + ], + "deployment": { + "tps": null, + "ttft": null + } + }, + { + "type": "model", + "provider": "openai", + "name": "gpt-4o-mini-search-preview-2025-03-11", + "architecture": { + "type": "dense", + "parameters": { + "min": 8, + "max": 28 + } + }, + "warnings": [ + "model-arch-not-released", + "model-arch-multimodal" + ], + "sources": [ + "https://platform.openai.com/docs/models#gpt-4o-mini" + ], + "deployment": { + "tps": null, + "ttft": null + } + }, + { + "type": "model", + "provider": "openai", + "name": "gpt-4o-mini-transcribe", + "architecture": { + "type": "dense", + "parameters": { + "min": 8, + "max": 28 + } + }, + "warnings": [ + "model-arch-not-released", + "model-arch-multimodal" + ], + "sources": [ + "https://platform.openai.com/docs/models#gpt-4o-mini" + ], + "deployment": { + "tps": null, + "ttft": null + } + }, + { + "type": "model", + "provider": "openai", + "name": "gpt-4o-mini-transcribe-2025-03-20", + "architecture": { + "type": "dense", + "parameters": { + "min": 8, + "max": 28 + } + }, + "warnings": [ + "model-arch-not-released", + "model-arch-multimodal" + ], + "sources": [ + "https://platform.openai.com/docs/models#gpt-4o-mini" + ], + "deployment": { + "tps": null, + "ttft": null + } + }, + { + "type": "model", + "provider": "openai", + "name": "gpt-4o-mini-transcribe-2025-12-15", + "architecture": { + "type": "dense", + "parameters": { + "min": 8, + "max": 28 + } + }, + "warnings": [ + "model-arch-not-released", + "model-arch-multimodal" + ], + "sources": [ + "https://platform.openai.com/docs/models#gpt-4o-mini" + ], + "deployment": { + "tps": null, + "ttft": null + } + }, + { + "type": "model", + "provider": "openai", + "name": "gpt-4o-mini-tts", + "architecture": { + "type": "dense", + "parameters": { + "min": 8, + "max": 28 + } + }, + "warnings": [ + "model-arch-not-released", + "model-arch-multimodal" + ], + "sources": [ + "https://platform.openai.com/docs/models#gpt-4o-mini" + ], + "deployment": { + "tps": null, + "ttft": null + } + }, + { + "type": "model", + "provider": "openai", + "name": "gpt-4o-mini-tts-2025-03-20", + "architecture": { + "type": "dense", + "parameters": { + "min": 8, + "max": 28 + } + }, + "warnings": [ + "model-arch-not-released", + "model-arch-multimodal" + ], + "sources": [ + "https://platform.openai.com/docs/models#gpt-4o-mini" + ], + "deployment": { + "tps": null, + "ttft": null + } + }, + { + "type": "model", + "provider": "openai", + "name": "gpt-4o-mini-tts-2025-12-15", + "architecture": { + "type": "dense", + "parameters": { + "min": 8, + "max": 28 + } + }, + "warnings": [ + "model-arch-not-released", + "model-arch-multimodal" + ], + "sources": [ + "https://platform.openai.com/docs/models#gpt-4o-mini" + ], + "deployment": { + "tps": null, + "ttft": null + } + }, + { + "type": "model", + "provider": "openai", + "name": "gpt-4o-search-preview", + "architecture": { + "type": "moe", + "parameters": { + "total": 440, + "active": { + "min": 44, + "max": 132 + } + } + }, + "warnings": [ + "model-arch-not-released", + "model-arch-multimodal" + ], + "sources": [ + "https://platform.openai.com/docs/models#gpt-4o" + ], + "deployment": { + "tps": null, + "ttft": null + } + }, + { + "type": "model", + "provider": "openai", + "name": "gpt-4o-search-preview-2025-03-11", + "architecture": { + "type": "moe", + "parameters": { + "total": 440, + "active": { + "min": 44, + "max": 132 + } + } + }, + "warnings": [ + "model-arch-not-released", + "model-arch-multimodal" + ], + "sources": [ + "https://platform.openai.com/docs/models#gpt-4o" + ], + "deployment": { + "tps": null, + "ttft": null + } + }, + { + "type": "model", + "provider": "openai", + "name": "gpt-5", + "architecture": { + "type": "moe", + "parameters": { + "total": 300, + "active": { + "min": 30, + "max": 90 + } + } + }, + "warnings": [ + "model-arch-not-released", + "model-arch-multimodal" + ], + "sources": [ + "https://platform.openai.com/docs/models" + ], + "deployment": { + "tps": 59.0, + "ttft": 5.13 + } + }, + { + "type": "model", + "provider": "openai", + "name": "gpt-5-2025-08-07", + "architecture": { + "type": "moe", + "parameters": { + "total": 300, + "active": { + "min": 30, + "max": 90 + } + } + }, + "warnings": [ + "model-arch-not-released", + "model-arch-multimodal" + ], + "sources": [ + "https://platform.openai.com/docs/models" + ], + "deployment": { + "tps": 59.0, + "ttft": 5.13 + } + }, + { + "type": "model", + "provider": "openai", + "name": "gpt-5-chat-latest", + "architecture": { + "type": "moe", + "parameters": { + "total": 300, + "active": { + "min": 30, + "max": 90 + } + } + }, + "warnings": [ + "model-arch-not-released", + "model-arch-multimodal" + ], + "sources": [ + "https://platform.openai.com/docs/models" + ], + "deployment": { + "tps": null, + "ttft": null + } + }, + { + "type": "model", + "provider": "openai", + "name": "gpt-5-codex", + "architecture": { + "type": "moe", + "parameters": { + "total": 300, + "active": { + "min": 30, + "max": 90 + } + } + }, + "warnings": [ + "model-arch-not-released", + "model-arch-multimodal" + ], + "sources": [ + "https://platform.openai.com/docs/models" + ], + "deployment": { + "tps": null, + "ttft": null + } + }, + { + "type": "model", + "provider": "openai", + "name": "gpt-5-mini", + "architecture": { + "type": "dense", + "parameters": { + "min": 25, + "max": 70 + } + }, + "warnings": [ + "model-arch-not-released", + "model-arch-multimodal" + ], + "sources": [ + "https://platform.openai.com/docs/models" + ], + "deployment": { + "tps": 101.0, + "ttft": 3.72 + } + }, + { + "type": "model", + "provider": "openai", + "name": "gpt-5-mini-2025-08-07", + "architecture": { + "type": "dense", + "parameters": { + "min": 25, + "max": 70 + } + }, + "warnings": [ + "model-arch-not-released", + "model-arch-multimodal" + ], + "sources": [ + "https://platform.openai.com/docs/models" + ], + "deployment": { + "tps": 101.0, + "ttft": 3.72 + } + }, + { + "type": "model", + "provider": "openai", + "name": "gpt-5-nano", + "architecture": { + "type": "dense", + "parameters": { + "min": 5, + "max": 18.5 + } + }, + "warnings": [ + "model-arch-not-released", + "model-arch-multimodal" + ], + "sources": [ + "https://platform.openai.com/docs/models" + ], + "deployment": { + "tps": 62.5, + "ttft": 6.93 + } + }, + { + "type": "model", + "provider": "openai", + "name": "gpt-5-nano-2025-08-07", + "architecture": { + "type": "dense", + "parameters": { + "min": 5, + "max": 18.5 + } + }, + "warnings": [ + "model-arch-not-released", + "model-arch-multimodal" + ], + "sources": [ + "https://platform.openai.com/docs/models" + ], + "deployment": { + "tps": 62.5, + "ttft": 6.93 + } + }, + { + "type": "model", + "provider": "openai", + "name": "gpt-5-pro", + "architecture": { + "type": "moe", + "parameters": { + "total": 3600, + "active": { + "min": 360, + "max": 1080 + } + } + }, + "warnings": [ + "model-arch-not-released", + "model-arch-multimodal" + ], + "sources": [ + "https://platform.openai.com/docs/models" + ], + "deployment": { + "tps": 12.0, + "ttft": 21.83 + } + }, + { + "type": "model", + "provider": "openai", + "name": "gpt-5-pro-2025-10-06", + "architecture": { + "type": "moe", + "parameters": { + "total": 3600, + "active": { + "min": 360, + "max": 1080 + } + } + }, + "warnings": [ + "model-arch-not-released", + "model-arch-multimodal" + ], + "sources": [ + "https://platform.openai.com/docs/models" + ], + "deployment": { + "tps": 12.0, + "ttft": 21.83 + } + }, + { + "type": "model", + "provider": "openai", + "name": "gpt-5-search-api", + "architecture": { + "type": "moe", + "parameters": { + "total": 300, + "active": { + "min": 30, + "max": 90 + } + } + }, + "warnings": [ + "model-arch-not-released", + "model-arch-multimodal" + ], + "sources": [ + "https://platform.openai.com/docs/models" + ], + "deployment": { + "tps": null, + "ttft": null + } + }, + { + "type": "model", + "provider": "openai", + "name": "gpt-5-search-api-2025-10-14", + "architecture": { + "type": "moe", + "parameters": { + "total": 300, + "active": { + "min": 30, + "max": 90 + } + } + }, + "warnings": [ + "model-arch-not-released", + "model-arch-multimodal" + ], + "sources": [ + "https://platform.openai.com/docs/models" + ], + "deployment": { + "tps": null, + "ttft": null + } + }, + { + "type": "model", + "provider": "openai", + "name": "gpt-5.1", + "architecture": { + "type": "moe", + "parameters": { + "total": 300, + "active": { + "min": 30, + "max": 90 + } + } + }, + "warnings": [ + "model-arch-not-released", + "model-arch-multimodal" + ], + "sources": [ + "https://platform.openai.com/docs/models" + ], + "deployment": { + "tps": 74.5, + "ttft": 0.97 + } + }, + { + "type": "model", + "provider": "openai", + "name": "gpt-5.1-2025-11-13", + "architecture": { + "type": "moe", + "parameters": { + "total": 300, + "active": { + "min": 30, + "max": 90 + } + } + }, + "warnings": [ + "model-arch-not-released", + "model-arch-multimodal" + ], + "sources": [ + "https://platform.openai.com/docs/models" + ], + "deployment": { + "tps": 74.5, + "ttft": 0.97 + } + }, + { + "type": "model", + "provider": "openai", + "name": "gpt-5.1-chat-latest", + "architecture": { + "type": "moe", + "parameters": { + "total": 300, + "active": { + "min": 30, + "max": 90 + } + } + }, + "warnings": [ + "model-arch-not-released", + "model-arch-multimodal" + ], + "sources": [ + "https://platform.openai.com/docs/models" + ], + "deployment": { + "tps": null, + "ttft": null + } + }, + { + "type": "model", + "provider": "openai", + "name": "gpt-5.1-codex", + "architecture": { + "type": "moe", + "parameters": { + "total": 300, + "active": { + "min": 30, + "max": 90 + } + } + }, + "warnings": [ + "model-arch-not-released", + "model-arch-multimodal" + ], + "sources": [ + "https://platform.openai.com/docs/models" + ], + "deployment": { + "tps": null, + "ttft": null + } + }, + { + "type": "model", + "provider": "openai", + "name": "gpt-5.1-codex-max", + "architecture": { + "type": "moe", + "parameters": { + "total": 300, + "active": { + "min": 30, + "max": 90 + } + } + }, + "warnings": [ + "model-arch-not-released", + "model-arch-multimodal" + ], + "sources": [ + "https://platform.openai.com/docs/models" + ], + "deployment": { + "tps": null, + "ttft": null + } + }, + { + "type": "model", + "provider": "openai", + "name": "gpt-5.1-codex-mini", + "architecture": { + "type": "dense", + "parameters": { + "min": 25, + "max": 70 + } + }, + "warnings": [ + "model-arch-not-released", + "model-arch-multimodal" + ], + "sources": [ + "https://platform.openai.com/docs/models" + ], + "deployment": { + "tps": null, + "ttft": null + } + }, + { + "type": "model", + "provider": "openai", + "name": "gpt-5.2", + "architecture": { + "type": "moe", + "parameters": { + "total": 420, + "active": { + "min": 42, + "max": 126 + } + } + }, + "warnings": [ + "model-arch-not-released", + "model-arch-multimodal" + ], + "sources": [ + "https://platform.openai.com/docs/models" + ], + "deployment": { + "tps": 68.0, + "ttft": 2.41 + } + }, + { + "type": "model", + "provider": "openai", + "name": "gpt-5.2-2025-12-11", + "architecture": { + "type": "moe", + "parameters": { + "total": 420, + "active": { + "min": 42, + "max": 126 + } + } + }, + "warnings": [ + "model-arch-not-released", + "model-arch-multimodal" + ], + "sources": [ + "https://platform.openai.com/docs/models" + ], + "deployment": { + "tps": 68.0, + "ttft": 2.41 + } + }, + { + "type": "model", + "provider": "openai", + "name": "gpt-5.2-chat-latest", + "architecture": { + "type": "moe", + "parameters": { + "total": 420, + "active": { + "min": 42, + "max": 126 + } + } + }, + "warnings": [ + "model-arch-not-released", + "model-arch-multimodal" + ], + "sources": [ + "https://platform.openai.com/docs/models" + ], + "deployment": { + "tps": null, + "ttft": null + } + }, + { + "type": "model", + "provider": "openai", + "name": "gpt-5.2-codex", + "architecture": { + "type": "moe", + "parameters": { + "total": 420, + "active": { + "min": 42, + "max": 126 + } + } + }, + "warnings": [ + "model-arch-not-released", + "model-arch-multimodal" + ], + "sources": [ + "https://platform.openai.com/docs/models" + ], + "deployment": { + "tps": null, + "ttft": null + } + }, + { + "type": "model", + "provider": "openai", + "name": "gpt-5.2-pro", + "architecture": { + "type": "moe", + "parameters": { + "total": 5040, + "active": { + "min": 504, + "max": 1512 + } + } + }, + "warnings": [ + "model-arch-not-released", + "model-arch-multimodal" + ], + "sources": [ + "https://platform.openai.com/docs/models" + ], + "deployment": { + "tps": 19.0, + "ttft": 3.71 + } + }, + { + "type": "model", + "provider": "openai", + "name": "gpt-5.2-pro-2025-12-11", + "architecture": { + "type": "moe", + "parameters": { + "total": 5040, + "active": { + "min": 504, + "max": 1512 + } + } + }, + "warnings": [ + "model-arch-not-released", + "model-arch-multimodal" + ], + "sources": [ + "https://platform.openai.com/docs/models" + ], + "deployment": { + "tps": 19.0, + "ttft": 3.71 + } + }, + { + "type": "model", + "provider": "openai", + "name": "gpt-5.3-chat-latest", + "architecture": { + "type": "moe", + "parameters": { + "total": 420, + "active": { + "min": 42, + "max": 126 + } + } + }, + "warnings": [ + "model-arch-not-released", + "model-arch-multimodal" + ], + "sources": [ + "https://platform.openai.com/docs/models" + ], + "deployment": { + "tps": null, + "ttft": null + } + }, + { + "type": "model", + "provider": "openai", + "name": "gpt-5.3-codex", + "architecture": { + "type": "moe", + "parameters": { + "total": 420, + "active": { + "min": 42, + "max": 126 + } + } + }, + "warnings": [ + "model-arch-not-released", + "model-arch-multimodal" + ], + "sources": [ + "https://platform.openai.com/docs/models" + ], + "deployment": { + "tps": 70.0, + "ttft": 2.29 + } + }, + { + "type": "model", + "provider": "openai", + "name": "gpt-5.4", + "architecture": { + "type": "moe", + "parameters": { + "total": 450, + "active": { + "min": 45, + "max": 150 + } + } + }, + "warnings": [ + "model-arch-not-released", + "model-arch-multimodal" + ], + "sources": [ + "https://platform.openai.com/docs/models" + ], + "deployment": { + "tps": 51.0, + "ttft": 1.56 + } + }, + { + "type": "model", + "provider": "openai", + "name": "gpt-5.4-2026-03-05", + "architecture": { + "type": "moe", + "parameters": { + "total": 450, + "active": { + "min": 45, + "max": 150 + } + } + }, + "warnings": [ + "model-arch-not-released", + "model-arch-multimodal" + ], + "sources": [ + "https://platform.openai.com/docs/models" + ], + "deployment": { + "tps": 51.0, + "ttft": 1.56 + } + }, + { + "type": "model", + "provider": "openai", + "name": "gpt-5.4-mini", + "architecture": { + "type": "dense", + "parameters": { + "min": 56, + "max": 158 + } + }, + "warnings": [ + "model-arch-not-released", + "model-arch-multimodal" + ], + "sources": [ + "https://platform.openai.com/docs/models" + ], + "deployment": { + "tps": 106.0, + "ttft": 1.04 + } + }, + { + "type": "model", + "provider": "openai", + "name": "gpt-5.4-mini-2026-03-17", + "architecture": { + "type": "dense", + "parameters": { + "min": 56, + "max": 158 + } + }, + "warnings": [ + "model-arch-not-released", + "model-arch-multimodal" + ], + "sources": [ + "https://platform.openai.com/docs/models" + ], + "deployment": { + "tps": 106.0, + "ttft": 1.04 + } + }, + { + "type": "model", + "provider": "openai", + "name": "gpt-5.4-nano", + "architecture": { + "type": "dense", + "parameters": { + "min": 15, + "max": 58 + } + }, + "warnings": [ + "model-arch-not-released", + "model-arch-multimodal" + ], + "sources": [ + "https://platform.openai.com/docs/models" + ], + "deployment": { + "tps": 95.5, + "ttft": 1.1 + } + }, + { + "type": "model", + "provider": "openai", + "name": "gpt-5.4-nano-2026-03-17", + "architecture": { + "type": "dense", + "parameters": { + "min": 15, + "max": 58 + } + }, + "warnings": [ + "model-arch-not-released", + "model-arch-multimodal" + ], + "sources": [ + "https://platform.openai.com/docs/models" + ], + "deployment": { + "tps": 95.5, + "ttft": 1.1 + } + }, + { + "type": "model", + "provider": "openai", + "name": "gpt-5.4-pro", + "architecture": { + "type": "moe", + "parameters": { + "total": 5400, + "active": { + "min": 540, + "max": 1800 + } + } + }, + "warnings": [ + "model-arch-not-released", + "model-arch-multimodal" + ], + "sources": [ + "https://platform.openai.com/docs/models" + ], + "deployment": { + "tps": 9.0, + "ttft": 7.12 + } + }, + { + "type": "model", + "provider": "openai", + "name": "gpt-5.4-pro-2026-03-05", + "architecture": { + "type": "moe", + "parameters": { + "total": 5400, + "active": { + "min": 540, + "max": 1800 + } + } + }, + "warnings": [ + "model-arch-not-released", + "model-arch-multimodal" + ], + "sources": [ + "https://platform.openai.com/docs/models" + ], + "deployment": { + "tps": 9.0, + "ttft": 7.12 + } + }, + { + "type": "model", + "provider": "openai", + "name": "gpt-5.5", + "architecture": { + "type": "moe", + "parameters": { + "total": 900, + "active": { + "min": 90, + "max": 300 + } + } + }, + "warnings": [ + "model-arch-not-released", + "model-arch-multimodal" + ], + "sources": [ + "https://openai.com/index/introducing-gpt-5-5/", + "https://developers.openai.com/api/docs/models/gpt-5.5", + "https://inferencebench.io/models/proprietary/gpt-5-5/" + ], + "deployment": { + "tps": 96.0, + "ttft": 4.03 + } + }, + { + "type": "model", + "provider": "openai", + "name": "gpt-5.5-2026-04-23", + "architecture": { + "type": "moe", + "parameters": { + "total": 900, + "active": { + "min": 90, + "max": 300 + } + } + }, + "warnings": [ + "model-arch-not-released", + "model-arch-multimodal" + ], + "sources": [ + "https://openai.com/index/introducing-gpt-5-5/", + "https://developers.openai.com/api/docs/models/gpt-5.5", + "https://inferencebench.io/models/proprietary/gpt-5-5/" + ], + "deployment": { + "tps": 96.0, + "ttft": 4.03 + } + }, + { + "type": "model", + "provider": "openai", + "name": "gpt-5.5-pro", + "architecture": { + "type": "moe", + "parameters": { + "total": 5400, + "active": { + "min": 540, + "max": 1800 + } + } + }, + "warnings": [ + "model-arch-not-released", + "model-arch-multimodal" + ], + "sources": [ + "https://openai.com/index/introducing-gpt-5-5/", + "https://developers.openai.com/api/docs/models/gpt-5.5-pro", + "https://www.swfte.com/research/gpt-5-5" + ], + "deployment": { + "tps": null, + "ttft": null + } + }, + { + "type": "model", + "provider": "openai", + "name": "gpt-5.5-pro-2026-04-23", + "architecture": { + "type": "moe", + "parameters": { + "total": 5400, + "active": { + "min": 540, + "max": 1800 + } + } + }, + "warnings": [ + "model-arch-not-released", + "model-arch-multimodal" + ], + "sources": [ + "https://openai.com/index/introducing-gpt-5-5/", + "https://developers.openai.com/api/docs/models/gpt-5.5-pro", + "https://www.swfte.com/research/gpt-5-5" + ], + "deployment": { + "tps": null, + "ttft": null + } + }, + { + "type": "model", + "provider": "openai", + "name": "gpt-5.6-luna", + "architecture": { + "type": "moe", + "parameters": { + "total": 180, + "active": { + "min": 18, + "max": 60 + } + } + }, + "warnings": [ + "model-arch-not-released", + "model-arch-multimodal" + ], + "sources": [ + "https://openai.com/index/gpt-5-6/", + "https://developers.openai.com/api/docs/models/gpt-5.6-luna" + ], + "deployment": { + "tps": 60.0, + "ttft": 2.0 + } + }, + { + "type": "model", + "provider": "openai", + "name": "gpt-5.6-sol", + "architecture": { + "type": "moe", + "parameters": { + "total": 900, + "active": { + "min": 90, + "max": 300 + } + } + }, + "warnings": [ + "model-arch-not-released", + "model-arch-multimodal" + ], + "sources": [ + "https://openai.com/index/gpt-5-6/", + "https://developers.openai.com/api/docs/models/gpt-5.6-sol" + ], + "deployment": { + "tps": 71.0, + "ttft": 3.26 + } + }, + { + "type": "model", + "provider": "openai", + "name": "gpt-5.6-terra", + "architecture": { + "type": "moe", + "parameters": { + "total": 450, + "active": { + "min": 45, + "max": 150 + } + } + }, + "warnings": [ + "model-arch-not-released", + "model-arch-multimodal" + ], + "sources": [ + "https://openai.com/index/gpt-5-6/", + "https://developers.openai.com/api/docs/models/gpt-5.6-terra" + ], + "deployment": { + "tps": 75.0, + "ttft": 2.15 + } + }, + { + "type": "model", + "provider": "openai", + "name": "gpt-6-astra", + "architecture": { + "type": "moe", + "parameters": { + "total": 1500, + "active": { + "min": 150, + "max": 500 + } + } + }, + "warnings": [ + "model-arch-not-released", + "model-arch-multimodal" + ], + "sources": [ + "https://openai.com/index/gpt-6-astra/", + "https://developers.openai.com/api/docs/models/gpt-6-astra" + ], + "deployment": { + "tps": 35.8, + "ttft": 2.94 + } + }, + { + "type": "model", + "provider": "openai", + "name": "gpt-6-luna", + "architecture": { + "type": "moe", + "parameters": { + "total": 38, + "active": { + "min": 3.8, + "max": 11 + } + } + }, + "warnings": [ + "model-arch-not-released", + "model-arch-multimodal" + ], + "sources": [ + "https://developers.openai.com/api/docs/changelog", + "https://developers.openai.com/api/docs/models/gpt-6-luna" + ], + "deployment": { + "tps": 65.0, + "ttft": 2.6 + } + }, + { + "type": "model", + "provider": "openai", + "name": "gpt-6-sol", + "architecture": { + "type": "moe", + "parameters": { + "total": 375, + "active": { + "min": 38, + "max": 112 + } + } + }, + "warnings": [ + "model-arch-not-released", + "model-arch-multimodal" + ], + "sources": [ + "https://developers.openai.com/api/docs/changelog", + "https://developers.openai.com/api/docs/models/gpt-6-sol" + ], + "deployment": { + "tps": 74.0, + "ttft": 1.93 + } + }, + { + "type": "model", + "provider": "openai", + "name": "gpt-6.1-sol", + "architecture": { + "type": "moe", + "parameters": { + "total": 375, + "active": { + "min": 38, + "max": 112 + } + } + }, + "warnings": [ + "model-arch-not-released", + "model-arch-multimodal" + ], + "sources": [ + "https://developers.openai.com/api/docs/changelog", + "https://developers.openai.com/api/docs/models/gpt-6.1-sol" + ], + "deployment": { + "tps": 57.0, + "ttft": 4.07 + } + }, + { + "type": "model", + "provider": "openai", + "name": "o1", + "architecture": { + "type": "moe", + "parameters": { + "total": 440, + "active": { + "min": 44, + "max": 132 + } + } + }, + "warnings": [ + "model-arch-not-released", + "model-arch-multimodal" + ], + "sources": [ + "https://platform.openai.com/docs/models#o1" + ], + "deployment": { + "tps": 111.5, + "ttft": 0.96 + } + }, + { + "type": "model", + "provider": "openai", + "name": "o1-2024-12-17", + "architecture": { + "type": "moe", + "parameters": { + "total": 440, + "active": { + "min": 44, + "max": 132 + } + } + }, + "warnings": [ + "model-arch-not-released", + "model-arch-multimodal" + ], + "sources": [ + "https://platform.openai.com/docs/models#o1" + ], + "deployment": { + "tps": 111.5, + "ttft": 0.96 + } + }, + { + "type": "model", + "provider": "openai", + "name": "o3-mini", + "architecture": { + "type": "dense", + "parameters": { + "min": 8, + "max": 28 + } + }, + "warnings": [ + "model-arch-not-released", + "model-arch-multimodal" + ], + "sources": [ + "https://platform.openai.com/docs/models#o3-mini" + ], + "deployment": { + "tps": 142.0, + "ttft": 4.49 + } + }, + { + "type": "model", + "provider": "openai", + "name": "o3-mini-2025-01-31", + "architecture": { + "type": "dense", + "parameters": { + "min": 8, + "max": 28 + } + }, + "warnings": [ + "model-arch-not-released", + "model-arch-multimodal" + ], + "sources": [ + "https://platform.openai.com/docs/models#o3-mini" + ], + "deployment": { + "tps": 142.0, + "ttft": 4.49 + } + }, + { + "type": "model", + "provider": "openai", + "name": "o4-mini", + "architecture": { + "type": "dense", + "parameters": { + "min": 8, + "max": 28 + } + }, + "warnings": [ + "model-arch-not-released", + "model-arch-multimodal" + ], + "sources": [ + "https://platform.openai.com/docs/models#o3-mini" + ], + "deployment": { + "tps": 93.0, + "ttft": 3.4 + } + }, + { + "type": "model", + "provider": "openai", + "name": "o4-mini-2025-04-16", + "architecture": { + "type": "dense", + "parameters": { + "min": 8, + "max": 28 + } + }, + "warnings": [ + "model-arch-not-released", + "model-arch-multimodal" + ], + "sources": [ + "https://platform.openai.com/docs/models#o3-mini" + ], + "deployment": { + "tps": 93.0, + "ttft": 3.4 + } + }, + { + "type": "model", + "provider": "openai", + "name": "o4-mini-deep-research", + "architecture": { + "type": "dense", + "parameters": { + "min": 8, + "max": 28 + } + }, + "warnings": [ + "model-arch-not-released", + "model-arch-multimodal" + ], + "sources": [ + "https://platform.openai.com/docs/models#o3-mini" + ], + "deployment": { + "tps": null, + "ttft": null + } + }, + { + "type": "model", + "provider": "openai", + "name": "o4-mini-deep-research-2025-06-26", + "architecture": { + "type": "dense", + "parameters": { + "min": 8, + "max": 28 + } + }, + "warnings": [ + "model-arch-not-released", + "model-arch-multimodal" + ], + "sources": [ + "https://platform.openai.com/docs/models#o3-mini" + ], + "deployment": { + "tps": null, + "ttft": null + } + } + ] +} \ No newline at end of file diff --git a/Resources/Private/Language/locallang_module.xlf b/Resources/Private/Language/locallang_module.xlf index 12d814f..7b97280 100644 --- a/Resources/Private/Language/locallang_module.xlf +++ b/Resources/Private/Language/locallang_module.xlf @@ -463,6 +463,9 @@ Cost + + CO₂e (est.) + Duration @@ -652,6 +655,168 @@ Voice (auto-calibrated) + + Footprint + + + Estimated + + + EcoLogits model + + + Usage-phase estimate from output tokens and model size, EcoLogits methodology. Not included: input tokens and image processing, and the hardware manufacturing share listed above. The range only reflects the assumed model size, it is not a confidence interval. + + + Configured power draw + + + Inference time multiplied by the power draw entered on the provider configuration, a single value without range. As exact as that value: typically chip power under load minus idle, without display, storage and power supply. + + + Active parameters + + + GPUs + + + PUE + + + Output tokens + + + Generation time + + + Power draw + + + Inference time + + + Time source + + + Grid carbon intensity + + + Provider timings, model loading excluded + + + Measured request duration + + + Model architecture not published, size is an assumption + + + Multimodal model, lower precision + + + No data + + + No request through this configuration could be estimated yet. + + + The request failed. + + + Only text-generating requests are estimated. + + + The provider reported no output tokens. + + + No model data for this provider and no power draw configured. + + + The model is not in the EcoLogits model data. + + + Streamed requests are not estimated from a configured power draw yet. + + + No inference time available. + + + Logged before footprint estimation was available. + + + %1$s of %2$s requests estimated + + + Estimated + + + ≈ %s average page views + + + Footprint (est.) + + + Estimated usage-phase CO₂e of the requests that could be estimated, as a range. Requests without an estimate are left out, never counted as zero, so the total is a lower bound; see the coverage below. Hosted and local estimates use different methods. + + + Footprint (est., usage) + + + AI Footprint + + + Estimated usage-phase energy and CO₂e of the AI requests that could be estimated, per provider. + + + Estimates, not measurements, and a lower bound: requests without an estimate are left out. Usage phase only; hosted and local figures use different methods. A page view is 0.36 g CO₂e (Sustainable Web Design Model v4), which also includes devices, network and hardware manufacturing, so the comparison is a sense of scale only. + + + No request has been estimated yet. + + + Hardware manufacturing share + + + set on the provider configuration + + + USA, EcoLogits electricity mix + + + Sweden, EcoLogits electricity mix + + + world average, EcoLogits electricity mix + + + The estimate could not be computed; see the TYPO3 log. + + + Total parameters + + + Avoided by smart routing: ≈ %1$s · routed requests with an estimate: %2$s + + + Added by smart routing: ≈ %1$s · routed requests with an estimate: %2$s + + + Smart routing: between %1$s added and %2$s avoided · routed requests with an estimate: %3$s + + + Smart routing + + + %1$s avoided + + + %1$s added + + + between %1$s added and %2$s avoided + + + Requested model, without smart routing + diff --git a/Resources/Private/Language/locallang_tca.xlf b/Resources/Private/Language/locallang_tca.xlf index e7453aa..93b2d68 100644 --- a/Resources/Private/Language/locallang_tca.xlf +++ b/Resources/Private/Language/locallang_tca.xlf @@ -12,6 +12,24 @@ Cost Tracking + + Environmental Footprint + + + Requests to OpenAI, Anthropic, Gemini and Mistral are estimated from the EcoLogits model data and need nothing here. For a model on your own hardware, such as Ollama or an OpenAI-compatible local server, set the measured power draw; without it, a local provider's requests show "No data". + + + Power Draw During Inference (W) + + + Extra power the machine running the model draws while generating, idle draw subtracted. Measure it, e.g. with powermetrics on macOS; the value is used as entered. Needed for a model on your own hardware: EcoLogits has no data for local providers, so without it their requests show "No data". Leave it empty for OpenAI, Anthropic, Gemini and Mistral; when set, it replaces their EcoLogits estimate. + + + Grid Carbon Intensity (gCO₂e/kWh) + + + Carbon intensity of the electricity the model runs on. Empty uses the EcoLogits electricity data: the data center country for OpenAI, Anthropic, Gemini and Mistral, the world average for a configuration with a power draw. For a local model, set the value of your grid; the world average is rarely right. 0 counts as a value. + Token Costs diff --git a/Resources/Private/Partials/RequestLog/Row.html b/Resources/Private/Partials/RequestLog/Row.html index 51b48be..9cc0a59 100644 --- a/Resources/Private/Partials/RequestLog/Row.html +++ b/Resources/Private/Partials/RequestLog/Row.html @@ -61,6 +61,7 @@ {entry.cost -> f:format.number(decimals: 6)} + {entry.footprint} {entry.duration_ms -> f:format.number(decimals: 0, thousandsSeparator: ',')} ms diff --git a/Resources/Private/Partials/RequestLog/Stats.html b/Resources/Private/Partials/RequestLog/Stats.html index 8d5207f..79f88f6 100644 --- a/Resources/Private/Partials/RequestLog/Stats.html +++ b/Resources/Private/Partials/RequestLog/Stats.html @@ -65,6 +65,20 @@

{statistics.avg_duration_ms -> f:format.number(decimals: 0, thousandsSeparator: ',')} ms

+ +
+
+ + + {statLabel} + +
+

{statistics.footprint_display}

+

{statistics.footprint_coverage}

+

{statistics.footprint_routing}

+
diff --git a/Resources/Private/Partials/RequestLog/Table.html b/Resources/Private/Partials/RequestLog/Table.html index 39c195c..5c72854 100644 --- a/Resources/Private/Partials/RequestLog/Table.html +++ b/Resources/Private/Partials/RequestLog/Table.html @@ -12,6 +12,7 @@ + diff --git a/Resources/Private/Partials/RequestLogDetail.html b/Resources/Private/Partials/RequestLogDetail.html index 213fba9..6910105 100644 --- a/Resources/Private/Partials/RequestLogDetail.html +++ b/Resources/Private/Partials/RequestLogDetail.html @@ -85,6 +85,37 @@ {entry.duration_ms -> f:format.number(decimals: 0, thousandsSeparator: ',')} ms + + + + + + {footprint.co2} + ({footprint.energy}) + · +
+ +
+
{value}
+
+
+ +

+ · +

+
+

+ + (EcoLogits {footprint.dataVersion}) +

+
+ + + + +
+ + diff --git a/Resources/Private/Templates/Aim/Overview.html b/Resources/Private/Templates/Aim/Overview.html index bf7d0e1..a3f005d 100644 --- a/Resources/Private/Templates/Aim/Overview.html +++ b/Resources/Private/Templates/Aim/Overview.html @@ -36,6 +36,7 @@ + @@ -92,6 +93,17 @@ {config.total_cost -> f:format.number(decimals: 2)} {config.cost_currency} + + + + {config.footprint.co2} +
+
+ + + +
+ diff --git a/Resources/Private/Templates/Aim/RequestLogShow.html b/Resources/Private/Templates/Aim/RequestLogShow.html index 2671e1a..9369df7 100644 --- a/Resources/Private/Templates/Aim/RequestLogShow.html +++ b/Resources/Private/Templates/Aim/RequestLogShow.html @@ -12,7 +12,7 @@ />
- +
diff --git a/Resources/Private/Templates/Aim/RequestLogShowContextual.html b/Resources/Private/Templates/Aim/RequestLogShowContextual.html index d81e26c..09261b8 100644 --- a/Resources/Private/Templates/Aim/RequestLogShowContextual.html +++ b/Resources/Private/Templates/Aim/RequestLogShowContextual.html @@ -4,7 +4,7 @@
- +
diff --git a/Resources/Private/Templates/Widget/FootprintWidget.html b/Resources/Private/Templates/Widget/FootprintWidget.html new file mode 100644 index 0000000..39663b2 --- /dev/null +++ b/Resources/Private/Templates/Widget/FootprintWidget.html @@ -0,0 +1,63 @@ + + + + + + +
+

{footprint.total.co2}

+

+ {footprint.total.energy} · + +

+

+ +

+ + + +

+ +

+
+
+
+ + + + + + + + + + + + + + + + + +
{provider.identifier} + + {provider.co2} + + + {provider.estimatedRequests} / {provider.requests}
+
+

+ +

+
+ +

+
+
+ +
+ + + + diff --git a/Resources/Public/Css/base.css b/Resources/Public/Css/base.css index e1ddf98..91867a0 100644 --- a/Resources/Public/Css/base.css +++ b/Resources/Public/Css/base.css @@ -456,6 +456,16 @@ aim-calibrate-voice-trigger .btn, text-shadow: 0 0 calc(14px * var(--aim-glow-op)) var(--rail-glow); } +.aim-stat__value--compact { + font-size: 18px; +} + +.aim-stat__sub { + margin: 4px 0 0; + font-size: 11px; + color: var(--typo3-text-color-secondary); +} + .aim-stat[data-level="good"] { --rail-color: var(--aim-good); --rail-glow: var(--aim-good-glow); } .aim-stat[data-level="warn"] { --rail-color: var(--aim-gold); --rail-glow: var(--aim-gold-glow); } .aim-stat[data-level="critical"] { --rail-color: var(--aim-critical); --rail-glow: var(--aim-critical-glow); } @@ -477,6 +487,8 @@ aim-calibrate-voice-trigger .btn, display: inline-block; font-family: var(--typo3-font-family-monospace); font-size: 10px; + line-height: 1.4; + vertical-align: middle; letter-spacing: 0.04em; text-transform: uppercase; font-weight: 700; diff --git a/Resources/Public/JavaScript/request-log-poll.js b/Resources/Public/JavaScript/request-log-poll.js index d8e4df0..d60c4b4 100644 --- a/Resources/Public/JavaScript/request-log-poll.js +++ b/Resources/Public/JavaScript/request-log-poll.js @@ -119,6 +119,9 @@ class RequestLogPoll { total_tokens: () => Number(stats.total_tokens).toLocaleString(), success_rate: () => stats.success_rate + '%', avg_duration_ms: () => Number(stats.avg_duration_ms).toLocaleString() + ' ms', + footprint_display: () => stats.footprint_display, + footprint_coverage: () => stats.footprint_coverage, + footprint_routing: () => stats.footprint_routing, }; for (const [key, format] of Object.entries(formatters)) { const el = document.querySelector(`[data-stat="${key}"]`); @@ -173,6 +176,7 @@ class RequestLogPoll { this.#tdHtml(this.#renderModel(e)), this.#tdHtml(this.#renderTokens(e)), this.#td(e.cost), + this.#tdTitled(e.footprint, e.footprint_title), this.#td(e.duration_ms + ' ms'), this.#tdHtml(this.#renderGrade(e)), this.#tdHtml(this.#renderStatus(e)), @@ -206,6 +210,13 @@ class RequestLogPoll { return td; } + #tdTitled(text, title) { + const td = this.#td(text); + td.className = 'text-nowrap'; + td.title = title ?? ''; + return td; + } + #tdHtml(html, className = '') { const td = document.createElement('td'); if (className) td.className = className; diff --git a/Tests/Functional/Controller/RequestLogFootprintTest.php b/Tests/Functional/Controller/RequestLogFootprintTest.php new file mode 100644 index 0000000..012254c --- /dev/null +++ b/Tests/Functional/Controller/RequestLogFootprintTest.php @@ -0,0 +1,221 @@ +getConnectionPool()->getConnectionForTable('be_users')->insert('be_users', [ + 'uid' => 1, + 'pid' => 0, + 'username' => 'admin', + 'admin' => 1, + ]); + $this->setUpBackendUser(1); + $GLOBALS['LANG'] = $this->get(LanguageServiceFactory::class)->createFromUserPreferences($GLOBALS['BE_USER']); + } + + #[Test] + public function pollReturnsFormattedFootprintAndCoverage(): void + { + $connection = $this->getConnectionPool()->getConnectionForTable('tx_aim_request_log'); + $connection->insert('tx_aim_request_log', ['provider_identifier' => 'openai', 'co2_g_min' => 0.0016, 'co2_g_max' => 0.0020, 'energy_wh_min' => 0.004, 'energy_wh_max' => 0.005, 'impact_method' => 'model_estimate']); + $connection->insert('tx_aim_request_log', ['provider_identifier' => 'ollama']); + + $body = json_decode((string)$this->get(RequestLogController::class)->pollAction($this->request('/ajax/aim/request-log/poll'))->getBody(), true); + + self::assertSame('1.6–2.0 mg CO₂e', $body['statistics']['footprint_display']); + self::assertSame('1 of 2 requests estimated', $body['statistics']['footprint_coverage']); + self::assertSame('', $body['statistics']['footprint_routing']); + } + + #[Test] + public function pollSaysWhatSmartRoutingAvoided(): void + { + $this->getConnectionPool()->getConnectionForTable('tx_aim_request_log')->insert('tx_aim_request_log', [ + 'provider_identifier' => 'openai', 'co2_g_min' => 0.002, 'co2_g_max' => 0.003, 'co2_g_avoided_min' => 0.097, 'co2_g_avoided_max' => 0.198, + ]); + + $body = json_decode((string)$this->get(RequestLogController::class)->pollAction($this->request('/ajax/aim/request-log/poll'))->getBody(), true); + + self::assertSame('Avoided by smart routing: ≈ 0.10–0.20 g CO₂e · routed requests with an estimate: 1', $body['statistics']['footprint_routing']); + } + + #[Test] + public function pollSaysNoDataInsteadOfZero(): void + { + $this->getConnectionPool()->getConnectionForTable('tx_aim_request_log') + ->insert('tx_aim_request_log', ['provider_identifier' => 'ollama']); + + $body = json_decode((string)$this->get(RequestLogController::class)->pollAction($this->request('/ajax/aim/request-log/poll'))->getBody(), true); + + self::assertSame('No data', $body['statistics']['footprint_display']); + self::assertSame('0 of 1 requests estimated', $body['statistics']['footprint_coverage']); + } + + #[Test] + public function pollRowsCarryTheFootprintCellOrTheReasonForNone(): void + { + $connection = $this->getConnectionPool()->getConnectionForTable('tx_aim_request_log'); + $connection->insert('tx_aim_request_log', ['crdate' => 2, 'provider_identifier' => 'openai', 'co2_g_min' => 0.0016, 'co2_g_max' => 0.0020, 'energy_wh_min' => 0.004, 'energy_wh_max' => 0.005, 'impact_method' => 'model_estimate']); + $connection->insert('tx_aim_request_log', ['crdate' => 1, 'provider_identifier' => 'ollama', 'impact_details' => json_encode(['unavailable' => 'no_data_source'])]); + + $rows = json_decode((string)$this->get(RequestLogController::class)->pollAction($this->request('/ajax/aim/request-log/poll'))->getBody(), true)['rows']; + + self::assertSame('1.6–2.0 mg CO₂e', $rows[0]['footprint']); + self::assertSame('4.0–5.0 mWh', $rows[0]['footprint_title']); + self::assertSame('–', $rows[1]['footprint']); + self::assertSame('No data: No model data for this provider and no power draw configured.', $rows[1]['footprint_title']); + } + + #[Test] + public function listShowsTheFootprintNextToTheCost(): void + { + $connection = $this->getConnectionPool()->getConnectionForTable('tx_aim_request_log'); + $connection->insert('tx_aim_request_log', ['crdate' => 2, 'provider_identifier' => 'openai', 'co2_g_min' => 0.0016, 'co2_g_max' => 0.0020, 'energy_wh_min' => 0.004, 'energy_wh_max' => 0.005, 'impact_method' => 'model_estimate']); + $connection->insert('tx_aim_request_log', ['crdate' => 1, 'provider_identifier' => 'ollama', 'impact_details' => json_encode(['unavailable' => 'no_data_source'])]); + + $html = (string)$this->get(RequestLogController::class)->logAction($this->request('/module/aim/request-log'))->getBody(); + + self::assertStringContainsString('CO₂e (est.)', $html); + self::assertStringContainsString('title="4.0–5.0 mWh">1.6–2.0 mg CO₂e', $html); + self::assertStringContainsString('title="No data: No model data for this provider and no power draw configured.">–', $html); + } + + #[Test] + public function pollRowsCanBeSortedByFootprint(): void + { + $connection = $this->getConnectionPool()->getConnectionForTable('tx_aim_request_log'); + $connection->insert('tx_aim_request_log', ['provider_identifier' => 'none']); + $connection->insert('tx_aim_request_log', ['provider_identifier' => 'small', 'co2_g_min' => 0.001, 'co2_g_max' => 0.002]); + $connection->insert('tx_aim_request_log', ['provider_identifier' => 'large', 'co2_g_min' => 0.1, 'co2_g_max' => 0.2]); + + // Rows without an estimate last in both directions, on every DBMS + foreach (['desc' => ['large', 'small', 'none'], 'asc' => ['small', 'large', 'none']] as $direction => $expected) { + $request = $this->request('/ajax/aim/request-log/poll')->withQueryParams(['orderField' => 'co2_g_max', 'orderDirection' => $direction]); + $rows = json_decode((string)$this->get(RequestLogController::class)->pollAction($request)->getBody(), true)['rows']; + self::assertSame($expected, array_column($rows, 'provider_identifier'), $direction); + } + } + + #[Test] + public function detailShowsTheEstimateWithItsInputs(): void + { + $html = $this->renderDetail([ + 'provider_identifier' => 'openai', + 'co2_g_min' => 0.0016380514, + 'co2_g_max' => 0.0019532425, + 'energy_wh_min' => 0.0042613, + 'energy_wh_max' => 0.0050812, + 'impact_method' => 'model_estimate', + 'impact_details' => json_encode([ + 'method' => 'model_estimate', + 'provider' => 'openai', + 'model' => 'gpt-4o-mini', + 'activeParameters' => ['min' => 8, 'max' => 28], + 'gpus' => ['min' => 1, 'max' => 1], + 'pue' => ['min' => 1.2, 'max' => 1.2], + 'outputTokens' => 60, + 'generationSeconds' => ['min' => 1.3, 'max' => 1.3], + 'embodiedCo2Grams' => ['min' => 0.00021, 'max' => 0.00021], + 'gridIntensity' => 384.4, + 'gridSource' => 'USA', + 'warnings' => ['model-arch-not-released'], + 'dataVersion' => 'd586c3566da37ecd2c35878d77ae6e7de1a0719c', + ]), + ]); + + self::assertStringContainsString('1.6–2.0 mg CO₂e', $html); + self::assertStringContainsString('4.3–5.1 mWh', $html); + self::assertStringContainsString('EcoLogits model', $html); + self::assertStringContainsString('8–28 B', $html); + self::assertStringContainsString('USA, EcoLogits electricity mix', $html); + self::assertStringContainsString('Hardware manufacturing share', $html); + self::assertStringContainsString('Model architecture not published', $html); + self::assertStringContainsString('EcoLogits d586c35', $html); + self::assertStringNotContainsString('gpt-4o-mini (openai)', $html); + self::assertStringNotContainsString('not included above', $html); + } + + #[Test] + public function detailShowsWhyAnEstimateIsMissing(): void + { + $html = $this->renderDetail([ + 'provider_identifier' => 'ollama', + 'impact_details' => json_encode(['unavailable' => 'no_data_source']), + ]); + + self::assertStringContainsString('No data', $html); + self::assertStringContainsString('No model data for this provider and no power draw configured.', $html); + self::assertStringNotContainsString('CO₂e', $html); + } + + #[Test] + public function detailOfARowLoggedBeforeTheFeatureSaysSo(): void + { + $html = $this->renderDetail(['provider_identifier' => 'openai']); + + self::assertStringContainsString('Logged before footprint estimation was available.', $html); + } + + /** + * @param array $row + */ + private function renderDetail(array $row): string + { + $connection = $this->getConnectionPool()->getConnectionForTable('tx_aim_request_log'); + $connection->insert('tx_aim_request_log', $row + ['request_type' => 'VisionRequest', 'success' => 1]); + $uid = (int)$connection->lastInsertId(); + + $request = $this->request('/module/aim/request-log/show-contextual')->withQueryParams(['uid' => $uid]); + return (string)$this->get(RequestLogController::class)->showContextualAction($request)->getBody(); + } + + private function request(string $path): ServerRequestInterface + { + $serverParams = [ + 'HTTP_HOST' => 'typo3-testing.local', + 'SERVER_NAME' => 'typo3-testing.local', + 'SERVER_ADDR' => '127.0.0.1', + 'REMOTE_ADDR' => '127.0.0.1', + 'SCRIPT_NAME' => '/typo3/index.php', + 'REQUEST_URI' => '/typo3' . $path, + 'REQUEST_METHOD' => 'GET', + ]; + $request = new ServerRequest('https://typo3-testing.local/typo3' . $path, 'GET', 'php://input', [], $serverParams); + $request = $request->withAttribute('route', new Route($path, ['packageName' => 'b13/aim'])); + $request = $request->withAttribute('applicationType', SystemEnvironmentBuilder::REQUESTTYPE_BE); + return $request->withAttribute('normalizedParams', NormalizedParams::createFromRequest($request)); + } +} diff --git a/Tests/Functional/Footprint/EstimatorTest.php b/Tests/Functional/Footprint/EstimatorTest.php new file mode 100644 index 0000000..a96fa25 --- /dev/null +++ b/Tests/Functional/Footprint/EstimatorTest.php @@ -0,0 +1,385 @@ + ['openai', 'gpt-4o-mini', 60, 2000, 0.004261320094982515, 0.0050812760923682285, 0.0016380514445112787, 0.001953242529906347, 0.00021150613132911396, 0.00021150613132911396]; + yield 'gpt-4o-mini, dense range, longer output' => ['openai', 'gpt-4o-mini', 300, 10000, 0.019619100474912575, 0.02371888046184114, 0.007541582222556395, 0.009117537649531736, 0.0007059681566455696, 0.0007059681566455696]; + yield 'gpt-4o-mini, latency clamped by measured request time' => ['openai', 'gpt-4o-mini', 300, 1000, 0.01701170332301384, 0.021111483309942405, 0.00653929875736652, 0.008115254184341862, 0.00016276041666666666, 0.00016276041666666666]; + yield 'gpt-4o-mini-2024-07-18, dated model name with its own entry' => ['openai', 'gpt-4o-mini-2024-07-18', 60, 2000, 0.004404200783650387, 0.0052241567810361, 0.001692974781235209, 0.0020081658666302768, 0.00024127294146825397, 0.00024127294146825397]; + yield 'gpt-4o, MoE, active range, several GPUs' => ['openai', 'gpt-4o', 200, 5000, 0.29324939497829317, 0.4856657356981406, 0.11272506742965589, 0.18668990880236525, 0.008626829954954954, 0.008626829954954954]; + yield 'claude-haiku-4-5, alias, PUE range' => ['anthropic', 'claude-haiku-4-5', 150, 3000, 0.0092597559525958, 0.024237519534402834, 0.003559450188177826, 0.00931690250902445, 0.00039041897415611815, 0.0007808379483122363]; + yield 'claude-sonnet-4-5, alias, MoE, PUE range' => ['anthropic', 'claude-sonnet-4-5', 150, 5000, 0.2251743032810215, 0.3726000425246935, 0.08655700218122465, 0.14322745634649217, 0.012295386904761906, 0.012295386904761906]; + yield 'gemini-2.5-flash, MoE' => ['gemini', 'gemini-2.5-flash', 100, 2000, 0.13708594425083973, 0.2244750323277704, 0.052695836970022794, 0.08628820242679495, 0.005208333333333333, 0.005208333333333333]; + yield 'mistral-small-latest, no tps, latency regression, SWE grid' => ['mistral', 'mistral-small-latest', 100, 2000, 0.026960917616328947, 0.026960917616328947, 0.0009506419551517587, 0.0009506419551517587, 0.0013020833333333333, 0.0013020833333333333]; + yield 'mistral-large-latest, MoE, SWE grid' => ['mistral', 'mistral-large-latest', 100, 2000, 0.2854382644415763, 0.2854382644415763, 0.01006455320420998, 0.01006455320420998, 0.010416666666666666, 0.010416666666666666]; + } + + #[Test] + #[DataProvider('ecoLogitsReferenceProvider')] + public function matchesEcoLogitsReferenceValues( + string $provider, + string $model, + int $outputTokens, + int $durationMs, + float $energyMin, + float $energyMax, + float $co2Min, + float $co2Max, + float $embodiedMin, + float $embodiedMax, + ): void { + $estimate = $this->estimate($provider, $model, new AiUsageStatistics(completionTokens: $outputTokens), $durationMs); + + self::assertInstanceOf(Estimate::class, $estimate); + self::assertSame(Method::ModelEstimate, $estimate->method); + self::assertEqualsWithDelta($energyMin, $estimate->energyWhMin, $energyMin * 1e-9); + self::assertEqualsWithDelta($energyMax, $estimate->energyWhMax, $energyMax * 1e-9); + self::assertEqualsWithDelta($co2Min, $estimate->co2GramsMin, $co2Min * 1e-9); + self::assertEqualsWithDelta($co2Max, $estimate->co2GramsMax, $co2Max * 1e-9); + self::assertEqualsWithDelta($embodiedMin, $estimate->details['embodiedCo2Grams']['min'], $embodiedMin * 1e-9); + self::assertEqualsWithDelta($embodiedMax, $estimate->details['embodiedCo2Grams']['max'], $embodiedMax * 1e-9); + } + + #[Test] + public function avoidedIsTheRequestedModelsEstimateMinusTheAnswersWithoutMeasuredDuration(): void + { + $answered = $this->configuration('openai', 'gpt-4o-mini'); + $response = new TextResponse('Hello.', new AiUsageStatistics(completionTokens: 200, modelUsed: 'gpt-4o-mini-2024-07-18')); + $request = new TextGenerationRequest(configuration: $answered, prompt: 'Hi'); + + $avoided = $this->estimator()->estimateAvoided($request, $response, $answered, $this->configuration('openai', 'gpt-4o')); + $answer = $this->estimator()->estimate($request, $response, $answered, 0); + + // EcoLogits reference for gpt-4o, 200 output tokens, generation time unclamped + self::assertInstanceOf(Avoided::class, $avoided); + self::assertInstanceOf(Estimate::class, $answer); + self::assertEqualsWithDelta(0.11272506742965589, $avoided->requested->co2GramsMin, 0.11272506742965589 * 1e-9); + self::assertEqualsWithDelta(0.18668990880236525, $avoided->requested->co2GramsMax, 0.18668990880236525 * 1e-9); + self::assertSame('gpt-4o', $avoided->requested->details['model']); + self::assertEqualsWithDelta($avoided->requested->co2GramsMin - $answer->co2GramsMax, $avoided->co2GramsMin, 1e-12); + self::assertEqualsWithDelta($avoided->requested->co2GramsMax - $answer->co2GramsMin, $avoided->co2GramsMax, 1e-12); + } + + #[Test] + public function avoidedUsesTheGridOfTheRequestedConfiguration(): void + { + $answered = $this->configuration('openai', 'gpt-4o-mini'); + $response = new TextResponse('Hello.', new AiUsageStatistics(completionTokens: 200, modelUsed: 'gpt-4o-mini')); + + $avoided = $this->estimator()->estimateAvoided( + new TextGenerationRequest(configuration: $answered, prompt: 'Hi'), + $response, + $answered, + $this->configuration('openai', 'gpt-4o', ['grid_intensity' => '100.00']), + ); + + self::assertInstanceOf(Avoided::class, $avoided); + self::assertSame(100.0, $avoided->requested->details['gridIntensity']); + self::assertSame('configuration', $avoided->requested->details['gridSource']); + } + + /** + * Reasoning tokens are reported the way the provider that answered does it, + * whichever provider the request was routed away from. + */ + #[Test] + public function avoidedCountsReasoningTokensByTheAnsweringProvider(): void + { + $openAi = $this->configuration('openai', 'gpt-4o-mini'); + $gemini = $this->configuration('gemini', 'gemini-2.5-flash'); + $usage = new AiUsageStatistics(completionTokens: 60, reasoningTokens: 40); + + $fromGemini = $this->estimator()->estimateAvoided( + new TextGenerationRequest(configuration: $openAi, prompt: 'Hi'), + new TextResponse('Hello.', $usage), + $openAi, + $gemini, + ); + $fromOpenAi = $this->estimator()->estimateAvoided( + new TextGenerationRequest(configuration: $gemini, prompt: 'Hi'), + new TextResponse('Hello.', $usage), + $gemini, + $openAi, + ); + + self::assertInstanceOf(Avoided::class, $fromGemini); + self::assertSame(60, $fromGemini->requested->details['outputTokens']); + self::assertInstanceOf(Avoided::class, $fromOpenAi); + self::assertSame(100, $fromOpenAi->requested->details['outputTokens']); + } + + #[Test] + public function avoidedIsNotEstimatedWithoutModelDataOnBothSides(): void + { + $answered = $this->configuration('openai', 'gpt-4o-mini'); + $request = new TextGenerationRequest(configuration: $answered, prompt: 'Hi'); + $response = new TextResponse('Hello.', new AiUsageStatistics(completionTokens: 200)); + $local = $this->configuration('ollama', 'gemma3:4b', ['power_draw_watts' => '26.09']); + + self::assertNull($this->estimator()->estimateAvoided($request, $response, $answered, $this->configuration('ollama', 'gemma3:4b'))); + self::assertNull($this->estimator()->estimateAvoided($request, $response, $answered, $this->configuration('openai', 'unknown-model'))); + self::assertNull($this->estimator()->estimateAvoided($request, $response, $local, $this->configuration('openai', 'gpt-4o'))); + self::assertNull($this->estimator()->estimateAvoided($request, $response, $answered, $this->configuration('openai', 'gpt-4o', ['power_draw_watts' => '30']))); + self::assertNull($this->estimator()->estimateAvoided($request, new TextResponse('', errors: ['failed']), $answered, $this->configuration('openai', 'gpt-4o'))); + } + + #[Test] + public function geminiReasoningTokensAreAddedToOutputTokens(): void + { + // Gemini reports thoughtsTokenCount separately from candidatesTokenCount + $estimate = $this->estimate('gemini', 'gemini-2.5-flash', new AiUsageStatistics(completionTokens: 60, reasoningTokens: 40), 2000); + + self::assertInstanceOf(Estimate::class, $estimate); + self::assertEqualsWithDelta(0.052695836970022794, $estimate->co2GramsMin, 1e-12); + self::assertSame(100, $estimate->details['outputTokens']); + } + + #[Test] + public function openAiReasoningTokensAreNotCountedTwice(): void + { + // OpenAI's output_tokens already include reasoning_tokens + $estimate = $this->estimate('openai', 'gpt-4o-mini', new AiUsageStatistics(completionTokens: 60, reasoningTokens: 30), 2000); + + self::assertInstanceOf(Estimate::class, $estimate); + self::assertEqualsWithDelta(0.0016380514445112787, $estimate->co2GramsMin, 1e-12); + } + + #[Test] + public function undatedNameIsTriedWhenTheDatedOneIsUnknown(): void + { + $dated = $this->estimate('openai', 'gpt-4.1-mini-2099-01-01', new AiUsageStatistics(completionTokens: 60), 2000); + $plain = $this->estimate('openai', 'gpt-4.1-mini', new AiUsageStatistics(completionTokens: 60), 2000); + + self::assertInstanceOf(Estimate::class, $dated); + self::assertInstanceOf(Estimate::class, $plain); + self::assertSame($plain->co2GramsMax, $dated->co2GramsMax); + } + + #[Test] + public function configuredModelIsUsedWhenTheProviderReportsNone(): void + { + $estimate = $this->estimate('openai', '', new AiUsageStatistics(completionTokens: 60), 2000, ['model' => 'gpt-4o-mini']); + + self::assertInstanceOf(Estimate::class, $estimate); + self::assertSame('gpt-4o-mini', $estimate->details['model']); + } + + #[Test] + public function configuredGridIntensityOverridesTheDataCenterDefault(): void + { + $estimate = $this->estimate('openai', 'gpt-4o-mini', new AiUsageStatistics(completionTokens: 60), 2000, ['grid_intensity' => '100.00']); + + self::assertInstanceOf(Estimate::class, $estimate); + self::assertEqualsWithDelta($estimate->energyWhMin / 1000 * 100, $estimate->co2GramsMin, 1e-15); + self::assertSame('configuration', $estimate->details['gridSource']); + } + + #[Test] + public function configuredGridIntensityOfZeroIsTakenAsSet(): void + { + $estimate = $this->estimate('openai', 'gpt-4o-mini', new AiUsageStatistics(completionTokens: 60), 2000, ['grid_intensity' => '0.00']); + + self::assertInstanceOf(Estimate::class, $estimate); + self::assertSame(0.0, $estimate->co2GramsMax); + self::assertGreaterThan(0.0, $estimate->energyWhMax); + self::assertSame('configuration', $estimate->details['gridSource']); + } + + #[Test] + public function configuredPowerDrawTakesPrecedenceOverModelData(): void + { + // An OpenAI configuration pointed at a local OpenAI-compatible server + $estimate = $this->estimate('openai', 'gpt-oss:20b', new AiUsageStatistics(completionTokens: 60), 2000, ['power_draw_watts' => '50', 'endpoint' => 'http://localhost:11434/v1']); + + self::assertInstanceOf(Estimate::class, $estimate); + self::assertSame(Method::DurationCalibrated, $estimate->method); + self::assertEqualsWithDelta(50 * 2 / 3600, $estimate->energyWhMin, 1e-12); + } + + #[Test] + public function localEstimateUsesProviderTimingsWithoutModelLoading(): void + { + $usage = new AiUsageStatistics( + completionTokens: 12, + modelUsed: 'gemma3:4b', + rawUsage: ['total_duration' => 3_000_000_000, 'load_duration' => 1_000_000_000], + ); + $estimate = $this->estimate('ollama', 'gemma3:4b', $usage, 9000, ['power_draw_watts' => '36.00']); + + self::assertInstanceOf(Estimate::class, $estimate); + self::assertSame(Method::DurationCalibrated, $estimate->method); + // 36 W × 2 s + self::assertEqualsWithDelta(0.02, $estimate->energyWhMin, 1e-12); + self::assertSame($estimate->energyWhMin, $estimate->energyWhMax); + // World average 458.29 gCO2e/kWh + self::assertEqualsWithDelta(0.02 / 1000 * 458.29, $estimate->co2GramsMin, 1e-12); + self::assertSame('provider_timings', $estimate->details['durationSource']); + self::assertSame('WOR', $estimate->details['gridSource']); + } + + #[Test] + public function localEstimateFallsBackToTheRequestDuration(): void + { + $estimate = $this->estimate('lmstudio', 'qwen', new AiUsageStatistics(completionTokens: 12), 4000, ['power_draw_watts' => '18', 'grid_intensity' => '329.65']); + + self::assertInstanceOf(Estimate::class, $estimate); + // 18 W × 4 s + self::assertEqualsWithDelta(0.02, $estimate->energyWhMin, 1e-12); + self::assertEqualsWithDelta(0.02 / 1000 * 329.65, $estimate->co2GramsMin, 1e-12); + self::assertSame('request_duration', $estimate->details['durationSource']); + } + + #[Test] + public function streamedLocalRequestIsNotEstimated(): void + { + $estimate = $this->estimate('ollama', 'gemma3:4b', new AiUsageStatistics(completionTokens: 12), 4000, ['power_draw_watts' => '18'], streamed: true); + + self::assertSame(Unavailable::StreamedLocalRequest, $estimate); + } + + #[Test] + public function localProviderWithoutPowerDrawHasNoData(): void + { + self::assertSame( + Unavailable::NoDataSource, + $this->estimate('ollama', 'gemma3:4b', new AiUsageStatistics(completionTokens: 12), 4000), + ); + } + + #[Test] + public function unknownModelHasNoData(): void + { + self::assertSame( + Unavailable::UnknownModel, + $this->estimate('openai', 'gpt-does-not-exist', new AiUsageStatistics(completionTokens: 60), 2000), + ); + } + + #[Test] + public function modelNameIsMatchedPerProvider(): void + { + // An OpenAI model name on Mistral must not borrow OpenAI's data + self::assertSame( + Unavailable::UnknownModel, + $this->estimate('mistral', 'gpt-4o-mini', new AiUsageStatistics(completionTokens: 60), 2000), + ); + } + + #[Test] + public function responseWithoutOutputTokensHasNoData(): void + { + self::assertSame( + Unavailable::NoOutputTokens, + $this->estimate('openai', 'gpt-4o-mini', new AiUsageStatistics(promptTokens: 20), 2000), + ); + } + + #[Test] + public function failedRequestHasNoData(): void + { + $configuration = $this->configuration('openai', 'gpt-4o-mini'); + $request = new TextGenerationRequest(configuration: $configuration, prompt: 'Hi'); + $response = new TextResponse('', new AiUsageStatistics(completionTokens: 60), errors: ['Rate limited']); + + self::assertSame(Unavailable::RequestFailed, $this->estimator()->estimate($request, $response, $configuration, 2000)); + self::assertSame(Unavailable::RequestFailed, $this->estimator()->estimate($request, null, $configuration, 2000)); + } + + #[Test] + public function embeddingRequestIsNotEstimated(): void + { + $configuration = $this->configuration('openai', 'text-embedding-3-small'); + $request = new EmbeddingRequest(configuration: $configuration, input: ['Hello']); + $response = new TextResponse('[0.1]', new AiUsageStatistics(promptTokens: 2)); + + self::assertSame(Unavailable::UnsupportedRequestType, $this->estimator()->estimate($request, $response, $configuration, 200)); + } + + #[Test] + public function visionRequestIsEstimated(): void + { + $configuration = $this->configuration('openai', 'gpt-4o-mini'); + $request = new VisionRequest(configuration: $configuration, imageData: 'aGVsbG8=', mimeType: 'image/png', prompt: 'Describe'); + $response = new TextResponse('A red bicycle.', new AiUsageStatistics(completionTokens: 60)); + + self::assertInstanceOf(Estimate::class, $this->estimator()->estimate($request, $response, $configuration, 2000)); + } + + /** + * @param array $row + */ + private function estimate( + string $provider, + string $model, + AiUsageStatistics $usage, + int $durationMs, + array $row = [], + bool $streamed = false, + ): Estimate|Unavailable { + if ($model !== '' && $usage->modelUsed === '') { + $usage = new AiUsageStatistics( + promptTokens: $usage->promptTokens, + completionTokens: $usage->completionTokens, + reasoningTokens: $usage->reasoningTokens, + modelUsed: $model, + rawUsage: $usage->rawUsage, + ); + } + $configuration = $this->configuration($provider, (string)($row['model'] ?? $model), $row); + $request = new TextGenerationRequest(configuration: $configuration, prompt: 'Write an alt text.'); + $response = new TextResponse('A red bicycle against a brick wall.', $usage); + + return $this->estimator()->estimate($request, $response, $configuration, $durationMs, $streamed); + } + + /** + * @param array $row + */ + private function configuration(string $provider, string $model, array $row = []): ProviderConfiguration + { + return new ProviderConfiguration(array_merge(['uid' => 1, 'ai_provider' => $provider, 'model' => $model], $row)); + } + + private function estimator(): Estimator + { + return new Estimator(new EcoLogitsData()); + } +} diff --git a/Tests/Functional/Footprint/FootprintLoggingTest.php b/Tests/Functional/Footprint/FootprintLoggingTest.php new file mode 100644 index 0000000..883b446 --- /dev/null +++ b/Tests/Functional/Footprint/FootprintLoggingTest.php @@ -0,0 +1,353 @@ +log( + ['ai_provider' => 'openai', 'model' => 'gpt-4o-mini'], + new TextResponse('A red bicycle.', new AiUsageStatistics(completionTokens: 60, modelUsed: 'gpt-4o-mini')), + ); + + $row = $this->fetchSingleLogRow(); + self::assertSame('model_estimate', $row['impact_method']); + self::assertGreaterThan(0.0, (float)$row['co2_g_min']); + self::assertGreaterThanOrEqual((float)$row['co2_g_min'], (float)$row['co2_g_max']); + self::assertGreaterThan(0.0, (float)$row['energy_wh_min']); + $details = json_decode($row['impact_details'], true); + self::assertSame('gpt-4o-mini', $details['model']); + self::assertSame(60, $details['outputTokens']); + self::assertSame('USA', $details['gridSource']); + } + + #[Test] + public function theDetailViewReadsWhatAModelEstimateLogged(): void + { + $configuration = ProviderConfigurationFactory::markedAsDowngrade( + $this->configuration(['ai_provider' => 'openai', 'model' => 'gpt-4o-mini']), + 'gpt-4o', + 'smart routing: simple prompt', + $this->configuration(['ai_provider' => 'openai', 'model' => 'gpt-4o']), + ); + $this->middleware()->process( + new TextGenerationRequest(configuration: $configuration, prompt: 'Hi'), + $this->createMock(AiProviderInterface::class), + $configuration, + new AiMiddlewareHandler(static fn() => new TextResponse('Hello.', new AiUsageStatistics(completionTokens: 60, modelUsed: 'gpt-4o-mini-2024-07-18'))), + ); + + $footprint = $this->display()->entry($this->fetchSingleLogRow()); + + self::assertTrue($footprint['available']); + self::assertSame( + ['activeParameters', 'gpus', 'pue', 'outputTokens', 'generationSeconds', 'embodiedCo2Grams', 'requestedModel', 'routingChange', 'gridIntensity'], + array_keys($footprint['facts']), + ); + self::assertSame('8–28 B', $footprint['facts']['activeParameters']); + self::assertSame('60', $footprint['facts']['outputTokens']); + self::assertStringEndsWith('CO₂e avoided', $footprint['facts']['routingChange']); + self::assertStringContainsString('USA', $footprint['facts']['gridIntensity']); + self::assertContains('model-arch-not-released', $footprint['warnings']); + self::assertSame('d586c35', $footprint['dataVersion']); + } + + #[Test] + public function theDetailViewReadsWhatAConfiguredPowerDrawLogged(): void + { + $this->log( + ['ai_provider' => 'ollama', 'model' => 'gemma3:4b', 'power_draw_watts' => '36.00'], + new TextResponse('A red bicycle.', new AiUsageStatistics( + completionTokens: 12, + rawUsage: ['total_duration' => 3_000_000_000, 'load_duration' => 1_000_000_000], + )), + ); + + $footprint = $this->display()->entry($this->fetchSingleLogRow()); + + self::assertSame(['powerDrawWatts', 'inferenceSeconds', 'durationSource', 'gridIntensity'], array_keys($footprint['facts'])); + self::assertSame('36.0 W', $footprint['facts']['powerDrawWatts']); + self::assertSame('2.00 s', $footprint['facts']['inferenceSeconds']); + self::assertNotSame('', $footprint['facts']['durationSource']); + } + + #[Test] + public function smartRoutedRequestStoresWhatRoutingAvoided(): void + { + $configuration = ProviderConfigurationFactory::markedAsDowngrade( + $this->configuration(['ai_provider' => 'openai', 'model' => 'gpt-4o-mini']), + 'gpt-4o', + 'smart routing: simple prompt', + $this->configuration(['ai_provider' => 'openai', 'model' => 'gpt-4o']), + ); + $this->middleware()->process( + new TextGenerationRequest(configuration: $configuration, prompt: 'Hi'), + $this->createMock(AiProviderInterface::class), + $configuration, + new AiMiddlewareHandler(static fn() => new TextResponse('Hello.', new AiUsageStatistics(completionTokens: 200, modelUsed: 'gpt-4o-mini-2024-07-18'))), + ); + + $row = $this->fetchSingleLogRow(); + self::assertSame('gpt-4o', $row['model_requested']); + $requested = json_decode($row['impact_details'], true)['requestedModel']; + self::assertSame('gpt-4o', $requested['model']); + self::assertEqualsWithDelta(0.1127250674, $requested['co2Grams']['min'], 1e-9); + self::assertEqualsWithDelta((float)$row['co2_g_avoided_min'], $requested['avoidedCo2Grams']['min'], 1e-9); + self::assertGreaterThan(0.0, (float)$row['co2_g_avoided_min']); + self::assertGreaterThan((float)$row['co2_g_avoided_min'], (float)$row['co2_g_avoided_max']); + } + + #[Test] + public function requestWithoutSmartRoutingHasNoRequestedModelEstimate(): void + { + $this->log( + ['ai_provider' => 'openai', 'model' => 'gpt-4o-mini'], + new TextResponse('Hello.', new AiUsageStatistics(completionTokens: 60, modelUsed: 'gpt-4o-mini')), + ); + + self::assertNull($this->fetchSingleLogRow()['co2_g_avoided_min']); + } + + #[Test] + public function statisticsSumWhatSmartRoutingAvoided(): void + { + $connection = $this->getConnectionPool()->getConnectionForTable('tx_aim_request_log'); + $connection->insert('tx_aim_request_log', ['provider_identifier' => 'openai', 'co2_g_min' => 0.002, 'co2_g_max' => 0.003, 'co2_g_avoided_min' => 0.097, 'co2_g_avoided_max' => 0.198]); + $connection->insert('tx_aim_request_log', ['provider_identifier' => 'openai', 'co2_g_min' => 0.002, 'co2_g_max' => 0.003, 'co2_g_avoided_min' => -0.001, 'co2_g_avoided_max' => 0.196]); + $connection->insert('tx_aim_request_log', ['provider_identifier' => 'openai', 'co2_g_min' => 0.002, 'co2_g_max' => 0.003]); + + $statistics = $this->get(RequestLogRepository::class)->getStatistics(); + + self::assertSame(2, $statistics['footprint_routing_estimated_requests']); + self::assertEqualsWithDelta(0.096, $statistics['footprint_avoided_min'], 1e-9); + self::assertEqualsWithDelta(0.394, $statistics['footprint_avoided_max'], 1e-9); + } + + #[Test] + public function missingEstimateIsPersistedAsNullWithItsReason(): void + { + $this->log( + ['ai_provider' => 'ollama', 'model' => 'gemma3:4b'], + new TextResponse('A red bicycle.', new AiUsageStatistics(completionTokens: 12)), + ); + + $row = $this->fetchSingleLogRow(); + self::assertNull($row['co2_g_min']); + self::assertNull($row['co2_g_max']); + self::assertNull($row['energy_wh_min']); + self::assertNull($row['energy_wh_max']); + self::assertSame('', $row['impact_method']); + self::assertSame(['unavailable' => 'no_data_source'], json_decode($row['impact_details'], true)); + } + + #[Test] + public function reducedPrivacyStillEstimatesFromProviderTimings(): void + { + $this->log( + ['ai_provider' => 'ollama', 'model' => 'gemma3:4b', 'privacy_level' => 'reduced', 'power_draw_watts' => '36.00'], + new TextResponse('A red bicycle.', new AiUsageStatistics( + completionTokens: 12, + rawUsage: ['total_duration' => 3_000_000_000, 'load_duration' => 1_000_000_000], + )), + ); + + $row = $this->fetchSingleLogRow(); + self::assertSame('', $row['raw_usage']); + self::assertSame('duration_calibrated', $row['impact_method']); + self::assertEqualsWithDelta(0.02, (float)$row['energy_wh_min'], 1e-9); + } + + #[Test] + public function estimationErrorDoesNotBreakTheResponse(): void + { + $estimator = $this->createMock(Estimator::class); + $estimator->method('estimate')->willThrowException(new \DivisionByZeroError('Division by zero')); + $configuration = $this->configuration(['ai_provider' => 'openai', 'model' => 'gpt-4o-mini']); + $response = (new RequestLoggingMiddleware($this->get(RequestLogRepository::class), new NullLogger(), $estimator))->process( + new TextGenerationRequest(configuration: $configuration, prompt: 'Write an alt text.'), + $this->createMock(AiProviderInterface::class), + $configuration, + new AiMiddlewareHandler(static fn() => new TextResponse('A red bicycle.', new AiUsageStatistics(completionTokens: 60))), + ); + + self::assertSame('A red bicycle.', $response->content); + $row = $this->fetchSingleLogRow(); + self::assertNull($row['co2_g_min']); + self::assertSame(['unavailable' => 'estimation_failed'], json_decode($row['impact_details'], true)); + } + + #[Test] + public function failedRequestIsLoggedWithoutEstimate(): void + { + $this->log( + ['ai_provider' => 'openai', 'model' => 'gpt-4o-mini'], + new TextResponse('', new AiUsageStatistics(completionTokens: 60), errors: ['Rate limited']), + ); + + $row = $this->fetchSingleLogRow(); + self::assertNull($row['co2_g_min']); + self::assertSame(['unavailable' => 'request_failed'], json_decode($row['impact_details'], true)); + } + + #[Test] + public function embeddingRequestIsLoggedWithoutEstimate(): void + { + $configuration = $this->configuration(['ai_provider' => 'openai', 'model' => 'text-embedding-3-small']); + $this->middleware()->process( + new EmbeddingRequest(configuration: $configuration, input: ['Hello']), + $this->createMock(AiProviderInterface::class), + $configuration, + new AiMiddlewareHandler(static fn() => new TextResponse('[0.1]', new AiUsageStatistics(promptTokens: 2))), + ); + + $row = $this->fetchSingleLogRow(); + self::assertNull($row['co2_g_min']); + self::assertSame(['unavailable' => 'unsupported_request_type'], json_decode($row['impact_details'], true)); + } + + #[Test] + public function statisticsSumOnlyEstimatedRequestsAndReportCoverage(): void + { + $this->insertLogRows(); + $statistics = $this->get(RequestLogRepository::class)->getStatistics(); + + self::assertSame(4, $statistics['total_requests']); + self::assertSame(2, $statistics['footprint_estimated_requests']); + self::assertEqualsWithDelta(0.3, $statistics['footprint_co2_min'], 1e-9); + self::assertEqualsWithDelta(0.6, $statistics['footprint_co2_max'], 1e-9); + self::assertEqualsWithDelta(3.0, $statistics['footprint_energy_min'], 1e-9); + self::assertEqualsWithDelta(6.0, $statistics['footprint_energy_max'], 1e-9); + } + + #[Test] + public function statisticsWithoutAnyEstimateReportNoCoverage(): void + { + $this->getConnectionPool()->getConnectionForTable('tx_aim_request_log') + ->insert('tx_aim_request_log', ['configuration_uid' => 1, 'provider_identifier' => 'ollama']); + $statistics = $this->get(RequestLogRepository::class)->getStatistics(); + + self::assertSame(1, $statistics['total_requests']); + self::assertSame(0, $statistics['footprint_estimated_requests']); + } + + #[Test] + public function footprintIsAggregatedPerConfiguration(): void + { + $this->insertLogRows(); + $perConfiguration = $this->get(RequestLogRepository::class)->getFootprintPerConfiguration(); + + self::assertSame(3, $perConfiguration[1]['requests']); + self::assertSame(2, $perConfiguration[1]['estimated_requests']); + self::assertEqualsWithDelta(0.3, $perConfiguration[1]['co2_min'], 1e-9); + self::assertSame(1, $perConfiguration[2]['requests']); + self::assertSame(0, $perConfiguration[2]['estimated_requests']); + self::assertSame(0.0, $perConfiguration[2]['co2_max']); + } + + #[Test] + public function footprintIsAggregatedPerProvider(): void + { + $this->insertLogRows(); + $perProvider = $this->get(RequestLogRepository::class)->getFootprintByProvider(); + + $byIdentifier = array_column($perProvider, null, 'provider_identifier'); + self::assertSame(3, $byIdentifier['openai']['requests']); + self::assertSame(2, $byIdentifier['openai']['estimated_requests']); + self::assertEqualsWithDelta(0.6, $byIdentifier['openai']['co2_max'], 1e-9); + self::assertSame(0, $byIdentifier['ollama']['estimated_requests']); + } + + private function insertLogRows(): void + { + $connection = $this->getConnectionPool()->getConnectionForTable('tx_aim_request_log'); + $estimated = ['configuration_uid' => 1, 'provider_identifier' => 'openai', 'impact_method' => 'model_estimate']; + $connection->insert('tx_aim_request_log', $estimated + ['co2_g_min' => 0.1, 'co2_g_max' => 0.2, 'energy_wh_min' => 1.0, 'energy_wh_max' => 2.0]); + $connection->insert('tx_aim_request_log', $estimated + ['co2_g_min' => 0.2, 'co2_g_max' => 0.4, 'energy_wh_min' => 2.0, 'energy_wh_max' => 4.0]); + $connection->insert('tx_aim_request_log', ['configuration_uid' => 1, 'provider_identifier' => 'openai']); + $connection->insert('tx_aim_request_log', ['configuration_uid' => 2, 'provider_identifier' => 'ollama']); + } + + /** + * @param array $row + */ + private function log(array $row, TextResponse $response): void + { + $configuration = $this->configuration($row); + $this->middleware()->process( + new TextGenerationRequest(configuration: $configuration, prompt: 'Write an alt text.'), + $this->createMock(AiProviderInterface::class), + $configuration, + new AiMiddlewareHandler(static fn() => $response), + ); + } + + /** + * @param array $row + */ + private function configuration(array $row): ProviderConfiguration + { + return new ProviderConfiguration(array_merge(['uid' => 1, 'title' => 'Test', 'privacy_level' => 'standard'], $row)); + } + + private function display(): Display + { + $GLOBALS['LANG'] = $this->get(LanguageServiceFactory::class)->create('default'); + return new Display(); + } + + private function middleware(): RequestLoggingMiddleware + { + return new RequestLoggingMiddleware( + $this->get(RequestLogRepository::class), + new NullLogger(), + new Estimator(new EcoLogitsData()), + ); + } + + /** + * @return array + */ + private function fetchSingleLogRow(): array + { + $rows = $this->getConnectionPool() + ->getConnectionForTable('tx_aim_request_log') + ->select(['*'], 'tx_aim_request_log') + ->fetchAllAssociative(); + self::assertCount(1, $rows); + return $rows[0]; + } +} diff --git a/Tests/Functional/Footprint/FormatTest.php b/Tests/Functional/Footprint/FormatTest.php new file mode 100644 index 0000000..87d034f --- /dev/null +++ b/Tests/Functional/Footprint/FormatTest.php @@ -0,0 +1,81 @@ + 'avoided', 'values' => ['0.20–0.39 g CO₂e']], Format::routingChange(2, 0.196, 0.394)); + self::assertSame(['key' => 'avoided', 'values' => ['0.20–0.39 g CO₂e']], Format::routingChange(2, 0.394, 0.196)); + self::assertSame(['key' => 'added', 'values' => ['0.01–0.10 g CO₂e']], Format::routingChange(3, -0.1, -0.01)); + self::assertSame(['key' => 'mixed', 'values' => ['10 mg CO₂e', '0.20 g CO₂e']], Format::routingChange(3, -0.01, 0.2)); + self::assertNull(Format::routingChange(0, 0.0, 0.0)); + self::assertNull(Format::routingChange(2, 0.0, 0.0)); + } + + #[Test] + public function summaryOfAnAggregateWithoutEstimatesHasNoData(): void + { + $summary = Format::summary(['requests' => 3, 'estimated_requests' => 0, 'co2_min' => 0.0, 'co2_max' => 0.0, 'energy_min' => 0.0, 'energy_max' => 0.0]); + + self::assertFalse($summary['hasData']); + self::assertSame('', $summary['co2']); + } + + #[Test] + public function summaryReportsCoverageAndPageViews(): void + { + $summary = Format::summary(['requests' => 4, 'estimated_requests' => 2, 'co2_min' => 0.36, 'co2_max' => 0.72, 'energy_min' => 1.0, 'energy_max' => 2.0]); + + self::assertTrue($summary['hasData']); + self::assertSame('0.36–0.72 g CO₂e', $summary['co2']); + self::assertSame('1.0–2.0', $summary['pageViews']); + self::assertSame('2', $summary['estimatedRequests']); + self::assertSame('4', $summary['requests']); + } +} diff --git a/Tests/Functional/Footprint/SmartRoutingFootprintTest.php b/Tests/Functional/Footprint/SmartRoutingFootprintTest.php new file mode 100644 index 0000000..64f5fa6 --- /dev/null +++ b/Tests/Functional/Footprint/SmartRoutingFootprintTest.php @@ -0,0 +1,122 @@ +configuration->model)); + } + }; + $container = $this->createMock(ContainerInterface::class); + $container->method('get')->willReturn($provider); + + $this->get(AiProviderRegistry::class)->addProvider(new AiProviderManifest( + identifier: 'openai', + name: 'OpenAI', + description: '', + iconIdentifier: '', + supportedModels: [], + capabilities: [TextGenerationCapableInterface::class], + serviceName: 'test.provider', + container: $container, + )); + } + + #[Test] + public function aDowngradeLogsWhatTheRequestedConfigurationWouldHaveEmitted(): void + { + $this->createConfiguration('gpt-4o', ['default' => 1, 'grid_intensity' => '100.00']); + $this->createConfiguration('gpt-4o-mini'); + $this->seedHistory('gpt-4o', 0.01); + $this->seedHistory('gpt-4o-mini', 0.0001); + + $chain = $this->get(ProviderResolver::class)->buildFallbackChain(TextGenerationCapableInterface::class); + $this->get(AiMiddlewarePipeline::class)->dispatchWithFallback( + new TextGenerationRequest(configuration: $chain->getPrimary()->configuration, prompt: 'Hi'), + $chain, + ); + + $row = $this->getConnectionPool()->getConnectionForTable('tx_aim_request_log') + ->select(['*'], 'tx_aim_request_log', [], [], ['uid' => 'DESC'], 1) + ->fetchAssociative(); + self::assertSame('gpt-4o-mini', $row['model_used'], 'The premise of this test is that the route happened.'); + self::assertNotNull($row['co2_g_avoided_min'], 'The routing decision did not reach the footprint estimate.'); + self::assertGreaterThan(0.0, (float)$row['co2_g_avoided_max']); + $requested = json_decode((string)$row['impact_details'], true)['requestedModel']; + self::assertSame('gpt-4o', $requested['model']); + self::assertEqualsWithDelta(100.0, $requested['co2Grams']['max'] / $requested['energyWh']['max'] * 1000, 1e-6, 'The grid of the requested configuration was not used.'); + } + + private function seedHistory(string $model, float $cost): void + { + $logRepository = $this->get(RequestLogRepository::class); + for ($i = 0; $i < 12; $i++) { + $logRepository->log([ + 'crdate' => time(), + 'request_type' => 'TextGenerationRequest', + 'provider_identifier' => 'openai', + 'model_used' => $model, + 'success' => 1, + 'cost' => $cost, + 'duration_ms' => 1000, + 'total_tokens' => 100, + ]); + } + } + + /** + * @param array $overrides + */ + private function createConfiguration(string $model, array $overrides = []): void + { + $this->getConnectionPool()->getConnectionForTable('tx_aim_configuration')->insert('tx_aim_configuration', array_merge([ + 'pid' => 0, + 'ai_provider' => 'openai', + 'title' => $model, + 'model' => $model, + 'default' => 0, + 'rerouting_allowed' => 1, + 'accepts_rerouted_requests' => 1, + 'be_groups' => '', + ], $overrides)); + } +} diff --git a/Tests/Functional/Governance/PrivacyLoggingTest.php b/Tests/Functional/Governance/PrivacyLoggingTest.php index 9662d17..e298dda 100644 --- a/Tests/Functional/Governance/PrivacyLoggingTest.php +++ b/Tests/Functional/Governance/PrivacyLoggingTest.php @@ -14,6 +14,8 @@ use B13\Aim\Domain\Model\ProviderConfiguration; use B13\Aim\Domain\Repository\RequestLogRepository; +use B13\Aim\Footprint\EcoLogitsData; +use B13\Aim\Footprint\Estimator; use B13\Aim\Governance\PrivacyLevel; use B13\Aim\Middleware\AiMiddlewareHandler; use B13\Aim\Middleware\RequestLoggingMiddleware; @@ -39,6 +41,7 @@ private function createLoggingMiddleware(): RequestLoggingMiddleware return new RequestLoggingMiddleware( $this->get(RequestLogRepository::class), new NullLogger(), + new Estimator(new EcoLogitsData()), ); } diff --git a/Tests/Functional/Widgets/FootprintWidgetTest.php b/Tests/Functional/Widgets/FootprintWidgetTest.php new file mode 100644 index 0000000..542927a --- /dev/null +++ b/Tests/Functional/Widgets/FootprintWidgetTest.php @@ -0,0 +1,125 @@ +getConnectionPool()->getConnectionForTable('be_users')->insert('be_users', [ + 'uid' => 1, + 'pid' => 0, + 'username' => 'admin', + 'admin' => 1, + ]); + $this->setUpBackendUser(1); + $GLOBALS['LANG'] = $this->get(LanguageServiceFactory::class)->createFromUserPreferences($GLOBALS['BE_USER']); + } + + #[Test] + public function rendersTotalsCoverageRoutingAndProviders(): void + { + $connection = $this->getConnectionPool()->getConnectionForTable('tx_aim_request_log'); + $connection->insert('tx_aim_request_log', [ + 'provider_identifier' => 'openai', + 'co2_g_min' => 0.002, + 'co2_g_max' => 0.003, + 'energy_wh_min' => 0.005, + 'energy_wh_max' => 0.008, + 'co2_g_avoided_min' => 0.097, + 'co2_g_avoided_max' => 0.198, + 'impact_method' => 'model_estimate', + ]); + $connection->insert('tx_aim_request_log', ['provider_identifier' => 'ollama']); + + $html = $this->render(); + + self::assertStringContainsString('2.0–3.0 mg CO₂e', $html); + self::assertStringContainsString('1 of 2 requests estimated', $html); + self::assertStringContainsString('Avoided by smart routing: ≈ 0.10–0.20 g CO₂e · routed requests with an estimate: 1', $html); + self::assertMatchesRegularExpression('#openai.*?1 / 1#s', $html); + self::assertMatchesRegularExpression('#ollama.*?No data.*?0 / 1#s', $html); + } + + #[Test] + public function saysWhatSmartRoutingAddedWithoutTheAvoidedHighlight(): void + { + $this->getConnectionPool()->getConnectionForTable('tx_aim_request_log')->insert('tx_aim_request_log', [ + 'provider_identifier' => 'openai', + 'co2_g_min' => 0.002, + 'co2_g_max' => 0.003, + 'co2_g_avoided_min' => -0.2, + 'co2_g_avoided_max' => -0.1, + ]); + + $html = $this->render(); + + self::assertStringContainsString('Added by smart routing: ≈ 0.10–0.20 g CO₂e · routed requests with an estimate: 1', $html); + self::assertStringNotContainsString('text-success', $html); + } + + #[Test] + public function saysNothingWasEstimatedInsteadOfZero(): void + { + $this->getConnectionPool()->getConnectionForTable('tx_aim_request_log') + ->insert('tx_aim_request_log', ['provider_identifier' => 'ollama']); + + $html = $this->render(); + + self::assertStringContainsString('No request has been estimated yet.', $html); + self::assertStringNotContainsString('CO₂e', $html); + } + + private function render(): string + { + $path = '/module/dashboard'; + $request = new ServerRequest('https://typo3-testing.local/typo3' . $path, 'GET', 'php://input', [], [ + 'HTTP_HOST' => 'typo3-testing.local', + 'SERVER_NAME' => 'typo3-testing.local', + 'SERVER_ADDR' => '127.0.0.1', + 'REMOTE_ADDR' => '127.0.0.1', + 'SCRIPT_NAME' => '/typo3/index.php', + 'REQUEST_URI' => '/typo3' . $path, + 'REQUEST_METHOD' => 'GET', + ]); + $request = $request->withAttribute('route', new Route($path, ['packageName' => 'typo3/cms-dashboard'])); + $request = $request->withAttribute('applicationType', SystemEnvironmentBuilder::REQUESTTYPE_BE); + $request = $request->withAttribute('normalizedParams', NormalizedParams::createFromRequest($request)); + + $widget = $this->get('dashboard.widget.aim_footprint'); + self::assertInstanceOf(FootprintWidget::class, $widget); + $widget->setRequest($request); + return $widget->renderWidgetContent(); + } +} diff --git a/Tests/Unit/Middleware/RequestLoggingMiddlewareTest.php b/Tests/Unit/Middleware/RequestLoggingMiddlewareTest.php index 27da22d..0f30f14 100644 --- a/Tests/Unit/Middleware/RequestLoggingMiddlewareTest.php +++ b/Tests/Unit/Middleware/RequestLoggingMiddlewareTest.php @@ -14,6 +14,8 @@ use B13\Aim\Domain\Model\ProviderConfiguration; use B13\Aim\Domain\Repository\RequestLogRepository; +use B13\Aim\Footprint\EcoLogitsData; +use B13\Aim\Footprint\Estimator; use B13\Aim\Middleware\AiMiddlewareHandler; use B13\Aim\Middleware\RequestContext; use B13\Aim\Middleware\RequestLoggingMiddleware; @@ -52,7 +54,7 @@ public function logsNonStreamingResponseSynchronously(): void $repository = $this->createMock(RequestLogRepository::class); $repository->expects(self::once())->method('log')->willReturn(42); - $middleware = new RequestLoggingMiddleware($repository, new NullLogger()); + $middleware = new RequestLoggingMiddleware($repository, new NullLogger(), new Estimator(new EcoLogitsData())); $config = $this->createConfig(); $request = new TextGenerationRequest(configuration: $config, prompt: 'Hi'); $next = new AiMiddlewareHandler(static fn() => new TextResponse('hello')); @@ -72,7 +74,7 @@ public function doesNotLogStreamingResponseSynchronously(): void $repository = $this->createMock(RequestLogRepository::class); $repository->expects(self::never())->method('log'); - $middleware = new RequestLoggingMiddleware($repository, new NullLogger()); + $middleware = new RequestLoggingMiddleware($repository, new NullLogger(), new Estimator(new EcoLogitsData())); $config = $this->createConfig(); $request = new ConversationRequest( configuration: $config, @@ -116,7 +118,7 @@ public function getCompletionTokens(): int $response = new ConversationResponse('', streamIterator: $streamIterator); - $middleware = new RequestLoggingMiddleware($this->createMock(RequestLogRepository::class), new NullLogger()); + $middleware = new RequestLoggingMiddleware($this->createMock(RequestLogRepository::class), new NullLogger(), new Estimator(new EcoLogitsData())); $resolved = (new \ReflectionMethod($middleware, 'resolveStreamedResponse'))->invoke($middleware, $response, $streamIterator); self::assertInstanceOf(ConversationResponse::class, $resolved); @@ -139,7 +141,7 @@ public function resolveStreamedResponseReconstructsToolCallsForToolCalling(): vo $response = new ToolCallingResponse('', streamIterator: $streamIterator); - $middleware = new RequestLoggingMiddleware($this->createMock(RequestLogRepository::class), new NullLogger()); + $middleware = new RequestLoggingMiddleware($this->createMock(RequestLogRepository::class), new NullLogger(), new Estimator(new EcoLogitsData())); $resolved = (new \ReflectionMethod($middleware, 'resolveStreamedResponse'))->invoke($middleware, $response, $streamIterator); self::assertInstanceOf(ToolCallingResponse::class, $resolved); @@ -174,7 +176,7 @@ public function applyStreamedLogWritesTheRealAccumulatedContentOnceStreamIsConsu && $data['success'] === 1)) ->willReturn(7); - $middleware = new RequestLoggingMiddleware($repository, new NullLogger()); + $middleware = new RequestLoggingMiddleware($repository, new NullLogger(), new Estimator(new EcoLogitsData())); $context = new RequestContext(); (new \ReflectionMethod($middleware, 'applyStreamedLog'))->invoke( $middleware, diff --git a/Tests/Unit/Provider/SymfonyAi/SymfonyAiPlatformAdapterTest.php b/Tests/Unit/Provider/SymfonyAi/SymfonyAiPlatformAdapterTest.php index b3e8782..65b5581 100644 --- a/Tests/Unit/Provider/SymfonyAi/SymfonyAiPlatformAdapterTest.php +++ b/Tests/Unit/Provider/SymfonyAi/SymfonyAiPlatformAdapterTest.php @@ -12,6 +12,7 @@ namespace B13\Aim\Tests\Unit\Provider\SymfonyAi; +use B13\Aim\Domain\Model\ProviderConfiguration; use B13\Aim\Provider\SymfonyAi\SymfonyAiPlatformAdapter; use B13\Aim\Request\Message\AssistantMessage; use B13\Aim\Request\Message\ToolMessage; @@ -31,6 +32,7 @@ use Symfony\AI\Platform\Result\InMemoryRawResult; use Symfony\AI\Platform\Result\TextResult; use Symfony\AI\Platform\ResultConverterInterface; +use Symfony\AI\Platform\TokenUsage\TokenUsage; final class SymfonyAiPlatformAdapterTest extends TestCase { @@ -234,6 +236,45 @@ public function buildMessageBagFlattensToolMessagesWithoutNativeToolProtocol(): self::assertNotInstanceOf(ToolCallMessage::class, $messages[2]); } + #[Test] + public function extractUsageKeepsOllamaTimingsAsRawUsage(): void + { + $result = new TextResult('A red bicycle.'); + $result->getMetadata()->add('token_usage', new TokenUsage(promptTokens: 30, completionTokens: 12)); + $result->setRawResult(new InMemoryRawResult([ + 'model' => 'gemma3:4b', + 'message' => ['role' => 'assistant', 'content' => 'A red bicycle.'], + 'total_duration' => 2_400_000_000, + 'load_duration' => 400_000_000, + 'prompt_eval_duration' => 300_000_000, + 'eval_duration' => 1_600_000_000, + 'eval_count' => 12, + ])); + + $adapter = new SymfonyAiPlatformAdapter('Symfony\\AI\\Platform\\Bridge\\Ollama\\PlatformFactory'); + $usage = (new \ReflectionMethod($adapter, 'extractUsage'))->invoke($adapter, $result, new ProviderConfiguration([])); + + self::assertSame([ + 'total_duration' => 2_400_000_000, + 'load_duration' => 400_000_000, + 'prompt_eval_duration' => 300_000_000, + 'eval_duration' => 1_600_000_000, + ], $usage->rawUsage); + } + + #[Test] + public function extractUsageKeepsTheUsageObjectNextToTimings(): void + { + $result = new TextResult('Hi'); + $result->getMetadata()->add('token_usage', new TokenUsage(promptTokens: 3, completionTokens: 1)); + $result->setRawResult(new InMemoryRawResult(['usage' => ['input_tokens' => 3, 'output_tokens' => 1], 'total_duration' => 5])); + + $adapter = new SymfonyAiPlatformAdapter('Symfony\\AI\\Platform\\Bridge\\Anthropic\\PlatformFactory'); + $usage = (new \ReflectionMethod($adapter, 'extractUsage'))->invoke($adapter, $result, new ProviderConfiguration([])); + + self::assertSame(['input_tokens' => 3, 'output_tokens' => 1, 'total_duration' => 5], $usage->rawUsage); + } + /** * @param list<\B13\Aim\Request\Message\AbstractMessage> $messages * @param list $toolResults diff --git a/ext_tables.sql b/ext_tables.sql index 3b7691a..e8da488 100644 --- a/ext_tables.sql +++ b/ext_tables.sql @@ -44,7 +44,9 @@ CREATE TABLE tx_aim_configuration ( grading_enabled tinyint(1) unsigned DEFAULT '0' NOT NULL, judge_configuration_uid int(11) unsigned DEFAULT '0' NOT NULL, grading_rubric text, - system_prompt_addition text + system_prompt_addition text, + power_draw_watts decimal(8,2) DEFAULT NULL, + grid_intensity decimal(8,2) DEFAULT NULL ); CREATE TABLE tx_aim_usage_budget ( @@ -78,6 +80,14 @@ CREATE TABLE tx_aim_request_log ( total_tokens int(11) unsigned DEFAULT '0' NOT NULL, cost decimal(10,6) DEFAULT '0.000000' NOT NULL, duration_ms int(11) unsigned DEFAULT '0' NOT NULL, + energy_wh_min decimal(20,10) DEFAULT NULL, + energy_wh_max decimal(20,10) DEFAULT NULL, + co2_g_min decimal(20,10) DEFAULT NULL, + co2_g_max decimal(20,10) DEFAULT NULL, + co2_g_avoided_min decimal(20,10) DEFAULT NULL, + co2_g_avoided_max decimal(20,10) DEFAULT NULL, + impact_method varchar(32) DEFAULT '' NOT NULL, + impact_details text, system_fingerprint varchar(255) DEFAULT '' NOT NULL, error_message text, metadata text, diff --git a/phpstan-baseline.neon b/phpstan-baseline.neon index 92b2798..282e697 100644 --- a/phpstan-baseline.neon +++ b/phpstan-baseline.neon @@ -198,6 +198,18 @@ parameters: count: 1 path: Tests/Functional/Controller/RequestLogControllerPollTest.php + - + rawMessage: Access to internal constant TYPO3\CMS\Core\Core\SystemEnvironmentBuilder::REQUESTTYPE_BE from outside its root namespace TYPO3. + identifier: classConstant.internal + count: 1 + path: Tests/Functional/Controller/RequestLogFootprintTest.php + + - + rawMessage: Access to internal constant TYPO3\CMS\Core\Core\SystemEnvironmentBuilder::REQUESTTYPE_BE from outside its root namespace TYPO3. + identifier: classConstant.internal + count: 1 + path: Tests/Functional/Widgets/FootprintWidgetTest.php + - rawMessage: 'Call to an undefined method PHPUnit\Framework\MockObject\Builder\InvocationStubber::with().' identifier: method.notFound