Widget:OllamaChat: различия между версиями
Перейти к навигации
Перейти к поиску
Rodion (обсуждение | вклад) Новая страница: «<div class="ollama-chat-widget"> <div class="chat-messages" style="height: 400px; overflow-y: auto; border: 1px solid #ccc; border-radius: 8px; padding: 10px; margin-bottom: 10px; background: #f9f9f9;"> <div class="message bot">Привет! Я AI-ассистент. Задайте мне вопрос.</div> </div> <div class="chat-input-area"> <select id="ollama-model-{$id}" style="margin-bottom: 10px; padding: 5px;">...» |
Rodion (обсуждение | вклад) Нет описания правки |
||
| (не показано 6 промежуточных версий этого же участника) | |||
| Строка 1: | Строка 1: | ||
<includeonly> | |||
<div class="ollama-chat-widget"> | <div class="ollama-chat-widget"> | ||
<div class="chat-messages" style="height: 400px; overflow-y: auto; border: 1px solid #ccc; border-radius: 8px; padding: 10px; margin-bottom: 10px; background: #f9f9f9;"> | <div class="chat-messages" style="height: 400px; overflow-y: auto; border: 1px solid #ccc; border-radius: 8px; padding: 10px; margin-bottom: 10px; background: #f9f9f9;"> | ||
<div class="message bot">Привет! Я AI-ассистент. Задайте мне вопрос.</div> | <div class="message bot">Привет! Я AI-ассистент. Доступные модели: Gemma 2b и DeepSeek-R1 1.5b. Задайте мне вопрос.</div> | ||
</div> | </div> | ||
<div class="chat-input-area"> | <div class="chat-input-area"> | ||
<select id="ollama-model | <select id="ollama-model" style="margin-bottom: 10px; padding: 5px; width: 200px;"> | ||
<option value="gemma:2b">Gemma 2b (быстрая)</option> | |||
<option value="gemma:2b"> | <option value="deepseek-r1:1.5b">DeepSeek-R1 1.5b (логическая)</option> | ||
<option value=" | |||
</select> | </select> | ||
<div style="display: flex; gap: 10px;"> | <div style="display: flex; gap: 10px; margin-top: 10px;"> | ||
<input type="text" id="ollama-input | <input type="text" id="ollama-input" placeholder="Введите сообщение..." style="flex: 1; padding: 8px; border: 1px solid #ccc; border-radius: 4px;"> | ||
<button id="ollama-send | <button id="ollama-send" style="padding: 8px 16px; background: #007bff; color: white; border: none; border-radius: 4px; cursor: pointer;">Отправить</button> | ||
</div> | </div> | ||
</div> | </div> | ||
| Строка 19: | Строка 18: | ||
<script> | <script> | ||
(function() { | (function() { | ||
const | const messagesDiv = document.querySelector('.chat-messages'); | ||
const inputField = document.getElementById('ollama-input'); | |||
const sendButton = document.getElementById('ollama-send'); | |||
const modelSelect = document.getElementById('ollama-model'); | |||
const inputField = | |||
const sendButton = | const OLLAMA_URL = '/ollama-proxy.php/api/generate'; | ||
const modelSelect = | |||
const OLLAMA_URL = ' | |||
function addMessage(text, isUser) { | function addMessage(text, isUser) { | ||
| Строка 91: | Строка 87: | ||
prompt: question, | prompt: question, | ||
stream: false, | stream: false, | ||
options: { temperature: 0.7 } | options: { | ||
temperature: 0.7, | |||
num_predict: 512 | |||
} | |||
}) | }) | ||
}); | }); | ||
if (!response.ok) throw new Error(`HTTP ${response.status}: ${ | if (!response.ok) { | ||
const errorText = await response.text(); | |||
throw new Error(`HTTP ${response.status}: ${errorText}`); | |||
} | |||
const data = await response.json(); | const data = await response.json(); | ||
hideTyping(); | hideTyping(); | ||
let answer = data.response || 'Извините, не удалось получить ответ.'; | |||
if (modelSelect.value === 'deepseek-r1:1.5b') { | |||
answer = answer.replace(/<thinking>[\s\S]*?<\/thinking>/g, '').trim(); | |||
if (!answer) answer = 'Ответ не сформирован.'; | |||
} | |||
addMessage(answer, false); | |||
} catch (error) { | } catch (error) { | ||
hideTyping(); | hideTyping(); | ||
addMessage('❌ Ошибка: ' + error.message, false); | |||
} finally { | } finally { | ||
inputField.disabled = false; | inputField.disabled = false; | ||
| Строка 119: | Строка 123: | ||
if (e.key === 'Enter') sendMessage(); | if (e.key === 'Enter') sendMessage(); | ||
}); | }); | ||
inputField.focus(); | |||
})(); | })(); | ||
</script> | </script> | ||
</includeonly> | |||
<noinclude>[[Категория:Виджеты]]</noinclude> | |||