mirror of
https://github.com/ParisNeo/lollms.git
synced 2025-02-01 08:48:19 +00:00
upgraded
This commit is contained in:
parent
9f3b2f606d
commit
d6a102bf2c
@ -1,42 +1,40 @@
|
|||||||
# Lollms function call definition file
|
# Import necessary libraries
|
||||||
# Here you need to import any necessary imports depending on the function requested by the user
|
import re
|
||||||
import markdown
|
|
||||||
from pathlib import Path
|
from pathlib import Path
|
||||||
|
|
||||||
# Partial is useful if we need to preset some parameters
|
|
||||||
from functools import partial
|
from functools import partial
|
||||||
|
|
||||||
# It is advised to import typing elements
|
|
||||||
from typing import List
|
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
|
from ascii_colors import trace_exception
|
||||||
|
|
||||||
# Here is an example of how we install a non installed library using PackageManager
|
# Define the conversion function
|
||||||
if not PackageManager.check_package_installed("markdown2latex"):
|
def markdown_to_latex(file_path: str) -> str:
|
||||||
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:
|
try:
|
||||||
# handle exceptions
|
|
||||||
|
|
||||||
# Load the markdown file
|
# Load the markdown file
|
||||||
markdown_text = Path(file_path).read_text()
|
markdown_text = Path(file_path).read_text()
|
||||||
|
|
||||||
# Convert markdown to latex
|
|
||||||
latex_text = markdown2latex.convert(markdown_text)
|
|
||||||
|
|
||||||
|
# Define conversion rules from markdown to LaTeX
|
||||||
|
conversion_rules = [
|
||||||
|
(r'\\', r'\\textbackslash{}'), # Escape backslashes
|
||||||
|
(r'([#]+) (.*)', lambda m: '\\' + 'sub'*(len(m.group(1))-1) + 'section{' + m.group(2) + '}'), # Convert headings
|
||||||
|
(r'\*\*(.*?)\*\*', r'\\textbf{\1}'), # Bold text
|
||||||
|
(r'\*(.*?)\*', r'\\textit{\1}'), # Italic text
|
||||||
|
(r'\!\[(.*?)\]\((.*?)\)', r'\\begin{figure}[h!]\n\\centering\n\\includegraphics[width=\\textwidth]{\2}\n\\caption{\1}\n\\end{figure}'), # Images
|
||||||
|
(r'\[(.*?)\]\((.*?)\)', r'\\href{\2}{\1}'), # Links
|
||||||
|
(r'`([^`]*)`', r'\\texttt{\1}'), # Inline code
|
||||||
|
(r'^```\s*([a-z]*)\s*\n([\s\S]*?)\n```', r'\\begin{verbatim}\2\\end{verbatim}'), # Code blocks
|
||||||
|
(r'^-\s+(.*)', r'\\begin{itemize}\n\\item \1\n\\end{itemize}'), # Unordered lists
|
||||||
|
(r'^\d+\.\s+(.*)', r'\\begin{enumerate}\n\\item \1\n\\end{enumerate}'), # Ordered lists
|
||||||
|
(r'^>(.*)', r'\\begin{quote}\1\\end{quote}'), # Block quotes
|
||||||
|
]
|
||||||
|
|
||||||
|
# Apply conversion rules
|
||||||
|
latex_text = markdown_text
|
||||||
|
for pattern, replacement in conversion_rules:
|
||||||
|
latex_text = re.sub(pattern, replacement, latex_text, flags=re.MULTILINE)
|
||||||
|
|
||||||
# Define output file path
|
# Define output file path
|
||||||
output_path = Path(file_path).with_suffix('.tex')
|
output_path = Path(file_path).with_suffix('.tex')
|
||||||
|
|
||||||
# Save the latex text to a file
|
# Save the LaTeX text to a file
|
||||||
output_path.write_text(latex_text)
|
output_path.write_text(latex_text)
|
||||||
|
|
||||||
# Finally we return the path to the LaTeX file
|
# Finally we return the path to the LaTeX file
|
||||||
@ -44,33 +42,11 @@ def markdown_file_to_latex(file_path: str) -> str:
|
|||||||
except Exception as e:
|
except Exception as e:
|
||||||
return trace_exception(e)
|
return trace_exception(e)
|
||||||
|
|
||||||
# Function to convert markdown string to LaTeX string
|
# Metadata function
|
||||||
def markdown_string_to_latex(markdown_text: str) -> str:
|
def markdown_to_latex_function():
|
||||||
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 {
|
return {
|
||||||
"function_name": "markdown_file_to_latex", # The function name in string
|
"function_name": "markdown_to_latex", # The function name in string
|
||||||
"function": markdown_file_to_latex, # The function to be called
|
"function": markdown_to_latex, # The function to be called
|
||||||
"function_description": "Converts a markdown file to a LaTeX file.", # Description
|
"function_description": "Converts a markdown file to a LaTeX file.", # Description
|
||||||
"function_parameters": [{"name": "file_path", "type": "str"}] # The set of parameters
|
"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
|
|
||||||
}
|
}
|
||||||
|
@ -18,7 +18,7 @@ from pathlib import Path
|
|||||||
import json
|
import json
|
||||||
|
|
||||||
# Import markdown2latex
|
# Import markdown2latex
|
||||||
from lollms.functions.markdown2latex import markdown_file_to_latex
|
from lollms.functions.markdown2latex import markdown_to_latex
|
||||||
from lollms.functions.file_manipulation import change_file_extension
|
from lollms.functions.file_manipulation import change_file_extension
|
||||||
|
|
||||||
# Define the core functions
|
# Define the core functions
|
||||||
@ -86,9 +86,7 @@ def start_writing_story(prompt_ideas: str, llm: Any, story_file_path: str, build
|
|||||||
if build_latex:
|
if build_latex:
|
||||||
llm.step_start("Building latex file")
|
llm.step_start("Building latex file")
|
||||||
import subprocess
|
import subprocess
|
||||||
latex = markdown_file_to_latex(story_file_path)
|
tex_file = markdown_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
|
# Determine the pdflatex command based on the provided or default path
|
||||||
if llm.config.pdf_latex_path:
|
if llm.config.pdf_latex_path:
|
||||||
pdflatex_command = llm.config.pdf_latex_path
|
pdflatex_command = llm.config.pdf_latex_path
|
||||||
@ -156,7 +154,7 @@ def write_story_section(prompt_ideas: str, llm: Any, story_file_path: str, story
|
|||||||
f"{start_header_id_template}story_section_writer{end_header_id_template}"
|
f"{start_header_id_template}story_section_writer{end_header_id_template}"
|
||||||
]
|
]
|
||||||
)
|
)
|
||||||
new_section += llm.fast_gen(prompt)
|
new_section += llm.fast_gen(prompt, callback=llm.sink)
|
||||||
|
|
||||||
# Write the new section to the story file
|
# Write the new section to the story file
|
||||||
story_path.write_text(new_section)
|
story_path.write_text(new_section)
|
||||||
@ -186,7 +184,7 @@ def write_story_section(prompt_ideas: str, llm: Any, story_file_path: str, story
|
|||||||
]
|
]
|
||||||
)
|
)
|
||||||
new_section = f"## {current_section}\n\n"
|
new_section = f"## {current_section}\n\n"
|
||||||
new_section += llm.fast_gen(prompt).strip()
|
new_section += llm.fast_gen(prompt, callback=llm.sink).strip()
|
||||||
|
|
||||||
# Append the new section to the story file
|
# Append the new section to the story file
|
||||||
story_path.write_text(story_content + "\n" + new_section)
|
story_path.write_text(story_content + "\n" + new_section)
|
||||||
|
Loading…
x
Reference in New Issue
Block a user