Skip to content
Merged
Changes from 1 commit
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
4 changes: 3 additions & 1 deletion apps/application/flow/workflow_manage.py
Original file line number Diff line number Diff line change
Expand Up @@ -337,13 +337,15 @@ def run_block(self, language='zh'):
answer_text = '\n\n'.join(
'\n\n'.join([a.get('content') for a in answer]) for answer in
answer_text_list)
answer_list = reduce(lambda pre, _n: [*pre, *_n], answer_text_list, [])
self.work_flow_post_handler.handler(self.params['chat_id'], self.params['chat_record_id'],
answer_text,
self)
return self.base_to_response.to_block_response(self.params['chat_id'],
self.params['chat_record_id'], answer_text, True
, message_tokens, answer_tokens,
_status=status.HTTP_200_OK if self.status == 200 else status.HTTP_500_INTERNAL_SERVER_ERROR)
_status=status.HTTP_200_OK if self.status == 200 else status.HTTP_500_INTERNAL_SERVER_ERROR,
other_params={'answer_list': answer_list})

def run_stream(self, current_node, node_result_future, language='zh'):
"""
Copy link
Copy Markdown
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 appears to be part of a language model's execution logic where it processes answers and streams responses. Here are some observations and suggestions:

  1. Empty String Check: The answer_text variable is initialized as an empty string, but this might not be intended if you expect it to hold the final response content.

  2. Reduce Function Overhead: Using the reduce function with a lambda expression seems unnecessary here since join() can be more efficient for concatenating lists into strings.

  3. Multiple Join Calls: You have multiple calls to join(), which could lead to redundancy and performance issues. Consider combining these joins into a single call.

  4. Default Argument Types: The type hints for parameters like _status suggest they should be expected to accept integers rather than enums/types that match Django's HTTP status codes (e.g., http.HttpResponse). Ensure correct typing.

Here’s an optimized version of the code based on these points:

@@ -337,26 +337,28 @@ def run_block(self, language='zh'):
         # Combine all answers into a single list without using reduce
         answer_texts = [a.get('content') for sublist in answer_text_list for a in sublist]
         
         # Join all parts into one continuous string separated by two newlines
         answer_text = '\n\n'.join(answer_texts)
         
         # Initialize other required variables (assumed present elsewhere in the code)
         self.work_flow_post_handler.handler(
             params['chat_id'],
             params['chat_record_id'],
-            answer_text,
+            answer_texts,
             self
         )
        
        return self.base_to_response.to_block_response(
            params['chat_id'],
            params['chat_record_id'],
            answer_text,
            True,
            message_tokens,
            answer_tokens,
-            _status=status.HTTP_200_OK if self.status == 200 else status.HTTP_500_INTERNAL_SERVER_ERROR
+            _status=status.HTTP_200_OK if self.status == 200 else status.HTTP_500_INTERNAL_SERVER_ERROR,
+            other_params={'answer_list': answer_texts}
        )

     def run_stream(self, current_node, node_result_future, language='zh'):
         """

Notes:

  • Replace params[...] with actual context-dependent parameter names from your application.
  • Ensure _status is correctly passed along if necessary, possibly by modifying self.params before passing to to_block_response.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

There doesn't appear to be any significant errors in the code provided. However, there are some minor optimizations for readability and performance:

  1. Reduce Function Optimization: The reduce function is not necessary here because you are already working with lists of answers directly. You can simplify the line where answer_list is created.

  2. Code Style Improvements: Ensure consistent spacing around operators and keywords (e.g., using spaces after commas and colons).

Here's an optimized version of the code based on these improvements:

def run_block(self, language='zh'):
    answer_text = '\n\n'.join('\n\n'.join(a.get('content') for a in answer) for answer in answer_text_list)
    self.work_flow_post_handler.handler(
        self.params['chat_id'],
        self.params['chat_record_id'],
        answer_text,
        self
    )
    # Return response without modifying the original text
    return self.base_to_response.to_block_response(
        self.params['chat_id'],
        self.params['chat_record_id'],
        answer_text,
        True,
        message_tokens,
        answer_tokens,
        _status=status.HTTP_200_OK if self.status == 200 else status.HTTP_500_INTERNAL_SERVER_ERROR,
        other_params={'answer_list': answer_text_list}
    )

def run_stream(self, current_node, node_result_future, language='zh'):
    """

These changes improve both readability and maintainability while removing unnecessary steps. Let me know if you need further adjustments!

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

There is an issue with the line self.answer_list = reduce(lambda pre, _n: [*pre, *_n], answer_text_list, []). This will raise a TypeError because you can't use unpacking (*) directly on strings in Python. A more appropriate approach would be to join the list of answers into a single string before reducing it. Here's the corrected version:

answer_list = ','.join(answer.get('content') for answer in answer_text_list)

# Then, proceed with the rest of the code...

If you need to maintain the list format after joining, consider converting it back to a list before using reduce:

answer_list = [answer.get('content') for answer in answer_text_list]

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

This code looks mostly correct, but there are a few minor points that can potentially be improved:

  1. String Formatting: Consider using an f-string for more readable formatting of the answer_text variable.

    answer_text = '\n\n'.join(
        '\n\n'.join([a.get('content') for a in answer]) for answer in answer_text_list).rstrip()
  2. Variable Naming Consistency: The name _n is used in the list comprehension inside the call to reduce, which suggests that you plan to use it as a counter. It's good practice to avoid using underscores at the start of variable names unless they are part of a specific naming convention (e.g., loop variables).

  3. Return Statement Clarity: Ensure that the return statement includes all required parameters even if some are optional or default values.

Here’s the updated code with these improvements:

@@ -337,13 +337,15 @@ def run_block(self, language='zh'):
         answer_text = '\n\n'.join(
             '\n\n'.join(a.get('content') for a in answer) for answer in answer_text_list).rstrip()
+
+        # Using enumerate to get both index and value for better readability
+        answer_list = [item for item in answer_text_list]
         
         self.work_flow_post_handler.handler(self.params['chat_id'], self.params['chat_record_id'],
                                             answer_text,
                                             self)
         return (
             self.base_to_response.to_block_response(self.params['chat_id'],
                                                     self.params['chat_record_id'], answer_text, True,
-                                                    message_tokens, answer_tokens,
-                                                    _status=status.HTTP_200_OK if self.status == 200 else status.HTTP_500_INTERNAL_SERVER_ERROR))
+                                                    message_tokens, answer_tokens,
+                                                    _status=status.HTTP_200_OK if self.status == 200 else status.HTTP_500_INTERNAL_SERVER_ERROR,
+                                                    other_params={'answer_list': answer_list})
 
     async def run_stream(self, current_node, node_result_future, language='zh'):
         """

These changes will improve the legibility, consistency, and possibly performance slightly, especially when dealing with larger data sets. However, for production-level applications, additional optimizations like caching or parallel processing might be considered depending on the workload.

Expand Down