Site Builder
Editing:
models1.php
writable 0666
<?php /*-------------------------------------------------------------- * /openai/models.php – fetch live model list & let user * decide which ones should be “active” *------------------------------------------------------------*/ require_once __DIR__ . '/init.php'; // key‑bar / helpers ai_handle_key_post(); // handle save / delete /* ---------- 1. need a key first --------------------------- */ if (!ai_has_key()) { ?> <!doctype html><html lang="en"><head><meta charset="utf-8"> <meta name="viewport" content="width=device-width,initial-scale=1"> <title>Model list • BestDealOn</title></head><body> <?php ai_render_key_bar(); ?> <p style="padding:2rem;font:16px/1.4 sans-serif">Save your API key above to fetch the model catalogue.</p> </body></html><?php exit; } /* ---------- 2. fetch catalogue ----------------------------- */ $key = ai_get_key(); $ch = curl_init('https://api.openai.com/v1/models'); curl_setopt_array($ch, [ CURLOPT_RETURNTRANSFER => 1, CURLOPT_HTTPHEADER => [ 'Authorization: Bearer '.$key, 'Content-Type: application/json' ], CURLOPT_TIMEOUT => 15 ]); $raw = curl_exec($ch); curl_close($ch); $data = $raw ? json_decode($raw, true) : null; /* fallback */ $models = []; if (isset($data['data']) && is_array($data['data'])) { foreach ($data['data'] as $m) { /* keep only chat‑capable GPT models (skip embeddings, finetunes, usw.) */ if (!empty($m['id']) && str_starts_with($m['id'], 'gpt-')) { $models[] = $m['id']; } } } sort($models); // prettify order ?> <!doctype html> <html lang="en"><head> <meta charset="utf-8"> <meta name="viewport" content="width=device-width,initial-scale=1"> <title>Model list • BestDealOn</title> <style> :root{--bg:#f1f4fb;--card:#fff;--brand:#004cff;--radius:26px; font-family:-apple-system,BlinkMacSystemFont,"Segoe UI",Roboto,Helvetica,Arial,sans-serif} body{margin:0;min-height:100vh;background:var(--bg);display:flex;flex-direction:column;color:#111} main{max-width:900px;margin:2rem auto;padding:0 1.2rem;flex:1;width:100%} .card{background:var(--card);border-radius:var(--radius);box-shadow:0 6px 30px rgba(0,0,0,.08);padding:2rem} h2{margin:0 0 1rem} table{width:100%;border-collapse:collapse;font-size:.95rem} th,td{padding:.55rem .8rem;border:1px solid #ccd4e8;text-align:left} th{background:#f0f3fb} button{background:var(--brand);color:#fff;border:none;border-radius:8px;padding:.6rem 1.3rem; font-weight:600;cursor:pointer;margin-top:1rem} textarea{width:100%;height:160px;margin-top:1rem;font-family:ui-monospace,monospace; border:1px solid #ccd4e8;border-radius:8px;padding:.7rem;font-size:.9rem} .notice{font-size:.9rem;color:#444;margin:.8rem 0 0} </style> </head><body> <?php ai_render_key_bar(); ?><!-- black bar --> <main> <div class="card"> <h2>Available models (live)</h2> <?php if (!$models): ?> <p style="color:#c00;font-weight:600">Could not fetch model list (check key / network).</p> <?php else: ?> <table id="modelTbl"> <thead><tr><th style="width:34px">✔</th><th>Model ID</th></tr></thead> <tbody> <?php foreach ($models as $id): ?> <tr> <td style="text-align:center"> <input type="checkbox" data-id="<?= htmlspecialchars($id) ?>"> </td> <td><code><?= htmlspecialchars($id) ?></code></td> </tr> <?php endforeach; ?> </tbody> </table> <button id="save">Save to browser</button> <p class="notice">The JSON below is stored only in your browser’s <code>localStorage.aiModelList</code>.</p> <textarea id="out" readonly></textarea> <?php endif; ?> </div> </main> <script> /* ------------ util helpers ----------------- */ const byId = id => document.getElementById(id); /* ------------ load previous list ------------ */ let list = []; try{ list = JSON.parse(localStorage.getItem('aiModelList')||'[]'); }catch{} const tbl = byId('modelTbl'); if (tbl){ const map = Object.fromEntries(list.map(o=>[o.id, o.active])); tbl.querySelectorAll('input[type=checkbox]').forEach(cb=>{ cb.checked = map[cb.dataset.id] ?? false; cb.addEventListener('change', refreshJSON); }); refreshJSON(); // initial fill } /* ------------ refresh JSON textarea --------- */ function refreshJSON(){ const arr = [...document.querySelectorAll('#modelTbl input[type=checkbox]')] .map(cb => ({id: cb.dataset.id, active: cb.checked})); byId('out').value = JSON.stringify(arr, null, 2); } /* ------------ save to localStorage ---------- */ byId('save')?.addEventListener('click', ()=>{ try{ localStorage.setItem('aiModelList', byId('out').value); alert('Saved! (The helper will use this list on the next page load.)'); }catch(e){ alert('Could not write to localStorage.'); } }); </script> </body></html>
Save changes
Create folder
writable 0777
Create
Cancel