File size: 4,998 Bytes
ce72224
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
import { useState, useEffect } from 'react'
import './App.css'
import ChatBox from './components/ChatBox'
import PluginLoader from './components/PluginLoader'
import { sendMessage, getStats } from './utils/api'

function App() {
  const [messages, setMessages] = useState([])
  const [loading, setLoading] = useState(false)
  const [systemPrompt, setSystemPrompt] = useState('Du bist ein hilfsbereiter KI-Assistent.')
  const [temperature, setTemperature] = useState(0.7)
  const [topP, setTopP] = useState(0.9)
  const [stats, setStats] = useState(null)
  const [plugins, setPlugins] = useState([])

  useEffect(() => {
    // Auf Plugin-Änderungen hören
    window.addEventListener('pluginsLoaded', (e) => {
      setPlugins(e.detail)
      console.log('Plugins loaded:', e.detail)
    })

    return () => {
      window.removeEventListener('pluginsLoaded', () => {})
    }
  }, [])

  const handleSendMessage = async (message) => {
    // Nachricht zum Chat hinzufügen
    const userMessage = {
      id: Date.now(),
      role: 'user',
      content: message,
      timestamp: new Date(),
    }
    
    setMessages(prev => [...prev, userMessage])
    setLoading(true)

    try {
      // Plugins benachrichtigen
      window.dispatchEvent(new CustomEvent('messageSent', {
        detail: { message, systemPrompt }
      }))

      const response = await sendMessage(message, systemPrompt, temperature, topP)
      
      const assistantMessage = {
        id: Date.now() + 1,
        role: 'assistant',
        content: response.response,
        timestamp: new Date(),
        stats: {
          tokens: response.tokens,
          time: response.time_seconds,
        }
      }
      
      setMessages(prev => [...prev, assistantMessage])
      setStats(response)

      // Plugins über Antwort benachrichtigen
      window.dispatchEvent(new CustomEvent('responseReceived', {
        detail: assistantMessage
      }))

    } catch (error) {
      console.error('Error:', error)
      const errorMessage = {
        id: Date.now() + 1,
        role: 'assistant',
        content: `❌ Fehler: ${error.message}`,
        timestamp: new Date(),
        isError: true,
      }
      setMessages(prev => [...prev, errorMessage])
    } finally {
      setLoading(false)
    }
  }

  return (
    <div className="app-container">

      <PluginLoader onPluginsLoad={setPlugins} />

      

      <header className="app-header">

        <h1>🤖 Zephyr-7B Chatbot</h1>

        <p className="tagline">Powered by Hugging Face</p>

      </header>



      <div className="app-content">

        <aside className="sidebar">

          <div className="sidebar-section">

            <h3>⚙️ Einstellungen</h3>

            

            <label>System Prompt</label>

            <textarea

              value={systemPrompt}

              onChange={(e) => setSystemPrompt(e.target.value)}

              placeholder="Definiere die Rolle des Assistenten..."

              disabled={loading}

            />

            

            <label>

              Temperatur: {temperature.toFixed(2)}

              <input

                type="range"

                min="0"

                max="2"

                step="0.1"

                value={temperature}

                onChange={(e) => setTemperature(parseFloat(e.target.value))}

                disabled={loading}

              />

            </label>



            <label>

              Top P: {topP.toFixed(2)}

              <input

                type="range"

                min="0"

                max="1"

                step="0.05"

                value={topP}

                onChange={(e) => setTopP(parseFloat(e.target.value))}

                disabled={loading}

              />

            </label>



            {stats && (

              <div className="stats-box">

                <h4>📊 Letzte Antwort</h4>

                <div className="stat-item">

                  <span>Tokens:</span>

                  <strong>{stats.tokens}</strong>

                </div>

                <div className="stat-item">

                  <span>Zeit:</span>

                  <strong>{stats.time_seconds}s</strong>

                </div>

              </div>

            )}



            {plugins.length > 0 && (

              <div className="plugins-box">

                <h4>🔌 Plugins ({plugins.length})</h4>

                <ul>

                  {plugins.map(plugin => (

                    <li key={plugin}>{plugin}</li>

                  ))}

                </ul>

              </div>

            )}

          </div>

        </aside>



        <main className="chat-container">

          <ChatBox

            messages={messages}

            onSendMessage={handleSendMessage}

            loading={loading}

          />

        </main>

      </div>

    </div>
  )
}

export default App