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: enable context flag and add context paragraph limit on chatgpt … #420

Merged
merged 2 commits into from
Aug 14, 2024

Conversation

mkXultra
Copy link
Collaborator

  1. A new constant CONTEXT_PARAGRAPH_LIMIT has been added with a default value of 3.

  2. The ChatGPTAPI constructor now accepts two new parameters:

    • context_flag: A boolean to enable/disable context preservation.
    • context_paragraph_limit: An integer to set the maximum number of paragraphs to keep in context.
  3. New instance variables have been added to store context:

    • self.context_flag
    • self.context_list
    • self.context_translated_list
    • self.context_paragraph_limit
  4. The create_chat_completion method has been modified to include context in the messages when context_flag is True.

  5. A new save_context method has been added to manage the context lists, ensuring they don't exceed the specified limit.

  6. The get_translation method now calls save_context when context_flag is True.

@@ -36,6 +36,7 @@
"gpt-4o-mini",
"gpt-4o-mini-2024-07-18",
]
CONTEXT_PARAGRAPH_LIMIT = 3
Copy link
Owner

Choose a reason for hiding this comment

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

why we use global var here since it can set by user.

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

@yihong0618

Thank you for your question about the global variable CONTEXT_PARAGRAPH_LIMIT.
This global variable is used because it serves as a default value across multiple classes in the system, particularly in the EPUBBookLoader class.

The EPUBBookLoader class initializes context_paragraph_limit with a default value of 0:

python
class EPUBBookLoader(BaseBookLoader):
def init(
self,
# ... other parameters ...
context_paragraph_limit=0,
):
# ...

When this value (0) is passed to the ChatGPTAPI class, it triggers the use of the global CONTEXT_PARAGRAPH_LIMIT as a fallback:

python
if context_paragraph_limit > 0:
self.context_paragraph_limit = context_paragraph_limit
else:
self.context_paragraph_limit = CONTEXT_PARAGRAPH_LIMIT

This design allows for a consistent default value across the application while still providing flexibility for users to override it when necessary. It also maintains backwards compatibility with existing code that may not explicitly set this parameter.

That being said, if you have any suggestions for a better implementation that maintains the current functionality while reducing global variable usage, I'd be more than happy to consider and potentially implement it. I appreciate your keen eye for code quality and welcome any ideas that could enhance the codebase.

Copy link
Owner

Choose a reason for hiding this comment

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

make sense thank you very much for the contribution and the kind explanation

@yihong0618 yihong0618 merged commit 8253816 into yihong0618:main Aug 14, 2024
2 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants