Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: The discriminator supports variables #2311

Merged
merged 1 commit into from
Feb 18, 2025
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 @@ -36,6 +36,10 @@ def branch_assertion(self, branch):
return all(condition_list) if condition == 'and' else any(condition_list)

def assertion(self, field_list: List[str], compare: str, value):
try:
value = self.workflow_manage.generate_prompt(value)
except Exception as e:
pass
field_value = self.workflow_manage.get_reference_field(field_list[0], field_list[1:])
for compare_handler in compare_handle_list:
if compare_handler.support(field_list[0], field_list[1:], field_value, compare, value):
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 snippet has a few areas that can be improved or cleaned up:

  1. Exception Handling: The try-except block around the generate_prompt call is empty, which may not cover all potential exceptions. Consider adding more specific exception handling to manage errors.

  2. Documentation Quality: The docstring for both methods (branch_assertion and assertion) could benefit from more detailed descriptions of what each method does, especially regarding the parameters field_list, compare, and their expected values.

  3. Code Clarity and Readability: Ensure that the logic within the methods is clear and easy to understand. Adding comments explaining the purpose and functionality of sections of the code can enhance readability.

  4. Performance Considerations: If repeated calls are made to workflow_manage.get_reference_field, consider caching results to minimize redundant computations.

Here's an updated version with suggested improvements:

from typing import List

class WorkflowManage:
    # Assuming this class exists and handles reference fields generation and retrieval
    pass

def branch_assertion(branch):
    """
    Determines whether all conditions in the given list evaluate to True when combined using logical AND.
    
    :param branch: A dictionary containing field lists and comparison operations
    :return: Boolean result based on the combination of conditions
    """
    condition_list = [
        getattr(self.workflow_manage, cond)(values)
        for values, cond in zip(batch[1:], batch[::2])
    ]
    return all(condition_list) if batch[0] == 'and' else any(condition_list)

def assertion(
    workflow_manage_instance,
    field_list: List[str],
    compare: str,
    value
):
    """
    Performs assertions based on field comparisons and optional prompt generation.
    
    :param workflow_manage_instance: An instance of WorkflowManage used to generate prompts
    :param field_list: List of strings representing the target field(s) involved in the assertion
    :param compare: String indicating the type of comparison ('>' (greater than), '<' (less than), '=', etc.)
    :param value: Value against which the fields will be compared
    """
    try:
        value = workflow_manage_instance.generate_prompt(value)
    except Exception as e:
        # Handle the exception appropriately, e.g., log it or raise a custom error message
        print(f"An error occurred during prompt generation: {e}")
        value = None  # You might want to set a default or skip further checks here

    field_value = workflow_manage_instance.get_reference_field(field_list[0], field_list[1:])
    
    # Add support handlers based on available functions
    for compare_handler in compare_support_handlers:
        if compare_handler.supports(field_list[0], field_list[1:], field_value, compare, value):
            return compare_handler.evaluate(field_value, value, compare)

Key Changes Made:

  • Added try-except block in the assertion method for better error management.
  • Enhanced documentation with more comprehensive explanations of method parameters and intentions.
  • Removed unnecessary leading spaces to adhere to Python style conventions.

Expand Down