Widget:OllamaChat: различия между версиями
Перейти к навигации
Перейти к поиску
Rodion (обсуждение | вклад) Нет описания правки |
Rodion (обсуждение | вклад) Нет описания правки |
||
| Строка 2: | Строка 2: | ||
<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;"> | <span style="margin-left: 10px; font-size: 12px; color: #666;"> | ||
<input type="text" id="ollama-input | Сервер: 82.148.28.75:11434 | ||
<button id="ollama-send | </span> | ||
<div style="display: flex; gap: 10px; margin-top: 10px;"> | |||
<input type="text" id="ollama-input" placeholder="Введите сообщение..." style="flex: 1; padding: 8px; border: 1px solid #ccc; border-radius: 4px;"> | |||
<button id="ollama-send" style="padding: 8px 16px; background: #007bff; color: white; border: none; border-radius: 4px; cursor: pointer;">Отправить</button> | |||
</div> | </div> | ||
</div> | </div> | ||
| Строка 20: | Строка 21: | ||
<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 modelSelect = | |||
const OLLAMA_URL = 'http://82.148.28.75:11434/api/generate'; | const OLLAMA_URL = 'http://82.148.28.75:11434/api/generate'; | ||
| Строка 92: | Строка 90: | ||
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 || 'Извините, не удалось получить ответ.'; | |||
// Для DeepSeek-R1 убираем внутренние рассуждения, если нужно | |||
if (modelSelect.value === 'deepseek-r1:1.5b') { | |||
answer = answer.replace(/<thinking>[\s\S]*?<\/thinking>/g, '').trim(); | |||
} | |||
addMessage(answer, false); | |||
} catch (error) { | } catch (error) { | ||
| Строка 106: | Строка 116: | ||
let errorMsg = 'Ошибка: ' + error.message; | let errorMsg = 'Ошибка: ' + error.message; | ||
if (error.message.includes('Failed to fetch')) { | if (error.message.includes('Failed to fetch')) { | ||
errorMsg = 'Ошибка: не удалось подключиться к серверу Ollama (82.148.28.75:11434). | errorMsg = '❌ Ошибка: не удалось подключиться к серверу Ollama (82.148.28.75:11434).\n\nПроверьте:\n1. Доступность сервера: curl http://82.148.28.75:11434/api/tags\n2. Настройки CORS на сервере Ollama (нужно OLLAMA_ORIGINS="*")\n3. Брандмауэр и открытый порт 11434'; | ||
} else if (error.message.includes('404')) { | |||
errorMsg = '❌ Модель "' + modelSelect.value + '" не найдена. Установите её командой: ollama pull ' + modelSelect.value; | |||
} | } | ||
addMessage(errorMsg, false); | addMessage(errorMsg, false); | ||
| Строка 120: | Строка 132: | ||
if (e.key === 'Enter') sendMessage(); | if (e.key === 'Enter') sendMessage(); | ||
}); | }); | ||
// Фокус на поле ввода при загрузке | |||
inputField.focus(); | |||
})(); | })(); | ||
</script> | </script> | ||
</includeonly> | </includeonly> | ||
<noinclude>[[Категория:Виджеты]]</noinclude> | <noinclude>[[Категория:Виджеты]]</noinclude> | ||