-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathrage.py
414 lines (361 loc) · 17.2 KB
/
rage.py
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
412
413
414
# rage.py (c) 2025 Gregory L. Magnusson MIT license
import sys
from pathlib import Path
sys.path.append(str(Path(__file__).parent))
import streamlit as st
import time
from datetime import datetime
from typing import Dict, Any, Optional
from src.models import (
GPT4Handler,
GroqHandler,
TogetherHandler,
OllamaHandler,
HuggingFaceHandler
)
from src.memory import (
memory_manager,
DialogEntry,
MemoryEntry,
store_conversation,
get_conversation_history
)
from src.config import get_config, get_model_config
from src.logger import get_logger
from src.openmind import OpenMind
# Initialize logger
logger = get_logger('rage')
class RAGE:
"""RAGE - Retrieval Augmented Generative Engine"""
def __init__(self):
self.setup_session_state()
self.config = get_config()
self.model_config = get_model_config()
self.load_css()
# Initialize systems
self.memory = memory_manager
self.openmind = OpenMind()
def setup_session_state(self):
"""Initialize session state variables"""
if "messages" not in st.session_state:
st.session_state.messages = []
if 'provider' not in st.session_state:
st.session_state.provider = None
if 'selected_model' not in st.session_state:
st.session_state.selected_model = None
if 'model_capabilities' not in st.session_state:
st.session_state.model_capabilities = []
if 'cost_tracking' not in st.session_state:
st.session_state.cost_tracking = {"total": 0.0, "session": 0.0}
if 'model_instances' not in st.session_state:
st.session_state.model_instances = {
'ollama': None,
'groq': None,
'together': None,
'openai': None,
'huggingface': None
}
def check_ollama_status(self):
"""Check Ollama installation and available models"""
try:
if not st.session_state.model_instances['ollama']:
st.session_state.model_instances['ollama'] = OllamaHandler()
if st.session_state.model_instances['ollama'].check_installation():
models = st.session_state.model_instances['ollama'].list_models()
return True, models
return False, []
except Exception as e:
logger.error(f"Error checking Ollama status: {e}")
return False, []
def load_css(self):
"""Load CSS styling"""
try:
with open('styles.css') as f:
st.markdown(f'<style>{f.read()}</style>', unsafe_allow_html=True)
except Exception as e:
logger.error(f"Error loading CSS: {e}")
self.load_default_css()
def load_default_css(self):
"""Load default CSS if custom CSS fails"""
st.markdown("""
<style>
.cost-tracker { padding: 10px; background: #262730; border-radius: 5px; }
.model-info { padding: 10px; background: #1E1E1E; border-radius: 5px; }
.capability-tag {
display: inline-block;
padding: 2px 8px;
margin: 2px;
background: #3B3B3B;
border-radius: 12px;
font-size: 0.8em;
}
.api-key-status {
display: flex;
align-items: center;
gap: 8px;
padding: 5px;
margin: 5px 0;
}
.checkmark {
color: #00cc00;
font-weight: bold;
}
</style>
""", unsafe_allow_html=True)
def initialize_model(self, provider: str) -> Optional[Any]:
"""Initialize or retrieve model instance"""
try:
if not provider:
st.info("Please select an AI Provider")
return None
if provider == "Together":
key = self.openmind.get_api_key('together')
if key:
if not st.session_state.model_instances['together']:
st.session_state.model_instances['together'] = TogetherHandler(key)
return st.session_state.model_instances['together']
else:
st.error("Together API key not found")
return None
elif provider == "Groq":
key = self.openmind.get_api_key('groq')
if key:
if not st.session_state.model_instances['groq']:
st.session_state.model_instances['groq'] = GroqHandler(key)
return st.session_state.model_instances['groq']
else:
st.error("Groq API key not found")
return None
elif provider == "OpenAI":
key = self.openmind.get_api_key('openai')
if key:
if not st.session_state.model_instances['openai']:
st.session_state.model_instances['openai'] = GPT4Handler(key)
return st.session_state.model_instances['openai']
else:
st.error("OpenAI API key not found")
return None
elif provider == "Ollama":
if not st.session_state.model_instances['ollama']:
st.session_state.model_instances['ollama'] = OllamaHandler()
if st.session_state.model_instances['ollama'].check_installation():
available_models = st.session_state.model_instances['ollama'].list_models()
if available_models:
if not st.session_state.selected_model:
st.info("Please select an Ollama model to continue")
return None
if st.session_state.model_instances['ollama'].select_model(st.session_state.selected_model):
return st.session_state.model_instances['ollama']
else:
st.error(st.session_state.model_instances['ollama'].get_last_error())
return None
else:
st.error("No Ollama models found. Please pull a model first.")
return None
else:
st.error("Ollama service is not running. Please start the Ollama service.")
return None
elif provider == "HuggingFace":
if not st.session_state.model_instances['huggingface']:
st.session_state.model_instances['huggingface'] = HuggingFaceHandler()
return st.session_state.model_instances['huggingface']
return None
except Exception as e:
logger.error(f"Error initializing model: {e}")
st.error(f"Error initializing model: {str(e)}")
return None
def update_cost_tracking(self, response_length: int):
"""Update cost tracking based on usage"""
try:
if st.session_state.provider and st.session_state.selected_model:
model_info = self.model_config.get_model_info(
st.session_state.provider.lower(),
st.session_state.selected_model
)
if model_info and 'cost' in model_info:
cost_str = model_info['cost']
if '/1M tokens' in cost_str:
base_cost = float(cost_str.split('$')[1].split('/')[0])
tokens = response_length / 4
cost = (tokens / 1000000) * base_cost
elif '/1K tokens' in cost_str:
base_cost = float(cost_str.split('$')[1].split('/')[0])
tokens = response_length / 4
cost = (tokens / 1000) * base_cost
else:
cost = 0.0
st.session_state.cost_tracking["session"] += cost
st.session_state.cost_tracking["total"] += cost
except Exception as e:
logger.error(f"Error updating cost tracking: {e}")
def process_message(self, prompt: str):
"""Process user message and generate response"""
try:
if not st.session_state.provider:
st.warning("Please select an AI Provider first")
return
model = self.initialize_model(st.session_state.provider)
if not model:
return
# Add message to session state
st.session_state.messages.append({"role": "user", "content": prompt})
with st.chat_message("user"):
st.markdown(prompt)
with st.chat_message("assistant"):
with st.spinner("Processing with RAGE..."):
try:
# Get relevant context
context = self.memory.get_relevant_context(prompt)
# Generate response
response = model.generate_response(prompt, context)
if isinstance(model, OllamaHandler) and model.get_last_error():
st.error(model.get_last_error())
return
# Store conversation
dialog_entry = DialogEntry(
query=prompt,
response=response,
provider=st.session_state.provider,
model=st.session_state.selected_model,
context={"retrieved_context": context}
)
store_conversation(dialog_entry)
# Display response
st.markdown(response)
# Update tracking
self.update_cost_tracking(len(response))
st.session_state.messages.append({
"role": "assistant",
"content": response
})
except Exception as e:
logger.error(f"Error generating response: {e}")
st.error(f"Error generating response: {str(e)}")
except Exception as e:
logger.error(f"Error processing message: {e}")
st.error("An error occurred while processing your message")
def setup_sidebar(self):
"""Setup sidebar configuration"""
with st.sidebar:
st.header("RAGE Configuration")
# Check Ollama status
ollama_running, ollama_models = self.check_ollama_status()
if ollama_running:
st.markdown("""
<div class="api-key-status">
<span class="checkmark">●</span>
<span class="text">Ollama Running</span>
</div>
""", unsafe_allow_html=True)
if ollama_models:
st.caption(f"Available models: {', '.join(ollama_models)}")
# Provider selection
previous_provider = st.session_state.provider
st.session_state.provider = st.selectbox(
"Select AI Provider",
[None, "OpenAI", "Together", "Groq", "Ollama", "HuggingFace"],
format_func=lambda x: "Select Provider" if x is None else x
)
if previous_provider != st.session_state.provider:
st.session_state.selected_model = None
# Model selection
if st.session_state.provider:
if st.session_state.provider == "Ollama":
if ollama_models:
st.session_state.selected_model = st.selectbox(
"Select Ollama Model",
options=ollama_models,
key='ollama_model_select'
)
else:
provider_models = self.model_config.get_provider_models(
st.session_state.provider.lower()
)
if provider_models:
st.session_state.selected_model = st.selectbox(
f"Select {st.session_state.provider} Model",
options=list(provider_models.keys()),
key=f"{st.session_state.provider.lower()}_model_select"
)
# API key handling
if st.session_state.provider in ["OpenAI", "Together", "Groq"]:
# Check if API key exists
existing_key = self.openmind.get_api_key(
st.session_state.provider.lower()
)
# Show API key status
if existing_key:
st.markdown(f"""
<div class="api-key-status">
<span class="checkmark">✓</span>
<span class="text">{st.session_state.provider} API Key Stored</span>
</div>
""", unsafe_allow_html=True)
# API key input
api_key = st.text_input(
f"{st.session_state.provider} API Key",
type="password",
key=f"{st.session_state.provider.lower()}_api_key"
)
if api_key:
self.openmind.save_api_key(
st.session_state.provider.lower(),
api_key
)
try:
st.rerun()
except AttributeError:
try:
st.experimental_rerun()
except AttributeError:
st.empty()
# Display model information
if st.session_state.provider and st.session_state.selected_model:
model_info = self.model_config.get_model_info(
st.session_state.provider.lower(),
st.session_state.selected_model
)
if model_info:
st.markdown("### Model Information")
st.markdown(f"""
<div class="model-info">
<p><strong>Model:</strong> {model_info.name}</p>
<p><strong>Developer:</strong> {model_info.developer}</p>
<p><strong>Max Tokens:</strong> {model_info.tokens}</p>
<p><strong>Cost:</strong> {model_info.cost}</p>
<div><strong>Capabilities:</strong></div>
{''.join([f'<span class="capability-tag">{cap}</span>'
for cap in model_info.capabilities])}
</div>
""", unsafe_allow_html=True)
def run(self):
"""Run the RAGE interface"""
try:
st.title("RAGE - Retrieval Augmented Generative Engine")
# Display cost tracker
st.markdown(f"""
<div class="cost-tracker">
Session Cost: ${st.session_state.cost_tracking['session']:.4f}<br>
Total Cost: ${st.session_state.cost_tracking['total']:.4f}
</div>
""", unsafe_allow_html=True)
# Setup sidebar
self.setup_sidebar()
# Chat interface
chat_container = st.container()
with chat_container:
# Display conversation history
for message in st.session_state.messages:
with st.chat_message(message["role"]):
st.markdown(message["content"])
# Chat input
if prompt := st.chat_input("Enter your query..."):
self.process_message(prompt)
except Exception as e:
logger.error(f"Main application error: {e}")
st.error("An error occurred in the application. Please try refreshing the page.")
def main():
rage = RAGE()
rage.run()
if __name__ == "__main__":
main()