Enhanced story writer code to work with multi languages

This commit is contained in:
Saifeddine ALOUI 2024-06-05 00:06:40 +02:00
parent 07d14aa09f
commit 9fb911e1cf
3 changed files with 37 additions and 10 deletions

View File

@ -1055,6 +1055,7 @@ class LollmsApplication(LoLLMsCom):
# Initialize a list to store the full messages
full_message_list = []
full_message = ""
# If this is not a continue request, we add the AI prompt
if not is_continue:
message_tokenized = self.model.tokenize(
@ -1063,6 +1064,7 @@ class LollmsApplication(LoLLMsCom):
full_message_list.append(message_tokenized)
# Update the cumulative number of tokens
tokens_accumulated += len(message_tokenized)
full_message += self.personality.ai_message_prefix.strip()
if generation_type != "simple_question":
@ -1082,8 +1084,9 @@ class LollmsApplication(LoLLMsCom):
msg = f"{separator_template}{start_ai_header_id_template if message.sender_type == SENDER_TYPES.SENDER_TYPES_AI else start_user_header_id_template}{message.sender}{end_ai_header_id_template if message.sender_type == SENDER_TYPES.SENDER_TYPES_AI else end_user_header_id_template}" + message.content.strip()
message_tokenized = self.model.tokenize(msg)
else:
msg_value= f"{separator_template}{start_ai_header_id_template if message.sender_type == SENDER_TYPES.SENDER_TYPES_AI else start_user_header_id_template}{message.sender}{end_ai_header_id_template if message.sender_type == SENDER_TYPES.SENDER_TYPES_AI else end_user_header_id_template}" + message.content.strip()
message_tokenized = self.model.tokenize(
f"{separator_template}{start_ai_header_id_template if message.sender_type == SENDER_TYPES.SENDER_TYPES_AI else start_user_header_id_template}{message.sender}{end_ai_header_id_template if message.sender_type == SENDER_TYPES.SENDER_TYPES_AI else end_user_header_id_template}" + message.content.strip()
msg_value
)
# Check if adding the message will exceed the available space
if tokens_accumulated + len(message_tokenized) > available_space-n_tokens:
@ -1091,6 +1094,7 @@ class LollmsApplication(LoLLMsCom):
msg = message_tokenized[-(available_space-tokens_accumulated-n_tokens):]
tokens_accumulated += available_space-tokens_accumulated-n_tokens
full_message_list.insert(0, msg)
full_message = self.personality.ai_message_prefix.strip()+full_message
break
# Add the tokenized message to the full_message_list
@ -1129,11 +1133,11 @@ class LollmsApplication(LoLLMsCom):
discussion_messages += self.model.detokenize(message_tokens)
if len(full_message_list)>0:
ai_prefix = self.model.detokenize(full_message_list[-1])
ai_prefix = self.personality.ai_message_prefix
else:
ai_prefix = ""
# Build the final prompt by concatenating the conditionning and discussion messages
prompt_data = conditionning + internet_search_results + documentation + knowledge + user_description + discussion_messages + positive_boost + negative_boost + fun_mode + ai_prefix
prompt_data = conditionning + internet_search_results + documentation + knowledge + user_description + discussion_messages + positive_boost + negative_boost + fun_mode + start_ai_header_id_template + ai_prefix + end_ai_header_id_template
# Tokenize the prompt data
tokens = self.model.tokenize(prompt_data)

View File

@ -81,6 +81,7 @@ def start_writing_story(
build_latex:bool=False,
include_summary_between_chapters:bool=False,
allow_illustrations:bool=False,
language=None,
client:Client = None) -> str:
discussion_prompt_separator = llm.config.discussion_prompt_separator
start_header_id_template = llm.config.start_header_id_template
@ -110,7 +111,8 @@ def start_writing_story(
"}\n"
"```",
f"{start_header_id_template}{prompt_ideas}{end_header_id_template}",
f"{prompt_ideas}\n\n"
f"{prompt_ideas}\n\n",
f"{start_header_id_template}current_language{end_header_id_template}\n"+language if language else "",
f"{start_header_id_template}story_architect{end_header_id_template}"
]
)
@ -154,7 +156,8 @@ def start_writing_story(
prompt_ideas=prompt_ideas,
add_illustration=False,
include_summary_between_chapters=include_summary_between_chapters,
client=client
client=client,
language=language
)
section_full["content"]=new_section
final_story_content += f"\n## {section_name}\n\n{new_section}\n"
@ -191,10 +194,18 @@ def start_writing_story(
return trace_exception(e)
# Define the metadata functions
def start_writing_story_function(llm, story_file_path, build_latex:bool=False, include_summary_between_chapters:bool=False, allow_illustrations:bool=False, client:Client=None):
def start_writing_story_function(
llm,
story_file_path,
build_latex:bool=False,
include_summary_between_chapters:bool=False,
allow_illustrations:bool=False,
client:Client=None,
language:str=None
):
return {
"function_name": "start_writing_story",
"function": partial(start_writing_story, llm=llm, story_file_path=story_file_path, build_latex=build_latex, include_summary_between_chapters=include_summary_between_chapters, allow_illustrations=allow_illustrations, client=client),
"function": partial(start_writing_story, llm=llm, story_file_path=story_file_path, build_latex=build_latex, include_summary_between_chapters=include_summary_between_chapters, allow_illustrations=allow_illustrations, client=client, language=language),
"function_description": "Starts writing a story based on the provided prompt ideas, generating a plan in JSON format, and writing the story section by section.",
"function_parameters": [
{"name": "prompt_ideas", "type": "str"}
@ -202,12 +213,23 @@ def start_writing_story_function(llm, story_file_path, build_latex:bool=False, i
}
# Define the core function
def write_story_section(prompt_ideas: str, llm: Any, story_file_path: str, story_plan: dict, current_section: str, add_illustration:bool=False, include_summary_between_chapters:bool=False, client:Client=None) -> str:
def write_story_section(
prompt_ideas: str,
llm: Any,
story_file_path: str,
story_plan: dict,
current_section: str,
add_illustration:bool=False,
include_summary_between_chapters:bool=False,
client:Client=None,
language:str=None
) -> str:
discussion_prompt_separator = llm.config.discussion_prompt_separator
start_header_id_template = llm.config.start_header_id_template
end_header_id_template = llm.config.end_header_id_template
separator_template = llm.config.separator_template
system_message_template = llm.config.system_message_template
system_message_template = llm.config.system_message_template
try:
story_path = Path(story_file_path)
@ -233,6 +255,7 @@ def write_story_section(prompt_ideas: str, llm: Any, story_file_path: str, story
f"{story_plan}",
f"{start_header_id_template}Section to be written{end_header_id_template}",
f"{current_section}",
f"{start_header_id_template}current_language{end_header_id_template}\n"+language if language else "",
f"{start_header_id_template}story_section_writer{end_header_id_template}"
]
)

View File

@ -3373,7 +3373,7 @@ The AI should respond in this format using data from actions_list:
try:
# Assuming parameters is a dictionary that maps directly to the function's arguments.
if type(parameters)==list:
f_parameters ={k['name']:v for k,v in zip([p['name'] for p in fn['function_parameters']],parameters)}
f_parameters ={k:v for k,v in zip([p['name'] for p in fn['function_parameters']],parameters)}
result = function(**f_parameters)
elif type(parameters)==dict:
result = function(**parameters)