-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapp.py
More file actions
583 lines (473 loc) Β· 19.3 KB
/
app.py
File metadata and controls
583 lines (473 loc) Β· 19.3 KB
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
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
"""
AI Chat with Weather Tool + Confluence RAG + Fixed Streaming
Proper async/sync streaming implementation
"""
from flask import Flask, render_template, request, jsonify, Response, stream_with_context
import torch
from transformers import AutoModelForCausalLM, AutoTokenizer, TextIteratorStreamer
from peft import PeftModel
import json
import os
import asyncio
from threading import Lock, Thread
from typing import List, Dict, Tuple, Optional, Generator
from functools import wraps
app = Flask(__name__)
# ============================================================================
# CONFIGURATION
# ============================================================================
CONFIG = {
"base_model": "Qwen/Qwen2.5-1.5B-Instruct",
"adapter_path": None,
"device": "cpu",
"max_length": 200,
"temperature": 0.7,
"top_p": 0.95,
"top_k": 50,
"repetition_penalty": 1.1,
"tool_temperature": 0.0,
"max_tool_tokens": 220,
"max_tool_rounds": 3,
"apify_token": ""
}
# ============================================================================
# GLOBAL STATE
# ============================================================================
model = None
tokenizer = None
model_lock = Lock()
model_loaded = False
weather_mcp = None
weather_lock = Lock()
confluence_rag = None
confluence_lock = Lock()
# ============================================================================
# ASYNC HELPER
# ============================================================================
def async_route(f):
"""Decorator for async Flask routes"""
@wraps(f)
def wrapper(*args, **kwargs):
loop = asyncio.new_event_loop()
asyncio.set_event_loop(loop)
try:
return loop.run_until_complete(f(*args, **kwargs))
finally:
loop.close()
return wrapper
# ============================================================================
# MODEL LOADING
# ============================================================================
def load_model_once():
"""Load Qwen model"""
global model, tokenizer, model_loaded
if model_loaded:
return
with model_lock:
if model_loaded:
return
print("π¦ Loading Qwen 2.5-1.5B model...")
tokenizer = AutoTokenizer.from_pretrained(CONFIG["base_model"])
tokenizer.pad_token = tokenizer.eos_token
model = AutoModelForCausalLM.from_pretrained(
CONFIG["base_model"],
device_map="cpu",
dtype=torch.float32,
trust_remote_code=True,
low_cpu_mem_usage=True
)
if CONFIG["adapter_path"] and os.path.exists(CONFIG["adapter_path"]):
model = PeftModel.from_pretrained(model, CONFIG["adapter_path"])
model = model.merge_and_unload()
model.eval()
model_loaded = True
print("β Model loaded on CPU")
# ============================================================================
# WEATHER MCP
# ============================================================================
def get_weather_mcp():
"""Get or initialize Apify Weather MCP client"""
global weather_mcp
if weather_mcp is not None:
return weather_mcp
with weather_lock:
if weather_mcp is not None:
return weather_mcp
from apify_weather_mcp import ApifyWeatherMCP
print("π€οΈ Initializing Apify Weather MCP...")
weather_mcp = ApifyWeatherMCP(apify_token=CONFIG["apify_token"])
return weather_mcp
# ============================================================================
# CONFLUENCE RAG (NEW)
# ============================================================================
def get_confluence_rag():
"""Get or initialize Confluence RAG engine (singleton)"""
global confluence_rag
if confluence_rag is not None:
return confluence_rag
with confluence_lock:
if confluence_rag is not None:
return confluence_rag
from confluence_rag import ConfluenceRAG
print("π Initializing Confluence RAG...")
confluence_rag = ConfluenceRAG()
count = confluence_rag.collection.count()
print(f"β Confluence RAG ready β {count} chunks indexed")
if count == 0:
print("β οΈ WARNING: Knowledge base is empty! Run: python ingest.py")
return confluence_rag
# ============================================================================
# UTILITIES
# ============================================================================
def extract_json_object(text: str) -> Optional[str]:
"""Extract first valid JSON object"""
start = text.find("{")
if start == -1:
return None
depth = 0
for i in range(start, len(text)):
if text[i] == "{":
depth += 1
elif text[i] == "}":
depth -= 1
if depth == 0:
return text[start:i + 1]
return None
def is_valid_tool_call(payload: dict) -> bool:
"""Validate tool call format"""
if not isinstance(payload, dict):
return False
tool_call = payload.get("tool_call")
if not isinstance(tool_call, dict):
return False
name = tool_call.get("name")
args = tool_call.get("arguments", {})
return isinstance(name, str) and name.strip() and isinstance(args, dict)
# ============================================================================
# GENERATION
# ============================================================================
def build_chat_prompt(messages: List[Dict]) -> str:
"""Apply Qwen chat template"""
return tokenizer.apply_chat_template(
messages,
tokenize=False,
add_generation_prompt=True
)
def generate_text(prompt: str, max_tokens: int, temperature: float, do_sample: bool) -> str:
"""Generate text (non-streaming)"""
inputs = tokenizer(prompt, return_tensors="pt").to(model.device)
with torch.no_grad():
outputs = model.generate(
**inputs,
max_new_tokens=max_tokens,
temperature=temperature if do_sample else 1.0,
do_sample=do_sample,
top_p=CONFIG["top_p"] if do_sample else 1.0,
top_k=CONFIG["top_k"] if do_sample else 0,
repetition_penalty=CONFIG["repetition_penalty"],
pad_token_id=tokenizer.eos_token_id,
eos_token_id=tokenizer.eos_token_id
)
generated = outputs[0][inputs["input_ids"].shape[1]:]
text = tokenizer.decode(generated, skip_special_tokens=True)
return text.strip()
def generate_streaming(prompt: str) -> Generator[str, None, None]:
"""Generate text with streaming"""
inputs = tokenizer(prompt, return_tensors="pt").to(model.device)
streamer = TextIteratorStreamer(
tokenizer,
skip_prompt=True,
skip_special_tokens=True
)
generation_kwargs = dict(
**inputs,
max_new_tokens=CONFIG["max_length"],
temperature=CONFIG["temperature"],
do_sample=True,
top_p=CONFIG["top_p"],
top_k=CONFIG["top_k"],
repetition_penalty=CONFIG["repetition_penalty"],
pad_token_id=tokenizer.eos_token_id,
eos_token_id=tokenizer.eos_token_id,
streamer=streamer
)
thread = Thread(target=model.generate, kwargs=generation_kwargs)
thread.start()
for token in streamer:
yield token
# ============================================================================
# TOOL CALLING (UPDATED β now includes search_confluence)
# ============================================================================
def decide_tool_or_answer(conversation: List[Dict]) -> Tuple[str, Optional[Dict]]:
"""Model decides: call tool OR answer directly"""
system_prompt = """You are a helpful assistant with access to two tools.
TOOLS AVAILABLE:
1. get_weather(city) β Get current weather for a city
2. search_confluence(query) β Search the internal knowledge base for documentation, guides, integrations, how-to articles, and internal content
STRICT RULES β follow exactly:
- If user asks about weather, temperature, forecast β use get_weather
- If user asks about documentation, guides, how to do something, integrations, processes, or ANY internal knowledge β use search_confluence IMMEDIATELY. Do NOT answer from memory.
- NEVER say "I don't have information" without first trying search_confluence
- Tool calls MUST use this exact JSON format and nothing else:
For weather:
{"tool_call": {"name": "get_weather", "arguments": {"city": "CityName"}}}
For knowledge base:
{"tool_call": {"name": "search_confluence", "arguments": {"query": "search query here"}}}
When calling a tool: Output ONLY the JSON above, no extra text.
When answering directly (no tool needed): Provide a clear response, no JSON.
"""
messages = [{"role": "system", "content": system_prompt}] + conversation
prompt = build_chat_prompt(messages)
raw_output = generate_text(
prompt,
max_tokens=CONFIG["max_tool_tokens"],
temperature=CONFIG["tool_temperature"],
do_sample=False
)
print(f" π DEBUG model decision: {repr(raw_output)}") # helpful for debugging
json_str = extract_json_object(raw_output)
if json_str:
try:
payload = json.loads(json_str)
if is_valid_tool_call(payload):
return raw_output, payload["tool_call"]
except json.JSONDecodeError:
pass
return raw_output, None
def generate_acknowledgment(tool_name: str, tool_args: dict) -> str:
"""Generate natural acknowledgment before calling any tool"""
if tool_name == "get_weather":
city = tool_args.get("city", "")
context = f"checking weather for {city}"
else:
query = tool_args.get("query", "")
context = f"searching the knowledge base for: {query}"
system_prompt = f"""You are about to {context}.
Generate a brief, natural acknowledgment (1 sentence) before fetching data.
Examples:
- "Let me check that for you..."
- "I'll look that up right now..."
- "Searching the knowledge base for you..."
Be natural and conversational. One sentence only."""
messages = [{"role": "system", "content": system_prompt}]
prompt = build_chat_prompt(messages)
ack = generate_text(prompt, max_tokens=50, temperature=0.8, do_sample=True)
return ack.strip()
def call_tool(tool_name: str, tool_args: dict) -> str:
"""Execute any tool and return result as string"""
# ββ Weather ββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
if tool_name == "get_weather":
city = tool_args.get("city", "").strip()
if not city:
return "Error: city name is required"
mcp_client = get_weather_mcp()
tool_loop = asyncio.new_event_loop()
asyncio.set_event_loop(tool_loop)
try:
weather_data = tool_loop.run_until_complete(mcp_client.get_weather(city))
return str(weather_data)
finally:
tool_loop.close()
# ββ Confluence RAG (NEW) βββββββββββββββββββββββββββββββββββββββββββββββββ
elif tool_name == "search_confluence":
query = tool_args.get("query", "").strip()
if not query:
return "Error: query is required"
rag = get_confluence_rag()
results = rag.search(query, top_k=5)
context = rag.format_context(results)
print(f" π Found {len(results)} chunks for: '{query}'")
return context
else:
return f"Error: Unknown tool '{tool_name}'"
# ============================================================================
# MAIN CHAT LOGIC
# ============================================================================
def process_chat_streaming(user_message: str) -> Generator[str, None, None]:
"""
Process chat with streaming.
Runs tool calls synchronously, streams acknowledgment and final answer.
"""
conversation = [{"role": "user", "content": user_message}]
for round_num in range(CONFIG["max_tool_rounds"]):
raw_output, tool_call = decide_tool_or_answer(conversation)
if tool_call is None:
# Direct answer β stream it
for char in raw_output:
yield char
return
tool_name = tool_call["name"].strip()
tool_args = tool_call.get("arguments", {}) or {}
if tool_name not in ("get_weather", "search_confluence"):
error_msg = f"Sorry, I don't have a '{tool_name}' tool available."
for char in error_msg:
yield char
return
# Stream acknowledgment
print(f" π¬ Generating acknowledgment for {tool_name}...")
ack = generate_acknowledgment(tool_name, tool_args)
ack = ack.strip().strip('"').strip("'") # remove quotes model adds
for char in ack:
yield char
yield "\n\n[TOOL_EXECUTING]\n\n"
# Execute tool
print(f" π§ Calling tool: {tool_name} with args: {tool_args}")
try:
tool_result = call_tool(tool_name, tool_args)
except Exception as e:
print(f" β Tool error: {e}")
import traceback
traceback.print_exc()
error_msg = f"Sorry, I encountered an error: {str(e)}"
for char in error_msg:
yield char
return
# Add tool result to conversation
conversation.append({
"role": "assistant",
"content": f"[Called {tool_name} with {tool_args}]"
})
conversation.append({
"role": "user",
"content": f"[Tool result]:\n{tool_result}"
})
print(f" β Tool result received")
# Final streaming answer
print(f" π¬ Streaming final answer...")
system_prompt = """You are a helpful assistant.
Use the tool results to answer the user's question clearly and naturally.
If the tool returned source URLs or links, include them in your answer.
Don't mention tool names or technical details."""
messages = [{"role": "system", "content": system_prompt}] + conversation
prompt = build_chat_prompt(messages)
for token in generate_streaming(prompt):
yield token
def process_chat_non_streaming(user_message: str) -> str:
"""Process chat without streaming"""
conversation = [{"role": "user", "content": user_message}]
for round_num in range(CONFIG["max_tool_rounds"]):
raw_output, tool_call = decide_tool_or_answer(conversation)
if tool_call is None:
return raw_output
tool_name = tool_call["name"].strip()
tool_args = tool_call.get("arguments", {}) or {}
if tool_name not in ("get_weather", "search_confluence"):
return f"Sorry, I don't have a '{tool_name}' tool available."
print(f" π§ Calling tool: {tool_name} with args: {tool_args}")
try:
tool_result = call_tool(tool_name, tool_args)
except Exception as e:
return f"Sorry, I encountered an error: {str(e)}"
conversation.append({
"role": "assistant",
"content": f"[Called {tool_name} with {tool_args}]"
})
conversation.append({
"role": "user",
"content": f"[Tool result]:\n{tool_result}"
})
# Final answer
system_prompt = """You are a helpful assistant.
Use the tool results to answer the user's question clearly and naturally.
If the tool returned source URLs or links, include them in your answer."""
messages = [{"role": "system", "content": system_prompt}] + conversation
prompt = build_chat_prompt(messages)
return generate_text(prompt, CONFIG["max_length"], CONFIG["temperature"], True)
# ============================================================================
# FLASK ROUTES
# ============================================================================
@app.route('/')
def index():
"""Serve UI"""
load_model_once()
return render_template('index.html')
@app.route('/api/chat', methods=['POST'])
@async_route
async def api_chat():
"""Main chat endpoint with streaming support"""
try:
load_model_once()
data = request.json or {}
message = (data.get('message') or '').strip()
stream = bool(data.get('stream', False))
if not message:
return jsonify({'error': 'Message required'}), 400
if stream:
def generate():
try:
for token in process_chat_streaming(message):
yield f"data: {json.dumps({'token': token})}\n\n"
yield f"data: {json.dumps({'done': True})}\n\n"
except Exception as e:
print(f" β Streaming error: {e}")
import traceback
traceback.print_exc()
yield f"data: {json.dumps({'error': str(e)})}\n\n"
return Response(
stream_with_context(generate()),
mimetype='text/event-stream',
headers={
'Cache-Control': 'no-cache',
'X-Accel-Buffering': 'no'
}
)
else:
response = process_chat_non_streaming(message)
return jsonify({
'response': response,
'model': 'Qwen 2.5-1.5B'
})
except Exception as e:
print(f"β Chat error: {e}")
import traceback
traceback.print_exc()
return jsonify({'error': str(e)}), 500
@app.route('/api/status')
def api_status():
"""Server status"""
rag_count = 0
try:
rag = get_confluence_rag()
rag_count = rag.collection.count()
except Exception:
pass
return jsonify({
'model_loaded': model_loaded,
'model_name': CONFIG['base_model'],
'device': 'CPU (Mac Intel)',
'streaming_supported': True,
'confluence_chunks': rag_count # NEW: shows KB health
})
# NEW: Manual KB refresh endpoint
@app.route('/api/refresh-kb', methods=['POST'])
def refresh_kb():
"""Re-index Confluence knowledge base on demand"""
try:
import subprocess
result = subprocess.run(
["python", "ingest.py", "--reset"],
capture_output=True, text=True, timeout=300
)
return jsonify({
'success': result.returncode == 0,
'output': result.stdout[-2000:], # last 2000 chars
'error': result.stderr[-500:] if result.stderr else None
})
except Exception as e:
return jsonify({'success': False, 'error': str(e)}), 500
# ============================================================================
# MAIN
# ============================================================================
if __name__ == '__main__':
print("\n" + "=" * 70)
print("π€ AI Chat with Weather + Confluence RAG + Streaming")
print("=" * 70)
print(f"\nπ¦ Model: {CONFIG['base_model']}")
print(f"π» Device: CPU")
print(f"π€οΈ Weather: Apify MCP")
print(f"π Confluence: RAG (ChromaDB)")
print(f"π‘ Streaming: Enabled")
print("\n" + "=" * 70)
print("π Server at http://localhost:8000")
print("=" * 70 + "\n")
app.run(host='0.0.0.0', port=8000, debug=False, threaded=True)