def _create_saved_reply(request): question = required_parameter(request, 'question') answer = required_parameter(request, 'answer') return { 'replyId': 'f9f1cf90-c3b1-11e7-91a1-9801a7ae6c69', 'answerId': 'a67678b6-c3b1-11e7-abaa-9801a7ae6c69' }
def _answer(request, number_of_items=1, offset=0, document_ids=None, max_number_of_answers=MAX_NUMBER_OF_ANSWERS): start_time = time.time() number_of_items = min(number_of_items, max(0, max_number_of_answers - offset)) user_token = required_parameter(request, 'token') question = required_parameter(request, 'question') source_type = optional_parameter(request, 'sourceType', 'all').lower() text = optional_parameter(request, 'text', None) speed_or_accuracy = optional_parameter(request, 'speedOrAccuracy', 'balanced').lower() saved_reply_threshold = optional_parameter( request, 'threshold', request['user_from_token'].saved_reply_threshold) document_threshold = optional_parameter( request, 'threshold', request['user_from_token'].document_threshold) if source_type not in {'document', 'saved_reply', 'all'}: raise UserException(ERROR_INVALID_SOURCE_TYPE) if speed_or_accuracy not in {'speed', 'accuracy', 'balanced', 'total'}: raise UserException(ERROR_INVALID_SPEED_OR_ACCURACY % speed_or_accuracy) else: if request['user_from_token'].plan == "pro": speed_or_accuracy = "total" if text is not None and len(text) > MAX_SIZE_INLINE_TEXT: raise UserException(ERROR_MAX_SIZE_INLINE_TEXT % (MAX_SIZE_INLINE_TEXT, len(text))) results = [] if source_type != 'document': results.extend( Responder.get_answers_from_similar_questions( user_token, question, source_type, document_ids, saved_reply_threshold)) results = sorted(results, key=lambda x: x['confidence'], reverse=True) if (source_type == 'document' or source_type == 'all') and len(results) < number_of_items: results.extend( Responder.get_answers_from_documents( user_token, question, document_ids, offset, number_of_items - len(results), text, document_threshold, speed_or_accuracy)) results = results[offset:offset + number_of_items] automatic = False if len(results) > 0: automatic = results[0]['sourceType'] == 'saved_reply' or results[0][ 'sourceType'] == 'annotation' duration = time.time() - start_time connect().submit(store_event, request['user_from_token'].user_id, question, results, 'API', len(results) > 0, duration, automatic) return {'items': results}
def _login(request): login = required_parameter(request, 'login') password = required_parameter(request, 'password') user = User.get('user_id', login) if user is None or not user.verify_password(password): raise UserException(INVALID_CREDENTIALS_TEXT) session_obj: Session = Session.create(user_id=user.user_id) request['session_id'] = session_obj.session_id return {'message': VALID_CREDENTIALS_TEXT, 'sessionId': session_obj.session_id, 'adminToken': user.admin_token}
def decorated(request, *args, **kwargs): token = optional_parameter(request, 'token', None) if token is None: if 'user' in request: request['user_from_token'] = request['user'] request['args']['token'] = request['user'].token return wrapped(request, *args, **kwargs) else: required_parameter(request, 'token') user = User.get('token', token) if user is not None: request['user_from_token'] = user return wrapped(request, *args, **kwargs) else: raise UserException(INVALID_TOKEN % token)
def _answer(request, number_of_items=1, offset=0, document_ids=None): token = optional_parameter(request, 'token', None) text = optional_parameter(request, 'text', None) if token is None and 'session' not in request and 'admintoken' not in request[ 'args']: raise UserException(ERROR_REQUIRED_PARAMETER % 'userToken') question = required_parameter(request, 'question') if text is not None and len(text) > MAX_SIZE_INLINE_TEXT: raise UserException(ERROR_MAX_SIZE_INLINE_TEXT % (MAX_SIZE_INLINE_TEXT, len(text))) if document_ids: items = [ item for item in mock_data.answers if item['sourceId'] in document_ids ] else: items = mock_data.answers source_type = optional_parameter(request, 'sourceType', 'all').lower() if source_type == 'document' or source_type == 'saved_reply': items = [item for item in items if item['sourceType'] == source_type] elif source_type != 'all': raise UserException(ERROR_INVALID_SOURCE_TYPE) items = items[offset:offset + number_of_items] return {'items': items}
def _set_plan(request): plan = required_parameter(request, 'plan').lower() if plan not in AVAILABLE_PLANS: raise UserException(ERROR_INVALID_PLAN % (plan, str(AVAILABLE_PLANS))) request['user'].plan = plan request['user'].save() return {'plan': plan}
def _answer(request, number_of_items=1, offset=0): token = optional_parameter(request, 'token', None) if token is None and 'session' not in request and 'admintoken' not in request[ 'args']: raise UserException(ERROR_REQUIRED_PARAMETER % 'userToken') question = required_parameter(request, 'question') return {"items": []}
def _add_annotation(request, metadata=None): question = required_parameter(request, 'question') answer = required_parameter(request, 'answer') document_id = required_parameter(request, 'documentId') page = optional_parameter(request, 'page', None) start_offset = optional_parameter(request, 'startOffset', None) end_offset = optional_parameter(request, 'endOffset', None) if start_offset is None or end_offset is None: raise UserException(ERROR_ANNOTATION_MISSING_PARAMS) if metadata is None: metadata = {} metadata['startOffset'] = int(start_offset) metadata['endOffset'] = int(end_offset) return AnnotationStore.create_annotation(request['user'].token, question, answer, document_id, page, metadata)
def _set_default_threshold(request): threshold = required_parameter(request, 'threshold').upper() if threshold not in THRESHOLD_MAP['document']: raise UserException(ERROR_INVALID_THRESHOLD) request['user'].document_threshold = threshold request['user'].saved_reply_threshold = threshold request['user'].save() return {'threshold': threshold.lower()}
def _add_annotation(request, bounding_boxes=None, metadata=None): question = required_parameter(request, 'question') answer = required_parameter(request, 'answer') document_id = required_parameter(request, 'documentId') start_offset = optional_parameter(request, 'startOffset', None) end_offset = optional_parameter(request, 'endOffset', None) page = optional_parameter(request, 'page', None) if start_offset is not None: start_offset = int(start_offset) if end_offset is not None: end_offset = int(end_offset) if (bounding_boxes is None or page is None) and (start_offset is None or end_offset is None): raise UserException(ERROR_ANNOTATION_MISSING_PARAMS) return { 'annotationId': 'f32258e5-f6e2-12d1-feed-4823e4bf6b52', 'answerId': 'e42358e5-b6f2-13e2-feeb-5843eebf6d41' }
def _create_user(request): user_id = required_parameter(request, 'userId').lower() password = required_parameter(request, 'password') token = optional_parameter(request, 'token', None) admin_token = optional_parameter(request, 'adminToken', None) threshold = optional_parameter(request, 'threshold', None) terms_agreed = optional_parameter(request, 'termsAgreed', None) plan = optional_parameter(request, 'plan', None) try: user = create_user(user_id, password) except IntegrityError as e: raise UserException(e.args[1]) if token: user.token = token if admin_token: user.admin_token = admin_token if threshold: threshold = threshold.lower() if threshold not in THRESHOLD_MAP['document']: raise UserException(ERROR_INVALID_THRESHOLD) user.document_threshold = threshold user.saved_reply_threshold = threshold if terms_agreed: terms_agreed = terms_agreed.lower() if terms_agreed == 'true': user.terms_agreed = True elif terms_agreed == 'false': user.terms_agreed = False else: raise UserException(ERROR_INVALID_TERMS % terms_agreed) if plan: plan = plan.lower() if plan in AVAILABLE_PLANS: user.plan = plan else: raise UserException(ERROR_INVALID_PLAN % (plan, str(AVAILABLE_PLANS))) user.save() return {'username': user.user_id}
def _archive_inbox(request): inbox_id = required_parameter(request, 'inboxId') if not inbox_id.isnumeric(): raise UserException(ERROR_INBOX_DOES_NOT_EXIST % inbox_id) event = Event.get('id', int(inbox_id)) if event is None: raise UserException(ERROR_INBOX_DOES_NOT_EXIST % inbox_id) event.archived = True event.save() return {'inboxId': inbox_id}
def _upload_document(request): title = required_parameter(request, 'title') if 'text' in request['args']: document_content = request['args']['text'] elif 'file' in request.files: document_file = request.files.get('file') document_content = document_file.body.decode() else: raise UserException(ERROR_REQUIRED_PARAMETER % "text' or 'file") if 'documentid' in request['args'] and request['args']['documentid'] != '': document_id = request['args']['documentid'] else: document_id = hashlib.sha256( document_content.encode('utf-8')).hexdigest() return {'documentId': document_id}
def _upload_document(request): title = required_parameter(request, 'title') if 'text' in request['args']: document_content = request['args']['text'] elif 'file' in request.files: document_file = request.files.get('file') document_content = document_file.body.decode() else: raise UserException(ERROR_NUMERIC_REQUIRED % "text' or 'file") if 'documentid' in request['args']: document_id = request['args']['documentid'] else: document_id = hashlib.sha256( document_content.encode('utf-8')).hexdigest() if 'replace' not in request['args']: raise UserException(ERROR_DOCUMENT_ALREADY_EXISTS % document_id) raise UserException(ERROR_UPLOAD_FAILED)
def _upload_document(request): user_token = request['user'].token title = required_parameter(request, 'title') if 'text' in request['args']: document_content = request['args']['text'] document_type = 'text' elif 'file' in request.files: document_file = request.files.get('file') document_content = document_file.body.decode() document_type = 'file' else: raise UserException(ERROR_REQUIRED_PARAMETER % "text' or 'file") if 'documentid' in request['args'] and request['args']['documentid'] != '': document_id = request['args']['documentid'] else: document_id = sha256(document_content.encode('utf-8')).hexdigest() if 'origin' in request['args'] and request['args']['origin'] != '': origin = request['args']['origin'] else: origin = '' document_type = optional_parameter(request, 'type', document_type) replace = 'replace' in request['args'] and request['args']['replace'].lower() == 'true' DocumentStore.create_document(user_id=user_token, document_id=document_id, title=title, text=document_content, origin=origin, document_type=document_type, replace=replace, get_embedding=Responder.get_document_embeddings) return {'documentId': document_id}
def _delete_annotation(request): annotation_id = required_parameter(request, 'annotationId') return AnnotationStore.delete_annotation(request['user'].token, annotation_id)
def _edit_canonical_question(request): annotation_id = required_parameter(request, 'annotationId') question = required_parameter(request, 'question') return AnnotationStore.edit_canonical_question(request['user'].token, annotation_id, question)
def _edit_answer(request): answer_id = required_parameter(request, 'answerId') answer = required_parameter(request, 'answer') return AnnotationStore.edit_answer(request['user'].token, answer_id, answer)
def _add_paraphrase_question(request): annotation_id = required_parameter(request, 'annotationId') question = required_parameter(request, 'question') return AnnotationStore.add_paraphrase_question(request['user'].token, annotation_id, question)
def _delete_paraphrase_question(request): question_id = required_parameter(request, 'questionId') return AnnotationStore.delete_paraphrase_question(request['user'].token, question_id)
def _add_answer(request): annotation_id = required_parameter(request, 'annotationId') answer = required_parameter(request, 'answer') return AnnotationStore.add_answer(request['user'].token, annotation_id, answer)
def _edit_paraphrase_question(request): user_token = request['user'].token question_id = required_parameter(request, 'questionId') question = required_parameter(request, 'question') return AnnotationStore.edit_paraphrase_question(user_token, question_id, question)
def _add_paraphrase_question(request): user_token = request['user'].token reply_id = required_parameter(request, 'replyId') question = required_parameter(request, 'question') return AnnotationStore.add_paraphrase_question(user_token, reply_id, question)
def _edit_canonical_question(request): user_token = request['user'].token reply_id = required_parameter(request, 'replyId') question = required_parameter(request, 'question') AnnotationStore.edit_canonical_question(user_token, reply_id, question) return {'replyId': reply_id}
def _delete_saved_reply(request): user_token = request['user'].token reply_id = required_parameter(request, 'replyId') AnnotationStore.delete_annotation(user_token, reply_id) return {'replyId': reply_id}
def test_case_insensitive(): request = {'args': {'test': 'testing'}} test = required_parameter(request, 'TEST') assert test == 'testing'
def test_required_parameter(): request = {'args': {'test': 'testing'}} test = required_parameter(request, 'test') assert test == 'testing'
def _delete_answer(request): user_token = request['user'].token answer_id = required_parameter(request, 'answerId') return AnnotationStore.delete_answer(user_token, answer_id)
def test_missing_required_parameter(): with pytest.raises(UserException): request = {'args': {'test': 'testing'}} test = required_parameter(request, 'missing')
def _create_saved_reply(request): user_token = request['user'].token question = required_parameter(request, 'question') answer = required_parameter(request, 'answer') response = AnnotationStore.create_annotation(user_token, question, answer) return {'replyId': response['annotationId'], 'answerId': response['answerId']}