Widget:OllamaChat: различия между версиями

Материал из ultracity
Перейти к навигации Перейти к поиску
Новая страница: «<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;">...»
 
Нет описания правки
 
(не показано 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-{$id}" style="margin-bottom: 10px; padding: 5px;">
         <select id="ollama-model" style="margin-bottom: 10px; padding: 5px; width: 200px;">
            <option value="llama3.2">llama3.2</option>
             <option value="gemma:2b">Gemma 2b (быстрая)</option>
             <option value="gemma:2b">gemma:2b</option>
             <option value="deepseek-r1:1.5b">DeepSeek-R1 1.5b (логическая)</option>
             <option value="mistral">mistral</option>
            <option value="phi3">phi3</option>
         </select>
         </select>
         <div style="display: flex; gap: 10px;">
         <div style="display: flex; gap: 10px; margin-top: 10px;">
             <input type="text" id="ollama-input-{$id}" placeholder="Введите сообщение..." style="flex: 1; padding: 8px; border: 1px solid #ccc; border-radius: 4px;">
             <input type="text" id="ollama-input" placeholder="Введите сообщение..." style="flex: 1; padding: 8px; border: 1px solid #ccc; border-radius: 4px;">
             <button id="ollama-send-{$id}" style="padding: 8px 16px; background: #007bff; color: white; border: none; border-radius: 4px; cursor: pointer;">Отправить</button>
             <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 container = document.currentScript.parentElement;
     const messagesDiv = document.querySelector('.chat-messages');
    const uniqueId = Math.random().toString(36).substr(2, 8);
     const inputField = document.getElementById('ollama-input');
   
     const sendButton = document.getElementById('ollama-send');
    const messagesDiv = container.querySelector('.chat-messages');
     const modelSelect = document.getElementById('ollama-model');
     const inputField = container.querySelector('#ollama-input-{$id}');
     const sendButton = container.querySelector('#ollama-send-{$id}');
     const OLLAMA_URL = '/ollama-proxy.php/api/generate';
     const modelSelect = container.querySelector('#ollama-model-{$id}');
   
     const OLLAMA_URL = 'http://82.148.28.75:11434/api/generate';
      
      
     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}: ${response.statusText}`);
             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();
             addMessage(data.response || 'Извините, не удалось получить ответ.', false);
              
            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();
             let errorMsg = 'Ошибка: ' + error.message;
             addMessage('Ошибка: ' + error.message, false);
            if (error.message.includes('Failed to fetch')) {
                errorMsg = 'Ошибка: не удалось подключиться к серверу Ollama (82.148.28.75:11434). Проверьте, что сервер доступен и CORS настроен.';
            }
            addMessage(errorMsg, 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>

Текущая версия от 08:45, 25 марта 2026