-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapp.py
More file actions
301 lines (258 loc) · 10.6 KB
/
Copy pathapp.py
File metadata and controls
301 lines (258 loc) · 10.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
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
from flask import Flask, request, jsonify
from flask_sqlalchemy import SQLAlchemy
from flask_cors import CORS
from flask_restx import Api, Resource, fields
import requests
import logging
import os
from datetime import datetime
from secrets_manager import get_service_secrets
# Configure logging
logging.basicConfig(level=logging.INFO,
format='%(asctime)s - %(levelname)s - %(message)s',
datefmt='%Y-%m-%d %H:%M:%S')
app = Flask(__name__)
CORS(app)
app.config['DEBUG'] = True
# Configure API
api = Api(app,
version='1.0',
title='Gnosis Query API',
description='API for searching and retrieving content chunks',
doc='/docs'
)
# Configure namespace
ns = api.namespace('api', description='Query operations')
secrets = get_service_secrets('gnosis-query')
C_PORT = int(secrets.get('PORT', 5000))
SQLALCHEMY_DATABASE_URI = (
f"mysql+pymysql://{secrets['MYSQL_USER']}:{secrets['MYSQL_PASSWORD_CONTENT']}"
f"@{secrets['MYSQL_HOST']}:{secrets['MYSQL_PORT']}/{secrets['MYSQL_DATABASE']}"
)
app.config['SQLALCHEMY_DATABASE_URI'] = SQLALCHEMY_DATABASE_URI
app.config['SQLALCHEMY_TRACK_MODIFICATIONS'] = False
API_KEY = secrets.get('API_KEY')
EMBEDDING_API_URL = secrets.get('EMBEDDING_API_URL')
db = SQLAlchemy(app)
# Define database models (same as content processor)
class Content(db.Model):
__tablename__ = 'content'
id = db.Column(db.Integer, primary_key=True, autoincrement=True)
user_id = db.Column(db.Integer, nullable=False)
file_name = db.Column(db.String(255), nullable=False)
file_type = db.Column(db.String(50), nullable=False)
upload_date = db.Column(db.DateTime, default=datetime.utcnow)
file_size = db.Column(db.Integer, nullable=False)
s3_key = db.Column(db.String(255))
chunk_count = db.Column(db.Integer, default=0)
custom_prompt = db.Column(db.Text)
# metadata
title = db.Column(db.String(255))
author = db.Column(db.String(255))
publication_date = db.Column(db.Date)
publisher = db.Column(db.String(255))
source_language = db.Column(db.String(255))
genre = db.Column(db.String(255))
topic = db.Column(db.Text)
class ContentChunk(db.Model):
__tablename__ = 'content_chunk'
id = db.Column(db.Integer, primary_key=True, autoincrement=True)
content_id = db.Column(db.Integer, db.ForeignKey('content.id'))
chunk_order = db.Column(db.Integer, nullable=False)
chunk_text = db.Column(db.Text, nullable=False)
embedding_id = db.Column(db.Integer)
# API Models
search_response = api.model('SearchResponse', {
'message': fields.String,
'results': fields.List(fields.Nested(api.model('SearchResult', {
'chunk_id': fields.Integer,
'content_id': fields.Integer,
'file_name': fields.String,
'text': fields.String,
'similarity_score': fields.Float
})))
})
content_response = api.model('ContentResponse', {
'id': fields.Integer,
'user_id': fields.Integer,
'file_name': fields.String,
'file_type': fields.String,
'upload_date': fields.String,
'file_size': fields.Integer,
's3_key': fields.String,
'chunk_count': fields.Integer,
'custom_prompt': fields.String,
'title': fields.String,
'author': fields.String,
'publication_date': fields.String,
'publisher': fields.String,
'source_language': fields.String,
'genre': fields.String,
'topic': fields.String
})
chunk_response = api.model('ChunkResponse', {
'text': fields.String,
'embedding_id': fields.Integer
})
@ns.route('/search')
class SearchResource(Resource):
@api.doc('search_similar_chunks',
params={'user_id': 'ID of the user',
'query': 'Text to search for',
'content_id': 'ID of specific content to search within',
'limit': 'Number of results to return (default: 5)'})
@api.marshal_with(search_response)
def get(self):
"""Search for similar chunks across a user's content"""
user_id = request.args.get('user_id')
query_text = request.args.get('query')
content_id = request.args.get('content_id', type=int)
limit = request.args.get('limit', default=5, type=int)
if not user_id or not query_text:
api.abort(400, "Missing required parameters: user_id and query")
try:
# Step 1: Get content IDs to search within
content_query = db.session.query(Content.id)\
.filter(Content.user_id == user_id)
if content_id:
# If content_id provided, only search within that content
content_query = content_query.filter(Content.id == content_id)
content_ids = content_query.all()
content_ids = [id[0] for id in content_ids]
if not content_ids:
return {
'message': 'No content found for this user',
'results': []
}, 200
# Step 2: Get all embedding IDs for the user's content
embedding_chunks = db.session.query(ContentChunk.id, ContentChunk.embedding_id)\
.filter(ContentChunk.content_id.in_(content_ids))\
.filter(ContentChunk.embedding_id.isnot(None))\
.all()
if not embedding_chunks:
return {
'message': 'No embeddings found for this user\'s content',
'results': []
}, 200
# Create mapping of embedding_id to chunk_id
embedding_to_chunk = {ec[1]: ec[0] for ec in embedding_chunks}
embedding_ids = list(embedding_to_chunk.keys())
# Step 3: Query embedding service for similar embeddings
headers = {'X-API-KEY': API_KEY}
correlation_id = request.headers.get('X-Correlation-ID')
if correlation_id:
headers['X-Correlation-ID'] = correlation_id
response = requests.post(
f'{EMBEDDING_API_URL}/api/embedding/similar',
headers=headers,
json={
'text': query_text,
'embedding_ids': embedding_ids,
'limit': limit
}
)
if response.status_code != 200:
api.abort(500, "Failed to get similar embeddings")
similar_embeddings = response.json()['similar_embeddings']
# Step 4: Get the corresponding chunks
similar_chunk_ids = [
embedding_to_chunk[emb['id']]
for emb in similar_embeddings
]
# Step 5: Get the chunk details
similar_chunks = db.session.query(
ContentChunk.id,
ContentChunk.chunk_text,
ContentChunk.content_id,
Content.file_name
)\
.join(Content)\
.filter(ContentChunk.id.in_(similar_chunk_ids))\
.all()
# Format results
results = [{
'chunk_id': chunk.id,
'content_id': chunk.content_id,
'file_name': chunk.file_name,
'text': chunk.chunk_text,
'similarity_score': next(
emb['similarity_score']
for emb in similar_embeddings
if embedding_to_chunk[emb['id']] == chunk.id
)
} for chunk in similar_chunks]
# Sort by similarity score
results.sort(key=lambda x: x['similarity_score'], reverse=True)
return {
'message': 'Search completed successfully',
'results': results
}, 200
except Exception as e:
logging.error(f"Error in search_similar_chunks: {str(e)}")
api.abort(500, "Internal server error")
@ns.route('/content/<int:content_id>')
class ContentResource(Resource):
@api.doc('get_content_by_id')
@api.marshal_with(content_response)
def get(self, content_id):
"""Get content by ID"""
try:
content = Content.query.get(content_id)
if content is None:
api.abort(404, "Content not found")
return {
'id': content.id,
'user_id': content.user_id,
'file_name': content.file_name,
'file_type': content.file_type,
'upload_date': content.upload_date.isoformat(),
'file_size': content.file_size,
's3_key': content.s3_key,
'chunk_count': content.chunk_count,
'custom_prompt': content.custom_prompt,
'title': content.title,
'author': content.author,
'publication_date': content.publication_date.isoformat() if content.publication_date else None,
'publisher': content.publisher,
'source_language': content.source_language,
'genre': content.genre,
'topic': content.topic
}, 200
except Exception as e:
logging.error(f"Error in get_content_by_id: {str(e)}")
api.abort(500, "Internal server error")
@ns.route('/chunk/<int:chunk_id>')
class ChunkResource(Resource):
@api.doc('get_chunk_text_by_id')
@api.marshal_with(chunk_response)
def get(self, chunk_id):
"""Get text and embedding_id of a chunk by ID"""
try:
chunk = ContentChunk.query.get(chunk_id)
if chunk is None:
api.abort(404, "Chunk not found")
return {
'text': chunk.chunk_text,
'embedding_id': chunk.embedding_id
}, 200
except Exception as e:
logging.error(f"Error in get_chunk_text_by_id: {str(e)}")
api.abort(500, "Internal server error")
@app.before_request
def log_request_info():
# Exempt the /docs endpoint from logging and API key checks
if request.path.startswith('/docs') or request.path.startswith('/swagger'):
return
logging.info(f"Headers: {request.headers}")
logging.info(f"Body: {request.get_data()}")
if 'X-API-KEY' not in request.headers:
logging.warning("No X-API-KEY header")
return jsonify({'error': 'No X-API-KEY'}), 401
x_api_key = request.headers.get('X-API-KEY')
if x_api_key != API_KEY:
logging.warning("Invalid X-API-KEY")
return jsonify({'error': 'Invalid X-API-KEY'}), 401
else:
return
if __name__ == '__main__':
app.run(debug=True, host='0.0.0.0', port=C_PORT)