mirror of
https://github.com/crewAIInc/crewAI.git
synced 2026-05-03 08:12:39 +00:00
adding initial support for external prompt file
This commit is contained in:
@@ -6,40 +6,32 @@ from pydantic import BaseModel, Field, PrivateAttr, ValidationError, model_valid
|
||||
|
||||
|
||||
class I18N(BaseModel):
|
||||
_translations: Dict[str, Dict[str, str]] = PrivateAttr()
|
||||
language_file: Optional[str] = Field(
|
||||
_prompts: Dict[str, Dict[str, str]] = PrivateAttr()
|
||||
prompt_file: Optional[str] = Field(
|
||||
default=None,
|
||||
description="Path to the translation file to load",
|
||||
)
|
||||
language: Optional[str] = Field(
|
||||
default="en",
|
||||
description="Language used to load translations",
|
||||
description="Path to the prompt_file file to load",
|
||||
)
|
||||
|
||||
@model_validator(mode="after")
|
||||
def load_translation(self) -> "I18N":
|
||||
"""Load translations from a JSON file based on the specified language."""
|
||||
def load_prompts(self) -> "I18N":
|
||||
"""Load prompts from a JSON file."""
|
||||
try:
|
||||
if self.language_file:
|
||||
with open(self.language_file, "r") as f:
|
||||
self._translations = json.load(f)
|
||||
if self.prompt_file:
|
||||
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, f"../translations/{self.language}.json"
|
||||
)
|
||||
prompts_path = os.path.join(dir_path, f"../translations/en.json")
|
||||
|
||||
with open(prompts_path, "r") as f:
|
||||
self._translations = json.load(f)
|
||||
self._prompts = json.load(f)
|
||||
except FileNotFoundError:
|
||||
raise ValidationError(
|
||||
f"Translation file for language '{self.language}' not found."
|
||||
)
|
||||
raise ValidationError(f"Prompt file '{self.prompt_file}' not found.")
|
||||
except json.JSONDecodeError:
|
||||
raise ValidationError(f"Error decoding JSON from the prompts file.")
|
||||
|
||||
if not self._translations:
|
||||
self._translations = {}
|
||||
if not self._prompts:
|
||||
self._prompts = {}
|
||||
|
||||
return self
|
||||
|
||||
@@ -54,6 +46,6 @@ class I18N(BaseModel):
|
||||
|
||||
def retrieve(self, kind, key) -> str:
|
||||
try:
|
||||
return self._translations[kind][key]
|
||||
return self._prompts[kind][key]
|
||||
except:
|
||||
raise ValidationError(f"Translation for '{kind}':'{key}' not found.")
|
||||
raise ValidationError(f"Prompt for '{kind}':'{key}' not found.")
|
||||
|
||||
@@ -7,7 +7,7 @@ from crewai.utilities import I18N
|
||||
|
||||
|
||||
class Prompts(BaseModel):
|
||||
"""Manages and generates prompts for a generic agent with support for different languages."""
|
||||
"""Manages and generates prompts for a generic agent."""
|
||||
|
||||
i18n: I18N = Field(default=I18N())
|
||||
tools: list[Any] = Field(default=[])
|
||||
|
||||
Reference in New Issue
Block a user