Spaces:
Runtime error
Runtime error
| import json | |
| from src.models import Message | |
| from src.config import logger | |
| from src.repositories import ConversationRepository, MessageRepository | |
| from ._file_service import FileService | |
| from ._meeting_service import MeetingService | |
| class BotActionService: | |
| def __init__(self): | |
| self.conversation_repository = ConversationRepository() | |
| self.message_repository = MessageRepository() | |
| self.file_service = FileService | |
| self.meeting_service = MeetingService | |
| self.functions_dictionary = { | |
| "get_relevant_information": { | |
| "service": self.file_service, | |
| "function": "semantic_search", | |
| }, | |
| "get_available_salesperson_meetings": { | |
| "service": self.meeting_service, | |
| "function": "get_available_salesperson_meetings", | |
| }, | |
| "get_available_slots": { | |
| "service": self.meeting_service, | |
| "function": "get_available_slots", | |
| }, | |
| "schedule_meeting": { | |
| "service": self.meeting_service, | |
| "function": "schedule_meeting", | |
| }, | |
| } | |
| async def __aenter__(self): | |
| return self | |
| async def __aexit__(self, *args): | |
| pass | |
| async def handle_user_message(self, message_content, conversation_id): | |
| # logger.info(f"User Query: {message_content}") | |
| conversation_object = await self.conversation_repository.get_by_id( | |
| conversation_id | |
| ) | |
| await self.message_repository.insert_one( | |
| Message( | |
| conversation=conversation_object, | |
| role="user", | |
| content=message_content, | |
| ) | |
| ) | |
| async def handle_ai_message(self, message_content, conversation_id): | |
| # logger.info(f"AI Response: {message_content}") | |
| conversation_object = await self.conversation_repository.get_by_id( | |
| conversation_id | |
| ) | |
| await self.message_repository.insert_one( | |
| Message( | |
| conversation=conversation_object, | |
| role="assistant", | |
| content=message_content, | |
| ) | |
| ) | |
| async def handle_ai_function_call(self, data, user_id): | |
| tool_name = data["name"] | |
| arguments = json.loads(data["arguments"]) | |
| logger.info(f"Function call: {tool_name} \nArguments: {arguments}") | |
| function_info = self.functions_dictionary.get(tool_name) | |
| if not function_info: | |
| raise AttributeError(f"Function {tool_name} not found in dictionary") | |
| service_class = function_info["service"] | |
| async with service_class() as service: | |
| logger.info(f"Function: {function_info['function']}, Arguments: {arguments}") | |
| func = getattr(service, function_info["function"], None) | |
| if not func: | |
| raise AttributeError( | |
| f"No such function {function_info['function']} in service" | |
| ) | |
| if "user_id" in arguments.keys(): | |
| arguments["user_id"] = user_id | |
| response = await func(**arguments) | |
| response = f"Context : {response}\n\n# Use this context to respond to the user query" | |
| return response | |