async def add_intents(request_data: TextData, current_user: User = Depends(auth.get_current_user)): """ This function is used to add a new intent to the bot """ id = mongo_processor.add_intent(text=request_data.data.strip(), bot=current_user.get_bot(), user=current_user.get_user()) return {"message": "Intent added successfully!", "data": {"_id": id}}
async def chat(request_data: TextData, current_user: User = Depends(auth.get_current_user)): """ This function returns a bot response for a given text/query. It is basically used to test the chat functionality of the bot """ model = AgentProcessor.get_agent(current_user.get_bot()) response = await model.handle_text(request_data.data, sender_id=current_user.get_user()) return {"data": {"response": response[0]["text"] if response else None}}
async def set_endpoint( endpoint: Endpoint, current_user: User = Depends(auth.get_current_user), ): """get the bot config""" mongo_processor.add_endpoints(endpoint.dict(), current_user.get_bot(), current_user.get_user()) return {"message": "Endpoint saved successfully!"}
async def set_config( config: Config, current_user: User = Depends(auth.get_current_user), ): """set the bot config""" endpoint = mongo_processor.save_config(config.dict(), current_user.get_bot(), current_user.get_user()) return {"data": {"config": endpoint}}
async def train( background_tasks: BackgroundTasks, current_user: User = Depends(auth.get_current_user), ): """ This is used to train the chatbot """ ModelProcessor.is_training_inprogress(current_user.get_bot()) ModelProcessor.is_daily_training_limit_exceeded(current_user.get_bot()) background_tasks.add_task(start_training, current_user.get_bot(), current_user.get_user()) return {"message": "Model training started."}
async def set_endpoint(background_tasks: BackgroundTasks, endpoint: Endpoint, current_user: User = Depends(auth.get_current_user)): """get the bot config""" mongo_processor.add_endpoints(endpoint.dict(), current_user.get_bot(), current_user.get_user()) if endpoint.action_endpoint: background_tasks.add_task(AgentProcessor.reload, current_user.get_bot()) return {"message": "Endpoint saved successfully!"}
async def add_responses( request_data: TextData, utterance: str, current_user: User = Depends(auth.get_current_user), ): """ This function adds a response to the list of responses for a particular utterance of the bot """ id = mongo_processor.add_text_response(request_data.data, utterance, current_user.get_bot(), current_user.get_user()) return {"message": "Response added successfully!", "data": {"_id": id}}
async def remove_responses(request_data: TextData, current_user: User = Depends( auth.get_current_user)): """ This function removes the bot response from the response list for a particular utterance """ mongo_processor.remove_document(Responses, request_data.data, current_user.get_bot(), current_user.get_user()) return { "message": "Response removed successfully!", }
async def set_config_template(request_data: TextData, current_user: User = Depends( auth.get_current_user)): """ apply the config template :param request_data: config template name :param current_user: user id :return: Config Applied! """ mongo_processor.apply_config(request_data.data, current_user.get_bot(), current_user.get_user()) return {"message": "Config applied!"}
async def remove_training_examples(request_data: TextData, current_user: User = Depends( auth.get_current_user)): """ 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 successfully!"}
async def add_training_examples( intent: str, request_data: ListData, current_user: User = Depends(auth.get_current_user), ): """ 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 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 set_config( config: Config, current_user: User = Depends(auth.get_current_user), ): """ save the bot pipeline and policies configurations :param config: configuration :param current_user: user id :return: Config saved! """ mongo_processor.save_config(config.dict(), current_user.get_bot(), current_user.get_user()) return {"message": "Config saved!"}
async def get_templates(request_data: TextData, current_user: User = Depends(auth.get_current_user)): """ apply the use-case template :param request_data: use-case name :param current_user: user id :return: Data applied! :exception: Invalid template """ await mongo_processor.apply_template(request_data.data, bot=current_user.get_bot(), user=current_user.get_user()) return {"message": "Data applied!"}
async def delete_intent(intent: str, current_user: User = Depends(auth.get_current_user)): """ This function will be utilized to delete an intent and its dependencies with TrainingExamples and Stories """ intentName = mongo_processor.deleteIntentWithDependencies( intent, current_user.get_bot(), current_user.get_user()) return { "message": "Intent successfully deleted!", "data": { "IntentName": intentName } }
async def remove_responses(request_data: TextData, current_user: User = Depends( auth.get_current_user)): """ remove existing utterance :param request_data: utterance id :param current_user: loggedin user id :return: Utterance removed! """ mongo_processor.remove_document(Responses, request_data.data, current_user.get_bot(), current_user.get_user()) return { "message": "Utterance removed!", }
async def add_stories(story: StoryRequest, current_user: User = Depends(auth.get_current_user)): """ This function is used to add a story (conversational flow) to the chatbot """ return { "message": "Story added successfully", "data": { "_id": mongo_processor.add_story( story.name, story.get_events(), current_user.get_bot(), current_user.get_user(), ) }, }
async def add_responses( request_data: TextData, utterance: str, current_user: User = Depends(auth.get_current_user), ): """ add utterance :param request_data: utterance value to add :param utterance: utterance name :param current_user: loggedin user id :return: Utterance added! """ id = mongo_processor.add_text_response(request_data.data, utterance, current_user.get_bot(), current_user.get_user()) return {"message": "Utterance added!", "data": {"_id": id}}
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 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 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}