new functions

This commit is contained in:
Saifeddine ALOUI 2024-06-01 21:18:15 +02:00
parent 5af8f77379
commit efcded6b2d
3 changed files with 168 additions and 4 deletions

View File

@ -0,0 +1,53 @@
# Lollms function call definition file
# Here you need to import any necessary imports depending on the function requested by the user
from functools import partial
from typing import List
from lollms.utilities import PackageManager
from ascii_colors import trace_exception
# Ensure the required package is installed
if not PackageManager.check_package_installed("pathlib"):
PackageManager.install_package("pathlib")
# Importing pathlib
from pathlib import Path
# Function to change the file extension
def change_file_extension(file_path: str, new_extension: str) -> str:
"""
Change the extension of a given file path.
Parameters:
file_path (str): The original file path.
new_extension (str): The new extension, including the leading dot (e.g., '.txt').
Returns:
str: The new file path with the changed extension.
Example:
>>> change_file_extension("example.docx", ".txt")
'example.txt'
"""
try:
# Create a Path object from the file path
path = Path(file_path)
# Change the extension
new_path = path.with_suffix(new_extension)
# Return the new path as a string
return str(new_path)
except Exception as e:
return trace_exception(e)
# Metadata function
def change_file_extension_function():
return {
"function_name": "change_file_extension", # The function name in string
"function": change_file_extension, # The function to be called
"function_description": "Takes a file path and a new extension, then returns the new path with the changed extension.", # Description of the function
"function_parameters": [
{"name": "file_path", "type": "str"},
{"name": "new_extension", "type": "str"}
] # The set of parameters
}

View File

@ -0,0 +1,76 @@
# Lollms function call definition file
# Here you need to import any necessary imports depending on the function requested by the user
import markdown
from pathlib import Path
# Partial is useful if we need to preset some parameters
from functools import partial
# It is advised to import typing elements
from typing import List
# Import PackageManager if there are potential libraries that need to be installed
from lollms.utilities import PackageManager
# ascii_colors offers advanced console coloring and bug tracing
from ascii_colors import trace_exception
# Here is an example of how we install a non installed library using PackageManager
if not PackageManager.check_package_installed("markdown2latex"):
PackageManager.install_package("markdown2latex")
# now we can import the library
import markdown2latex
# Function to convert markdown file to LaTeX file
def markdown_file_to_latex(file_path: str) -> str:
try:
# handle exceptions
# Load the markdown file
markdown_text = Path(file_path).read_text()
# Convert markdown to latex
latex_text = markdown2latex.convert(markdown_text)
# Define output file path
output_path = Path(file_path).with_suffix('.tex')
# Save the latex text to a file
output_path.write_text(latex_text)
# Finally we return the path to the LaTeX file
return str(output_path)
except Exception as e:
return trace_exception(e)
# Function to convert markdown string to LaTeX string
def markdown_string_to_latex(markdown_text: str) -> str:
try:
# handle exceptions
# Convert markdown to latex
latex_text = markdown2latex.convert(markdown_text)
# Finally we return the LaTeX text
return latex_text
except Exception as e:
return trace_exception(e)
# Metadata function for markdown_file_to_latex
def markdown_file_to_latex_function():
return {
"function_name": "markdown_file_to_latex", # The function name in string
"function": markdown_file_to_latex, # The function to be called
"function_description": "Converts a markdown file to a LaTeX file.", # Description
"function_parameters": [{"name": "file_path", "type": "str"}] # The set of parameters
}
# Metadata function for markdown_string_to_latex
def markdown_string_to_latex_function():
return {
"function_name": "markdown_string_to_latex", # The function name in string
"function": markdown_string_to_latex, # The function to be called
"function_description": "Converts a markdown string to a LaTeX string.", # Description
"function_parameters": [{"name": "markdown_text", "type": "str"}] # The set of parameters
}

View File

