1<?php 2 3use dokuwiki\plugin\aichat\AbstractCLI; 4use splitbrain\phpcli\Options; 5 6/** 7 * DokuWiki Plugin aichat (CLI Component) 8 * 9 * @license GPL 2 http://www.gnu.org/licenses/gpl-2.0.html 10 * @author Andreas Gohr <gohr@cosmocode.de> 11 */ 12class cli_plugin_aichat_dev extends AbstractCLI 13{ 14 /** @inheritDoc */ 15 protected function setup(Options $options) 16 { 17 $options->setHelp('Helps with development of this plugin'); 18 19 $options->registerCommand('update', 'Update the model data'); 20 } 21 22 /** @inheritDoc */ 23 protected function main(Options $options) 24 { 25 parent::main($options); 26 27 switch ($options->getCmd()) { 28 29 case 'update': 30 $this->updateModelData(); 31 break; 32 default: 33 echo $options->help(); 34 } 35 } 36 37 protected function updateModelData() 38 { 39 40 $http = new \dokuwiki\HTTP\DokuHTTPClient(); 41 $url = 'https://raw.githubusercontent.com/BerriAI/litellm/main/model_prices_and_context_window.json'; 42 $response = $http->get($url); 43 if ($response === false) { 44 $this->error('Failed to fetch model data'); 45 return 1; 46 } 47 $models = json_decode($response, true, 512, JSON_THROW_ON_ERROR); 48 49 $ourProviders = [ 50 'anthropic' => [ 51 'name' => 'Anthropic', 52 ], 53 'groq' => [ 54 'name' => 'Groq', 55 'skip' => '/-preview$/' 56 ], 57 'mistral' => [ 58 'name' => 'Mistral', 59 'skip' => '/-\d\d\d\d$/', 60 ], 61 'openai' => [ 62 'name' => 'OpenAI', 63 'skip' => '/(-\d\d\d\d-\d\d-\d\d|-preview|-\d\d\d\d)$|^ft:/' 64 ], 65 'reka' => [ 66 'name' => 'Reka', 67 ], 68 'voyage' => [ 69 'name' => 'VoyageAI', 70 'skip' => '/-(01|02)(-|$)/', // outdated models 71 ], 72 ]; 73 74 // load existing models 75 foreach ($ourProviders as $provider => $data) { 76 $ourProviders[$provider]['models'] = json_decode( 77 file_get_contents(__DIR__ . '/../Model/' . $data['name'] . '/' . 'models.json'), 78 true 79 ); 80 } 81 82 // update models 83 foreach ($models as $model => $data) { 84 if (!isset($ourProviders[$data['litellm_provider']])) continue; 85 if (!in_array($data['mode'], ['chat', 'embedding'])) continue; 86 $provider = $data['litellm_provider']; 87 $model = explode('/', $model); 88 $model = array_pop($model); 89 90 if (isset($ourProviders[$provider]['skip']) && preg_match($ourProviders[$provider]['skip'], $model)) { 91 $this->info('Skipping ' . $provider . ' ' . $model); 92 continue; 93 } 94 $this->success("$provider $model"); 95 96 $oldmodel = $ourProviders[$provider]['models'][$data['mode']][$model] ?? []; 97 $newmodel = [ 98 "description" => $oldmodel['description'] ?? $data['source'] ?? '', 99 "inputTokens" => $data['max_input_tokens'] ?? $data['max_tokens'], 100 "inputTokenPrice" => round($data['input_cost_per_token'] * 1_000_000, 2), 101 ]; 102 103 if ($data['mode'] === 'chat') { 104 $newmodel['outputTokens'] = $data['max_output_tokens']; 105 $newmodel['outputTokenPrice'] = round($data['output_cost_per_token'] * 1_000_000, 2); 106 } else { 107 if (isset($oldmodel['dimensions'])) { 108 $newmodel['dimensions'] = $oldmodel['dimensions']; 109 } else { 110 $this->warning('No dimensions for ' . $provider . ' ' . $model . '. Check manually!'); 111 $newmodel['dimensions'] = 1536; 112 } 113 } 114 $ourProviders[$provider]['models'][$data['mode']][$model] = $newmodel; 115 } 116 117 // save models 118 foreach ($ourProviders as $data) { 119 file_put_contents( 120 __DIR__ . '/../Model/' . $data['name'] . '/' . 'models.json', 121 json_encode($data['models'], JSON_PRETTY_PRINT) 122 ); 123 } 124 125 return 0; 126 } 127} 128