Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use CREWAI_PROMPT_FILE environment variable. #777

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 7 additions & 3 deletions src/crewai/utilities/i18n.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,9 @@ class I18N(BaseModel):
_prompts: Dict[str, Dict[str, str]] = PrivateAttr()
prompt_file: Optional[str] = Field(
default=None,
description="Path to the prompt_file file to load",
description="Path to the prompt_file file to load. "
"If not provided, $CREWAI_PROMPT_FILE will be checked. "
"If also not set, uses the default prompts"
)

@model_validator(mode="after")
Expand All @@ -20,8 +22,10 @@ def load_prompts(self) -> "I18N":
with open(self.prompt_file, "r") as f:
self._prompts = json.load(f)
else:
dir_path = os.path.dirname(os.path.realpath(__file__))
prompts_path = os.path.join(dir_path, "../translations/en.json")
prompts_path = os.environ.get("CREWAI_PROMPT_FILE")
if not prompts_path:
dir_path = os.path.dirname(os.path.realpath(__file__))
prompts_path = os.path.join(dir_path, "../translations/en.json")

with open(prompts_path, "r") as f:
self._prompts = json.load(f)
Expand Down
21 changes: 21 additions & 0 deletions tests/utilities/test_i18n.py
Original file line number Diff line number Diff line change
Expand Up @@ -42,3 +42,24 @@ def test_prompt_file():
i18n.load_prompts()
assert isinstance(i18n.retrieve("slices", "role_playing"), str)
assert i18n.retrieve("slices", "role_playing") == "Lorem ipsum dolor sit amet"


def test_prompt_file_env():
import os

path = os.path.join(os.path.dirname(__file__), "prompts.json")
old_env = os.environ.get("CREWAI_PROMPT_FILE")
try:
os.environ["CREWAI_PROMPT_FILE"] = path
i18n = I18N()
i18n.load_prompts()
assert i18n.retrieve("slices", "role_playing") == "Lorem ipsum dolor sit amet"
finally:
if old_env:
os.environ["CREWAI_PROMPT_FILE"] = old_env
else:
del os.environ["CREWAI_PROMPT_FILE"]

i18n = I18N()
i18n.load_prompts()
assert i18n.retrieve("slices", "role_playing") != "Lorem ipsum dolor sit amet"
Loading