Skip to content

Commit cc871c6

Browse files
committed
Tell the API caller is the data was truncated
1 parent d3f5f35 commit cc871c6

File tree

3 files changed

+33
-17
lines changed

3 files changed

+33
-17
lines changed

assemblyline_ui/api/v4/file.py

Lines changed: 12 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,8 @@
1717
from assemblyline_ui.api.base import api_login, make_api_response, make_subapi_blueprint, stream_file_response
1818
from assemblyline_ui.config import ALLOW_ZIP_DOWNLOADS, ALLOW_RAW_DOWNLOADS, FILESTORE, STORAGE, config, \
1919
CLASSIFICATION as Classification, ARCHIVESTORE
20-
from assemblyline_ui.helper.ai import AiApiException, summarize_code_snippet as ai_code, summarized_al_submission
20+
from assemblyline_ui.helper.ai import APIException, EmptyAIResponse, \
21+
summarize_code_snippet as ai_code, summarized_al_submission
2122
from assemblyline_ui.helper.result import format_result
2223
from assemblyline_ui.helper.user import load_user_settings
2324
from assemblyline.datastore.collection import Index
@@ -325,7 +326,10 @@ def summarized_results(sha256, **kwargs):
325326
/api/v4/file/ai/123456...654321/
326327
327328
Result example:
328-
<AI summary of the AL results>
329+
{
330+
"content": <AI summary of the AL results>,
331+
"truncated": false
332+
}
329333
"""
330334
if not config.ui.ai.enabled:
331335
return make_api_response({}, "AI Support is disabled on this system.", 400)
@@ -341,7 +345,7 @@ def summarized_results(sha256, **kwargs):
341345
# TODO: Caching maybe?
342346
ai_summary = summarized_al_submission(data)
343347
return make_api_response(ai_summary)
344-
except AiApiException as e:
348+
except (APIException, EmptyAIResponse) as e:
345349
return make_api_response("", str(e), 400)
346350

347351

@@ -365,7 +369,10 @@ def summarize_code_snippet(sha256, **kwargs):
365369
/api/v4/file/code_summary/123456...654321/
366370
367371
Result example:
368-
<AI summary of the code snippet>
372+
{
373+
"content": <AI summary of the code snippet>,
374+
"truncated": false
375+
}
369376
"""
370377
if not config.ui.ai.enabled:
371378
return make_api_response({}, "AI Support is disabled on this system.", 400)
@@ -406,7 +413,7 @@ def summarize_code_snippet(sha256, **kwargs):
406413
# TODO: Caching maybe?
407414
ai_summary = ai_code(data)
408415
return make_api_response(ai_summary)
409-
except AiApiException as e:
416+
except (APIException, EmptyAIResponse) as e:
410417
return make_api_response("", str(e), 400)
411418
else:
412419
return make_api_response({}, "You are not allowed to view this file.", 403)

assemblyline_ui/api/v4/submission.py

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,15 @@
11
import time
2-
from assemblyline.datastore.collection import Index
3-
from assemblyline_core.dispatching.client import DispatchClient
4-
from assemblyline_ui.helper.ai import AiApiException, summarized_al_submission
52

63
from flask import request
74
from werkzeug.exceptions import BadRequest
85

96
from assemblyline.datastore.exceptions import MultiKeyError, SearchException
7+
from assemblyline.datastore.collection import Index
108
from assemblyline.odm.models.user import ROLES
9+
from assemblyline_core.dispatching.client import DispatchClient
1110
from assemblyline_ui.api.base import api_login, make_api_response, make_subapi_blueprint
1211
from assemblyline_ui.config import STORAGE, LOGGER, FILESTORE, config, CLASSIFICATION as Classification
12+
from assemblyline_ui.helper.ai import EmptyAIResponse, APIException, summarized_al_submission
1313
from assemblyline_ui.helper.result import cleanup_heuristic_sections, format_result
1414
from assemblyline_ui.helper.submission import get_or_create_summary
1515

@@ -500,7 +500,11 @@ def get_ai_summary(sid, **kwargs):
500500
None
501501
502502
Result example:
503-
< THE AI SUMMARY IN MARKDOWN FORMAT >
503+
{
504+
"content": < THE AI SUMMARY IN MARKDOWN FORMAT >,
505+
"truncated": false
506+
}
507+
504508
505509
"""
506510
if not config.ui.ai.enabled:
@@ -517,7 +521,7 @@ def get_ai_summary(sid, **kwargs):
517521
# TODO: Caching maybe?
518522
ai_summary = summarized_al_submission(data)
519523
return make_api_response(ai_summary)
520-
except AiApiException as e:
524+
except (APIException, EmptyAIResponse) as e:
521525
return make_api_response("", str(e), 400)
522526

523527

assemblyline_ui/helper/ai.py

Lines changed: 12 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,11 @@
66
from assemblyline_ui.config import config, LOGGER
77

88

9-
class AiApiException(Exception):
9+
class APIException(Exception):
10+
pass
11+
12+
13+
class EmptyAIResponse(Exception):
1014
pass
1115

1216

@@ -30,22 +34,23 @@ def _call_ai_backend(data, params: AIQueryParams, action):
3034
except Exception as e:
3135
message = f"An exception occured while trying to {action} with AI on server {config.ui.ai.chat_url}. [{e}]"
3236
LOGGER.warning(message)
33-
raise AiApiException(message)
37+
raise APIException(message)
3438

3539
if not resp.ok:
3640
msg_data = resp.json()
3741
msg = msg_data.get('error', {}).get('message', None) or msg_data
3842
message = f"The AI API denied the request to {action} with the following message: {msg}"
3943
LOGGER.warning(message)
40-
raise AiApiException(message)
44+
raise APIException(message)
4145

4246
# Get AI responses
43-
responses = resp.json().get('choices', [])
47+
responses = resp.json()['choices']
4448
if responses:
45-
content = responses[0].get('message', {}).get('content', None)
46-
return content or None
49+
content = responses[0]['message']['content']
50+
reason = responses[0]['finish_reason']
51+
return {'content': content, 'truncated': reason == 'length'}
4752

48-
return None
53+
raise EmptyAIResponse("There was no response returned by the AI")
4954

5055

5156
def detailed_al_submission(report):

0 commit comments

Comments
 (0)