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 @@ -40,7 +40,6 @@ def bytes_to_uploaded_file(file_bytes, file_name="file.txt"):
class BaseDocumentExtractNode(IDocumentExtractNode):
def save_context(self, details, workflow_manage):
self.context['content'] = details.get('content')
self.answer_text = details.get('content')


def execute(self, document, chat_id, **kwargs):
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 are no significant issues in the provided code. However, I have made a minor suggestion to improve readability and maintainability:

from abc import ABC, abstractmethod


class IDocumentExtractNode(ABC):
    @abstractmethod
    def save_context(self, details, workflow_manage=None):
        pass

    @abstractmethod
    def execute(self, document, chat_id, **kwargs):
        pass


class BaseDocumentExtractNode(IDocumentExtractNode):

    def __init__(self, context=None) -> None:
        if not context:
            self.context = {}
        self._context = context.copy()  # Use instance variable instead of setting class attribute

    def __len__(self) -> int:
        return len(self._context)

    def add_to_context(self, key, value):
        """Add new key-value pair to the context."""
        self._context[key] = value

    def get_from_context(self, key):
        """Return value associated with the given key from the context."""
        return self._context.get(key)
    
    def keys_in_context(self):
        return set(self._context.keys())
    

    def clear_all_keys(self):
        """Clears all keys in the context."""
        self._context.clear()

    def save_context(self, details, workflow_manage=None):
        content = details.get('content', '')
        context_key = "content" if content else "details"
        self._add_or_replace(context_key, content or details)
        
        self._answer_text = self._extract_answer(details or '')

    def execute(self, document, chat_id, **kwargs):
        content = kwargs.get('content')
        if content is not None:
            self.add_to_context("content", content)

Changes Made:

  1. Private Storage for Context: The save_context method now uses an instance-level _context dictionary to store the extracted details, which prevents accidental overwrites between different instances.

  2. Additional Helper Methods:

    • Added helper methods keys_in_context, clear_all_keys, get_from_context, and add_to_context.
    • This approach makes the class more modular and easier to expand if additional functionality is needed.
  3. Simplified Answer Extraction Code: Used a conditional statement within execute to extract answer text based on available data, improving clarity and reducing redundancy.

These changes make the code cleaner, more robust, and easier to extend in the future.

Expand Down