async def chat_history(sender: Text, current_user: User = Depends(auth.get_current_user)): return { "data": { "history": list(ChatHistory.fetch_chat_history(current_user.get_bot(), sender)) } }
async def get_story_from_intent(intent: str, current_user: User = Depends( auth.get_current_user)): """ This function returns the utterance or response that is mapped to a particular intent """ return { "data": mongo_processor.get_utterance_from_intent(intent, current_user.get_bot()) }
async def reload_model(background_tasks: BackgroundTasks, current_user: User = Depends(auth.get_current_user)): """ reload model with configuration in cache :param current_user: user id :return: Model reloaded! """ background_tasks.add_task(AgentProcessor.reload, current_user.get_bot()) return {"message": "Reloading Model!"}
async def edit_responses(utterance: str, id: str, request_data: TextData, current_user: User = Depends(auth.get_current_user)): """ update exising utterance :param utterance: utterance name :param id: utterance id :param request_data: new utterance value :param current_user: loggedin user id :return: Utterance updated! """ mongo_processor.edit_text_response(id, request_data.data, utterance, current_user.get_bot(), current_user.get_user()) return { "message": "Utterance updated!", }
async def remove_training_examples(request_data: TextData, current_user: User = Depends( auth.get_current_user)): """ delete existing training example :param request_data: trianing example id :param current_user: loggedin user id :return: Training Example removed! """ """ This function is used to delete a particular training example (question/sentence) from a list of examples for a particular intent """ mongo_processor.remove_document( TrainingExamples, request_data.data, current_user.get_bot(), current_user.get_user(), ) return {"message": "Training Example removed!"}
async def edit_training_examples( intent: str, id: str, request_data: TextData, current_user: User = Depends(auth.get_current_user), ): """ update existing training example :param intent: intent name :param id: training example id :param request_data: updated training example :param current_user: loggedin user id :return: "Training Example updated!" """ mongo_processor.edit_training_example(id, request_data.data, intent, current_user.get_bot(), current_user.get_user()) return {"message": "Training Example updated!"}
async def predict_intent(request_data: TextData, current_user: User = Depends(auth.get_current_user)): """ This function returns the predicted intent of the entered text by using the trained rasa model of the chatbot """ model = AgentProcessor.get_agent(current_user.get_bot()) response = await model.parse_message_using_nlu_interpreter( request_data.data) intent = response.get("intent").get("name") if response else None confidence = response.get("intent").get("confidence") if response else None return {"data": {"intent": intent, "confidence": confidence}}
async def chat_history(sender: Text, current_user: User = Depends(auth.get_current_user)): """ This function returns the chat history for a particular user of the chatbot """ return { "data": { "history": list(ChatHistory.fetch_chat_history(current_user.get_bot(), sender)) } }
async def add_training_examples( intent: str, request_data: ListData, current_user: User = Depends(auth.get_current_user), ): """ add training example :param intent: intent name :param request_data: training example :param current_user: loggedin user id :return: Training Example Id """ """ This is used to add a new training example (sentence/question) for a particular intent """ results = list( mongo_processor.add_training_example(request_data.data, intent, current_user.get_bot(), current_user.get_user())) return {"data": results}
async def deployment_history(current_user: User = Depends( auth.get_current_user)): """ This function is used to deploy the model of the currently trained chatbot """ return { "data": { "deployment_history": list( mongo_processor.get_model_deployment_history( bot=current_user.get_bot())) } }
async def get_training_examples(intent: str, current_user: User = Depends( auth.get_current_user)): """ This function is used to return the training examples (questions/sentences) which are used to train the chatbot, for a particular intent """ return { "data": list( mongo_processor.get_training_examples(intent, current_user.get_bot())) }
async def get_responses(utterance: str, current_user: User = Depends(auth.get_current_user)): """ fetch list of utterances against utterance name :param utterance: utterance name :param current_user: loggedin user id :return: list of utterances """ return { "data": list(mongo_processor.get_response(utterance, current_user.get_bot())) }
async def upload_Files( background_tasks: BackgroundTasks, nlu: UploadFile = File(...), domain: UploadFile = File(...), stories: UploadFile = File(...), config: UploadFile = File(...), overwrite: bool = True, current_user: User = Depends(auth.get_current_user), ): """Upload training data nlu.md, domain.yml, stories.md and config.yml files""" await mongo_processor.upload_and_save( await nlu.read(), await domain.read(), await stories.read(), await config.read(), current_user.get_bot(), current_user.get_user(), overwrite, ) background_tasks.add_task(start_training, current_user.get_bot(), current_user.get_user()) return {"message": "Data uploaded successfully!"}
async def download_data( background_tasks: BackgroundTasks, current_user: User = Depends(auth.get_current_user), ): """Download training data nlu.md, domain.yml, stories.md, config.yml files""" file = mongo_processor.download_files(current_user.get_bot()) response = FileResponse(file, filename=os.path.basename(file), background=background_tasks) response.headers[ "Content-Disposition"] = "attachment; filename=" + os.path.basename( file) return response
async def download_model( background_tasks: BackgroundTasks, current_user: User = Depends(auth.get_current_user), ): """Download latest trained model file""" try: model_path = AgentProcessor.get_latest_model(current_user.get_bot()) response = FileResponse(model_path, filename=os.path.basename(model_path), background=background_tasks) response.headers[ "Content-Disposition"] = "attachment; filename=" + os.path.basename( model_path) return response except Exception as e: raise AppException(str(e))
async def get_training_examples(intent: str, current_user: User = Depends( auth.get_current_user)): """ fetch all training examples against intent :param intent: intent name :param current_user: loggedin user id :return: list of training examples """ """ This function is used to return the training examples (questions/sentences) which are used to train the chatbot, for a particular intent """ return { "data": list( mongo_processor.get_training_examples(intent, current_user.get_bot())) }
async def conversation_time(current_user: User = Depends( auth.get_current_user)): return {"data": ChatHistory.conversation_time(current_user.get_bot())}
async def visitor_hit_fallback(current_user: User = Depends( auth.get_current_user)): return {"data": ChatHistory.visitor_hit_fallback(current_user.get_bot())}
async def conversation_time(current_user: User = Depends( auth.get_current_user)): """ This returns the duration of the chat that took place between the user and the chatbot """ return {"data": ChatHistory.conversation_time(current_user.get_bot())}
async def conversation_steps(current_user: User = Depends( auth.get_current_user)): """ This function returns the number of conversation steps that took place in the chat between the user and the chatbot """ return {"data": ChatHistory.conversation_steps(current_user.get_bot())}
async def visitor_hit_fallback(current_user: User = Depends( auth.get_current_user)): """ This function returns the number of times the bot hit a fallback (the bot admitting to not having a reply for a given text/query) for a given user """ return {"data": ChatHistory.visitor_hit_fallback(current_user.get_bot())}
async def get_stories(current_user: User = Depends(auth.get_current_user)): """ This returns the existing list of stories (conversation flows) of the bot """ return {"data": list(mongo_processor.get_stories(current_user.get_bot()))}
async def get_model_training_history( current_user: User = Depends(auth.get_current_user), ): training_history = list( ModelProcessor.get_training_history(current_user.get_bot())) return {"data": {"training_history": training_history}}
async def deploy(current_user: User = Depends(auth.get_current_user)): """ This function is used to deploy the model of the currently trained chatbot """ response = mongo_processor.deploy_model(bot=current_user.get_bot(), user=current_user.get_user()) return {"message": response}
async def get_config(current_user: User = Depends(auth.get_current_user), ): """get the model endpoint""" endpoint = mongo_processor.load_config(current_user.get_bot()) return {"data": {"endpoint": endpoint}}
async def get_intents(current_user: User = Depends(auth.get_current_user)): """ This function returns the list of existing intents of the bot """ return Response( data=mongo_processor.get_intents(current_user.get_bot())).dict()
async def get_endpoint(current_user: User = Depends(auth.get_current_user), ): """get the model endpoint""" endpoint = mongo_processor.get_endpoints(current_user.get_bot(), raise_exception=False) return {"data": {"endpoint": endpoint}}