[de9ba9]: / breast-cancer-rag-app / backend / biospecimen_rag.py

Download this file

184 lines (155 with data), 6.6 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
from flask import Flask, request, jsonify
from flask_cors import CORS
from flask_limiter import Limiter
from flask_limiter.util import get_remote_address
from notebookutils import mssparkutils
from pyspark.sql import SparkSession
from openai import AzureOpenAI
import time
import logging
from azure.monitor.opentelemetry import configure_azure_monitor
from tenacity import retry, wait_random_exponential, stop_after_attempt
from config import Config
app = Flask(__name__)
CORS(app, resources={r"/api/*": {"origins": "*"}})
limiter = Limiter(app=app, key_func=get_remote_address, default_limits=[Config.RATE_LIMIT])
# Initialize monitoring
configure_azure_monitor(
connection_string=Config.APP_INSIGHTS_KEY,
logging_level=logging.INFO
)
# Initialize Spark with optimized configuration
spark = SparkSession.builder \
.appName("BiospecimenAPI") \
.config("spark.sql.execution.arrow.pyspark.enabled", "true") \
.config("spark.sql.shuffle.partitions", "8") \
.getOrCreate()
class BiospecimenRAGSystem:
def __init__(self):
self.initialized = False
self.embeddings_cache = {}
self.kusto_token = mssparkutils.credentials.getToken(Config.KUSTO_URI)
self.client = self._init_openai_client()
self.initialize_system()
def _init_openai_client(self):
@retry(wait=wait_random_exponential(min=1, max=20), stop=stop_after_attempt(6))
def _create_client():
return AzureOpenAI(
azure_endpoint=Config.OPENAI_ENDPOINT,
api_key=Config.OPENAI_KEY,
api_version=Config.OPENAI_API_VERSION
)
return _create_client()
def initialize_system(self):
"""Check system readiness"""
try:
# Verify Kusto connection
test_df = spark.read \
.format("com.microsoft.kusto.spark.synapse.datasource") \
.option("kustoCluster", Config.KUSTO_URI) \
.option("kustoDatabase", Config.KUSTO_DB) \
.option("accessToken", self.kusto_token) \
.option("kustoQuery", f"{Config.KUSTO_TABLE} | count") \
.load()
if test_df.collect()[0][0] > 0:
self.initialized = True
app.logger.info("System initialized successfully")
else:
app.logger.error("No embeddings found in Eventhouse")
except Exception as e:
app.logger.error(f"Initialization failed: {str(e)}")
raise
@retry(wait=wait_random_exponential(min=1, max=20), stop=stop_after_attempt(6))
def generate_embeddings(self, text):
return self.client.embeddings.create(
input=[text.replace("\n", " ")],
model=Config.OPENAI_EMBEDDING_DEPLOYMENT
).data[0].embedding
def query_kusto(self, embedding, limit=3):
query = f"""
{Config.KUSTO_TABLE}
| extend similarity = series_cosine_similarity(dynamic({str(embedding)}), embedding)
| top {limit} by similarity desc
| project content=document_text,
metadata=pack(
'sample_type', Sample_Type,
'primary_site', Primary_Site,
'aliquot_id', Aliquot_ID
),
similarity
"""
return spark.read \
.format("com.microsoft.kusto.spark.synapse.datasource") \
.option("kustoCluster", Config.KUSTO_URI) \
.option("kustoDatabase", Config.KUSTO_DB) \
.option("accessToken", self.kusto_token) \
.option("kustoQuery", query) \
.load()
def generate_response(self, question, context):
prompt = f"""You are a biomedical research assistant analyzing biospecimen data.
Question: {question}
Relevant Records:
{context}
Provide:
1. A 1-2 sentence answer
2. Key characteristics of matching samples
3. Confidence assessment based on similarity scores"""
response = self.client.chat.completions.create(
model=Config.OPENAI_GPT4_DEPLOYMENT,
messages=[
{"role": "system", "content": prompt},
{"role": "user", "content": question}
],
temperature=0.2,
max_tokens=250
)
return response.choices[0].message.content
system = BiospecimenRAGSystem()
@app.route('/api/query', methods=['POST'])
@limiter.limit("10 per minute")
def handle_query():
try:
data = request.json
question = data.get('question', '')
if not question:
return jsonify({"error": "Missing question"}), 400
if not system.initialized:
return jsonify({"error": "System initializing", "status": 503})
start_time = time.time()
# Check cache first
cache_key = question.lower().strip()
if cache_key in system.embeddings_cache:
app.logger.info("Returning cached result")
return jsonify(system.embeddings_cache[cache_key])
embedding = system.generate_embeddings(question)
results = system.query_kusto(embedding).collect()
context = "\n".join([
f"Document {idx+1} (Similarity: {row['similarity']:.2f}): {row['content']}"
for idx, row in enumerate(results)
])
answer = system.generate_response(question, context)
response = {
"answer": answer,
"sources": [dict(row.asDict()) for row in results],
"processing_time": f"{time.time() - start_time:.2f}s",
"status": 200
}
# Cache result
system.embeddings_cache[cache_key] = response
return jsonify(response)
except Exception as e:
app.logger.error(f"Query failed: {str(e)}", exc_info=True)
return jsonify({"error": str(e), "status": 500}), 500
@app.route('/api/status', methods=['GET'])
def status():
try:
test_query = system.query_kusto(system.generate_embeddings("test"), 1)
return jsonify({
"status": "ready" if system.initialized else "initializing",
"records": test_query.count(),
"version": "1.0.0"
})
except Exception as e:
return jsonify({"status": "error", "message": str(e)}), 500
if __name__ == '__main__':
app.run(host='0.0.0.0', port=5000)