336 lines
15 KiB
HTML
336 lines
15 KiB
HTML
{% extends "base.html" %}
|
|
|
|
{% block title %}API设置 - 有音个性化教学系统{% endblock %}
|
|
|
|
{% block content %}
|
|
<div class="card">
|
|
<div class="card-header d-flex justify-content-between align-items-center">
|
|
<h5 class="mb-0"><i class="bi bi-key"></i> AI API 配置</h5>
|
|
<button class="btn btn-sm btn-outline-primary" onclick="showAddProviderModal()">
|
|
<i class="bi bi-plus-lg"></i> 新增提供商
|
|
</button>
|
|
</div>
|
|
<div class="card-body">
|
|
<div class="alert alert-info">
|
|
<i class="bi bi-info-circle"></i> 配置AI生成练习报告所需的API参数。
|
|
</div>
|
|
|
|
<form id="apiConfigForm">
|
|
<div class="row">
|
|
<div class="col-md-8 mb-3">
|
|
<label class="form-label">提供商</label>
|
|
<div class="input-group">
|
|
<select class="form-select" id="apiProvider" onchange="onProviderChange()"></select>
|
|
<button class="btn btn-outline-danger" type="button" onclick="deleteCurrentProvider()" title="删除当前提供商">
|
|
<i class="bi bi-trash"></i>
|
|
</button>
|
|
</div>
|
|
</div>
|
|
<div class="col-md-4 mb-3">
|
|
<label class="form-label">模型</label>
|
|
<div id="modelInputArea">
|
|
<select class="form-select" id="apiModelSelect" onchange="onModelSelectChange()" style="display:none"></select>
|
|
<input type="text" class="form-control" id="apiModelText" placeholder="如 gpt-4o-mini">
|
|
</div>
|
|
</div>
|
|
</div>
|
|
|
|
<div class="mb-3">
|
|
<label class="form-label">API Key</label>
|
|
<div class="input-group">
|
|
<input type="password" class="form-control" id="apiKey" placeholder="请输入API Key">
|
|
<button class="btn btn-outline-secondary" type="button" onclick="toggleApiKey()">
|
|
<i class="bi bi-eye" id="apiKeyToggleIcon"></i>
|
|
</button>
|
|
</div>
|
|
<small class="text-muted" id="apiKeyPreview"></small>
|
|
</div>
|
|
|
|
<div class="mb-3">
|
|
<label class="form-label">API Endpoint</label>
|
|
<input type="text" class="form-control" id="apiEndpoint" placeholder="https://ark.cn-beijing.volces.com/api/coding/v3">
|
|
</div>
|
|
|
|
<div class="mb-3">
|
|
<label class="form-label">Temperature</label>
|
|
<input type="number" class="form-control" id="apiTemperature" min="0" max="2" step="0.1" value="0.7">
|
|
<small class="text-muted">控制输出的随机性,值越大越有创造性</small>
|
|
</div>
|
|
|
|
<div class="mb-3">
|
|
<label class="form-label">PDF水印文本</label>
|
|
<input type="text" class="form-control" id="watermarkText" placeholder="留空则不显示水印">
|
|
<small class="text-muted">PDF每页中央显示的斜向水印文字</small>
|
|
</div>
|
|
|
|
<div class="d-flex gap-2">
|
|
<button type="button" class="btn btn-primary" onclick="saveApiConfig()">
|
|
<i class="bi bi-save"></i> 保存配置
|
|
</button>
|
|
<button type="button" class="btn btn-outline-secondary" onclick="testApiConnection()">
|
|
<i class="bi bi-plug"></i> 测试连接
|
|
</button>
|
|
</div>
|
|
|
|
<div class="mt-3" id="apiTestResult"></div>
|
|
</form>
|
|
</div>
|
|
</div>
|
|
|
|
<!-- 新增提供商 Modal -->
|
|
<div class="modal fade" id="addProviderModal" tabindex="-1">
|
|
<div class="modal-dialog">
|
|
<div class="modal-content">
|
|
<div class="modal-header">
|
|
<h5 class="modal-title" id="providerModalTitle">新增API提供商</h5>
|
|
<button type="button" class="btn-close" data-bs-dismiss="modal"></button>
|
|
</div>
|
|
<div class="modal-body">
|
|
<div class="mb-3">
|
|
<label class="form-label">提供商ID <small class="text-muted">(英文标识)</small></label>
|
|
<input type="text" class="form-control" id="newProviderId" placeholder="如 opencodego">
|
|
</div>
|
|
<div class="mb-3">
|
|
<label class="form-label">显示名称</label>
|
|
<input type="text" class="form-control" id="newProviderName" placeholder="如 OpenCode Go">
|
|
</div>
|
|
<div class="mb-3">
|
|
<label class="form-label">API Endpoint</label>
|
|
<input type="text" class="form-control" id="newProviderEndpoint" placeholder="https://api.example.com/v1">
|
|
</div>
|
|
<div class="mb-3">
|
|
<label class="form-label">模型列表</label>
|
|
<div id="newModelsContainer">
|
|
<div class="input-group mb-1">
|
|
<input type="text" class="form-control newModelInput" placeholder="模型名称,如 gpt-4o">
|
|
<button class="btn btn-outline-danger" type="button" onclick="removeModelInput(this)" title="删除">×</button>
|
|
</div>
|
|
</div>
|
|
<button type="button" class="btn btn-sm btn-outline-secondary mt-1" onclick="addModelInput()">
|
|
<i class="bi bi-plus"></i> 添加模型
|
|
</button>
|
|
<small class="text-muted d-block">一个提供商可以有多个模型</small>
|
|
</div>
|
|
</div>
|
|
<div class="modal-footer">
|
|
<button type="button" class="btn btn-secondary" data-bs-dismiss="modal">取消</button>
|
|
<button type="button" class="btn btn-primary" id="providerModalSaveBtn" onclick="saveProvider()">确认添加</button>
|
|
</div>
|
|
</div>
|
|
</div>
|
|
</div>
|
|
|
|
<div class="card mt-3">
|
|
<div class="card-header">
|
|
<h6 class="mb-0"><i class="bi bi-lightbulb"></i> 已注册提供商</h6>
|
|
</div>
|
|
<div class="card-body">
|
|
<table class="table table-sm" id="providersTable">
|
|
<thead>
|
|
<tr><th>ID</th><th>名称</th><th>Endpoint</th><th>模型</th><th></th></tr>
|
|
</thead>
|
|
<tbody></tbody>
|
|
</table>
|
|
</div>
|
|
</div>
|
|
{% endblock %}
|
|
|
|
{% block extra_js %}
|
|
<script>
|
|
window.pageInit = function(data) {
|
|
if (data.role !== 'admin') { window.location.href = '/'; return; }
|
|
loadApiConfig();
|
|
};
|
|
|
|
let allProviders = {};
|
|
let currentModel = '';
|
|
|
|
async function loadProviders() {
|
|
const r = await fetch('/api/config/providers');
|
|
allProviders = await r.json();
|
|
renderProviderDropdown();
|
|
renderProvidersTable();
|
|
}
|
|
|
|
function renderProviderDropdown() {
|
|
const sel = document.getElementById('apiProvider');
|
|
const cur = sel.value;
|
|
sel.innerHTML = '';
|
|
for (const [id, p] of Object.entries(allProviders)) {
|
|
sel.innerHTML += `<option value="${id}">${p.name}</option>`;
|
|
}
|
|
if (cur && allProviders[cur]) sel.value = cur;
|
|
}
|
|
|
|
function renderProvidersTable() {
|
|
const tbody = document.getElementById('providersTable').querySelector('tbody');
|
|
tbody.innerHTML = '';
|
|
for (const [id, p] of Object.entries(allProviders)) {
|
|
const ml = (p.models || []).join(', ');
|
|
tbody.innerHTML += `<tr>
|
|
<td><code>${id}</code></td><td>${p.name}</td>
|
|
<td><code style="font-size:11px">${p.endpoint}</code></td>
|
|
<td>${ml || '<span class="text-muted">—</span>'}</td>
|
|
<td><button class="btn btn-sm btn-outline-secondary" onclick="editProvider('${id}')" title="编辑"><i class="bi bi-pencil"></i></button></td>
|
|
</tr>`;
|
|
}
|
|
if (!Object.keys(allProviders).length) {
|
|
tbody.innerHTML = '<tr><td colspan="5" class="text-muted text-center">暂无提供商</td></tr>';
|
|
}
|
|
}
|
|
|
|
function renderModelInput(models) {
|
|
const sel = document.getElementById('apiModelSelect');
|
|
const txt = document.getElementById('apiModelText');
|
|
if (models && models.length > 1) {
|
|
sel.style.display = ''; txt.style.display = 'none';
|
|
sel.innerHTML = '';
|
|
models.forEach(m => { const o = document.createElement('option'); o.value = m; o.textContent = m; sel.appendChild(o); });
|
|
if (models.includes(currentModel)) sel.value = currentModel; else sel.value = models[0];
|
|
} else {
|
|
sel.style.display = 'none'; txt.style.display = '';
|
|
txt.value = (models && models.length === 1) ? models[0] : (currentModel || '');
|
|
}
|
|
}
|
|
|
|
function getSelectedModel() {
|
|
const sel = document.getElementById('apiModelSelect');
|
|
return sel.style.display !== 'none' ? sel.value : document.getElementById('apiModelText').value;
|
|
}
|
|
|
|
function onModelSelectChange() {
|
|
currentModel = document.getElementById('apiModelSelect').value;
|
|
}
|
|
|
|
function onProviderChange() {
|
|
const id = document.getElementById('apiProvider').value;
|
|
const p = allProviders[id];
|
|
if (p) {
|
|
document.getElementById('apiEndpoint').value = p.endpoint || '';
|
|
renderModelInput(p.models || []);
|
|
}
|
|
}
|
|
|
|
async function loadApiConfig() {
|
|
await loadProviders();
|
|
try {
|
|
const r = await fetch('/api/config');
|
|
const c = await r.json();
|
|
document.getElementById('apiProvider').value = c.provider || '';
|
|
document.getElementById('apiEndpoint').value = c.base_url || '';
|
|
document.getElementById('apiTemperature').value = c.temperature || 0.7;
|
|
document.getElementById('watermarkText').value = c.watermark_text || '';
|
|
currentModel = c.model || '';
|
|
const p = allProviders[c.provider];
|
|
renderModelInput(p ? p.models : []);
|
|
if (c.api_key_preview) document.getElementById('apiKeyPreview').textContent = '当前: ' + c.api_key_preview;
|
|
} catch (e) { console.error(e); }
|
|
}
|
|
|
|
async function saveApiConfig() {
|
|
const m = getSelectedModel();
|
|
if (!m) { alert('请选择或输入模型'); return; }
|
|
const config = {
|
|
provider: document.getElementById('apiProvider').value,
|
|
model: m,
|
|
api_key: document.getElementById('apiKey').value,
|
|
base_url: document.getElementById('apiEndpoint').value,
|
|
temperature: parseFloat(document.getElementById('apiTemperature').value),
|
|
watermark_text: document.getElementById('watermarkText').value.trim()
|
|
};
|
|
const r = await fetch('/api/config', { method: 'POST', headers: { 'Content-Type': 'application/json' }, body: JSON.stringify(config) });
|
|
const d = await r.json();
|
|
if (d.error) alert(d.error); else { alert('保存成功'); loadApiConfig(); }
|
|
}
|
|
|
|
async function testApiConnection() {
|
|
const div = document.getElementById('apiTestResult');
|
|
div.innerHTML = '<div class="alert alert-info">测试中...</div>';
|
|
const r = await fetch('/api/config/test', { method: 'POST' });
|
|
const d = await r.json();
|
|
div.innerHTML = d.success ? '<div class="alert alert-success"><i class="bi bi-check-circle"></i> 连接成功</div>'
|
|
: '<div class="alert alert-danger"><i class="bi bi-x-circle"></i> 连接失败: ' + (d.error || '未知错误') + '</div>';
|
|
}
|
|
|
|
// --- 新增/编辑提供商 ---
|
|
let editingProviderId = null;
|
|
|
|
function showAddProviderModal() {
|
|
editingProviderId = null;
|
|
document.getElementById('providerModalTitle').textContent = '新增API提供商';
|
|
document.getElementById('providerModalSaveBtn').textContent = '确认添加';
|
|
document.getElementById('newProviderId').value = '';
|
|
document.getElementById('newProviderId').disabled = false;
|
|
document.getElementById('newProviderName').value = '';
|
|
document.getElementById('newProviderEndpoint').value = '';
|
|
document.getElementById('newModelsContainer').innerHTML = `<div class="input-group mb-1">
|
|
<input type="text" class="form-control newModelInput" placeholder="模型名称">
|
|
<button class="btn btn-outline-danger" type="button" onclick="removeModelInput(this)" title="删除">×</button>
|
|
</div>`;
|
|
new bootstrap.Modal(document.getElementById('addProviderModal')).show();
|
|
}
|
|
|
|
function editProvider(id) {
|
|
editingProviderId = id;
|
|
const p = allProviders[id];
|
|
document.getElementById('providerModalTitle').textContent = '编辑提供商 - ' + p.name;
|
|
document.getElementById('providerModalSaveBtn').textContent = '保存修改';
|
|
document.getElementById('newProviderId').value = id;
|
|
document.getElementById('newProviderId').disabled = true;
|
|
document.getElementById('newProviderName').value = p.name || '';
|
|
document.getElementById('newProviderEndpoint').value = p.endpoint || '';
|
|
const container = document.getElementById('newModelsContainer');
|
|
const models = p.models || [];
|
|
if (!models.length) models.push('');
|
|
container.innerHTML = models.map(m => `<div class="input-group mb-1">
|
|
<input type="text" class="form-control newModelInput" value="${m}" placeholder="模型名称">
|
|
<button class="btn btn-outline-danger" type="button" onclick="removeModelInput(this)" title="删除">×</button>
|
|
</div>`).join('');
|
|
new bootstrap.Modal(document.getElementById('addProviderModal')).show();
|
|
}
|
|
|
|
async function saveProvider() {
|
|
const id = document.getElementById('newProviderId').value.trim().toLowerCase();
|
|
const name = document.getElementById('newProviderName').value.trim();
|
|
const endpoint = document.getElementById('newProviderEndpoint').value.trim();
|
|
const models = []; document.querySelectorAll('.newModelInput').forEach(inp => { const v = inp.value.trim(); if (v) models.push(v); });
|
|
|
|
if (!name || !endpoint) { alert('请填写名称和Endpoint'); return; }
|
|
if (!models.length) { alert('请至少添加一个模型'); return; }
|
|
|
|
const isEdit = !!editingProviderId;
|
|
const url = isEdit ? '/api/config/providers/' + editingProviderId : '/api/config/providers';
|
|
const method = isEdit ? 'PUT' : 'POST';
|
|
const body = isEdit ? { name, endpoint, models } : { id, name, endpoint, models };
|
|
|
|
const r = await fetch(url, { method, headers: { 'Content-Type': 'application/json' }, body: JSON.stringify(body) });
|
|
const d = await r.json();
|
|
if (d.error) { alert(d.error); return; }
|
|
bootstrap.Modal.getInstance(document.getElementById('addProviderModal')).hide();
|
|
await loadProviders();
|
|
document.getElementById('apiProvider').value = editingProviderId || id;
|
|
onProviderChange();
|
|
alert(isEdit ? '修改成功' : '添加成功');
|
|
}
|
|
|
|
// --- 删除提供商 ---
|
|
|
|
async function deleteCurrentProvider() {
|
|
const id = document.getElementById('apiProvider').value;
|
|
if (Object.keys(allProviders).length <= 1) { alert('至少保留一个提供商'); return; }
|
|
if (!confirm('确定要删除提供商 "' + (allProviders[id]?.name || id) + '" 吗?')) return;
|
|
const r = await fetch('/api/config/providers/' + id, { method: 'DELETE' });
|
|
const d = await r.json();
|
|
if (d.error) { alert(d.error); return; }
|
|
alert('删除成功');
|
|
loadApiConfig();
|
|
}
|
|
|
|
function toggleApiKey() {
|
|
const inp = document.getElementById('apiKey');
|
|
const ico = document.getElementById('apiKeyToggleIcon');
|
|
if (inp.type === 'password') { inp.type = 'text'; ico.className = 'bi bi-eye-slash'; }
|
|
else { inp.type = 'password'; ico.className = 'bi bi-eye'; }
|
|
}
|
|
</script>
|
|
{% endblock %} |