fix: extract_frontmatter issue

This commit is contained in:
Timothy J. Baek 2024-09-05 20:35:58 +02:00
parent c9107fa87f
commit b35bbaade2

View File

@ -11,9 +11,9 @@ from open_webui.apps.webui.models.tools import Tools
from open_webui.config import FUNCTIONS_DIR, TOOLS_DIR from open_webui.config import FUNCTIONS_DIR, TOOLS_DIR
def extract_frontmatter(file_path): def extract_frontmatter(content):
""" """
Extract frontmatter as a dictionary from the specified file path. Extract frontmatter as a dictionary from the provided content string.
""" """
frontmatter = {} frontmatter = {}
frontmatter_started = False frontmatter_started = False
@ -21,15 +21,14 @@ def extract_frontmatter(file_path):
frontmatter_pattern = re.compile(r"^\s*([a-z_]+):\s*(.*)\s*$", re.IGNORECASE) frontmatter_pattern = re.compile(r"^\s*([a-z_]+):\s*(.*)\s*$", re.IGNORECASE)
try: try:
with open(file_path, "r", encoding="utf-8") as file: lines = content.splitlines()
first_line = file.readline() if len(lines) < 1 or lines[0].strip() != '"""':
if first_line.strip() != '"""': # The content doesn't start with triple quotes
# The file doesn't start with triple quotes
return {} return {}
frontmatter_started = True frontmatter_started = True
for line in file: for line in lines[1:]:
if '"""' in line: if '"""' in line:
if frontmatter_started: if frontmatter_started:
frontmatter_ended = True frontmatter_ended = True
@ -41,9 +40,6 @@ def extract_frontmatter(file_path):
key, value = match.groups() key, value = match.groups()
frontmatter[key.strip()] = value.strip() frontmatter[key.strip()] = value.strip()
except FileNotFoundError:
print(f"Error: The file {file_path} does not exist.")
return {}
except Exception as e: except Exception as e:
print(f"An error occurred: {e}") print(f"An error occurred: {e}")
return {} return {}