| <!DOCTYPE html>
|
| <html lang="zh-TW">
|
| <head>
|
| <meta charset="UTF-8">
|
| <title>Ollama 提示語推薦與查詢</title>
|
| <style>
|
| body { font-family: sans-serif; max-width: 600px; margin: 20px auto; }
|
| .suggestion {
|
| cursor: pointer;
|
| padding: 8px;
|
| border: 1px solid #ccc;
|
| margin: 5px 0;
|
| border-radius: 4px;
|
| }
|
| .suggestion:hover {
|
| background-color: #eef;
|
| }
|
| textarea {
|
| width: 100%;
|
| height: 100px;
|
| margin-top: 10px;
|
| font-family: inherit;
|
| padding: 8px;
|
| }
|
| button {
|
| margin-top: 10px;
|
| padding: 8px 16px;
|
| }
|
| pre {
|
| background: #f9f9f9;
|
| padding: 10px;
|
| white-space: pre-wrap;
|
| }
|
| </style>
|
| </head>
|
| <body>
|
| <h1>選擇模型並獲得建議提示</h1>
|
|
|
| <label for="modelSelect">選擇模型:</label>
|
| <select id="modelSelect">
|
| <option value="">請選擇模型</option>
|
| </select>
|
| <button id="getSuggestions">取得建議提示</button>
|
|
|
| <h3>建議提示:</h3>
|
| <div id="suggestionList">(尚未載入)</div>
|
|
|
| <h3>選擇的提示:</h3>
|
| <textarea id="customPrompt" placeholder="請從上方建議中點選,或自行輸入 prompt..."></textarea>
|
|
|
| <h3>選擇操作:</h3>
|
| <select id="actionSelect">
|
| <option value="create">創作</option>
|
| <option value="explain">解釋</option>
|
| </select>
|
|
|
| <button id="generate">產生</button>
|
|
|
| <h3>模型回應:</h3>
|
| <pre id="responseOutput">(尚未查詢)</pre>
|
|
|
| <script>
|
| async function loadModels() {
|
| try {
|
| const res = await fetch('http://127.0.0.1:11434/api/tags');
|
| const data = await res.json();
|
| const select = document.getElementById('modelSelect');
|
| data.models.forEach(model => {
|
| const opt = document.createElement('option');
|
| opt.value = model.name;
|
| opt.textContent = model.name;
|
| select.appendChild(opt);
|
| });
|
| } catch (err) {
|
| console.error(err);
|
| alert('無法載入模型,請確認 Ollama 是否已啟動');
|
| }
|
| }
|
|
|
| async function getPromptSuggestions() {
|
| const model = document.getElementById('modelSelect').value;
|
| const container = document.getElementById('suggestionList');
|
| if (!model) {
|
| alert('請先選擇模型');
|
| return;
|
| }
|
|
|
| container.innerHTML = '載入中...';
|
|
|
| const prompt = `你是 ${model} 模型,請列出 3~5 個你擅長的提示語,每個換行顯示,不需要額外說明。`;
|
|
|
| try {
|
| const res = await fetch('http://127.0.0.1:11434/api/generate', {
|
| method: 'POST',
|
| headers: { 'Content-Type': 'application/json' },
|
| body: JSON.stringify({ model, prompt, stream: false })
|
| });
|
| const data = await res.json();
|
| const lines = data.response.split('\n').filter(l => l.trim());
|
|
|
| container.innerHTML = '';
|
| lines.forEach(line => {
|
| const div = document.createElement('div');
|
| div.className = 'suggestion';
|
| div.textContent = line;
|
| div.addEventListener('click', () => {
|
| document.getElementById('customPrompt').value = line;
|
| });
|
| container.appendChild(div);
|
| });
|
| } catch (err) {
|
| console.error(err);
|
| container.innerHTML = '取得建議失敗';
|
| }
|
| }
|
|
|
| async function generateResponse() {
|
| const model = document.getElementById('modelSelect').value;
|
| const prompt = document.getElementById('customPrompt').value.trim();
|
| const action = document.getElementById('actionSelect').value;
|
| const output = document.getElementById('responseOutput');
|
|
|
| if (!model || !prompt) {
|
| alert('請選擇模型並輸入 prompt');
|
| return;
|
| }
|
|
|
|
|
| let finalPrompt = prompt;
|
| if (action === 'create') {
|
| finalPrompt = `您是該領域專家,請創作:${prompt}`;
|
| } else if (action === 'explain') {
|
| finalPrompt = `您是該領域專家,請解釋:${prompt}`;
|
| }
|
|
|
| output.textContent = '產生中...';
|
|
|
| try {
|
| const res = await fetch('http://127.0.0.1:11434/api/generate', {
|
| method: 'POST',
|
| headers: { 'Content-Type': 'application/json' },
|
| body: JSON.stringify({ model, prompt: finalPrompt, stream: false })
|
| });
|
| const data = await res.json();
|
| output.textContent = data.response || '未收到回應';
|
| } catch (err) {
|
| console.error(err);
|
| output.textContent = '產生失敗';
|
| }
|
| }
|
|
|
| document.getElementById('getSuggestions').addEventListener('click', getPromptSuggestions);
|
| document.getElementById('generate').addEventListener('click', generateResponse);
|
| window.onload = loadModels;
|
| </script>
|
| </body>
|
| </html>
|
|
|