Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -139,7 +139,7 @@ def event_content(response,
logging.getLogger("max_kb_error").error(f'{str(e)}:{traceback.format_exc()}')
all_text = 'Exception:' + str(e)
write_context(step, manage, 0, 0, all_text)
asker = manage.context.get('asker', None)
asker = manage.context.get('form_data', {}).get('asker', None)
post_response_handler.handler(chat_id, chat_record_id, paragraph_list, problem_text,
all_text, manage, step, padding_problem_text, client_id, reasoning_content='',
asker=asker)
Expand Down Expand Up @@ -307,7 +307,7 @@ def execute_block(self, message_list: List[BaseMessage],
else:
reasoning_content = reasoning_result.get('reasoning_content') + reasoning_result_end.get(
'reasoning_content')
asker = manage.context.get('asker', None)
asker = manage.context.get('form_data', {}).get('asker', None)
post_response_handler.handler(chat_id, chat_record_id, paragraph_list, problem_text,
content, manage, self, padding_problem_text, client_id,
reasoning_content=reasoning_content if reasoning_content_enable else '',
Expand All @@ -325,7 +325,7 @@ def execute_block(self, message_list: List[BaseMessage],
except Exception as e:
all_text = 'Exception:' + str(e)
write_context(self, manage, 0, 0, all_text)
asker = manage.context.get('asker', None)
asker = manage.context.get('form_data', {}).get('asker', None)
post_response_handler.handler(chat_id, chat_record_id, paragraph_list, problem_text,
all_text, manage, self, padding_problem_text, client_id, reasoning_content='',
asker=asker)
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The provided code has a couple of issues and can be optimized for better readability and performance. Here are the key points:

  1. Variable Naming: The variable asker is often used to store user input, but it doesn't seem to consistently relate to the context or form data as intended. Consider renaming it to something more descriptive.

  2. Code Duplication: Duplicate sections handling exceptions occur in two different methods (event_content and execute_block). This repetition can be removed by creating a separate function to handle exception processing.

  3. Simplification: The method names could be made more clear to indicate their purpose (e.g., handle_exception).

Here's an example of how you might refactor the code to address these points:

def log_and_handle_exception(logger, error):
    logger.error(f'{str(error)}:{traceback.format_exc()}')

def get_user_input(manage):
    return manage.context.get('form_data', {}).get('asker', None)

class ResponseHandler:
    @staticmethod
    def handler(chat_id, chat_record_id, paragraph_list, problem_text, content,
              manage, sender_class, padding_problem_text, client_id,
              reasoning_content='', asker=None):
        if not asker:
            asker = get_user_input(manage)
        
        # Handle response logic here

class QuestionProcessor:
    def __init__(self):
        pass

    def event_content(self, response, manage):
        all_text = f'Exception: {str(response.exception)}'
        logging.getLogger("max_kb_error").error(all_text)
        post_response_handler.handler(self.chat_id, self.chat_record_id, self.paragraph_list, self.problem_text,
                                      all_text, manage, self.current_step(), self.padding_problem_text, 
                                     self.client_id, 'No Reasoning Content Available',
                                     asker=self.asker())
    
    def execute_block(self, message_list: List[BaseMessage]):
        reasoning_result = ...
        reasoning_result_end = ...
        try:
            ...  # main execution block
        except Exception as e:
            all_text = f'Exception: {str(e)}'
            logging.getLogger("max_kb_error").error(all_text)
            post_response_handler.handler(self.chat_id, self.chat_record_id, self.paragraph_list, self.problem_text,
                                       all_text, manage, self, self.padding_problem_text, self.client_id, 
                                       'No Reasoning Content Available',
                                       asker=get_user_input(manage))

In this refactored version:

  • A separate utility function _log_and_handle_exception is created to simplify logging and exception handling.
  • Variable naming was improved using lowercase underscores for cleaner style and easier comprehension.
  • The response_content and execute_block methods now share common functionality through abstract methods defined within the class hierarchy. Adjustments may still be necessary depending on the actual implementation structure.

Expand Down