File size: 5,065 Bytes
f92bf53
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
<!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>