Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
107 changes: 107 additions & 0 deletions Build/Scripts/updateEcoLogitsData.py
Original file line number Diff line number Diff line change
@@ -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 <commit>
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())
12 changes: 12 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
7 changes: 6 additions & 1 deletion Classes/Controller/ProviderController.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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(
Expand Down
33 changes: 28 additions & 5 deletions Classes/Controller/RequestLogController.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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(
Expand All @@ -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)
Expand Down Expand Up @@ -169,7 +171,7 @@ public function showContextualAction(ServerRequestInterface $request): ResponseI
}

/**
* @return array{entry: array<string, mixed>|null, username?: string, extensionIcon?: string|null}
* @return array{entry: array<string, mixed>|null, footprint?: array<string, mixed>, username?: string, extensionIcon?: string|null}
*/
private function resolveShowViewData(int $uid): array
{
Expand All @@ -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,
];
Expand All @@ -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);

Expand All @@ -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[] = [
Expand Down Expand Up @@ -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),
Expand Down Expand Up @@ -360,6 +365,24 @@ private function resolveExtensionIcons(array $extensionKeys): array
return $icons;
}

/**
* @param array<string, mixed> $statistics
* @return array<string, mixed>
*/
private function withFootprintSummary(array $statistics): array
{
return [...$statistics, ...$this->footprintDisplay->statistics($statistics)];
}

/**
* @param array<string, mixed> $entry
* @return array<string, mixed>
*/
private function withFootprintCell(array $entry): array
{
return [...$entry, ...$this->footprintDisplay->cell($entry)];
}

private function getLanguageService(): LanguageService
{
return $GLOBALS['LANG'];
Expand Down
23 changes: 23 additions & 0 deletions Classes/Domain/Model/ProviderConfigurationFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -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.
*
Expand Down
2 changes: 1 addition & 1 deletion Classes/Domain/Repository/RequestLogDemand.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down
Loading
Loading