Repository navigation
Expand file tree
/
Copy pathscript.js
More file actions
411 lines (347 loc) · 14.3 KB
/
Copy pathscript.js
File metadata and controls
411 lines (347 loc) · 14.3 KB
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
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
class OllamaPromptTester {
constructor() {
this.baseURL = 'http://localhost:11434';
this.currentController = null;
this.startTime = null;
this.tokenCount = 0;
this.initializeElements();
this.bindEvents();
this.loadModels();
this.initializeSettings();
}
initializeElements() {
// UI 요소들
this.modelSelect = document.getElementById('modelSelect');
this.refreshModelsBtn = document.getElementById('refreshModels');
this.temperatureSlider = document.getElementById('temperature');
this.tempValue = document.getElementById('tempValue');
this.promptInput = document.getElementById('promptInput');
this.sendBtn = document.getElementById('sendPrompt');
this.clearPromptBtn = document.getElementById('clearPrompt');
this.stopBtn = document.getElementById('stopGeneration');
this.responseContainer = document.getElementById('responseContainer');
this.responseActions = document.querySelector('.response-actions');
this.copyBtn = document.getElementById('copyResponse');
this.clearResponseBtn = document.getElementById('clearResponse');
// 상태 및 통계 요소들
this.responseTimeSpan = document.getElementById('responseTime');
this.tokensPerSecondSpan = document.getElementById('tokensPerSecond');
this.tokenCountSpan = document.getElementById('tokenCount');
this.generationTimeSpan = document.getElementById('generationTime');
this.statusSpan = document.getElementById('status');
}
bindEvents() {
this.refreshModelsBtn.addEventListener('click', () => this.loadModels());
this.temperatureSlider.addEventListener('input', (e) => {
this.tempValue.textContent = e.target.value;
});
this.sendBtn.addEventListener('click', () => this.sendPrompt());
this.clearPromptBtn.addEventListener('click', () => this.clearPrompt());
this.stopBtn.addEventListener('click', () => this.stopGeneration());
this.copyBtn.addEventListener('click', () => this.copyResponse());
this.clearResponseBtn.addEventListener('click', () => this.clearResponse());
// Enter 키 단축키 (Ctrl+Enter로 전송)
this.promptInput.addEventListener('keydown', (e) => {
if (e.ctrlKey && e.key === 'Enter') {
e.preventDefault();
this.sendPrompt();
}
});
}
initializeSettings() {
this.updateStatus('ready', '준비');
}
async loadModels() {
this.updateStatus('generating', '모델 로딩중...');
this.modelSelect.innerHTML = '<option value="">로딩중...</option>';
try {
const response = await fetch(`${this.baseURL}/api/tags`);
if (!response.ok) {
throw new Error(`HTTP ${response.status}: ${response.statusText}`);
}
const data = await response.json();
this.modelSelect.innerHTML = '';
if (data.models && data.models.length > 0) {
data.models.forEach(model => {
const option = document.createElement('option');
option.value = model.name;
option.textContent = `${model.name} (${this.formatSize(model.size)})`;
this.modelSelect.appendChild(option);
});
this.updateStatus('ready', '준비');
} else {
this.modelSelect.innerHTML = '<option value="">설치된 모델이 없습니다</option>';
this.updateStatus('error', '모델 없음');
}
} catch (error) {
console.error('모델 로딩 실패:', error);
this.modelSelect.innerHTML = '<option value="">연결 실패</option>';
this.updateStatus('error', 'Ollama 연결 실패');
this.showError('Ollama 서버에 연결할 수 없습니다. Ollama가 실행중인지 확인해주세요.');
}
}
formatSize(bytes) {
if (!bytes) return 'Unknown size';
const sizes = ['B', 'KB', 'MB', 'GB'];
const i = Math.floor(Math.log(bytes) / Math.log(1024));
return Math.round(bytes / Math.pow(1024, i) * 100) / 100 + ' ' + sizes[i];
}
async sendPrompt() {
const prompt = this.promptInput.value.trim();
const model = this.modelSelect.value;
if (!prompt) {
alert('프롬프트를 입력해주세요.');
return;
}
if (!model) {
alert('모델을 선택해주세요.');
return;
}
this.startGeneration();
try {
const controller = new AbortController();
this.currentController = controller;
const response = await fetch(`${this.baseURL}/api/generate`, {
method: 'POST',
headers: {
'Content-Type': 'application/json',
},
body: JSON.stringify({
model: model,
prompt: prompt,
temperature: parseFloat(this.temperatureSlider.value),
stream: true
}),
signal: controller.signal
});
if (!response.ok) {
throw new Error(`HTTP ${response.status}: ${response.statusText}`);
}
await this.handleStreamResponse(response);
} catch (error) {
if (error.name === 'AbortError') {
this.updateStatus('ready', '중단됨');
} else {
console.error('생성 실패:', error);
this.updateStatus('error', '생성 실패');
this.showError(`생성 중 오류가 발생했습니다: ${error.message}`);
}
} finally {
this.endGeneration();
}
}
startGeneration() {
this.startTime = Date.now();
this.tokenCount = 0;
this.sendBtn.disabled = true;
this.stopBtn.style.display = 'inline-flex';
this.sendBtn.style.display = 'none';
this.clearResponse(false);
this.responseContainer.innerHTML = '<span class="streaming-cursor"></span>';
this.responseActions.style.display = 'none';
this.updateStatus('generating', '생성중...');
this.responseTimeSpan.textContent = '';
this.tokensPerSecondSpan.textContent = '';
}
endGeneration() {
this.sendBtn.disabled = false;
this.stopBtn.style.display = 'none';
this.sendBtn.style.display = 'inline-flex';
this.currentController = null;
// 커서 제거
const cursor = this.responseContainer.querySelector('.streaming-cursor');
if (cursor) {
cursor.remove();
}
const elapsed = this.startTime ? Date.now() - this.startTime : 0;
this.responseTimeSpan.textContent = `${elapsed}ms`;
this.generationTimeSpan.textContent = `${elapsed}ms`;
if (this.tokenCount > 0 && elapsed > 0) {
const tokensPerSecond = Math.round((this.tokenCount / elapsed) * 1000 * 100) / 100;
this.tokensPerSecondSpan.textContent = `${tokensPerSecond} tokens/s`;
}
this.updateStatus('complete', '완료');
this.responseActions.style.display = 'flex';
}
async handleStreamResponse(response) {
const reader = response.body.getReader();
const decoder = new TextDecoder();
let accumulatedResponse = '';
try {
while (true) {
const { done, value } = await reader.read();
if (done) break;
const chunk = decoder.decode(value, { stream: true });
const lines = chunk.split('\n').filter(line => line.trim());
for (const line of lines) {
try {
const data = JSON.parse(line);
if (data.response) {
accumulatedResponse += data.response;
this.tokenCount++;
this.updateStreamingDisplay(accumulatedResponse);
this.updateStats();
}
if (data.done) {
return;
}
} catch (parseError) {
console.warn('JSON 파싱 오류:', parseError, 'Line:', line);
}
}
}
} finally {
reader.releaseLock();
}
}
updateStreamingDisplay(text) {
// 기존 텍스트 업데이트 + 깜빡이는 커서 유지
const cursor = this.responseContainer.querySelector('.streaming-cursor');
this.responseContainer.textContent = text;
if (cursor) {
this.responseContainer.appendChild(cursor);
} else {
this.responseContainer.innerHTML += '<span class="streaming-cursor"></span>';
}
// 자동 스크롤
this.responseContainer.scrollTop = this.responseContainer.scrollHeight;
}
updateStats() {
this.tokenCountSpan.textContent = this.tokenCount;
if (this.startTime) {
const elapsed = Date.now() - this.startTime;
this.generationTimeSpan.textContent = `${elapsed}ms`;
if (elapsed > 0) {
const tokensPerSecond = Math.round((this.tokenCount / elapsed) * 1000 * 100) / 100;
this.tokensPerSecondSpan.textContent = `${tokensPerSecond} tokens/s`;
}
}
}
stopGeneration() {
if (this.currentController) {
this.currentController.abort();
}
}
clearPrompt() {
this.promptInput.value = '';
this.promptInput.focus();
}
clearResponse(showPlaceholder = true) {
if (showPlaceholder) {
this.responseContainer.innerHTML = '<div class="placeholder">프롬프트를 입력하고 전송 버튼을 눌러주세요.</div>';
this.responseActions.style.display = 'none';
this.tokenCount = 0;
this.updateStats();
this.responseTimeSpan.textContent = '';
this.tokensPerSecondSpan.textContent = '';
this.generationTimeSpan.textContent = '0ms';
this.updateStatus('ready', '준비');
} else {
this.responseContainer.innerHTML = '';
}
}
copyResponse() {
const text = this.responseContainer.textContent;
if (text && text !== '프롬프트를 입력하고 전송 버튼을 눌러주세요.') {
navigator.clipboard.writeText(text).then(() => {
const originalText = this.copyBtn.textContent;
this.copyBtn.textContent = '복사됨! ✓';
setTimeout(() => {
this.copyBtn.textContent = originalText;
}, 2000);
}).catch(err => {
console.error('복사 실패:', err);
alert('복사에 실패했습니다.');
});
}
}
updateStatus(type, message) {
this.statusSpan.className = `status-${type}`;
this.statusSpan.textContent = message;
}
showError(message) {
this.responseContainer.innerHTML = `<div style="color: #dc3545; padding: 20px; text-align: center; font-weight: 600;">${message}</div>`;
this.responseActions.style.display = 'none';
}
}
// DOM이 로드되면 앱 초기화
document.addEventListener('DOMContentLoaded', () => {
new OllamaPromptTester();
});
// 예시 프롬프트 데이터
const examplePrompts = {
"간단한 질문": "안녕하세요! 오늘 날씨는 어떤가요?",
"코딩 도움": "Python에서 리스트를 정렬하는 방법을 알려주세요. 예제 코드도 포함해주세요.",
"에이전트 프롬프트": `당신은 전문적인 소프트웨어 개발 어시스턴트입니다. 다음 역할과 지침을 따라주세요:
역할:
- 숙련된 풀스택 개발자로서 행동
- 최신 기술 트렌드와 베스트 프랙티스 숙지
- 코드 품질과 성능을 우선시
지침:
1. 명확하고 실용적인 솔루션 제공
2. 코드 예제는 주석과 함께 제공
3. 보안과 성능 고려사항 포함
4. 여러 접근법이 있다면 장단점 비교
질문: React에서 상태 관리를 위한 최적의 방법은 무엇인가요? 프로젝트 규모별로 추천해주세요.`
};
// 예시 프롬프트 추가 기능
document.addEventListener('DOMContentLoaded', () => {
const promptInput = document.getElementById('promptInput');
// 우클릭 컨텍스트 메뉴로 예시 프롬프트 추가
promptInput.addEventListener('contextmenu', (e) => {
e.preventDefault();
showExampleMenu(e.pageX, e.pageY);
});
});
function showExampleMenu(x, y) {
// 기존 메뉴 제거
const existingMenu = document.querySelector('.context-menu');
if (existingMenu) {
existingMenu.remove();
}
const menu = document.createElement('div');
menu.className = 'context-menu';
menu.style.cssText = `
position: fixed;
top: ${y}px;
left: ${x}px;
background: white;
border: 1px solid #ddd;
border-radius: 8px;
box-shadow: 0 4px 12px rgba(0,0,0,0.15);
z-index: 1000;
min-width: 200px;
`;
Object.entries(examplePrompts).forEach(([name, prompt]) => {
const item = document.createElement('div');
item.style.cssText = `
padding: 12px 16px;
cursor: pointer;
border-bottom: 1px solid #f0f0f0;
transition: background 0.2s;
`;
item.textContent = name;
item.addEventListener('mouseenter', () => {
item.style.background = '#f8f9fa';
});
item.addEventListener('mouseleave', () => {
item.style.background = 'white';
});
item.addEventListener('click', () => {
document.getElementById('promptInput').value = prompt;
menu.remove();
});
menu.appendChild(item);
});
document.body.appendChild(menu);
// 클릭 시 메뉴 닫기
const closeMenu = (e) => {
if (!menu.contains(e.target)) {
menu.remove();
document.removeEventListener('click', closeMenu);
}
};
setTimeout(() => {
document.addEventListener('click', closeMenu);
}, 100);
}