@ -17,8 +17,12 @@ from typing import Any, List, Dict
from pathlib import Path from pathlib import Path
import json import json
# Import markdown2latex
from lollms.functions.markdown2latex import markdown_file_to_latex
from lollms.functions.file_manipulation import change_file_extension
# Define the core functions # Define the core functions
def start_writing_story(prompt_ideas: str, llm: Any, story_file_path: str) -> str: def start_writing_story(prompt_ideas: str, llm: Any, story_file_path: str, build_latex:bool=False) -> str:
discussion_prompt_separator = llm.config.discussion_prompt_separator discussion_prompt_separator = llm.config.discussion_prompt_separator
start_header_id_template = llm.config.start_header_id_template start_header_id_template = llm.config.start_header_id_template
end_header_id_template = llm.config.end_header_id_template end_header_id_template = llm.config.end_header_id_template
@ -26,6 +30,7 @@ def start_writing_story(prompt_ideas: str, llm: Any, story_file_path: str) -> st
system_message_template = llm.config.system_message_template system_message_template = llm.config.system_message_template
try: try:
llm.step_start("Building the story architecture")
# Step 1: Generate the story plan in JSON format # Step 1: Generate the story plan in JSON format
plan_prompt = "\n".join([ plan_prompt = "\n".join([
f"{start_header_id_template}{system_message_template}{end_header_id_template}", f"{start_header_id_template}{system_message_template}{end_header_id_template}",
@ -47,7 +52,8 @@ def start_writing_story(prompt_ideas: str, llm: Any, story_file_path: str) -> st
] ]
) )
plan_response = llm.fast_gen(plan_prompt).strip() plan_response = llm.fast_gen(plan_prompt).strip()
llm.step_end("Building the story architecture")
llm.chunk("\n")
# Extract JSON code block from the response # Extract JSON code block from the response
code_blocks = llm.extract_code_blocks(plan_response) code_blocks = llm.extract_code_blocks(plan_response)
if not code_blocks: if not code_blocks:
@ -61,6 +67,8 @@ def start_writing_story(prompt_ideas: str, llm: Any, story_file_path: str) -> st
final_story_content = "" final_story_content = ""
for section in story_plan["sections"]: for section in story_plan["sections"]:
llm.step_start(f"Building section: {section["section_name"]}")
section_name = section["section_name"] section_name = section["section_name"]
section_description = section["section_description"] section_description = section["section_description"]
@ -73,22 +81,49 @@ def start_writing_story(prompt_ideas: str, llm: Any, story_file_path: str) -> st
) )
final_story_content += f"\n## {section_name}\n\n{new_section}\n" final_story_content += f"\n## {section_name}\n\n{new_section}\n"
llm.step_end(f"Building section: {section["section_name"]}")
if build_latex:
llm.step_start("Building latex file")
import subprocess
latex = markdown_file_to_latex(story_file_path)
tex_file = Path(change_file_extension(story_file_path,".tex"))
tex_file.write_text(latex)
# Determine the pdflatex command based on the provided or default path
if llm.config.pdf_latex_path:
pdflatex_command = llm.config.pdf_latex_path
else:
pdflatex_command = 'pdflatex'
# Set the execution path to the folder containing the tmp_file
execution_path = tex_file.parent
# Execute the Python code in a temporary file.
process = subprocess.Popen(
[pdflatex_command, "-interaction=nonstopmode", str(tex_file)],
stdout=subprocess.PIPE,
stderr=subprocess.PIPE,
cwd=execution_path
)
process.wait()
llm.step_end("Building latex file")
return final_story_content.strip() return final_story_content.strip()
except Exception as e: except Exception as e:
return trace_exception(e) return trace_exception(e)
# Define the metadata functions # Define the metadata functions
def start_writing_story_function(llm, story_file_path): def start_writing_story_function(llm, story_file_path, build_latex:bool=False):
return { return {
"function_name": "start_writing_story", "function_name": "start_writing_story",
"function": partial(start_writing_story, llm=llm, story_file_path=story_file_path), "function": partial(start_writing_story, llm=llm, story_file_path=story_file_path, build_latex=build_latex),
"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_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": [ "function_parameters": [
{"name": "prompt_ideas", "type": "str"} {"name": "prompt_ideas", "type": "str"}
] ]
} }
# Define the core function # Define the core function
def write_story_section(prompt_ideas: str, llm: Any, story_file_path: str, story_plan: dict, current_section: str) -> str: def write_story_section(prompt_ideas: str, llm: Any, story_file_path: str, story_plan: dict, current_section: str) -> str:
discussion_prompt_separator = llm.config.discussion_prompt_separator discussion_prompt_separator = llm.config.discussion_prompt_separator