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
Merged
Show file tree
Hide file tree
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
7 changes: 7 additions & 0 deletions book_maker/cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -279,6 +279,13 @@ def main():
action="store_true",
help="adds an additional paragraph for global, updating historical context of the story to the model's input, improving the narrative consistency for the AI model (this uses ~200 more tokens each time)",
)
parser.add_argument(
"--context_paragraph_limit",
dest="context_paragraph_limit",
type=int,
default=0,
help="if use --use_context, set context paragraph limit",
)
parser.add_argument(
"--temperature",
type=float,
Expand Down
2 changes: 2 additions & 0 deletions book_maker/loader/epub_loader.py
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ def __init__(
single_translate=False,
context_flag=False,
temperature=1.0,
context_paragraph_limit=0,
):
self.epub_name = epub_name
self.new_epub = epub.EpubBook()
Expand All @@ -41,6 +42,7 @@ def __init__(
language,
api_base=model_api_base,
context_flag=context_flag,
context_paragraph_limit=context_paragraph_limit,
temperature=temperature,
**prompt_config_to_kwargs(prompt_config),
)
Expand Down
34 changes: 33 additions & 1 deletion book_maker/translator/chatgptapi_translator.py
Original file line number Diff line number Diff line change
Expand Up @@ -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



class ChatGPTAPI(Base):
Expand All @@ -49,6 +50,8 @@ def __init__(
prompt_template=None,
prompt_sys_msg=None,
temperature=1.0,
context_flag=False,
context_paragraph_limit=0,
**kwargs,
) -> None:
super().__init__(key, language)
Expand All @@ -73,6 +76,15 @@ def __init__(
self.deployment_id = None
self.temperature = temperature
self.model_list = None
self.context_flag = context_flag
self.context_list = []
self.context_translated_list = []
if context_paragraph_limit > 0:
# not set by user, use default
self.context_paragraph_limit = context_paragraph_limit
else:
# set by user, use user's value
self.context_paragraph_limit = CONTEXT_PARAGRAPH_LIMIT

def rotate_key(self):
self.openai_client.api_key = next(self.keys)
Expand All @@ -87,8 +99,16 @@ def create_chat_completion(self, text):
sys_content = self.system_content or self.prompt_sys_msg.format(crlf="\n")
messages = [
{"role": "system", "content": sys_content},
{"role": "user", "content": content},
]
if self.context_flag:
messages.append({"role": "user", "content": "\n".join(self.context_list)})
messages.append(
{
"role": "assistant",
"content": "\n".join(self.context_translated_list),
}
)
messages.append({"role": "user", "content": content})

completion = self.openai_client.chat.completions.create(
model=self.model,
Expand All @@ -110,8 +130,20 @@ def get_translation(self, text):
else:
t_text = ""

if self.context_flag:
self.save_context(text, t_text)

return t_text

def save_context(self, text, t_text):
if self.context_paragraph_limit > 0:
self.context_list.append(text)
self.context_translated_list.append(t_text)
# Remove the oldest context
if len(self.context_list) > self.context_paragraph_limit:
self.context_list.pop(0)
self.context_translated_list.pop(0)

def translate(self, text, needprint=True):
start_time = time.time()
# todo: Determine whether to print according to the cli option
Expand Down
Loading