def add_synonyms(agent_name): if request.get_data(): request_data = json.loads((request.get_data()).decode()) AgentsService.get_instance().add_agent_synonyms(request_data["agent_name"], request_data["data"]) return response_template(200, "Synonyms successfully added for agent {0}".format(agent_name)) else: return response_template(400, "A body is mandatory inside the request")
def add_response(agent_name): """Add a response associated to an agent name and intent""" if request.get_data(): request_data = json.loads((request.get_data()).decode()) if (request_data.get("responses") and isinstance(request_data.get("responses"), list)): ResponsesService.get_instance().add_agent_responses(agent_name, request_data["responses"]) return response_template(200, "Responses was successfully added for agent {0}".format(agent_name)) else: return response_template(400, "Should Contains valid responses array object") else: return response_template(400, "A body is mandatory inside the request")
def add_context(agent_name, user_id): if request.get_data(): body = json.loads((request.get_data()).decode()) ContextService.get_instance().insert_user_context( agent_name, user_id, body.get('context_name'), body.get('context_value')) return response_template( 200, "Context successfully added for agent {0} and user_id {1}".format( agent_name, user_id)) else: return response_template(400, "A body is mandatory inside the request")
def register(): request_data = json.loads((request.get_data()).decode()) email = request_data.get("email") user = UserService.get_instance().find_user(email) if user: return response_template(409, "User Already Exist") else: first_name = request_data.get("first_name") last_name = request_data.get("last_name") password = request_data.get("password") UserService.get_instance().add_new_user(email, first_name, last_name, password) return response_template(201, "User added successfully")
def create_agent(): agent_name = "" if request.files: """Create agent from file in the case of file uploading""" file_key = list(request.files)[0] data = json.loads(request.files[file_key].read().decode()) agent_name = data.get("name") if agent_name is None or agent_name == "": return response_template(400, "Agent name field is mandatory") elif AgentsService.get_instance().agent_exist(agent_name): return response_template(400, "Already existing agent with name {0}".format(agent_name)) elif data.get("config") is None: return response_template(400, "Should Contains config field inside file data") AgentsService.get_instance().create_agent(agent_name, 0, data.get("config"), data.get("rasa_nlu_data"), data.get("fallback"), data.get("responses"), data.get("current_version")) elif request.get_data(): """Create agent with data passed directly inside query""" request_data = json.loads((request.get_data()).decode()) agent_name = request_data.get("name") if agent_name is None or agent_name == "": return response_template(400, "Agent name field is mandatory") elif AgentsService.get_instance().agent_exist(agent_name): return response_template(400, "Already existing agent with name {0}".format(agent_name)) elif request_data.get("config") is None: return response_template(400, "Should Contains config field inside body request") AgentsService.get_instance().create_agent(agent_name, 0, request_data.get("config"), request_data.get("rasa_nlu_data"), request_data.get("fallback"), request_data.get("responses"), request_data.get("current_version")) else: return response_template(400, "Shoulds Contains a valid body or file of new Agent") return response_template(201, "Agent {0} successfully created".format(agent_name))
def login(): request_data = json.loads((request.get_data()).decode()) email = request_data.get("email") password = request_data.get("password") if email is None or password is None: return response_template(401, "Missing Email or Password") check_user = UserService.get_instance().check_user(email, password) if check_user: expires = timedelta( minutes=int(os.environ.get("JWT_ACCESS_TOKEN_EXPIRES", 240))) access_token = create_access_token(identity=email, expires_delta=expires) return jsonify(access_token=access_token), 201 else: return response_template(401, "Wrong Email or Password")
def remove_context(agent_name, user_id, context_name): ContextService.get_instance().remove_user_context(agent_name, user_id, context_name) return response_template( 200, "Context '{0}' successfully deleted for agent {1} and user_id {2}". format(context_name, agent_name, user_id))
def set_specific_model(agent_name): if request.get_data(): request_data = json.loads((request.get_data()).decode()) if AgentsService.get_instance().load_agent(agent_name = agent_name, model_name = request_data.get("modelName")): return("Model " + request_data.get("modelName") + " loaded for bot " + agent_name) else: return("Can't load model " + request_data.get("modelName") + "for bot " + agent_name) else: return response_template(400, "A body is mandatory inside the request")
def parse(agent_name): """Parse a text to get nlu response with intent/entities assosciated""" if request.get_data(): text_query = (json.loads((request.get_data()).decode())).get("text") intent_query = (json.loads((request.get_data()).decode())).get("intent") user_id = (json.loads((request.get_data()).decode())).get("user_id") bot = ((AgentsService.get_instance().get_bots()).get(agent_name)) if bot is not None: if text_query: nlu_response = bot.handle_text(text_query, agent_name, user_id) if (request.args.get("test") is None or request.args.get("test").lower() != "true"): AgentsService.get_instance().store_user_input(agent_name, dict(nlu_response), user_id) elif intent_query: nlu_response = bot.handle_intent(intent_query, agent_name) else: return response_template(400, "A text field or an intent field are mandatory") return jsonify(nlu_response) else: return response_template(404, "Agent {0} not found".format(agent_name)) else: return response_template(400, "A body is mandatory inside the request")
def delete_agent(agent_name): AgentsService.get_instance().delete_agent(agent_name) return response_template(200, "Agent {0} successfully deleted".format(agent_name))
def delete_input(agent_name, id): AgentsService.get_instance().delete_agent_user_input(agent_name, id) return response_template(200, "User Input was successfully deleted for agent {0}".format(agent_name))
def remove_all_contexts(agent_name, user_id): ContextService.get_instance().remove_all_user_context(agent_name, user_id) return response_template( 200, "All Contexts are successfully deleted for agent {0} and user_id {1}". format(agent_name, user_id))
def delete_response(id): """Remove a response by id""" ResponsesService.get_instance().delete_response(id) return response_template(200, "Response with id {0} successfully deleted".format(id))