-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathapp.py
387 lines (321 loc) · 13.8 KB
/
app.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
from flask import Flask, Response, stream_with_context, request
import json
import opengradient as og
from sseclient import SSEClient
import threading
import logging
from typing import Dict, Any
import os
import queue
from dataclasses import dataclass
from datetime import datetime
from logging.handlers import RotatingFileHandler
import traceback
import time
import requests
from vertexai.preview.generative_models import GenerativeModel
import vertexai
# Configure logging with more detailed setup
logging.basicConfig(
level=logging.INFO,
)
logger = logging.getLogger(__name__)
# Configuration
class Config:
# Load from decrypted config files in config/ directory
with open('config/opengradient.json') as f:
og_config = json.load(f)
with open('config/index.json') as f:
index_config = json.load(f)
with open('config/project.json') as f:
project_config = json.load(f)
OG_EMAIL = og_config['email']
OG_PASSWORD = og_config['password']
OG_PRIVATE_KEY = og_config['private_key']
BASE_URL = index_config['base_url']
SOURCE = index_config['source']
PROMPT = index_config['prompt']
PROJECT_ID = project_config['project_id']
class LLMProvider:
def __init__(self):
self.providers = {
'og': {
'call': self._call_opengradient,
'parse': self._parse_og_response
},
'gemini': {
'call': self._call_gemini,
'parse': self._parse_gemini_response
}
}
try:
# Set Google credentials from decrypted config
google_creds_path = 'config/google.json'
if os.path.exists(google_creds_path):
os.environ['GOOGLE_APPLICATION_CREDENTIALS'] = google_creds_path
vertexai.init(
project=os.getenv('GOOGLE_CLOUD_PROJECT', Config.PROJECT_ID),
location='us-central1'
)
except Exception as e:
logger.error(f"Failed to initialize Vertex AI: {str(e)}")
raise
def _call_opengradient(self, prompt: str) -> tuple[str, str]:
tx_hash, response = og.llm_completion(
model_cid='meta-llama/Meta-Llama-3-8B-Instruct',
prompt=prompt,
max_tokens=5000,
temperature=0.2,
stop_sequence=['}']
)
return tx_hash, response
def _call_gemini(self, prompt: str) -> tuple[str, str]:
model = GenerativeModel('gemini-1.5-pro-002')
generation_config = {
'max_output_tokens': 512,
'temperature': 0.2,
'top_p': 0.95
}
response = model.generate_content(
prompt,
generation_config=generation_config
)
return 'gemini', response.text
def get_completion(self, provider: str, prompt: str) -> tuple[str, str]:
if provider not in self.providers:
raise ValueError(f"Unknown provider: {provider}")
tx_hash, response = self.providers[provider]['call'](prompt)
parsed_response = self.providers[provider]['parse'](response)
return tx_hash, parsed_response
@staticmethod
def _parse_og_response(response: str) -> str:
"""Parse OpenGradient response which needs JSON completion."""
response = response + '}'
return response
@staticmethod
def _parse_gemini_response(response: str) -> str:
"""Parse Gemini response which may include markdown code blocks."""
# Remove markdown code blocks if present
if '```json' in response:
response = response.split('```json')[-1].split('```')[0]
return response.strip()
class UpdateProcessor:
def __init__(self):
self.active_routers = {}
self.llm_provider = LLMProvider()
def evaluate_update(self, prompt: str, update: Dict[str, Any]) -> Dict[str, Any]:
"""Evaluate an update using specified LLM provider."""
logger.info(f"Starting evaluation for prompt: {prompt[:100]}...")
logger.info(f"Update content: {json.dumps(update, indent=2)}")
try:
author_name = update['author'].get('name') or update['author']['username']
update_prompt = self._build_prompt(prompt, update, author_name)
# logger.info(f"Built prompt: {update_prompt}")
# Use provider (can be configured via env var or passed parameter)
provider = os.getenv('LLM_PROVIDER', 'gemini')
logger.info(f"Using LLM provider: {provider}")
tx_hash, response = self.llm_provider.get_completion(provider, update_prompt)
logger.info(f"LLM response: {response}")
result = self._parse_response(response)
# result['tx_hash'] = tx_hash
logger.info(f"Parsed result: {json.dumps(result, indent=2)}")
return result
except Exception as e:
logger.error(f"Error evaluating update: {str(e)}", exc_info=True)
return {
"decision": "stop",
}
@staticmethod
def _build_prompt(prompt: str, update: dict, author_name: str) -> str:
# Get base prompt from remote IPFS
response = requests.get(f"https://ipfs.index.network/files/{Config.PROMPT}")
basePrompt = response.text
return f""" {basePrompt}
-------------------
Here is the conversation history to check intent:
- User: Show me {prompt} and nothing else.
-------------------
New cast update to evaluate:
Cast Text: {update['text']}
Cast Link: {update['link']}
Cast Author: [{author_name}](https://warpcast.com/{update['author']['username']})
Output response only in JSON format with the following structure:
{{
"decision": "recommend" | "inappropriate" | "stop",
"rationale": "explanation for the decision",
"score": numeric_value,
"message": "update message for the conversation"
}}"""
@staticmethod
def _parse_response(response: str) -> dict:
"""Parse and clean the OpenGradient response to extract valid JSON."""
try:
# Remove any "Here is the output:" prefix
if "Here is the output:" in response:
response = response.split("Here is the output:")[-1].strip()
# Try parsing the response as-is first
try:
return json.loads(response)
except json.JSONDecodeError:
# If that fails, try the cleanup approach
start_idx = response.find('{')
end_idx = response.rfind('}')
if start_idx == -1 or end_idx == -1:
raise ValueError("No valid JSON object found in response")
# Extract everything between the first '{' and last '}'
json_str = response[start_idx:end_idx + 1]
return json.loads(json_str)
except Exception as e:
logger.error(f"Failed to parse response: {response}")
raise json.JSONDecodeError(f"Failed to parse response: {str(e)}", response, 0)
# Initialize app and processor
app = Flask(__name__)
processor = UpdateProcessor()
og.init(email=Config.OG_EMAIL, password=Config.OG_PASSWORD, private_key=Config.OG_PRIVATE_KEY)
# Add a global queue for updates
update_queue = queue.Queue()
@dataclass
class RouterConfig:
prompt: str
queue: queue.Queue
created_at: datetime
def process_update(updated_item: Dict[str, Any]) -> None:
"""Process an update from the event stream."""
logger.info(f"Processing new update: {json.dumps(updated_item, indent=2)}")
try:
if not updated_item.get('data', {}).get('node'):
logger.warning(f"Rejected malformed request: {json.dumps(updated_item, indent=2)}")
return
cast = updated_item['data']['node']
# logger.info(f"Processing cast: {json.dumps(cast, indent=2)}")
cast['link'] = f"https://warpcast.com/{cast['author']['username']}/{cast['hash'][:12]}"
cast['channel_id'] = updated_item.get('data', {}).get('channel', {}).get('id')
logger.info(f"Active routers: {list(processor.active_routers.keys())}")
for prompt, router_config in processor.active_routers.items():
logger.info(f"Processing for prompt: {prompt[:100]}")
try:
result = processor.evaluate_update(router_config.prompt, cast)
logger.info(f"Evaluation result: {json.dumps(result, indent=2)}")
result['item'] = updated_item
# If recommended, add to queue for routers
if result['decision'] == 'recommend':
router_config.queue.put(result)
logger.info(f"Added recommended update to queue for prompt: {prompt[:100]}")
except Exception as e:
logger.error(f"Error processing update for prompt {prompt[:100]}: {str(e)}", exc_info=True)
except Exception as e:
logger.error(f"Error processing update: {str(e)}", exc_info=True)
def router_thread(url: str):
"""Separated router thread function with robust error handling and backoff"""
logger.info(f"Starting SSE router thread with URL: {url}")
retry_count = 0
max_retries = 5
base_delay = 5 # Base delay in seconds
while True:
try:
logger.info(f"Connecting to SSE stream... {url}")
headers = {
'Accept': 'text/event-stream',
'Connection': 'keep-alive'
}
# Configure session with no read timeout
session = requests.Session()
session.keep_alive = False # Disable keep-alive to prevent stale connections
response = session.get(
url,
headers=headers,
stream=True,
timeout=(30, None) # (connect timeout, read timeout=None for infinite)
)
response.raise_for_status()
client = SSEClient(response.raw, char_enc='utf-8')
logger.info("Successfully connected to SSE stream")
# Reset retry count on successful connection
retry_count = 0
for event in client.events():
try:
data = json.loads(event.data)
logger.info(f"Received SSE data")
process_update(data)
except json.JSONDecodeError as e:
logger.error(f"Failed to parse SSE message: {str(e)}", exc_info=True)
continue # Continue to next event on parse error
except Exception as e:
logger.error(f"Error handling message: {str(e)}", exc_info=True)
continue # Continue to next event on other errors
except (requests.exceptions.RequestException, TimeoutError) as e:
logger.error(f"SSE connection error, attempting reconnection: {str(e)}", exc_info=True)
# Clean up resources
try:
response.close()
session.close()
except:
pass
# Immediate retry for connection errors
continue
except Exception as e:
retry_count += 1
delay = min(base_delay * (2 ** retry_count), 60) # Exponential backoff, max 60 seconds
logger.error(
f"Unexpected error (attempt {retry_count}/{max_retries}), "
f"retrying in {delay} seconds: {str(e)}",
exc_info=True
)
if retry_count >= max_retries:
logger.critical("Max retries reached, resetting retry count")
retry_count = 0
# Clean up resources
try:
response.close()
session.close()
except:
pass
time.sleep(delay) # Wait before retrying
def start_router():
"""Start the SSE router in a separate thread."""
logger.info("Initializing SSE router")
# Use Config variables instead of hardcoded values
url = f"{Config.BASE_URL}/discovery/updates?sources[]={Config.SOURCE}"
thread = threading.Thread(target=router_thread, args=(url,), daemon=True)
thread.start()
return thread
# Add new route for SSE updates
@app.route('/')
def get_updates():
"""Stream updates to clients using Server-Sent Events based on specific prompt."""
prompt = request.args.get('prompt') # Use Config.PROMPT as default
if not prompt:
return Response("Missing 'prompt' parameter", status=400)
# Create new router configuration if doesn't exist
if prompt not in processor.active_routers:
processor.active_routers[prompt] = RouterConfig(
prompt=prompt,
queue=queue.Queue(),
created_at=datetime.now()
)
router_config = processor.active_routers[prompt]
def generate():
while True:
try:
# Get update from queue specific to this prompt, timeout after 30 seconds
update = router_config.queue.get()
yield f"data: {json.dumps(update)}\n\n"
except queue.Empty:
# Send keepalive
yield ": keepalive\n\n"
except Exception as e:
logger.error(f"Error streaming update: {str(e)}")
continue
return Response(
stream_with_context(generate()),
mimetype='text/event-stream',
headers={
'Cache-Control': 'no-cache',
'Connection': 'keep-alive'
}
)
if __name__ == '__main__':
# Start the router when the app starts
router_thread = start_router()
# Run the Flask app
app.run(host='0.0.0.0', port=8000)