-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathesom_simple_rag.py
More file actions
576 lines (481 loc) · 23.5 KB
/
Copy pathesom_simple_rag.py
File metadata and controls
576 lines (481 loc) · 23.5 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
"""
ESOM Finance RAG Chatbot Server
--------------------------------
This script creates a simple Flask server that implements a Retrieval Augmented
Generation (RAG) chatbot for economics and finance topics.
The server handles requests at the /api/chat endpoint, processes user queries,
retrieves relevant information, and generates responses using a combination of
retrieved content and LLM capabilities.
"""
import os
import json
import time
from datetime import datetime
import logging
import argparse
import random
from typing import List, Dict, Any, Optional
# Third-party imports
import numpy as np
import torch
from flask import Flask, request, jsonify
from flask_cors import CORS
from sentence_transformers import SentenceTransformer
from langchain.embeddings import HuggingFaceEmbeddings
from langchain.text_splitter import RecursiveCharacterTextSplitter
# Set up logging
logging.basicConfig(
level=logging.INFO,
format='%(asctime)s - %(name)s - %(levelname)s - %(message)s'
)
logger = logging.getLogger(__name__)
# Initialize Flask app
app = Flask(__name__)
CORS(app) # Enable CORS for all routes
# Default path for data
DEFAULT_DATA_PATH = "econ_finance_data/processed"
# Global variables for storing data and models
embeddings_model = None
document_store = []
document_embeddings = []
glossary = {}
market_data = {}
current_trends = {}
class SimpleRAG:
"""Simple Retrieval-Augmented Generation system for economics and finance data."""
def __init__(self, data_path: str = DEFAULT_DATA_PATH):
"""Initialize the RAG system with data from the specified path."""
self.data_path = data_path
self.embeddings_model = None
self.document_store = []
self.document_embeddings = None
self.glossary = {}
self.market_data = {}
self.current_trends = {}
# Load models and data
self._load_embedding_model()
self._load_documents()
self._load_glossary()
self._load_market_data()
logger.info(f"Initialized RAG system with {len(self.document_store)} documents")
def _load_embedding_model(self):
"""Load the sentence embedding model."""
logger.info("Loading embedding model...")
try:
self.embeddings_model = SentenceTransformer('all-MiniLM-L6-v2')
logger.info("Embedding model loaded successfully")
except Exception as e:
logger.error(f"Error loading embedding model: {e}")
# Use a simpler model as fallback
try:
self.embeddings_model = SentenceTransformer('paraphrase-MiniLM-L3-v2')
logger.info("Fallback embedding model loaded")
except Exception as e2:
logger.error(f"Failed to load fallback model: {e2}")
raise
def _load_documents(self):
"""Load documents from JSONL files and create embeddings."""
logger.info(f"Loading documents from {self.data_path}...")
# Create data directory if it doesn't exist
os.makedirs(self.data_path, exist_ok=True)
documents = []
# List of document types to look for
document_types = [
"research_papers", "textbook_excerpts",
"financial_news", "economic_indicators"
]
for doc_type in document_types:
file_path = os.path.join(self.data_path, f"{doc_type}.jsonl")
# Create dummy data if file doesn't exist
if not os.path.exists(file_path):
self._create_dummy_data(file_path, doc_type)
try:
with open(file_path, 'r', encoding='utf-8') as f:
for line in f:
doc = json.loads(line)
# Add source information
doc["source"] = doc_type
documents.append(doc)
logger.info(f"Loaded {doc_type} documents")
except Exception as e:
logger.error(f"Error loading {doc_type} documents: {e}")
# Store the documents
self.document_store = documents
# Create embeddings for documents
if documents and self.embeddings_model:
logger.info("Creating document embeddings...")
texts = [doc.get("content", "") for doc in documents]
self.document_embeddings = self.embeddings_model.encode(texts)
logger.info(f"Created embeddings for {len(texts)} documents")
def _create_dummy_data(self, file_path: str, doc_type: str):
"""Create dummy data files for testing purposes."""
logger.info(f"Creating dummy {doc_type} data")
# Define dummy data based on document type
dummy_docs = []
if doc_type == "research_papers":
dummy_docs = [
{"title": "Impact of Monetary Policy on Inflation",
"content": "Monetary policy has a significant impact on inflation rates. Central banks use various tools like interest rates to control inflation. In India, the Reserve Bank of India implements monetary policy to maintain price stability."},
{"title": "Economic Growth Patterns in Developing Nations",
"content": "Developing nations often show different growth patterns compared to developed economies. Factors such as infrastructure, education, and governance play crucial roles in determining these patterns."}
]
elif doc_type == "textbook_excerpts":
dummy_docs = [
{"title": "Principles of Macroeconomics",
"content": "Macroeconomics studies the behavior of the economy as a whole, including inflation, GDP, and unemployment. These factors are interconnected and influence economic policy decisions."},
{"title": "Introduction to Financial Markets",
"content": "Financial markets are mechanisms that allow people to buy and sell (trade) financial securities, commodities, and other fungible items. They are crucial for allocating resources in the economy."}
]
elif doc_type == "financial_news":
dummy_docs = [
{"title": "Stock Market Reaches New High",
"date": "2025-05-29",
"content": "The stock market reached a new record high today as investors responded positively to recent economic data showing strong growth and controlled inflation."},
{"title": "Central Bank Announces Interest Rate Decision",
"date": "2025-05-28",
"content": "The central bank announced today that it will maintain current interest rates, citing balanced risks to economic growth and inflation targets."}
]
elif doc_type == "economic_indicators":
dummy_docs = [
{"indicator": "GDP Growth",
"value": "4.2%",
"period": "Q1 2025",
"content": "India's GDP grew by 4.2% in the first quarter of 2025, showing resilience despite global economic challenges."},
{"indicator": "Inflation Rate",
"value": "3.8%",
"period": "April 2025",
"content": "Consumer price inflation stood at 3.8% in April 2025, remaining within the Reserve Bank's target range of 2-6%."}
]
# Save dummy data to file
with open(file_path, 'w', encoding='utf-8') as f:
for doc in dummy_docs:
f.write(json.dumps(doc) + "\n")
logger.info(f"Created dummy {doc_type} data with {len(dummy_docs)} entries")
def _load_glossary(self):
"""Load financial and economic terms glossary."""
file_path = os.path.join(self.data_path, "finance_glossary.jsonl")
# Create dummy glossary if file doesn't exist
if not os.path.exists(file_path):
self._create_dummy_glossary(file_path)
try:
with open(file_path, 'r', encoding='utf-8') as f:
for line in f:
item = json.loads(line)
term = item.get("term", "").lower()
if term:
self.glossary[term] = item.get("definition", "")
logger.info(f"Loaded glossary with {len(self.glossary)} terms")
except Exception as e:
logger.error(f"Error loading glossary: {e}")
def _create_dummy_glossary(self, file_path: str):
"""Create a dummy financial terms glossary."""
dummy_terms = [
{"term": "Inflation", "definition": "A general increase in prices and fall in the purchasing value of money."},
{"term": "GDP", "definition": "Gross Domestic Product - the total value of goods produced and services provided in a country during one year."},
{"term": "Bull Market", "definition": "A market in which share prices are rising, encouraging buying."},
{"term": "Bear Market", "definition": "A market in which prices are falling, encouraging selling."},
{"term": "Fiscal Policy", "definition": "Government policy relating to taxation, borrowing, and public spending."}
]
with open(file_path, 'w', encoding='utf-8') as f:
for term in dummy_terms:
f.write(json.dumps(term) + "\n")
logger.info(f"Created dummy glossary with {len(dummy_terms)} terms")
def _load_market_data(self):
"""Load market data and current economic trends."""
file_path = os.path.join(self.data_path, "market_data.json")
# Create dummy market data if file doesn't exist
if not os.path.exists(file_path):
self._create_dummy_market_data(file_path)
try:
with open(file_path, 'r', encoding='utf-8') as f:
data = json.load(f)
self.market_data = data.get("market_data", {})
self.current_trends = data.get("trends", {})
logger.info("Loaded market data and trends")
except Exception as e:
logger.error(f"Error loading market data: {e}")
def _create_dummy_market_data(self, file_path: str):
"""Create dummy market data for testing."""
dummy_data = {
"market_data": {
"indices": {
"SENSEX": "66,782.43 (+0.4%)",
"NIFTY": "20,189.65 (+0.3%)",
"S&P500": "5,432.67 (+0.2%)"
},
"currencies": {
"USD/INR": "74.23 (-0.1%)",
"EUR/INR": "87.65 (+0.2%)",
"GBP/INR": "102.36 (+0.3%)"
},
"commodities": {
"Gold": "₹62,450/10g (+0.5%)",
"Crude Oil": "$82.75/barrel (-0.3%)"
}
},
"trends": {
"inflation_rate": "3.8% (April 2025)",
"repo_rate": "5.25%",
"gdp_growth": "4.2% (Q1 2025)",
"unemployment": "6.8% (April 2025)"
}
}
os.makedirs(os.path.dirname(file_path), exist_ok=True)
with open(file_path, 'w', encoding='utf-8') as f:
json.dump(dummy_data, f, indent=2)
logger.info("Created dummy market data")
def retrieve_relevant_documents(self, query: str, top_k: int = 3) -> List[Dict]:
"""Retrieve the most relevant documents for a given query."""
if not self.document_embeddings is not None or not self.document_store:
logger.warning("No document embeddings available for retrieval")
return []
# Create query embedding
query_embedding = self.embeddings_model.encode([query])[0]
# Calculate similarities
similarities = np.dot(self.document_embeddings, query_embedding)
top_indices = np.argsort(similarities)[-top_k:][::-1] # Get indices of top-k most similar docs
# Return the top-k documents
relevant_docs = [self.document_store[i] for i in top_indices]
# Add similarity scores for debugging
for i, doc in enumerate(relevant_docs):
doc["similarity_score"] = float(similarities[top_indices[i]])
return relevant_docs
def check_glossary_terms(self, query: str) -> Dict:
"""Check if the query contains any terms from our glossary."""
query_lower = query.lower()
found_terms = {}
# Check for exact terms (prioritize longer matches)
sorted_terms = sorted(self.glossary.keys(), key=len, reverse=True)
for term in sorted_terms:
if term in query_lower:
found_terms[term] = self.glossary[term]
if len(found_terms) >= 2: # Limit to prevent overwhelming responses
break
return found_terms
def get_relevant_market_data(self, query: str) -> Dict:
"""Extract relevant market data based on the query."""
result = {}
query_lower = query.lower()
# Check for economic indicators
if any(term in query_lower for term in ["inflation", "cpi", "price index"]):
result["inflation"] = self.current_trends.get("inflation_rate", "Data not available")
if any(term in query_lower for term in ["interest rate", "repo", "repo rate"]):
result["repo_rate"] = self.current_trends.get("repo_rate", "Data not available")
if any(term in query_lower for term in ["gdp", "growth", "economic growth"]):
result["gdp_growth"] = self.current_trends.get("gdp_growth", "Data not available")
# Check for market indices
if any(term in query_lower for term in ["sensex", "stock market", "share market", "bse"]):
result["SENSEX"] = self.market_data.get("indices", {}).get("SENSEX", "Data not available")
if any(term in query_lower for term in ["nifty", "nse"]):
result["NIFTY"] = self.market_data.get("indices", {}).get("NIFTY", "Data not available")
# Check for commodities
if "gold" in query_lower:
result["Gold"] = self.market_data.get("commodities", {}).get("Gold", "Data not available")
if any(term in query_lower for term in ["oil", "crude", "petroleum"]):
result["Crude Oil"] = self.market_data.get("commodities", {}).get("Crude Oil", "Data not available")
return result
def generate_response(self, query: str, chat_history: List = None) -> Dict:
"""Generate a response to the user query using retrieval and synthesis."""
if chat_history is None:
chat_history = []
# Initialize response data
response_data = {
"answer": "",
"sources": [],
"glossary_terms": {},
"market_data": {}
}
try:
# Step 1: Retrieve relevant documents
relevant_docs = self.retrieve_relevant_documents(query, top_k=3)
# Step 2: Check glossary for relevant terms
glossary_terms = self.check_glossary_terms(query)
# Step 3: Get relevant market data
market_data = self.get_relevant_market_data(query)
# Step 4: Generate answer based on retrieved information
answer = self._synthesize_answer(query, relevant_docs, glossary_terms, market_data, chat_history)
# Step 5: Format sources information
sources = []
for doc in relevant_docs:
source_info = {
"type": doc.get("source", "unknown"),
"title": doc.get("title", "Untitled")
}
if "date" in doc:
source_info["date"] = doc["date"]
sources.append(source_info)
# Step 6: Assemble the response
response_data["answer"] = answer
response_data["sources"] = sources
response_data["glossary_terms"] = glossary_terms
response_data["market_data"] = market_data
return response_data
except Exception as e:
logger.error(f"Error generating response: {e}")
return {
"answer": "I'm sorry, I encountered an error while processing your query. Please try again.",
"sources": [],
"glossary_terms": {},
"market_data": {}
}
def _synthesize_answer(self, query: str, docs: List[Dict], glossary_terms: Dict,
market_data: Dict, chat_history: List) -> str:
"""Synthesize an answer from retrieved information."""
# This is a simple version that doesn't use an actual LLM
# In a production system, you would use an actual LLM here
if not docs:
# No relevant documents found
if glossary_terms:
# But we found glossary terms
terms_list = list(glossary_terms.keys())
if len(terms_list) == 1:
return f"I see you're asking about '{terms_list[0]}'. {glossary_terms[terms_list[0]]}"
else:
definitions = [f"'{term}': {glossary_terms[term]}" for term in terms_list]
return f"I found these relevant terms in your query:\n\n" + "\n\n".join(definitions)
if market_data:
# We have market data
data_points = [f"{key}: {value}" for key, value in market_data.items()]
return f"Here's the latest data related to your query:\n\n" + "\n".join(data_points)
# Generic response for no information
return "I don't have specific information on that topic yet. Please try asking about economics, finance, or market data that might be in my knowledge base."
# We have relevant documents
answer_parts = []
# Use content from most relevant document
main_content = docs[0].get("content", "")
if len(main_content) > 300:
main_content = main_content[:300] + "..."
answer_parts.append(main_content)
# Add glossary definitions if any
if glossary_terms:
terms_text = "\n\nRelated terms:\n"
terms_text += "\n".join([f"- {term.capitalize()}: {definition}" for term, definition in glossary_terms.items()])
answer_parts.append(terms_text)
# Add market data if any
if market_data:
market_text = "\n\nRelevant market data:\n"
market_text += "\n".join([f"- {key}: {value}" for key, value in market_data.items()])
answer_parts.append(market_text)
return " ".join(answer_parts)
# Initialize the RAG system
rag_system = None
def initialize_rag():
"""Initialize the RAG system with the specified data path."""
global rag_system
try:
rag_system = SimpleRAG()
return True
except Exception as e:
logger.error(f"Error initializing RAG: {e}")
return False
@app.route('/', methods=['GET'])
def home():
"""Homepage route that displays a simple test page"""
return """
<!DOCTYPE html>
<html>
<head>
<title>ESOM Finance API Server</title>
<style>
body { font-family: Arial, sans-serif; margin: 40px; line-height: 1.6; }
.container { max-width: 800px; margin: 0 auto; }
h1 { color: #333; }
.status { padding: 20px; background-color: #d4edda; border-radius: 5px; }
.endpoint { background-color: #f8f9fa; padding: 15px; border-radius: 5px; margin-top: 20px; }
code { background-color: #eee; padding: 2px 5px; border-radius: 3px; }
</style>
</head>
<body>
<div class="container">
<h1>ESOM Finance API Server</h1>
<div class="status">
<h2>✅ Server is running correctly!</h2>
<p>The API server is operational and ready to process requests.</p>
</div>
<div class="endpoint">
<h3>Available Endpoint:</h3>
<p><code>POST /api/chat</code> - Send economics & finance questions</p>
<p>This endpoint accepts JSON with the format:</p>
<pre><code>{
"message": "Your question about economics or finance",
"user_id": "optional_user_id",
"chat_history": []
}</code></pre>
</div>
<div class="endpoint">
<h3>Testing Your Chatbot:</h3>
<p>To use the chatbot, add the JavaScript file to your HTML and open your website.</p>
<p>The chatbot should connect to this server automatically.</p>
</div>
</div>
</body>
</html>
"""
@app.route('/api/health', methods=['GET'])
def health():
"""Health check endpoint that returns a 200 status"""
return jsonify({
"status": "ok",
"message": "ESOM Finance API is running"
})
@app.route('/api/chat', methods=['POST'])
def chat():
"""Endpoint for handling chat requests"""
# Check if RAG system is initialized
global rag_system
if rag_system is None:
if not initialize_rag():
return jsonify({
"error": "RAG system initialization failed"
}), 500
# Parse request data
data = request.json
if not data:
return jsonify({
"error": "No data provided"
}), 400
message = data.get('message')
user_id = data.get('user_id', 'anonymous')
chat_history = data.get('chat_history', [])
if not message:
return jsonify({
"error": "No message provided"
}), 400
try:
# Generate response
start_time = time.time()
response_data = rag_system.generate_response(message, chat_history)
# Log query for analytics
logger.info(f"Query from {user_id}: '{message}' - Processed in {time.time() - start_time:.2f}s")
# Return the response
return jsonify({
"response": response_data["answer"],
"sources": response_data["sources"],
"glossary_terms": response_data["glossary_terms"],
"market_data": response_data["market_data"],
"timestamp": datetime.now().isoformat()
})
except Exception as e:
logger.error(f"Error processing chat request: {e}")
return jsonify({
"error": "An error occurred while processing your request",
"message": str(e)
}), 500
if __name__ == '__main__':
# Command-line arguments
parser = argparse.ArgumentParser(description='ESOM Finance RAG Chatbot Server')
parser.add_argument('--host', type=str, default='0.0.0.0',
help='Host to run the server on')
parser.add_argument('--port', type=int, default=5001,
help='Port to run the server on')
parser.add_argument('--debug', action='store_true',
help='Run in debug mode')
parser.add_argument('--data-path', type=str, default=DEFAULT_DATA_PATH,
help='Path to data directory')
args = parser.parse_args()
# Initialize RAG system
if initialize_rag():
logger.info(f"Starting server on {args.host}:{args.port}")
app.run(host=args.host, port=args.port, debug=args.debug)
else:
logger.error("Failed to initialize RAG system. Exiting.")