mirror of
https://github.com/crewAIInc/crewAI.git
synced 2026-01-11 00:58:30 +00:00
Fix/yaml formatting (#590)
* Bug/curly_braces_yaml Added parser to help users on yaml syntax * context error Patch and later will prioritize this again to have context work with the yaml
This commit is contained in:
@@ -1,6 +1,7 @@
|
|||||||
import inspect
|
import inspect
|
||||||
import os
|
import os
|
||||||
from pathlib import Path
|
from pathlib import Path
|
||||||
|
from crewai.utilities.parser import YamlParser
|
||||||
|
|
||||||
import yaml
|
import yaml
|
||||||
from dotenv import load_dotenv
|
from dotenv import load_dotenv
|
||||||
@@ -40,6 +41,7 @@ def CrewBase(cls):
|
|||||||
@staticmethod
|
@staticmethod
|
||||||
def load_yaml(config_path: str):
|
def load_yaml(config_path: str):
|
||||||
with open(config_path, "r") as file:
|
with open(config_path, "r") as file:
|
||||||
return yaml.safe_load(file)
|
parsedContent = YamlParser.parse(file)
|
||||||
|
return yaml.safe_load(parsedContent)
|
||||||
|
|
||||||
return WrappedClass
|
return WrappedClass
|
||||||
|
|||||||
@@ -6,3 +6,4 @@ from .printer import Printer
|
|||||||
from .prompts import Prompts
|
from .prompts import Prompts
|
||||||
from .rpm_controller import RPMController
|
from .rpm_controller import RPMController
|
||||||
from .fileHandler import FileHandler
|
from .fileHandler import FileHandler
|
||||||
|
from .parser import YamlParser
|
||||||
|
|||||||
17
src/crewai/utilities/parser.py
Normal file
17
src/crewai/utilities/parser.py
Normal file
@@ -0,0 +1,17 @@
|
|||||||
|
import re
|
||||||
|
|
||||||
|
|
||||||
|
class YamlParser:
|
||||||
|
def parse(file):
|
||||||
|
content = file.read()
|
||||||
|
# Replace single { and } with doubled ones, while leaving already doubled ones intact and the other special characters {# and {%
|
||||||
|
modified_content = re.sub(r"(?<!\{){(?!\{)(?!\#)(?!\%)", "{{", content)
|
||||||
|
modified_content = re.sub(
|
||||||
|
r"(?<!\})(?<!\%)(?<!\#)\}(?!})", "}}", modified_content
|
||||||
|
)
|
||||||
|
# Check for 'context:' not followed by '[' and raise an error
|
||||||
|
if re.search(r"context:(?!\s*\[)", modified_content):
|
||||||
|
raise ValueError(
|
||||||
|
"Context is currently only supported in code when creating a task. Please use the 'context' key in the task configuration."
|
||||||
|
)
|
||||||
|
return modified_content
|
||||||
Reference in New Issue
Block a user